Add basic project with gravity system test

This commit is contained in:
karl 2019-12-13 13:26:18 +01:00
parent 83e1cec88b
commit 8032f2efec
6 changed files with 79 additions and 0 deletions

2
.idea/ecsgame.iml generated Normal file
View File

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<module classpath="CMake" type="CPP_MODULE" version="4" />

7
.idea/misc.xml generated Normal file
View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
<component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" />
</component>
</project>

8
.idea/modules.xml generated Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/ecsgame.iml" filepath="$PROJECT_DIR$/.idea/ecsgame.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml generated Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

6
CMakeLists.txt Normal file
View File

@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.15)
project(ecsgame)
set(CMAKE_CXX_STANDARD 17)
add_executable(ecsgame main.cpp)

50
main.cpp Normal file
View File

@ -0,0 +1,50 @@
#include <iostream>
#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<Position>([&](Entity* ent, ComponentHandle<Position> 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<Position>(0.f, 0.f, 0.f); // assign() takes arguments and passes them to the constructor
world->tick(1.0f);
ComponentHandle<Position> pos = ent->get<Position>();
std::cout << "y position after tick: " << pos->y << std::endl;
return 0;
}