Skip to content

Commit

Permalink
Revert "feat: Time System (#4)"
Browse files Browse the repository at this point in the history
This reverts commit 93d8641.
  • Loading branch information
BenjaminHalko authored Jun 8, 2024
1 parent 93d8641 commit 14e1ee9
Show file tree
Hide file tree
Showing 27 changed files with 80 additions and 98 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ set(CMAKE_CXX_STANDARD 17)

# Add source files
file(GLOB_RECURSE SOURCES src/*)
add_executable(Textual_Game ${SOURCES})
add_executable(Textual_Game ${SOURCES}
src/systems/renderSystem.cpp)
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ class GravityComponent : public Component {
float _gravity;
public:
explicit GravityComponent(float gravity) : _gravity(gravity) {}
GravityComponent& operator=(float gravity);
float operator()() const;
GravityComponent& operator=(float gravity) {
this->_gravity = gravity;
return *this;
}
float operator()() const {
return _gravity;
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ class ScriptComponent : public Component {
void (*_script)();
public:
explicit ScriptComponent(void (*script)()) : _script(script) {}
void operator()();
void operator()() { _script(); }
};
File renamed without changes.
File renamed without changes.
File renamed without changes.
32 changes: 32 additions & 0 deletions src/ecs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <iostream>
#include "ecs.h"

// We need to define the static variables here
std::multiset<std::unique_ptr<Entity>> ECS::entities;
std::set<std::unique_ptr<System>> ECS::systems;

/**
* @brief Gets the entities
*/
std::multiset<std::unique_ptr<Entity>>& ECS::GetEntities() {
return entities;
}

/**
* @brief Runs the every frame
* @details It loops over all the systems and calls their UpdateEntity function
*/
void ECS::Update() {
for (auto& system : systems) {
system->Update();
}

// Loop over all the entities
for(auto entity = entities.begin(); entity != entities.end(); entity++) {
// Check if the entity got destroyed
// If it did, remove it from the set
// and move the iterator back one step
if ((*entity)->gotDestroyed())
entity = std::prev(entities.erase(entity));
}
}
13 changes: 5 additions & 8 deletions src/engine/ecs.h → src/ecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
#include <set>

class ECS {
static std::multiset<std::unique_ptr<Entity>> _entities;
static std::set<std::unique_ptr<System>> _systems;
static bool _isRunning;
static std::multiset<std::unique_ptr<Entity>> entities;
static std::set<std::unique_ptr<System>> systems;
public:
/**
* @brief Adds an entity to the ECS
Expand All @@ -18,7 +17,7 @@ class ECS {
*/
template <typename EntityType, typename... Args>
static void AddEntity(Args&&... args) {
_entities.insert(std::make_unique<EntityType>());
entities.insert(std::make_unique<EntityType>());
}

/**
Expand All @@ -27,14 +26,12 @@ class ECS {
*/
template <typename SystemType>
static void AddSystem() {
_systems.insert(std::make_unique<SystemType>());
systems.insert(std::make_unique<SystemType>());
}

static std::multiset<std::unique_ptr<Entity>>& GetEntities();

static void GameLoop();

static void StopGame();
static void Update();
};


10 changes: 0 additions & 10 deletions src/engine/components/gravityComponent.cpp

This file was deleted.

5 changes: 0 additions & 5 deletions src/engine/components/scriptComponent.cpp

This file was deleted.

35 changes: 0 additions & 35 deletions src/engine/ecs.cpp

This file was deleted.

20 changes: 0 additions & 20 deletions src/engine/systems/timeSystem.cpp

This file was deleted.

14 changes: 0 additions & 14 deletions src/engine/systems/timeSystem.h

This file was deleted.

File renamed without changes.
File renamed without changes.
35 changes: 33 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,37 @@
#include "engine/ecs.h"
#include "ecs.h"
#include "systems/input.h"

#include <chrono>
#include <thread>

// ECS
// ECS::entities
// Components
// What systems need to be run
// ECS::systems
// A function on how to manipulate the entities components
// Gets looped over all entities, that use the system

/**
* @brief The main game loop
*/
void GameLoop() {
const auto timeStep = std::chrono::milliseconds(1000 / 60);
do {
// Start the timer
auto start = std::chrono::high_resolution_clock::now();

// Update the entities
ECS::Update();

// Sleep for the remaining time
std::this_thread::sleep_until(start + timeStep);
} while(!Input::GetKeyPressed(Key::QUIT));
}

int main() {
ECS::GameLoop();
// Game Loop
GameLoop();

return 0;
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 14e1ee9

Please sign in to comment.