diff options
author | max-001 <maxsmits21@kpnmail.nl> | 2024-12-17 09:26:01 +0100 |
---|---|---|
committer | max-001 <maxsmits21@kpnmail.nl> | 2024-12-17 09:26:01 +0100 |
commit | 19d70481301e89481da64bc9bc59e1b81697c9e3 (patch) | |
tree | e05188b795edc90eb79c7f64bb3469a179741dfb /src/example/GameScene.cpp | |
parent | 5995e40f583ef281468866954f6f82c71ab43453 (diff) |
Moved Scene into seperate file
Diffstat (limited to 'src/example/GameScene.cpp')
-rw-r--r-- | src/example/GameScene.cpp | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/example/GameScene.cpp b/src/example/GameScene.cpp new file mode 100644 index 0000000..8a0ec5c --- /dev/null +++ b/src/example/GameScene.cpp @@ -0,0 +1,50 @@ +#include "GameScene.h" +#include "Background.h" + +#include <crepe/api/BehaviorScript.h> +#include <crepe/api/Camera.h> +#include <crepe/api/Color.h> +#include <crepe/api/Event.h> +#include <crepe/api/GameObject.h> +#include <crepe/api/Rigidbody.h> +#include <crepe/api/Script.h> +#include <crepe/types.h> + +using namespace crepe; +using namespace std; + +class MoveCameraScript : public Script { +public: + void init() { + subscribe<KeyPressEvent>( + [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<Transform>("camera").front(); + cam.position.x += 100; + } else if (event.key == Keycode::LEFT) { + Transform & cam = this->get_components_by_name<Transform>("camera").front(); + cam.position.x -= 100; + } + return true; + } +}; + +void GameScene::load_scene() { + Background background(*this); + + GameObject camera = new_object("camera", "camera", vec2(600, 0)); + camera.add_component<Camera>(ivec2(1700, 720), vec2(2000, 800), + Camera::Data{ + .bg_color = Color::RED, + }); + camera.add_component<BehaviorScript>().set_script<MoveCameraScript>(); + camera.add_component<Rigidbody>(Rigidbody::Data{ + .linear_velocity = vec2(100, 0), + }); +} + +string GameScene::get_name() const { return "scene1"; } |