small refactoring

master
Vectozavr 2022-02-23 11:51:53 +07:00
parent 8f271b1a67
commit 532990ec94
22 changed files with 286 additions and 57 deletions

View File

@ -5,7 +5,7 @@ set(CMAKE_CXX_STANDARD 20)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")
add_executable(shooter add_executable(${CMAKE_PROJECT_NAME}
# game: # game:
Source.cpp Source.cpp
Player.cpp Player.cpp
@ -64,6 +64,10 @@ add_executable(shooter
engine/Keyboard.h engine/Keyboard.h
engine/Mouse.cpp engine/Mouse.cpp
engine/Mouse.h engine/Mouse.h
engine/SoundController.cpp
engine/SoundController.h
engine/CameraController.cpp
engine/CameraController.h
engine/animation/Animation.h engine/animation/Animation.h
engine/animation/Timeline.cpp engine/animation/Timeline.cpp
engine/animation/Timeline.h engine/animation/Timeline.h
@ -74,6 +78,13 @@ add_executable(shooter
engine/animation/ARotate.h engine/animation/ARotate.h
engine/animation/AWait.h engine/animation/AWait.h
engine/animation/AFunction.h engine/animation/AFunction.h
engine/animation/AAttractToPoint.h
engine/animation/ARotateRelativePoint.h
engine/animation/ARotateLeft.h
engine/animation/Interpolation.cpp
engine/animation/Animations.h
engine/animation/AShowCreation.h
engine/animation/AShowUncreation.h
engine/physics/RigidBody.cpp engine/physics/RigidBody.cpp
engine/physics/RigidBody.h engine/physics/RigidBody.h
engine/physics/Simplex.h engine/physics/Simplex.h
@ -97,12 +108,7 @@ add_executable(shooter
engine/network/UDPConnection.h engine/network/UDPConnection.h
engine/network/UDPSocket.cpp engine/network/UDPSocket.cpp
engine/network/UDPSocket.h engine/network/UDPSocket.h
engine/SoundController.cpp )
engine/SoundController.h
engine/animation/AAttractToPoint.h
engine/animation/ARotateRelativePoint.h
engine/animation/ARotateLeft.h
engine/animation/Interpolation.cpp engine/animation/Animations.h engine/animation/AShowCreation.h)
if(APPLE OR UNIX) if(APPLE OR UNIX)
include_directories(/usr/local/include) include_directories(/usr/local/include)
@ -117,7 +123,7 @@ if (SFML_FOUND)
include_directories(${SFML_INCLUDE_DIR}) include_directories(${SFML_INCLUDE_DIR})
endif() endif()
target_link_libraries(shooter sfml-audio sfml-network sfml-graphics sfml-window sfml-system) target_link_libraries(${CMAKE_PROJECT_NAME} sfml-audio sfml-network sfml-graphics sfml-window sfml-system)
# OpenGL part # OpenGL part
if (APPLE) if (APPLE)
@ -129,12 +135,12 @@ if (APPLE)
set(GLFW_LINK /usr/local/Cellar/glfw/3.2.1/lib/libglfw.3.dylib) set(GLFW_LINK /usr/local/Cellar/glfw/3.2.1/lib/libglfw.3.dylib)
link_libraries(${OPENGL} ${GLEW_LINK} ${GLFW_LINK}) link_libraries(${OPENGL} ${GLEW_LINK} ${GLFW_LINK})
target_link_libraries(shooter "-framework OpenGL") target_link_libraries(${CMAKE_PROJECT_NAME} "-framework OpenGL")
target_link_libraries(shooter "-framework GLUT") target_link_libraries(${CMAKE_PROJECT_NAME} "-framework GLUT")
elseif(UNIX) elseif(UNIX)
find_package(OpenGL REQUIRED) find_package(OpenGL REQUIRED)
find_package(GLUT REQUIRED) find_package(GLUT REQUIRED)
target_link_libraries(shooter ${OPENGL_LIBRARIES}) target_link_libraries(${CMAKE_PROJECT_NAME} ${OPENGL_LIBRARIES})
target_link_libraries(shooter ${GLUT_LIBRARY}) target_link_libraries(${CMAKE_PROJECT_NAME} ${GLUT_LIBRARY})
endif() endif()

View File

@ -89,8 +89,6 @@ void Shooter::start() {
player->reInitWeapons(); player->reInitWeapons();
player->translate(Vec3D{0, 0, 0});
camera->translateToPoint(player->position() + Vec3D{0, 1.8, 0}); camera->translateToPoint(player->position() + Vec3D{0, 1.8, 0});
player->attach(camera); player->attach(camera);
world->addBody(player); world->addBody(player);
@ -159,6 +157,7 @@ void Shooter::update() {
if (keyboard->isKeyTapped(sf::Keyboard::P)) { if (keyboard->isKeyTapped(sf::Keyboard::P)) {
screen->startRender(); screen->startRender();
} }
if (keyboard->isKeyTapped(sf::Keyboard::L)) { if (keyboard->isKeyTapped(sf::Keyboard::L)) {
screen->stopRender(); screen->stopRender();
} }
@ -179,7 +178,6 @@ void Shooter::update() {
} }
void Shooter::gui() { void Shooter::gui() {
sf::Sprite sprite; sf::Sprite sprite;
sprite.setTexture(*ResourceManager::loadTexture(ShooterConsts::MAIN_MENU_GUI)); sprite.setTexture(*ResourceManager::loadTexture(ShooterConsts::MAIN_MENU_GUI));
sprite.setTextureRect(sf::IntRect(243, 3, 9, 9)); sprite.setTextureRect(sf::IntRect(243, 3, 9, 9));
@ -192,7 +190,6 @@ void Shooter::gui() {
// health player stats // health player stats
drawPlayerStats(); drawPlayerStats();
drawStatsTable(); drawStatsTable();
} }
void Shooter::drawStatsTable() { void Shooter::drawStatsTable() {
@ -288,9 +285,6 @@ void Shooter::spawnPlayer(sf::Uint16 id) {
world->body(ObjectNameTag(name + "_foot_2"))->setColor(ShooterConsts::DARK_COLORS[colorFootNum]); world->body(ObjectNameTag(name + "_foot_2"))->setColor(ShooterConsts::DARK_COLORS[colorFootNum]);
changeEnemyWeapon("gun", id); changeEnemyWeapon("gun", id);
newPlayer->scale(Vec3D(0.01, 0.01, 0.01));
Timeline::addAnimation<AScale>(AnimationListTag(name + "_creation"), newPlayer, Vec3D(100, 100, 100));
} }
void Shooter::removePlayer(sf::Uint16 id) { void Shooter::removePlayer(sf::Uint16 id) {
@ -325,6 +319,7 @@ void Shooter::removeFireTrace(const ObjectNameTag &traceName) {
} }
void Shooter::addBonus(const string &bonusName, const Vec3D &position) { void Shooter::addBonus(const string &bonusName, const Vec3D &position) {
std::string name = bonusName.substr(6, bonusName.size() - 3 - 5); std::string name = bonusName.substr(6, bonusName.size() - 3 - 5);
ObjectNameTag nameTag(bonusName); ObjectNameTag nameTag(bonusName);
@ -342,6 +337,7 @@ void Shooter::addBonus(const string &bonusName, const Vec3D &position) {
bonus, Vec3D{0, 2 * Consts::PI, 0}, 4, bonus, Vec3D{0, 2 * Consts::PI, 0}, 4,
Animation::LoopOut::Continue, Animation::LoopOut::Continue,
Animation::InterpolationType::Linear); Animation::InterpolationType::Linear);
} }
void Shooter::removeBonus(const ObjectNameTag &bonusName) { void Shooter::removeBonus(const ObjectNameTag &bonusName) {

View File

@ -10,10 +10,12 @@ using namespace std;
int main() { int main() {
Shooter game; Shooter game;
game.create(1280, 720, ShooterConsts::PROJECT_NAME, true); // Optimal for standard monitors:
//game.create(1920, 1080, ShooterConsts::PROJECT_NAME, true, Consts::BACKGROUND_COLOR); //game.create(720, 480, ShooterConsts::PROJECT_NAME, true);
//game.create(1920, 1080, ShooterConsts::PROJECT_NAME, true, Consts::BACKGROUND_COLOR, sf::Style::Fullscreen);
//game.create(2048, 1152, ShooterConsts::PROJECT_NAME, false); // Optimal for MacBook Pro 16 display:
game.create(2048, 1152, ShooterConsts::PROJECT_NAME, true, Consts::BACKGROUND_COLOR);
//game.create(3072, 1920, ShooterConsts::PROJECT_NAME, true, Consts::BACKGROUND_COLOR); //game.create(3072, 1920, ShooterConsts::PROJECT_NAME, true, Consts::BACKGROUND_COLOR);
return 0; return 0;

View File

@ -0,0 +1,43 @@
//
// Created by Иван Ильин on 22.01.2022.
//
#include "CameraController.h"
#include "utils/Time.h"
#include "Vec2D.h"
CameraController::CameraController(std::shared_ptr<Camera> camera,
std::shared_ptr<Keyboard> keyboard,
std::shared_ptr<Mouse> mouse) :
_camera(std::move(camera)),
_keyboard(std::move(keyboard)),
_mouse(std::move(mouse)){
}
void CameraController::update() {
// Left and right
if (Keyboard::isKeyPressed(sf::Keyboard::A))
_camera->translate(_camera->left()*Time::deltaTime()*5.0);
if (Keyboard::isKeyPressed(sf::Keyboard::D))
_camera->translate(-_camera->left()*Time::deltaTime()*5.0);
// Forward and backward
if (Keyboard::isKeyPressed(sf::Keyboard::W))
_camera->translate(_camera->lookAt()*Time::deltaTime()*5.0);
if (Keyboard::isKeyPressed(sf::Keyboard::S))
_camera->translate(-_camera->lookAt()*Time::deltaTime()*5.0);
if (Keyboard::isKeyPressed(sf::Keyboard::LShift))
_camera->translate(Vec3D{0.0, -Time::deltaTime()*5.0, 0});
if (Keyboard::isKeyPressed(sf::Keyboard::Space))
_camera->translate(Vec3D{0.0, Time::deltaTime()*5.0, 0});
// Mouse movement
Vec2D disp = _mouse->getMouseDisplacement();
_camera->rotate(Vec3D{0, -disp.x()/1000.0, 0});
_camera->rotateLeft(disp.y()/1000.0);
}

26
engine/CameraController.h Normal file
View File

@ -0,0 +1,26 @@
//
// Created by Иван Ильин on 22.01.2022.
//
#ifndef SHOOTER_CAMERACONTROLLER_H
#define SHOOTER_CAMERACONTROLLER_H
#include "Camera.h"
#include "Keyboard.h"
#include "Mouse.h"
class CameraController {
private:
std::shared_ptr<Camera> _camera;
std::shared_ptr<Keyboard> _keyboard;
std::shared_ptr<Mouse> _mouse;
public:
CameraController(std::shared_ptr<Camera> camera,
std::shared_ptr<Keyboard> keyboard,
std::shared_ptr<Mouse> mouse);
void update();
};
#endif //SHOOTER_CAMERACONTROLLER_H

View File

@ -118,13 +118,17 @@ void Engine::printDebugInfo() const {
if (_showDebugInfo) { if (_showDebugInfo) {
// coordinates & fps: // coordinates & fps:
std::string text = _name + "\n\n X: " + std::string text = _name + "\n\n X: " +
std::to_string((camera->position().x())) + "\n Y: " + std::to_string((camera->position().x())) + "\n Y: " +
std::to_string((camera->position().y())) + "\n Z: " + std::to_string((camera->position().y())) + "\n Z: " +
std::to_string((camera->position().z())) + "\n\n" + std::to_string((camera->position().z())) + "\n RY:" +
std::to_string(camera->angle().y()/M_PI) + "PI\n RL: " +
std::to_string(camera->angleLeftUpLookAt().x()/M_PI) + "PI\n\n" +
std::to_string(screen->width()) + "x" + std::to_string(screen->width()) + "x" +
std::to_string(screen->height()) + "\t" + std::to_string(screen->height()) + "\t" +
std::to_string(Time::fps()) + " fps"; std::to_string(Time::fps()) + " fps";
if (_useOpenGL) { if (_useOpenGL) {
text += "\n Using OpenGL acceleration"; text += "\n Using OpenGL acceleration";
} else { } else {

View File

@ -42,13 +42,12 @@ Mesh::Mesh(ObjectNameTag nameTag, const vector<Triangle> &tries) : Object(std::m
void Mesh::setColor(const sf::Color &c) { void Mesh::setColor(const sf::Color &c) {
_color = c; _color = c;
// change color of all mesh triangles:
std::vector<Triangle> newTriangles;
newTriangles.reserve(_tris.size());
for (auto &t : _tris) { for (auto &t : _tris) {
newTriangles.emplace_back(Triangle(t[0], t[1], t[2], c)); t.setColor(c);
} }
setTriangles(std::move(newTriangles));
// because we change the color of mesh we should update geometry with a new color
glFreeFloatArray();
} }
Mesh Mesh
@ -61,15 +60,15 @@ Mesh::LineTo(ObjectNameTag nameTag, const Vec3D &from, const Vec3D &to, double l
Vec3D v3 = v1.cross(v2).normalized(); Vec3D v3 = v1.cross(v2).normalized();
// from plane // from plane
Vec4D p1 = (from - v2 * line_width / 2.0 - v3 * line_width / 2.0).makePoint4D(); Vec4D p1 = (- v2 * line_width / 2.0 - v3 * line_width / 2.0).makePoint4D();
Vec4D p2 = (from - v2 * line_width / 2.0 + v3 * line_width / 2.0).makePoint4D(); Vec4D p2 = (- v2 * line_width / 2.0 + v3 * line_width / 2.0).makePoint4D();
Vec4D p3 = (from + v2 * line_width / 2.0 + v3 * line_width / 2.0).makePoint4D(); Vec4D p3 = ( v2 * line_width / 2.0 + v3 * line_width / 2.0).makePoint4D();
Vec4D p4 = (from + v2 * line_width / 2.0 - v3 * line_width / 2.0).makePoint4D(); Vec4D p4 = ( v2 * line_width / 2.0 - v3 * line_width / 2.0).makePoint4D();
// to plane // to plane
Vec4D p5 = (to - v2 * line_width / 2.0 - v3 * line_width / 2.0).makePoint4D(); Vec4D p5 = (to - from - v2 * line_width / 2.0 - v3 * line_width / 2.0).makePoint4D();
Vec4D p6 = (to - v2 * line_width / 2.0 + v3 * line_width / 2.0).makePoint4D(); Vec4D p6 = (to - from - v2 * line_width / 2.0 + v3 * line_width / 2.0).makePoint4D();
Vec4D p7 = (to + v2 * line_width / 2.0 + v3 * line_width / 2.0).makePoint4D(); Vec4D p7 = (to - from + v2 * line_width / 2.0 + v3 * line_width / 2.0).makePoint4D();
Vec4D p8 = (to + v2 * line_width / 2.0 - v3 * line_width / 2.0).makePoint4D(); Vec4D p8 = (to - from + v2 * line_width / 2.0 - v3 * line_width / 2.0).makePoint4D();
line._tris = std::move(std::vector<Triangle>{ line._tris = std::move(std::vector<Triangle>{
@ -87,10 +86,70 @@ Mesh::LineTo(ObjectNameTag nameTag, const Vec3D &from, const Vec3D &to, double l
{p1, p4, p8} {p1, p4, p8}
}); });
line.setColor(color); line.setColor(color);
line.translateToPoint(from);
return line; return line;
} }
Mesh Mesh::ArrowTo(ObjectNameTag nameTag, const Vec3D &from, const Vec3D &to, double line_width, sf::Color color) {
Mesh arrow(std::move(nameTag));
Vec3D v1 = (to - from).normalized();
Vec3D v2 = from.cross(from + Vec3D{1, 0, 0}).normalized();
Vec3D v3 = v1.cross(v2).normalized();
Vec3D to_line = to - v1*0.4;
// from plane
Vec4D p1 = (- v2 * line_width / 2.0 - v3 * line_width / 2.0).makePoint4D();
Vec4D p2 = (- v2 * line_width / 2.0 + v3 * line_width / 2.0).makePoint4D();
Vec4D p3 = ( v2 * line_width / 2.0 + v3 * line_width / 2.0).makePoint4D();
Vec4D p4 = ( v2 * line_width / 2.0 - v3 * line_width / 2.0).makePoint4D();
// to plane
Vec4D p5 = (to_line - from - v2 * line_width / 2.0 - v3 * line_width / 2.0).makePoint4D();
Vec4D p6 = (to_line - from - v2 * line_width / 2.0 + v3 * line_width / 2.0).makePoint4D();
Vec4D p7 = (to_line - from + v2 * line_width / 2.0 + v3 * line_width / 2.0).makePoint4D();
Vec4D p8 = (to_line - from + v2 * line_width / 2.0 - v3 * line_width / 2.0).makePoint4D();
// arrow
Vec4D p9 = (to_line - from - v2 * line_width*2 - v3 * line_width*2).makePoint4D();
Vec4D p10 = (to_line - from - v2 * line_width*2 + v3 * line_width*2).makePoint4D();
Vec4D p11 = (to_line - from + v2 * line_width*2 + v3 * line_width*2).makePoint4D();
Vec4D p12 = (to_line - from + v2 * line_width*2 - v3 * line_width*2).makePoint4D();
Vec4D p13 = (to - from).makePoint4D();
arrow._tris = std::move(std::vector<Triangle>{
{p2, p4, p1},
{p2, p3, p4},
{p1, p6, p2},
{p1, p5, p6},
{p2, p6, p7},
{p2, p7, p3},
{p6, p5, p8},
{p6, p8, p7},
{p4, p3, p7},
{p4, p7, p8},
{p1, p8, p5},
{p1, p4, p8},
{ p9, p10, p13 },
{ p10, p11, p13 },
{ p11, p12, p13 },
{ p12, p9, p13 },
});
arrow.setColor(color);
arrow.translateToPoint(from);
return arrow;
}
void Mesh::setOpacity(double t) {
setColor(sf::Color(_color.r, _color.g, _color.b, t*255));
}
void Mesh::setTriangles(vector<Triangle>&& t) { void Mesh::setTriangles(vector<Triangle>&& t) {
_tris = std::move(t); _tris = std::move(t);
} }

View File

@ -46,6 +46,8 @@ public:
void setColor(const sf::Color &c); void setColor(const sf::Color &c);
void setOpacity(double t);
void setVisible(bool visibility) { _visible = visibility; } void setVisible(bool visibility) { _visible = visibility; }
[[nodiscard]] bool isVisible() const { return _visible; } [[nodiscard]] bool isVisible() const { return _visible; }
@ -55,6 +57,8 @@ public:
Mesh static LineTo(ObjectNameTag nameTag, const Vec3D &from, const Vec3D &to, double line_width = 0.1, Mesh static LineTo(ObjectNameTag nameTag, const Vec3D &from, const Vec3D &to, double line_width = 0.1,
const sf::Color &color = {150, 150, 150, 100}); const sf::Color &color = {150, 150, 150, 100});
Mesh static ArrowTo(ObjectNameTag nameTag, const Vec3D& from, const Vec3D& to, double line_width = 0.1, sf::Color color = {150, 150, 150, 255});
// OpenGL functions // OpenGL functions
GLfloat *glFloatArray() const; GLfloat *glFloatArray() const;
void glFreeFloatArray(); void glFreeFloatArray();

View File

@ -21,8 +21,8 @@ Vec2D Mouse::getMouseDisplacement() const {
} }
void Mouse::setMouseInCenter() const { void Mouse::setMouseInCenter() const {
sf::Mouse::setPosition({static_cast<int>(_screen->width() / 2), static_cast<int>(_screen->height() / 2)}, sf::Vector2<int> center = sf::Vector2<int>(_screen->width() / 2, _screen->height() / 2);
*_screen->renderWindow()); sf::Mouse::setPosition(center,*_screen->renderWindow());
} }
bool Mouse::isButtonPressed(sf::Mouse::Button button) { bool Mouse::isButtonPressed(sf::Mouse::Button button) {

View File

@ -16,7 +16,6 @@ private:
const std::shared_ptr<Screen> _screen; const std::shared_ptr<Screen> _screen;
std::map<sf::Mouse::Button, double> _tappedButtons; std::map<sf::Mouse::Button, double> _tappedButtons;
public: public:
explicit Mouse(std::shared_ptr<Screen> screen) : _screen(std::move(screen)) {}; explicit Mouse(std::shared_ptr<Screen> screen) : _screen(std::move(screen)) {};

View File

@ -59,7 +59,7 @@ void Object::scale(const Vec3D &s) {
void Object::rotate(const Vec3D &r) { void Object::rotate(const Vec3D &r) {
_angle = _angle + r; _angle = _angle + r;
Matrix4x4 rotationMatrix = Matrix4x4::RotationZ(r.z()) * Matrix4x4::RotationY(r.y()) * Matrix4x4::RotationX(r.z()); Matrix4x4 rotationMatrix = Matrix4x4::RotationX(r.x()) * Matrix4x4::RotationY(r.y()) * Matrix4x4::RotationZ(r.z());
transform(rotationMatrix); transform(rotationMatrix);
} }

View File

@ -153,13 +153,6 @@ void Screen::drawText(const sf::Text &text) {
// OpenGL functions // OpenGL functions
void Screen::prepareToGlDrawMesh() { void Screen::prepareToGlDrawMesh() {
if (!sf::Shader::isAvailable())
{
Log::log("Shaders are not available!");
}
sf::Shader::bind(NULL);
glEnable(GL_CULL_FACE); // enable culling face glEnable(GL_CULL_FACE); // enable culling face
glCullFace(GL_BACK); // cull faces from back glCullFace(GL_BACK); // cull faces from back
glFrontFace(GL_CCW); // vertex order (counter clock wise) glFrontFace(GL_CCW); // vertex order (counter clock wise)
@ -173,6 +166,9 @@ void Screen::prepareToGlDrawMesh() {
glDisable(GL_LIGHTING); glDisable(GL_LIGHTING);
// enable alpha channel: // enable alpha channel:
glEnable( GL_ALPHA_TEST );
glAlphaFunc(GL_NOTEQUAL, 0.0);
glEnable(GL_BLEND); glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

View File

@ -38,6 +38,8 @@ public:
[[nodiscard]] sf::Color color() const { return _color; } [[nodiscard]] sf::Color color() const { return _color; }
void setColor(sf::Color newColor) { _color = newColor; }
[[nodiscard]] double distance(const Vec3D &vec) const { return norm().dot(Vec3D(_points[0]) - vec); } [[nodiscard]] double distance(const Vec3D &vec) const { return norm().dot(Vec3D(_points[0]) - vec); }
}; };

View File

@ -56,7 +56,7 @@ IntersectionInformation World::rayCast(const Vec3D &from, const Vec3D &to, const
for (auto &tri : body->triangles()) { for (auto &tri : body->triangles()) {
Matrix4x4 model = body->model(); Matrix4x4 model = body->model();
Triangle tri_translated(model * tri[0], model * tri[1], model * tri[2]); Triangle tri_translated(model * tri[0], model * tri[1], model * tri[2], tri.color());
Plane plane(tri_translated); Plane plane(tri_translated);
auto intersection = plane.intersection(from, to); auto intersection = plane.intersection(from, to);

View File

@ -23,8 +23,27 @@ private:
std::vector<Triangle> newTriangles; std::vector<Triangle> newTriangles;
newTriangles.reserve(_triangles.size()); newTriangles.reserve(_triangles.size());
double shift = 0.95/_triangles.size();
double oneTriangleTime = 1.0 - shift*_triangles.size();
double k = 0.0;
for(auto &t : _triangles) { for(auto &t : _triangles) {
newTriangles.emplace_back(t[0], t[1], t[1] + (t[2] - t[1]) * progress(), t.color()); if(progress() >= shift*k) {
if(progress() <= shift*k + oneTriangleTime) {
double triProgressLinear = (progress() - shift*k) / oneTriangleTime;
double triProgressBezier = Interpolation::Bezier(Consts::BEZIER[0], Consts::BEZIER[1], triProgressLinear);
newTriangles.emplace_back(t[0], t[1], t[1] + (t[2] - t[1]) * triProgressBezier, sf::Color(t.color().r, t.color().g, t.color().b, t.color().a*triProgressBezier));
} else {
newTriangles.emplace_back(t[0], t[1], t[2], t.color());
}
} else {
newTriangles.emplace_back(t[0], t[0], t[0]);
}
k = k + 1.0;
} }
mesh->setTriangles(std::move(newTriangles)); mesh->setTriangles(std::move(newTriangles));
mesh->glFreeFloatArray(); mesh->glFreeFloatArray();

View File

@ -0,0 +1,61 @@
//
// Created by Иван Ильин on 10.11.2021.
//
#ifndef SHOOTER_ASHOWUNCREATION_H
#define SHOOTER_ASHOWUNCREATION_H
#include "Animation.h"
#include "../Mesh.h"
class AShowUncreation final : public Animation {
private:
const std::weak_ptr<Mesh> _mesh;
const std::vector<Triangle> _triangles;
void update() override {
auto mesh = _mesh.lock();
if (mesh == nullptr) {
stop();
return;
}
std::vector<Triangle> newTriangles;
newTriangles.reserve(_triangles.size());
double shift = 0.95/_triangles.size();
double oneTriangleTime = 1.0 - shift*_triangles.size();
double k = 0.0;
double progress_inv = 1 - progress();
for(auto &t : _triangles) {
if(progress_inv >= shift*k) {
if(progress_inv <= shift*k + oneTriangleTime) {
double triProgressLinear = (progress_inv - shift*k) / oneTriangleTime;
double triProgressBezier = Interpolation::Bezier(Consts::BEZIER[0], Consts::BEZIER[1], triProgressLinear);
newTriangles.emplace_back(t[0], t[1], t[1] + (t[2] - t[1]) * triProgressBezier, sf::Color(t.color().r, t.color().g, t.color().b, t.color().a*triProgressBezier));
} else {
newTriangles.emplace_back(t[0], t[1], t[2], t.color());
}
} else {
newTriangles.emplace_back(t[0], t[0], t[0]);
}
k = k + 1.0;
}
mesh->setTriangles(std::move(newTriangles));
mesh->glFreeFloatArray();
}
public:
AShowUncreation(std::weak_ptr<Mesh> mesh, double duration = 1, LoopOut looped = LoopOut::None,
InterpolationType interpolationType = InterpolationType::Bezier) : Animation(duration, looped,
interpolationType),
_mesh(mesh), _triangles(mesh.lock()->triangles()) {}
};
#endif //SHOOTER_ASHOWUNCREATION_H

View File

@ -34,7 +34,8 @@ bool Animation::updateState() {
break; break;
default: default:
throw std::logic_error{ throw std::logic_error{
"Animation::updateState: unknown interpolation type " + std::to_string(static_cast<int>(_intType))}; "Animation::updateState: unknown interpolation type " + std::to_string(static_cast<int>(_intType))
};
} }
if (_time + _dtime > 1.0) { if (_time + _dtime > 1.0) {
@ -43,6 +44,7 @@ bool Animation::updateState() {
_dprogress = 1.0 - _progress; _dprogress = 1.0 - _progress;
_progress = 1.0; _progress = 1.0;
_finished = true; _finished = true;
} else { } else {
_time += _dtime; _time += _dtime;
_progress += _dprogress; _progress += _dprogress;

View File

@ -31,7 +31,7 @@ private:
// If '_waitForFinish' == true then we need to finish all animation before starting this one. (for example AWait) // If '_waitForFinish' == true then we need to finish all animation before starting this one. (for example AWait)
// In addition new animations in particular animation list will be started only after finishing this animation. // In addition new animations in particular animation list will be started only after finishing this animation.
const bool _waitForFinish = false; const bool _waitForFinish;
const double _duration = 0; const double _duration = 0;
const LoopOut _looped = LoopOut::None; const LoopOut _looped = LoopOut::None;
const InterpolationType _intType = InterpolationType::Bezier; const InterpolationType _intType = InterpolationType::Bezier;
@ -46,7 +46,7 @@ protected:
void stop() { _finished = true; } void stop() { _finished = true; }
public: public:
Animation(double duration, LoopOut looped, InterpolationType intType, bool _waitFor = false); Animation(double duration, LoopOut looped, InterpolationType intType, bool waitForFinish = false);
virtual ~Animation() = default; virtual ~Animation() = default;

View File

@ -18,5 +18,7 @@
#include "ATranslateToPoint.h" #include "ATranslateToPoint.h"
#include "AWait.h" #include "AWait.h"
#include "AShowCreation.h" #include "AShowCreation.h"
#include "AShowUncreation.h"
#endif //SHOOTER_ANIMATIONS_H #endif //SHOOTER_ANIMATIONS_H

View File

@ -4,7 +4,6 @@
#include <list> #include <list>
#include "Animation.h"
#include "Timeline.h" #include "Timeline.h"
#include "../utils/Log.h" #include "../utils/Log.h"
@ -66,12 +65,12 @@ void Timeline::update() {
auto& animationList = iter->second; auto& animationList = iter->second;
auto it = animationList.begin(); auto it = animationList.begin();
// If it the front animation is 'a_wait()' we should wait until waiting time is over // If it the front animation is 'AWait' we should wait until waiting time is over
if ((it != animationList.end()) && (*it)->isWaitingForFinish()) {
if ((*it)->isWaitingForFinish()) {
if (!(*it)->updateState()) { if (!(*it)->updateState()) {
animationList.erase(it); animationList.erase(it);
} }
++iter;
continue; continue;
} }

View File

@ -59,6 +59,15 @@ public:
_instance->_animations[listName].emplace_back(std::make_shared<T>(args...)); _instance->_animations[listName].emplace_back(std::make_shared<T>(args...));
} }
template <typename T, typename... Arguments>
static void addAnimation(Arguments... args) {
if (_instance == nullptr) {
return;
}
_instance->_animations[AnimationListTag("timeline_0")].emplace_back(std::make_shared<T>(args...));
}
}; };
#endif //SHOOTER_TIMELINE_H #endif //SHOOTER_TIMELINE_H

View File

@ -49,7 +49,7 @@ void Time::update() {
_instance->_last = t; _instance->_last = t;
if (_instance->_deltaTime > 10000) { if (_instance->_deltaTime > 10) {
return; return;
} }