From 8032f2efec59a28128b6fe21b58a2eda43d4998a Mon Sep 17 00:00:00 2001 From: karl Date: Fri, 13 Dec 2019 13:26:18 +0100 Subject: [PATCH] Add basic project with gravity system test --- .idea/ecsgame.iml | 2 ++ .idea/misc.xml | 7 +++++++ .idea/modules.xml | 8 ++++++++ .idea/vcs.xml | 6 ++++++ CMakeLists.txt | 6 ++++++ main.cpp | 50 +++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 79 insertions(+) create mode 100644 .idea/ecsgame.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 CMakeLists.txt create mode 100644 main.cpp diff --git a/.idea/ecsgame.iml b/.idea/ecsgame.iml new file mode 100644 index 0000000..f08604b --- /dev/null +++ b/.idea/ecsgame.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..8822db8 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..26103e7 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..6fbf53f --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.15) +project(ecsgame) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(ecsgame main.cpp) \ No newline at end of file diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..4226a0c --- /dev/null +++ b/main.cpp @@ -0,0 +1,50 @@ +#include +#include "ECS.h" + +using namespace ECS; + +struct Position +{ + Position(float x, float y, float z) : x(x), y(y), z(z) {} + Position() : x(0.f), y(0.f), z(0.f) {} + + float x; + float y; + float z; +}; + +class GravitySystem : public EntitySystem +{ +public: + GravitySystem(float amount) + { + gravityAmount = amount; + } + + virtual ~GravitySystem() {} + + virtual void tick(World* world, float deltaTime) override + { + world->each([&](Entity* ent, ComponentHandle position) { + position->y += gravityAmount * deltaTime; + }); + } + +private: + float gravityAmount; +}; + +int main() { + World* world = World::createWorld(); + world->registerSystem(new GravitySystem(-9.8f)); + + Entity* ent = world->create(); + ent->assign(0.f, 0.f, 0.f); // assign() takes arguments and passes them to the constructor + + world->tick(1.0f); + + ComponentHandle pos = ent->get(); + + std::cout << "y position after tick: " << pos->y << std::endl; + return 0; +}