Godzil hace 3 años
padre
commit
896639600c
Se han modificado 3 ficheros con 96 adiciones y 7 borrados
  1. 23 7
      source/Game.cpp
  2. 33 0
      source/include/Components/Animation.h
  3. 40 0
      source/include/Systems/Animation.h

+ 23 - 7
source/Game.cpp

@@ -18,12 +18,14 @@
 #include <Logger.h>
 #include <ECS.h>
 
+#include <Components/Animation.h>
 #include <Components/Transform.h>
 #include <Components/RigidBody.h>
 #include <Components/Sprite.h>
 
 #include <Systems/Movement.h>
 #include <Systems/Render.h>
+#include <Systems/Animation.h>
 
 Game::Game()
 {
@@ -105,12 +107,13 @@ void Game::LoadLevel(int level)
 {
     this->registry->addSystem<MovementSystem>();
     this->registry->addSystem<RenderSystem>();
+    this->registry->addSystem<AnimationSystem>();
 
     this->assetStore->addTexture(this->renderer, "tank-image", "assets/images/tank-panther-right.png");
     this->assetStore->addTexture(this->renderer, "truck-image", "assets/images/truck-ford-right.png");
-
-
+    this->assetStore->addTexture(this->renderer, "chopper-image", "assets/images/chopper.png");
     this->assetStore->addTexture(this->renderer, "main-tileset", "assets/tilemaps/jungle.png");
+    this->assetStore->addTexture(this->renderer, "radar-image", "assets/images/radar.png");
 
     Logger::Debug("struct SpriteComponent size is %d", sizeof(struct SpriteComponent));
 
@@ -143,14 +146,26 @@ void Game::LoadLevel(int level)
     }
     mapFile.close();
 
+    Entity chopper = this->registry->createEntity();
+    chopper.addComponent<TransformComponent>(glm::vec2(10, 10), glm::vec2(1, 1), 0);
+    chopper.addComponent<SpriteComponent>("chopper-image", 2, 32, 32);
+    chopper.addComponent<RigidBodyComponent>(glm::vec2(0, 0));
+    chopper.addComponent<AnimationComponent>(2, 15, true);
+
+    Entity radar = this->registry->createEntity();
+    radar.addComponent<TransformComponent>(glm::vec2(this->windowsWidth - 75, 10), glm::vec2(1, 1), 0);
+    radar.addComponent<RigidBodyComponent>(glm::vec2(0, 0));
+    radar.addComponent<SpriteComponent>("radar-image", 3, 64, 64);
+    radar.addComponent<AnimationComponent>(8, 5, true);
+
     Entity tank = this->registry->createEntity();
-    tank.addComponent<TransformComponent>(glm::vec2(10, 30), glm::vec2(1, 1), 0);
-    tank.addComponent<RigidBodyComponent>(glm::vec2(40, 0));
-    tank.addComponent<SpriteComponent>("tank-image", 2, 32, 32);
+    tank.addComponent<TransformComponent>(glm::vec2(10, 50), glm::vec2(1, 1), 0);
+    tank.addComponent<RigidBodyComponent>(glm::vec2(20, 0));
+    tank.addComponent<SpriteComponent>("tank-image", 1, 32, 32);
 
     Entity truck = this->registry->createEntity();
-    truck.addComponent<TransformComponent>(glm::vec2(50, 100), glm::vec2(1, 1), 0);
-    truck.addComponent<RigidBodyComponent>(glm::vec2(0, 50));
+    truck.addComponent<TransformComponent>(glm::vec2(10, 100), glm::vec2(1, 1), 0);
+    truck.addComponent<RigidBodyComponent>(glm::vec2(25, 0));
     truck.addComponent<SpriteComponent>("truck-image", 1, 32, 32);
 }
 
@@ -171,6 +186,7 @@ void Game::Update()
     this->millisecsPerviousFrame = SDL_GetTicks();
 
     registry->getSystem<MovementSystem>().update(deltaTime);
+    registry->getSystem<AnimationSystem>().update();
 
     registry->update();
 }

+ 33 - 0
source/include/Components/Animation.h

@@ -0,0 +1,33 @@
+/*
+ * 2D Game Engine 
+ * Animation.h: 
+ * Based on pikuma.com 2D game engine in C++ and Lua course
+ * Copyright (c) 2021 986-Studio. All rights reserved.
+ *
+ * Created by Manoël Trapier on 15/02/2021.
+ */
+
+#ifndef GAMEENGINE_ANIMATION_H
+#define GAMEENGINE_ANIMATION_H
+
+#include <stdint.h>
+#include <SDL.h>
+
+struct AnimationComponent
+{
+    uint32_t numFrames;
+    uint32_t currentFrame;
+    uint32_t frameSpeedRate;
+    bool isLoop;
+    uint32_t startTime;
+
+    explicit AnimationComponent(uint32_t numFrames = 1, int frameSpeedRate = 1,
+                                bool isLoop = true) :numFrames(numFrames), frameSpeedRate(frameSpeedRate), isLoop(isLoop)
+    {
+        this->currentFrame = 1;
+        this->startTime = SDL_GetTicks();
+    };
+};
+
+
+#endif /* GAMEENGINE_ANIMATION_H */

+ 40 - 0
source/include/Systems/Animation.h

@@ -0,0 +1,40 @@
+/*
+ * 2D Game Engine 
+ * Animation.h: 
+ * Based on pikuma.com 2D game engine in C++ and Lua course
+ * Copyright (c) 2021 986-Studio. All rights reserved.
+ *
+ * Created by Manoël Trapier on 15/02/2021.
+ */
+
+#ifndef GAMEENGINE_ANIMATIONSYS_H
+#define GAMEENGINE_ANIMATIONSYS_H
+
+#include <ECS.h>
+#include <Components/Animation.h>
+#include <Components/Sprite.h>
+
+class AnimationSystem: public System
+{
+public:
+    AnimationSystem()
+    {
+        this->requireComponent<AnimationComponent>();
+        this->requireComponent<SpriteComponent>();
+    }
+
+    void update()
+    {
+        for(auto entity: this->getSystemEntities())
+        {
+            auto &animation = entity.getComponent<AnimationComponent>();
+            auto &sprite = entity.getComponent<SpriteComponent>();
+
+            animation.currentFrame = ((SDL_GetTicks() - animation.startTime) *  animation.frameSpeedRate / 1000) % animation.numFrames;
+
+            sprite.sourceRect.x = animation.currentFrame * sprite.width;
+        }
+    }
+};
+
+#endif /* GAMEENGINE_ANIMATIONSYS_H */