aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/crepe/api/LoopManager.cpp6
-rw-r--r--src/crepe/api/LoopManager.h5
-rw-r--r--src/crepe/api/Script.h9
-rw-r--r--src/crepe/system/CollisionSystem.cpp6
-rw-r--r--src/crepe/system/CollisionSystem.h2
-rw-r--r--src/example/CMakeLists.txt2
-rw-r--r--src/example/game.cpp (renamed from src/example/collision.cpp)84
-rw-r--r--src/example/particles.cpp43
-rw-r--r--src/example/physics.cpp24
-rw-r--r--src/example/rendering.cpp4
-rw-r--r--src/test/CMakeLists.txt2
-rw-r--r--src/test/CollisionTest.cpp121
12 files changed, 101 insertions, 207 deletions
diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp
index a4bc101..586919d 100644
--- a/src/crepe/api/LoopManager.cpp
+++ b/src/crepe/api/LoopManager.cpp
@@ -35,10 +35,8 @@ void LoopManager::start() {
void LoopManager::set_running(bool running) { this->game_running = running; }
void LoopManager::fixed_update() {
- PhysicsSystem phys;
- phys.update();
- CollisionSystem col;
- col.update();
+ this->get_system<PhysicsSystem>().update();
+ this->get_system<CollisionSystem>().update();
}
void LoopManager::loop() {
diff --git a/src/crepe/api/LoopManager.h b/src/crepe/api/LoopManager.h
index f6904be..37f13ac 100644
--- a/src/crepe/api/LoopManager.h
+++ b/src/crepe/api/LoopManager.h
@@ -4,6 +4,7 @@
#include "../ComponentManager.h"
#include "../system/System.h"
+#include "api/SceneManager.h"
namespace crepe {
@@ -71,7 +72,9 @@ private:
private:
//! Component manager instance
ComponentManager component_manager{};
-
+public:
+ //! Scene manager instance
+ SceneManager scene_manager{component_manager};
private:
/**
* \brief Collection of System instances
diff --git a/src/crepe/api/Script.h b/src/crepe/api/Script.h
index 2b70379..939d142 100644
--- a/src/crepe/api/Script.h
+++ b/src/crepe/api/Script.h
@@ -63,7 +63,14 @@ protected:
*/
template <typename T>
std::vector<std::reference_wrapper<T>> get_components() const;
-
+
+ /**
+ * \brief Gets game object id this script is attached to
+ *
+ * \returns game object id
+ */
+ game_object_id_t get_game_object_id() const {return this->game_object_id;};
+
protected:
// NOTE: Script must have a constructor without arguments so the game programmer doesn't need
// to manually add `using Script::Script` to their concrete script class.
diff --git a/src/crepe/system/CollisionSystem.cpp b/src/crepe/system/CollisionSystem.cpp
index df0ee41..3e73b44 100644
--- a/src/crepe/system/CollisionSystem.cpp
+++ b/src/crepe/system/CollisionSystem.cpp
@@ -18,11 +18,9 @@
using namespace crepe;
-CollisionSystem::CollisionSystem() {}
-
void CollisionSystem::update() {
// Get collider components and keep them seperate
- ComponentManager & mgr = ComponentManager::get_instance();
+ ComponentManager & mgr = this->component_manager;
std::vector<std::reference_wrapper<BoxCollider>> boxcolliders = mgr.get_components_by_type<BoxCollider>();
std::vector<std::reference_wrapper<CircleCollider>> circlecolliders = mgr.get_components_by_type<CircleCollider>();
@@ -185,7 +183,7 @@ void CollisionSystem::static_collision_handler(CollisionInfo& info){
}
std::vector<std::pair<CollisionSystem::CollidedInfoStor,CollisionSystem::CollidedInfoStor>> CollisionSystem::check_collisions(const std::vector<std::reference_wrapper<BoxCollider>>& boxcolliders, const std::vector<std::reference_wrapper<CircleCollider>>& circlecolliders) {
- ComponentManager & mgr = ComponentManager::get_instance();
+ ComponentManager & mgr = this->component_manager;
std::vector<std::pair<CollidedInfoStor,CollidedInfoStor>> collisions_ret;
// TODO:
diff --git a/src/crepe/system/CollisionSystem.h b/src/crepe/system/CollisionSystem.h
index 0518b34..d94f6bc 100644
--- a/src/crepe/system/CollisionSystem.h
+++ b/src/crepe/system/CollisionSystem.h
@@ -70,8 +70,6 @@ public:
public:
- CollisionSystem();
-
//! Updates the collision system by checking for collisions between colliders and handling them.
void update() override;
diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt
index d2ea926..2facc4d 100644
--- a/src/example/CMakeLists.txt
+++ b/src/example/CMakeLists.txt
@@ -28,7 +28,7 @@ add_example(proxy)
add_example(db)
add_example(ecs)
add_example(scene_manager)
-add_example(collision)
+add_example(game)
add_example(events)
add_example(particles)
add_example(gameloop)
diff --git a/src/example/collision.cpp b/src/example/game.cpp
index f51380e..a9f6103 100644
--- a/src/example/collision.cpp
+++ b/src/example/game.cpp
@@ -1,31 +1,19 @@
-#include "api/BoxCollider.h"
-#include "system/CollisionSystem.h"
-#include <crepe/system/ScriptSystem.h>
-#include <crepe/Component.h>
-#include <crepe/ComponentManager.h>
#include <crepe/api/GameObject.h>
+#include <crepe/api/Transform.h>
#include <crepe/api/Rigidbody.h>
#include <crepe/api/BoxCollider.h>
-#include <crepe/api/Transform.h>
-#include <crepe/system/PhysicsSystem.h>
-#include <crepe/system/RenderSystem.h>
-#include <crepe/util/log.h>
-
+#include <crepe/api/Camera.h>
#include <crepe/api/Script.h>
-#include <crepe/api/AssetManager.h>
#include <crepe/api/Color.h>
#include <crepe/api/Sprite.h>
#include <crepe/api/Texture.h>
-#include <crepe/api/Transform.h>
#include <crepe/api/Vector2.h>
#include <crepe/api/Event.h>
#include <crepe/api/EventManager.h>
#include <crepe/api/LoopManager.h>
-#include <chrono>
-#include <memory>
-
using namespace crepe;
+
using namespace std;
class MyScript : public Script {
@@ -34,27 +22,28 @@ class MyScript : public Script {
return true;
}
void init() {
- EventManager::get_instance().subscribe<CollisionEvent>(oncollision, this->parent->game_object_id);
+ EventManager::get_instance().subscribe<CollisionEvent>(oncollision, this->get_game_object_id());
}
void update() {
// Retrieve component from the same GameObject this script is on
}
-
-
};
-int main(int argc, char * argv[]) {
- //setup
- LoopManager gameloop;
- Color color(0, 0, 0, 0);
+class ConcreteScene1 : public Scene {
+public:
+ using Scene::Scene;
+
+ void load_scene() {
+ ComponentManager & mgr = this->component_manager;
+ Color color(0, 0, 0, 0);
double screen_size_width = 640;
double screen_size_height = 480;
double world_collider = 1000;
//define playable world
- GameObject World(0, "Name", "Tag", Vector2{screen_size_width/2, screen_size_height/2}, 0, 1);
- World.add_component<Rigidbody>(Rigidbody::Data{
+ GameObject world = mgr.new_object("Name", "Tag", Vector2{screen_size_width/2, screen_size_height/2}, 0, 1);
+ world.add_component<Rigidbody>(Rigidbody::Data{
.mass = 0,
.gravity_scale = 0,
.body_type = Rigidbody::BodyType::STATIC,
@@ -63,13 +52,13 @@ int main(int argc, char * argv[]) {
.bounce = false,
.offset = {0,0}
});
- World.add_component<BoxCollider>(Vector2{0, 0-(screen_size_height/2+world_collider/2)}, world_collider, world_collider);; // Top
- World.add_component<BoxCollider>(Vector2{0, screen_size_height/2+world_collider/2}, world_collider, world_collider); // Bottom
- World.add_component<BoxCollider>(Vector2{0-(screen_size_width/2+world_collider/2), 0}, world_collider, world_collider); // Left
- World.add_component<BoxCollider>(Vector2{screen_size_width/2+world_collider/2, 0}, world_collider, world_collider); // right
+ world.add_component<BoxCollider>(Vector2{0, 0-(screen_size_height/2+world_collider/2)}, world_collider, world_collider);; // Top
+ world.add_component<BoxCollider>(Vector2{0, screen_size_height/2+world_collider/2}, world_collider, world_collider); // Bottom
+ world.add_component<BoxCollider>(Vector2{0-(screen_size_width/2+world_collider/2), 0}, world_collider, world_collider); // Left
+ world.add_component<BoxCollider>(Vector2{screen_size_width/2+world_collider/2, 0}, world_collider, world_collider); // right
- GameObject game_object1(1, "Name", "Tag", Vector2{screen_size_width/2, screen_size_height/2}, 0, 1);
+ GameObject game_object1 = mgr.new_object("Name", "Tag", Vector2{screen_size_width/2, screen_size_height/2}, 0, 1);
game_object1.add_component<Rigidbody>(Rigidbody::Data{
.mass = 1,
.gravity_scale = 0.01,
@@ -87,37 +76,14 @@ int main(int argc, char * argv[]) {
make_shared<Texture>("/home/jaro/crepe/asset/texture/green_square.png"), color,
FlipSettings{true, true});
game_object1.add_component<Camera>(Color::get_white());
-
-
- // GameObject game_object2(2, "Name", "Tag", Vector2{20, 470}, 0, 1);
- // game_object2.add_component<Rigidbody>(Rigidbody::Data{
- // .mass = 1,
- // .gravity_scale = 1,
- // .body_type = Rigidbody::BodyType::DYNAMIC,
- // .linear_velocity = {0,0},
- // .constraints = {0, 0, 0},
- // .use_gravity = false,
- // .bounce = false,
- // .offset = {0,0},
- // });
- // game_object2.add_component<BoxCollider>(Vector2{0, 0}, 0, 0);
- // game_object2.add_component<BehaviorScript>().set_script<MyScript>();
- // game_object2.add_component<Sprite>(
- // make_shared<Texture>("/home/jaro/crepe/asset/texture/red_square.png"), color,
- // FlipSettings{true, true});
-
-
- crepe::ScriptSystem sys;
- // Update all scripts. This should result in MyScript::update being called
- sys.update();
-
- gameloop.start();
- // auto & render = crepe::RenderSystem::get_instance();
- // auto start = std::chrono::steady_clock::now();
- // while (std::chrono::steady_clock::now() - start < std::chrono::seconds(5)) {
- // render.update();
- // }
+ }
+};
+int main(int argc, char * argv[]) {
+ LoopManager gameloop;
+ gameloop.scene_manager.add_scene<ConcreteScene1>("scene1");
+ gameloop.scene_manager.load_next_scene();
+ gameloop.start();
return 0;
}
diff --git a/src/example/particles.cpp b/src/example/particles.cpp
deleted file mode 100644
index 3d5f676..0000000
--- a/src/example/particles.cpp
+++ /dev/null
@@ -1,43 +0,0 @@
-#include <crepe/ComponentManager.h>
-#include <crepe/api/AssetManager.h>
-
-#include <crepe/Component.h>
-#include <crepe/api/Color.h>
-#include <crepe/api/GameObject.h>
-#include <crepe/api/ParticleEmitter.h>
-#include <crepe/api/Rigidbody.h>
-#include <crepe/api/Sprite.h>
-#include <crepe/api/Texture.h>
-#include <crepe/api/Transform.h>
-
-using namespace crepe;
-using namespace std;
-
-int main(int argc, char * argv[]) {
- ComponentManager mgr{};
- GameObject game_object = mgr.new_object("", "", Vector2{0, 0}, 0, 0);
- Color color(0, 0, 0, 0);
- Sprite test_sprite = game_object.add_component<Sprite>(
- make_shared<Texture>("../asset/texture/img.png"), color, FlipSettings{true, true});
- game_object.add_component<ParticleEmitter>(ParticleEmitter::Data{
- .position = {0, 0},
- .max_particles = 100,
- .emission_rate = 0,
- .min_speed = 0,
- .max_speed = 0,
- .min_angle = 0,
- .max_angle = 0,
- .begin_lifespan = 0,
- .end_lifespan = 0,
- .force_over_time = Vector2{0, 0},
- .boundary{
- .width = 0,
- .height = 0,
- .offset = Vector2{0, 0},
- .reset_on_exit = false,
- },
- .sprite = test_sprite,
- });
-
- return 0;
-}
diff --git a/src/example/physics.cpp b/src/example/physics.cpp
deleted file mode 100644
index ad663a0..0000000
--- a/src/example/physics.cpp
+++ /dev/null
@@ -1,24 +0,0 @@
-#include <crepe/Component.h>
-#include <crepe/ComponentManager.h>
-#include <crepe/api/GameObject.h>
-#include <crepe/api/Rigidbody.h>
-#include <crepe/api/Transform.h>
-#include <crepe/system/PhysicsSystem.h>
-
-using namespace crepe;
-using namespace std;
-
-int main(int argc, char * argv[]) {
- ComponentManager mgr{};
-
- GameObject game_object = mgr.new_object("Name", "Tag", Vector2{0, 0}, 0, 0);
- game_object.add_component<Rigidbody>(Rigidbody::Data{
- .mass = 1,
- .gravity_scale = 1,
- .body_type = Rigidbody::BodyType::DYNAMIC,
- .constraints = {0, 0, 0},
- .use_gravity = true,
- .bounce = false,
- });
- return 0;
-}
diff --git a/src/example/rendering.cpp b/src/example/rendering.cpp
index c9e62f1..14ecaa9 100644
--- a/src/example/rendering.cpp
+++ b/src/example/rendering.cpp
@@ -30,13 +30,13 @@ int main() {
// Normal adding components
{
Color color(0, 0, 0, 0);
- obj.add_component<Sprite>(make_shared<Texture>("../asset/texture/img.png"), color,
+ obj.add_component<Sprite>(make_shared<Texture>("/home/jaro/crepe/asset/texture/green_square.png"), color,
FlipSettings{false, false});
obj.add_component<Camera>(Color::get_red());
}
{
Color color(0, 0, 0, 0);
- obj1.add_component<Sprite>(make_shared<Texture>("../asset/texture/second.png"), color,
+ obj1.add_component<Sprite>(make_shared<Texture>("/home/jaro/crepe/asset/texture/green_square.png"), color,
FlipSettings{true, true});
}
diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt
index a41d097..f830165 100644
--- a/src/test/CMakeLists.txt
+++ b/src/test/CMakeLists.txt
@@ -1,6 +1,4 @@
target_sources(test_main PUBLIC
- dummy.cpp
- # audio.cpp
CollisionTest.cpp
main.cpp
PhysicsTest.cpp
diff --git a/src/test/CollisionTest.cpp b/src/test/CollisionTest.cpp
index 3e43479..83564e1 100644
--- a/src/test/CollisionTest.cpp
+++ b/src/test/CollisionTest.cpp
@@ -1,83 +1,76 @@
-#include "api/BoxCollider.h"
-#include "api/CircleCollider.h"
-#include "api/Vector2.h"
#include <crepe/ComponentManager.h>
#include <crepe/api/Config.h>
#include <crepe/api/GameObject.h>
#include <crepe/api/Rigidbody.h>
#include <crepe/api/Transform.h>
-#include <crepe/system/CollisionSystem.h>
+#include <crepe/system/PhysicsSystem.h>
#include <gtest/gtest.h>
using namespace std;
using namespace std::chrono_literals;
using namespace crepe;
-class CollisionTest : public ::testing::Test {
-protected:
- GameObject * game_object1;
- GameObject * game_object2;
- CollisionSystem collision_system;
- void SetUp() override {
- ComponentManager & mgr = ComponentManager::get_instance();
- mgr.delete_all_components();
- std::vector<std::reference_wrapper<Transform>> transforms
- = mgr.get_components_by_id<Transform>(0);
-
- // ob 1
- game_object1 = new GameObject(0, "", "", Vector2{0, 0}, 0, 0);
- game_object1->add_component<Rigidbody>(Rigidbody::Data{
- .mass = 1,
- .gravity_scale = 1,
- .body_type = Rigidbody::BodyType::DYNAMIC,
- .max_linear_velocity = Vector2{10, 10},
- .max_angular_velocity = 10,
- .constraints = {0, 0, 0},
- .use_gravity = false,
- .bounce = false,
- });
-
- game_object1->add_component<BoxCollider>(Vector2{0,0},10,10);
+class MyScript : public Script {
+ static bool oncollision(const CollisionEvent& test) {
+ std::cout << "test collision: " << test.info.first.collider.game_object_id << std::endl;
+ return true;
+ }
+ void init() {
+ EventManager::get_instance().subscribe<CollisionEvent>(oncollision, this->get_game_object_id());
+ }
+ void update() {
+ // Retrieve component from the same GameObject this script is on
+ }
+};
+
+class PhysicsTest : public ::testing::Test {
+public:
+ ComponentManager component_manager;
+ PhysicsSystem system{component_manager};
- //ob 2
- game_object2 = new GameObject(1, "", "", Vector2{50, 50}, 0, 0);
- game_object2->add_component<Rigidbody>(Rigidbody::Data{
- .mass = 1,
- .gravity_scale = 1,
- .body_type = Rigidbody::BodyType::DYNAMIC,
- .max_linear_velocity = Vector2{10, 10},
- .max_angular_velocity = 10,
- .constraints = {0, 0, 0},
- .use_gravity = false,
- .bounce = false,
- });
- game_object2->add_component<CircleCollider>(Vector2{0,0},5);
+ void SetUp() override {
+ ComponentManager & mgr = this->component_manager;
+ vector<reference_wrapper<Transform>> transforms
+ = mgr.get_components_by_id<Transform>(0);
+ if (transforms.empty()) {
+ auto entity = mgr.new_object("", "", Vector2{0, 0}, 0, 0);
+ entity.add_component<Rigidbody>(Rigidbody::Data{
+ .mass = 1,
+ .gravity_scale = 1,
+ .body_type = Rigidbody::BodyType::DYNAMIC,
+ .max_linear_velocity = Vector2{10, 10},
+ .max_angular_velocity = 10,
+ .constraints = {0, 0},
+ .use_gravity = true,
+ .bounce = false,
+ });
+ }
+ transforms = mgr.get_components_by_id<Transform>(0);
+ Transform & transform = transforms.front().get();
+ transform.position.x = 0.0;
+ transform.position.y = 0.0;
+ transform.rotation = 0.0;
+ vector<reference_wrapper<Rigidbody>> rigidbodies
+ = mgr.get_components_by_id<Rigidbody>(0);
+ Rigidbody & rigidbody = rigidbodies.front().get();
+ rigidbody.data.angular_velocity = 0;
+ rigidbody.data.linear_velocity.x = 0;
+ rigidbody.data.linear_velocity.y = 0;
}
};
-TEST_F(CollisionTest, box_box_collision) {
+TEST_F(PhysicsTest, gravity) {
Config::get_instance().physics.gravity = 1;
- ComponentManager & mgr = ComponentManager::get_instance();
- std::vector<std::reference_wrapper<Transform>> transforms
- = mgr.get_components_by_id<Transform>(0);
- Transform & transform = transforms.front().get();
+ ComponentManager & mgr = this->component_manager;
+ vector<reference_wrapper<Transform>> transforms = mgr.get_components_by_id<Transform>(0);
+ const Transform & transform = transforms.front().get();
ASSERT_FALSE(transforms.empty());
- transform.position = {39,50};
- collision_system.update();
- transform.position = {40,50};
- collision_system.update();
- transform.position = {50,39};
- collision_system.update();
- transform.position = {50,40};
- collision_system.update();
- transform.position = {50,60};
- collision_system.update();
- transform.position = {50,61};
- collision_system.update();
- transform.position = {60,50};
- collision_system.update();
- transform.position = {61,50};
- collision_system.update();
-}
+ EXPECT_EQ(transform.position.y, 0);
+
+ system.update();
+ EXPECT_EQ(transform.position.y, 1);
+ system.update();
+ EXPECT_EQ(transform.position.y, 3);
+}