Page Menu
Home
Phabricator (Chris)
Search
Configure Global Search
Log In
Files
F130195
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Authored By
Unknown
Size
33 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/src/Component.h b/src/Component.h
index c7dd6e9..912f515 100644
--- a/src/Component.h
+++ b/src/Component.h
@@ -1,30 +1,45 @@
//
// 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,
+ DAMAGE,
+ PIERCE,
+ SPREAD,
+ SEQUENCE,
+ ACTION,
+ DRAGGABLE,
LENGTH
};
class Component {
Entity *entity;
public:
explicit Component(Entity *entity) : entity(entity) {};
+ virtual ~Component() = default;
Entity *getEntity() { return entity; }
};
#endif //SDL2_GAME_COMPONENT_H
diff --git a/src/Entity.cpp b/src/Entity.cpp
index 2fc8206..1149efd 100644
--- a/src/Entity.cpp
+++ b/src/Entity.cpp
@@ -1,6 +1,7 @@
//
// Created by Ido Mozes on 20/06/2019.
//
#include "Entity.h"
+
diff --git a/src/Entity.h b/src/Entity.h
index fd5c2ae..75bb20f 100644
--- a/src/Entity.h
+++ b/src/Entity.h
@@ -1,66 +1,61 @@
//
// 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:
-// bool hasComponents(uint64_t mask) { return (componentsMask.to_ullong() & mask) == mask; }
-
-// template<class T>
-// T &getComponent() { return *(T *) (components[T::getComponentType()].get()); }
-
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();
+ 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)...);
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();
componentsMask[T::type] = false;
}
};
#endif //SDL2_GAME_ENTITY_H
diff --git a/src/Game.h b/src/Game.h
index bcd7f48..0ad6355 100644
--- a/src/Game.h
+++ b/src/Game.h
@@ -1,46 +1,46 @@
//
// 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 "GameData.h"
#include "boost/filesystem.hpp"
#include <iostream>
class Game {
std::vector<std::unique_ptr<System>> systems;
- std::vector<std::shared_ptr<Entity>> layers[N_LAYERS];
+ 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/System.h b/src/System.h
index a565f76..34233e7 100644
--- a/src/System.h
+++ b/src/System.h
@@ -1,27 +1,36 @@
//
// 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"
+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{
+class System {
public:
- virtual void update(std::vector<std::shared_ptr<Entity>> *entities, GameData & gameData)=0;
+ virtual void update(Entities *entities, GameData &gameData) = 0;
};
+
#endif //SDL2_GAME_SYSTEM_H
diff --git a/src/components/Action.h b/src/components/Action.h
index 432a4c4..115abaf 100644
--- a/src/components/Action.h
+++ b/src/components/Action.h
@@ -1,26 +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
};
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),
disabled(disabled) {}
+
+ ~Action() override = default;
};
#endif //SDL_GAME_ACTION_H
diff --git a/src/components/ComponentHeader.h b/src/components/ComponentHeader.h
index c67c4c4..8196426 100644
--- a/src/components/ComponentHeader.h
+++ b/src/components/ComponentHeader.h
@@ -1,7 +1,8 @@
#include "../Component.h"
class __: public Component{
public:
static constexpr ComponentType type = ComponentType::__;
__(Entity *entity):Component(entity){}
+ ~__() override = default;
};
\ No newline at end of file
diff --git a/src/components/Draggable.h b/src/components/Draggable.h
index 5870b89..ac1e84a 100644
--- a/src/components/Draggable.h
+++ b/src/components/Draggable.h
@@ -1,15 +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() override = default;
};
#endif //SDL_GAME_DRAGGABLE_H
diff --git a/src/components/Kind.h b/src/components/Kind.h
index daeff11..8dda3ca 100644
--- a/src/components/Kind.h
+++ b/src/components/Kind.h
@@ -1,22 +1,23 @@
//
// 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) {}
+ ~Kind() override = default;
};
#endif //SDL_GAME_KIND_H
diff --git a/src/components/PathIndex.h b/src/components/PathIndex.h
index fdeafef..5526d35 100644
--- a/src/components/PathIndex.h
+++ b/src/components/PathIndex.h
@@ -1,21 +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) {}
+ ~PathIndex() override = default;
};
#endif //SDL_GAME_PATHINDEX_H
diff --git a/src/components/Position.h b/src/components/Position.h
index cbc72a1..7e4483f 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() = 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/Sequence.h b/src/components/Sequence.h
index 4135478..490770c 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"
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() override = default;
int getAmountReady();
};
#endif //SDL_GAME_SEQUENCE_H
diff --git a/src/components/Speed.h b/src/components/Speed.h
index 8a77728..270a130 100644
--- a/src/components/Speed.h
+++ b/src/components/Speed.h
@@ -1,20 +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) {}
+ ~Speed() override = default;
};
#endif //SDL_GAME_SPEED_H
diff --git a/src/components/Type.h b/src/components/Type.h
index fdc9d38..5f88a08 100644
--- a/src/components/Type.h
+++ b/src/components/Type.h
@@ -1,16 +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() override = default;
};
#endif //SDL_GAME_TYPE_H
diff --git a/src/components/Velocity.h b/src/components/Velocity.h
index 284e650..fb7bcac 100644
--- a/src/components/Velocity.h
+++ b/src/components/Velocity.h
@@ -1,64 +1,63 @@
//
// 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;
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() = default;
+ ~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()= default;
};
#endif //SDL_GAME_VELOCITY_H
diff --git a/src/components/Visibility.h b/src/components/Visibility.h
index 64dedfe..3cbd628 100644
--- a/src/components/Visibility.h
+++ b/src/components/Visibility.h
@@ -1,48 +1,48 @@
//
// 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();
+ ~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/systems/DraggingSystem.cpp b/src/systems/DraggingSystem.cpp
index badac80..bca80f7 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(std::vector<std::shared_ptr<Entity>> *layers, GameData &gameData) {
+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]) {
freePosition = false;
goto setIsPlaceable;
}
}
}
setIsPlaceable:
draggable.isPlaceable = freePosition;
}
}
}
}
\ No newline at end of file
diff --git a/src/systems/DraggingSystem.h b/src/systems/DraggingSystem.h
index e4c9d78..52b5926 100644
--- a/src/systems/DraggingSystem.h
+++ b/src/systems/DraggingSystem.h
@@ -1,13 +1,13 @@
//
// Created by Ido Mozes on 09/07/2019.
//
#ifndef SDL_GAME_DRAGGINGSYSTEM_H
#define SDL_GAME_DRAGGINGSYSTEM_H
#include <iostream>
#include "../System.h"
class DraggingSystem : public System {
public:
- void update(std::vector<std::shared_ptr<Entity>> *layers, GameData &gameData) override;
+ void update(Entities *layers, GameData &gameData) override;
};
#endif //SDL_GAME_DRAGGINGSYSTEM_H
diff --git a/src/systems/EventSystem.cpp b/src/systems/EventSystem.cpp
index 5f7c962..ead595b 100644
--- a/src/systems/EventSystem.cpp
+++ b/src/systems/EventSystem.cpp
@@ -1,90 +1,91 @@
//
// Created by Ido Mozes on 08/07/2019.
//
#include "EventSystem.h"
-void EventSystem::update(std::vector<std::shared_ptr<Entity>> *layers, GameData &gameData) {
+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: {
- std::vector<std::shared_ptr<Entity>> newEntities;
- std::vector<std::shared_ptr<Entity>> entitiesToRemove;
+ Entities newEntities;
+// Entities entitiesToRemove;
int mouseX, mouseY, originalMouseX;
SDL_GetMouseState(&mouseX, &mouseY);
mouseX = originalMouseX = int(mouseX / gameData.mapScale);
mouseY = int(mouseY / gameData.mapScale);
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 == DRAG and gameData.isDragging))
+ 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;
- if (auto positionP=entity->getComponent<Position>()) {
+ 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 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);
draggable->addComponent<Draggable>();
draggable->addComponent<Action>(DROP);
draggable->addComponent<Kind>(kind);
newEntities.emplace_back(draggable);
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;
}
- break;
+ goto entityClicked;
}
}
}
}
}
}
entityClicked:
- layers[3].insert(layers[3].end(), std::make_move_iterator(newEntities.begin()),
- std::make_move_iterator(newEntities.end()));
+ layers[3] += newEntities;
break;
}
default:
break;
}
}
}
diff --git a/src/systems/EventSystem.h b/src/systems/EventSystem.h
index a57cb3b..2a25c6c 100644
--- a/src/systems/EventSystem.h
+++ b/src/systems/EventSystem.h
@@ -1,19 +1,19 @@
//
// Created by Ido Mozes on 08/07/2019.
//
#ifndef SDL_GAME_EVENTSYSTEM_H
#define SDL_GAME_EVENTSYSTEM_H
#include <iostream>
#include "../System.h"
#include "../systems/RenderSystem.h"
class EventSystem : public System {
public:
- void update(std::vector<std::shared_ptr<Entity>> *layers, GameData &gameData) override;
+ void update(Entities *layers, GameData &gameData) override;
};
#endif //SDL_GAME_EVENTSYSTEM_H
diff --git a/src/systems/MovementSystem.cpp b/src/systems/MovementSystem.cpp
index d5a60cc..bc54442 100644
--- a/src/systems/MovementSystem.cpp
+++ b/src/systems/MovementSystem.cpp
@@ -1,68 +1,68 @@
//
// Created by Ido Mozes on 02/07/2019.
//
#include "MovementSystem.h"
-void MovementSystem::update(std::vector<std::shared_ptr<Entity>> *layers, GameData &gameData) {
+void MovementSystem::update(Entities *layers, GameData &gameData) {
for (int i = 0; i < N_LAYERS; ++i) {
for (auto &entity: layers[i]) {
// Move all entities with Visibility and Position
if (auto components = entity->getComponents<Visibility, Position>()) {
auto[visibility, position]=components.value();
float deltaX = 0, deltaY = 0;
if (auto velocityP = entity->getComponent<Velocity>()) {
auto &velocity = *velocityP;
if (auto accelerationP = entity->getComponent<Acceleration>()) {
auto &acceleration = *accelerationP;
velocity.changeVelocity(acceleration.getX(), acceleration.getY());
}
deltaX = velocity.getX();
deltaY = velocity.getY();
} else if (auto components2 = entity->getComponents<PathIndex, Speed>()) {
auto[pathIndex, speed] =components2.value();
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++;
}
}
position.changePosition(deltaX, deltaY);
}
}
}
}
diff --git a/src/systems/MovementSystem.h b/src/systems/MovementSystem.h
index 089e90a..14238a2 100644
--- a/src/systems/MovementSystem.h
+++ b/src/systems/MovementSystem.h
@@ -1,18 +1,18 @@
//
// Created by Ido Mozes on 02/07/2019.
//
#ifndef SDL_GAME_MOVEMENTSYSTEM_H
#define SDL_GAME_MOVEMENTSYSTEM_H
#include <iostream>
#include "../GameData.h"
#include "../System.h"
class MovementSystem : public System{
public:
MovementSystem()= default;
~MovementSystem()= default;
- void update(std::vector<std::shared_ptr<Entity>> *layers, GameData & gameData) override;
+ void update(Entities *layers, GameData & gameData) override;
};
#endif //SDL_GAME_MOVEMENTSYSTEM_H
diff --git a/src/systems/RenderSystem.cpp b/src/systems/RenderSystem.cpp
index a0318d1..9879145 100644
--- a/src/systems/RenderSystem.cpp
+++ b/src/systems/RenderSystem.cpp
@@ -1,64 +1,64 @@
//
// 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.renderer = SDL_CreateRenderer(gameData.window, -1, 0);
SDL_SetRenderDrawColor(gameData.renderer, 255, 255, 255, 255);
}
-void RenderSystem::update(std::vector<std::shared_ptr<Entity>> *layers, GameData &gameData) {
+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)};
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) {
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);
}
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);
}
}
}
}
SDL_RenderPresent(gameData.renderer);
}
diff --git a/src/systems/RenderSystem.h b/src/systems/RenderSystem.h
index f1fd4a3..a88f40d 100644
--- a/src/systems/RenderSystem.h
+++ b/src/systems/RenderSystem.h
@@ -1,26 +1,26 @@
//
// Created by Ido Mozes on 23/06/2019.
//
#ifndef SDL2_GAME_RENDERSYSTEM_H
#define SDL2_GAME_RENDERSYSTEM_H
#include <iostream>
#include "../System.h"
#include "../GameData.h"
#include "SDL2_gfxPrimitives.h"
class RenderSystem : public System {
public:
RenderSystem() { SDL_Init(SDL_INIT_EVERYTHING); }
void init(GameData &gameData);
- void update(std::vector<std::shared_ptr<Entity>> *layers , GameData & gameData) override;
+ void update(Entities *layers , GameData & gameData) override;
};
#endif //SDL2_GAME_RENDERSYSTEM_H
diff --git a/src/systems/SpawnSystem.cpp b/src/systems/SpawnSystem.cpp
index 31639c2..26157f2 100644
--- a/src/systems/SpawnSystem.cpp
+++ b/src/systems/SpawnSystem.cpp
@@ -1,32 +1,31 @@
//
// Created by Ido Mozes on 07/07/2019.
//
#include "SpawnSystem.h"
-void SpawnSystem::update(std::vector<std::shared_ptr<Entity>> *layers, GameData &gameData) {
+void SpawnSystem::update(Entities *layers, GameData &gameData) {
for (int i = 0; i < N_LAYERS; ++i) {
- std::vector<std::shared_ptr<Entity>> newEntities;
+ 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();
+ 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].insert(layers[i].end(), std::make_move_iterator(newEntities.begin()),
- std::make_move_iterator(newEntities.end()));
+ layers[i] += newEntities;
}
}
diff --git a/src/systems/SpawnSystem.h b/src/systems/SpawnSystem.h
index fdc00b5..43bfc11 100644
--- a/src/systems/SpawnSystem.h
+++ b/src/systems/SpawnSystem.h
@@ -1,17 +1,17 @@
//
// 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"
class SpawnSystem : public System {
public:
- void update(std::vector<std::shared_ptr<Entity>> *layers, GameData &gameData) override;
+ void update(Entities *layers, GameData &gameData) override;
};
#endif //SDL_GAME_SPAWNSYSTEM_H
diff --git a/src/systems/SystemHeader.h b/src/systems/SystemHeader.h
index b85af64..dc81fa0 100644
--- a/src/systems/SystemHeader.h
+++ b/src/systems/SystemHeader.h
@@ -1,5 +1,5 @@
#include "../System.h"
class __ : public System {
public:
- void update(std::vector<std::shared_ptr<Entity>> *layers, GameData &gameData) override;
+ void update(Entities *layers, GameData &gameData) override;
};
\ No newline at end of file
diff --git a/src/systems/SystemSource.cpp b/src/systems/SystemSource.cpp
index 025038c..3a6bfc7 100644
--- a/src/systems/SystemSource.cpp
+++ b/src/systems/SystemSource.cpp
@@ -1,9 +1,9 @@
-void __::update(std::vector<std::shared_ptr<Entity>> *layers, GameData &gameData) {
+void __::update(Entities *layers, GameData &gameData) {
for (int i = 0; i < N_LAYERS; ++i) {
for (auto &entity: layers[i]) {
}
}
}
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Mon, Jun 15, 11:25 PM (2 w, 3 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
71981
Default Alt Text
(33 KB)
Attached To
Mode
R74 BloonsTD
Attached
Detach File
Event Timeline