Page MenuHomePhabricator (Chris)

No OneTemporary

Authored By
Unknown
Size
24 KB
Referenced Files
None
Subscribers
None
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 5a1bbc9..728d9e7 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,7 +1,10 @@
cmake_minimum_required(VERSION 3.14)
project(SDL_Game)
-set(CMAKE_CXX_STANDARD 14)
+set(CMAKE_CXX_STANDARD 17)
include_directories(include)
+find_package(Boost REQUIRED COMPONENTS filesystem)
+include_directories(${Boost_INCLUDE_DIRS})
link_directories(lib)
link_libraries(mingw32 SDL2main SDL2 SDL2_image)
-add_executable(SDL_Game src/main.cpp src/Game.cpp src/Game.h src/Entity.cpp src/Entity.h src/Component.cpp src/Component.h src/System.cpp src/System.h src/systems/RenderSystem.cpp src/systems/RenderSystem.h src/components/Visibility.cpp src/components/Visibility.h src/components/Velocity.cpp src/components/Velocity.h src/systems/MovementSystem.cpp src/systems/MovementSystem.h src/components/Position.cpp src/components/Position.h src/components/PathIndex.cpp src/components/PathIndex.h src/GameData.h src/components/Speed.h)
+add_executable(SDL_Game src/main.cpp src/Game.cpp src/Game.h src/Entity.cpp src/Entity.h src/Component.cpp src/Component.h src/System.cpp src/System.h src/systems/RenderSystem.cpp src/systems/RenderSystem.h src/components/Visibility.cpp src/components/Visibility.h src/components/Velocity.cpp src/components/Velocity.h src/systems/MovementSystem.cpp src/systems/MovementSystem.h src/components/Position.cpp src/components/Position.h src/components/PathIndex.cpp src/components/PathIndex.h src/GameData.h src/components/Speed.h src/components/Sequence.cpp src/components/Sequence.h src/components/Kind.h src/systems/SpawnSystem.cpp src/systems/SpawnSystem.h src/GameData.cpp)
+target_link_libraries(SDL_Game ${Boost_LIBRARIES})
\ No newline at end of file
diff --git a/src/Component.h b/src/Component.h
index 436f7d4..deb40a7 100644
--- a/src/Component.h
+++ b/src/Component.h
@@ -1,31 +1,31 @@
//
// Created by Ido Mozes on 20/06/2019.
//
#ifndef SDL2_GAME_COMPONENT_H
#define SDL2_GAME_COMPONENT_H
struct Point {
short X;
short Y;
};
class Entity;
enum ComponentType {
- VISIBILITY, POSITION, VELOCITY,SPEED, ACCELERATION, PATH_INDEX, HEALTH, KIND, TYPE, DAMAGE, PIERCE, SPREAD,
+ VISIBILITY, POSITION, VELOCITY,SPEED, ACCELERATION, PATH_INDEX, HEALTH, KIND, TYPE, DAMAGE, PIERCE, SPREAD,SEQUENCE,
LENGTH
};
class Component {
Entity *entity;
public:
explicit Component(Entity *entity) : entity(entity) {};
~Component() = default;
Entity *getEntity() { return entity; }
};
#endif //SDL2_GAME_COMPONENT_H
diff --git a/src/Game.cpp b/src/Game.cpp
index a3f35c4..0750cb0 100644
--- a/src/Game.cpp
+++ b/src/Game.cpp
@@ -1,63 +1,78 @@
//
// Created by Ido Mozes on 18/06/2019.
//
#include <array>
+
#include "Game.h"
+using namespace boost::filesystem;
Game::Game(bool fullscreen, float mapScale) {
gameData.mapScale = mapScale;
gameData.fullscreen = fullscreen;
+ path p = path("../assets/Bloons");
+ directory_iterator it{p};
+ for (auto &p :it) {
+ gameData.assets[p.path().filename().string().substr(0, p.path().filename().string().length() - 4)] = IMG_Load(
+ p.path().string().c_str());
+ }
+ gameData.assets["map"] = IMG_Load("../assets/map0.jpg");
renderSystem.init(gameData);
- loadLevel();
+ loadMap();
auto mapEntity = new Entity();
- mapEntity->addComponent<Visibility>(renderSystem.getRenderer(), "../assets/level0.jpg");
+ mapEntity->addComponent<Visibility>(gameData.renderer, gameData.assets["map"]);
entities.emplace_back(mapEntity);
- auto player = new Entity();
- player->addComponent<Visibility>(renderSystem.getRenderer(), "../assets/knight.png", new SDL_Rect{0, 0, 50, 0});
- player->addComponent<Position>(gameData.startingPoint.X, gameData.startingPoint.Y);
- player->addComponent<Speed>(10);
- player->addComponent<PathIndex>(0);
- entities.emplace_back(player);
+ auto s = new Entity();
+ s->addComponent<Sequence>(10, 60, 0);
+ s->addComponent<Kind>(std::string("Red"));
+ s->addComponent<Speed>(1);
+ entities.emplace_back(s);
+ s = new Entity();
+ s->addComponent<Sequence>(10, 60, 60*5);
+ s->addComponent<Kind>(std::string("Blue"));
+ s->addComponent<Speed>(1.5);
+ entities.emplace_back(s);
+
+ systems.emplace_back(new SpawnSystem);
systems.emplace_back(new MovementSystem);
}
Game::~Game() {
SDL_Quit();
}
void Game::handleEvents() {
SDL_Event event;
SDL_PollEvent(&event);
switch (event.type) {
case SDL_QUIT:
gameData.isRunning = false;
break;
default:
break;
}
}
void Game::update() {
for (auto &system : systems) {
system->update(entities, gameData);
}
renderSystem.update(entities, gameData);
}
-void Game::loadLevel() {
+void Game::loadMap() {
gameData.path.clear();
- std::string fileName = "../assets/level" + std::to_string(gameData.level);
+ std::string fileName = "../assets/map" + std::to_string(gameData.map);
std::ifstream file(fileName + "_path.data", std::ios::binary);
unsigned int length;
file.read((char *) &gameData.startingPoint, 4);
file.read((char *) &length, 4);
gameData.path.resize(length);
file.read(&gameData.path[0], length);
file.read((char *) &gameData.finishPoint, 4);
}
diff --git a/src/Game.h b/src/Game.h
index d92a595..2888d9e 100644
--- a/src/Game.h
+++ b/src/Game.h
@@ -1,46 +1,49 @@
//
// Created by Ido Mozes on 18/06/2019.
//
#ifndef SDL2_GAME_GAME_H
#define SDL2_GAME_GAME_H
#include <iostream>
#include <fstream>
#include <memory>
#include <vector>
#include "SDL.h"
#include "SDL_image.h"
-
+#include <comdef.h>
#include "Entity.h"
#include "System.h"
#include "systems/RenderSystem.h"
#include "systems/MovementSystem.h"
+#include "systems/SpawnSystem.h"
#include "components/Visibility.h"
#include "components/PathIndex.h"
#include "components/Speed.h"
#include "GameData.h"
+#include "boost/filesystem.hpp"
+#include <iostream>
class Game {
std::vector<std::unique_ptr<System>> systems;
std::vector<std::shared_ptr<Entity>> entities;
RenderSystem renderSystem;
GameData gameData;
public:
explicit Game(bool fullscreen, float mapScale=1.5);
~Game();
void handleEvents();
void update();
bool running() { return gameData.isRunning; }
- void loadLevel();
+ void loadMap();
};
#endif //SDL2_GAME_GAME_H
diff --git a/src/GameData.cpp b/src/GameData.cpp
new file mode 100644
index 0000000..da0953b
--- /dev/null
+++ b/src/GameData.cpp
@@ -0,0 +1,9 @@
+//
+// Created by Ido Mozes on 07/07/2019.
+//
+#include "GameData.h"
+
+GameData::~GameData() {
+ SDL_DestroyWindow(window);
+ SDL_DestroyRenderer(renderer);
+}
diff --git a/src/GameData.h b/src/GameData.h
index 4e15998..11a6a29 100644
--- a/src/GameData.h
+++ b/src/GameData.h
@@ -1,23 +1,31 @@
//
// Created by Ido Mozes on 03/07/2019.
//
#ifndef SDL_GAME_GAMEDATA_H
#define SDL_GAME_GAMEDATA_H
#include <vector>
+#include <unordered_map>
#include "Component.h"
+#include "SDL.h"
class GameData {
public:
bool isRunning = true;
int level = 0;
+ int map = 0;
float mapScale = 1.5;
bool fullscreen;
std::vector<char> path;
- char **map;
+// char **mapData;
Point startingPoint;
Point finishPoint;
+ std::unordered_map<std::string,SDL_Surface *> assets;
+ SDL_Window *window = nullptr;
+ SDL_Renderer *renderer = nullptr;
+
+ ~GameData();
};
#endif //SDL_GAME_GAMEDATA_H
diff --git a/src/components/Kind.h b/src/components/Kind.h
new file mode 100644
index 0000000..f6000e0
--- /dev/null
+++ b/src/components/Kind.h
@@ -0,0 +1,21 @@
+//
+// Created by Ido Mozes on 07/07/2019.
+//
+
+#ifndef SDL_GAME_KIND_H
+#define SDL_GAME_KIND_H
+
+#include "../Component.h"
+#include <string>
+
+class Kind : public Component {
+
+
+public:
+ std::string kind;
+ static ComponentType getComponentType() { return ComponentType::KIND; }
+
+ Kind(Entity *entity, std::string kind) : Component(entity), kind(std::move(kind)) {}
+};
+
+#endif //SDL_GAME_KIND_H
diff --git a/src/components/PathIndex.h b/src/components/PathIndex.h
index 456897a..b0f8f67 100644
--- a/src/components/PathIndex.h
+++ b/src/components/PathIndex.h
@@ -1,20 +1,21 @@
//
// Created by Ido Mozes on 03/07/2019.
//
#ifndef SDL_GAME_PATHINDEX_H
#define SDL_GAME_PATHINDEX_H
#include "../Component.h"
class PathIndex : public Component {
public:
int index;
+ float progress;
static ComponentType getComponentType() { return ComponentType::PATH_INDEX; }
- PathIndex(Entity *entity, int index) : Component(entity), index(index) {}
+ PathIndex(Entity *entity, int index) : Component(entity), index(index), progress(index) {}
};
#endif //SDL_GAME_PATHINDEX_H
diff --git a/src/components/Sequence.cpp b/src/components/Sequence.cpp
new file mode 100644
index 0000000..789c231
--- /dev/null
+++ b/src/components/Sequence.cpp
@@ -0,0 +1,32 @@
+//
+// Created by Ido Mozes on 07/07/2019.
+//
+
+#include "Sequence.h"
+
+
+int Sequence::getAmountReady() {
+ if (amount == 0)
+ return SEQUENCE_FINISHED;
+ int amountToReturn;
+ if (amountReady <= amount) {
+ amount -= amountReady;
+ amountToReturn = amountReady;
+ } else {
+ amountToReturn = amount;
+ amount = 0;
+ }
+ amountReady = 0;
+ if (timeToRecharge == 1) {
+ timeToRecharge = interval;
+ amountReady = 1;
+ } else if (timeToRecharge > 1) {
+ timeToRecharge -= 1;
+ } else {
+ amountReady = 1 + int((1 - timeToRecharge) / interval);
+ timeToRecharge = fmodf((1 - timeToRecharge), interval);
+ if(timeToRecharge == 0)
+ timeToRecharge = interval;
+ }
+ return amountToReturn;
+}
diff --git a/src/components/Sequence.h b/src/components/Sequence.h
new file mode 100644
index 0000000..83ba584
--- /dev/null
+++ b/src/components/Sequence.h
@@ -0,0 +1,30 @@
+//
+// Created by Ido Mozes on 07/07/2019.
+//
+
+#ifndef SDL_GAME_SEQUENCE_H
+#define SDL_GAME_SEQUENCE_H
+
+#include <cmath>
+#include "../Component.h"
+
+#define SEQUENCE_FINISHED (-1)
+
+class Sequence : public Component {
+ int amount;
+ float interval;
+ float timeToRecharge;
+ int amountReady;
+
+public:
+ static ComponentType getComponentType() { return ComponentType::SEQUENCE; }
+
+ Sequence(Entity *entity, int amount, float interval, float delay) : Component(entity), amount(amount),
+ interval(interval),
+ timeToRecharge(delay ? delay : interval),
+ amountReady(delay == 0) {}
+
+ int getAmountReady();
+};
+
+#endif //SDL_GAME_SEQUENCE_H
diff --git a/src/components/Visibility.cpp b/src/components/Visibility.cpp
index ec9ea7a..dfe13c2 100644
--- a/src/components/Visibility.cpp
+++ b/src/components/Visibility.cpp
@@ -1,42 +1,40 @@
//
// Created by Ido Mozes on 23/06/2019.
//
#include "Visibility.h"
-Visibility::Visibility(Entity *entity, SDL_Renderer *renderer, const char *filePath, SDL_Rect *dstR)
- : Component(entity) {
- setDstRect(dstR);
- loadTexture(renderer, filePath);
+Visibility::Visibility(Entity *entity, SDL_Renderer *renderer, SDL_Surface *newSurface, std::optional<SDL_Rect> dstR)
+ : Component(entity), dstRect(dstR) {
+ loadTexture(renderer, newSurface);
}
Visibility::~Visibility() {
- delete dstRect;
SDL_DestroyTexture(texture);
SDL_FreeSurface(surface);
}
-void Visibility::loadTexture(SDL_Renderer *renderer, const char *filePath) {
- loadSurface(filePath);
+void Visibility::loadTexture(SDL_Renderer *renderer, SDL_Surface *newSurface) {
+ surface = newSurface;
reloadTexture(renderer);
if (dstRect) {
if (dstRect->w == 0)
dstRect->w = int((float(surface->w) / surface->h) * dstRect->h);
else if (dstRect->h == 0)
dstRect->h = int((float(surface->h) / surface->w) * dstRect->w);
}
}
void Visibility::reloadTexture(SDL_Renderer *renderer) {
texture = SDL_CreateTextureFromSurface(renderer, surface);
}
void Visibility::setPosition(int x, int y) {
- if (dstRect != nullptr) {
+ if (dstRect) {
dstRect->x = x;
dstRect->y = y;
}
}
diff --git a/src/components/Visibility.h b/src/components/Visibility.h
index 84c21c6..89fc46a 100644
--- a/src/components/Visibility.h
+++ b/src/components/Visibility.h
@@ -1,47 +1,42 @@
//
// Created by Ido Mozes on 23/06/2019.
//
#ifndef SDL2_GAME_VISIBILITY_H
#define SDL2_GAME_VISIBILITY_H
#include <fstream>
+#include <optional>
#include "SDL.h"
#include "SDL_image.h"
#include "../Component.h"
SDL_Texture *loadTexture(SDL_Renderer *renderer, const char *path);
class Visibility : public Component {
SDL_Surface *surface;
SDL_Texture *texture;
- SDL_Rect *dstRect;
+ std::optional<SDL_Rect> dstRect;
- void loadSurface(const char *filePath) { surface = IMG_Load(filePath); }
public:
static ComponentType getComponentType() { return ComponentType::VISIBILITY; }
- Visibility(Entity *entity, SDL_Renderer *renderer, const char *filePath, SDL_Rect *dstR = nullptr);
+ Visibility(Entity *entity, SDL_Renderer *renderer, SDL_Surface * newSurface,std::optional<SDL_Rect> dstR = std::nullopt);
~Visibility();
SDL_Texture *getTexture() { return texture; }
- SDL_Rect *getDstRect() { return dstRect; }
-
- void setDstRect(SDL_Rect *dstR) {
- dstRect = dstR ? new SDL_Rect(*dstR) : nullptr;
- delete dstR;
- }
+ SDL_Rect *getDstRect() { return dstRect ? &dstRect.value() : nullptr; }
void setPosition(int x, int y);
- void loadTexture(SDL_Renderer *renderer, const char *filePath);
+ void loadTexture(SDL_Renderer *renderer, SDL_Surface * newSurface);
void reloadTexture(SDL_Renderer *renderer);
};
#endif //SDL2_GAME_VISIBILITY_H
diff --git a/src/main.cpp b/src/main.cpp
index f8b6eb7..6faa8be 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -1,21 +1,21 @@
//#include <iostream>
#include "SDL.h"
#include "Game.h"
int main(int argc, char *argv[]) {
const int FPS = 60, frameDelay = 1000 / FPS;
Uint32 frameStart;
int frameTime;
- Game game(false);
+ Game game(false, 1.5);
while (game.running()) {
frameStart = SDL_GetTicks();
game.handleEvents();
game.update();
frameTime = SDL_GetTicks() - frameStart;
if (frameDelay > frameTime) {
SDL_Delay(frameDelay - frameTime);
}
}
return 0;
}
\ No newline at end of file
diff --git a/src/systems/MovementSystem.cpp b/src/systems/MovementSystem.cpp
index aae375c..532f1c3 100644
--- a/src/systems/MovementSystem.cpp
+++ b/src/systems/MovementSystem.cpp
@@ -1,65 +1,68 @@
//
// Created by Ido Mozes on 02/07/2019.
//
#include "MovementSystem.h"
-void MovementSystem::update(std::vector<std::shared_ptr<Entity>> &entities, GameData & gameData) {
+void MovementSystem::update(std::vector<std::shared_ptr<Entity>> &entities, GameData &gameData) {
for (auto &entity: entities) {
// Move all entities with Visibility and Position
if (entity->hasComponents(createMask({ComponentType::VISIBILITY, ComponentType::POSITION}))) {
auto &visibility = entity->getComponent<Visibility>();
auto &position = entity->getComponent<Position>();
float deltaX = 0, deltaY = 0;
if (entity->hasComponents(createMask({ComponentType::VELOCITY}))) {
auto &velocity = entity->getComponent<Velocity>();
if (entity->hasComponents(createMask({ACCELERATION}))) {
auto &acceleration = entity->getComponent<Acceleration>();
velocity.changeVelocity(acceleration.getX(), acceleration.getY());
}
deltaX = velocity.getX();
deltaY = velocity.getY();
+
} else if (entity->hasComponents(createMask({ComponentType::PATH_INDEX, ComponentType::SPEED}))) {
auto &pathIndex = entity->getComponent<PathIndex>();
auto &speed = entity->getComponent<Speed>();
- for (int i = 0; i < speed.speed; ++i) {
+ pathIndex.progress += speed.speed;
+ float deltaIndex = pathIndex.progress - pathIndex.index;
+ while (deltaIndex >= (gameData.path[pathIndex.index] % 2 == 0 ? 1 : sqrt(2))) {
switch (gameData.path[pathIndex.index]) {
case 0:
deltaX += 1;
break;
case 1:
- deltaX += sqrt(0.5);
- deltaY += sqrt(0.5);
+ deltaX += 1;
+ deltaY += 1;
break;
case 2:
deltaY += 1;
break;
case 3:
- deltaX += -sqrt(0.5);
- deltaY += sqrt(0.5);
+ deltaX += -1;
+ deltaY += 1;
break;
case 4:
deltaX += -1;
break;
case 5:
- deltaX += -sqrt(0.5);
- deltaY += -sqrt(0.5);
+ deltaX += -1;
+ deltaY += -1;
break;
case 6:
deltaY += -1;
break;
case 7:
- deltaX += sqrt(0.5);
- deltaY += -sqrt(0.5);
+ deltaX += 1;
+ deltaY += -1;
break;
}
+ deltaIndex -= (gameData.path[pathIndex.index] % 2 == 0 ? 1 : sqrt(2));
if (pathIndex.index < gameData.path.size() - 1)
pathIndex.index++;
}
}
- position.changePosition(deltaX * gameData.mapScale, deltaY * gameData.mapScale);
- visibility.setPosition(int(position.getX()), int(position.getY()));
+ position.changePosition(deltaX , deltaY);
}
}
}
diff --git a/src/systems/RenderSystem.cpp b/src/systems/RenderSystem.cpp
index 0f67273..3d0f820 100644
--- a/src/systems/RenderSystem.cpp
+++ b/src/systems/RenderSystem.cpp
@@ -1,37 +1,50 @@
//
// Created by Ido Mozes on 23/06/2019.
//
#include "RenderSystem.h"
#include "../components/Visibility.h"
+#include "../components/Position.h"
uint64_t RenderSystem::mask = createMask({ComponentType::VISIBILITY});
-RenderSystem::~RenderSystem() {
- SDL_DestroyWindow(window);
- SDL_DestroyRenderer(renderer);
+void RenderSystem::init(GameData &gameData) {
+ if (gameData.window != nullptr)
+ SDL_DestroyWindow(gameData.window);
+ if (gameData.renderer != nullptr)
+ SDL_DestroyRenderer(gameData.renderer);
+ gameData.window = SDL_CreateWindow("BloonsTD", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
+ 685 * gameData.mapScale,
+ 511 * gameData.mapScale, gameData.fullscreen ? SDL_WINDOW_FULLSCREEN : 0);
+ gameData.renderer = SDL_CreateRenderer(gameData.window, -1, 0);
+ SDL_SetRenderDrawColor(gameData.renderer, 255, 255, 255, 255);
}
-void RenderSystem::init(GameData &gameData){
- if (window != nullptr)
- SDL_DestroyWindow(window);
- if (renderer != nullptr)
- SDL_DestroyRenderer(renderer);
- window = SDL_CreateWindow("BloonsTD", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 700 *gameData.mapScale,
- 520 * gameData.mapScale, gameData.fullscreen ? SDL_WINDOW_FULLSCREEN : 0);
- renderer = SDL_CreateRenderer(window, -1, 0);
- SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
-}
-void RenderSystem::update(std::vector<std::shared_ptr<Entity>> &entities, GameData & gameData) {
- SDL_RenderClear(renderer);
+
+void RenderSystem::update(std::vector<std::shared_ptr<Entity>> &entities, GameData &gameData) {
+ SDL_RenderClear(gameData.renderer);
for (auto &entity: entities) {
if (entity->hasComponents(mask)) {
- auto &v = entity->getComponent<Visibility>();
- SDL_RenderCopy(renderer, v.getTexture(), nullptr, v.getDstRect());
+ auto &visibility = entity->getComponent<Visibility>();
+ if (visibility.getDstRect()) {
+ SDL_Rect r = {int(visibility.getDstRect()->x * gameData.mapScale),
+ int(visibility.getDstRect()->y * gameData.mapScale),
+ int(visibility.getDstRect()->w * gameData.mapScale),
+ int(visibility.getDstRect()->h * gameData.mapScale)};
+ if (entity->hasComponents(createMask({ComponentType::POSITION}))) {
+ auto &position = entity->getComponent<Position>();
+ r.x = int(position.getX() * gameData.mapScale);
+ r.y = int(position.getY() * gameData.mapScale);
+ }
+ r.x -= int(r.w / 2.0);
+ r.y -= int(r.h / 2.0);
+ SDL_RenderCopy(gameData.renderer, visibility.getTexture(), nullptr, &r);
+ } else
+ SDL_RenderCopy(gameData.renderer, visibility.getTexture(), nullptr, nullptr);
}
}
- SDL_RenderPresent(renderer);
+ SDL_RenderPresent(gameData.renderer);
}
diff --git a/src/systems/RenderSystem.h b/src/systems/RenderSystem.h
index fa6de46..7ef4e86 100644
--- a/src/systems/RenderSystem.h
+++ b/src/systems/RenderSystem.h
@@ -1,29 +1,25 @@
//
// Created by Ido Mozes on 23/06/2019.
//
#ifndef SDL2_GAME_RENDERSYSTEM_H
#define SDL2_GAME_RENDERSYSTEM_H
#include <iostream>
#include "../components/Visibility.h"
#include "../System.h"
#include "../GameData.h"
class RenderSystem : public System {
static uint64_t mask;
- SDL_Window *window = nullptr;
- SDL_Renderer *renderer = nullptr;
+
public:
RenderSystem() { SDL_Init(SDL_INIT_EVERYTHING); }
- ~RenderSystem();
void init(GameData &gameData);
- SDL_Renderer *getRenderer() { return renderer; }
-
void update(std::vector<std::shared_ptr<Entity>> &entities , GameData & gameData) override;
};
#endif //SDL2_GAME_RENDERSYSTEM_H
diff --git a/src/systems/SpawnSystem.cpp b/src/systems/SpawnSystem.cpp
new file mode 100644
index 0000000..666955f
--- /dev/null
+++ b/src/systems/SpawnSystem.cpp
@@ -0,0 +1,30 @@
+//
+// Created by Ido Mozes on 07/07/2019.
+//
+
+#include "SpawnSystem.h"
+
+uint64_t SpawnSystem::mask = createMask({ComponentType::SEQUENCE, ComponentType::KIND});
+
+void SpawnSystem::update(std::vector<std::shared_ptr<Entity>> &entities, GameData &gameData) {
+ std::vector<std::shared_ptr<Entity>> newEntities;
+ for (auto &entity : entities) {
+ if (entity->hasComponents(mask)) {
+ int amount = entity->getComponent<Sequence>().getAmountReady();
+ for (int i = 0; i < amount; ++i) {
+ auto *newEntity = new Entity();
+ newEntity->addComponent<Kind>(entity->getComponent<Kind>().kind);
+ newEntity->addComponent<Position>(gameData.startingPoint.X, gameData.startingPoint.Y);
+ newEntity->addComponent<PathIndex>(0);
+ newEntity->addComponent<Speed>(entity->getComponent<Speed>().speed);
+ SDL_Surface *surface = gameData.assets[entity->getComponent<Kind>().kind];
+ newEntity->addComponent<Visibility>(gameData.renderer, surface,
+ SDL_Rect{gameData.startingPoint.X, gameData.startingPoint.Y,
+ surface->w/3, surface->h/3});
+ newEntities.emplace_back(newEntity);
+ }
+ }
+ }
+ entities.insert(entities.end(), std::make_move_iterator(newEntities.begin()),
+ std::make_move_iterator(newEntities.end()));
+}
diff --git a/src/systems/SpawnSystem.h b/src/systems/SpawnSystem.h
new file mode 100644
index 0000000..0f3a904
--- /dev/null
+++ b/src/systems/SpawnSystem.h
@@ -0,0 +1,24 @@
+//
+// Created by Ido Mozes on 07/07/2019.
+//
+
+#ifndef SDL_GAME_SPAWNSYSTEM_H
+#define SDL_GAME_SPAWNSYSTEM_H
+
+#include <vector>
+#include <iterator>
+#include "../System.h"
+#include "../components/Sequence.h"
+#include "../components/Speed.h"
+#include "../components/Kind.h"
+#include "../components/Position.h"
+#include "../components/Visibility.h"
+#include "../components/PathIndex.h"
+
+class SpawnSystem : public System {
+ static uint64_t mask;
+public:
+ void update(std::vector<std::shared_ptr<Entity>> &entities, GameData &gameData) override;
+};
+
+#endif //SDL_GAME_SPAWNSYSTEM_H

File Metadata

Mime Type
text/x-diff
Expires
Tue, Jun 16, 12:09 AM (2 w, 2 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
70591
Default Alt Text
(24 KB)

Event Timeline