Page MenuHomePhabricator (Chris)

No OneTemporary

Authored By
Unknown
Size
46 KB
Referenced Files
None
Subscribers
None
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 6de4187..8b53919 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,10 +1,10 @@
cmake_minimum_required(VERSION 3.14)
project(SDL_Game)
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 SDL2_gfx)
-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 src/components/Action.cpp src/components/Action.h src/systems/EventSystem.cpp src/systems/EventSystem.h src/components/Draggable.h src/systems/DraggingSystem.cpp src/systems/DraggingSystem.h src/Settings.h src/components/Type.h)
+add_executable(SDL_Game src/main.cpp src/Game.cpp src/Game.h src/Entity.h src/Component.h 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.h 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 src/components/Action.h src/systems/EventSystem.cpp src/systems/EventSystem.h src/components/Draggable.h src/systems/DraggingSystem.cpp src/systems/DraggingSystem.h src/Settings.h src/components/Type.h src/systems/RemoveEntitiesSystem.cpp src/systems/RemoveEntitiesSystem.h src/eventComponents/RemoveEntityEvent.h src/eventComponents/MoveEntityEvent.h src/systems/HandleMoveEntitiySystem.cpp src/systems/HandleMoveEntitiySystem.h src/components/Range.h)
target_link_libraries(SDL_Game ${Boost_LIBRARIES})
\ No newline at end of file
diff --git a/src/Component.cpp b/src/Component.cpp
deleted file mode 100644
index 22d4c59..0000000
--- a/src/Component.cpp
+++ /dev/null
@@ -1,6 +0,0 @@
-//
-// Created by Ido Mozes on 20/06/2019.
-//
-
-#include "Component.h"
-
diff --git a/src/Component.h b/src/Component.h
index 912f515..8ab6c76 100644
--- a/src/Component.h
+++ b/src/Component.h
@@ -1,45 +1,29 @@
//
// 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,
- SEQUENCE,
- ACTION,
- DRAGGABLE,
+ VISIBILITY, POSITION, VELOCITY, SPEED, ACCELERATION, PATH_INDEX, HEALTH, KIND, TYPE,RANGE, DAMAGE, PIERCE, SPREAD, SEQUENCE, ACTION, DRAGGABLE,
+ MOVE_ENTITY_EVENT, REMOVE_ENTITY_EVENT,
LENGTH
};
class Component {
- Entity *entity;
public:
- explicit Component(Entity *entity) : entity(entity) {};
+ Component() = default;
virtual ~Component() = default;
- Entity *getEntity() { return entity; }
-
};
#endif //SDL2_GAME_COMPONENT_H
diff --git a/src/Entity.cpp b/src/Entity.cpp
deleted file mode 100644
index 1149efd..0000000
--- a/src/Entity.cpp
+++ /dev/null
@@ -1,7 +0,0 @@
-//
-// Created by Ido Mozes on 20/06/2019.
-//
-
-#include "Entity.h"
-
-
diff --git a/src/Entity.h b/src/Entity.h
index 75bb20f..15a04ac 100644
--- a/src/Entity.h
+++ b/src/Entity.h
@@ -1,61 +1,60 @@
//
// Created by Ido Mozes on 20/06/2019.
//
#ifndef SDL2_GAME_ENTITY_H
#define SDL2_GAME_ENTITY_H
#include <bitset>
#include <vector>
#include <cmath>
#include <memory>
#include <optional>
#include "Component.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 Entity {
std::bitset<ComponentType::LENGTH> componentsMask;
std::unique_ptr<Component> components[ComponentType::LENGTH];
public:
-
template<class ... T>
std::optional<std::tuple<T &...>> getComponents() {
constexpr uint64_t mask = createMask({T::type...});
if ((componentsMask.to_ullong() & mask) == mask) {
return std::tuple<T &...>((*(T *) components[T::type].get())...);
}
return std::nullopt;
}
template<class T>
T *getComponent() {
if (auto &c = components[T::type])
return (T *) c.get();
return nullptr;
}
template<typename T, typename ... Targs>
Component &addComponent(Targs &&... args) {
- T *c = new T(this, std::forward<Targs>(args)...);
+ T *c = new T(std::forward<Targs>(args)...);
components[T::type] = std::move(std::unique_ptr<Component>(c));
componentsMask[T::type] = true;
return *c;
}
template<typename T>
void removeComponent() {
- components[T::type].release();
+ components[T::type].reset(nullptr);
componentsMask[T::type] = false;
}
};
#endif //SDL2_GAME_ENTITY_H
diff --git a/src/Game.cpp b/src/Game.cpp
index 2575aec..195b2de 100644
--- a/src/Game.cpp
+++ b/src/Game.cpp
@@ -1,120 +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, gameData.assets["Super_Monkey"]->w,
- gameData.assets["Super_Monkey"]->h});
+ SDL_Rect{SIDEBAR_WIDTH + MAP_WIDTH + 10, 10,
+ int(gameData.assets["Super_Monkey"]->w / 1.5), 0});
button->addComponent<Action>(DRAG);
- layers[3].emplace_back(button);
+ 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, gameData.assets["Sniper_Monkey"]->w,
- gameData.assets["Sniper_Monkey"]->h});
+ SDL_Rect{SIDEBAR_WIDTH + MAP_WIDTH + 100, 10,
+ int(gameData.assets["Sniper_Monkey"]->w / 1.5), 0});
button->addComponent<Action>(DRAG);
- layers[3].emplace_back(button);
+ button->addComponent<Range>(50);
+ 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[1].emplace_back(s);
+ 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[1].emplace_back(s);
+ 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 & int(pow(2, j))) >> j;
+ char bit = (byte & 2 << j) >> j;
gameData.mapData[x][y] = bit;
x++;
- if(x ==MAP_WIDTH){
+ if (x == MAP_WIDTH) {
x = 0;
y++;
}
- if (x*y >= MAP_WIDTH * MAP_HEIGHT)
+ 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/Game.h b/src/Game.h
index 0ad6355..8824648 100644
--- a/src/Game.h
+++ b/src/Game.h
@@ -1,46 +1,48 @@
//
// Created by Ido Mozes on 18/06/2019.
//
#ifndef SDL2_GAME_GAME_H
#define SDL2_GAME_GAME_H
#include <iostream>
#include <cmath>
#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/EventSystem.h"
#include "systems/SpawnSystem.h"
#include "systems/DraggingSystem.h"
+#include "systems/HandleMoveEntitiySystem.h"
+#include "systems/RemoveEntitiesSystem.h"
#include "GameData.h"
#include "boost/filesystem.hpp"
#include <iostream>
class Game {
std::vector<std::unique_ptr<System>> systems;
Entities layers[N_LAYERS];
RenderSystem * renderSystem;
GameData gameData;
public:
explicit Game(bool fullscreen, float mapScale=1.5);
~Game();
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 075d864..3013f1a 100644
--- a/src/GameData.h
+++ b/src/GameData.h
@@ -1,33 +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;
~GameData();
};
#endif //SDL_GAME_GAMEDATA_H
diff --git a/src/Settings.h b/src/Settings.h
index 99f2478..dca535e 100644
--- a/src/Settings.h
+++ b/src/Settings.h
@@ -1,12 +1,15 @@
//
// Created by Ido Mozes on 09/07/2019.
//
#ifndef SDL_GAME_SETTINGS_H
#define SDL_GAME_SETTINGS_H
+enum Layers{
+ BACKGROUND_LAYER,GAME_LAYER,MENU_LAYER,
+ N_LAYERS
+};
constexpr int SIDEBAR_WIDTH = 150;
constexpr int MENU_WIDTH = 250;
constexpr int MAP_WIDTH = 686;
constexpr int MAP_HEIGHT = 511;
-constexpr int N_LAYERS = 4;
#endif //SDL_GAME_SETTINGS_H
diff --git a/src/System.cpp b/src/System.cpp
deleted file mode 100644
index 2c9b5b8..0000000
--- a/src/System.cpp
+++ /dev/null
@@ -1,6 +0,0 @@
-//
-// Created by Ido Mozes on 23/06/2019.
-//
-
-#include "System.h"
-
diff --git a/src/System.h b/src/System.h
index 34233e7..b86504d 100644
--- a/src/System.h
+++ b/src/System.h
@@ -1,36 +1,39 @@
//
// 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"
#include "components/Sequence.h"
#include "components/Speed.h"
#include "components/Kind.h"
#include "components/Position.h"
#include "components/Visibility.h"
#include "components/PathIndex.h"
#include "components/Velocity.h"
#include "components/Draggable.h"
#include "components/Action.h"
+#include "components/Range.h"
+#include "eventComponents/MoveEntityEvent.h"
+#include "eventComponents/RemoveEntityEvent.h"
typedef std::vector<std::shared_ptr<Entity>> Entities;
inline void
operator+=(Entities &originalVector, Entities &newVector) {
originalVector.insert(originalVector.end(), std::make_move_iterator(newVector.begin()),
std::make_move_iterator(newVector.end()));
}
class System {
public:
virtual void update(Entities *entities, GameData &gameData) = 0;
};
#endif //SDL2_GAME_SYSTEM_H
diff --git a/src/components/Action.cpp b/src/components/Action.cpp
deleted file mode 100644
index 9286f63..0000000
--- a/src/components/Action.cpp
+++ /dev/null
@@ -1,5 +0,0 @@
-//
-// Created by Ido Mozes on 08/07/2019.
-//
-
-#include "Action.h"
diff --git a/src/components/Action.h b/src/components/Action.h
index 115abaf..38cc02a 100644
--- a/src/components/Action.h
+++ b/src/components/Action.h
@@ -1,28 +1,28 @@
//
// Created by Ido Mozes on 08/07/2019.
//
#ifndef SDL_GAME_ACTION_H
#define SDL_GAME_ACTION_H
#include "../Component.h"
enum ActionType {
- DRAG, CLICK, DROP
+ DRAG, CLICK, DROP, CHOOSE
};
class Action : public Component {
public:
bool disabled;
ActionType actionType;
static constexpr ComponentType type = ComponentType::ACTION;
- Action(Entity *entity, ActionType actionType, bool disabled = false) : Component(entity), actionType(actionType),
+ explicit Action(ActionType actionType, bool disabled = false) :actionType(actionType),
disabled(disabled) {}
~Action() override = default;
};
#endif //SDL_GAME_ACTION_H
diff --git a/src/components/ComponentHeader.h b/src/components/ComponentHeader.h
index 8196426..0f7d528 100644
--- a/src/components/ComponentHeader.h
+++ b/src/components/ComponentHeader.h
@@ -1,8 +1,8 @@
#include "../Component.h"
class __: public Component{
public:
static constexpr ComponentType type = ComponentType::__;
- __(Entity *entity):Component(entity){}
+ __() = default;
~__() override = default;
};
\ No newline at end of file
diff --git a/src/components/Draggable.h b/src/components/Draggable.h
index ac1e84a..d6b4796 100644
--- a/src/components/Draggable.h
+++ b/src/components/Draggable.h
@@ -1,16 +1,16 @@
//
// Created by Ido Mozes on 09/07/2019.
//
#ifndef SDL_GAME_DRAGGABLE_H
#define SDL_GAME_DRAGGABLE_H
#include "../Component.h"
class Draggable: public Component{
public:
bool isPlaceable = false;
static constexpr ComponentType type = ComponentType::DRAGGABLE;
- Draggable(Entity *entity):Component(entity){}
+ Draggable() = default;
~Draggable() override = default;
};
#endif //SDL_GAME_DRAGGABLE_H
diff --git a/src/components/Kind.h b/src/components/Kind.h
index 8dda3ca..fa5f33e 100644
--- a/src/components/Kind.h
+++ b/src/components/Kind.h
@@ -1,23 +1,22 @@
//
// 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 constexpr ComponentType type = ComponentType::KIND;
- Kind(Entity *entity, std::string kind) : Component(entity), kind(std::move(kind)) {}
- Kind(Entity *entity, Kind& kind) : Component(entity), kind(kind.kind) {}
+ explicit Kind(std::string kind) :kind(std::move(kind)) {}
~Kind() override = default;
};
#endif //SDL_GAME_KIND_H
diff --git a/src/components/PathIndex.cpp b/src/components/PathIndex.cpp
deleted file mode 100644
index ae4e292..0000000
--- a/src/components/PathIndex.cpp
+++ /dev/null
@@ -1,5 +0,0 @@
-//
-// Created by Ido Mozes on 03/07/2019.
-//
-
-#include "PathIndex.h"
diff --git a/src/components/PathIndex.h b/src/components/PathIndex.h
index 5526d35..160187e 100644
--- a/src/components/PathIndex.h
+++ b/src/components/PathIndex.h
@@ -1,22 +1,22 @@
//
// 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 constexpr ComponentType type = ComponentType::PATH_INDEX;
- PathIndex(Entity *entity, int index) : Component(entity), index(index), progress(index) {}
+ explicit PathIndex(int index) :index(index), progress(index) {}
~PathIndex() override = default;
};
#endif //SDL_GAME_PATHINDEX_H
diff --git a/src/components/Position.cpp b/src/components/Position.cpp
deleted file mode 100644
index 7301ee6..0000000
--- a/src/components/Position.cpp
+++ /dev/null
@@ -1,5 +0,0 @@
-//
-// Created by Ido Mozes on 02/07/2019.
-//
-
-#include "Position.h"
diff --git a/src/components/Position.h b/src/components/Position.h
index 7e4483f..7fbb5ab 100644
--- a/src/components/Position.h
+++ b/src/components/Position.h
@@ -1,34 +1,34 @@
//
// Created by Ido Mozes on 02/07/2019.
//
#ifndef SDL_GAME_POSITION_H
#define SDL_GAME_POSITION_H
#include "../Component.h"
class Position : public Component {
float X, Y;
public:
static constexpr ComponentType type = ComponentType::POSITION;
- Position(Entity *entity, float x, float y) : Component(entity), X(x), Y(y) {};
+ Position(float x, float y) : X(x), Y(y) {};
- ~Position() override= default;
+ ~Position() override = default;
float getX() { return X; }
float getY() { return Y; }
void setPosition(float x, float y) {
X = x;
Y = y;
}
void changePosition(float deltaX, float deltaY) {
X += deltaX;
Y += deltaY;
}
};
#endif //SDL_GAME_POSITION_H
diff --git a/src/components/Range.h b/src/components/Range.h
new file mode 100644
index 0000000..329b10e
--- /dev/null
+++ b/src/components/Range.h
@@ -0,0 +1,20 @@
+//
+// Created by Ido Mozes on 10/07/2019.
+//
+
+#ifndef SDL_GAME_RANGE_H
+#define SDL_GAME_RANGE_H
+
+#include "../Component.h"
+
+class Range : public Component {
+public:
+ float range;
+ static constexpr ComponentType type = ComponentType::RANGE;
+
+ explicit Range(float range) : range(range) {}
+
+ ~Range() override = default;
+};
+
+#endif //SDL_GAME_RANGE_H
diff --git a/src/components/Sequence.h b/src/components/Sequence.h
index 490770c..18ed930 100644
--- a/src/components/Sequence.h
+++ b/src/components/Sequence.h
@@ -1,30 +1,31 @@
//
// Created by Ido Mozes on 07/07/2019.
//
#ifndef SDL_GAME_SEQUENCE_H
#define SDL_GAME_SEQUENCE_H
#include <cmath>
#include "../Component.h"
constexpr int SEQUENCE_FINISHED = -1;
class Sequence : public Component {
int amount;
float interval;
float timeToRecharge;
int amountReady;
public:
static constexpr ComponentType type = ComponentType::SEQUENCE;
- Sequence(Entity *entity, int amount, float interval, float delay) : Component(entity), amount(amount),
- interval(interval),
- timeToRecharge(delay ? delay : interval),
- amountReady(delay == 0) {}
+ Sequence(int amount, float interval, float delay) : amount(amount), interval(interval),
+ timeToRecharge(delay ? delay : interval),
+ amountReady(delay == 0) {}
+
~Sequence() override = default;
+
int getAmountReady();
};
#endif //SDL_GAME_SEQUENCE_H
diff --git a/src/components/Speed.h b/src/components/Speed.h
index 270a130..496cdcf 100644
--- a/src/components/Speed.h
+++ b/src/components/Speed.h
@@ -1,21 +1,21 @@
//
// Created by Ido Mozes on 03/07/2019.
//
#ifndef SDL_GAME_SPEED_H
#define SDL_GAME_SPEED_H
#include "../Component.h"
class Speed : public Component {
public:
float speed;
static constexpr ComponentType type = ComponentType::SPEED;
- Speed(Entity *entity, float speed) : Component(entity), speed(speed) {}
+ explicit Speed(float speed) :speed(speed) {}
~Speed() override = default;
};
#endif //SDL_GAME_SPEED_H
diff --git a/src/components/Type.h b/src/components/Type.h
index 5f88a08..2739684 100644
--- a/src/components/Type.h
+++ b/src/components/Type.h
@@ -1,17 +1,17 @@
//
// Created by Ido Mozes on 09/07/2019.
//
#ifndef SDL_GAME_TYPE_H
#define SDL_GAME_TYPE_H
#include "../Component.h"
enum Types{
OBSTACLE, TOWER, BLOON, RADIOS
};
class Type: public Component{
public:
static constexpr ComponentType type = ComponentType::TYPE;
- Type(Entity *entity):Component(entity){}
+ Type() = default;
~Type() override = default;
};
#endif //SDL_GAME_TYPE_H
diff --git a/src/components/Velocity.cpp b/src/components/Velocity.cpp
index 3af03f9..3a4e958 100644
--- a/src/components/Velocity.cpp
+++ b/src/components/Velocity.cpp
@@ -1,34 +1,34 @@
//
// Created by Ido Mozes on 02/07/2019.
//
#include "Velocity.h"
-Velocity::Velocity(Entity *entity, float x, float y) : Component(entity), X(x), Y(y) {}
+Velocity::Velocity(float x, float y) : X(x), Y(y) {}
void Velocity::turnDirection(float deltaDeg) {
float alpha, R;
getAlphaAndR(alpha, R);
setXAndY(alpha + deltaDeg, R);
}
void Velocity::setDirection(float deg) {
float alpha, R;
getAlphaAndR(alpha, R);
setXAndY(deg, R);
}
void Velocity::changeSpeed(float deltaSpeed) {
float alpha, R;
getAlphaAndR(alpha, R);
setXAndY(alpha, R + deltaSpeed);
}
void Velocity::setSpeed(float speed) {
float alpha, R;
getAlphaAndR(alpha, R);
setXAndY(alpha, speed);
}
diff --git a/src/components/Velocity.h b/src/components/Velocity.h
index fb7bcac..4c802c4 100644
--- a/src/components/Velocity.h
+++ b/src/components/Velocity.h
@@ -1,63 +1,64 @@
//
// Created by Ido Mozes on 02/07/2019.
//
#ifndef SDL_GAME_VELOCITY_H
#define SDL_GAME_VELOCITY_H
#include <cmath>
#include "../Component.h"
class Velocity : public Component {
- float X,Y;
+ float X, Y;
inline void getAlphaAndR(float &alpha, float &R) {
alpha = atan2f(Y, X);
R = X / sinf(alpha);
}
inline void setXAndY(float alpha, float R) {
X = R * cosf(alpha);
Y = R * sinf(alpha);
}
public:
static constexpr ComponentType type = ComponentType::VELOCITY;
- Velocity(Entity *entity, float x, float y);
+ Velocity(float x, float y);
~Velocity() override = default;
float getX() { return X; }
float getY() { return Y; }
void setVelocity(float x, float y) {
X = x;
Y = y;
}
void changeVelocity(float deltaX, float deltaY) {
X += deltaX;
Y += deltaY;
}
void turnDirection(float deltaDeg);
void setDirection(float deg);
void setSpeed(float speed);
void changeSpeed(float deltaSpeed);
};
class Acceleration : public Velocity {
public:
static constexpr ComponentType type = ComponentType::ACCELERATION;
- Acceleration(Entity *entity, float x, float y):Velocity(entity,x,y){}
+
+ Acceleration(float x, float y) : Velocity(x, y) {}
};
#endif //SDL_GAME_VELOCITY_H
diff --git a/src/components/Visibility.cpp b/src/components/Visibility.cpp
index e801e94..3640ebf 100644
--- a/src/components/Visibility.cpp
+++ b/src/components/Visibility.cpp
@@ -1,39 +1,38 @@
//
// Created by Ido Mozes on 23/06/2019.
//
#include "Visibility.h"
-Visibility::Visibility(Entity *entity, SDL_Renderer *renderer, SDL_Surface *newSurface, std::optional<SDL_Rect> dstR,
- std::optional<float> radios) : Component(entity), dstRect(dstR), radios(radios) {
+Visibility::Visibility(SDL_Renderer *renderer, SDL_Surface *newSurface, std::optional<SDL_Rect> dstR) : dstRect(dstR){
loadTexture(renderer, newSurface);
}
Visibility::~Visibility() {
SDL_DestroyTexture(texture);
}
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) {
dstRect->x = x;
dstRect->y = y;
}
}
diff --git a/src/components/Visibility.h b/src/components/Visibility.h
index 3cbd628..af6924c 100644
--- a/src/components/Visibility.h
+++ b/src/components/Visibility.h
@@ -1,48 +1,43 @@
//
// 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"
-constexpr float NO_RADIOS = -1;
+
+
SDL_Texture *loadTexture(SDL_Renderer *renderer, const char *path);
class Visibility : public Component {
SDL_Surface *surface = nullptr;
SDL_Texture *texture = nullptr;
std::optional<SDL_Rect> dstRect;
- std::optional<float> radios;
public:
static constexpr ComponentType type = ComponentType::VISIBILITY;
- Visibility(Entity *entity, SDL_Renderer *renderer, SDL_Surface *newSurface,
- std::optional<SDL_Rect> dstR = std::nullopt, std::optional<float> radios = std::nullopt);
+ Visibility(SDL_Renderer *renderer, SDL_Surface *newSurface, std::optional<SDL_Rect> dstR = std::nullopt);
~Visibility() override;
SDL_Texture *getTexture() { return texture; }
SDL_Rect *getDstRect() { return dstRect ? &dstRect.value() : nullptr; }
- float getRadios() { return radios ? radios.value() : NO_RADIOS; }
-
- void setRadios(std::optional<float> newRadios) { radios = newRadios; }
-
void setPosition(int x, int y);
void loadTexture(SDL_Renderer *renderer, SDL_Surface *newSurface);
void reloadTexture(SDL_Renderer *renderer);
};
#endif //SDL2_GAME_VISIBILITY_H
diff --git a/src/eventComponents/MoveEntityEvent.h b/src/eventComponents/MoveEntityEvent.h
new file mode 100644
index 0000000..cd89a6d
--- /dev/null
+++ b/src/eventComponents/MoveEntityEvent.h
@@ -0,0 +1,16 @@
+//
+// Created by Ido Mozes on 10/07/2019.
+//
+
+#ifndef SDL_GAME_MOVEENTITYEVENT_H
+#define SDL_GAME_MOVEENTITYEVENT_H
+#include "../Component.h"
+
+class MoveEntityEvent: public Component{
+public:
+ int toLayer;
+ static constexpr ComponentType type = ComponentType::MOVE_ENTITY_EVENT;
+ explicit MoveEntityEvent(int toLayer):toLayer(toLayer){}
+ ~MoveEntityEvent() override = default;
+};
+#endif //SDL_GAME_MOVEENTITYEVENT_H
diff --git a/src/eventComponents/RemoveEntityEvent.h b/src/eventComponents/RemoveEntityEvent.h
new file mode 100644
index 0000000..1537623
--- /dev/null
+++ b/src/eventComponents/RemoveEntityEvent.h
@@ -0,0 +1,15 @@
+//
+// Created by Ido Mozes on 10/07/2019.
+//
+
+#ifndef SDL_GAME_REMOVEENTITYEVENT_H
+#define SDL_GAME_REMOVEENTITYEVENT_H
+#include "../Component.h"
+
+class RemoveEntityEvent: public Component{
+public:
+ static constexpr ComponentType type = ComponentType::REMOVE_ENTITY_EVENT;
+ RemoveEntityEvent() = default;
+ ~RemoveEntityEvent() override = default;
+};
+#endif //SDL_GAME_REMOVEENTITYEVENT_H
diff --git a/src/systems/DraggingSystem.cpp b/src/systems/DraggingSystem.cpp
index bca80f7..002436e 100644
--- a/src/systems/DraggingSystem.cpp
+++ b/src/systems/DraggingSystem.cpp
@@ -1,40 +1,40 @@
//
// Created by Ido Mozes on 09/07/2019.
//
#include "DraggingSystem.h"
void DraggingSystem::update(Entities *layers, GameData &gameData) {
int mouseX, mouseY;
SDL_GetMouseState(&mouseX, &mouseY);
mouseX = int(mouseX / gameData.mapScale);
mouseY = int(mouseY / gameData.mapScale);
for (int i = 0; i < N_LAYERS; ++i) {
for (auto &entity: layers[i]) {
if (auto components = entity->getComponents<Draggable, Visibility>()) {
auto[draggable, visibility] = components.value();
visibility.setPosition(mouseX - int(visibility.getDstRect()->w / 2.0),
mouseY - int(visibility.getDstRect()->h / 2.0));
bool freePosition = true;
if (mouseX > SIDEBAR_WIDTH + MAP_WIDTH or mouseX < SIDEBAR_WIDTH)
freePosition = false;
else
for (int x = mouseX - SIDEBAR_WIDTH - 4; x < mouseX - SIDEBAR_WIDTH + 5; ++x) {
for (int y = mouseY - 4; y < mouseY + 5; ++y) {
- if (gameData.mapData[x][y]) {
+ if (gameData.mapData[x][y] == OBSTACLE or gameData.mapData[x][y] == TOWER) {
freePosition = false;
goto setIsPlaceable;
}
}
}
setIsPlaceable:
draggable.isPlaceable = freePosition;
}
}
}
}
\ No newline at end of file
diff --git a/src/systems/EventSystem.cpp b/src/systems/EventSystem.cpp
index ead595b..31d020f 100644
--- a/src/systems/EventSystem.cpp
+++ b/src/systems/EventSystem.cpp
@@ -1,91 +1,116 @@
//
// 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: {
- Entities newEntities;
-// Entities entitiesToRemove;
+ 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;
- entityX = visibility.getDstRect()->x;
- entityY = visibility.getDstRect()->y;
- w = visibility.getDstRect()->w;
- h = visibility.getDstRect()->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 / 2.0), mouseY - int(surface->h / 2.0),
- surface->w, surface->h}, 100);
+ 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);
- newEntities.emplace_back(draggable);
+ draggable->addComponent<Range>(range);
+ std::shared_ptr<Entity> ptr(draggable);
+ gameData.selected = ptr;
+ newEntities[MENU_LAYER].emplace_back(ptr);
gameData.isDragging = true;
goto entityClicked;
}
case CLICK: {
goto entityClicked;
}
case DROP: {
auto &draggable = *entity->getComponent<Draggable>();
if (draggable.isPlaceable) {
entity->removeComponent<Draggable>();
entity->getComponent<Action>()->actionType = CLICK;
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)
+ entity->addComponent<MoveEntityEvent>(GAME_LAYER);
}
goto entityClicked;
}
+ case CHOOSE: {
+ goto entityClicked;
+ }
}
}
+
}
}
+
}
entityClicked:
- layers[3] += newEntities;
+ for (int i = 0; i < N_LAYERS; ++i) {
+ if (!newEntities[i].empty())
+ layers[i] += newEntities[i];
+ }
break;
}
default:
break;
}
}
}
diff --git a/src/systems/HandleMoveEntitiySystem.cpp b/src/systems/HandleMoveEntitiySystem.cpp
new file mode 100644
index 0000000..6e398ef
--- /dev/null
+++ b/src/systems/HandleMoveEntitiySystem.cpp
@@ -0,0 +1,16 @@
+//
+// Created by Ido Mozes on 10/07/2019.
+//
+
+#include "HandleMoveEntitiySystem.h"
+void HandleMoveEntitiySystem::update(Entities *layers, GameData &gameData) {
+ for (int i = 0; i < N_LAYERS; ++i) {
+ for (auto &entity: layers[i]) {
+ if(auto moveEntityEventP = entity->getComponent<MoveEntityEvent>()) {
+ int newLayer = moveEntityEventP->toLayer;
+ entity->removeComponent<MoveEntityEvent>();
+ layers[newLayer].emplace_back(std::move(entity));
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/systems/HandleMoveEntitiySystem.h b/src/systems/HandleMoveEntitiySystem.h
new file mode 100644
index 0000000..3a78602
--- /dev/null
+++ b/src/systems/HandleMoveEntitiySystem.h
@@ -0,0 +1,13 @@
+//
+// Created by Ido Mozes on 10/07/2019.
+//
+
+#ifndef SDL_GAME_HANDLEMOVEENTITIYSYSTEM_H
+#define SDL_GAME_HANDLEMOVEENTITIYSYSTEM_H
+#include <algorithm>
+#include "../System.h"
+class HandleMoveEntitiySystem : public System {
+public:
+ void update(Entities *layers, GameData &gameData) override;
+};
+#endif //SDL_GAME_HANDLEMOVEENTITIYSYSTEM_H
diff --git a/src/systems/RemoveEntitiesSystem.cpp b/src/systems/RemoveEntitiesSystem.cpp
new file mode 100644
index 0000000..0d396fb
--- /dev/null
+++ b/src/systems/RemoveEntitiesSystem.cpp
@@ -0,0 +1,15 @@
+//
+// Created by Ido Mozes on 10/07/2019.
+//
+
+#include "RemoveEntitiesSystem.h"
+
+void RemoveEntitiesSystem::update(Entities *layers, GameData &gameData) {
+ for (int i = 0; i < N_LAYERS; ++i) {
+ layers[i].erase(
+ std::remove_if(layers[i].begin(), layers[i].end(), [](auto &entity) {
+ return !entity or entity->template getComponent<RemoveEntityEvent>();
+ }),
+ layers[i].end());
+ }
+}
\ No newline at end of file
diff --git a/src/systems/RemoveEntitiesSystem.h b/src/systems/RemoveEntitiesSystem.h
new file mode 100644
index 0000000..ae3f932
--- /dev/null
+++ b/src/systems/RemoveEntitiesSystem.h
@@ -0,0 +1,13 @@
+//
+// Created by Ido Mozes on 10/07/2019.
+//
+
+#ifndef SDL_GAME_REMOVEENTITIESSYSTEM_H
+#define SDL_GAME_REMOVEENTITIESSYSTEM_H
+#include <algorithm>
+#include "../System.h"
+class RemoveEntitiesSystem : public System {
+public:
+ void update(Entities *layers, GameData &gameData) override;
+};
+#endif //SDL_GAME_REMOVEENTITIESSYSTEM_H
diff --git a/src/systems/RenderSystem.cpp b/src/systems/RenderSystem.cpp
index 9879145..876055a 100644
--- a/src/systems/RenderSystem.cpp
+++ b/src/systems/RenderSystem.cpp
@@ -1,64 +1,61 @@
//
// 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,
- (MAP_WIDTH + SIDEBAR_WIDTH + MENU_WIDTH) * gameData.mapScale,
- MAP_HEIGHT * gameData.mapScale, gameData.fullscreen ? SDL_WINDOW_FULLSCREEN : 0);
+ 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]) {
if (auto visibilityP = entity->getComponent<Visibility>()) {
auto &visibility = *visibilityP;
auto positionP = entity->getComponent<Position>();
- 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)};
+ 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);
}
SDL_RenderCopy(gameData.renderer, visibility.getTexture(), nullptr, &r);
- if (visibility.getRadios() != NO_RADIOS) {
+ if (gameData.selected == entity) {
int entityX, entityY;
if (positionP) {
entityX = r.x;
entityY = r.y;
} else {
- entityX = r.x + int((visibility.getDstRect()->w / 2.0) * gameData.mapScale);
- entityY = r.y + int((visibility.getDstRect()->h / 2.0) * gameData.mapScale);
+ entityX = r.x + int((dstRect->w / 2.0) * gameData.mapScale);
+ entityY = r.y + int((dstRect->h / 2.0) * gameData.mapScale);
}
auto draggableP = entity->getComponent<Draggable>();
bool isRed = draggableP ? !draggableP->isPlaceable : false;
- filledCircleRGBA(gameData.renderer, entityX, entityY, visibility.getRadios(), isRed ? 255 : 0, 0, 0,
- 100);
- aacircleRGBA(gameData.renderer, entityX, entityY, visibility.getRadios(), isRed ? 255 : 0, 0, 0,
- 150);
-
+ float range = entity->getComponent<Range>()->range;
+ filledCircleRGBA(gameData.renderer, entityX, entityY, range, isRed ? 255 : 0, 0, 0, 100);
+ aacircleRGBA(gameData.renderer, entityX, entityY, range, isRed ? 255 : 0, 0, 0, 150);
}
}
}
}
SDL_RenderPresent(gameData.renderer);
}
diff --git a/src/systems/SpawnSystem.cpp b/src/systems/SpawnSystem.cpp
index 26157f2..7c419eb 100644
--- a/src/systems/SpawnSystem.cpp
+++ b/src/systems/SpawnSystem.cpp
@@ -1,31 +1,32 @@
//
// Created by Ido Mozes on 07/07/2019.
//
#include "SpawnSystem.h"
void SpawnSystem::update(Entities *layers, GameData &gameData) {
for (int i = 0; i < N_LAYERS; ++i) {
Entities newEntities;
for (auto &entity: layers[i]) {
if (auto components = entity->getComponents<Sequence, Kind, Speed>()) {
auto[sequence, kind, speed] = components.value();
int amount = sequence.getAmountReady();
for (int j = 0; j < amount; ++j) {
auto *newEntity = new Entity();
newEntity->addComponent<Kind>(kind);
newEntity->addComponent<Position>(gameData.startingPoint.X, gameData.startingPoint.Y);
newEntity->addComponent<PathIndex>(0);
newEntity->addComponent<Speed>(speed.speed);
SDL_Surface *surface = gameData.assets[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] += newEntities;
+ if (!newEntities.empty())
+ layers[i] += newEntities;
}
}

File Metadata

Mime Type
text/x-diff
Expires
Mon, Jun 15, 11:39 PM (2 w, 2 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
72299
Default Alt Text
(46 KB)

Event Timeline