mirror of
https://github.com/Nimac0/SDL_Minigame
synced 2026-01-12 13:43:41 +00:00
Compare commits
3 Commits
8e0c76457e
...
e3852379e4
| Author | SHA1 | Date | |
|---|---|---|---|
| e3852379e4 | |||
| cffe1bdc31 | |||
| 6744f519c5 |
@ -3,6 +3,7 @@
|
||||
#include <SDL_mixer.h>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <functional>
|
||||
|
||||
#include "Entity.h"
|
||||
|
||||
@ -24,7 +25,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, PowerupType type);
|
||||
void createPowerup(Vector2D pos, std::function<void (Entity*)> pickupFunc, std::string texturePath);
|
||||
|
||||
Vector2D calculateSpawnPosition();
|
||||
PowerupType calculateType();
|
||||
|
||||
@ -1,19 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
#include "Component.h"
|
||||
#include "AssetManager.h"
|
||||
|
||||
class PowerupComponent : public Component
|
||||
{
|
||||
public:
|
||||
PowerupComponent(PowerupType type);
|
||||
PowerupComponent(std::function<void (Entity*)> func);
|
||||
~PowerupComponent() {};
|
||||
|
||||
void update() override;
|
||||
void heartEffect(Entity* player);
|
||||
void movementSpeedEffect(Entity* player);
|
||||
void atkSpeedEffect(Entity* player);
|
||||
|
||||
private:
|
||||
void (PowerupComponent::*pickupFunc)(Entity* player);
|
||||
std::function<void (Entity*)> pickupFunc;
|
||||
};
|
||||
@ -24,5 +24,6 @@ public:
|
||||
|
||||
void init() override;
|
||||
void update() override;
|
||||
void setPositionAfterCollision(Vector2D& positionChange);
|
||||
void modifySpeed(int8_t modifier);
|
||||
};
|
||||
|
||||
@ -48,25 +48,21 @@ void AssetManager::createProjectile(Vector2D pos, Vector2D velocity, int scale,
|
||||
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;
|
||||
|
||||
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>(it->second.data());
|
||||
powerups.addComponent<SpriteComponent>(texturePath.c_str());
|
||||
}
|
||||
catch (std::runtime_error e) {
|
||||
std::cout << e.what() << std::endl;
|
||||
}
|
||||
|
||||
powerups.addComponent<ColliderComponent>("powerup", 0.6f);
|
||||
powerups.addComponent<PowerupComponent>(type);
|
||||
powerups.addComponent<PowerupComponent>(pickupFunc);
|
||||
powerups.addGroup((size_t)Entity::GroupLabel::POWERUPS);
|
||||
}
|
||||
|
||||
|
||||
32
src/Game.cpp
32
src/Game.cpp
@ -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();
|
||||
//}
|
||||
|
||||
@ -7,22 +7,9 @@
|
||||
#include "Constants.h"
|
||||
#include <cstdint>
|
||||
|
||||
PowerupComponent::PowerupComponent(PowerupType type)
|
||||
PowerupComponent::PowerupComponent(std::function<void (Entity*)> 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;
|
||||
}
|
||||
this->pickupFunc = func;
|
||||
}
|
||||
|
||||
void PowerupComponent::update()
|
||||
@ -35,23 +22,7 @@ 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);
|
||||
}
|
||||
@ -60,22 +60,8 @@ void TransformComponent::update()
|
||||
direction.y * speed * multiplier
|
||||
);
|
||||
|
||||
// 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;
|
||||
this->setPositionAfterCollision(positionChange);
|
||||
}
|
||||
|
||||
position += positionChange;
|
||||
@ -85,3 +71,23 @@ 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;
|
||||
}
|
||||
16
src/main.cpp
16
src/main.cpp
@ -21,7 +21,6 @@ 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();
|
||||
|
||||
@ -35,21 +34,6 @@ 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();
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user