Page Menu
Home
Phabricator (Chris)
Search
Configure Global Search
Log In
Files
F134038
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Authored By
Unknown
Size
17 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/src/Game.cpp b/src/Game.cpp
index 1287469..411b587 100644
--- a/src/Game.cpp
+++ b/src/Game.cpp
@@ -1,124 +1,124 @@
//
// 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["upgrade_bar2"] = IMG_Load("../assets/upgrade_bar2.jpg");
gameData.assets["menu"] = IMG_Load("../assets/menu.jpg");
gameData.assets["Super_Monkey"] = IMG_Load("../assets/Super_Monkey.png");
gameData.assets["Sniper_Monkey"] = IMG_Load("../assets/Sniper_Monkey.png");
renderSystem = new RenderSystem();
renderSystem->init(gameData);
loadMap();
std::initializer_list<std::pair<std::string, Point>> sprites[]{
{{"map", {SIDEBAR_WIDTH, 0}}},
{},
+ {},
{{"upgrade_bar", {0, 0}}, {"menu", {MAP_WIDTH + SIDEBAR_WIDTH, 0}}},
- {}
};
for (int i = 0; i < N_LAYERS; i++) {
for (auto &sprite :sprites[i]) {
auto spriteEntity = new Entity();
spriteEntity->addComponent<Visibility>(gameData.renderer, gameData.assets[sprite.first],
SDL_Rect{sprite.second.X, sprite.second.Y,
gameData.assets[sprite.first]->w,
gameData.assets[sprite.first]->h});
layers[i].emplace_back(spriteEntity);
}
}
auto button = new Entity();
button->addComponent<Kind>("Super_Monkey");
button->addComponent<Visibility>(gameData.renderer, gameData.assets["Super_Monkey"],
SDL_Rect{SIDEBAR_WIDTH + MAP_WIDTH + 10, 10,
int(gameData.assets["Super_Monkey"]->w / 1.5), 0});
button->addComponent<Action>(DRAG);
button->addComponent<Range>(200);
layers[MENU_LAYER].emplace_back(button);
button = new Entity();
button->addComponent<Kind>("Sniper_Monkey");
button->addComponent<Visibility>(gameData.renderer, gameData.assets["Sniper_Monkey"],
SDL_Rect{SIDEBAR_WIDTH + MAP_WIDTH + 100, 10,
int(gameData.assets["Sniper_Monkey"]->w / 1.5), 0});
button->addComponent<Action>(DRAG);
- button->addComponent<Range>(50);
+ button->addComponent<Range>(30);
layers[MENU_LAYER].emplace_back(button);
-// auto s = new Entity();
-// s->addComponent<Sequence>(100, 10, 0);
-// s->addComponent<Kind>(std::string("Ceramic"));
-// s->addComponent<Speed>(3.5);
-// layers[GAME_LAYER].emplace_back(s);
-// s = new Entity();
-// s->addComponent<Sequence>(100, 10, 60 * 5);
-// s->addComponent<Kind>(std::string("Blue"));
-// s->addComponent<Speed>(1.5);
-// layers[GAME_LAYER].emplace_back(s);
+ auto s = new Entity();
+ s->addComponent<Sequence>(100, 10, 0);
+ s->addComponent<Kind>(std::string("Ceramic"));
+ s->addComponent<Speed>(3.5);
+ layers[GAME_LAYER].emplace_back(s);
+ s = new Entity();
+ s->addComponent<Sequence>(100, 10, 60 * 5);
+ s->addComponent<Kind>(std::string("Blue"));
+ s->addComponent<Speed>(1.5);
+ layers[GAME_LAYER].emplace_back(s);
systems.emplace_back(new EventSystem);
systems.emplace_back(new SpawnSystem);
systems.emplace_back(new DraggingSystem);
systems.emplace_back(new MovementSystem);
systems.emplace_back(new HandleMoveEntitiySystem);
systems.emplace_back(new RemoveEntitiesSystem);
systems.emplace_back(renderSystem);
}
Game::~Game() {
SDL_Quit();
}
void Game::update() {
for (auto &system : systems) {
system->update(layers, gameData);
}
}
void Game::loadMap() {
std::string fileName = "../assets/map" + std::to_string(gameData.map);
std::ifstream obstaclesFile(fileName + "_obstacles.data", std::ios::binary);
int x = 0, y = 0;
for (int i = 0; i < ceilf(MAP_WIDTH * MAP_HEIGHT / 8.0); ++i) {
unsigned char byte;
obstaclesFile.read((char *) &byte, 1);
for (int j = 0; j < 8; ++j) {
char bit = (byte & 2 << j) >> j;
gameData.mapData[x][y] = bit;
x++;
if (x == MAP_WIDTH) {
x = 0;
y++;
}
if (x * y >= MAP_WIDTH * MAP_HEIGHT)
goto readPathFile;
}
}
readPathFile:
gameData.path.clear();
std::ifstream pathFile(fileName + "_path.data", std::ios::binary);
unsigned int length;
pathFile.read((char *) &gameData.startingPoint, 4);
pathFile.read((char *) &length, 4);
gameData.path.resize(length);
pathFile.read(&gameData.path[0], length);
pathFile.read((char *) &gameData.finishPoint, 4);
}
diff --git a/src/GameData.h b/src/GameData.h
index 3a544d9..3013f1a 100644
--- a/src/GameData.h
+++ b/src/GameData.h
@@ -1,40 +1,39 @@
//
// Created by Ido Mozes on 03/07/2019.
//
#ifndef SDL_GAME_GAMEDATA_H
#define SDL_GAME_GAMEDATA_H
#include <vector>
#include <memory>
#include <unordered_map>
#include "Component.h"
#include "Settings.h"
#include "SDL.h"
constexpr char FREE = 0;
constexpr char OBSTACLE = 1;
constexpr char TOWER = 2;
constexpr char BLOON = 3;
class GameData {
public:
bool isRunning = true;
bool isDragging = false;
int level = 0;
int map = 0;
float mapScale = 1.5;
bool fullscreen;
std::vector<char> path;
char mapData[MAP_WIDTH][MAP_HEIGHT];
Point startingPoint;
Point finishPoint;
std::unordered_map<std::string, SDL_Surface *> assets;
SDL_Window *window = nullptr;
SDL_Renderer *renderer = nullptr;
std::shared_ptr<Entity> selected;
- bool selectedHasRangeShadow = false;
~GameData();
};
#endif //SDL_GAME_GAMEDATA_H
diff --git a/src/systems/EventSystem.cpp b/src/systems/EventSystem.cpp
index 7854e84..1beffad 100644
--- a/src/systems/EventSystem.cpp
+++ b/src/systems/EventSystem.cpp
@@ -1,125 +1,122 @@
//
// Created by Ido Mozes on 08/07/2019.
//
#include "EventSystem.h"
void EventSystem::update(Entities *layers, GameData &gameData) {
SDL_PumpEvents();
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT: {
gameData.isRunning = false;
break;
}
case SDL_MOUSEBUTTONDOWN: {
if (!gameData.isDragging)
gameData.selected.reset();
int mouseX, mouseY, originalMouseX;
SDL_GetMouseState(&mouseX, &mouseY);
mouseX = originalMouseX = int(mouseX / gameData.mapScale);
mouseY = int(mouseY / gameData.mapScale);
bool entityClicked = false;
Entities newEntities[N_LAYERS];
for (int i = N_LAYERS - 1; i >= 0; --i) {
for (auto &entity: layers[i]) {
if (auto components = entity->getComponents<Action, Visibility>()) {
auto[action, visibility] = components.value();
if (action.disabled or (action.actionType != DROP and gameData.isDragging))
continue;
int entityX, entityY, w, h;
SDL_Rect *dstRect = visibility.getDstRect();
entityX = dstRect->x;
entityY = dstRect->y;
w = dstRect->w;
h = dstRect->h;
if (auto positionP = entity->getComponent<Position>()) {
auto &position = *positionP;
entityX = int(position.getX() - w / 2.0);
entityY = int(position.getY() - h / 2.0);
mouseX = originalMouseX - SIDEBAR_WIDTH;
} else
mouseX = originalMouseX;
if (entityX <= mouseX and mouseX <= entityX + w and entityY <= mouseY and
mouseY <= entityY + h) {
switch (action.actionType) {
case DRAG: {
auto &kind = *entity->getComponent<Kind>();
auto &range = *entity->getComponent<Range>();
auto draggable = new Entity();
SDL_Surface *surface = gameData.assets[kind.kind];
draggable->addComponent<Visibility>(gameData.renderer, surface, SDL_Rect{
originalMouseX - int(surface->w / 4.0), mouseY - int(surface->h / 4.0),
int(surface->w / 1.5), int(surface->h / 1.5)});
draggable->addComponent<Draggable>();
draggable->addComponent<Action>(DROP);
draggable->addComponent<Kind>(kind);
draggable->addComponent<Range>(range);
std::shared_ptr<Entity> ptr(draggable);
gameData.selected = ptr;
+ auto rangeShadow = new Entity();
+ rangeShadow->addComponent<RangeShadow>(ptr);
+ newEntities[SHADOW_LAYER].emplace_back(rangeShadow);
newEntities[MENU_LAYER].emplace_back(ptr);
gameData.isDragging = true;
- gameData.selectedHasRangeShadow = false;
goto entityClicked;
}
case CLICK: {
goto entityClicked;
}
case DROP: {
auto &draggable = *entity->getComponent<Draggable>();
if (draggable.isPlaceable) {
entity->removeComponent<Draggable>();
entity->getComponent<Action>()->actionType = SELECT;
gameData.isDragging = false;
for (int x = std::max(mouseX - SIDEBAR_WIDTH - 20, 0);
x < std::min(mouseX - SIDEBAR_WIDTH + 21, MAP_WIDTH); ++x) {
for (int y = std::max(mouseY - 20, 0);
y < std::min(mouseY + 21, MAP_HEIGHT); ++y) {
if (gameData.mapData[x][y] == FREE)
gameData.mapData[x][y] = TOWER;
}
}
if (i == MENU_LAYER){
- gameData.selectedHasRangeShadow=true;
- auto rangeShadow = new Entity();
- rangeShadow->addComponent<RangeShadow>(entity);
- newEntities[SHADOW_LAYER].emplace_back(rangeShadow);
entity->addComponent<MoveEntityEvent>(GAME_LAYER);
}
}
goto entityClicked;
}
case SELECT: {
gameData.selected = entity;
- gameData.selectedHasRangeShadow=true;
goto entityClicked;
}
}
}
}
}
}
entityClicked:
for (int i = 0; i < N_LAYERS; ++i) {
if (!newEntities[i].empty())
layers[i] += newEntities[i];
}
break;
}
default:
break;
}
}
}
diff --git a/src/systems/RenderSystem.cpp b/src/systems/RenderSystem.cpp
index 163899b..e4e9089 100644
--- a/src/systems/RenderSystem.cpp
+++ b/src/systems/RenderSystem.cpp
@@ -1,71 +1,69 @@
//
// Created by Ido Mozes on 23/06/2019.
//
#include "RenderSystem.h"
#include "../components/Visibility.h"
#include "../components/Position.h"
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, // NOLINT(hicpp-signed-bitwise)
int((MAP_WIDTH + SIDEBAR_WIDTH + MENU_WIDTH) * gameData.mapScale),
int(MAP_HEIGHT * 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(Entities *layers, GameData &gameData) {
SDL_RenderClear(gameData.renderer);
for (int i = 0; i < N_LAYERS; ++i) {
for (auto &entity: layers[i]) {
auto rangeShadowP = entity->getComponent<RangeShadow>();
auto ¤tEntity = (rangeShadowP and rangeShadowP->entity == gameData.selected) ? rangeShadowP->entity
: entity;
if (auto visibilityP = currentEntity->getComponent<Visibility>()) {
auto &visibility = *visibilityP;
auto positionP = currentEntity->getComponent<Position>();
SDL_Rect *dstRect = visibility.getDstRect();
SDL_Rect r = {int(dstRect->x * gameData.mapScale), int(dstRect->y * gameData.mapScale),
int(dstRect->w * gameData.mapScale), int(dstRect->h * gameData.mapScale)};
if (positionP) {
auto &position = *positionP;
r.x = int((position.getX() + SIDEBAR_WIDTH) * gameData.mapScale - r.w / 2.0);
r.y = int(position.getY() * gameData.mapScale - r.h / 2.0);
}
- if (gameData.selected == currentEntity and
- ((gameData.selectedHasRangeShadow and currentEntity != entity) or
- (!gameData.selectedHasRangeShadow and currentEntity == entity))) {
+ if (gameData.selected == currentEntity and currentEntity != entity){
int currentEntityX, currentEntityY;
if (positionP) {
currentEntityX = r.x;
currentEntityY = r.y;
} else {
currentEntityX = r.x + int((dstRect->w / 2.0) * gameData.mapScale);
currentEntityY = r.y + int((dstRect->h / 2.0) * gameData.mapScale);
}
auto draggableP = currentEntity->getComponent<Draggable>();
bool isRed = draggableP ? !draggableP->isPlaceable : false;
float range = currentEntity->getComponent<Range>()->range;
filledCircleRGBA(gameData.renderer, currentEntityX, currentEntityY, range, isRed ? 255 : 0, 0, 0,
100);
aacircleRGBA(gameData.renderer, currentEntityX, currentEntityY, range, isRed ? 255 : 0, 0, 0, 150);
}
if (entity == currentEntity)
SDL_RenderCopy(gameData.renderer, visibility.getTexture(), nullptr, &r);
}
}
}
SDL_RenderPresent(gameData.renderer);
}
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Wed, Jun 17, 9:14 PM (1 w, 5 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
72619
Default Alt Text
(17 KB)
Attached To
Mode
R74 BloonsTD
Attached
Detach File
Event Timeline