Page MenuHomePhabricator (Chris)

No OneTemporary

Authored By
Unknown
Size
16 KB
Referenced Files
None
Subscribers
None
diff --git a/src/Entity.h b/src/Entity.h
index f0afe01..fd5c2ae 100644
--- a/src/Entity.h
+++ b/src/Entity.h
@@ -1,66 +1,66 @@
//
// 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() {
- uint64_t mask = createMask({T::getComponentType()...});
+ constexpr uint64_t mask = createMask({T::type...});
if ((componentsMask.to_ullong() & mask) == mask) {
- return std::tuple<T &...>((*(T *) components[T::getComponentType()].get())...);
+ return std::tuple<T &...>((*(T *) components[T::type].get())...);
}
return std::nullopt;
}
template<class T>
T * getComponent() {
- if (auto &c =components[T::getComponentType()])
+ 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::getComponentType()] = std::move(std::unique_ptr<Component>(c));
- componentsMask[T::getComponentType()] = true;
+ components[T::type] = std::move(std::unique_ptr<Component>(c));
+ componentsMask[T::type] = true;
return *c;
}
template<typename T>
void removeComponent() {
- components[T::getComponentType()].release();
- componentsMask[T::getComponentType()] = false;
+ components[T::type].release();
+ componentsMask[T::type] = false;
}
};
#endif //SDL2_GAME_ENTITY_H
diff --git a/src/components/Action.h b/src/components/Action.h
index ee83bf4..432a4c4 100644
--- a/src/components/Action.h
+++ b/src/components/Action.h
@@ -1,26 +1,26 @@
//
// 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 type;
+ ActionType actionType;
- static ComponentType getComponentType() { return ComponentType::ACTION; }
+ static constexpr ComponentType type = ComponentType::ACTION;
- Action(Entity *entity, ActionType type, bool disabled = false) : Component(entity), type(type),
+ Action(Entity *entity, ActionType actionType, bool disabled = false) : Component(entity), actionType(actionType),
disabled(disabled) {}
};
#endif //SDL_GAME_ACTION_H
diff --git a/src/components/ComponentHeader.h b/src/components/ComponentHeader.h
index e8232db..c67c4c4 100644
--- a/src/components/ComponentHeader.h
+++ b/src/components/ComponentHeader.h
@@ -1,7 +1,7 @@
#include "../Component.h"
class __: public Component{
public:
- static ComponentType getComponentType() { return ComponentType::__; }
+ static constexpr ComponentType type = ComponentType::__;
__(Entity *entity):Component(entity){}
};
\ No newline at end of file
diff --git a/src/components/Draggable.h b/src/components/Draggable.h
index 6bffb89..5870b89 100644
--- a/src/components/Draggable.h
+++ b/src/components/Draggable.h
@@ -1,15 +1,15 @@
//
// 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 ComponentType getComponentType() { return ComponentType::DRAGGABLE; }
+ static constexpr ComponentType type = ComponentType::DRAGGABLE;
Draggable(Entity *entity):Component(entity){}
};
#endif //SDL_GAME_DRAGGABLE_H
diff --git a/src/components/Kind.h b/src/components/Kind.h
index e5452d4..daeff11 100644
--- a/src/components/Kind.h
+++ b/src/components/Kind.h
@@ -1,22 +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 ComponentType getComponentType() { return ComponentType::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) {}
};
#endif //SDL_GAME_KIND_H
diff --git a/src/components/PathIndex.h b/src/components/PathIndex.h
index b0f8f67..fdeafef 100644
--- a/src/components/PathIndex.h
+++ b/src/components/PathIndex.h
@@ -1,21 +1,21 @@
//
// Created by Ido Mozes on 03/07/2019.
//
#ifndef SDL_GAME_PATHINDEX_H
#define SDL_GAME_PATHINDEX_H
#include "../Component.h"
class PathIndex : public Component {
public:
int index;
float progress;
- static ComponentType getComponentType() { return ComponentType::PATH_INDEX; }
+ static constexpr ComponentType type = ComponentType::PATH_INDEX;
PathIndex(Entity *entity, int index) : Component(entity), index(index), progress(index) {}
};
#endif //SDL_GAME_PATHINDEX_H
diff --git a/src/components/Position.h b/src/components/Position.h
index 2702de7..cbc72a1 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 ComponentType getComponentType() { return ComponentType::POSITION; }
+ static constexpr ComponentType type = ComponentType::POSITION;
Position(Entity *entity, float x, float y) : Component(entity), X(x), Y(y) {};
~Position() = 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 411dcaf..4135478 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 ComponentType getComponentType() { return ComponentType::SEQUENCE; }
+ 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) {}
int getAmountReady();
};
#endif //SDL_GAME_SEQUENCE_H
diff --git a/src/components/Speed.h b/src/components/Speed.h
index 8092528..8a77728 100644
--- a/src/components/Speed.h
+++ b/src/components/Speed.h
@@ -1,20 +1,20 @@
//
// 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 ComponentType getComponentType() { return ComponentType::SPEED; }
+ static constexpr ComponentType type = ComponentType::SPEED;
Speed(Entity *entity, float speed) : Component(entity), speed(speed) {}
};
#endif //SDL_GAME_SPEED_H
diff --git a/src/components/Type.h b/src/components/Type.h
index 4882227..fdc9d38 100644
--- a/src/components/Type.h
+++ b/src/components/Type.h
@@ -1,16 +1,16 @@
//
// 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 ComponentType getComponentType() { return ComponentType::TYPE; }
+ static constexpr ComponentType type = ComponentType::TYPE;
Type(Entity *entity):Component(entity){}
};
#endif //SDL_GAME_TYPE_H
diff --git a/src/components/Velocity.h b/src/components/Velocity.h
index 2507124..284e650 100644
--- a/src/components/Velocity.h
+++ b/src/components/Velocity.h
@@ -1,64 +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;
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 ComponentType getComponentType() { return ComponentType::VELOCITY; }
+ static constexpr ComponentType type = ComponentType::VELOCITY;
Velocity(Entity *entity, float x, float y);
~Velocity() = 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 ComponentType getComponentType() { return ComponentType::ACCELERATION; }
+ 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 3401552..64dedfe 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 inline ComponentType getComponentType() { return ComponentType::VISIBILITY; }
+ 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_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/EventSystem.cpp b/src/systems/EventSystem.cpp
index f61506b..5f7c962 100644
--- a/src/systems/EventSystem.cpp
+++ b/src/systems/EventSystem.cpp
@@ -1,90 +1,90 @@
//
// Created by Ido Mozes on 08/07/2019.
//
#include "EventSystem.h"
void EventSystem::update(std::vector<std::shared_ptr<Entity>> *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;
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.type == DRAG and gameData.isDragging))
+ if (action.disabled or (action.actionType == DRAG 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>()) {
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.type) {
+ 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>();
gameData.isDragging = false;
}
break;
}
}
}
}
}
}
entityClicked:
layers[3].insert(layers[3].end(), std::make_move_iterator(newEntities.begin()),
std::make_move_iterator(newEntities.end()));
break;
}
default:
break;
}
}
}

File Metadata

Mime Type
text/x-diff
Expires
Wed, Jun 17, 9:29 PM (1 w, 5 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
72266
Default Alt Text
(16 KB)

Event Timeline