|
@@ -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> ®istry)
|
|
|
{
|
|
|
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();
|
|
|
|