diff options
| -rw-r--r-- | game/StartGameScript.cpp | 2 | ||||
| -rw-r--r-- | game/background/HallwaySubScene.cpp | 7 | ||||
| -rw-r--r-- | src/crepe/api/Animator.cpp | 19 | ||||
| -rw-r--r-- | src/crepe/api/Animator.h | 21 | ||||
| -rw-r--r-- | src/crepe/api/Sprite.h | 13 | ||||
| -rw-r--r-- | src/crepe/system/AnimatorSystem.cpp | 34 | 
6 files changed, 58 insertions, 38 deletions
| diff --git a/game/StartGameScript.cpp b/game/StartGameScript.cpp index 1196d47..50ba86c 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 9a6654a..4d96c94 100644 --- a/game/background/HallwaySubScene.cpp +++ b/game/background/HallwaySubScene.cpp @@ -153,6 +153,11 @@ void HallwaySubScene::add_sector_number(  	Animator & sector_num_anim = obj.add_component<Animator>(  		sector_num_sprite, ivec2(256, 128), uvec2(4, 4), Animator::Data {}  	); -	sector_num_anim.data.frame++; +	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/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp index f6f2654..c558d86 100644 --- a/src/crepe/api/Animator.cpp +++ b/src/crepe/api/Animator.cpp @@ -36,11 +36,24 @@ void Animator::pause() { this->active = false; }  void Animator::stop() {  	this->active = false; -	this->data.frame = this->data.cycle_start; +	this->data.col = 0; +	this->data.row = 0;  }  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 bcf6379..102894d 100644 --- a/src/crepe/api/Animator.h +++ b/src/crepe/api/Animator.h @@ -1,6 +1,5 @@  #pragma once -#include "../manager/LoopTimerManager.h"  #include "../types.h"  #include "Component.h" @@ -23,15 +22,16 @@ public:  	struct Data {  		//! frames per second for animation  		unsigned int fps = 1; -		//! The current frame being shown -		unsigned int frame = 0; - +		//! The current col being animated. +		unsigned int col = 0; +		//! The current row being animated. +		unsigned int row = 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) -		unsigned int cycle_end = -1; +		int cycle_end = -1;  	};  public: @@ -60,6 +60,14 @@ 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:  	/** @@ -93,9 +101,6 @@ private:  	//! Uses the spritesheet  	friend AnimatorSystem; - -	//! Elasped time since last frame change -	duration_t elapsed;  };  } // namespace crepe diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h index ef65f64..a3fc319 100644 --- a/src/crepe/api/Sprite.h +++ b/src/crepe/api/Sprite.h @@ -110,16 +110,15 @@ private:  	 */  	float aspect_ratio = 0; -public: -	struct Mask { -		unsigned w = 0; -		unsigned h = 0; -		unsigned x = 0; -		unsigned y = 0; +	struct Rect { +		int w = 0; +		int h = 0; +		int x = 0; +		int 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 -	Mask mask; +	Rect mask;  };  } // namespace crepe diff --git a/src/crepe/system/AnimatorSystem.cpp b/src/crepe/system/AnimatorSystem.cpp index 6c93372..e5ab2fa 100644 --- a/src/crepe/system/AnimatorSystem.cpp +++ b/src/crepe/system/AnimatorSystem.cpp @@ -13,30 +13,28 @@ void AnimatorSystem::frame_update() {  	LoopTimerManager & timer = this->mediator.loop_timer;  	RefVector<Animator> animations = mgr.get_components_by_type<Animator>(); -	duration_t elapsed = timer.get_delta_time(); +	float elapsed_time = duration_cast<duration<float>>(timer.get_elapsed_time()).count(); -	for (Animator & animator : animations) { -		if (!animator.active) continue; +	for (Animator & a : animations) { +		if (!a.active) continue; +		if (a.data.fps == 0) continue; -		Animator::Data & data = animator.data; -		if (animator.data.fps == 0) continue; +		Animator::Data & ctx = a.data; +		float frame_duration = 1.0f / ctx.fps; -		if (animator.data.cycle_end == -1) -			animator.data.cycle_end = animator.grid_size.x * animator.grid_size.y; +		int last_frame = ctx.row; -		animator.elapsed += elapsed; -		duration_t frame_duration = 1000ms / animator.data.fps; +		int cycle_end = (ctx.cycle_end == -1) ? a.grid_size.x : ctx.cycle_end; +		int total_frames = cycle_end - ctx.cycle_start; -		if (animator.elapsed > frame_duration) { -			animator.elapsed = 0ms; -			animator.data.frame++; +		int curr_frame = static_cast<int>(elapsed_time / frame_duration) % total_frames; -			if (animator.data.looping && animator.data.frame >= animator.data.cycle_end) -				animator.data.frame = animator.data.cycle_start; -		} +		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); -		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; +		if (!ctx.looping && curr_frame == ctx.cycle_start && last_frame == total_frames - 1) { +			a.active = false; +		}  	}  } |