From 7e12ebdf945d40d6f11872cf5852c9bb54d1864f Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sun, 22 Dec 2024 16:12:31 +0100 Subject: big WIP --- game/CMakeLists.txt | 17 +++++----- game/Config.h | 11 ++++++ game/GameScene.cpp | 20 ++++++----- game/PlayerScript.cpp | 11 ------ game/PlayerScript.h | 8 ----- game/PlayerSubScene.cpp | 76 ------------------------------------------ game/PlayerSubScene.h | 10 ------ game/background/CMakeLists.txt | 9 +++++ game/main.cpp | 3 ++ game/prefab/CMakeLists.txt | 5 +++ game/prefab/PlayerObject.cpp | 76 ++++++++++++++++++++++++++++++++++++++++++ game/prefab/PlayerObject.h | 30 +++++++++++++++++ game/prefab/PlayerScript.cpp | 26 +++++++++++++++ game/prefab/PlayerScript.h | 23 +++++++++++++ game/prefab/ZapperObject.h | 7 ++++ 15 files changed, 209 insertions(+), 123 deletions(-) delete mode 100644 game/PlayerScript.cpp delete mode 100644 game/PlayerScript.h delete mode 100644 game/PlayerSubScene.cpp delete mode 100644 game/PlayerSubScene.h create mode 100644 game/background/CMakeLists.txt create mode 100644 game/prefab/CMakeLists.txt create mode 100644 game/prefab/PlayerObject.cpp create mode 100644 game/prefab/PlayerObject.h create mode 100644 game/prefab/PlayerScript.cpp create mode 100644 game/prefab/PlayerScript.h create mode 100644 game/prefab/ZapperObject.h (limited to 'game') diff --git a/game/CMakeLists.txt b/game/CMakeLists.txt index 937b5e6..a149487 100644 --- a/game/CMakeLists.txt +++ b/game/CMakeLists.txt @@ -8,20 +8,19 @@ set(CMAKE_BUILD_TYPE Debug) project(game C CXX) add_subdirectory(../src crepe) -add_executable(main - background/AquariumSubScene.cpp - background/BackgroundSubScene.cpp - background/ForestParallaxScript.cpp - background/ForestSubScene.cpp + +add_executable(main) + +target_sources(main PUBLIC GameScene.cpp - background/HallwaySubScene.cpp MoveCameraManualyScript.cpp - PlayerScript.cpp - PlayerSubScene.cpp StartGameScript.cpp - background/StartSubScene.cpp main.cpp ) +add_subdirectory(background) +add_subdirectory(prefab) + target_link_libraries(main PUBLIC crepe) +target_include_directories(main PRIVATE .) diff --git a/game/Config.h b/game/Config.h index ec753df..350cd02 100644 --- a/game/Config.h +++ b/game/Config.h @@ -1,5 +1,16 @@ #pragma once +#include + +static const crepe::Config ENGINE_CONFIG { + .log { + .level = crepe::Log::Level::DEBUG, + }, + .window_settings { + .window_title = "Jetpack joyride clone", + }, +}; + static constexpr int SORT_IN_LAY_BACK_BACKGROUND = 3; // For all scenes static constexpr int SORT_IN_LAY_BACKGROUND = 4; // For all scenes static constexpr int SORT_IN_LAY_FORE_BACKGROUND = 5; // For all scenes diff --git a/game/GameScene.cpp b/game/GameScene.cpp index 2511567..57c6531 100644 --- a/game/GameScene.cpp +++ b/game/GameScene.cpp @@ -1,11 +1,3 @@ -#include "GameScene.h" -#include "Config.h" -#include "MoveCameraManualyScript.h" -#include "PlayerSubScene.h" -#include "StartGameScript.h" - -#include "background/BackgroundSubScene.h" - #include #include #include @@ -22,10 +14,20 @@ #include #include +#include "GameScene.h" +#include "Config.h" +#include "MoveCameraManualyScript.h" +#include "StartGameScript.h" + +#include "background/BackgroundSubScene.h" +#include "prefab/PlayerObject.h" + using namespace crepe; using namespace std; void GameScene::load_scene() { + logf(Log::DEBUG, "Loading (main) GameScene..."); + BackgroundSubScene background(*this); GameObject camera = new_object("camera", "camera", vec2(650, 0)); @@ -38,7 +40,7 @@ void GameScene::load_scene() { camera.add_component().set_script(); camera.add_component(Rigidbody::Data {}); - PlayerSubScene player(*this); + PlayerObject player {new_object("player", "player", vec2(-100, 200))}; GameObject floor = new_object("floor", "game_world", vec2(0, 325)); floor.add_component(Rigidbody::Data { diff --git a/game/PlayerScript.cpp b/game/PlayerScript.cpp deleted file mode 100644 index 1c388f5..0000000 --- a/game/PlayerScript.cpp +++ /dev/null @@ -1,11 +0,0 @@ -#include "PlayerScript.h" - -#include - -using namespace crepe; -using namespace std; - -void PlayerScript::fixed_update(crepe::duration_t dt) { - Rigidbody & rb = this->get_components_by_name("player").front(); - if (this->get_key_state(Keycode::SPACE)) rb.add_force_linear(vec2(0, -10)); -} diff --git a/game/PlayerScript.h b/game/PlayerScript.h deleted file mode 100644 index 84c4f7f..0000000 --- a/game/PlayerScript.h +++ /dev/null @@ -1,8 +0,0 @@ -#pragma once - -#include - -class PlayerScript : public crepe::Script { -public: - void fixed_update(crepe::duration_t dt); -}; diff --git a/game/PlayerSubScene.cpp b/game/PlayerSubScene.cpp deleted file mode 100644 index 00b7810..0000000 --- a/game/PlayerSubScene.cpp +++ /dev/null @@ -1,76 +0,0 @@ -#include "PlayerSubScene.h" -#include "Config.h" -#include "PlayerScript.h" - -#include -#include -#include -#include -#include - -using namespace crepe; -using namespace std; - -PlayerSubScene::PlayerSubScene(Scene & scn) { - GameObject player = scn.new_object("player", "player", vec2(-100, 200)); - Asset player_body_asset {"asset/barry/defaultBody.png"}; - Sprite & player_body_sprite = player.add_component( - player_body_asset, - Sprite::Data { - .sorting_in_layer = SORT_IN_LAY_PLAYER, - .order_in_layer = 0, - .size = vec2(0, 50), - } - ); - player.add_component( - player_body_sprite, ivec2(32, 32), uvec2(4, 8), - Animator::Data { - .fps = 5, - .looping = true, - } - ); - Asset player_head_asset {"asset/barry/defaultHead.png"}; - Sprite & player_head_sprite = player.add_component( - player_head_asset, - Sprite::Data { - .sorting_in_layer = SORT_IN_LAY_PLAYER, - .order_in_layer = 1, - .size = vec2(0, 50), - .position_offset = vec2(0, -20), - } - ); - player.add_component( - player_head_sprite, ivec2(32, 32), uvec2(4, 8), - Animator::Data { - .fps = 5, - .looping = true, - } - ); - Asset player_jetpack_asset {"asset/barry/jetpackDefault.png"}; - Sprite & player_jetpack_sprite = player.add_component( - player_jetpack_asset, - Sprite::Data { - .sorting_in_layer = SORT_IN_LAY_PLAYER, - .order_in_layer = 2, - .size = vec2(0, 60), - .position_offset = vec2(-20, 0), - } - ); - player_jetpack_sprite.active = false; - player.add_component( - player_jetpack_sprite, ivec2(32, 44), uvec2(4, 4), - Animator::Data { - .fps = 5, - .looping = true, - } - ); - player.add_component(Rigidbody::Data { - .gravity_scale = 20, - .body_type = Rigidbody::BodyType::DYNAMIC, - .linear_velocity = vec2(100, 0), - .collision_layers = {COLL_LAY_BOT_TOP}, - .collision_layer = COLL_LAY_PLAYER, - }); - player.add_component(vec2(50, 50)); - player.add_component().set_script().active = false; -} diff --git a/game/PlayerSubScene.h b/game/PlayerSubScene.h deleted file mode 100644 index bf94c32..0000000 --- a/game/PlayerSubScene.h +++ /dev/null @@ -1,10 +0,0 @@ -#pragma once - -namespace crepe { -class Scene; -} - -class PlayerSubScene { -public: - PlayerSubScene(crepe::Scene & scn); -}; diff --git a/game/background/CMakeLists.txt b/game/background/CMakeLists.txt new file mode 100644 index 0000000..1d705f5 --- /dev/null +++ b/game/background/CMakeLists.txt @@ -0,0 +1,9 @@ +target_sources(main PUBLIC + AquariumSubScene.cpp + BackgroundSubScene.cpp + ForestParallaxScript.cpp + ForestSubScene.cpp + HallwaySubScene.cpp + StartSubScene.cpp +) + diff --git a/game/main.cpp b/game/main.cpp index 325b66d..2198f73 100644 --- a/game/main.cpp +++ b/game/main.cpp @@ -1,11 +1,14 @@ #include #include +#include "Config.h" #include "GameScene.h" using namespace crepe; int main() { + Config::get_instance() = ENGINE_CONFIG; + Engine gameloop; gameloop.add_scene(); diff --git a/game/prefab/CMakeLists.txt b/game/prefab/CMakeLists.txt new file mode 100644 index 0000000..a588090 --- /dev/null +++ b/game/prefab/CMakeLists.txt @@ -0,0 +1,5 @@ +target_sources(main PUBLIC + PlayerObject.cpp + PlayerScript.cpp +) + diff --git a/game/prefab/PlayerObject.cpp b/game/prefab/PlayerObject.cpp new file mode 100644 index 0000000..736704a --- /dev/null +++ b/game/prefab/PlayerObject.cpp @@ -0,0 +1,76 @@ +#include + +#include "Config.h" +#include "PlayerObject.h" +#include "PlayerScript.h" + +using namespace crepe; + +PlayerObject::PlayerObject(crepe::GameObject && base) + : GameObject(std::move(base)), + sprite { + .body = add_component( + Asset {"asset/barry/defaultBody.png"}, + Sprite::Data { + .sorting_in_layer = SORT_IN_LAY_PLAYER, + .order_in_layer = 0, + .size = vec2(0, 50), + } + ), + .head = add_component( + Asset {"asset/barry/defaultHead.png"}, + Sprite::Data { + .sorting_in_layer = SORT_IN_LAY_PLAYER, + .order_in_layer = 1, + .size = vec2(0, 50), + .position_offset = vec2(0, -20), + } + ), + .jetpack = add_component( + Asset {"asset/barry/jetpackDefault.png"}, + Sprite::Data { + .sorting_in_layer = SORT_IN_LAY_PLAYER, + .order_in_layer = 2, + .size = vec2(0, 60), + .position_offset = vec2(-20, 0), + } + ) + }, + animator { + .body = add_component( + sprite.body, ivec2(32, 32), uvec2(4, 8), + Animator::Data { + .fps = 5, + .looping = true, + } + ), + .head = add_component( + sprite.head, ivec2(32, 32), uvec2(4, 8), + Animator::Data { + .fps = 5, + .looping = true, + } + ), + .jetpack = add_component( + sprite.jetpack, ivec2(32, 44), uvec2(4, 4), + Animator::Data { + .fps = 5, + .looping = true, + } + ), + }, + body(add_component(Rigidbody::Data { + .gravity_scale = 20, + .body_type = Rigidbody::BodyType::DYNAMIC, + .linear_velocity = vec2(100, 0), + .collision_layers = {COLL_LAY_BOT_TOP}, + .collision_layer = COLL_LAY_PLAYER, + })), + collider(add_component(vec2(50, 50))), + controller(add_component().set_script(this)) { + sprite.jetpack.active = false; + // controller.active = false; + + Log::logf(Log::DEBUG, "PlayerObject: ref {}", (void*) &(this->body.game_object_id)); +} + diff --git a/game/prefab/PlayerObject.h b/game/prefab/PlayerObject.h new file mode 100644 index 0000000..a44d367 --- /dev/null +++ b/game/prefab/PlayerObject.h @@ -0,0 +1,30 @@ +#pragma once + +#include +#include +#include +#include +#include +#include + +class PlayerObject : public crepe::GameObject { +public: + PlayerObject(crepe::GameObject &&); + +public: + struct { + crepe::Sprite & body; + crepe::Sprite & head; + crepe::Sprite & jetpack; + } sprite; + + struct { + crepe::Animator & body; + crepe::Animator & head; + crepe::Animator & jetpack; + } animator; + + crepe::Rigidbody & body; + crepe::BoxCollider & collider; + crepe::BehaviorScript & controller; +}; diff --git a/game/prefab/PlayerScript.cpp b/game/prefab/PlayerScript.cpp new file mode 100644 index 0000000..e4a4951 --- /dev/null +++ b/game/prefab/PlayerScript.cpp @@ -0,0 +1,26 @@ +#include +#include + +#include "PlayerScript.h" + +using namespace crepe; +using namespace std; + +PlayerScript::PlayerScript(PlayerObject * player) : player(player) { + logf(Log::DEBUG, "PlayerScript: [C] player {}", (void*) &(*player)); + logf(Log::DEBUG, "PlayerScript: [C] player.body {}", (void*) &(player->body)); + logf(Log::DEBUG, "PlayerScript: [C] player.body.id {}", (void*) &(player->body.game_object_id)); +} + +void PlayerScript::init() { + logf(Log::DEBUG, "PlayerScript: [C] player {}", (void*) &(*player)); + logf(Log::DEBUG, "PlayerScript: [C] player.body {}", (void*) &(player->body)); + logf(Log::DEBUG, "PlayerScript: [C] player.body.id {}", (void*) &(player->body.game_object_id)); + player->controller.active = false; +} + +void PlayerScript::fixed_update(crepe::duration_t dt) { + if (this->get_key_state(Keycode::SPACE)) + player->body.add_force_linear({ 0, -10 }); +} + diff --git a/game/prefab/PlayerScript.h b/game/prefab/PlayerScript.h new file mode 100644 index 0000000..bd4a00a --- /dev/null +++ b/game/prefab/PlayerScript.h @@ -0,0 +1,23 @@ +#pragma once + +#include + +#include "PlayerObject.h" + +class PlayerScript : public crepe::Script { +public: + PlayerScript(PlayerObject * player); + + PlayerScript(const PlayerScript &) = delete; + PlayerScript(PlayerScript &&) = delete; + PlayerScript & operator=(const PlayerScript &) = delete; + PlayerScript & operator=(PlayerScript &&) = delete; + +protected: + void fixed_update(crepe::duration_t dt); + void init(); + +protected: + PlayerObject * player = nullptr; +}; + diff --git a/game/prefab/ZapperObject.h b/game/prefab/ZapperObject.h new file mode 100644 index 0000000..1f32cd7 --- /dev/null +++ b/game/prefab/ZapperObject.h @@ -0,0 +1,7 @@ +#pragma once + +#include + +class ZapperObject : public crepe::GameObject { + // afsd +}; -- cgit v1.2.3 From 0dae96161a99158815a8075b5c405a35b0939936 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sun, 22 Dec 2024 17:26:27 +0100 Subject: fix Player prefab --- game/prefab/PlayerObject.cpp | 8 ++------ game/prefab/PlayerScript.cpp | 15 ++------------- game/prefab/PlayerScript.h | 10 ++-------- src/crepe/api/GameObject.h | 7 ------- src/crepe/system/ScriptSystem.cpp | 8 +++++--- 5 files changed, 11 insertions(+), 37 deletions(-) (limited to 'game') diff --git a/game/prefab/PlayerObject.cpp b/game/prefab/PlayerObject.cpp index 736704a..ef7b6cc 100644 --- a/game/prefab/PlayerObject.cpp +++ b/game/prefab/PlayerObject.cpp @@ -1,5 +1,3 @@ -#include - #include "Config.h" #include "PlayerObject.h" #include "PlayerScript.h" @@ -67,10 +65,8 @@ PlayerObject::PlayerObject(crepe::GameObject && base) .collision_layer = COLL_LAY_PLAYER, })), collider(add_component(vec2(50, 50))), - controller(add_component().set_script(this)) { + controller(add_component().set_script(*this)) { sprite.jetpack.active = false; - // controller.active = false; - - Log::logf(Log::DEBUG, "PlayerObject: ref {}", (void*) &(this->body.game_object_id)); + controller.active = false; } diff --git a/game/prefab/PlayerScript.cpp b/game/prefab/PlayerScript.cpp index e4a4951..67aadb6 100644 --- a/game/prefab/PlayerScript.cpp +++ b/game/prefab/PlayerScript.cpp @@ -6,21 +6,10 @@ using namespace crepe; using namespace std; -PlayerScript::PlayerScript(PlayerObject * player) : player(player) { - logf(Log::DEBUG, "PlayerScript: [C] player {}", (void*) &(*player)); - logf(Log::DEBUG, "PlayerScript: [C] player.body {}", (void*) &(player->body)); - logf(Log::DEBUG, "PlayerScript: [C] player.body.id {}", (void*) &(player->body.game_object_id)); -} - -void PlayerScript::init() { - logf(Log::DEBUG, "PlayerScript: [C] player {}", (void*) &(*player)); - logf(Log::DEBUG, "PlayerScript: [C] player.body {}", (void*) &(player->body)); - logf(Log::DEBUG, "PlayerScript: [C] player.body.id {}", (void*) &(player->body.game_object_id)); - player->controller.active = false; -} +PlayerScript::PlayerScript(const PlayerObject & player) : player(player) { } void PlayerScript::fixed_update(crepe::duration_t dt) { if (this->get_key_state(Keycode::SPACE)) - player->body.add_force_linear({ 0, -10 }); + player.body.add_force_linear({ 0, -10 }); } diff --git a/game/prefab/PlayerScript.h b/game/prefab/PlayerScript.h index bd4a00a..131c73f 100644 --- a/game/prefab/PlayerScript.h +++ b/game/prefab/PlayerScript.h @@ -6,18 +6,12 @@ class PlayerScript : public crepe::Script { public: - PlayerScript(PlayerObject * player); - - PlayerScript(const PlayerScript &) = delete; - PlayerScript(PlayerScript &&) = delete; - PlayerScript & operator=(const PlayerScript &) = delete; - PlayerScript & operator=(PlayerScript &&) = delete; + PlayerScript(const PlayerObject & player); protected: void fixed_update(crepe::duration_t dt); - void init(); protected: - PlayerObject * player = nullptr; + PlayerObject player; }; diff --git a/src/crepe/api/GameObject.h b/src/crepe/api/GameObject.h index c66da3d..043913a 100644 --- a/src/crepe/api/GameObject.h +++ b/src/crepe/api/GameObject.h @@ -37,13 +37,6 @@ private: //! ComponentManager instances GameObject friend class ComponentManager; -protected: - GameObject(GameObject &&) = default; - - GameObject(const GameObject &) = delete; - GameObject & operator=(const GameObject &) = delete; - GameObject & operator=(GameObject &&) = delete; - public: //! The id of the GameObject const game_object_id_t id; diff --git a/src/crepe/system/ScriptSystem.cpp b/src/crepe/system/ScriptSystem.cpp index 4cce42b..411a9a3 100644 --- a/src/crepe/system/ScriptSystem.cpp +++ b/src/crepe/system/ScriptSystem.cpp @@ -36,15 +36,17 @@ void ScriptSystem::update( script->init(); script->initialized = true; } catch (const exception & e) { - Log::logf(Log::Level::WARNING, "Uncaught exception in {} init: {}", behavior_script.name, e.what()); + Log::logf(Log::Level::WARNING, "Disabled script \"{}\" due to exception in init function: {}", behavior_script.name, e.what()); + behavior_script.active = false; } } try { (*script.*update_function)(delta_time); } catch (const exception & e) { - // TODO: print if it is fixed/frame update - Log::logf(Log::Level::WARNING, "Uncaught exception in {}: {}", behavior_script.name, e.what()); + // TODO: discern between fixed/frame update + Log::logf(Log::Level::WARNING, "Disabled script \"{}\" due to exception in update function: {}", behavior_script.name, e.what()); + behavior_script.active = false; } } } -- cgit v1.2.3 From a600cc55468a6513743ce916aa3da129270c23f0 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sun, 22 Dec 2024 20:04:33 +0100 Subject: more WIP zapper --- game/GameScene.cpp | 5 ++- game/StartGameScript.cpp | 2 +- game/background/HallwaySubScene.cpp | 7 +--- game/prefab/CMakeLists.txt | 2 + game/prefab/ZapperObject.cpp | 84 +++++++++++++++++++++++++++++++++++++ game/prefab/ZapperObject.h | 32 +++++++++++++- game/prefab/ZapperScript.cpp | 23 ++++++++++ game/prefab/ZapperScript.h | 18 ++++++++ src/crepe/api/Animator.cpp | 18 ++------ src/crepe/api/Animator.h | 21 ++++------ src/crepe/api/BehaviorScript.h | 2 + src/crepe/api/BehaviorScript.hpp | 1 + src/crepe/api/Sprite.h | 13 +++--- src/crepe/system/AnimatorSystem.cpp | 36 ++++++++-------- 14 files changed, 204 insertions(+), 60 deletions(-) create mode 100644 game/prefab/ZapperObject.cpp create mode 100644 game/prefab/ZapperScript.cpp create mode 100644 game/prefab/ZapperScript.h (limited to 'game') diff --git a/game/GameScene.cpp b/game/GameScene.cpp index 57c6531..90a8b58 100644 --- a/game/GameScene.cpp +++ b/game/GameScene.cpp @@ -21,6 +21,7 @@ #include "background/BackgroundSubScene.h" #include "prefab/PlayerObject.h" +#include "prefab/ZapperObject.h" using namespace crepe; using namespace std; @@ -40,7 +41,7 @@ void GameScene::load_scene() { camera.add_component().set_script(); camera.add_component(Rigidbody::Data {}); - PlayerObject player {new_object("player", "player", vec2(-100, 200))}; + PlayerObject {new_object("player", "player", vec2(-100, 200))}; GameObject floor = new_object("floor", "game_world", vec2(0, 325)); floor.add_component(Rigidbody::Data { @@ -66,6 +67,8 @@ void GameScene::load_scene() { }); ceiling.add_component(vec2(INFINITY, 200)); + ZapperObject {new_object("zapper", "zapper", vec2(800, 0))}; + GameObject start_game_script = new_object("start_game_script", "script", vec2(0, 0)); start_game_script.add_component().set_script(); } diff --git a/game/StartGameScript.cpp b/game/StartGameScript.cpp index 50ba86c..1196d47 100644 --- a/game/StartGameScript.cpp +++ b/game/StartGameScript.cpp @@ -40,7 +40,7 @@ void StartGameScript::fixed_update(crepe::duration_t dt) { if (player_transform.position.x > 275 && !this->took_jetpack) { Animator & jetpack_stand_anim = this->get_components_by_name("start_begin").back(); - jetpack_stand_anim.next_anim(); + // jetpack_stand_anim.next_anim(); Sprite & jetpack_sprite = this->get_components_by_name("player").back(); jetpack_sprite.active = true; diff --git a/game/background/HallwaySubScene.cpp b/game/background/HallwaySubScene.cpp index 4d96c94..9a6654a 100644 --- a/game/background/HallwaySubScene.cpp +++ b/game/background/HallwaySubScene.cpp @@ -153,11 +153,6 @@ void HallwaySubScene::add_sector_number( Animator & sector_num_anim = obj.add_component( 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.data.frame++; sector_num_anim.pause(); } diff --git a/game/prefab/CMakeLists.txt b/game/prefab/CMakeLists.txt index a588090..03084e4 100644 --- a/game/prefab/CMakeLists.txt +++ b/game/prefab/CMakeLists.txt @@ -1,5 +1,7 @@ target_sources(main PUBLIC PlayerObject.cpp PlayerScript.cpp + ZapperObject.cpp + ZapperScript.cpp ) diff --git a/game/prefab/ZapperObject.cpp b/game/prefab/ZapperObject.cpp new file mode 100644 index 0000000..2791162 --- /dev/null +++ b/game/prefab/ZapperObject.cpp @@ -0,0 +1,84 @@ +#include "Config.h" +#include "ZapperObject.h" +#include "ZapperScript.h" + +using namespace crepe; + +ZapperObject::ZapperObject(crepe::GameObject && base) + : GameObject(std::move(base)), + sprite { + .orb_start = add_component( + Asset {"asset/obstacles/zapper/orbAnim.png"}, + Sprite::Data { + .sorting_in_layer = SORT_IN_LAY_OBSTACLES, + .order_in_layer = 1, + .size = vec2{0, 1} * SCALE, + .position_offset = {0, 100}, + } + ), + .orb_end = add_component( + sprite.orb_start.source, + Sprite::Data { + .flip = {true, true}, + .sorting_in_layer = SORT_IN_LAY_OBSTACLES, + .order_in_layer = 1, + .size = vec2{0, 1} * SCALE, + .position_offset = {0, -100}, + } + ), + .glow_start = add_component( + Asset {"asset/obstacles/zapper/regular_zappers/glow.png"}, + Sprite::Data { + .sorting_in_layer = SORT_IN_LAY_OBSTACLES, + .order_in_layer = -1, + .size = vec2{2, 2} * SCALE, + .position_offset = {0, 100}, + } + ), + .glow_end = add_component( + sprite.glow_start.source, + Sprite::Data { + .flip = {true, true}, + .sorting_in_layer = SORT_IN_LAY_OBSTACLES, + .order_in_layer = -1, + .size = vec2{2, 2} * SCALE, + .position_offset = {0, -100}, + } + ), + .beam = add_component( + Asset {"asset/obstacles/zapper/regular_zappers/zapEffect.png"}, + Sprite::Data { + .sorting_in_layer = SORT_IN_LAY_OBSTACLES, + .order_in_layer = 0, + .size = vec2{200, 50}, + .angle_offset = 90, + } + ), + }, + animator { + .orb_start = add_component( + sprite.orb_start, ivec2(62, 42), uvec2(4, 1), + Animator::Data { + .fps = 10, + .looping = true, + } + ), + .orb_end = add_component( + sprite.orb_end, ivec2(62, 42), uvec2(4, 1), + animator.orb_start.data + ), + .glow_start = add_component( + sprite.glow_start, ivec2(128, 128), uvec2(4, 4), + Animator::Data { + .fps = 30, + .looping = true, + } + ), + .glow_end = add_component( + sprite.glow_end, ivec2(128, 128), uvec2(4, 4), + animator.glow_start.data + ), + }, + controller(add_component().set_script(*this)) +{ } + diff --git a/game/prefab/ZapperObject.h b/game/prefab/ZapperObject.h index 1f32cd7..6b68146 100644 --- a/game/prefab/ZapperObject.h +++ b/game/prefab/ZapperObject.h @@ -1,7 +1,37 @@ #pragma once +#include +#include +#include #include +#include +#include class ZapperObject : public crepe::GameObject { - // afsd +public: + ZapperObject(crepe::GameObject &&); + +public: + struct { + crepe::Sprite & orb_start; + crepe::Sprite & orb_end; + crepe::Sprite & glow_start; + crepe::Sprite & glow_end; + crepe::Sprite & beam; + } sprite; + + struct { + crepe::Animator & orb_start; + crepe::Animator & orb_end; + crepe::Animator & glow_start; + crepe::Animator & glow_end; + } animator; + + // crepe::Rigidbody & body; + // crepe::BoxCollider & collider; + crepe::BehaviorScript & controller; + +private: + static constexpr int SCALE = 60; }; + diff --git a/game/prefab/ZapperScript.cpp b/game/prefab/ZapperScript.cpp new file mode 100644 index 0000000..b9b24be --- /dev/null +++ b/game/prefab/ZapperScript.cpp @@ -0,0 +1,23 @@ +#include +#include + +#include "ZapperScript.h" + +using namespace crepe; +using namespace std; + +ZapperScript::ZapperScript(const ZapperObject & zapper) : zapper(zapper) { } + +void ZapperScript::init() { + zapper.sprite.beam.mask = { + .w = 350, + .h = 117, + .x = 0, + .y = 0, + }; +} + +void ZapperScript::frame_update(duration_t delta_time) { + zapper.sprite.beam.mask.x += 4; +} + diff --git a/game/prefab/ZapperScript.h b/game/prefab/ZapperScript.h new file mode 100644 index 0000000..5a960df --- /dev/null +++ b/game/prefab/ZapperScript.h @@ -0,0 +1,18 @@ +#pragma once + +#include + +#include "ZapperObject.h" + +class ZapperScript : public crepe::Script { +public: + ZapperScript(const ZapperObject & zapper); + +protected: + void init(); + void frame_update(crepe::duration_t delta_time); + +protected: + ZapperObject zapper; +}; + diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp index c558d86..5ac0617 100644 --- a/src/crepe/api/Animator.cpp +++ b/src/crepe/api/Animator.cpp @@ -36,24 +36,12 @@ void Animator::pause() { this->active = false; } void Animator::stop() { this->active = false; - this->data.col = 0; - this->data.row = 0; + this->data.frame = this->data.cycle_start; } void Animator::set_fps(int fps) { this->data.fps = fps; } void Animator::set_cycle_range(int start, int end) { - this->data.cycle_start = start, this->data.cycle_end = end; + this->data.cycle_start = start; + this->data.cycle_end = end; } -void Animator::set_anim(int col) { - Animator::Data & ctx = this->data; - this->spritesheet.mask.x = ctx.row = 0; - ctx.col = col; - this->spritesheet.mask.y = ctx.col * this->spritesheet.mask.h; -} - -void Animator::next_anim() { - Animator::Data & ctx = this->data; - ctx.row = ++ctx.row % this->grid_size.x; - this->spritesheet.mask.x = ctx.row * this->spritesheet.mask.w; -} diff --git a/src/crepe/api/Animator.h b/src/crepe/api/Animator.h index 102894d..8be693e 100644 --- a/src/crepe/api/Animator.h +++ b/src/crepe/api/Animator.h @@ -1,6 +1,7 @@ #pragma once #include "../types.h" +#include "../manager/LoopTimerManager.h" #include "Component.h" #include "Sprite.h" @@ -22,16 +23,15 @@ public: struct Data { //! frames per second for animation unsigned int fps = 1; - //! The current col being animated. - unsigned int col = 0; - //! The current row being animated. - unsigned int row = 0; + //! The current frame being shown + unsigned int frame = 0; + //! should the animation loop bool looping = false; //! starting frame for cycling unsigned int cycle_start = 0; //! end frame for cycling (-1 = use last frame) - int cycle_end = -1; + unsigned int cycle_end = -1; }; public: @@ -60,14 +60,6 @@ public: * \param end of row animation */ void set_cycle_range(int start, int end); - /** - * \brief select which column to animate from - * - * \param col animation column - */ - void set_anim(int col); - //! will go to the next animaiton of current row - void next_anim(); public: /** @@ -101,6 +93,9 @@ private: //! Uses the spritesheet friend AnimatorSystem; + + //! Elasped time since last frame change + duration_t elapsed; }; } // namespace crepe diff --git a/src/crepe/api/BehaviorScript.h b/src/crepe/api/BehaviorScript.h index 3909b96..52cf259 100644 --- a/src/crepe/api/BehaviorScript.h +++ b/src/crepe/api/BehaviorScript.h @@ -48,6 +48,8 @@ public: BehaviorScript & set_script(Args &&... args); protected: + //! Script type name + std::string name = "unknown script"; //! Script instance std::unique_ptr