diff options
-rw-r--r-- | game/GameScene.cpp | 5 | ||||
-rw-r--r-- | game/StartGameScript.cpp | 2 | ||||
-rw-r--r-- | game/background/HallwaySubScene.cpp | 7 | ||||
-rw-r--r-- | game/prefab/CMakeLists.txt | 2 | ||||
-rw-r--r-- | game/prefab/ZapperObject.cpp | 84 | ||||
-rw-r--r-- | game/prefab/ZapperObject.h | 32 | ||||
-rw-r--r-- | game/prefab/ZapperScript.cpp | 23 | ||||
-rw-r--r-- | game/prefab/ZapperScript.h | 18 | ||||
-rw-r--r-- | src/crepe/api/Animator.cpp | 18 | ||||
-rw-r--r-- | src/crepe/api/Animator.h | 21 | ||||
-rw-r--r-- | src/crepe/api/BehaviorScript.h | 2 | ||||
-rw-r--r-- | src/crepe/api/BehaviorScript.hpp | 1 | ||||
-rw-r--r-- | src/crepe/api/Sprite.h | 13 | ||||
-rw-r--r-- | src/crepe/system/AnimatorSystem.cpp | 36 |
14 files changed, 204 insertions, 60 deletions
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<BehaviorScript>().set_script<MoveCameraManualyScript>(); camera.add_component<Rigidbody>(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>(Rigidbody::Data { @@ -66,6 +67,8 @@ void GameScene::load_scene() { }); ceiling.add_component<BoxCollider>(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<BehaviorScript>().set_script<StartGameScript>(); } 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<Animator>("start_begin").back(); - jetpack_stand_anim.next_anim(); + // jetpack_stand_anim.next_anim(); Sprite & jetpack_sprite = this->get_components_by_name<Sprite>("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<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.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<Sprite>( + 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>( + 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<Sprite>( + 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>( + 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<Sprite>( + 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<Animator>( + sprite.orb_start, ivec2(62, 42), uvec2(4, 1), + Animator::Data { + .fps = 10, + .looping = true, + } + ), + .orb_end = add_component<Animator>( + sprite.orb_end, ivec2(62, 42), uvec2(4, 1), + animator.orb_start.data + ), + .glow_start = add_component<Animator>( + sprite.glow_start, ivec2(128, 128), uvec2(4, 4), + Animator::Data { + .fps = 30, + .looping = true, + } + ), + .glow_end = add_component<Animator>( + sprite.glow_end, ivec2(128, 128), uvec2(4, 4), + animator.glow_start.data + ), + }, + controller(add_component<BehaviorScript>().set_script<ZapperScript>(*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 <crepe/api/Animator.h> +#include <crepe/api/BehaviorScript.h> +#include <crepe/api/BoxCollider.h> #include <crepe/api/GameObject.h> +#include <crepe/api/Rigidbody.h> +#include <crepe/api/Sprite.h> 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 <crepe/api/Rigidbody.h> +#include <cassert> + +#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 <crepe/api/Script.h> + +#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<Script> script = nullptr; //! ScriptSystem needs direct access to the script instance diff --git a/src/crepe/api/BehaviorScript.hpp b/src/crepe/api/BehaviorScript.hpp index 353d5e2..218f27c 100644 --- a/src/crepe/api/BehaviorScript.hpp +++ b/src/crepe/api/BehaviorScript.hpp @@ -11,6 +11,7 @@ template <class T, typename... Args> BehaviorScript & BehaviorScript::set_script(Args &&... args) { static_assert(std::is_base_of<Script, T>::value); this->script = std::unique_ptr<Script>(new T(std::forward<Args>(args)...)); + this->name = typeid(T).name(); this->script->game_object_id = this->game_object_id; this->script->active = this->active; diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h index a3fc319..ef65f64 100644 --- a/src/crepe/api/Sprite.h +++ b/src/crepe/api/Sprite.h @@ -110,15 +110,16 @@ private: */ float aspect_ratio = 0; - struct Rect { - int w = 0; - int h = 0; - int x = 0; - int y = 0; +public: + struct Mask { + unsigned w = 0; + unsigned h = 0; + unsigned x = 0; + unsigned y = 0; }; //! Render area of the sprite this will also be adjusted by the AnimatorSystem if an Animator // object is present in GameObject. this is in sprite pixels - Rect mask; + Mask mask; }; } // namespace crepe diff --git a/src/crepe/system/AnimatorSystem.cpp b/src/crepe/system/AnimatorSystem.cpp index e5ab2fa..467d2a2 100644 --- a/src/crepe/system/AnimatorSystem.cpp +++ b/src/crepe/system/AnimatorSystem.cpp @@ -13,28 +13,30 @@ void AnimatorSystem::frame_update() { LoopTimerManager & timer = this->mediator.loop_timer; RefVector<Animator> animations = mgr.get_components_by_type<Animator>(); - float elapsed_time = duration_cast<duration<float>>(timer.get_elapsed_time()).count(); + duration_t elapsed = timer.get_delta_time(); - for (Animator & a : animations) { - if (!a.active) continue; - if (a.data.fps == 0) continue; + for (Animator & animator : animations) { + if (!animator.active) continue; - Animator::Data & ctx = a.data; - float frame_duration = 1.0f / ctx.fps; + Animator::Data & data = animator.data; + if (animator.data.fps == 0) continue; - int last_frame = ctx.row; + if (animator.data.cycle_end == -1) + animator.data.cycle_end = animator.grid_size.x * animator.grid_size.y; - int cycle_end = (ctx.cycle_end == -1) ? a.grid_size.x : ctx.cycle_end; - int total_frames = cycle_end - ctx.cycle_start; + animator.elapsed += elapsed; + duration_t frame_duration = 1000ms / animator.data.fps; - int curr_frame = static_cast<int>(elapsed_time / frame_duration) % total_frames; - - ctx.row = ctx.cycle_start + curr_frame; - a.spritesheet.mask.x = ctx.row * a.spritesheet.mask.w; - a.spritesheet.mask.y = (ctx.col * a.spritesheet.mask.h); - - if (!ctx.looping && curr_frame == ctx.cycle_start && last_frame == total_frames - 1) { - a.active = false; + if (animator.elapsed > frame_duration) { + animator.elapsed = 0ms; + animator.data.frame++; + + if (animator.data.looping && animator.data.frame >= animator.data.cycle_end) + animator.data.frame = animator.data.cycle_start; } + + Sprite::Mask & mask = animator.spritesheet.mask; + mask.x = animator.data.frame % animator.grid_size.x * mask.w; + mask.y = animator.data.frame / animator.grid_size.x * mask.h; } } |