Page Menu
Home
Phabricator (Chris)
Search
Configure Global Search
Log In
Files
F133505
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Authored By
Unknown
Size
22 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/src/Game.cpp b/src/Game.cpp
index 0750cb0..27848eb 100644
--- a/src/Game.cpp
+++ b/src/Game.cpp
@@ -1,78 +1,89 @@
//
// 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");
+ gameData.assets["upgrade_bar"] = IMG_Load("../assets/upgrade_bar.jpg");
+ gameData.assets["menu"] = IMG_Load("../assets/menu.jpg");
renderSystem.init(gameData);
loadMap();
auto mapEntity = new Entity();
- mapEntity->addComponent<Visibility>(gameData.renderer, gameData.assets["map"]);
- entities.emplace_back(mapEntity);
+ mapEntity->addComponent<Visibility>(gameData.renderer, gameData.assets["map"],
+ SDL_Rect{150, 0, gameData.assets["map"]->w, gameData.assets["map"]->h});
+ layers[0].emplace_back(mapEntity);
+ auto upgrade_bar = new Entity();
+ upgrade_bar->addComponent<Visibility>(gameData.renderer, gameData.assets["upgrade_bar"],
+ SDL_Rect{0, 0, gameData.assets["upgrade_bar"]->w, gameData.assets["upgrade_bar"]->h});
+ layers[1].emplace_back(upgrade_bar);
+ auto menu = new Entity();
+ menu->addComponent<Visibility>(gameData.renderer, gameData.assets["menu"],
+ SDL_Rect{685+150, 0, gameData.assets["menu"]->w, gameData.assets["menu"]->h});
+ layers[1].emplace_back(menu);
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<Sequence>(100, 10, 0);
+ s->addComponent<Kind>(std::string("Ceramic"));
+ s->addComponent<Speed>(3.5);
+ layers[0].emplace_back(s);
+ s = new Entity();
+ s->addComponent<Sequence>(100, 10, 60 * 5);
s->addComponent<Kind>(std::string("Blue"));
s->addComponent<Speed>(1.5);
- entities.emplace_back(s);
+ layers[0].emplace_back(s);
systems.emplace_back(new SpawnSystem);
systems.emplace_back(new MovementSystem);
+ systems.emplace_back(&renderSystem);
}
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);
+ system->update(layers, gameData);
}
- renderSystem.update(entities, gameData);
}
void Game::loadMap() {
gameData.path.clear();
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 2888d9e..31c70cd 100644
--- a/src/Game.h
+++ b/src/Game.h
@@ -1,49 +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;
+ std::vector<std::shared_ptr<Entity>> layers[N_LAYERS];
RenderSystem renderSystem;
GameData gameData;
public:
explicit Game(bool fullscreen, float mapScale=1.5);
~Game();
void handleEvents();
void update();
bool running() { return gameData.isRunning; }
void loadMap();
};
#endif //SDL2_GAME_GAME_H
diff --git a/src/GameData.h b/src/GameData.h
index 11a6a29..d7e699a 100644
--- a/src/GameData.h
+++ b/src/GameData.h
@@ -1,31 +1,31 @@
//
// Created by Ido Mozes on 03/07/2019.
//
#ifndef SDL_GAME_GAMEDATA_H
#define SDL_GAME_GAMEDATA_H
-
+constexpr int N_LAYERS = 2;
#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 **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/System.h b/src/System.h
index 3af893a..332f21b 100644
--- a/src/System.h
+++ b/src/System.h
@@ -1,24 +1,24 @@
//
// Created by Ido Mozes on 23/06/2019.
//
#ifndef SDL2_GAME_SYSTEM_H
#define SDL2_GAME_SYSTEM_H
#include <initializer_list>
#include <vector>
#include <cmath>
#include "Entity.h"
#include "GameData.h"
constexpr uint64_t createMask( std::initializer_list<int> types) noexcept {
uint64_t mask = 0;
for (int bit : types) {
mask |= (uint64_t) pow(2, bit);
}
return mask;
}
class System{
public:
- virtual void update(std::vector<std::shared_ptr<Entity>> &entities, GameData & gameData)=0;
+ virtual void update(std::vector<std::shared_ptr<Entity>> *entities, GameData & gameData)=0;
};
#endif //SDL2_GAME_SYSTEM_H
diff --git a/src/components/Sequence.h b/src/components/Sequence.h
index 83ba584..411dcaf 100644
--- a/src/components/Sequence.h
+++ b/src/components/Sequence.h
@@ -1,30 +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)
+constexpr int 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/systems/MovementSystem.cpp b/src/systems/MovementSystem.cpp
index 532f1c3..d822eb3 100644
--- a/src/systems/MovementSystem.cpp
+++ b/src/systems/MovementSystem.cpp
@@ -1,68 +1,70 @@
//
// Created by Ido Mozes on 02/07/2019.
//
#include "MovementSystem.h"
-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();
+void MovementSystem::update(std::vector<std::shared_ptr<Entity>> *layers, GameData &gameData) {
+ for (int i = 0; i < N_LAYERS; ++i) {
+ for (auto &entity: layers[i]) {
+ // 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>();
- 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 += 1;
- deltaY += 1;
- break;
- case 2:
- deltaY += 1;
- break;
- case 3:
- deltaX += -1;
- deltaY += 1;
- break;
- case 4:
- deltaX += -1;
- break;
- case 5:
- deltaX += -1;
- deltaY += -1;
- break;
- case 6:
- deltaY += -1;
- break;
- case 7:
- deltaX += 1;
- deltaY += -1;
- break;
+ } else if (entity->hasComponents(createMask({ComponentType::PATH_INDEX, ComponentType::SPEED}))) {
+ auto &pathIndex = entity->getComponent<PathIndex>();
+ auto &speed = entity->getComponent<Speed>();
+ 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 += 1;
+ deltaY += 1;
+ break;
+ case 2:
+ deltaY += 1;
+ break;
+ case 3:
+ deltaX += -1;
+ deltaY += 1;
+ break;
+ case 4:
+ deltaX += -1;
+ break;
+ case 5:
+ deltaX += -1;
+ deltaY += -1;
+ break;
+ case 6:
+ deltaY += -1;
+ break;
+ case 7:
+ deltaX += 1;
+ deltaY += -1;
+ break;
+ }
+ deltaIndex -= (gameData.path[pathIndex.index] % 2 == 0 ? 1 : sqrt(2));
+ if (pathIndex.index < gameData.path.size() - 1)
+ pathIndex.index++;
}
- deltaIndex -= (gameData.path[pathIndex.index] % 2 == 0 ? 1 : sqrt(2));
- if (pathIndex.index < gameData.path.size() - 1)
- pathIndex.index++;
}
+ position.changePosition(deltaX, deltaY);
}
- position.changePosition(deltaX , deltaY);
}
}
}
diff --git a/src/systems/MovementSystem.h b/src/systems/MovementSystem.h
index e646949..bb2ede3 100644
--- a/src/systems/MovementSystem.h
+++ b/src/systems/MovementSystem.h
@@ -1,23 +1,23 @@
//
// Created by Ido Mozes on 02/07/2019.
//
#ifndef SDL_GAME_MOVEMENTSYSTEM_H
#define SDL_GAME_MOVEMENTSYSTEM_H
#include <iostream>
#include "../components/Visibility.h"
#include "../components/Velocity.h"
#include "../components/Position.h"
#include "../components/PathIndex.h"
#include "../components/Speed.h"
#include "../GameData.h"
#include "../System.h"
class MovementSystem : public System{
public:
MovementSystem()= default;
~MovementSystem()= default;
- void update(std::vector<std::shared_ptr<Entity>> &entities, GameData & gameData) override;
+ void update(std::vector<std::shared_ptr<Entity>> *layers, GameData & gameData) override;
};
#endif //SDL_GAME_MOVEMENTSYSTEM_H
diff --git a/src/systems/RenderSystem.cpp b/src/systems/RenderSystem.cpp
index 3d0f820..9665c52 100644
--- a/src/systems/RenderSystem.cpp
+++ b/src/systems/RenderSystem.cpp
@@ -1,50 +1,52 @@
//
// 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});
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,
+ (685 + 400) * 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::update(std::vector<std::shared_ptr<Entity>> &entities, GameData &gameData) {
+void RenderSystem::update(std::vector<std::shared_ptr<Entity>> *layers, GameData &gameData) {
SDL_RenderClear(gameData.renderer);
- for (auto &entity: entities) {
- if (entity->hasComponents(mask)) {
- auto &visibility = entity->getComponent<Visibility>();
- if (visibility.getDstRect()) {
+ for (int i = 0; i < N_LAYERS; ++i) {
+ for (auto &entity: layers[i]) {
+ if (entity->hasComponents(mask)) {
+ auto &visibility = entity->getComponent<Visibility>();
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);
+ 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);
+ r.x += int(150 * 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(gameData.renderer);
}
diff --git a/src/systems/RenderSystem.h b/src/systems/RenderSystem.h
index 7ef4e86..c3d9ff2 100644
--- a/src/systems/RenderSystem.h
+++ b/src/systems/RenderSystem.h
@@ -1,25 +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;
public:
RenderSystem() { SDL_Init(SDL_INIT_EVERYTHING); }
void init(GameData &gameData);
- void update(std::vector<std::shared_ptr<Entity>> &entities , GameData & gameData) override;
+ void update(std::vector<std::shared_ptr<Entity>> *layers , GameData & gameData) override;
};
#endif //SDL2_GAME_RENDERSYSTEM_H
diff --git a/src/systems/SpawnSystem.cpp b/src/systems/SpawnSystem.cpp
index 666955f..67a351d 100644
--- a/src/systems/SpawnSystem.cpp
+++ b/src/systems/SpawnSystem.cpp
@@ -1,30 +1,32 @@
//
// 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);
+void SpawnSystem::update(std::vector<std::shared_ptr<Entity>> *layers, GameData &gameData) {
+ for (int i = 0; i < N_LAYERS; ++i) {
+ std::vector<std::shared_ptr<Entity>> newEntities;
+ for (auto &entity: layers[i]) {
+ 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);
+ }
}
}
+ layers[i].insert(layers[i].end(), std::make_move_iterator(newEntities.begin()),
+ std::make_move_iterator(newEntities.end()));
}
- 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
index 0f3a904..ce93985 100644
--- a/src/systems/SpawnSystem.h
+++ b/src/systems/SpawnSystem.h
@@ -1,24 +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;
+ void update(std::vector<std::shared_ptr<Entity>> *layers, GameData &gameData) override;
};
#endif //SDL_GAME_SPAWNSYSTEM_H
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Tue, Jun 16, 2:00 AM (2 w, 12 h ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
70780
Default Alt Text
(22 KB)
Attached To
Mode
R74 BloonsTD
Attached
Detach File
Event Timeline