Page Menu
Home
Phabricator (Chris)
Search
Configure Global Search
Log In
Files
F131965
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Authored By
Unknown
Size
26 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/CMakeLists.txt b/CMakeLists.txt
index c11b660..5a1bbc9 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.14)
project(SDL_Game)
set(CMAKE_CXX_STANDARD 14)
include_directories(include)
link_directories(lib)
link_libraries(mingw32 SDL2main SDL2 SDL2_image)
-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)
+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)
diff --git a/assets/knight.png b/assets/knight.png
index 69e89bf..9737c77 100644
Binary files a/assets/knight.png and b/assets/knight.png differ
diff --git a/scripts/convert_maps.py b/scripts/convert_maps.py
new file mode 100644
index 0000000..1a14afc
--- /dev/null
+++ b/scripts/convert_maps.py
@@ -0,0 +1,84 @@
+from PIL import Image
+from sys import argv
+from struct import pack
+
+arrows = ['→', '↘', '↓', '↙', '←', '↖', '↑', '↗']
+# obstacles
+image = Image.open(argv[1] + "_obstacles.bmp")
+width, height = image.size
+obstacles = b''
+byte = 0
+i = 7
+for i, pixel in enumerate(image.getdata()):
+ byte += 2 ** (i % 8) * (pixel == (0, 0, 0))
+ if i % 8 == 7:
+ obstacles += bytes([byte])
+ byte = 0
+if i % 8 != 7:
+ obstacles += bytes([byte])
+
+with open(argv[1] + "_obstacles.data", 'wb') as file:
+ file.write(pack('<H', width))
+ file.write(pack('<H', height))
+ file.write(obstacles)
+
+# path
+image = Image.open(argv[1] + "_path.bmp")
+width, height = image.size
+instructions = b''
+i = 0
+for i, pixel in enumerate(image.getdata()):
+ if pixel == (255, 0, 0):
+ break
+start_loc_x = x = i % width
+start_loc_y = y = i // width
+# print(x, y)
+prev_x = prev_y = next_x = next_y = 0
+pixels = image.load()
+pixel = pixels[x, y]
+
+length = 0
+prev_locations = [(x, y)]
+directions = ((1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1))
+while pixel != (255, 255, 0):
+ length += 1
+ possible_locations = [(pixels[x + delta_x, y + delta_y], x + delta_x, y + delta_y, mask) for
+ mask, (delta_x, delta_y) in
+ enumerate(directions) if (0 <= x + delta_x < width) and (0 <= y + delta_y < height) and not (
+ x + delta_x == prev_x and y + delta_y == prev_y) and pixels[x + delta_x, y + delta_y] != (
+ 255, 255, 255)]
+ if len(possible_locations) == 1:
+ pixel, next_x, next_y, mask = possible_locations[0]
+ else:
+ special_locations = sorted([loc for loc in possible_locations if loc[0][2] == 255])
+ if len(special_locations) > 0:
+ pixel, next_x, next_y, mask = special_locations[0]
+ ttl = pixel[1] - 1
+ pixels[next_x, next_y] = (pixel[0] if ttl else 0, ttl, 255 if ttl else 0)
+ else:
+ new_locations = [loc for loc in possible_locations if (loc[1], loc[2]) not in prev_locations]
+ if len(new_locations) > 0:
+ pixel, next_x, next_y, mask = new_locations[0]
+ else:
+ old_locations = sorted((prev_locations.index((loc[1], loc[2])), loc) for loc in possible_locations if
+ (loc[1], loc[2]) in prev_locations)[::-1]
+ pixel, next_x, next_y, mask = old_locations[0][1]
+ if (next_x, next_y) in prev_locations:
+ prev_locations.remove((next_x, next_y))
+ prev_locations.append((next_x, next_y))
+ # print(next_x, next_y)
+ # print(arrows[mask])
+ # _ = input()
+ instructions += bytes([mask])
+ prev_x = x
+ prev_y = y
+ x = next_x
+ y = next_y
+
+with open(argv[1] + "_path.data", 'wb') as file:
+ file.write(pack('<H', start_loc_x))
+ file.write(pack('<H', start_loc_y))
+ file.write(pack('<I', length))
+ file.write(instructions)
+ file.write(pack('<H', x))
+ file.write(pack('<H', y))
diff --git a/src/Component.h b/src/Component.h
index 289f0b4..436f7d4 100644
--- a/src/Component.h
+++ b/src/Component.h
@@ -1,28 +1,31 @@
//
// 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 {
- POSITION, VISIBILITY,
+ VISIBILITY, POSITION, VELOCITY,SPEED, ACCELERATION, PATH_INDEX, HEALTH, KIND, TYPE, DAMAGE, PIERCE, SPREAD,
LENGTH
};
class Component {
Entity *entity;
public:
explicit Component(Entity *entity) : entity(entity) {};
~Component() = default;
Entity *getEntity() { return entity; }
};
#endif //SDL2_GAME_COMPONENT_H
diff --git a/src/Entity.h b/src/Entity.h
index 1f989c6..961818c 100644
--- a/src/Entity.h
+++ b/src/Entity.h
@@ -1,52 +1,47 @@
//
// Created by Ido Mozes on 20/06/2019.
//
#ifndef SDL2_GAME_ENTITY_H
#define SDL2_GAME_ENTITY_H
#include <bitset>
#include <vector>
#include <memory>
#include "Component.h"
constexpr int maxComponents = 64;
typedef std::size_t EntityId;
class Entity {
static EntityId getNewId() {
static EntityId id_counter = 0;
return id_counter++;
}
- bool active = true;
EntityId id;
std::bitset<ComponentType::LENGTH> componentsMask;
std::array<Component *, ComponentType::LENGTH> components;
public:
Entity();
~Entity() = default;
EntityId getId() { return id; }
- bool isActive() { return active; }
-
- void destroy() { active = false; }
-
bool hasComponents(uint64_t mask) { return (componentsMask.to_ullong() & mask) == mask; }
template<class T>
T &getComponent() { return *(T *) (components[T::getComponentType()]); }
template<typename T, typename ... Targs>
Component &addComponent(Targs &&... args){
T *c = new T(this, std::forward<Targs>(args)...);
components[T::getComponentType()] = c;
componentsMask[T::getComponentType()] = true;
return *c;
}
};
#endif //SDL2_GAME_ENTITY_H
diff --git a/src/Game.cpp b/src/Game.cpp
index ae05be7..a3f35c4 100644
--- a/src/Game.cpp
+++ b/src/Game.cpp
@@ -1,42 +1,63 @@
//
// Created by Ido Mozes on 18/06/2019.
//
#include <array>
#include "Game.h"
+Game::Game(bool fullscreen, float mapScale) {
+ gameData.mapScale = mapScale;
+ gameData.fullscreen = fullscreen;
+ renderSystem.init(gameData);
+ loadLevel();
+ auto mapEntity = new Entity();
+ mapEntity->addComponent<Visibility>(renderSystem.getRenderer(), "../assets/level0.jpg");
+ entities.emplace_back(mapEntity);
-Game::Game(bool fullscreen):renderSystem(fullscreen),isRunning(true) {
auto player = new Entity();
- player->addComponent<Visibility>(renderSystem.getRenderer(),"../assets/knight.png",new SDL_Rect{0,0,128,128});
+ player->addComponent<Visibility>(renderSystem.getRenderer(), "../assets/knight.png", new SDL_Rect{0, 0, 50, 0});
+ player->addComponent<Position>(gameData.startingPoint.X, gameData.startingPoint.Y);
+ player->addComponent<Speed>(10);
+ player->addComponent<PathIndex>(0);
entities.emplace_back(player);
- auto player2 = new Entity();
- player2->addComponent<Visibility>(renderSystem.getRenderer(),"../assets/knight.png",new SDL_Rect{0,150,128,128});
- entities.emplace_back(player2);
-// systems = {
-//
-// };
+ systems.emplace_back(new MovementSystem);
+
}
Game::~Game() {
SDL_Quit();
}
void Game::handleEvents() {
SDL_Event event;
SDL_PollEvent(&event);
switch (event.type) {
case SDL_QUIT:
- isRunning = false;
+ gameData.isRunning = false;
break;
default:
break;
}
}
void Game::update() {
- renderSystem.update(entities);
+ for (auto &system : systems) {
+ system->update(entities, gameData);
+ }
+ renderSystem.update(entities, gameData);
+}
+
+void Game::loadLevel() {
+ gameData.path.clear();
+ std::string fileName = "../assets/level" + std::to_string(gameData.level);
+ std::ifstream file(fileName + "_path.data", std::ios::binary);
+ unsigned int length;
+ file.read((char *) &gameData.startingPoint, 4);
+ file.read((char *) &length, 4);
+ gameData.path.resize(length);
+ file.read(&gameData.path[0], length);
+ file.read((char *) &gameData.finishPoint, 4);
}
diff --git a/src/Game.h b/src/Game.h
index 0c34399..d92a595 100644
--- a/src/Game.h
+++ b/src/Game.h
@@ -1,39 +1,46 @@
//
// Created by Ido Mozes on 18/06/2019.
//
#ifndef SDL2_GAME_GAME_H
#define SDL2_GAME_GAME_H
#include <iostream>
+#include <fstream>
#include <memory>
#include <vector>
#include "SDL.h"
#include "SDL_image.h"
#include "Entity.h"
#include "System.h"
#include "systems/RenderSystem.h"
+#include "systems/MovementSystem.h"
#include "components/Visibility.h"
+#include "components/PathIndex.h"
+#include "components/Speed.h"
+#include "GameData.h"
class Game {
- bool isRunning;
std::vector<std::unique_ptr<System>> systems;
- std::vector<std::unique_ptr<Entity>> entities;
+ std::vector<std::shared_ptr<Entity>> entities;
RenderSystem renderSystem;
+ GameData gameData;
+
+
public:
- explicit Game(bool fullscreen);
+ explicit Game(bool fullscreen, float mapScale=1.5);
~Game();
void handleEvents();
void update();
- bool running() { return isRunning; }
-
+ bool running() { return gameData.isRunning; }
+ void loadLevel();
};
#endif //SDL2_GAME_GAME_H
diff --git a/src/GameData.h b/src/GameData.h
new file mode 100644
index 0000000..4e15998
--- /dev/null
+++ b/src/GameData.h
@@ -0,0 +1,23 @@
+//
+// Created by Ido Mozes on 03/07/2019.
+//
+
+#ifndef SDL_GAME_GAMEDATA_H
+#define SDL_GAME_GAMEDATA_H
+
+#include <vector>
+#include "Component.h"
+
+class GameData {
+public:
+ bool isRunning = true;
+ int level = 0;
+ float mapScale = 1.5;
+ bool fullscreen;
+ std::vector<char> path;
+ char **map;
+ Point startingPoint;
+ Point finishPoint;
+};
+
+#endif //SDL_GAME_GAMEDATA_H
diff --git a/src/System.h b/src/System.h
index c5949d4..3af893a 100644
--- a/src/System.h
+++ b/src/System.h
@@ -1,23 +1,24 @@
//
// Created by Ido Mozes on 23/06/2019.
//
#ifndef SDL2_GAME_SYSTEM_H
#define SDL2_GAME_SYSTEM_H
#include <initializer_list>
#include <vector>
#include <cmath>
#include "Entity.h"
+#include "GameData.h"
constexpr uint64_t createMask( std::initializer_list<int> types) noexcept {
uint64_t mask = 0;
for (int bit : types) {
mask |= (uint64_t) pow(2, bit);
}
return mask;
}
class System{
public:
- virtual void update(std::vector<std::unique_ptr<Entity>> &entities)=0;
+ virtual void update(std::vector<std::shared_ptr<Entity>> &entities, GameData & gameData)=0;
};
#endif //SDL2_GAME_SYSTEM_H
diff --git a/src/components/PathIndex.cpp b/src/components/PathIndex.cpp
new file mode 100644
index 0000000..ae4e292
--- /dev/null
+++ b/src/components/PathIndex.cpp
@@ -0,0 +1,5 @@
+//
+// Created by Ido Mozes on 03/07/2019.
+//
+
+#include "PathIndex.h"
diff --git a/src/components/PathIndex.h b/src/components/PathIndex.h
new file mode 100644
index 0000000..456897a
--- /dev/null
+++ b/src/components/PathIndex.h
@@ -0,0 +1,20 @@
+//
+// 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;
+
+ static ComponentType getComponentType() { return ComponentType::PATH_INDEX; }
+
+ PathIndex(Entity *entity, int index) : Component(entity), index(index) {}
+};
+
+#endif //SDL_GAME_PATHINDEX_H
diff --git a/src/components/Position.cpp b/src/components/Position.cpp
new file mode 100644
index 0000000..7301ee6
--- /dev/null
+++ b/src/components/Position.cpp
@@ -0,0 +1,5 @@
+//
+// Created by Ido Mozes on 02/07/2019.
+//
+
+#include "Position.h"
diff --git a/src/components/Position.h b/src/components/Position.h
new file mode 100644
index 0000000..2702de7
--- /dev/null
+++ b/src/components/Position.h
@@ -0,0 +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; }
+
+ 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/Speed.h b/src/components/Speed.h
new file mode 100644
index 0000000..8092528
--- /dev/null
+++ b/src/components/Speed.h
@@ -0,0 +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; }
+
+ Speed(Entity *entity, float speed) : Component(entity), speed(speed) {}
+};
+
+#endif //SDL_GAME_SPEED_H
diff --git a/src/components/Velocity.cpp b/src/components/Velocity.cpp
new file mode 100644
index 0000000..3af03f9
--- /dev/null
+++ b/src/components/Velocity.cpp
@@ -0,0 +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) {}
+
+
+
+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
new file mode 100644
index 0000000..2507124
--- /dev/null
+++ b/src/components/Velocity.h
@@ -0,0 +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; }
+
+ 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; }
+ Acceleration(Entity *entity, float x, float y):Velocity(entity,x,y){}
+ ~Acceleration()= default;
+};
+
+#endif //SDL_GAME_VELOCITY_H
diff --git a/src/components/Visibility.cpp b/src/components/Visibility.cpp
index f668823..ec9ea7a 100644
--- a/src/components/Visibility.cpp
+++ b/src/components/Visibility.cpp
@@ -1,23 +1,42 @@
//
// Created by Ido Mozes on 23/06/2019.
//
#include "Visibility.h"
-SDL_Texture *loadTexture(SDL_Renderer * renderer, const char *path) {
- SDL_Surface *tmpSurface = IMG_Load(path);
- SDL_Texture *tex = SDL_CreateTextureFromSurface(renderer, tmpSurface);
- SDL_FreeSurface(tmpSurface);
- return tex;
-}
-Visibility::Visibility(Entity *entity, SDL_Renderer *renderer, const char *path, SDL_Rect *dstR, SDL_Rect *srcR)
+
+Visibility::Visibility(Entity *entity, SDL_Renderer *renderer, const char *filePath, SDL_Rect *dstR)
: Component(entity) {
- texture = loadTexture(renderer, path);
setDstRect(dstR);
- setSrcRect(srcR);
+ loadTexture(renderer, filePath);
}
Visibility::~Visibility() {
delete dstRect;
- delete srcRect;
SDL_DestroyTexture(texture);
+ SDL_FreeSurface(surface);
+}
+
+void Visibility::loadTexture(SDL_Renderer *renderer, const char *filePath) {
+ loadSurface(filePath);
+ 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 != nullptr) {
+ dstRect->x = x;
+ dstRect->y = y;
+ }
+}
+
+
diff --git a/src/components/Visibility.h b/src/components/Visibility.h
index 97bfdb8..84c21c6 100644
--- a/src/components/Visibility.h
+++ b/src/components/Visibility.h
@@ -1,46 +1,47 @@
//
// Created by Ido Mozes on 23/06/2019.
//
#ifndef SDL2_GAME_VISIBILITY_H
#define SDL2_GAME_VISIBILITY_H
+
+#include <fstream>
#include "SDL.h"
#include "SDL_image.h"
-
-
#include "../Component.h"
SDL_Texture *loadTexture(SDL_Renderer *renderer, const char *path);
class Visibility : public Component {
+ SDL_Surface *surface;
SDL_Texture *texture;
- SDL_Rect *srcRect;
SDL_Rect *dstRect;
+ void loadSurface(const char *filePath) { surface = IMG_Load(filePath); }
+
public:
- static ComponentType getComponentType() { return ComponentType::VISIBILITY; };
+ static ComponentType getComponentType() { return ComponentType::VISIBILITY; }
- Visibility(Entity *entity, SDL_Renderer *renderer, const char *path, SDL_Rect *dstR = nullptr,
- SDL_Rect *srcR = nullptr);
+ Visibility(Entity *entity, SDL_Renderer *renderer, const char *filePath, SDL_Rect *dstR = nullptr);
~Visibility();
SDL_Texture *getTexture() { return texture; }
- SDL_Rect *getSrcRect() { return srcRect; }
-
SDL_Rect *getDstRect() { return dstRect; }
- void setSrcRect(SDL_Rect *srcR) {
- srcRect = srcR ? new SDL_Rect(*srcR) : nullptr;
- delete srcR;
- }
-
void setDstRect(SDL_Rect *dstR) {
dstRect = dstR ? new SDL_Rect(*dstR) : nullptr;
delete dstR;
}
+ void setPosition(int x, int y);
+
+ void loadTexture(SDL_Renderer *renderer, const char *filePath);
+
+ void reloadTexture(SDL_Renderer *renderer);
+
+
};
#endif //SDL2_GAME_VISIBILITY_H
diff --git a/src/systems/MovementSystem.cpp b/src/systems/MovementSystem.cpp
new file mode 100644
index 0000000..aae375c
--- /dev/null
+++ b/src/systems/MovementSystem.cpp
@@ -0,0 +1,65 @@
+//
+// Created by Ido Mozes on 02/07/2019.
+//
+
+#include "MovementSystem.h"
+
+
+void MovementSystem::update(std::vector<std::shared_ptr<Entity>> &entities, GameData & gameData) {
+ for (auto &entity: entities) {
+ // Move all entities with Visibility and Position
+ if (entity->hasComponents(createMask({ComponentType::VISIBILITY, ComponentType::POSITION}))) {
+ auto &visibility = entity->getComponent<Visibility>();
+ auto &position = entity->getComponent<Position>();
+ float deltaX = 0, deltaY = 0;
+ if (entity->hasComponents(createMask({ComponentType::VELOCITY}))) {
+ auto &velocity = entity->getComponent<Velocity>();
+ if (entity->hasComponents(createMask({ACCELERATION}))) {
+ auto &acceleration = entity->getComponent<Acceleration>();
+ velocity.changeVelocity(acceleration.getX(), acceleration.getY());
+ }
+ deltaX = velocity.getX();
+ deltaY = velocity.getY();
+ } else if (entity->hasComponents(createMask({ComponentType::PATH_INDEX, ComponentType::SPEED}))) {
+ auto &pathIndex = entity->getComponent<PathIndex>();
+ auto &speed = entity->getComponent<Speed>();
+ for (int i = 0; i < speed.speed; ++i) {
+ switch (gameData.path[pathIndex.index]) {
+ case 0:
+ deltaX += 1;
+ break;
+ case 1:
+ deltaX += sqrt(0.5);
+ deltaY += sqrt(0.5);
+ break;
+ case 2:
+ deltaY += 1;
+ break;
+ case 3:
+ deltaX += -sqrt(0.5);
+ deltaY += sqrt(0.5);
+ break;
+ case 4:
+ deltaX += -1;
+ break;
+ case 5:
+ deltaX += -sqrt(0.5);
+ deltaY += -sqrt(0.5);
+ break;
+ case 6:
+ deltaY += -1;
+ break;
+ case 7:
+ deltaX += sqrt(0.5);
+ deltaY += -sqrt(0.5);
+ break;
+ }
+ if (pathIndex.index < gameData.path.size() - 1)
+ pathIndex.index++;
+ }
+ }
+ position.changePosition(deltaX * gameData.mapScale, deltaY * gameData.mapScale);
+ visibility.setPosition(int(position.getX()), int(position.getY()));
+ }
+ }
+}
diff --git a/src/systems/MovementSystem.h b/src/systems/MovementSystem.h
new file mode 100644
index 0000000..e646949
--- /dev/null
+++ b/src/systems/MovementSystem.h
@@ -0,0 +1,23 @@
+//
+// Created by Ido Mozes on 02/07/2019.
+//
+
+#ifndef SDL_GAME_MOVEMENTSYSTEM_H
+#define SDL_GAME_MOVEMENTSYSTEM_H
+#include <iostream>
+#include "../components/Visibility.h"
+#include "../components/Velocity.h"
+#include "../components/Position.h"
+#include "../components/PathIndex.h"
+#include "../components/Speed.h"
+#include "../GameData.h"
+#include "../System.h"
+class MovementSystem : public System{
+public:
+ MovementSystem()= default;
+
+ ~MovementSystem()= default;
+
+ void update(std::vector<std::shared_ptr<Entity>> &entities, GameData & gameData) override;
+};
+#endif //SDL_GAME_MOVEMENTSYSTEM_H
diff --git a/src/systems/RenderSystem.cpp b/src/systems/RenderSystem.cpp
index 2e84616..0f67273 100644
--- a/src/systems/RenderSystem.cpp
+++ b/src/systems/RenderSystem.cpp
@@ -1,35 +1,37 @@
//
// Created by Ido Mozes on 23/06/2019.
//
#include "RenderSystem.h"
#include "../components/Visibility.h"
uint64_t RenderSystem::mask = createMask({ComponentType::VISIBILITY});
-RenderSystem::RenderSystem(bool fullscreen) {
- SDL_Init(SDL_INIT_EVERYTHING);
- window = SDL_CreateWindow("BloonsTD", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720,
- fullscreen ? SDL_WINDOW_FULLSCREEN : 0);
- renderer = SDL_CreateRenderer(window, -1, 0);
- SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
-}
RenderSystem::~RenderSystem() {
SDL_DestroyWindow(window);
SDL_DestroyRenderer(renderer);
}
-
-void RenderSystem::update(std::vector<std::unique_ptr<Entity>>& entities) {
+void RenderSystem::init(GameData &gameData){
+ if (window != nullptr)
+ SDL_DestroyWindow(window);
+ if (renderer != nullptr)
+ SDL_DestroyRenderer(renderer);
+ window = SDL_CreateWindow("BloonsTD", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 700 *gameData.mapScale,
+ 520 * gameData.mapScale, gameData.fullscreen ? SDL_WINDOW_FULLSCREEN : 0);
+ renderer = SDL_CreateRenderer(window, -1, 0);
+ SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
+}
+void RenderSystem::update(std::vector<std::shared_ptr<Entity>> &entities, GameData & gameData) {
SDL_RenderClear(renderer);
for (auto &entity: entities) {
if (entity->hasComponents(mask)) {
auto &v = entity->getComponent<Visibility>();
- SDL_RenderCopy(renderer, v.getTexture(), v.getSrcRect(), v.getDstRect());
+ SDL_RenderCopy(renderer, v.getTexture(), nullptr, v.getDstRect());
}
}
SDL_RenderPresent(renderer);
}
diff --git a/src/systems/RenderSystem.h b/src/systems/RenderSystem.h
index 3da3e88..fa6de46 100644
--- a/src/systems/RenderSystem.h
+++ b/src/systems/RenderSystem.h
@@ -1,26 +1,29 @@
//
// Created by Ido Mozes on 23/06/2019.
//
#ifndef SDL2_GAME_RENDERSYSTEM_H
#define SDL2_GAME_RENDERSYSTEM_H
#include <iostream>
#include "../components/Visibility.h"
#include "../System.h"
+#include "../GameData.h"
class RenderSystem : public System {
static uint64_t mask;
- SDL_Window *window;
- SDL_Renderer *renderer;
+ SDL_Window *window = nullptr;
+ SDL_Renderer *renderer = nullptr;
public:
- explicit RenderSystem(bool fullscreen);
+ RenderSystem() { SDL_Init(SDL_INIT_EVERYTHING); }
~RenderSystem();
+ void init(GameData &gameData);
+
SDL_Renderer *getRenderer() { return renderer; }
- void update(std::vector<std::unique_ptr<Entity>> &entities) override;
+ void update(std::vector<std::shared_ptr<Entity>> &entities , GameData & gameData) override;
};
#endif //SDL2_GAME_RENDERSYSTEM_H
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Tue, Jun 16, 12:25 AM (2 w, 1 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
70868
Default Alt Text
(26 KB)
Attached To
Mode
R74 BloonsTD
Attached
Detach File
Event Timeline