Skip to content

Commit

Permalink
ECSify
Browse files Browse the repository at this point in the history
  • Loading branch information
Ravbug committed Jul 7, 2024
1 parent f55f7c3 commit 00d8b40
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,5 @@ INSTALL(CODE
COMPONENT Runtime
)
endif()

set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT "${EXECUTABLE_NAME}")
40 changes: 20 additions & 20 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,32 +81,32 @@ struct HelloCubeWorld : public RavEngine::World {

// Lights use the transformation of their parent entity to determine their rotation and position.
lightsEntity.GetTransform().LocalRotateDelta(vector3{ deg_to_rad(45), deg_to_rad(45),0 });
}

// The engine calls this method synchronously every tick after the rest of the pipeline has finished processing.
void PostTick(float tickrateScale) final {
// Finally, we'll add a System. Systems are how you update objects in the world.
// A System can be anything with a call operator. Here we'll use a struct, but lambdas also work.
struct CubeSpinSystem {

// We can hold onto an entity as a class variable, but I'll instead use this opportunity to demonstrate the query system.
// RavEngine worlds can be queried extremely efficiently by component type. Since we know that this world contains
// one static mesh, we can use that to get our entity.
// Note that order is not guarenteed, so I advise against using this trick unless you are certain there is only one instance of the type.
auto& meshComp = GetComponent<StaticMesh>();
// This System will run for every entity in the current world that has both a StaticMesh and a Transform component.
// Our world currently only has one object that matches this component loadout - the cube we added earlier.
// For best performance, order your parameter types from least common to most common.
void operator()(const StaticMesh& mesh, Transform& t) const {

// some components provide a convenience method to get the owning Entity.
auto entity = meshComp.GetOwner();
// Let's spin our obuect.
// RavEngine expects rotations in radians. Use deg_to_rad to convert to degrees.
auto rotVec = vector3(deg_to_rad(1), deg_to_rad(2), deg_to_rad(-0.5));

// Let's spin our cube.
// RavEngine expects rotations in radians. Use deg_to_rad to convert to degrees.
auto rotVec = vector3(deg_to_rad(1), deg_to_rad(2), deg_to_rad(-0.5));
// If the engine were to fall behind, we need to ensure our game does not run in slow motion.
// To accomplish this, we simply multiply our movements by a scale factor passed into this method.
// RavEngine has already calcuated the scale factor for us. This is *not* a deltaTime, as opposed to Unity and Unreal.
rotVec *= GetApp()->GetCurrentFPSScale();

// If the engine were to fall behind, we need to ensure our game does not run in slow motion.
// To accomplish this, we simply multiply our movements by a scale factor passed into this method.
// RavEngine has already calcuated the scale factor for us. This is *not* a deltaTime, as opposed to Unity and Unreal.
rotVec *= tickrateScale;
// Write the change to the transform.
t.LocalRotateDelta(rotVec);
}
};

// Entities can also be queried efficiently for components, just like worlds. Since Transforms are
// a very frequent access, GameObjects provide the convenience function GetTransform, to use instead.
entity.GetTransform().LocalRotateDelta(rotVec);
// Add our new System to the world. The engine will automatically invoke it every frame.
EmplaceSystem<CubeSpinSystem>();
}
};

Expand Down

0 comments on commit 00d8b40

Please sign in to comment.