mirror of
https://github.com/Nimac0/SDL_Minigame
synced 2026-01-12 22:23:43 +00:00
Compare commits
7 Commits
5dfe42195f
...
58e2bb0d30
| Author | SHA1 | Date | |
|---|---|---|---|
| 58e2bb0d30 | |||
| 7ffda89c9d | |||
| 8c5c5c7215 | |||
| 5182312887 | |||
| 68a3e48131 | |||
| 31d7f42a31 | |||
| 1f27d24de3 |
@ -16,11 +16,5 @@ struct Animation
|
||||
}
|
||||
};
|
||||
|
||||
enum AnimationType //TODO enum class
|
||||
{
|
||||
IDLE = 0,
|
||||
WALK = 1
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
@ -24,7 +24,3 @@ constexpr int MAP_SIZE_Y = 20;
|
||||
|
||||
constexpr int SPAWN_ATTEMPTS = 20;
|
||||
|
||||
constexpr int BUFF_DURATION = 240;
|
||||
|
||||
constexpr int BUFF_VALUE = 1;
|
||||
|
||||
|
||||
@ -1,76 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL.h>
|
||||
#include <SDL_image.h>
|
||||
#include <SDL_mixer.h>
|
||||
#include <functional>
|
||||
#include <vector>
|
||||
class GameInternal;
|
||||
|
||||
#include "Manager.h"
|
||||
#include "Vector2D.h"
|
||||
#include "Entity.h"
|
||||
|
||||
typedef std::function<void()> gamefunction;
|
||||
|
||||
class AssetManager;
|
||||
class CollisionHandler;
|
||||
class TextureManager;
|
||||
class SoundManager;
|
||||
class Map;
|
||||
|
||||
namespace engine {
|
||||
extern gamefunction init;
|
||||
extern gamefunction update;
|
||||
|
||||
extern Game* game; // this is a temporary fix to remove artifacts of chicken_game from the engine while the API is not yet finalized
|
||||
}
|
||||
|
||||
class Game
|
||||
{
|
||||
// TODO: add managers here
|
||||
class Game {
|
||||
public:
|
||||
Game();
|
||||
~Game();
|
||||
virtual ~Game() {}
|
||||
|
||||
void init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen);
|
||||
void selectCharacters(const char* &playerSprite, const char* &enemySprite);
|
||||
virtual void init() = 0;
|
||||
virtual void update() = 0;
|
||||
|
||||
void handleEvents();
|
||||
void update();
|
||||
void render();
|
||||
void clean();
|
||||
bool running() const;
|
||||
|
||||
/* static */ SDL_Renderer* renderer = nullptr;
|
||||
/* static */ SDL_Event event;
|
||||
/* static */ CollisionHandler* collisionHandler;
|
||||
/* static */ AssetManager* assets;
|
||||
/* static */ TextureManager* textureManager;
|
||||
/* static */ SoundManager* soundManager;
|
||||
|
||||
// moved globals
|
||||
Manager manager;
|
||||
Map* map; // game specific, might not be needed for all types of games
|
||||
|
||||
Entity& player1;
|
||||
Entity& player2;
|
||||
|
||||
Entity& wall;
|
||||
|
||||
std::vector<Entity*>& tiles;
|
||||
std::vector<Entity*>& players;
|
||||
std::vector<Entity*>& projectiles;
|
||||
std::vector<Entity*>& hearts;
|
||||
std::vector<Entity*>& powerups;
|
||||
// end moved globals
|
||||
|
||||
void refreshPlayers();
|
||||
Entity::TeamLabel getWinner() const;
|
||||
void setWinner(Entity::TeamLabel winningTeam);
|
||||
|
||||
private:
|
||||
|
||||
int counter = 0;
|
||||
bool isRunning = false;
|
||||
SDL_Window* window;
|
||||
Entity::TeamLabel winner;
|
||||
GameInternal* gameInternal; //!< \deprecated
|
||||
};
|
||||
|
||||
|
||||
// game factory include to simplify imports in implementation
|
||||
#include "GameFactory.h"
|
||||
59
include/GameFactory.h
Normal file
59
include/GameFactory.h
Normal file
@ -0,0 +1,59 @@
|
||||
#pragma once
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <functional>
|
||||
#include <string>
|
||||
|
||||
#include "Game.h"
|
||||
|
||||
class GameInternal;
|
||||
|
||||
class GameFactory {
|
||||
public:
|
||||
using CreateFunc = std::function<Game*()>;
|
||||
|
||||
static GameFactory& instance() {
|
||||
static GameFactory factory;
|
||||
return factory;
|
||||
}
|
||||
|
||||
/*Game* get() {
|
||||
assert(this->gameInstance != nullptr);
|
||||
return this->gameInstance;
|
||||
}*/
|
||||
|
||||
/*Game* create(GameInternal* gameInternal) {
|
||||
Game* game = this->gameInstance == nullptr ? this->creator() : this->gameInstance; // TODO: error handling
|
||||
game->gameInternal = gameInternal;
|
||||
this->gameInstance = game;
|
||||
return game;
|
||||
}*/
|
||||
|
||||
void registerClass(const std::string& className, CreateFunc createFunc) {
|
||||
this->creators[className] = createFunc;
|
||||
}
|
||||
|
||||
Game* create(const std::string& className, GameInternal* gameInternal) {
|
||||
auto it = this->creators.find(className);
|
||||
if (it != creators.end()) {
|
||||
Game* game = it->second();
|
||||
game->gameInternal = gameInternal;
|
||||
return game;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
private:
|
||||
CreateFunc creator;
|
||||
std::map<std::string, CreateFunc> creators;
|
||||
};
|
||||
|
||||
/*
|
||||
#define REGISTER_GAME(className) \
|
||||
static bool registered_##className = []() { \
|
||||
GameFactory::instance().registerClass(#className, []() -> Game* { return new className; }); \
|
||||
return true; \
|
||||
}();
|
||||
*/
|
||||
68
include/GameInternal.h
Normal file
68
include/GameInternal.h
Normal file
@ -0,0 +1,68 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL.h>
|
||||
#include <SDL_image.h>
|
||||
#include <SDL_mixer.h>
|
||||
#include <functional>
|
||||
#include <vector>
|
||||
|
||||
#include "Manager.h"
|
||||
#include "Vector2D.h"
|
||||
#include "Entity.h"
|
||||
|
||||
typedef std::function<void()> gamefunction;
|
||||
|
||||
class AssetManager;
|
||||
class CollisionHandler;
|
||||
class TextureManager;
|
||||
class SoundManager;
|
||||
class Map;
|
||||
class Game;
|
||||
|
||||
class GameInternal
|
||||
{
|
||||
public:
|
||||
GameInternal();
|
||||
~GameInternal();
|
||||
|
||||
void init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen);
|
||||
void selectCharacters(const char* &playerSprite, const char* &enemySprite);
|
||||
|
||||
void handleEvents();
|
||||
void update();
|
||||
void render();
|
||||
void clean();
|
||||
bool isRunning() const;
|
||||
void setRunning(bool running);
|
||||
|
||||
/* static */ SDL_Renderer* renderer = nullptr;
|
||||
/* static */ SDL_Event event;
|
||||
/* static */ CollisionHandler* collisionHandler;
|
||||
/* static */ AssetManager* assets;
|
||||
/* static */ TextureManager* textureManager;
|
||||
/* static */ SoundManager* soundManager;
|
||||
|
||||
// moved globals
|
||||
Manager manager;
|
||||
Map* map; // game specific, might not be needed for all types of games
|
||||
|
||||
std::vector<Entity*>& tiles;
|
||||
std::vector<Entity*>& players;
|
||||
std::vector<Entity*>& projectiles;
|
||||
std::vector<Entity*>& hearts;
|
||||
std::vector<Entity*>& powerups;
|
||||
// end moved globals
|
||||
|
||||
void refreshPlayers();
|
||||
Entity::TeamLabel getWinner() const;
|
||||
void setWinner(Entity::TeamLabel winningTeam);
|
||||
|
||||
private:
|
||||
|
||||
Game* gameInstance;
|
||||
|
||||
int counter = 0;
|
||||
bool running = false;
|
||||
SDL_Window* window;
|
||||
Entity::TeamLabel winner;
|
||||
};
|
||||
17
include/GameRegistryHelper.h
Normal file
17
include/GameRegistryHelper.h
Normal file
@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#include "GameFactory.h"
|
||||
|
||||
namespace vego {
|
||||
template<typename T>
|
||||
class GameRegistryHelper {
|
||||
public:
|
||||
GameRegistryHelper(const std::string& className) {
|
||||
static_assert(std::is_base_of<Game, T>::value, "Your class must inherit from Game");
|
||||
GameFactory::instance().registerClass(
|
||||
className,
|
||||
[]() -> Game* { return new T; }
|
||||
);
|
||||
};
|
||||
};
|
||||
}
|
||||
@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include "Direction.h"
|
||||
#include "Component.h"
|
||||
|
||||
@ -9,7 +10,7 @@ class HealthComponent : public Component
|
||||
{
|
||||
public:
|
||||
|
||||
HealthComponent(int health, Direction side) : health(health), side(side) {}
|
||||
HealthComponent(int health, Direction side, std::string healthTexture) : health(health), side(side), healthTexture(healthTexture) {}
|
||||
~HealthComponent() {}
|
||||
|
||||
void modifyHealth(int health = -1);
|
||||
@ -26,4 +27,5 @@ private:
|
||||
|
||||
int health;
|
||||
Direction side;
|
||||
std::string healthTexture;
|
||||
};
|
||||
@ -9,11 +9,11 @@
|
||||
#include "Constants.h"
|
||||
#include "Entity.h"
|
||||
|
||||
class Game;
|
||||
class GameInternal;
|
||||
class Manager
|
||||
{
|
||||
public:
|
||||
Manager(Game* game) : game(game) {};
|
||||
Manager(GameInternal* game) : game(game) {};
|
||||
|
||||
void update();
|
||||
void draw();
|
||||
@ -29,10 +29,10 @@ public:
|
||||
|
||||
Entity& addEntity();
|
||||
|
||||
Game* getGame() { return this->game; };
|
||||
GameInternal* getGame() { return this->game; };
|
||||
|
||||
private:
|
||||
Game* game;
|
||||
GameInternal* game;
|
||||
std::vector<std::unique_ptr<Entity>> entities;
|
||||
std::array<std::vector<Entity*>, MAX_GROUPS> entitiesByGroup;
|
||||
std::array<std::vector<Entity*>, MAX_TEAMS> entitiesByTeam;
|
||||
|
||||
@ -3,7 +3,7 @@
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
class Game;
|
||||
class GameInternal;
|
||||
class Map
|
||||
{
|
||||
public:
|
||||
@ -21,6 +21,6 @@ public:
|
||||
* \return Boolean for success
|
||||
*
|
||||
*/
|
||||
static void loadMap(const char* path, int sizeX, int sizeY, Game* game, const std::map<int, std::pair<std::string, bool>>* textureDict /* backreference */);
|
||||
static void addTile(unsigned long id, int x, int y, Game* game, const std::map<int, std::pair<std::string, bool>>* textureDict);
|
||||
static void loadMap(const char* path, int sizeX, int sizeY, GameInternal* game, const std::map<int, std::pair<std::string, bool>>* textureDict /* backreference */);
|
||||
static void addTile(unsigned long id, int x, int y, GameInternal* game, const std::map<int, std::pair<std::string, bool>>* textureDict);
|
||||
};
|
||||
|
||||
@ -5,7 +5,7 @@
|
||||
|
||||
#include "Entity.h"
|
||||
|
||||
class Game;
|
||||
class GameInternal;
|
||||
|
||||
class PopupWindow {
|
||||
|
||||
|
||||
@ -13,7 +13,7 @@ enum SoundTypes
|
||||
THROW_EGG,
|
||||
};
|
||||
|
||||
class Game;
|
||||
class GameInternal;
|
||||
class SoundManager
|
||||
{
|
||||
public:
|
||||
@ -30,6 +30,6 @@ class SoundManager
|
||||
std::map<const char*, Mix_Chunk*> sound_cache;
|
||||
|
||||
Mix_Chunk* loadSound(const char* fileName);
|
||||
static void playSound(Game* game, SoundTypes sound);
|
||||
static void playSound(GameInternal* game, SoundTypes sound);
|
||||
private:
|
||||
};
|
||||
@ -3,6 +3,7 @@
|
||||
#include <map>
|
||||
#include <SDL_render.h>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "AnimationHandler.h"
|
||||
#include "Component.h"
|
||||
@ -15,7 +16,7 @@ class SpriteComponent : public Component
|
||||
public:
|
||||
int animationIndex = 0;
|
||||
|
||||
std::map<AnimationType, std::unique_ptr<Animation>> animations;
|
||||
std::map<std::string, std::unique_ptr<Animation>>* animations = nullptr;
|
||||
|
||||
private:
|
||||
TransformComponent* transform;
|
||||
@ -32,7 +33,11 @@ private:
|
||||
public:
|
||||
SpriteComponent() = default;
|
||||
SpriteComponent(const char* path);
|
||||
SpriteComponent(const char* path, bool isAnimated);
|
||||
SpriteComponent(
|
||||
const char* path,
|
||||
bool isAnimated,
|
||||
std::map<std::string, std::unique_ptr<Animation>>* animationList,
|
||||
std::string defaultAnimation);
|
||||
~SpriteComponent();
|
||||
|
||||
void setTexture(const char* path);
|
||||
@ -40,6 +45,6 @@ public:
|
||||
void init() override;
|
||||
void update() override;
|
||||
void draw() override;
|
||||
void playAnimation(AnimationType type);
|
||||
void playAnimation(std::string type);
|
||||
void setDirection(Direction direction);
|
||||
};
|
||||
|
||||
@ -18,9 +18,10 @@ public:
|
||||
void init() override;
|
||||
void update() override;
|
||||
|
||||
void modifyStatDur(Stats stat, int duration);
|
||||
void modifyStatDur(Stats stat, int duration, int value);
|
||||
|
||||
void modifyStatValue(Stats stat, int modifier);
|
||||
void resetStatValue(Stats stat);
|
||||
|
||||
private:
|
||||
std::array<int, MAX_STATS> buffs = { 0 };
|
||||
|
||||
@ -14,7 +14,8 @@ public:
|
||||
int width = 32;
|
||||
int scale = 1;
|
||||
|
||||
int speed = 3;
|
||||
int getSpeed() { return speed + speedMod; };
|
||||
void resetSpeedMod() { speedMod = 0; };
|
||||
|
||||
TransformComponent();
|
||||
explicit TransformComponent(int scale);
|
||||
@ -26,4 +27,8 @@ public:
|
||||
void update() override;
|
||||
void setPositionAfterCollision(Vector2D& positionChange);
|
||||
void modifySpeed(int8_t modifier);
|
||||
|
||||
private:
|
||||
int speed = 3;
|
||||
int speedMod = 0;
|
||||
};
|
||||
|
||||
@ -3,7 +3,7 @@
|
||||
#include "TextureManager.h"
|
||||
#include "SoundManager.h"
|
||||
#include "ProjectileComponent.h"
|
||||
#include "Game.h"
|
||||
#include "GameInternal.h"
|
||||
|
||||
#include "TransformComponent.h"
|
||||
|
||||
@ -11,7 +11,6 @@
|
||||
#include "ColliderComponent.h"
|
||||
#include "Constants.h"
|
||||
#include "Entity.h"
|
||||
#include "Game.h"
|
||||
#include "Vector2D.h"
|
||||
#include "PowerupComponent.h"
|
||||
#include <iostream>
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
|
||||
#include "CollisionHandler.h"
|
||||
#include "Entity.h"
|
||||
#include "Game.h"
|
||||
#include "GameInternal.h"
|
||||
#include "TransformComponent.h"
|
||||
#include <iostream>
|
||||
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
#include "Game.h"
|
||||
#include "GameInternal.h"
|
||||
|
||||
#include <SDL_error.h>
|
||||
|
||||
@ -14,31 +14,28 @@
|
||||
#include "TextureManager.h"
|
||||
#include "StatEffectsComponent.h"
|
||||
#include "Constants.h"
|
||||
#include "Game.h"
|
||||
#include "GameFactory.h"
|
||||
|
||||
Game* engine::game = nullptr; // will be initialized in constructor
|
||||
|
||||
Game::Game() :
|
||||
GameInternal::GameInternal() :
|
||||
manager(this),
|
||||
tiles(manager.getGroup((size_t)Entity::GroupLabel::MAPTILES)),
|
||||
players(manager.getGroup((size_t)Entity::GroupLabel::PLAYERS)),
|
||||
projectiles(manager.getGroup((size_t)Entity::GroupLabel::PROJECTILE)),
|
||||
hearts(manager.getGroup((size_t)Entity::GroupLabel::HEARTS)),
|
||||
powerups(manager.getGroup((size_t)Entity::GroupLabel::POWERUPS)),
|
||||
player1(manager.addEntity()),
|
||||
player2(manager.addEntity()),
|
||||
wall(manager.addEntity())
|
||||
{
|
||||
engine::game = this;
|
||||
};
|
||||
powerups(manager.getGroup((size_t)Entity::GroupLabel::POWERUPS))
|
||||
//player1(manager.addEntity()),
|
||||
//player2(manager.addEntity())
|
||||
{};
|
||||
|
||||
Game::~Game() = default;
|
||||
GameInternal::~GameInternal() = default;
|
||||
|
||||
void Game::init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen)
|
||||
void GameInternal::init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen)
|
||||
{
|
||||
Game::assets = new AssetManager(&manager);
|
||||
Game::textureManager = new TextureManager(&manager);
|
||||
Game::soundManager = new SoundManager();
|
||||
Game::collisionHandler = new CollisionHandler(manager); // why does this use a referrence, but AssetManager a pointer?
|
||||
GameInternal::assets = new AssetManager(&manager);
|
||||
GameInternal::textureManager = new TextureManager(&manager);
|
||||
GameInternal::soundManager = new SoundManager();
|
||||
GameInternal::collisionHandler = new CollisionHandler(manager); // why does this use a referrence, but AssetManager a pointer?
|
||||
|
||||
int flags = 0;
|
||||
if (fullscreen)
|
||||
@ -84,7 +81,7 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo
|
||||
}
|
||||
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
|
||||
|
||||
SDL_Texture* backgroundTexture = Game::textureManager->loadTexture("assets/startscreen.png");
|
||||
SDL_Texture* backgroundTexture = GameInternal::textureManager->loadTexture("assets/startscreen.png");
|
||||
|
||||
SDL_RenderClear(renderer);
|
||||
SDL_RenderCopy(renderer, backgroundTexture, NULL, NULL);
|
||||
@ -131,7 +128,7 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo
|
||||
|
||||
if (hasQuit)
|
||||
{
|
||||
this->isRunning = false;
|
||||
this->setRunning(false);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -142,7 +139,7 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo
|
||||
const char* player2Sprite;
|
||||
|
||||
selectCharacters(player1Sprite, player2Sprite);
|
||||
if (this->isRunning == false) return;
|
||||
if (this->isRunning() == false) return;
|
||||
|
||||
map = new Map();
|
||||
|
||||
@ -160,31 +157,30 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo
|
||||
|
||||
//ecs implementation
|
||||
|
||||
player1.setTeam(Entity::TeamLabel::BLUE);
|
||||
player1.addComponent<TransformComponent>(80,80,2); //posx, posy, scale
|
||||
player1.addComponent<SpriteComponent>(player1Sprite, true); //adds sprite (32x32px), path needed
|
||||
// player1.setTeam(Entity::TeamLabel::BLUE);
|
||||
// player1.addComponent<TransformComponent>(80,80,2); //posx, posy, scale
|
||||
// player1.addComponent<SpriteComponent>(player1Sprite, true); //adds sprite (32x32px), path needed
|
||||
// player1.addComponent<KeyboardController>(SDL_SCANCODE_W, SDL_SCANCODE_S, SDL_SCANCODE_A, SDL_SCANCODE_D, SDL_SCANCODE_E, Vector2D(2, 0));//custom keycontrols can be added
|
||||
player1.addComponent<InputComponent>();
|
||||
player1.addComponent<ColliderComponent>("player", 0.8f); //adds tag (for further use, reference tag)
|
||||
player1.addComponent<HealthComponent>(5, Direction::LEFT);
|
||||
player1.addComponent<StatEffectsComponent>();
|
||||
player1.addGroup((size_t) Entity::GroupLabel::PLAYERS); //tell programm what group it belongs to for rendering order
|
||||
// player1.addComponent<ColliderComponent>("player", 0.8f); //adds tag (for further use, reference tag)
|
||||
// player1.addComponent<HealthComponent>(5, Direction::LEFT, "assets/heart.png");
|
||||
// player1.addComponent<StatEffectsComponent>();
|
||||
// player1.addGroup((size_t) Entity::GroupLabel::PLAYERS); //tell programm what group it belongs to for rendering order
|
||||
|
||||
|
||||
player2.setTeam(Entity::TeamLabel::RED);
|
||||
player2.addComponent<TransformComponent>(600, 500, 2);
|
||||
player2.addComponent<SpriteComponent>(player2Sprite, true);
|
||||
// player2.setTeam(Entity::TeamLabel::RED);
|
||||
// player2.addComponent<TransformComponent>(600, 500, 2);
|
||||
// player2.addComponent<SpriteComponent>(player2Sprite, true);
|
||||
// player2.addComponent<KeyboardController>(SDL_SCANCODE_UP, SDL_SCANCODE_DOWN, SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT, SDL_SCANCODE_RCTRL, Vector2D(-2, 0));
|
||||
player2.addComponent<InputComponent>();
|
||||
player2.addComponent<ColliderComponent>("enemy", 0.8f);
|
||||
player2.addComponent<HealthComponent>(5, Direction::RIGHT);
|
||||
player2.addComponent<StatEffectsComponent>();
|
||||
player2.addGroup((size_t) Entity::GroupLabel::PLAYERS);
|
||||
// player2.addComponent<ColliderComponent>("enemy", 0.8f);
|
||||
// player2.addComponent<HealthComponent>(5, Direction::RIGHT, "assets/heart.png");
|
||||
// player2.addComponent<StatEffectsComponent>();
|
||||
// player2.addGroup((size_t) Entity::GroupLabel::PLAYERS);
|
||||
|
||||
engine::init();
|
||||
this->gameInstance = GameFactory::instance().create("Chickengame", this); //!< \todo Should be specified via a config file
|
||||
this->gameInstance->init();
|
||||
}
|
||||
|
||||
void Game::selectCharacters(const char* &playerSprite, const char* &enemySprite)
|
||||
void GameInternal::selectCharacters(const char* &playerSprite, const char* &enemySprite)
|
||||
{
|
||||
// TODO: move this whereever it makes sense (maybe game as a member)
|
||||
std::map<int, std::pair<const char*, const char*>> characterSprites;
|
||||
@ -252,7 +248,7 @@ void Game::selectCharacters(const char* &playerSprite, const char* &enemySprite)
|
||||
}
|
||||
}
|
||||
|
||||
SDL_Texture* backgroundTexture = Game::textureManager->loadTexture("assets/characterSelection.png");
|
||||
SDL_Texture* backgroundTexture = GameInternal::textureManager->loadTexture("assets/characterSelection.png");
|
||||
SDL_RenderClear(renderer);
|
||||
SDL_RenderCopy(renderer, backgroundTexture, NULL, NULL);
|
||||
|
||||
@ -271,22 +267,22 @@ void Game::selectCharacters(const char* &playerSprite, const char* &enemySprite)
|
||||
|
||||
if (hasQuit)
|
||||
{
|
||||
this->isRunning = false;
|
||||
this->setRunning(false);
|
||||
return;
|
||||
}
|
||||
|
||||
playerSprite = characterSprites.find(playerSelection)->second.second;
|
||||
enemySprite = characterSprites.find(enemySelection)->second.second;
|
||||
this->isRunning = true;
|
||||
this->setRunning(true);
|
||||
}
|
||||
|
||||
void Game::handleEvents()
|
||||
void GameInternal::handleEvents()
|
||||
{
|
||||
SDL_PollEvent(&event);
|
||||
|
||||
switch (event.type)
|
||||
{
|
||||
case SDL_QUIT: this->isRunning = false;
|
||||
case SDL_QUIT: this->setRunning(false);
|
||||
break;
|
||||
|
||||
default:
|
||||
@ -294,15 +290,15 @@ void Game::handleEvents()
|
||||
}
|
||||
}
|
||||
|
||||
void Game::update()
|
||||
void GameInternal::update()
|
||||
{
|
||||
manager.refresh();
|
||||
manager.update();
|
||||
|
||||
engine::update(); // TODO: this might have to be split up into two update functions, before and after manager...
|
||||
this->gameInstance->update(); // TODO: this might have to be split up into two update functions, before and after manager...
|
||||
}
|
||||
|
||||
void Game::render()
|
||||
void GameInternal::render()
|
||||
{
|
||||
SDL_RenderClear(renderer);
|
||||
for (auto& t : tiles)
|
||||
@ -323,7 +319,7 @@ void Game::render()
|
||||
SDL_RenderPresent(renderer);
|
||||
}
|
||||
|
||||
void Game::clean()
|
||||
void GameInternal::clean()
|
||||
{
|
||||
delete(textureManager);
|
||||
SDL_DestroyRenderer(renderer);
|
||||
@ -332,18 +328,23 @@ void Game::clean()
|
||||
std::cout << "Game Cleaned!" << std::endl;
|
||||
}
|
||||
|
||||
bool Game::running() const
|
||||
bool GameInternal::isRunning() const
|
||||
{
|
||||
return isRunning;
|
||||
return running;
|
||||
}
|
||||
|
||||
void Game::setWinner(Entity::TeamLabel winningTeam)
|
||||
void GameInternal::setRunning(bool running)
|
||||
{
|
||||
this->running = running;
|
||||
}
|
||||
|
||||
void GameInternal::setWinner(Entity::TeamLabel winningTeam)
|
||||
{
|
||||
this->winner = winningTeam;
|
||||
this->isRunning = false;
|
||||
this->setRunning(false);
|
||||
}
|
||||
|
||||
Entity::TeamLabel Game::getWinner() const
|
||||
Entity::TeamLabel GameInternal::getWinner() const
|
||||
{
|
||||
return this->winner;
|
||||
}
|
||||
@ -2,7 +2,7 @@
|
||||
|
||||
#include "SDL_error.h"
|
||||
#include "TextureManager.h"
|
||||
#include "Game.h"
|
||||
#include "GameInternal.h"
|
||||
|
||||
GameObject::GameObject(const char* texturesheet, int x, int y)
|
||||
{
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
|
||||
#include "Direction.h"
|
||||
#include "Entity.h"
|
||||
#include "Game.h"
|
||||
#include "GameInternal.h"
|
||||
#include <cstdio>
|
||||
|
||||
void HealthComponent::init()
|
||||
@ -57,7 +57,7 @@ void HealthComponent::createHeartComponents(int x)
|
||||
{
|
||||
auto& heart(this->entity->getManager().addEntity());
|
||||
heart.addComponent<TransformComponent>(x,5,2);
|
||||
heart.addComponent<SpriteComponent>("assets/heart.png");
|
||||
heart.addComponent<SpriteComponent>(this->healthTexture.data());
|
||||
heart.addGroup((size_t)Entity::GroupLabel::HEARTS);
|
||||
heart.setTeam(this->entity->getTeam());
|
||||
}
|
||||
@ -6,11 +6,11 @@
|
||||
#include <utility>
|
||||
|
||||
#include "Constants.h"
|
||||
#include "Game.h"
|
||||
#include "GameInternal.h"
|
||||
#include "SDL_error.h"
|
||||
#include "TileComponent.h"
|
||||
|
||||
void Map::loadMap(const char* path, int sizeX, int sizeY, Game* game, const std::map<int, std::pair<std::string, bool>>* textureDict /* backreference */)
|
||||
void Map::loadMap(const char* path, int sizeX, int sizeY, GameInternal* game, const std::map<int, std::pair<std::string, bool>>* textureDict /* backreference */)
|
||||
{
|
||||
std::string tileIDstr;
|
||||
char singleChar = 0;
|
||||
@ -56,7 +56,7 @@ void Map::loadMap(const char* path, int sizeX, int sizeY, Game* game, const std:
|
||||
mapFile.close();
|
||||
}
|
||||
|
||||
void Map::addTile(unsigned long id, int x, int y, Game* game, const std::map<int, std::pair<std::string, bool>>* textureDict) // tile entity
|
||||
void Map::addTile(unsigned long id, int x, int y, GameInternal* game, const std::map<int, std::pair<std::string, bool>>* textureDict) // tile entity
|
||||
{
|
||||
auto& tile(game->manager.addEntity());
|
||||
tile.addComponent<TileComponent>(x, y, TILE_SIZE, TILE_SIZE, id, textureDict);
|
||||
|
||||
@ -4,7 +4,7 @@
|
||||
#include "Entity.h"
|
||||
#include "PopupWindow.h"
|
||||
#include "TextureManager.h"
|
||||
#include "Game.h"
|
||||
#include "GameInternal.h"
|
||||
|
||||
PopupWindow::PopupWindow(const char* title, const std::string &message) :
|
||||
continueGame(false), interacted(false) {
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
#include "PowerupComponent.h"
|
||||
#include "Game.h"
|
||||
#include "GameInternal.h"
|
||||
#include "CollisionHandler.h"
|
||||
#include "Entity.h"
|
||||
#include "HealthComponent.h"
|
||||
|
||||
@ -4,7 +4,7 @@
|
||||
#include "SoundManager.h"
|
||||
#include "TransformComponent.h"
|
||||
#include "Entity.h"
|
||||
#include "Game.h"
|
||||
#include "GameInternal.h"
|
||||
#include "HealthComponent.h"
|
||||
#include "Vector2D.h"
|
||||
#include <cassert>
|
||||
|
||||
@ -4,7 +4,7 @@
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
#include "Game.h"
|
||||
#include "GameInternal.h"
|
||||
#include "AssetManager.h"
|
||||
|
||||
Mix_Chunk* SoundManager::loadSound(const char* fileName)
|
||||
@ -27,7 +27,7 @@ Mix_Chunk* SoundManager::loadSound(const char* fileName)
|
||||
return sound;
|
||||
}
|
||||
|
||||
void SoundManager::playSound(Game* game, SoundTypes sound)
|
||||
void SoundManager::playSound(GameInternal* game, SoundTypes sound)
|
||||
{
|
||||
switch (sound)
|
||||
{
|
||||
|
||||
@ -9,7 +9,7 @@
|
||||
#include "TextureManager.h"
|
||||
#include "Entity.h"
|
||||
#include "TransformComponent.h"
|
||||
#include "Game.h"
|
||||
#include "GameInternal.h"
|
||||
#include "Manager.h"
|
||||
|
||||
SpriteComponent::SpriteComponent(const char* path)
|
||||
@ -17,14 +17,17 @@ SpriteComponent::SpriteComponent(const char* path)
|
||||
this->texturePath = path;
|
||||
}
|
||||
|
||||
SpriteComponent::SpriteComponent(const char* path, bool isAnimated)
|
||||
SpriteComponent::SpriteComponent(
|
||||
const char* path,
|
||||
bool isAnimated,
|
||||
std::map<std::string, std::unique_ptr<Animation>>* animationMap,
|
||||
std::string defaultAnimation)
|
||||
{
|
||||
animated = isAnimated;
|
||||
|
||||
animations.emplace(IDLE, std::make_unique<Animation>((uint8_t)AnimationType::IDLE, 2, 200));
|
||||
animations.emplace(WALK, std::make_unique<Animation>((uint8_t)AnimationType::WALK, 2, 200));
|
||||
animations = animationMap;
|
||||
|
||||
playAnimation(IDLE);
|
||||
playAnimation(defaultAnimation);
|
||||
|
||||
this->texturePath = path;
|
||||
}
|
||||
@ -71,11 +74,11 @@ void SpriteComponent::draw()
|
||||
this->entity->getManager().getGame()->textureManager->draw(this->entity->getManager().getGame()->renderer, this->texture, this->srcRect, this->destRect, this->animated && this->flipped);
|
||||
}
|
||||
|
||||
void SpriteComponent::playAnimation(AnimationType type)
|
||||
void SpriteComponent::playAnimation(std::string type)
|
||||
{
|
||||
this->animationIndex = animations.at(type)->index;
|
||||
this->frames = animations.at(type)->frames;
|
||||
this->speed = animations.at(type)->speed;
|
||||
this->animationIndex = animations->at(type)->index;
|
||||
this->frames = animations->at(type)->frames;
|
||||
this->speed = animations->at(type)->speed;
|
||||
}
|
||||
|
||||
void SpriteComponent::setDirection(Direction direction)
|
||||
|
||||
@ -15,15 +15,15 @@ void StatEffectsComponent::update()
|
||||
if (this->buffs.at(i) == 0) continue;
|
||||
if (this->buffs.at(i) - 1 == 0)
|
||||
{
|
||||
this->modifyStatValue((Stats)i, BUFF_VALUE * -1);
|
||||
this->resetStatValue((Stats)i);
|
||||
}
|
||||
this->buffs.at(i) -= 1;
|
||||
}
|
||||
}
|
||||
|
||||
void StatEffectsComponent::modifyStatDur(Stats stat, int duration)
|
||||
void StatEffectsComponent::modifyStatDur(Stats stat, int duration, int value)
|
||||
{
|
||||
if(this->buffs.at((uint8_t)stat) == 0) this->modifyStatValue(stat, BUFF_VALUE);
|
||||
if(this->buffs.at((uint8_t)stat) == 0) this->modifyStatValue(stat, value);
|
||||
this->buffs.at((uint8_t)stat) += duration;
|
||||
}
|
||||
|
||||
@ -40,3 +40,17 @@ void StatEffectsComponent::modifyStatValue(Stats stat, int modifier) //modifier
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
|
||||
void StatEffectsComponent::resetStatValue(Stats stat)
|
||||
{
|
||||
switch (stat)
|
||||
{
|
||||
case Stats::MOVEMENT_SPEED:
|
||||
this->entity->getComponent<TransformComponent>().resetSpeedMod();
|
||||
break;
|
||||
case Stats::ATTACK_SPEED:
|
||||
// this->entity->getComponent<KeyboardController>().resetAtkSpeedMod();
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
@ -4,7 +4,7 @@
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
#include "Game.h"
|
||||
#include "GameInternal.h"
|
||||
|
||||
SDL_Texture* TextureManager::loadTexture(const char* fileName)
|
||||
{
|
||||
|
||||
@ -4,7 +4,7 @@
|
||||
#include "ColliderComponent.h"
|
||||
#include "Constants.h"
|
||||
#include "Entity.h"
|
||||
#include "Game.h"
|
||||
#include "GameInternal.h"
|
||||
#include "Vector2D.h"
|
||||
#include <cstdio>
|
||||
#include <initializer_list>
|
||||
@ -56,8 +56,8 @@ void TransformComponent::update()
|
||||
|
||||
float multiplier = direction.x != 0 && direction.y != 0 ? 0.707 : 1; // normalizes vector; only works if directions are in increments of 45°
|
||||
Vector2D positionChange(
|
||||
direction.x * speed * multiplier,
|
||||
direction.y * speed * multiplier
|
||||
direction.x * this->getSpeed() * multiplier,
|
||||
direction.y * this->getSpeed() * multiplier
|
||||
);
|
||||
|
||||
if (this->entity->hasGroup((size_t)Entity::GroupLabel::PLAYERS)){
|
||||
@ -69,7 +69,7 @@ void TransformComponent::update()
|
||||
|
||||
void TransformComponent::modifySpeed(int8_t modifier)
|
||||
{
|
||||
this->speed += modifier;
|
||||
this->speedMod += modifier;
|
||||
}
|
||||
|
||||
void TransformComponent::setPositionAfterCollision(Vector2D& positionChange)
|
||||
|
||||
@ -2,11 +2,11 @@
|
||||
#include <ctime>
|
||||
|
||||
#include "Entity.h"
|
||||
#include "Game.h"
|
||||
#include "GameInternal.h"
|
||||
#include "Constants.h"
|
||||
#include "PopupWindow.h"
|
||||
|
||||
Game* game = nullptr;
|
||||
GameInternal* game = nullptr;
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
@ -18,10 +18,10 @@ int main(int argc, char* argv[])
|
||||
Uint32 frameStart;
|
||||
int frameTime;
|
||||
|
||||
game = new Game();
|
||||
game = new GameInternal();
|
||||
|
||||
game->init("No_Name_Chicken_Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_SIZE_WIDTH, SCREEN_SIZE_HEIGHT, false);
|
||||
while (game->running()) {
|
||||
while (game->isRunning()) {
|
||||
frameStart = SDL_GetTicks();
|
||||
|
||||
game->handleEvents();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user