From 19d70481301e89481da64bc9bc59e1b81697c9e3 Mon Sep 17 00:00:00 2001 From: max-001 Date: Tue, 17 Dec 2024 09:26:01 +0100 Subject: Moved Scene into seperate file --- src/example/CMakeLists.txt | 2 ++ src/example/GameScene.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++++ src/example/GameScene.h | 11 ++++++++++ src/example/game.cpp | 52 ++-------------------------------------------- 4 files changed, 65 insertions(+), 50 deletions(-) create mode 100644 src/example/GameScene.cpp create mode 100644 src/example/GameScene.h (limited to 'src') diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt index 911a99c..307c8da 100644 --- a/src/example/CMakeLists.txt +++ b/src/example/CMakeLists.txt @@ -4,6 +4,7 @@ set(GAME_HEADERS Forest.h Hallway.h Start.h + GameScene.h ) set(GAME_SOURCES @@ -13,6 +14,7 @@ set(GAME_SOURCES Forest.cpp Hallway.cpp Start.cpp + GameScene.cpp ) add_executable(game ${GAME_SOURCES} ${GAME_HEADERS}) 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 +#include +#include +#include +#include +#include +#include +#include + +using namespace crepe; +using namespace std; + +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; + } +}; + +void GameScene::load_scene() { + Background background(*this); + + GameObject camera = 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().set_script(); + camera.add_component(Rigidbody::Data{ + .linear_velocity = vec2(100, 0), + }); +} + +string GameScene::get_name() const { return "scene1"; } diff --git a/src/example/GameScene.h b/src/example/GameScene.h new file mode 100644 index 0000000..16e2919 --- /dev/null +++ b/src/example/GameScene.h @@ -0,0 +1,11 @@ +#pragma once + +#include +#include + +class GameScene : public crepe::Scene { +public: + void load_scene(); + + std::string get_name() const; +}; diff --git a/src/example/game.cpp b/src/example/game.cpp index 8f3996b..378a64a 100644 --- a/src/example/game.cpp +++ b/src/example/game.cpp @@ -1,61 +1,13 @@ -#include "Background.h" +#include "GameScene.h" -#include -#include -#include -#include -#include #include -#include -#include -#include -#include using namespace crepe; using namespace std; -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() { - Background background(*this); - - GameObject camera = 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().set_script(); - camera.add_component(Rigidbody::Data{ - .linear_velocity = vec2(100, 0), - }); - } - - string get_name() const { return "scene1"; } -}; - int main(int argc, char * argv[]) { LoopManager gameloop; - gameloop.add_scene(); + gameloop.add_scene(); gameloop.start(); return 0; } -- cgit v1.2.3