Godzil 3 lat temu
rodzic
commit
3cb526eadb
2 zmienionych plików z 55 dodań i 1 usunięć
  1. 2 0
      source/Game.cpp
  2. 53 1
      source/include/Systems/ProjectileEmit.h

+ 2 - 0
source/Game.cpp

@@ -185,6 +185,7 @@ void Game::LoadLevel(int level)
     chopper.addComponent<AnimationComponent>(2, 15, true);
     chopper.addComponent<KeyboardControlComponent>(glm::vec2(0, -80), glm::vec2(80, 0),
                                                    glm::vec2(0, 80), glm::vec2(-80, 0));
+    chopper.addComponent<ProjectileEmitterComponent>(glm::vec2(180, 180), 0, 10000, 0, true);
     chopper.addComponent<HealthComponent>(100);
     chopper.addComponent<CameraFollowComponent>();
 
@@ -232,6 +233,7 @@ void Game::Update()
 
     registry->getSystem<DamageSystem>().subscriptToEvents(eventBus);
     registry->getSystem<KeyboardMovementSystem>().subscriptToEvents(eventBus);
+    registry->getSystem<ProjectileEmitSystem>().subscriptToEvents(eventBus);
 
     registry->update();
 

+ 53 - 1
source/include/Systems/ProjectileEmit.h

@@ -20,6 +20,7 @@
 #include <Components/Transform.h>
 #include <Components/BoxCollider.h>
 #include <Components/Projectile.h>
+#include <Components/CameraFollow.h>
 
 class ProjectileEmitSystem: public System
 {
@@ -30,6 +31,57 @@ public:
         this->requireComponent<TransformComponent>();
     }
 
+    void subscriptToEvents(std::unique_ptr<EventBus> &eventBus)
+    {
+        eventBus->subscribeToEvent<KeyPressedEvent>(this, &ProjectileEmitSystem::onKeyPressed);
+    }
+
+    void onKeyPressed(KeyPressedEvent &event)
+    {
+        if (event.keyCode == SDLK_SPACE)
+        {
+            for (auto entity: this->getSystemEntities())
+            {
+                if (entity.hasComponent<CameraFollowComponent>())
+                {
+                    auto &emitter = entity.getComponent<ProjectileEmitterComponent>();
+                    const auto transform = entity.getComponent<TransformComponent>();
+                    const auto rigidBody = entity.getComponent<RigidBodyComponent>();
+
+                    Entity projectile = entity.registry->createEntity();
+
+                    glm::vec2 projectilePosition = transform.position;
+                    if ( entity.hasComponent<SpriteComponent>() )
+                    {
+                        auto sprite = entity.getComponent<SpriteComponent>();
+                        projectilePosition.x += (transform.scale.x * sprite.width) / 2.;
+                        projectilePosition.y += (transform.scale.y * sprite.height) / 2.;
+                    }
+
+                    glm::vec2 projectileVelocity = glm::vec2(0, 0);
+
+                    if (rigidBody.velocity.x != 0)
+                    {
+                        projectileVelocity.x = (rigidBody.velocity.x > 0) ? emitter.projectileVelocity.x : -emitter.projectileVelocity.x;
+                    }
+                    if (rigidBody.velocity.y != 0)
+                    {
+                        projectileVelocity.y = (rigidBody.velocity.y > 0) ? emitter.projectileVelocity.y : -emitter.projectileVelocity.y;
+                    }
+
+                    projectile.addComponent<TransformComponent>(projectilePosition, glm::vec2(1, 1), 0);
+                    projectile.addComponent<RigidBodyComponent>(projectileVelocity);
+                    projectile.addComponent<SpriteComponent>("bullet-image", 4, 4, 4);
+                    projectile.addComponent<BoxColliderComponent>(4, 4);
+                    projectile.addComponent<ProjectileComponent>(emitter.projectileDuration, emitter.hitPercentDamage, emitter.isFriendly);
+
+                    emitter.lastEmissionTime = SDL_GetTicks();
+
+                }
+            }
+        }
+    }
+
     void update(std::unique_ptr<Registry> &registry)
     {
         for(auto entity: this->getSystemEntities())
@@ -37,7 +89,7 @@ public:
             auto &emitter = entity.getComponent<ProjectileEmitterComponent>();
             const auto transform = entity.getComponent<TransformComponent>();
 
-            if ((SDL_GetTicks() - emitter.lastEmissionTime) > emitter.repeatFrequency)
+            if ((emitter.repeatFrequency > 0) && ((SDL_GetTicks() - emitter.lastEmissionTime) > emitter.repeatFrequency))
             {
                 Entity projectile = registry->createEntity();