From 338604322942d5e0ebc09d40c43c6f3a96b67947 Mon Sep 17 00:00:00 2001 From: max-001 Date: Fri, 13 Dec 2024 10:16:46 +0100 Subject: Added canera move script --- src/example/game.cpp | 42 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 8 deletions(-) diff --git a/src/example/game.cpp b/src/example/game.cpp index ec49742..dfa1e7e 100644 --- a/src/example/game.cpp +++ b/src/example/game.cpp @@ -1,3 +1,4 @@ +#include "api/BehaviorScript.h" #include "api/CircleCollider.h" #include "api/Scene.h" #include "manager/ComponentManager.h" @@ -19,12 +20,9 @@ using namespace crepe; using namespace std; -class Scene1 : public Scene { +class Background { public: - void load_scene() { - Mediator & m = this->mediator; - ComponentManager & mgr = m.component_manager; - + Background(ComponentManager & mgr) { GameObject start_begin = mgr.new_object("start_begin", "background", vec2(0, 0)); Asset start_begin_asset{"asset/jetpack_joyride/background/start/titleFG_1_TVOS.png"}; start_begin.add_component(start_begin_asset, Sprite::Data{ @@ -157,15 +155,43 @@ public: .order_in_layer = 1, .size = vec2(0, 800), }); + } +}; + +class MoveCameraScript : public Script { +public: + void init() { + subscribe( + [this](const KeyPressEvent & ev) -> bool { return this->keypressed(ev); }); + } + +private: + bool keypressed(const KeyPressEvent & event) { + if (event.key == Keycode::RIGHT) { + Transform & cam = this->get_components_by_name("camera").front(); + cam.position.x += 100; + } else if (event.key == Keycode::LEFT) { + Transform & cam = this->get_components_by_name("camera").front(); + cam.position.x -= 100; + } + return true; + } +}; + +class Scene1 : public Scene { +public: + void load_scene() { + Mediator & m = this->mediator; + ComponentManager & mgr = m.component_manager; + + Background background(mgr); GameObject camera = mgr.new_object("camera", "camera", vec2(600, 0)); camera.add_component(ivec2(1700, 720), vec2(2000, 800), Camera::Data{ .bg_color = Color::RED, }); - camera.add_component(Rigidbody::Data{ - .linear_velocity = vec2(1.5, 0), - }); + camera.add_component().set_script(); } string get_name() const { return "scene1"; } -- cgit v1.2.3