0
0
mirror of https://github.com/Nimac0/SDL_Minigame synced 2026-01-12 15:53:42 +00:00

Compare commits

..

3 Commits

8 changed files with 50 additions and 94 deletions

View File

@ -3,6 +3,7 @@
#include <SDL_mixer.h> #include <SDL_mixer.h>
#include <map> #include <map>
#include <string> #include <string>
#include <functional>
#include "Entity.h" #include "Entity.h"
@ -24,7 +25,7 @@ public:
~AssetManager(); ~AssetManager();
void createProjectile(Vector2D pos, Vector2D velocity, int scale, int range, int speed, const char* texturePath, Entity::TeamLabel teamLabel); void createProjectile(Vector2D pos, Vector2D velocity, int scale, int range, int speed, const char* texturePath, Entity::TeamLabel teamLabel);
void createPowerup(Vector2D pos, PowerupType type); void createPowerup(Vector2D pos, std::function<void (Entity*)> pickupFunc, std::string texturePath);
Vector2D calculateSpawnPosition(); Vector2D calculateSpawnPosition();
PowerupType calculateType(); PowerupType calculateType();

View File

@ -1,19 +1,16 @@
#pragma once #pragma once
#include <functional>
#include "Component.h" #include "Component.h"
#include "AssetManager.h"
class PowerupComponent : public Component class PowerupComponent : public Component
{ {
public: public:
PowerupComponent(PowerupType type); PowerupComponent(std::function<void (Entity*)> func);
~PowerupComponent() {}; ~PowerupComponent() {};
void update() override; void update() override;
void heartEffect(Entity* player);
void movementSpeedEffect(Entity* player);
void atkSpeedEffect(Entity* player);
private: private:
void (PowerupComponent::*pickupFunc)(Entity* player); std::function<void (Entity*)> pickupFunc;
}; };

View File

@ -24,5 +24,6 @@ public:
void init() override; void init() override;
void update() override; void update() override;
void setPositionAfterCollision(Vector2D& positionChange);
void modifySpeed(int8_t modifier); void modifySpeed(int8_t modifier);
}; };

View File

@ -48,25 +48,21 @@ void AssetManager::createProjectile(Vector2D pos, Vector2D velocity, int scale,
projectile.setTeam(teamLabel); projectile.setTeam(teamLabel);
} }
void AssetManager::createPowerup(Vector2D pos, PowerupType type) { void AssetManager::createPowerup(Vector2D pos, std::function<void (Entity*)> pickupFunc, std::string texturePath) {
TextureDict textureDict; TextureDict textureDict;
auto& powerups(man->addEntity()); auto& powerups(man->addEntity());
powerups.addComponent<TransformComponent>(pos.x, pos.y, 32, 32, 1); //32x32 is standard size for objects powerups.addComponent<TransformComponent>(pos.x, pos.y, 32, 32, 1); //32x32 is standard size for objects
auto it = textureDict.powerupDictionary.find(type);
if (it == textureDict.powerupDictionary.end()) {
std::cout << "it end" << std::endl;
}
try { try {
powerups.addComponent<SpriteComponent>(it->second.data()); powerups.addComponent<SpriteComponent>(texturePath.c_str());
} }
catch (std::runtime_error e) { catch (std::runtime_error e) {
std::cout << e.what() << std::endl; std::cout << e.what() << std::endl;
} }
powerups.addComponent<ColliderComponent>("powerup", 0.6f); powerups.addComponent<ColliderComponent>("powerup", 0.6f);
powerups.addComponent<PowerupComponent>(type); powerups.addComponent<PowerupComponent>(pickupFunc);
powerups.addGroup((size_t)Entity::GroupLabel::POWERUPS); powerups.addGroup((size_t)Entity::GroupLabel::POWERUPS);
} }

View File

@ -354,19 +354,19 @@ Entity::TeamLabel Game::getWinner() const
return this->winner; return this->winner;
} }
void Game::refreshPlayers() { //void Game::refreshPlayers() {
//
for(auto& p : projectiles) { // for(auto& p : projectiles) {
p->destroy(); // p->destroy();
} // }
//
player1.getComponent<TransformComponent>().position = Vector2D(80, 80); // player1.getComponent<TransformComponent>().position = Vector2D(80, 80);
player2.getComponent<TransformComponent>().position = Vector2D(600, 500); // player2.getComponent<TransformComponent>().position = Vector2D(600, 500);
//
player1.getComponent<HealthComponent>().setHealth(5); // player1.getComponent<HealthComponent>().setHealth(5);
player2.getComponent<HealthComponent>().setHealth(5); // player2.getComponent<HealthComponent>().setHealth(5);
//
isRunning = true; // isRunning = true;
//
update(); // update();
} //}

View File

@ -7,22 +7,9 @@
#include "Constants.h" #include "Constants.h"
#include <cstdint> #include <cstdint>
PowerupComponent::PowerupComponent(PowerupType type) PowerupComponent::PowerupComponent(std::function<void (Entity*)> func)
{ {
switch (type) this->pickupFunc = func;
{
case PowerupType::HEART:
this->pickupFunc = (&PowerupComponent::heartEffect);
break;
case PowerupType::WALKINGSPEED:
this->pickupFunc = (&PowerupComponent::movementSpeedEffect);
break;
case PowerupType::SHOOTINGSPEED:
this->pickupFunc = (&PowerupComponent::atkSpeedEffect);
break;
default:
break;
}
} }
void PowerupComponent::update() void PowerupComponent::update()
@ -35,23 +22,7 @@ void PowerupComponent::update()
{}, {},
true)) != nullptr) true)) != nullptr)
{ {
(this->*pickupFunc)(player); (this->pickupFunc)(player);
this->entity->destroy(); this->entity->destroy();
} }
}
void PowerupComponent::heartEffect(Entity* player)
{
if(player->getComponent<HealthComponent>().getHealth() < 5)
player->getComponent<HealthComponent>().modifyHealth(1);
}
void PowerupComponent::movementSpeedEffect(Entity* player)
{
player->getComponent<StatEffectsComponent>().modifyStatDur(Stats::MOVEMENT_SPEED, BUFF_DURATION);
}
void PowerupComponent::atkSpeedEffect(Entity* player)
{
player->getComponent<StatEffectsComponent>().modifyStatDur(Stats::ATTACK_SPEED, BUFF_DURATION);
} }

View File

@ -60,22 +60,8 @@ void TransformComponent::update()
direction.y * speed * multiplier direction.y * speed * multiplier
); );
// TODO: move to separate functions if (this->entity->hasGroup((size_t)Entity::GroupLabel::PLAYERS)){
this->setPositionAfterCollision(positionChange);
if (this->entity->hasGroup((size_t)Entity::GroupLabel::PLAYERS)) {
IntersectionBitSet intersections =
(CollisionHandler::getIntersectionWithBounds(entity, Vector2D(positionChange.x, 0)) |
(this->entity->getManager().getGame()->collisionHandler->getAnyIntersection<IntersectionBitSet>(entity, Vector2D(positionChange.x, 0), { Entity::GroupLabel::MAPTILES, Entity::GroupLabel::COLLIDERS })) &
IntersectionBitSet("0011")) |
(CollisionHandler::getIntersectionWithBounds(entity, Vector2D(0, positionChange.y)) |
(this->entity->getManager().getGame()->collisionHandler->getAnyIntersection<IntersectionBitSet>(entity, Vector2D(0, positionChange.y), { Entity::GroupLabel::MAPTILES, Entity::GroupLabel::COLLIDERS })) &
IntersectionBitSet("1100"));
if (intersections.test((size_t)Direction::LEFT) || intersections.test((size_t)Direction::RIGHT))
positionChange.x = 0;
if (intersections.test((size_t)Direction::UP) || intersections.test((size_t)Direction::DOWN))
positionChange.y = 0;
} }
position += positionChange; position += positionChange;
@ -84,4 +70,24 @@ void TransformComponent::update()
void TransformComponent::modifySpeed(int8_t modifier) void TransformComponent::modifySpeed(int8_t modifier)
{ {
this->speed += modifier; this->speed += modifier;
}
void TransformComponent::setPositionAfterCollision(Vector2D& positionChange)
{
std::initializer_list colliders = { Entity::GroupLabel::MAPTILES, Entity::GroupLabel::COLLIDERS };
IntersectionBitSet intersections =
(CollisionHandler::getIntersectionWithBounds(entity, Vector2D(positionChange.x, 0)) |
(this->entity->getManager()
.getGame()->collisionHandler->getAnyIntersection<IntersectionBitSet>(entity, Vector2D(positionChange.x, 0), colliders)) &
IntersectionBitSet("0011")) |
(CollisionHandler::getIntersectionWithBounds(entity, Vector2D(0, positionChange.y)) |
(this->entity->getManager()
.getGame()->collisionHandler->getAnyIntersection<IntersectionBitSet>(entity, Vector2D(0, positionChange.y), colliders)) &
IntersectionBitSet("1100"));
if (intersections.test((size_t)Direction::LEFT) || intersections.test((size_t)Direction::RIGHT))
positionChange.x = 0;
if (intersections.test((size_t)Direction::UP) || intersections.test((size_t)Direction::DOWN))
positionChange.y = 0;
} }

View File

@ -21,7 +21,6 @@ int main(int argc, char* argv[])
game = new Game(); game = new Game();
game->init("No_Name_Chicken_Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_SIZE_WIDTH, SCREEN_SIZE_HEIGHT, false); game->init("No_Name_Chicken_Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_SIZE_WIDTH, SCREEN_SIZE_HEIGHT, false);
while(playing) {
while (game->running()) { while (game->running()) {
frameStart = SDL_GetTicks(); frameStart = SDL_GetTicks();
@ -35,21 +34,6 @@ int main(int argc, char* argv[])
SDL_Delay(frameDelay - frameTime); SDL_Delay(frameDelay - frameTime);
} }
} }
Entity::TeamLabel winner = game->getWinner();
PopupWindow popupWindow("Game over", winner == Entity::TeamLabel::BLUE ?
"Player1 won! Press 'C' to continue or 'Q' to quit." :
"Player2 won! Press 'C' to continue or 'Q' to quit.");
popupWindow.renderWinnerPopup(winner);
while (!popupWindow.interacted) {
popupWindow.handleWinnerEvents();
SDL_Delay(10);
}
playing = popupWindow.shouldContinue();
game->refreshPlayers();
}
game->clean(); game->clean();