diff --git a/include/AssetManager.h b/include/AssetManager.h index c74d441..43cefe8 100644 --- a/include/AssetManager.h +++ b/include/AssetManager.h @@ -13,7 +13,6 @@ public: AssetManager(Manager* manager); ~AssetManager(); - //game object management void createProjectile(Vector2D pos, Vector2D velocity, bool source, int scale, int range, int speed, const char* texturePath); //texture management diff --git a/include/ColliderComponent.h b/include/ColliderComponent.h index a090b1f..ec0746a 100644 --- a/include/ColliderComponent.h +++ b/include/ColliderComponent.h @@ -10,7 +10,7 @@ public: const char* tag; TransformComponent* transform; - bool hasCollision; + bool hasCollision; //added for removing collision of destroyed projectiles ColliderComponent(const char* tag) { diff --git a/include/KeyboardController.h b/include/KeyboardController.h index 01bfe0d..3d64256 100644 --- a/include/KeyboardController.h +++ b/include/KeyboardController.h @@ -14,8 +14,9 @@ public: SDL_Scancode right; SDL_Scancode fire; + //for attack cooldown in between shots Uint32 lastFireTime; - Uint32 fireCooldown = 1000; + Uint32 fireCooldown = 1000; //in ms can be adjusted to change possible attack-speed KeyboardController(); KeyboardController(SDL_Scancode up, SDL_Scancode down, SDL_Scancode left, SDL_Scancode right, SDL_Scancode fire, Vector2D fireVelocity); @@ -27,6 +28,7 @@ public: void update() override; private: - TransformComponent* player; - Vector2D fireVelocity; + //for creation of projectiles + TransformComponent* player; //for starting position of projectile + Vector2D fireVelocity; //decide source of projectile and flying direction }; diff --git a/include/ProjectileComponent.h b/include/ProjectileComponent.h index 7cdb308..911a03b 100644 --- a/include/ProjectileComponent.h +++ b/include/ProjectileComponent.h @@ -6,6 +6,8 @@ class ProjectileComponent : public Component { + //can maybe be split in separate .cpp file + public: ProjectileComponent(int range, int speed, Vector2D velocity, bool source) : range(range), speed(speed), velocity(velocity), source(source) { @@ -19,13 +21,15 @@ public: } void update() override { + transformComponent->velocity = velocity; + distance += speed; if (distance > range) { entity->destroy(); entity->getComponent().removeCollision(); - std::cout << "out of range" << std::endl; + //std::cout << "out of range" << std::endl; } } @@ -43,7 +47,7 @@ private: int speed = 0; int distance = 0; - const bool source; + const bool source; //true if from player1 / false if from player2 Vector2D velocity; }; \ No newline at end of file diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp index 8b57afb..4212671 100644 --- a/src/AssetManager.cpp +++ b/src/AssetManager.cpp @@ -18,7 +18,7 @@ SDL_Texture* AssetManager::getTexture(std::string id) { void AssetManager::createProjectile(Vector2D pos, Vector2D velocity, bool source, int scale, int range, int speed, const char* texturePath) { auto& projectile(man->addEntity()); - projectile.addComponent(pos.x, pos.y, 32, 32, scale); + projectile.addComponent(pos.x, pos.y, 32, 32, scale); //32x32 is standard size for objects projectile.addComponent(texturePath); projectile.addComponent(range, speed, velocity, source); projectile.addComponent("projectile"); diff --git a/src/Game.cpp b/src/Game.cpp index f5ef63c..61e93df 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -63,6 +63,8 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo map = new Map(); map->loadMap("assets/SDL_map_test.txt", 25, 20); + //adding textures to the library in AssetManager + assets->addTexture("player1", "assets/chicken_neutral_knight.png"); assets->addTexture("player2", "assets/chicken_neutral.png"); assets->addTexture("bigEgg", "assets/bigger_egg.png"); @@ -81,16 +83,6 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo enemy.addComponent(SDL_SCANCODE_UP, SDL_SCANCODE_DOWN, SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT, SDL_SCANCODE_RCTRL, Vector2D(-1, 0)); enemy.addComponent("enemy"); enemy.addGroup(GROUP_ENEMIES); - - /* - projectile.addComponent(500, 500, 32, 32, 2); - projectile.addComponent("assets/chicken_neutral_knight.png"); - projectile.addComponent(200, 1, Vector2D(1,0)); - projectile.addComponent("projectile"); - projectile.addGroup(Game::PROJECTILE); - - assets->createProjectile(Vector2D(50, 100), Vector2D(1,0), 1, 180, 1, "assets/chicken_neutral_knight.png"); - */ } auto& tiles(manager.getGroup(Game::GROUP_MAP)); @@ -133,18 +125,18 @@ void Game::update() } } - + //checking if projectiles hit player1 or player2 for (auto& p : projectiles) { if(SDL_HasIntersection(&enemy.getComponent().collider, &p->getComponent().collider) && (p->getComponent().hasCollision) && !p->getComponent().getSource()) { - std::cout << "Enemy hit!"; + //std::cout << "Enemy hit!"; p->getComponent().removeCollision(); p->destroy(); } if(SDL_HasIntersection(&player.getComponent().collider, &p->getComponent().collider) && (p->getComponent().hasCollision) && p->getComponent().getSource()) { - std::cout << "Player hit!"; + //std::cout << "Player hit!"; p->getComponent().removeCollision(); p->destroy(); } diff --git a/src/KeyboardController.cpp b/src/KeyboardController.cpp index 88ef377..f2bf9e7 100644 --- a/src/KeyboardController.cpp +++ b/src/KeyboardController.cpp @@ -45,10 +45,15 @@ void KeyboardController::update() } if (keystates[this->fire]) { + Uint32 currentTicks = SDL_GetTicks(); + if (currentTicks - lastFireTime >= fireCooldown) { player = &entity->getComponent(); + + //checks player source via the firing velocity + //TODO: adding actual projectile textures if(fireVelocity.x > 0) { Game::assets->createProjectile(Vector2D(player->position.x, player->position.y), fireVelocity, false,1, 180, 1, "assets/chicken_neutral_knight.png");