aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/crepe/api/Script.cpp15
-rw-r--r--src/crepe/api/Script.h38
-rw-r--r--src/crepe/system/InputSystem.cpp6
-rw-r--r--src/example/game.cpp311
4 files changed, 366 insertions, 4 deletions
diff --git a/src/crepe/api/Script.cpp b/src/crepe/api/Script.cpp
index b147252..06b535f 100644
--- a/src/crepe/api/Script.cpp
+++ b/src/crepe/api/Script.cpp
@@ -20,6 +20,21 @@ void Script::subscribe(const EventHandler<CollisionEvent> & callback) {
this->subscribe_internal(callback, this->game_object_id);
}
+template <>
+void Script::subscribe(const EventHandler<ButtonExitEvent> & callback) {
+ this->subscribe_internal(callback, this->game_object_id);
+}
+
+template <>
+void Script::subscribe(const EventHandler<ButtonPressEvent> & callback) {
+ this->subscribe_internal(callback, this->game_object_id);
+}
+
+template <>
+void Script::subscribe(const EventHandler<ButtonEnterEvent> & callback) {
+ this->subscribe_internal(callback, this->game_object_id);
+}
+
void Script::set_next_scene(const string & name) {
SceneManager & mgr = this->mediator->scene_manager;
mgr.set_next_scene(name);
diff --git a/src/crepe/api/Script.h b/src/crepe/api/Script.h
index 5f68928..bbee920 100644
--- a/src/crepe/api/Script.h
+++ b/src/crepe/api/Script.h
@@ -8,6 +8,7 @@
#include "../manager/Mediator.h"
#include "../manager/ReplayManager.h"
#include "../system/CollisionSystem.h"
+#include "../system/InputSystem.h"
#include "../types.h"
#include "../util/Log.h"
#include "../util/OptionalRef.h"
@@ -270,7 +271,42 @@ void Script::subscribe(const EventHandler<CollisionEvent> & callback);
template <>
void Script::subscribe(const EventHandler<CollisionEvent> & callback, event_channel_t)
= delete;
-
+/**
+ * \brief Subscribe to ButtonPressEvent for the current GameObject
+ *
+ * This is a template specialization for Script::subscribe which automatically sets the event
+ * channel so the callback handler is only called for ButtonPressEvent events that apply to the
+ * current GameObject the parent BehaviorScript is attached to.
+ */
+template <>
+void Script::subscribe(const EventHandler<ButtonPressEvent> & callback);
+template <>
+void Script::subscribe(const EventHandler<ButtonPressEvent> & callback, event_channel_t)
+ = delete;
+/**
+ * \brief Subscribe to ButtonExitEvent for the current GameObject
+ *
+ * This is a template specialization for Script::subscribe which automatically sets the event
+ * channel so the callback handler is only called for ButtonExitEvent events that apply to the
+ * current GameObject the parent BehaviorScript is attached to.
+ */
+template <>
+void Script::subscribe(const EventHandler<ButtonExitEvent> & callback);
+template <>
+void Script::subscribe(const EventHandler<ButtonExitEvent> & callback, event_channel_t)
+ = delete;
+/**
+ * \brief Subscribe to ButtonEnterEvent for the current GameObject
+ *
+ * This is a template specialization for Script::subscribe which automatically sets the event
+ * channel so the callback handler is only called for ButtonEnterEvent events that apply to the
+ * current GameObject the parent BehaviorScript is attached to.
+ */
+template <>
+void Script::subscribe(const EventHandler<ButtonEnterEvent> & callback);
+template <>
+void Script::subscribe(const EventHandler<ButtonEnterEvent> & callback, event_channel_t)
+ = delete;
} // namespace crepe
#include "Script.hpp"
diff --git a/src/crepe/system/InputSystem.cpp b/src/crepe/system/InputSystem.cpp
index 858a645..8e9f763 100644
--- a/src/crepe/system/InputSystem.cpp
+++ b/src/crepe/system/InputSystem.cpp
@@ -171,12 +171,12 @@ void InputSystem::handle_move(const EventData & event_data, const vec2 & mouse_p
if (this->is_mouse_inside_button(mouse_pos, button, transform, cam_transform)) {
button.hover = true;
if (!was_hovering) {
- event_mgr.trigger_event<ButtonEnterEvent>(metadata);
+ event_mgr.trigger_event<ButtonEnterEvent>(metadata, metadata.game_object_id);
}
} else {
button.hover = false;
if (was_hovering) {
- event_mgr.trigger_event<ButtonExitEvent>(metadata);
+ event_mgr.trigger_event<ButtonExitEvent>(metadata, metadata.game_object_id);
}
}
}
@@ -196,7 +196,7 @@ void InputSystem::handle_click(const MouseButton & mouse_button, const vec2 & mo
const Transform & transform
= mgr.get_components_by_id<Transform>(button.game_object_id).front();
if (this->is_mouse_inside_button(mouse_pos, button, transform, cam_transform)) {
- event_mgr.trigger_event<ButtonPressEvent>(metadata);
+ event_mgr.trigger_event<ButtonPressEvent>(metadata, metadata.game_object_id);
}
}
}
diff --git a/src/example/game.cpp b/src/example/game.cpp
index b9f98fc..a028791 100644
--- a/src/example/game.cpp
+++ b/src/example/game.cpp
@@ -1,10 +1,321 @@
#include "GameScene.h"
#include <crepe/api/Engine.h>
+#include <crepe/api/Script.h>
using namespace crepe;
using namespace std;
+class ScriptBox : public Script {
+public:
+ bool oncollision(const CollisionEvent & test) {
+ Log::logf("Box {} on_collision() with {}", test.info.self.metadata.game_object_id,
+ test.info.other.metadata.game_object_id);
+ return true;
+ }
+
+ void init() {
+ subscribe<CollisionEvent>(
+ [this](const CollisionEvent & ev) -> bool { return this->oncollision(ev); });
+ }
+};
+
+class ScriptCircle : public Script {
+public:
+ bool oncollision(const CollisionEvent & test) {
+ Log::logf("Circle {} on_collision() with {}", test.info.self.metadata.game_object_id,
+ test.info.other.metadata.game_object_id);
+ return true;
+ }
+
+ void init() {
+ subscribe<CollisionEvent>(
+ [this](const CollisionEvent & ev) -> bool { return this->oncollision(ev); });
+ }
+};
+
+class ScriptMoveToLeft : public Script {
+public:
+ void update() {
+ Transform & transform = this->get_component<Transform>();
+ transform.position.x -= 0.02;
+ }
+};
+
+class ScriptMoveToRight : public Script {
+public:
+ void update() {
+ Transform & transform = this->get_component<Transform>();
+ transform.position.x += 0.02;
+ }
+};
+
+class ConcreteScene1 : public Scene {
+public:
+ void load_scene() {
+ GameObject camera = this->new_object("camera");
+ camera.add_component<Camera>(ivec2(1080, 720), vec2(10, 10),
+ Camera::Data{.bg_color = Color::WHITE, .zoom = 1});
+
+ GameObject reference = this->new_object("reference", "tag", vec2(0, 0), 0, 1);
+ Asset reference_asset = Asset("asset/texture/square.png");
+ reference.add_component<Sprite>(reference_asset, Sprite::Data{
+ .color = Color::RED,
+ .sorting_in_layer = 10,
+ .order_in_layer = 0,
+ .size = vec2(0.1, 0.1),
+ .angle_offset = 0,
+ .scale_offset = 1,
+ .position_offset = vec2(0, 0),
+ });
+
+ /*GameObject box_1 = this->new_object("box_1", "tag", vec2(0, 0), 0, 1);
+ Asset box_1_asset = Asset("asset/texture/square.png");
+ box_1.add_component<Sprite>(box_1_asset, Sprite::Data{
+ .sorting_in_layer = 0,
+ .order_in_layer = 0,
+ .size = vec2(1, 1),
+ .angle_offset = 0,
+ .scale_offset = 1,
+ .position_offset = vec2(0, 0.5),
+ });
+ Asset box_1_asset2 = Asset("asset/texture/test_ap43.png");
+ box_1.add_component<Sprite>(box_1_asset2, Sprite::Data{
+ .sorting_in_layer = 0,
+ .order_in_layer = 0,
+ .size = vec2(1, 1),
+ .angle_offset = 0,
+ .scale_offset = 1,
+ .position_offset = vec2(0, -0.5),
+ });*/
+
+ /*GameObject particles = this->new_object("particles", "tag", vec2(-1, 0), 180, 1);
+ Asset particles_asset = Asset("asset/texture/test_ap43.png");
+ Sprite & particles_sprite = particles.add_component<Sprite>(particles_asset, Sprite::Data{
+ .sorting_in_layer = 0,
+ .order_in_layer = 0,
+ .size = vec2(1, 1),
+ .angle_offset = 0,
+ .scale_offset = 1,
+ .position_offset = vec2(0, 0),
+ });
+ ParticleEmitter & particles_emitter = particles.add_component<ParticleEmitter>(particles_sprite, ParticleEmitter::Data{
+ .offset = vec2(0, 0),
+ .emission_rate = 1,
+ .min_speed = 1,
+ .max_speed = 1,
+ .min_angle = 0,
+ .max_angle = 0,
+ .end_lifespan = 4,
+ });*/
+
+ const bool SCRIPT = false;
+
+ const Rigidbody::BodyType BODYTYPE_LEFT = Rigidbody::BodyType::DYNAMIC;
+ const Rigidbody::BodyType BODYTYPE_RIGHT = Rigidbody::BodyType::DYNAMIC;
+ const bool BOUNCE_LEFT = false;
+ const bool BOUNCE_RIGHT = false;
+ const bool KINEMATIC_COLLISION = true;
+ const bool ONTOP = false;
+
+ const float SCALE = 0.5;
+ const float OFFSET_X_LEFT = 0;
+ const float OFFSET_X_RIGHT = 0;
+ const float OFFSET_Y_LEFT = 0;
+ const float OFFSET_Y_RIGHT = 0;
+
+ GameObject box_1 = this->new_object("box_1", "tag",
+ ONTOP ? vec2(-0.25, -4) : vec2(-2, -4), 0, SCALE);
+ Asset box_1_asset = Asset("asset/texture/square.png");
+ box_1.add_component<Sprite>(box_1_asset,
+ Sprite::Data{
+ .sorting_in_layer = 0,
+ .order_in_layer = 0,
+ .size = vec2(1, 1),
+ .angle_offset = 0,
+ .scale_offset = 1,
+ .position_offset = vec2(OFFSET_X_LEFT, OFFSET_Y_LEFT),
+ });
+ box_1.add_component<Rigidbody>(Rigidbody::Data{
+ .gravity_scale = 0,
+ .body_type = BODYTYPE_LEFT,
+ .linear_velocity = SCRIPT ? vec2{0, 0} : vec2{1, 0},
+ .elasticity_coefficient = BOUNCE_LEFT ? 0.5 : 0,
+ .kinematic_collision = KINEMATIC_COLLISION,
+ });
+ box_1.add_component<BehaviorScript>().set_script<ScriptMoveToRight>().active = SCRIPT;
+ box_1.add_component<BoxCollider>(vec2(1, 1), vec2(OFFSET_X_LEFT, OFFSET_Y_LEFT));
+ box_1.add_component<BehaviorScript>().set_script<ScriptBox>();
+
+ GameObject circle_1 = this->new_object("ricle_1", "tag",
+ ONTOP ? vec2(0.25, -4) : vec2(2, -4), 0, SCALE);
+ Asset circle_1_asset = Asset("asset/texture/circle.png");
+ circle_1.add_component<Sprite>(
+ circle_1_asset, Sprite::Data{
+ .sorting_in_layer = 0,
+ .order_in_layer = 0,
+ .size = vec2(1, 1),
+ .angle_offset = 0,
+ .scale_offset = 1,
+ .position_offset = vec2(OFFSET_X_RIGHT, OFFSET_Y_RIGHT),
+ });
+ circle_1.add_component<Rigidbody>(Rigidbody::Data{
+ .gravity_scale = 0,
+ .body_type = BODYTYPE_RIGHT,
+ .linear_velocity = SCRIPT ? vec2{0, 0} : vec2{-1, 0},
+ .elasticity_coefficient = BOUNCE_RIGHT ? 0.5 : 0,
+ .kinematic_collision = KINEMATIC_COLLISION,
+ });
+ circle_1.add_component<BehaviorScript>().set_script<ScriptMoveToLeft>().active
+ = SCRIPT;
+ circle_1.add_component<CircleCollider>(0.5, vec2(OFFSET_X_RIGHT, OFFSET_Y_RIGHT));
+ circle_1.add_component<BehaviorScript>().set_script<ScriptCircle>();
+
+ GameObject circle_2 = this->new_object(
+ "ricle_2", "tag", ONTOP ? vec2(-0.25, -1.5) : vec2(-2.5, -1.5), 0, SCALE);
+ Asset circle_2_asset = Asset("asset/texture/circle.png");
+ circle_2.add_component<Sprite>(
+ circle_2_asset, Sprite::Data{
+ .sorting_in_layer = 0,
+ .order_in_layer = 0,
+ .size = vec2(1, 1),
+ .angle_offset = 0,
+ .scale_offset = 1,
+ .position_offset = vec2(OFFSET_X_LEFT, OFFSET_Y_LEFT),
+ });
+ circle_2.add_component<Rigidbody>(Rigidbody::Data{
+ .gravity_scale = 0,
+ .body_type = BODYTYPE_LEFT,
+ .linear_velocity = SCRIPT ? vec2{0, 0} : vec2{1, 0},
+ .elasticity_coefficient = BOUNCE_LEFT ? 0.5 : 0,
+ .kinematic_collision = KINEMATIC_COLLISION,
+ });
+ circle_2.add_component<BehaviorScript>().set_script<ScriptMoveToRight>().active
+ = SCRIPT;
+ circle_2.add_component<CircleCollider>(0.5, vec2(OFFSET_X_LEFT, OFFSET_Y_LEFT));
+ circle_2.add_component<BehaviorScript>().set_script<ScriptCircle>();
+
+ GameObject box_2 = this->new_object(
+ "box_2", "tag", ONTOP ? vec2(0.25, -1.5) : vec2(2.5, -1.5), 0, SCALE);
+ Asset box_2_asset = Asset("asset/texture/square.png");
+ box_2.add_component<Sprite>(
+ box_2_asset, Sprite::Data{
+ .sorting_in_layer = 0,
+ .order_in_layer = 0,
+ .size = vec2(1, 1),
+ .angle_offset = 0,
+ .scale_offset = 1,
+ .position_offset = vec2(OFFSET_X_RIGHT, OFFSET_Y_RIGHT),
+ });
+ box_2.add_component<Rigidbody>(Rigidbody::Data{
+ .gravity_scale = 0,
+ .body_type = BODYTYPE_RIGHT,
+ .linear_velocity = SCRIPT ? vec2{0, 0} : vec2{-1, 0},
+ .elasticity_coefficient = BOUNCE_RIGHT ? 0.5 : 0,
+ .kinematic_collision = KINEMATIC_COLLISION,
+ });
+ box_2.add_component<BehaviorScript>().set_script<ScriptMoveToLeft>().active = SCRIPT;
+ box_2.add_component<BoxCollider>(vec2(1, 1), vec2(OFFSET_X_RIGHT, OFFSET_Y_RIGHT));
+ box_2.add_component<BehaviorScript>().set_script<ScriptBox>();
+
+ GameObject box_3 = this->new_object(
+ "box_3", "tag", ONTOP ? vec2(-0.25, 1.5) : vec2(-3, 1.5), 0, SCALE);
+ Asset box_3_asset = Asset("asset/texture/square.png");
+ box_3.add_component<Sprite>(box_3_asset,
+ Sprite::Data{
+ .sorting_in_layer = 0,
+ .order_in_layer = 0,
+ .size = vec2(1, 1),
+ .angle_offset = 0,
+ .scale_offset = 1,
+ .position_offset = vec2(OFFSET_X_LEFT, OFFSET_Y_LEFT),
+ });
+ box_3.add_component<Rigidbody>(Rigidbody::Data{
+ .gravity_scale = 0,
+ .body_type = BODYTYPE_LEFT,
+ .linear_velocity = SCRIPT ? vec2{0, 0} : vec2{1, 0},
+ .elasticity_coefficient = BOUNCE_LEFT ? 0.5 : 0,
+ .kinematic_collision = KINEMATIC_COLLISION,
+ });
+ box_3.add_component<BehaviorScript>().set_script<ScriptMoveToRight>().active = SCRIPT;
+ box_3.add_component<BoxCollider>(vec2(1, 1), vec2(OFFSET_X_LEFT, OFFSET_Y_LEFT));
+ box_3.add_component<BehaviorScript>().set_script<ScriptBox>();
+
+ GameObject box_4 = this->new_object("box_4", "tag",
+ ONTOP ? vec2(0.25, 1.5) : vec2(3, 1.5), 0, SCALE);
+ Asset box_4_asset = Asset("asset/texture/square.png");
+ box_4.add_component<Sprite>(
+ box_4_asset, Sprite::Data{
+ .sorting_in_layer = 0,
+ .order_in_layer = 0,
+ .size = vec2(1, 1),
+ .angle_offset = 0,
+ .scale_offset = 1,
+ .position_offset = vec2(OFFSET_X_RIGHT, OFFSET_Y_RIGHT),
+ });
+ box_4.add_component<Rigidbody>(Rigidbody::Data{
+ .gravity_scale = 0,
+ .body_type = BODYTYPE_RIGHT,
+ .linear_velocity = SCRIPT ? vec2{0, 0} : vec2{-1, 0},
+ .elasticity_coefficient = BOUNCE_RIGHT ? 0.5 : 0,
+ .kinematic_collision = KINEMATIC_COLLISION,
+ });
+ box_4.add_component<BehaviorScript>().set_script<ScriptMoveToLeft>().active = SCRIPT;
+ box_4.add_component<BoxCollider>(vec2(1, 1), vec2(OFFSET_X_RIGHT, OFFSET_Y_RIGHT));
+ box_4.add_component<BehaviorScript>().set_script<ScriptBox>();
+
+ GameObject circle_3 = this->new_object(
+ "ricle_3", "tag", ONTOP ? vec2(-0.25, 4) : vec2(-3.5, 4), 0, SCALE);
+ Asset circle_3_asset = Asset("asset/texture/circle.png");
+ circle_3.add_component<Sprite>(
+ circle_3_asset, Sprite::Data{
+ .sorting_in_layer = 0,
+ .order_in_layer = 0,
+ .size = vec2(1, 1),
+ .angle_offset = 0,
+ .scale_offset = 1,
+ .position_offset = vec2(OFFSET_X_LEFT, OFFSET_Y_LEFT),
+ });
+ circle_3.add_component<Rigidbody>(Rigidbody::Data{
+ .gravity_scale = 0,
+ .body_type = BODYTYPE_LEFT,
+ .linear_velocity = SCRIPT ? vec2{0, 0} : vec2{1, 0},
+ .elasticity_coefficient = BOUNCE_LEFT ? 0.5 : 0,
+ .kinematic_collision = KINEMATIC_COLLISION,
+ });
+ circle_3.add_component<BehaviorScript>().set_script<ScriptMoveToRight>().active
+ = SCRIPT;
+ circle_3.add_component<CircleCollider>(0.5, vec2(OFFSET_X_LEFT, OFFSET_Y_LEFT));
+ circle_3.add_component<BehaviorScript>().set_script<ScriptCircle>();
+
+ GameObject circle_4 = this->new_object("ricle_4", "tag",
+ ONTOP ? vec2(0.25, 4) : vec2(3.5, 4), 0, SCALE);
+ Asset circle_4_asset = Asset("asset/texture/circle.png");
+ circle_4.add_component<Sprite>(
+ circle_4_asset, Sprite::Data{
+ .sorting_in_layer = 0,
+ .order_in_layer = 0,
+ .size = vec2(1, 1),
+ .angle_offset = 0,
+ .scale_offset = 1,
+ .position_offset = vec2(OFFSET_X_RIGHT, OFFSET_Y_RIGHT),
+ });
+ circle_4.add_component<Rigidbody>(Rigidbody::Data{
+ .gravity_scale = 0,
+ .body_type = BODYTYPE_RIGHT,
+ .linear_velocity = SCRIPT ? vec2{0, 0} : vec2{-1, 0},
+ .elasticity_coefficient = BOUNCE_RIGHT ? 0.5 : 0,
+ .kinematic_collision = KINEMATIC_COLLISION,
+ });
+ circle_4.add_component<BehaviorScript>().set_script<ScriptMoveToLeft>().active
+ = SCRIPT;
+ circle_4.add_component<CircleCollider>(0.5, vec2(OFFSET_X_RIGHT, OFFSET_Y_RIGHT));
+ circle_4.add_component<BehaviorScript>().set_script<ScriptCircle>();
+ }
+
+ string get_name() const { return "scene1"; }
+};
+
int main(int argc, char * argv[]) {
Engine gameloop;
gameloop.add_scene<GameScene>();