diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | src/crepe/api/Animator.cpp | 2 | ||||
-rw-r--r-- | src/crepe/api/Scene.h | 2 | ||||
-rw-r--r-- | src/example/AITest.cpp | 90 | ||||
-rw-r--r-- | src/example/Aquarium.cpp | 135 | ||||
-rw-r--r-- | src/example/Aquarium.h | 13 | ||||
-rw-r--r-- | src/example/Background.cpp | 37 | ||||
-rw-r--r-- | src/example/Background.h | 10 | ||||
-rw-r--r-- | src/example/CMakeLists.txt | 40 | ||||
-rw-r--r-- | src/example/Forest.cpp | 161 | ||||
-rw-r--r-- | src/example/Forest.h | 15 | ||||
-rw-r--r-- | src/example/GameScene.cpp | 130 | ||||
-rw-r--r-- | src/example/GameScene.h | 11 | ||||
-rw-r--r-- | src/example/Hallway.cpp | 128 | ||||
-rw-r--r-- | src/example/Hallway.h | 21 | ||||
-rw-r--r-- | src/example/Player.cpp | 78 | ||||
-rw-r--r-- | src/example/Player.h | 10 | ||||
-rw-r--r-- | src/example/Start.cpp | 388 | ||||
-rw-r--r-- | src/example/Start.h | 20 | ||||
-rw-r--r-- | src/example/button.cpp | 41 | ||||
-rw-r--r-- | src/example/game.cpp | 285 | ||||
-rw-r--r-- | src/example/rendering_particle.cpp | 73 |
22 files changed, 1184 insertions, 507 deletions
@@ -16,3 +16,4 @@ CTestTestfile.cmake _deps CMakeUserPresets.json compile.sh +asset/jetpack_joyride diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp index 4ce4bf0..5016e7e 100644 --- a/src/crepe/api/Animator.cpp +++ b/src/crepe/api/Animator.cpp @@ -52,6 +52,6 @@ void Animator::set_anim(int col) { void Animator::next_anim() { Animator::Data & ctx = this->data; - ctx.row = ctx.row++ % this->grid_size.x; + ctx.row = ++ctx.row % this->grid_size.x; this->spritesheet.mask.x = ctx.row * this->spritesheet.mask.w; } diff --git a/src/crepe/api/Scene.h b/src/crepe/api/Scene.h index dcca9d4..d552a43 100644 --- a/src/crepe/api/Scene.h +++ b/src/crepe/api/Scene.h @@ -60,7 +60,7 @@ private: OptionalRef<Mediator> mediator; //! \} -protected: +public: /** * \brief Retrieve the reference to the SaveManager instance * diff --git a/src/example/AITest.cpp b/src/example/AITest.cpp deleted file mode 100644 index 93ba500..0000000 --- a/src/example/AITest.cpp +++ /dev/null @@ -1,90 +0,0 @@ -#include <crepe/api/AI.h> -#include <crepe/api/BehaviorScript.h> -#include <crepe/api/Camera.h> -#include <crepe/api/Color.h> -#include <crepe/api/GameObject.h> -#include <crepe/api/LoopManager.h> -#include <crepe/api/Rigidbody.h> -#include <crepe/api/Scene.h> -#include <crepe/api/Script.h> -#include <crepe/api/Sprite.h> -#include <crepe/manager/Mediator.h> -#include <crepe/types.h> - -using namespace crepe; -using namespace std; - -class Script1 : public Script { - bool shutdown(const ShutDownEvent & event) { - // Very dirty way of shutting down the game - throw "ShutDownEvent"; - return true; - } - - bool mousemove(const MouseMoveEvent & event) { - /*RefVector<AI> aivec = this->get_components<AI>(); - AI & ai = aivec.front().get(); - ai.flee_target - = vec2{static_cast<float>(event.mouse_x), static_cast<float>(event.mouse_y)};*/ - return true; - } - - void init() { - subscribe<ShutDownEvent>( - [this](const ShutDownEvent & ev) -> bool { return this->shutdown(ev); }); - subscribe<MouseMoveEvent>( - [this](const MouseMoveEvent & ev) -> bool { return this->mousemove(ev); }); - } -}; - -class Scene1 : public Scene { -public: - void load_scene() override { - Mediator & mediator = this->mediator; - ComponentManager & mgr = mediator.component_manager; - - GameObject game_object1 = mgr.new_object("", "", vec2{0, 0}, 0, 1); - GameObject game_object2 = mgr.new_object("", "", vec2{0, 0}, 0, 1); - - Asset img{"asset/texture/test_ap43.png"}; - - Sprite & test_sprite = game_object1.add_component<Sprite>( - img, Sprite::Data{ - .color = Color::MAGENTA, - .flip = Sprite::FlipSettings{false, false}, - .sorting_in_layer = 2, - .order_in_layer = 2, - .size = {0, 100}, - .angle_offset = 0, - .position_offset = {0, 0}, - }); - - AI & ai = game_object1.add_component<AI>(3000); - // ai.arrive_on(); - // ai.flee_on(); - ai.path_follow_on(); - ai.make_oval_path(500, 1000, {0, -1000}, 1.5708, true); - ai.make_oval_path(1000, 500, {0, 500}, 4.7124, false); - game_object1.add_component<Rigidbody>(Rigidbody::Data{ - .mass = 0.1f, - .max_linear_velocity = 40, - }); - game_object1.add_component<BehaviorScript>().set_script<Script1>(); - - game_object2.add_component<Camera>(ivec2{1080, 720}, vec2{5000, 5000}, - Camera::Data{ - .bg_color = Color::WHITE, - .zoom = 1, - }); - } - - string get_name() const override { return "Scene1"; } -}; - -int main() { - LoopManager engine; - engine.add_scene<Scene1>(); - engine.start(); - - return 0; -} diff --git a/src/example/Aquarium.cpp b/src/example/Aquarium.cpp new file mode 100644 index 0000000..df44483 --- /dev/null +++ b/src/example/Aquarium.cpp @@ -0,0 +1,135 @@ +#include "Aquarium.h" + +#include <crepe/api/Animator.h> +#include <crepe/api/GameObject.h> +#include <crepe/api/Scene.h> +#include <crepe/api/Sprite.h> +#include <crepe/types.h> + +using namespace crepe; +using namespace std; + +float Aquarium::create(Scene & scn, float begin_x) { + this->add_background(scn, begin_x); + + GameObject aquarium_begin + = scn.new_object("aquarium_begin", "background", vec2(begin_x, 0)); + Asset aquarium_begin_asset{ + "asset/jetpack_joyride/background/aquarium/glassTubeFG_1_TVOS.png"}; + aquarium_begin.add_component<Sprite>(aquarium_begin_asset, Sprite::Data{ + .sorting_in_layer = 4, + .order_in_layer = 0, + .size = vec2(0, 800), + }); + begin_x += 600; + + GameObject aquarium_middle_1 + = scn.new_object("aquarium_middle", "background", vec2(begin_x, 0)); + Asset aquarium_middle_1_asset{ + "asset/jetpack_joyride/background/aquarium/glassTubeFG_3_TVOS.png"}; + aquarium_middle_1.add_component<Sprite>(aquarium_middle_1_asset, Sprite::Data{ + .sorting_in_layer = 4, + .order_in_layer = 2, + .size = vec2(0, 800), + }); + begin_x += 400; + + this->add_background(scn, begin_x - 200); + + GameObject aquarium_middle_2 + = scn.new_object("aquarium_middle", "background", vec2(begin_x, 0)); + Asset aquarium_middle_2_asset{ + "asset/jetpack_joyride/background/aquarium/glassTubeFG_3_TVOS.png"}; + aquarium_middle_2.add_component<Sprite>(aquarium_middle_2_asset, Sprite::Data{ + .sorting_in_layer = 4, + .order_in_layer = 3, + .size = vec2(0, 800), + }); + begin_x += 400; + + GameObject aquarium_middle_3 + = scn.new_object("aquarium_middle", "background", vec2(begin_x, 0)); + Asset aquarium_middle_3_asset{ + "asset/jetpack_joyride/background/aquarium/glassTubeFG_3_TVOS.png"}; + aquarium_middle_3.add_component<Sprite>(aquarium_middle_3_asset, Sprite::Data{ + .sorting_in_layer = 4, + .order_in_layer = 4, + .size = vec2(0, 800), + }); + begin_x += 400; + + this->add_background(scn, begin_x - 200); + + GameObject aquarium_middle_4 + = scn.new_object("aquarium_middle", "background", vec2(begin_x, 0)); + Asset aquarium_middle_4_asset{ + "asset/jetpack_joyride/background/aquarium/glassTubeFG_3_TVOS.png"}; + aquarium_middle_4.add_component<Sprite>(aquarium_middle_4_asset, Sprite::Data{ + .sorting_in_layer = 4, + .order_in_layer = 5, + .size = vec2(0, 800), + }); + begin_x += 600; + + this->add_background(scn, begin_x); + + GameObject aquarium_end = scn.new_object("aquarium_end", "background", vec2(begin_x, 0)); + Asset aquarium_end_asset{ + "asset/jetpack_joyride/background/aquarium/glassTubeFG_2_TVOS.png"}; + aquarium_end.add_component<Sprite>(aquarium_end_asset, Sprite::Data{ + .sorting_in_layer = 4, + .order_in_layer = 1, + .size = vec2(0, 800), + }); + begin_x += 600; + + return begin_x; +} + +void Aquarium::add_background(Scene & scn, float begin_x) { + GameObject bg_1 = scn.new_object("aquarium_bg_1", "aquarium_background", vec2(begin_x, 0)); + Asset bg_1_1_asset{"asset/jetpack_joyride/background/aquarium/AquariumBG1_1_TVOS.png"}; + bg_1.add_component<Sprite>(bg_1_1_asset, Sprite::Data{ + .sorting_in_layer = 3, + .order_in_layer = 2, + .size = vec2(0, 400), + .position_offset = vec2(-200, 100), + }); + Asset bg_1_2_asset{"asset/jetpack_joyride/background/aquarium/AquariumBG1_2_TVOS.png"}; + bg_1.add_component<Sprite>(bg_1_2_asset, Sprite::Data{ + .sorting_in_layer = 3, + .order_in_layer = 2, + .size = vec2(0, 400), + .position_offset = vec2(200, 100), + }); + GameObject bg_2 = scn.new_object("aquarium_bg_2", "aquarium_background", vec2(begin_x, 0)); + Asset bg_2_1_asset{"asset/jetpack_joyride/background/aquarium/AquariumBG2_1_TVOS.png"}; + bg_2.add_component<Sprite>(bg_2_1_asset, Sprite::Data{ + .sorting_in_layer = 3, + .order_in_layer = 1, + .size = vec2(0, 400), + .position_offset = vec2(200, -50), + }); + Asset bg_2_2_asset{"asset/jetpack_joyride/background/aquarium/AquariumBG2_2_TVOS.png"}; + bg_2.add_component<Sprite>(bg_2_2_asset, Sprite::Data{ + .sorting_in_layer = 3, + .order_in_layer = 1, + .size = vec2(0, 400), + .position_offset = vec2(-200, -50), + }); + GameObject bg_3 = scn.new_object("aquarium_bg_3", "aquarium_background", vec2(begin_x, 0)); + Asset bg_3_1_asset{"asset/jetpack_joyride/background/aquarium/AquariumBG3_1_TVOS.png"}; + bg_3.add_component<Sprite>(bg_3_1_asset, Sprite::Data{ + .sorting_in_layer = 3, + .order_in_layer = 0, + .size = vec2(0, 400), + .position_offset = vec2(200, -200), + }); + Asset bg_3_2_asset{"asset/jetpack_joyride/background/aquarium/AquariumBG3_2_TVOS.png"}; + bg_3.add_component<Sprite>(bg_3_2_asset, Sprite::Data{ + .sorting_in_layer = 3, + .order_in_layer = 0, + .size = vec2(0, 400), + .position_offset = vec2(-200, -200), + }); +} diff --git a/src/example/Aquarium.h b/src/example/Aquarium.h new file mode 100644 index 0000000..8815e9c --- /dev/null +++ b/src/example/Aquarium.h @@ -0,0 +1,13 @@ +#pragma once + +namespace crepe { +class Scene; +} + +class Aquarium { +public: + float create(crepe::Scene & scn, float begin_x); + +private: + void add_background(crepe::Scene & scn, float begin_x); +}; diff --git a/src/example/Background.cpp b/src/example/Background.cpp new file mode 100644 index 0000000..0ad2c0c --- /dev/null +++ b/src/example/Background.cpp @@ -0,0 +1,37 @@ +#include "Background.h" +#include "Aquarium.h" +#include "Forest.h" +#include "Hallway.h" +#include "Start.h" + +#include <crepe/api/Color.h> + +using namespace crepe; +using namespace std; + +Background::Background(Scene & scn) { + Start start; + Hallway hallway; + Forest forest; + Aquarium aquarium; + + float begin_x = 400; + + begin_x = start.create(scn, begin_x); + + begin_x = hallway.create(scn, begin_x, 1, Color::YELLOW); + + begin_x = forest.create(scn, begin_x, "1"); + + begin_x = hallway.create(scn, begin_x, 2, Color::MAGENTA); + + begin_x = aquarium.create(scn, begin_x); + + begin_x = hallway.create(scn, begin_x, 3, Color::CYAN); + + begin_x = forest.create(scn, begin_x, "2"); + + begin_x = hallway.create(scn, begin_x, 4, Color::GREEN); + + begin_x = aquarium.create(scn, begin_x); +} diff --git a/src/example/Background.h b/src/example/Background.h new file mode 100644 index 0000000..7c5a2bb --- /dev/null +++ b/src/example/Background.h @@ -0,0 +1,10 @@ +#pragma once + +namespace crepe { +class Scene; +} + +class Background { +public: + Background(crepe::Scene & scn); +}; diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt index 187ed46..ef2595f 100644 --- a/src/example/CMakeLists.txt +++ b/src/example/CMakeLists.txt @@ -1,22 +1,24 @@ -# all examples -add_custom_target(examples) +set(GAME_HEADERS + Aquarium.h + Background.h + Forest.h + Hallway.h + Start.h + GameScene.h + Player.h +) -# add_example(target_name [SOURCES...]) -function(add_example target_name) - # if SOURCES is not specified - if(NOT ARGV1) - # A .cpp file with target_name exists, and should be used - set(sources ${target_name}.cpp) - else() - set(sources ${ARGV}) - endif() +set(GAME_SOURCES + game.cpp + Aquarium.cpp + Background.cpp + Forest.cpp + Hallway.cpp + Start.cpp + GameScene.cpp + Player.cpp +) - add_executable(${target_name} EXCLUDE_FROM_ALL ${sources}) - target_link_libraries(${target_name} PUBLIC crepe) - add_dependencies(examples ${target_name}) -endfunction() +add_executable(game ${GAME_SOURCES} ${GAME_HEADERS}) -add_example(rendering_particle) -add_example(game) -add_example(button) -add_example(AITest) +target_link_libraries(game PUBLIC crepe) diff --git a/src/example/Forest.cpp b/src/example/Forest.cpp new file mode 100644 index 0000000..3c45e09 --- /dev/null +++ b/src/example/Forest.cpp @@ -0,0 +1,161 @@ +#include "Forest.h" + +#include <crepe/api/Animator.h> +#include <crepe/api/BehaviorScript.h> +#include <crepe/api/GameObject.h> +#include <crepe/api/Scene.h> +#include <crepe/api/Script.h> +#include <crepe/api/Sprite.h> +#include <crepe/types.h> + +using namespace crepe; +using namespace std; + +class ParallaxScript : public Script { +public: + ParallaxScript(float begin_x, float end_x, std::string unique_bg_name) + : begin_x(begin_x), + end_x(end_x), + name(unique_bg_name) {} + + void update() { + RefVector<Transform> vec_2 + = this->get_components_by_name<Transform>("forest_bg_2_" + name); + RefVector<Transform> vec_3 + = this->get_components_by_name<Transform>("forest_bg_3_" + name); + + for (Transform & t : vec_2) { + if (t.position.x > end_x - 400) { + t.position.x = begin_x - 400; + } + } + for (Transform & t : vec_3) { + if (t.position.x > end_x - 400) { + t.position.x = begin_x - 400; + } + } + } + +private: + const float begin_x; + const float end_x; + const std::string name; +}; + +float Forest::create(Scene & scn, float begin_x, std::string unique_bg_name) { + GameObject script = scn.new_object("forest_script", "background"); + script.add_component<BehaviorScript>().set_script<ParallaxScript>( + begin_x - 400, begin_x + 3000 + 400, unique_bg_name); + + this->add_background(scn, begin_x, unique_bg_name); + + GameObject begin = scn.new_object("forest_begin", "background", vec2(begin_x, 0)); + Asset begin_asset{"asset/jetpack_joyride/background/forest/forestFG_1_TVOS.png"}; + begin.add_component<Sprite>(begin_asset, Sprite::Data{ + .sorting_in_layer = 4, + .order_in_layer = 0, + .size = vec2(0, 800), + }); + begin_x += 800; + + this->add_background(scn, begin_x, unique_bg_name); + + GameObject middle_1 = scn.new_object("forest_middle", "background", vec2(begin_x, 0)); + Asset middle_1_asset{"asset/jetpack_joyride/background/forest/forestFG_3_TVOS.png"}; + middle_1.add_component<Sprite>(middle_1_asset, Sprite::Data{ + .sorting_in_layer = 4, + .order_in_layer = 2, + .size = vec2(0, 800), + }); + begin_x += 800; + + this->add_background(scn, begin_x, unique_bg_name); + + GameObject middle_2 = scn.new_object("forest_middle", "background", vec2(begin_x, 0)); + Asset middle_2_asset{"asset/jetpack_joyride/background/forest/forestFG_3_TVOS.png"}; + middle_2.add_component<Sprite>(middle_2_asset, Sprite::Data{ + .sorting_in_layer = 4, + .order_in_layer = 3, + .size = vec2(0, 800), + }); + begin_x += 800; + + this->add_background(scn, begin_x, unique_bg_name); + + GameObject end = scn.new_object("forest_end", "background", vec2(begin_x, 0)); + Asset end_asset{"asset/jetpack_joyride/background/forest/forestFG_2_TVOS.png"}; + end.add_component<Sprite>(end_asset, Sprite::Data{ + .sorting_in_layer = 4, + .order_in_layer = 1, + .size = vec2(0, 800), + }); + begin_x += 600; + + this->add_background(scn, begin_x + 200, unique_bg_name); + + return begin_x; +} + +void Forest::add_background(Scene & scn, float begin_x, std::string name) { + GameObject bg_1 + = scn.new_object("forest_bg_1_" + name, "forest_background", vec2(begin_x, 0)); + Asset bg_1_asset{"asset/jetpack_joyride/background/forest/forestBG1_1_TVOS.png"}; + bg_1.add_component<Sprite>(bg_1_asset, Sprite::Data{ + .sorting_in_layer = 3, + .order_in_layer = 2, + .size = vec2(0, 800), + }); + GameObject bg_2 + = scn.new_object("forest_bg_2_" + name, "forest_background", vec2(begin_x, 0)); + Asset bg_2_1_asset{"asset/jetpack_joyride/background/forest/forestBG2_1_TVOS.png"}; + bg_2.add_component<Sprite>(bg_2_1_asset, Sprite::Data{ + .sorting_in_layer = 3, + .order_in_layer = 1, + .size = vec2(0, 400), + .position_offset = vec2(200, 0), + }); + Asset bg_2_2_asset{"asset/jetpack_joyride/background/forest/forestBG2_2_TVOS.png"}; + bg_2.add_component<Sprite>(bg_2_2_asset, Sprite::Data{ + .sorting_in_layer = 3, + .order_in_layer = 1, + .size = vec2(0, 400), + .position_offset = vec2(-200, 0), + }); + GameObject bg_3 + = scn.new_object("forest_bg_3_" + name, "forest_background", vec2(begin_x, 0)); + Asset bg_3_1_asset{"asset/jetpack_joyride/background/forest/forestBG3_1_TVOS.png"}; + bg_3.add_component<Sprite>(bg_3_1_asset, Sprite::Data{ + .sorting_in_layer = 3, + .order_in_layer = 0, + .size = vec2(0, 200), + .position_offset = vec2(300, 0), + }); + Asset bg_3_2_asset{"asset/jetpack_joyride/background/forest/forestBG3_2_TVOS.png"}; + bg_3.add_component<Sprite>(bg_3_2_asset, Sprite::Data{ + .sorting_in_layer = 3, + .order_in_layer = 0, + .size = vec2(0, 200), + .position_offset = vec2(100, 0), + }); + Asset bg_3_3_asset{"asset/jetpack_joyride/background/forest/forestBG3_3_TVOS.png"}; + bg_3.add_component<Sprite>(bg_3_3_asset, Sprite::Data{ + .sorting_in_layer = 3, + .order_in_layer = 0, + .size = vec2(0, 200), + .position_offset = vec2(-100, 0), + }); + Asset bg_3_4_asset{"asset/jetpack_joyride/background/forest/forestBG3_4_TVOS.png"}; + bg_3.add_component<Sprite>(bg_3_4_asset, Sprite::Data{ + .sorting_in_layer = 3, + .order_in_layer = 0, + .size = vec2(0, 200), + .position_offset = vec2(-300, 0), + }); + + bg_2.add_component<Rigidbody>(Rigidbody::Data{ + .linear_velocity = vec2(30, 0), + }); + bg_3.add_component<Rigidbody>(Rigidbody::Data{ + .linear_velocity = vec2(40, 0), + }); +} diff --git a/src/example/Forest.h b/src/example/Forest.h new file mode 100644 index 0000000..f612c91 --- /dev/null +++ b/src/example/Forest.h @@ -0,0 +1,15 @@ +#pragma once + +#include <string> + +namespace crepe { +class Scene; +} + +class Forest { +public: + float create(crepe::Scene & scn, float begin_x, std::string unique_bg_name); + +private: + void add_background(crepe::Scene & scn, float begin_x, std::string name); +}; diff --git a/src/example/GameScene.cpp b/src/example/GameScene.cpp new file mode 100644 index 0000000..5ab4be0 --- /dev/null +++ b/src/example/GameScene.cpp @@ -0,0 +1,130 @@ +#include "GameScene.h" +#include "Background.h" +#include "Player.h" + +#include <cmath> +#include <crepe/api/Animator.h> +#include <crepe/api/Asset.h> +#include <crepe/api/BehaviorScript.h> +#include <crepe/api/BoxCollider.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/api/Sprite.h> +#include <crepe/api/Transform.h> +#include <crepe/types.h> +#include <iostream> + +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; + return true; + } else if (event.key == Keycode::LEFT) { + Transform & cam = this->get_components_by_name<Transform>("camera").front(); + cam.position.x -= 100; + return true; + } + return false; + } +}; + +class StartGameScript : public Script { +public: + void update() { + Transform & player_transform + = this->get_components_by_name<Transform>("player").front(); + + // Create hole in wall and activate panic lamp + if (player_transform.position.x > 75 && !this->created_hole) { + Sprite & lamp_sprite = this->get_components_by_name<Sprite>("start_end").back(); + lamp_sprite.active = true; + Sprite & hole_sprite = this->get_components_by_name<Sprite>("start_hole").front(); + hole_sprite.active = true; + + RefVector<Rigidbody> frags_rg + = this->get_components_by_tag<Rigidbody>("wall_fragment"); + RefVector<Sprite> frags_sprite + = this->get_components_by_tag<Sprite>("wall_fragment"); + for (Rigidbody & frag_rg : frags_rg) { + frag_rg.active = true; + } + for (Sprite & frag_sprite : frags_sprite) { + frag_sprite.active = true; + } + + this->created_hole = true; + } + + // Take jetpack from jetpack stand + if (player_transform.position.x > 275 && !this->took_jetpack) { + Animator & jetpack_stand_anim + = this->get_components_by_name<Animator>("start_begin").back(); + jetpack_stand_anim.next_anim(); + Sprite & jetpack_sprite = this->get_components_by_name<Sprite>("player").back(); + jetpack_sprite.active = true; + + this->took_jetpack = true; + } + + // Start camera movement, enable player jumping and disable this script + if (player_transform.position.x > 500) { + Rigidbody & rb = this->get_components_by_name<Rigidbody>("camera").front(); + rb.data.linear_velocity = vec2(100, 0); + BehaviorScript & player_script + = this->get_components_by_name<BehaviorScript>("player").front(); + player_script.active = true; + BehaviorScript & this_script + = this->get_components_by_name<BehaviorScript>("start_game_script").front(); + this_script.active = false; + } + } + +private: + bool created_hole = false; + bool took_jetpack = false; +}; + +void GameScene::load_scene() { + Background background(*this); + + GameObject camera = new_object("camera", "camera", vec2(650, 0)); + camera.add_component<Camera>(ivec2(990, 720), vec2(1100, 800), + Camera::Data{ + .bg_color = Color::RED, + }); + camera.add_component<BehaviorScript>().set_script<MoveCameraScript>(); + camera.add_component<Rigidbody>(Rigidbody::Data{}); + + Player player(*this); + + GameObject floor = new_object("floor", "game_world", vec2(0, 325)); + floor.add_component<Rigidbody>(Rigidbody::Data{ + .body_type = Rigidbody::BodyType::STATIC, + }); + floor.add_component<BoxCollider>(vec2(INFINITY, 200)); + GameObject ceiling = new_object("ceiling", "game_world", vec2(0, -325)); + ceiling.add_component<Rigidbody>(Rigidbody::Data{ + .body_type = Rigidbody::BodyType::STATIC, + }); + ceiling.add_component<BoxCollider>(vec2(INFINITY, 200)); + + GameObject start_game_script = new_object("start_game_script", "script", vec2(0, 0)); + start_game_script.add_component<BehaviorScript>().set_script<StartGameScript>(); +} + +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 <crepe/api/Scene.h> +#include <string> + +class GameScene : public crepe::Scene { +public: + void load_scene(); + + std::string get_name() const; +}; diff --git a/src/example/Hallway.cpp b/src/example/Hallway.cpp new file mode 100644 index 0000000..59d200f --- /dev/null +++ b/src/example/Hallway.cpp @@ -0,0 +1,128 @@ +#include "Hallway.h" + +#include <crepe/api/Animator.h> +#include <crepe/api/Color.h> +#include <crepe/api/GameObject.h> +#include <crepe/api/Scene.h> +#include <crepe/api/Sprite.h> + +using namespace crepe; +using namespace std; + +float Hallway::create(Scene & scn, float begin_x, unsigned int sector_num, + Color sector_color) { + GameObject begin = scn.new_object("hallway_begin", "background", vec2(begin_x, 0)); + Asset begin_asset{"asset/jetpack_joyride/background/hallway/hallway1FG_1_TVOS.png"}; + begin.add_component<Sprite>(begin_asset, Sprite::Data{ + .sorting_in_layer = 4, + .order_in_layer = 0, + .size = vec2(0, 800), + }); + begin_x += 600; + + this->add_sector_number(begin, vec2(-200, 0), sector_num, sector_color); + this->add_lamp(begin, vec2(330, -120), 11); + this->add_lamp(begin, vec2(430, -120), 9); + + GameObject middle_1 = scn.new_object("hallway_middle", "background", vec2(begin_x, 0)); + Asset middle_asset{"asset/jetpack_joyride/background/hallway/hallway1FG_2_TVOS.png"}; + middle_1.add_component<Sprite>(middle_asset, Sprite::Data{ + .sorting_in_layer = 4, + .order_in_layer = 2, + .size = vec2(0, 800), + }); + begin_x += 600; + + GameObject middle_2 = scn.new_object("hallway_middle", "background", vec2(begin_x, 0)); + Asset middle_asset_2{"asset/jetpack_joyride/background/hallway/hallway1FG_2_TVOS.png"}; + middle_2.add_component<Sprite>(middle_asset_2, Sprite::Data{ + .sorting_in_layer = 4, + .order_in_layer = 3, + .size = vec2(0, 800), + }); + begin_x += 200; + + GameObject middle_3 = scn.new_object("hallway_middle", "background", vec2(begin_x, 0)); + Asset middle_asset_3{"asset/jetpack_joyride/background/hallway/hallway1FG_2_TVOS.png"}; + middle_3.add_component<Sprite>(middle_asset_3, Sprite::Data{ + .sorting_in_layer = 4, + .order_in_layer = 4, + .size = vec2(0, 800), + }); + begin_x += 400; + + this->add_lamp(middle_3, vec2(0, -120)); + + GameObject middle_4 = scn.new_object("hallway_middle", "background", vec2(begin_x, 0)); + Asset middle_asset_4{"asset/jetpack_joyride/background/hallway/hallway1FG_2_TVOS.png"}; + middle_4.add_component<Sprite>(middle_asset_4, Sprite::Data{ + .sorting_in_layer = 4, + .order_in_layer = 5, + .size = vec2(0, 800), + }); + begin_x += 600; + + GameObject end = scn.new_object("hallway_end", "background", vec2(begin_x, 0)); + Asset end_asset{"asset/jetpack_joyride/background/hallway/hallway1FG_1_TVOS.png"}; + end.add_component<Sprite>(end_asset, Sprite::Data{ + .sorting_in_layer = 4, + .order_in_layer = 1, + .size = vec2(0, 800), + }); + begin_x += 600; + + return begin_x; +} + +void Hallway::add_lamp(GameObject & obj, vec2 offset, unsigned int fps) { + Asset lamp_asset{"asset/jetpack_joyride/background/hallway/alarmLight_TVOS.png"}; + obj.add_component<Sprite>(lamp_asset, Sprite::Data{ + .sorting_in_layer = 5, + .order_in_layer = 0, + .size = vec2(0, 100), + .position_offset = offset, + }); + Asset lamp_glow_asset{"asset/jetpack_joyride/background/hallway/alarmGlow_TVOS.png"}; + Sprite & lamp_glow_sprite = obj.add_component<Sprite>( + lamp_glow_asset, Sprite::Data{ + .sorting_in_layer = 5, + .order_in_layer = 1, + .size = vec2(0, 300), + .position_offset = offset - vec2(65, -30), + }); + obj.add_component<Animator>(lamp_glow_sprite, ivec2(422, 384), uvec2(6, 1), + Animator::Data{ + .fps = fps, + .looping = true, + }); +} + +void Hallway::add_sector_number(GameObject & obj, vec2 offset, unsigned int sector_num, + Color sector_color) { + Asset sector_text_asset{"asset/jetpack_joyride/background/hallway/sectorText_TVOS.png"}; + obj.add_component<Sprite>(sector_text_asset, Sprite::Data{ + .color = sector_color, + .sorting_in_layer = 5, + .order_in_layer = 0, + .size = vec2(0, 100), + .position_offset = offset, + }); + Asset sector_num_asset{"asset/jetpack_joyride/background/hallway/sectorNumbers_TVOS.png"}; + Sprite & sector_num_sprite = obj.add_component<Sprite>( + sector_num_asset, Sprite::Data{ + .color = sector_color, + .sorting_in_layer = 5, + .order_in_layer = 0, + .size = vec2(0, 100), + .position_offset = offset + vec2(200, 0), + }); + Animator & sector_num_anim = obj.add_component<Animator>( + sector_num_sprite, ivec2(256, 128), uvec2(4, 4), Animator::Data{}); + int column = (sector_num - 1) / 4; + int row = (sector_num - 1) % 4; + sector_num_anim.set_anim(column); + for (int i = 0; i < row; i++) { + sector_num_anim.next_anim(); + } + sector_num_anim.pause(); +} diff --git a/src/example/Hallway.h b/src/example/Hallway.h new file mode 100644 index 0000000..f173313 --- /dev/null +++ b/src/example/Hallway.h @@ -0,0 +1,21 @@ +#pragma once + +#include <crepe/types.h> + +namespace crepe { +class Scene; +class GameObject; +class Color; +} // namespace crepe + +class Hallway { +public: + float create(crepe::Scene & scn, float begin_x, unsigned int sector_num, + crepe::Color sector_color); + +private: + void add_lamp(crepe::GameObject & obj, crepe::vec2 offset, unsigned int fps = 10); + + void add_sector_number(crepe::GameObject & obj, crepe::vec2 offset, + unsigned int sector_num, crepe::Color sector_color); +}; diff --git a/src/example/Player.cpp b/src/example/Player.cpp new file mode 100644 index 0000000..a3126cf --- /dev/null +++ b/src/example/Player.cpp @@ -0,0 +1,78 @@ +#include "Player.h" + +#include <crepe/api/Animator.h> +#include <crepe/api/GameObject.h> +#include <crepe/api/Scene.h> +#include <crepe/api/Script.h> +#include <crepe/api/Sprite.h> + +using namespace crepe; +using namespace std; + +class PlayerScript : 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::SPACE) { + Rigidbody & rb = this->get_components_by_name<Rigidbody>("player").front(); + rb.add_force_linear(vec2(0, -10)); + return true; + } + return false; + } +}; + +Player::Player(Scene & scn) { + GameObject player = scn.new_object("player", "player", vec2(-100, 200)); + Asset player_body_asset{"asset/jetpack_joyride/barry/defaultBody.png"}; + Sprite & player_body_sprite + = player.add_component<Sprite>(player_body_asset, Sprite::Data{ + .sorting_in_layer = 10, + .order_in_layer = 0, + .size = vec2(0, 50), + }); + player.add_component<Animator>(player_body_sprite, ivec2(32, 32), uvec2(4, 8), + Animator::Data{ + .fps = 5, + .looping = true, + }); + Asset player_head_asset{"asset/jetpack_joyride/barry/defaultHead.png"}; + Sprite & player_head_sprite + = player.add_component<Sprite>(player_head_asset, Sprite::Data{ + .sorting_in_layer = 10, + .order_in_layer = 1, + .size = vec2(0, 50), + .position_offset = vec2(0, -20), + }); + player.add_component<Animator>(player_head_sprite, ivec2(32, 32), uvec2(4, 8), + Animator::Data{ + .fps = 5, + .looping = true, + }); + Asset player_jetpack_asset{"asset/jetpack_joyride/barry/jetpackDefault.png"}; + Sprite & player_jetpack_sprite = player.add_component<Sprite>( + player_jetpack_asset, Sprite::Data{ + .sorting_in_layer = 10, + .order_in_layer = 2, + .size = vec2(0, 60), + .position_offset = vec2(-20, 0), + }); + player_jetpack_sprite.active = false; + player.add_component<Animator>(player_jetpack_sprite, ivec2(32, 44), uvec2(4, 4), + Animator::Data{ + .fps = 5, + .looping = true, + }); + player.add_component<Rigidbody>(Rigidbody::Data{ + .gravity_scale = 10, + .body_type = Rigidbody::BodyType::DYNAMIC, + .linear_velocity = vec2(100, 0), + }); + player.add_component<BoxCollider>(vec2(50, 50)); + player.add_component<BehaviorScript>().set_script<PlayerScript>().active = false; +} diff --git a/src/example/Player.h b/src/example/Player.h new file mode 100644 index 0000000..139f6e4 --- /dev/null +++ b/src/example/Player.h @@ -0,0 +1,10 @@ +#pragma once + +namespace crepe { +class Scene; +} + +class Player { +public: + Player(crepe::Scene & scn); +}; diff --git a/src/example/Start.cpp b/src/example/Start.cpp new file mode 100644 index 0000000..f710a3a --- /dev/null +++ b/src/example/Start.cpp @@ -0,0 +1,388 @@ +#include "Start.h" +#include "api/Asset.h" +#include "api/CircleCollider.h" +#include "api/Rigidbody.h" +#include "types.h" + +#include <crepe/api/Animator.h> +#include <crepe/api/GameObject.h> +#include <crepe/api/Scene.h> +#include <crepe/api/Sprite.h> + +using namespace crepe; +using namespace std; + +float Start::create(Scene & scn, float begin_x) { + this->create_wall_fragments(scn, begin_x - 300); + + GameObject begin = scn.new_object("start_begin", "background", vec2(begin_x, 0)); + Asset begin_asset{"asset/jetpack_joyride/background/start/titleFG_1_TVOS.png"}; + begin.add_component<Sprite>(begin_asset, Sprite::Data{ + .sorting_in_layer = 4, + .order_in_layer = 0, + .size = vec2(0, 800), + }); + GameObject hole = scn.new_object("start_hole", "background", vec2(begin_x - 250, 140)); + Asset hole_asset{"asset/jetpack_joyride/background/start/titleWallHole.png"}; + Sprite & hole_sprite = hole.add_component<Sprite>(hole_asset, Sprite::Data{ + .sorting_in_layer = 4, + .order_in_layer = 1, + .size = vec2(0, 200), + }); + hole_sprite.active = false; + begin_x += 700; + + this->add_table(begin, vec2(-150, 150)); + this->add_light(begin, vec2(-125, -150)); + this->add_jetpack_stand(begin, vec2(-125, 200)); + + GameObject end = scn.new_object("start_end", "background", vec2(begin_x, 0)); + Asset end_asset{"asset/jetpack_joyride/background/start/titleFG_2_TVOS.png"}; + end.add_component<Sprite>(end_asset, Sprite::Data{ + .sorting_in_layer = 4, + .order_in_layer = 1, + .size = vec2(0, 800), + }); + begin_x += 100; + + this->add_lamp(end, vec2(-350, -95)); + + return begin_x; +} + +void Start::add_lamp(GameObject & obj, vec2 offset, unsigned int fps) { + Asset lamp_asset{"asset/jetpack_joyride/background/start/alarmLight_TVOS.png"}; + obj.add_component<Sprite>(lamp_asset, Sprite::Data{ + .sorting_in_layer = 5, + .order_in_layer = 0, + .size = vec2(0, 100), + .position_offset = offset, + }); + Asset lamp_glow_asset{"asset/jetpack_joyride/background/start/alarmGlow_TVOS.png"}; + Sprite & lamp_glow_sprite = obj.add_component<Sprite>( + lamp_glow_asset, Sprite::Data{ + .sorting_in_layer = 5, + .order_in_layer = 1, + .size = vec2(0, 300), + .position_offset = offset - vec2(65, -55), + }); + lamp_glow_sprite.active = false; + obj.add_component<Animator>(lamp_glow_sprite, ivec2(422, 384), uvec2(6, 1), + Animator::Data{ + .fps = fps, + .looping = true, + }); +} + +void Start::add_table(GameObject & obj, vec2 offset) { + Asset table_asset{"asset/jetpack_joyride/background/start/table.png"}; + obj.add_component<Sprite>(table_asset, Sprite::Data{ + .sorting_in_layer = 5, + .order_in_layer = 0, + .size = vec2(0, 100), + .position_offset = offset, + }); + Asset gramophone_asset{"asset/jetpack_joyride/background/start/gramophone_TVOS.png"}; + Sprite & gramophone_sprite = obj.add_component<Sprite>( + gramophone_asset, Sprite::Data{ + .sorting_in_layer = 5, + .order_in_layer = 1, + .size = vec2(0, 100), + .position_offset = offset + vec2(0, -50), + }); + obj.add_component<Animator>(gramophone_sprite, ivec2(64, 128), uvec2(2, 1), + Animator::Data{ + .fps = 10, + .looping = true, + }); +} + +void Start::add_light(crepe::GameObject & obj, crepe::vec2 offset) { + Asset light_asset{"asset/jetpack_joyride/background/start/title_light_TVOS.png"}; + obj.add_component<Sprite>(light_asset, Sprite::Data{ + .sorting_in_layer = 5, + .order_in_layer = 0, + .size = vec2(0, 200), + .position_offset = offset, + }); + Asset light_glow_asset{"asset/jetpack_joyride/background/start/lightEffect2.png"}; + obj.add_component<Sprite>(light_glow_asset, Sprite::Data{ + .sorting_in_layer = 5, + .order_in_layer = 1, + .size = vec2(0, 100), + .position_offset = offset + vec2(0, 75), + }); + Asset light_effect_asset{"asset/jetpack_joyride/background/start/lightEffect.png"}; + obj.add_component<Sprite>(light_effect_asset, Sprite::Data{ + .sorting_in_layer = 5, + .order_in_layer = 0, + .size = vec2(0, 100), + .position_offset = offset + vec2(0, 350), + }); +} + +void Start::add_jetpack_stand(crepe::GameObject & obj, crepe::vec2 offset) { + Asset jetpack_stand_asset{"asset/jetpack_joyride/background/start/JetpackStand.png"}; + Sprite & jetpeck_stand_sprite + = obj.add_component<Sprite>(jetpack_stand_asset, Sprite::Data{ + .sorting_in_layer = 5, + .order_in_layer = 1, + .size = vec2(0, 70), + .position_offset = offset, + }); + obj.add_component<Animator>(jetpeck_stand_sprite, ivec2(34, 46), uvec2(2, 1), + Animator::Data{ + .fps = 10, + .looping = true, + }) + .pause(); + Asset do_not_steal = {"asset/jetpack_joyride/background/start/doNotTouchSign_TVOS.png"}; + obj.add_component<Sprite>(do_not_steal, Sprite::Data{ + .sorting_in_layer = 5, + .order_in_layer = 1, + .size = vec2(0, 100), + .position_offset = offset + vec2(-75, -25), + }); +} + +void Start::create_wall_fragments(crepe::Scene & scn, float begin_x) { + GameObject frag_1 = scn.new_object("frag_1", "wall_fragment", vec2(begin_x, 200)); + Asset frag_1_asset{"asset/jetpack_joyride/background/start/StartWall_frag1.png"}; + Sprite & frag_1_sprite + = frag_1.add_component<Sprite>(frag_1_asset, Sprite::Data{ + .sorting_in_layer = 5, + .order_in_layer = 5, + .size = vec2(0, 50), + }); + frag_1_sprite.active = false; + Rigidbody & frag_1_rb = frag_1.add_component<Rigidbody>(Rigidbody::Data{ + .gravity_scale = 10, + .linear_velocity = vec2(400, 400), + .linear_velocity_coefficient = vec2(0.5, 0.6), + .angular_velocity = 500, + .angular_velocity_coefficient = 0.55, + .elastisity_coefficient = 0.5, + }); + frag_1_rb.active = false; + frag_1.add_component<CircleCollider>(25); + + GameObject frag_2 = scn.new_object("frag_2", "wall_fragment", vec2(begin_x, 180)); + Asset frag_2_asset{"asset/jetpack_joyride/background/start/StartWall_frag2.png"}; + Sprite & frag_2_sprite + = frag_2.add_component<Sprite>(frag_2_asset, Sprite::Data{ + .sorting_in_layer = 5, + .order_in_layer = 5, + .size = vec2(0, 50), + }); + frag_2_sprite.active = false; + Rigidbody & frag_2_rb = frag_2.add_component<Rigidbody>(Rigidbody::Data{ + .gravity_scale = 20, + .linear_velocity = vec2(400, 400), + .linear_velocity_coefficient = vec2(0.35, 0.4), + .angular_velocity = 400, + .angular_velocity_coefficient = 0.55, + .elastisity_coefficient = 0.5, + }); + frag_2_rb.active = false; + frag_2.add_component<CircleCollider>(55); + + GameObject frag_3 = scn.new_object("frag_3", "wall_fragment", vec2(begin_x, 170)); + Asset frag_3_asset{"asset/jetpack_joyride/background/start/StartWall_frag3.png"}; + Sprite & frag_3_sprite + = frag_3.add_component<Sprite>(frag_3_asset, Sprite::Data{ + .sorting_in_layer = 5, + .order_in_layer = 5, + .size = vec2(0, 50), + }); + frag_3_sprite.active = false; + Rigidbody & frag_3_rb = frag_3.add_component<Rigidbody>(Rigidbody::Data{ + .gravity_scale = 30, + .linear_velocity = vec2(400, 400), + .linear_velocity_coefficient = vec2(0.3, 0.3), + .angular_velocity = 300, + .angular_velocity_coefficient = 0.55, + .elastisity_coefficient = 0.5, + }); + frag_3_rb.active = false; + frag_3.add_component<CircleCollider>(35); + + GameObject frag_4 = scn.new_object("frag_4", "wall_fragment", vec2(begin_x, 160)); + Asset frag_4_asset{"asset/jetpack_joyride/background/start/StartWall_frag4.png"}; + Sprite & frag_4_sprite + = frag_4.add_component<Sprite>(frag_4_asset, Sprite::Data{ + .sorting_in_layer = 5, + .order_in_layer = 5, + .size = vec2(0, 50), + }); + frag_4_sprite.active = false; + Rigidbody & frag_4_rb = frag_4.add_component<Rigidbody>(Rigidbody::Data{ + .gravity_scale = 40, + .linear_velocity = vec2(700, 400), + .linear_velocity_coefficient = vec2(0.2, 0.2), + .angular_velocity = 200, + .angular_velocity_coefficient = 0.55, + .elastisity_coefficient = 0.5, + }); + frag_4_rb.active = false; + frag_4.add_component<CircleCollider>(60); + + GameObject frag_5 = scn.new_object("frag_5", "wall_fragment", vec2(begin_x, 150)); + Asset frag_5_asset{"asset/jetpack_joyride/background/start/StartWall_frag5.png"}; + Sprite & frag_5_sprite + = frag_5.add_component<Sprite>(frag_5_asset, Sprite::Data{ + .sorting_in_layer = 5, + .order_in_layer = 5, + .size = vec2(0, 50), + }); + frag_5_sprite.active = false; + Rigidbody & frag_5_rb = frag_5.add_component<Rigidbody>(Rigidbody::Data{ + .gravity_scale = 50, + .linear_velocity = vec2(600, 800), + .linear_velocity_coefficient = vec2(0.25, 0.15), + .angular_velocity = 100, + .angular_velocity_coefficient = 0.55, + .elastisity_coefficient = 0.5, + }); + frag_5_rb.active = false; + frag_5.add_component<CircleCollider>(5); + + GameObject frag_6 = scn.new_object("frag_6", "wall_fragment", vec2(begin_x, 140)); + Asset frag_6_asset{"asset/jetpack_joyride/background/start/StartWall_frag6.png"}; + Sprite & frag_6_sprite + = frag_6.add_component<Sprite>(frag_6_asset, Sprite::Data{ + .sorting_in_layer = 5, + .order_in_layer = 5, + .size = vec2(0, 50), + }); + frag_6_sprite.active = false; + Rigidbody & frag_6_rb = frag_6.add_component<Rigidbody>(Rigidbody::Data{ + .gravity_scale = 30, + .linear_velocity = vec2(300, 800), + .linear_velocity_coefficient = vec2(0.35, 0.25), + .angular_velocity = 100, + .angular_velocity_coefficient = 0.55, + .elastisity_coefficient = 0.5, + }); + frag_6_rb.active = false; + frag_6.add_component<CircleCollider>(30); + + GameObject frag_7 = scn.new_object("frag_7", "wall_fragment", vec2(begin_x, 130)); + Asset frag_7_asset{"asset/jetpack_joyride/background/start/StartWall_frag7.png"}; + Sprite & frag_7_sprite + = frag_7.add_component<Sprite>(frag_7_asset, Sprite::Data{ + .sorting_in_layer = 5, + .order_in_layer = 5, + .size = vec2(0, 50), + }); + frag_7_sprite.active = false; + Rigidbody & frag_7_rb = frag_7.add_component<Rigidbody>(Rigidbody::Data{ + .gravity_scale = 20, + .linear_velocity = vec2(400, 500), + .linear_velocity_coefficient = vec2(0.45, 0.6), + .angular_velocity = 800, + .angular_velocity_coefficient = 0.55, + .elastisity_coefficient = 0.5, + }); + frag_7_rb.active = false; + frag_7.add_component<CircleCollider>(45); + + GameObject frag_8 = scn.new_object("frag_8", "wall_fragment", vec2(begin_x, 120)); + Asset frag_8_asset{"asset/jetpack_joyride/background/start/StartWall_frag8.png"}; + Sprite & frag_8_sprite + = frag_8.add_component<Sprite>(frag_8_asset, Sprite::Data{ + .sorting_in_layer = 5, + .order_in_layer = 5, + .size = vec2(0, 50), + }); + frag_8_sprite.active = false; + Rigidbody & frag_8_rb = frag_8.add_component<Rigidbody>(Rigidbody::Data{ + .gravity_scale = 30, + .linear_velocity = vec2(400, 400), + .linear_velocity_coefficient = vec2(0.5, 0.6), + .angular_velocity = 500, + .angular_velocity_coefficient = 0.55, + .elastisity_coefficient = 0.5, + }); + frag_8_rb.active = false; + frag_8.add_component<CircleCollider>(25); + + GameObject frag_9 = scn.new_object("frag_9", "wall_fragment", vec2(begin_x, 110)); + Asset frag_9_asset{"asset/jetpack_joyride/background/start/StartWall_frag9.png"}; + Sprite & frag_9_sprite + = frag_9.add_component<Sprite>(frag_9_asset, Sprite::Data{ + .sorting_in_layer = 5, + .order_in_layer = 5, + .size = vec2(0, 50), + }); + frag_9_sprite.active = false; + Rigidbody & frag_9_rb = frag_9.add_component<Rigidbody>(Rigidbody::Data{ + .gravity_scale = 40, + .linear_velocity = vec2(200, 400), + .linear_velocity_coefficient = vec2(0.5, 0.25), + .angular_velocity = 500, + .angular_velocity_coefficient = 0.55, + .elastisity_coefficient = 0.5, + }); + frag_9_rb.active = false; + frag_9.add_component<CircleCollider>(15); + + GameObject frag_10 = scn.new_object("frag_10", "wall_fragment", vec2(begin_x, 100)); + Asset frag_10_asset{"asset/jetpack_joyride/background/start/StartWall_frag10.png"}; + Sprite & frag_10_sprite + = frag_10.add_component<Sprite>(frag_10_asset, Sprite::Data{ + .sorting_in_layer = 5, + .order_in_layer = 5, + .size = vec2(0, 50), + }); + frag_10_sprite.active = false; + Rigidbody & frag_10_rb = frag_10.add_component<Rigidbody>(Rigidbody::Data{ + .gravity_scale = 50, + .linear_velocity = vec2(400, 900), + .linear_velocity_coefficient = vec2(0.35, 0.4), + .angular_velocity = 300, + .angular_velocity_coefficient = 0.55, + .elastisity_coefficient = 0.5, + }); + frag_10_rb.active = false; + frag_10.add_component<CircleCollider>(60); + + GameObject frag_11 = scn.new_object("frag_11", "wall_fragment", vec2(begin_x, 90)); + Asset frag_11_asset{"asset/jetpack_joyride/background/start/StartWall_frag11.png"}; + Sprite & frag_11_sprite + = frag_11.add_component<Sprite>(frag_11_asset, Sprite::Data{ + .sorting_in_layer = 5, + .order_in_layer = 5, + .size = vec2(0, 50), + }); + frag_11_sprite.active = false; + Rigidbody & frag_11_rb = frag_11.add_component<Rigidbody>(Rigidbody::Data{ + .gravity_scale = 60, + .linear_velocity = vec2(600, 800), + .linear_velocity_coefficient = vec2(0.3, 0.3), + .angular_velocity = 200, + .angular_velocity_coefficient = 0.55, + .elastisity_coefficient = 0.5, + }); + frag_11_rb.active = false; + frag_11.add_component<CircleCollider>(5); + + GameObject frag_12 = scn.new_object("frag_12", "wall_fragment", vec2(begin_x, 80)); + Asset frag_12_asset{"asset/jetpack_joyride/background/start/StartWall_frag12.png"}; + Sprite & frag_12_sprite + = frag_12.add_component<Sprite>(frag_12_asset, Sprite::Data{ + .sorting_in_layer = 5, + .order_in_layer = 5, + .size = vec2(0, 50), + }); + frag_12_sprite.active = false; + Rigidbody & frag_12_rb = frag_12.add_component<Rigidbody>(Rigidbody::Data{ + .gravity_scale = 70, + .linear_velocity = vec2(500, 800), + .linear_velocity_coefficient = vec2(0.25, 0.15), + .angular_velocity = 100, + .angular_velocity_coefficient = 0.55, + .elastisity_coefficient = 0.5, + }); + frag_12_rb.active = false; + frag_12.add_component<CircleCollider>(50); +} diff --git a/src/example/Start.h b/src/example/Start.h new file mode 100644 index 0000000..24e0dbf --- /dev/null +++ b/src/example/Start.h @@ -0,0 +1,20 @@ +#pragma once + +#include <crepe/types.h> + +namespace crepe { +class Scene; +class GameObject; +} // namespace crepe + +class Start { +public: + float create(crepe::Scene & scn, float begin_x); + +private: + void add_lamp(crepe::GameObject & obj, crepe::vec2 offset, unsigned int fps = 10); + void add_table(crepe::GameObject & obj, crepe::vec2 offset); + void add_light(crepe::GameObject & obj, crepe::vec2 offset); + void add_jetpack_stand(crepe::GameObject & obj, crepe::vec2 offset); + void create_wall_fragments(crepe::Scene & scn, float begin_x); +}; diff --git a/src/example/button.cpp b/src/example/button.cpp deleted file mode 100644 index 4220588..0000000 --- a/src/example/button.cpp +++ /dev/null @@ -1,41 +0,0 @@ -#include <SDL2/SDL_timer.h> -#include <chrono> -#include <crepe/Component.h> -#include <crepe/api/Animator.h> -#include <crepe/api/Button.h> -#include <crepe/api/Camera.h> -#include <crepe/api/Color.h> -#include <crepe/api/GameObject.h> -#include <crepe/api/Sprite.h> -#include <crepe/api/Texture.h> -#include <crepe/api/Transform.h> -#include <crepe/facade/SDLContext.h> -#include <crepe/manager/ComponentManager.h> -#include <crepe/manager/EventManager.h> -#include <crepe/system/AnimatorSystem.h> -#include <crepe/system/InputSystem.h> -#include <crepe/system/RenderSystem.h> -#include <crepe/types.h> -using namespace crepe; -using namespace std; - -int main(int argc, char * argv[]) { - Mediator mediator; - ComponentManager mgr{mediator}; - RenderSystem sys{mediator}; - EventManager event_mgr{mediator}; - InputSystem input_sys{mediator}; - SDLContext sdl_context{mediator}; - GameObject obj = mgr.new_object("camera", "camera", vec2{0, 0}, 0, 1); - auto & camera = obj.add_component<Camera>( - ivec2{500, 500}, vec2{500, 500}, Camera::Data{.bg_color = Color::WHITE, .zoom = 1.0f}); - auto start = std::chrono::steady_clock::now(); - while (true) { - const keyboard_state_t & keyboard_state = sdl_context.get_keyboard_state(); - input_sys.update(); - sys.update(); - event_mgr.dispatch_events(); - SDL_Delay(30); - } - return 0; -} diff --git a/src/example/game.cpp b/src/example/game.cpp index 22effd2..378a64a 100644 --- a/src/example/game.cpp +++ b/src/example/game.cpp @@ -1,292 +1,13 @@ -#include "api/CircleCollider.h" -#include "api/ParticleEmitter.h" -#include "api/Scene.h" -#include "manager/ComponentManager.h" -#include "manager/Mediator.h" -#include "types.h" -#include <crepe/api/BoxCollider.h> -#include <crepe/api/Camera.h> -#include <crepe/api/Color.h> -#include <crepe/api/Event.h> -#include <crepe/api/GameObject.h> +#include "GameScene.h" + #include <crepe/api/LoopManager.h> -#include <crepe/api/Rigidbody.h> -#include <crepe/api/Script.h> -#include <crepe/api/Sprite.h> -#include <crepe/api/Transform.h> -#include <crepe/api/Vector2.h> using namespace crepe; - using namespace std; -class MyScript1 : public Script { - bool flip = false; - bool oncollision(const CollisionEvent & test) { - Log::logf("Box {} script on_collision()", test.info.this_collider.game_object_id); - return true; - } - bool keypressed(const KeyPressEvent & test) { - Log::logf("Box script keypressed()"); - switch (test.key) { - case Keycode::A: { - Rigidbody & tf = this->get_component<Rigidbody>(); - tf.data.linear_velocity.x -= 1; - break; - } - case Keycode::W: { - Rigidbody & tf = this->get_component<Rigidbody>(); - // tf.data.linear_velocity.y -= 1; - tf.add_force_linear({0, -1}); - break; - } - case Keycode::S: { - Rigidbody & tf = this->get_component<Rigidbody>(); - tf.data.linear_velocity.y += 1; - break; - } - case Keycode::D: { - Rigidbody & tf = this->get_component<Rigidbody>(); - tf.data.linear_velocity.x += 1; - break; - } - case Keycode::E: { - if (flip) { - flip = false; - this->get_component<BoxCollider>().active = true; - this->get_components<Sprite>()[0].get().active = true; - this->get_component<CircleCollider>().active = false; - this->get_components<Sprite>()[1].get().active = false; - } else { - flip = true; - this->get_component<BoxCollider>().active = false; - this->get_components<Sprite>()[0].get().active = false; - this->get_component<CircleCollider>().active = true; - this->get_components<Sprite>()[1].get().active = true; - } - - //add collider switch - break; - } - case Keycode::Q: { - Rigidbody & rg = this->get_component<Rigidbody>(); - rg.data.angular_velocity = 1; - break; - } - default: - break; - } - return false; - } - - void init() { - Log::logf("init"); - subscribe<CollisionEvent>( - [this](const CollisionEvent & ev) -> bool { return this->oncollision(ev); }); - subscribe<KeyPressEvent>( - [this](const KeyPressEvent & ev) -> bool { return this->keypressed(ev); }); - } - void update() { - Rigidbody & tf = this->get_component<Rigidbody>(); - Log::logf("linear_velocity.x {}", tf.data.linear_velocity.x); - Log::logf("linear_velocity.y {}", tf.data.linear_velocity.y); - // tf.data.linear_velocity = {0,0}; - } -}; - -class MyScript2 : public Script { - bool flip = false; - bool oncollision(const CollisionEvent & test) { - Log::logf("Box {} script on_collision()", test.info.this_collider.game_object_id); - return true; - } - bool keypressed(const KeyPressEvent & test) { - Log::logf("Box script keypressed()"); - switch (test.key) { - case Keycode::LEFT: { - Transform & tf = this->get_component<Transform>(); - tf.position.x -= 1; - break; - } - case Keycode::UP: { - Transform & tf = this->get_component<Transform>(); - tf.position.y -= 1; - break; - } - case Keycode::DOWN: { - Transform & tf = this->get_component<Transform>(); - tf.position.y += 1; - break; - } - case Keycode::RIGHT: { - Transform & tf = this->get_component<Transform>(); - tf.position.x += 1; - break; - } - case Keycode::PAUSE: { - if (flip) { - flip = false; - this->get_component<BoxCollider>().active = true; - this->get_components<Sprite>()[0].get().active = true; - this->get_component<CircleCollider>().active = false; - this->get_components<Sprite>()[1].get().active = false; - } else { - flip = true; - this->get_component<BoxCollider>().active = false; - this->get_components<Sprite>()[0].get().active = false; - this->get_component<CircleCollider>().active = true; - this->get_components<Sprite>()[1].get().active = true; - } - - //add collider switch - break; - } - default: - break; - } - return false; - } - - void init() { - Log::logf("init"); - subscribe<CollisionEvent>( - [this](const CollisionEvent & ev) -> bool { return this->oncollision(ev); }); - subscribe<KeyPressEvent>( - [this](const KeyPressEvent & ev) -> bool { return this->keypressed(ev); }); - } - void update() { - // Retrieve component from the same GameObject this script is on - } -}; - -class ConcreteScene1 : public Scene { -public: - using Scene::Scene; - - void load_scene() { - - Color color(0, 0, 0, 255); - - float screen_size_width = 320; - float screen_size_height = 240; - float world_collider = 1000; - //define playable world - GameObject world = new_object( - "Name", "Tag", vec2{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, - .offset = {0, 0}, - }); - world.add_component<BoxCollider>( - vec2{world_collider, world_collider}, - vec2{0, 0 - (screen_size_height / 2 + world_collider / 2)}); // Top - world.add_component<BoxCollider>( - vec2{world_collider, world_collider}, - vec2{0, screen_size_height / 2 + world_collider / 2}); // Bottom - world.add_component<BoxCollider>( - vec2{world_collider, world_collider}, - vec2{0 - (screen_size_width / 2 + world_collider / 2), 0}); // Left - world.add_component<BoxCollider>( - vec2{world_collider, world_collider}, - vec2{screen_size_width / 2 + world_collider / 2, 0}); // right - world.add_component<Camera>( - ivec2{static_cast<int>(screen_size_width), static_cast<int>(screen_size_height)}, - vec2{screen_size_width, screen_size_height}, - Camera::Data{ - .bg_color = Color::WHITE, - .zoom = 1, - }); - - GameObject game_object1 = new_object( - "Name", "Tag", vec2{screen_size_width / 2, screen_size_height / 2}, 0, 1); - game_object1.add_component<Rigidbody>(Rigidbody::Data{ - .mass = 1, - .gravity_scale = 1, - .body_type = Rigidbody::BodyType::DYNAMIC, - .linear_velocity = {0, 1}, - .constraints = {0, 0, 0}, - .elastisity_coefficient = 0, - .offset = {0, 0}, - }); - // add box with boxcollider - game_object1.add_component<BoxCollider>(vec2{20, 20}); - game_object1.add_component<BehaviorScript>().set_script<MyScript1>(); - - Asset img1{"asset/texture/square.png"}; - game_object1.add_component<Sprite>(img1, Sprite::Data{ - .size = {20, 20}, - }); - - //add circle with cirlcecollider deactiveated - game_object1.add_component<CircleCollider>(10).active = false; - Asset img2{"asset/texture/circle.png"}; - game_object1 - .add_component<Sprite>(img2, - Sprite::Data{ - .size = {20, 20}, - }) - .active - = false; - - GameObject game_object2 = new_object( - "Name", "Tag", vec2{screen_size_width / 2, screen_size_height / 2}, 0, 1); - game_object2.add_component<Rigidbody>(Rigidbody::Data{ - .mass = 1, - .gravity_scale = 0, - .body_type = Rigidbody::BodyType::STATIC, - .linear_velocity = {0, 0}, - .constraints = {0, 0, 0}, - .elastisity_coefficient = 1, - .offset = {0, 0}, - }); - // add box with boxcollider - game_object2.add_component<BoxCollider>(vec2{20, 20}); - game_object2.add_component<BehaviorScript>().set_script<MyScript2>(); - - game_object2.add_component<Sprite>(img1, Sprite::Data{ - .size = {20, 20}, - }); - - //add circle with cirlcecollider deactiveated - game_object2.add_component<CircleCollider>(10).active = false; - - game_object2 - .add_component<Sprite>(img2, - Sprite::Data{ - .size = {20, 20}, - }) - .active - = false; - Asset img5{"asset/texture/square.png"}; - - GameObject particle = new_object( - "Name", "Tag", vec2{screen_size_width / 2, screen_size_height / 2}, 0, 1); - auto & particle_image = particle.add_component<Sprite>(img5, Sprite::Data{ - .size = {5, 5}, - }); - auto & test - = particle.add_component<ParticleEmitter>(particle_image, ParticleEmitter::Data{ - .offset = {0, 0}, - .max_particles = 256, - .emission_rate = 1, - .min_speed = 10, - .max_speed = 20, - .min_angle = -20, - .max_angle = 20, - .begin_lifespan = 0, - .end_lifespan = 5, - }); - } - - string get_name() const { return "scene1"; } -}; - int main(int argc, char * argv[]) { - LoopManager gameloop; - gameloop.add_scene<ConcreteScene1>(); + gameloop.add_scene<GameScene>(); gameloop.start(); return 0; } diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp deleted file mode 100644 index add43f4..0000000 --- a/src/example/rendering_particle.cpp +++ /dev/null @@ -1,73 +0,0 @@ -#include "api/Asset.h" -#include <crepe/Component.h> -#include <crepe/api/Animator.h> -#include <crepe/api/Button.h> -#include <crepe/api/Camera.h> -#include <crepe/api/Color.h> -#include <crepe/api/GameObject.h> -#include <crepe/api/LoopManager.hpp> -#include <crepe/api/ParticleEmitter.h> -#include <crepe/api/Rigidbody.h> -#include <crepe/api/Sprite.h> -#include <crepe/api/Transform.h> -#include <crepe/manager/ComponentManager.h> -#include <crepe/manager/Mediator.h> -#include <crepe/types.h> -#include <iostream> - -using namespace crepe; -using namespace std; - -class TestScene : public Scene { -public: - void load_scene() { - - cout << "TestScene" << endl; - Mediator & mediator = this->mediator; - ComponentManager & mgr = mediator.component_manager; - GameObject game_object = mgr.new_object("", "", vec2{0, 0}, 0, 1); - - Color color(255, 255, 255, 255); - - Asset img{"asset/spritesheet/spritesheet_test.png"}; - - Sprite & test_sprite = game_object.add_component<Sprite>( - img, Sprite::Data{ - .color = color, - .flip = Sprite::FlipSettings{false, false}, - .sorting_in_layer = 2, - .order_in_layer = 2, - .size = {0, 100}, - .angle_offset = 0, - .position_offset = {0, 0}, - }); - - //auto & anim = game_object.add_component<Animator>(test_sprite,ivec2{32, 64}, uvec2{4,1}, Animator::Data{}); - //anim.set_anim(0); - - auto & cam = game_object.add_component<Camera>(ivec2{720, 1280}, vec2{400, 400}, - Camera::Data{ - .bg_color = Color::WHITE, - }); - - function<void()> on_click = [&]() { cout << "button clicked" << std::endl; }; - function<void()> on_enter = [&]() { cout << "enter" << std::endl; }; - function<void()> on_exit = [&]() { cout << "exit" << std::endl; }; - - auto & button - = game_object.add_component<Button>(vec2{200, 200}, vec2{0, 0}, on_click, false); - button.on_mouse_enter = on_enter; - button.on_mouse_exit = on_exit; - button.is_toggle = true; - button.active = true; - } - - string get_name() const { return "TestScene"; }; -}; - -int main(int argc, char * argv[]) { - LoopManager engine; - engine.add_scene<TestScene>(); - engine.start(); - return 0; -} |