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

Compare commits

..

No commits in common. "e3852379e4cd83137fa1badf248511dabe754c5d" and "8e0c76457ee30d4626b12c97881ea68b830df609" have entirely different histories.

8 changed files with 94 additions and 50 deletions

View File

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

View File

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

View File

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

View File

@ -48,21 +48,25 @@ void AssetManager::createProjectile(Vector2D pos, Vector2D velocity, int scale,
projectile.setTeam(teamLabel);
}
void AssetManager::createPowerup(Vector2D pos, std::function<void (Entity*)> pickupFunc, std::string texturePath) {
void AssetManager::createPowerup(Vector2D pos, PowerupType type) {
TextureDict textureDict;
auto& powerups(man->addEntity());
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 {
powerups.addComponent<SpriteComponent>(texturePath.c_str());
powerups.addComponent<SpriteComponent>(it->second.data());
}
catch (std::runtime_error e) {
std::cout << e.what() << std::endl;
}
powerups.addComponent<ColliderComponent>("powerup", 0.6f);
powerups.addComponent<PowerupComponent>(pickupFunc);
powerups.addComponent<PowerupComponent>(type);
powerups.addGroup((size_t)Entity::GroupLabel::POWERUPS);
}

View File

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

View File

@ -7,9 +7,22 @@
#include "Constants.h"
#include <cstdint>
PowerupComponent::PowerupComponent(std::function<void (Entity*)> func)
PowerupComponent::PowerupComponent(PowerupType type)
{
this->pickupFunc = func;
switch (type)
{
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()
@ -22,7 +35,23 @@ void PowerupComponent::update()
{},
true)) != nullptr)
{
(this->pickupFunc)(player);
(this->*pickupFunc)(player);
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,8 +60,22 @@ void TransformComponent::update()
direction.y * speed * multiplier
);
if (this->entity->hasGroup((size_t)Entity::GroupLabel::PLAYERS)){
this->setPositionAfterCollision(positionChange);
// TODO: move to separate functions
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;
@ -70,24 +84,4 @@ void TransformComponent::update()
void TransformComponent::modifySpeed(int8_t 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,6 +21,7 @@ int main(int argc, char* argv[])
game = new Game();
game->init("No_Name_Chicken_Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_SIZE_WIDTH, SCREEN_SIZE_HEIGHT, false);
while(playing) {
while (game->running()) {
frameStart = SDL_GetTicks();
@ -34,6 +35,21 @@ int main(int argc, char* argv[])
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();