diff options
| author | Loek Le Blansch <loek@pipeframe.xyz> | 2025-01-07 10:37:52 +0100 | 
|---|---|---|
| committer | Loek Le Blansch <loek@pipeframe.xyz> | 2025-01-07 10:37:52 +0100 | 
| commit | d46548ab15699f7ae91227625302553e79a126bc (patch) | |
| tree | e5b05fc99dca66512cf76cce30bf905e2213de8b | |
| parent | c817f6ff091de9869c803868c50a9ea884ead376 (diff) | |
| parent | 77d02bf2e2d5d04e8cacb3c783446541517e8e76 (diff) | |
merge master
| -rw-r--r-- | game/CMakeLists.txt | 6 | ||||
| -rw-r--r-- | game/Config.h | 6 | ||||
| -rw-r--r-- | game/GameScene.cpp | 56 | ||||
| -rw-r--r-- | game/PlayerScript.cpp | 11 | ||||
| -rw-r--r-- | game/PlayerSubScene.cpp | 76 | ||||
| -rw-r--r-- | game/StartGameScript.cpp | 3 | ||||
| -rw-r--r-- | game/player/PlayerEndScript.cpp | 96 | ||||
| -rw-r--r-- | game/player/PlayerEndScript.h | 14 | ||||
| -rw-r--r-- | game/player/PlayerScript.cpp | 119 | ||||
| -rw-r--r-- | game/player/PlayerScript.h (renamed from game/PlayerScript.h) | 8 | ||||
| -rw-r--r-- | game/player/PlayerSubScene.cpp | 153 | ||||
| -rw-r--r-- | game/player/PlayerSubScene.h (renamed from game/PlayerSubScene.h) | 0 | ||||
| -rw-r--r-- | src/crepe/api/Animator.h | 7 | ||||
| -rw-r--r-- | src/crepe/api/Button.cpp | 7 | ||||
| -rw-r--r-- | src/crepe/api/Button.h | 18 | ||||
| -rw-r--r-- | src/crepe/api/Text.cpp | 6 | ||||
| -rw-r--r-- | src/crepe/api/Text.h | 24 | ||||
| -rw-r--r-- | src/crepe/api/UIObject.h | 4 | ||||
| -rw-r--r-- | src/crepe/facade/SDLContext.cpp | 17 | ||||
| -rw-r--r-- | src/crepe/system/AnimatorSystem.cpp | 30 | ||||
| -rw-r--r-- | src/crepe/system/InputSystem.cpp | 6 | ||||
| -rw-r--r-- | src/crepe/system/InputSystem.h | 5 | ||||
| -rw-r--r-- | src/example/rendering_particle.cpp | 34 | 
23 files changed, 540 insertions, 166 deletions
| diff --git a/game/CMakeLists.txt b/game/CMakeLists.txt index 32d7085..cc0cc84 100644 --- a/game/CMakeLists.txt +++ b/game/CMakeLists.txt @@ -14,9 +14,11 @@ add_executable(main)  target_sources(main PUBLIC  	GameScene.cpp  	MoveCameraManualyScript.cpp +	player/PlayerScript.cpp +	player/PlayerSubScene.cpp  	StartGameScript.cpp -	PlayerScript.cpp -	PlayerSubScene.cpp +	player/PlayerEndScript.cpp +	background/StartSubScene.cpp  	main.cpp  ) diff --git a/game/Config.h b/game/Config.h index 350cd02..940bf1d 100644 --- a/game/Config.h +++ b/game/Config.h @@ -24,9 +24,15 @@ static constexpr int COLL_LAY_BOT_LOW = 2; // Only for GameScene  static constexpr int COLL_LAY_BOT_HIGH = 3; // Only for GameScene  static constexpr int COLL_LAY_PLAYER = 4; // Only for GameScene  static constexpr int COLL_LAY_WALL_FRAGS = 5; // Only for GameScene +static constexpr int COLL_LAY_ZAPPER = 6; // Only for GameScene +static constexpr int COLL_LAY_LASER = 7; // Only for GameScene +static constexpr int COLL_LAY_MISSILE = 8; // Only for GameScene  static constexpr int GAME_HEIGHT = 800; // In game units  static constexpr int VIEWPORT_X = 1100; // In game units  // 'GAME_HEIGHT' (below) should be replaced by '500' when game development is finished  static constexpr int VIEWPORT_Y = GAME_HEIGHT; // In game units + +static constexpr int PLAYER_SPEED = 7500; // In game units +static constexpr int PLAYER_GRAVITY_SCALE = 60; // In game units diff --git a/game/GameScene.cpp b/game/GameScene.cpp index dc0038b..4193581 100644 --- a/game/GameScene.cpp +++ b/game/GameScene.cpp @@ -15,11 +15,13 @@  #include <crepe/types.h>  #include "Config.h" -#include "GameScene.h"  #include "MoveCameraManualyScript.h"  #include "StartGameScript.h" -#include "PlayerSubScene.h" +#include "GameScene.h" +#include "MoveCameraManualyScript.h" + +#include "player/PlayerSubScene.h"  #include "background/BackgroundSubScene.h"  #include "prefab/ZapperObject.h" @@ -71,6 +73,56 @@ void GameScene::load_scene() {  	GameObject start_game_script = new_object("start_game_script", "script", vec2(0, 0));  	start_game_script.add_component<BehaviorScript>().set_script<StartGameScript>(); + +	// zapper, laser and missile (below) for testing purpose only!!! +	GameObject zapper = new_object("zapper", "zapper", vec2(1000, 0)); +	Asset zapper_asset {"asset/obstacles/zapper/regular_zappers/zapEffect.png"}; +	Sprite & zapper_sprite = zapper.add_component<Sprite>( +		zapper_asset, +		Sprite::Data { +			.sorting_in_layer = SORT_IN_LAY_OBSTACLES, +			.order_in_layer = 0, +			.size = vec2(100, 100), +		} +	); +	zapper.add_component<Rigidbody>(Rigidbody::Data { +		.body_type = Rigidbody::BodyType::KINEMATIC, +		.kinematic_collision = false, +		.collision_layer = COLL_LAY_ZAPPER, +	}); +	zapper.add_component<BoxCollider>(vec2(100, 100)); +	GameObject laser = new_object("laser", "laser", vec2(2000, 0)); +	Asset laser_asset {"asset/obstacles/laser/laserPower.png"}; +	Sprite & laser_sprite = laser.add_component<Sprite>( +		laser_asset, +		Sprite::Data { +			.sorting_in_layer = SORT_IN_LAY_OBSTACLES, +			.order_in_layer = 0, +			.size = vec2(100, 100), +		} +	); +	laser.add_component<Rigidbody>(Rigidbody::Data { +		.body_type = Rigidbody::BodyType::KINEMATIC, +		.kinematic_collision = false, +		.collision_layer = COLL_LAY_LASER, +	}); +	laser.add_component<BoxCollider>(vec2(100, 100)); +	GameObject missile = new_object("missile", "missile", vec2(4000, 0)); +	Asset missile_asset {"asset/obstacles/missile/missile.png"}; +	Sprite & missile_sprite = missile.add_component<Sprite>( +		missile_asset, +		Sprite::Data { +			.sorting_in_layer = SORT_IN_LAY_OBSTACLES, +			.order_in_layer = 0, +			.size = vec2(100, 100), +		} +	); +	missile.add_component<Rigidbody>(Rigidbody::Data { +		.body_type = Rigidbody::BodyType::KINEMATIC, +		.kinematic_collision = false, +		.collision_layer = COLL_LAY_MISSILE, +	}); +	missile.add_component<BoxCollider>(vec2(100, 100));  }  string GameScene::get_name() const { return "scene1"; } 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 <crepe/api/Rigidbody.h> - -using namespace crepe; -using namespace std; - -void PlayerScript::fixed_update(crepe::duration_t dt) { -	Rigidbody & rb = this->get_components_by_name<Rigidbody>("player").front(); -	if (this->get_key_state(Keycode::SPACE)) rb.add_force_linear(vec2(0, -10)); -} 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 <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; - -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<Sprite>( -		player_body_asset, -		Sprite::Data { -			.sorting_in_layer = SORT_IN_LAY_PLAYER, -			.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/barry/defaultHead.png"}; -	Sprite & player_head_sprite = player.add_component<Sprite>( -		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<Animator>( -		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<Sprite>( -		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<Animator>( -		player_jetpack_sprite, ivec2(32, 44), uvec2(4, 4), -		Animator::Data { -			.fps = 5, -			.looping = true, -		} -	); -	player.add_component<Rigidbody>(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<BoxCollider>(vec2(50, 50)); -	player.add_component<BehaviorScript>().set_script<PlayerScript>().active = false; -} diff --git a/game/StartGameScript.cpp b/game/StartGameScript.cpp index 50ba86c..c786eb4 100644 --- a/game/StartGameScript.cpp +++ b/game/StartGameScript.cpp @@ -1,4 +1,5 @@  #include "StartGameScript.h" +#include "Config.h"  #include <crepe/api/Animator.h>  #include <crepe/api/ParticleEmitter.h> @@ -50,7 +51,7 @@ void StartGameScript::fixed_update(crepe::duration_t dt) {  	// 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); +		rb.data.linear_velocity = vec2(PLAYER_SPEED * dt.count(), 0);  		BehaviorScript & player_script  			= this->get_components_by_name<BehaviorScript>("player").front();  		player_script.active = true; diff --git a/game/player/PlayerEndScript.cpp b/game/player/PlayerEndScript.cpp new file mode 100644 index 0000000..e04fb9d --- /dev/null +++ b/game/player/PlayerEndScript.cpp @@ -0,0 +1,96 @@ +#include "PlayerEndScript.h" + +#include "../Config.h" +#include "manager/LoopTimerManager.h" + +#include <crepe/api/Animator.h> +#include <crepe/api/BoxCollider.h> +#include <crepe/api/CircleCollider.h> +#include <crepe/api/Rigidbody.h> +#include <crepe/types.h> + +using namespace crepe; +using namespace std; + +void PlayerEndScript::init() { +	Rigidbody & rb_player = this->get_components_by_name<Rigidbody>("player").front(); +	rb_player.data.elasticity_coefficient = 0.7; + +	subscribe<CollisionEvent>([this](const CollisionEvent & ev) -> bool { +		return this->on_collision(ev); +	}); +} + +bool PlayerEndScript::on_collision(const crepe::CollisionEvent & ev) { +	if (ev.info.other.metadata.name == "floor") { +		Transform & transform_player +			= this->get_components_by_name<Transform>("player").front(); +		RefVector<Animator> anim_player = this->get_components_by_name<Animator>("player"); +		Rigidbody & rb_player = this->get_components_by_name<Rigidbody>("player").front(); +		Rigidbody & rb_camera = this->get_components_by_name<Rigidbody>("camera").front(); + +		float dt = this->get_loop_timer().get_fixed_delta_time().count(); + +		if (jump == 0) { +			int random_number = rand() % 4; +			for (Animator & anim : anim_player) { +				anim.active = false; +				anim.set_anim(6); +				for (int i = 0; i < random_number; i++) { +					anim.next_anim(); +				} +			} +		} else if (jump == 1) { +			for (Animator & anim : anim_player) { +				anim.next_anim(); +			} +		} + +		if (jump == 0) { +			rb_player.data.angular_velocity = 16000 * dt; +			rb_player.data.angular_velocity_coefficient = 0.7; +			jump++; +		} else if (jump == 1) { +			jump++; +		} else if (jump == 2) { +			RefVector<Rigidbody> rb_back_forest +				= this->get_components_by_tag<Rigidbody>("forest_background"); +			for (Rigidbody & rb : rb_back_forest) { +				rb.data.linear_velocity_coefficient = vec2(0.5, 0.5); +			} + +			rb_player.data.angular_velocity = 0; +			rb_player.data.elasticity_coefficient = 0; +			rb_player.data.linear_velocity = vec2(PLAYER_SPEED * dt, 0); +			rb_player.data.linear_velocity_coefficient = vec2(0.5, 0.5); +			rb_camera.data.linear_velocity_coefficient = vec2(0.5, 0.5); +			for (Animator & anim : anim_player) { +				anim.active = false; +				anim.set_anim(7); +			} +			if (transform_player.rotation > 0 && transform_player.rotation < 90) { +				// Do not call next_anim() +			} else if (transform_player.rotation > 90 && transform_player.rotation < 180) { +				for (Animator & anim : anim_player) { +					anim.next_anim(); +				} +			} else if (transform_player.rotation > 180 && transform_player.rotation < 270) { +				for (Animator & anim : anim_player) { +					anim.next_anim(); +					anim.next_anim(); +				} +			} else { +				for (Animator & anim : anim_player) { +					anim.next_anim(); +					anim.next_anim(); +					anim.next_anim(); +				} +			} +			jump++; +		} + +		return true; +	} + +	return false; +} diff --git a/game/player/PlayerEndScript.h b/game/player/PlayerEndScript.h new file mode 100644 index 0000000..03ea8a9 --- /dev/null +++ b/game/player/PlayerEndScript.h @@ -0,0 +1,14 @@ +#pragma once + +#include <crepe/api/Script.h> + +class PlayerEndScript : public crepe::Script { +public: +	void init(); + +private: +	bool on_collision(const crepe::CollisionEvent & ev); + +private: +	int jump = 0; +}; diff --git a/game/player/PlayerScript.cpp b/game/player/PlayerScript.cpp new file mode 100644 index 0000000..472d7c8 --- /dev/null +++ b/game/player/PlayerScript.cpp @@ -0,0 +1,119 @@ +#include "PlayerScript.h" + +#include "../Config.h" + +#include <crepe/api/Animator.h> +#include <crepe/api/ParticleEmitter.h> +#include <crepe/api/Rigidbody.h> +#include <crepe/api/Transform.h> +#include <crepe/types.h> + +using namespace crepe; +using namespace std; + +void PlayerScript::init() { +	subscribe<CollisionEvent>([this](const CollisionEvent & ev) -> bool { +		return this->on_collision(ev); +	}); +} + +bool PlayerScript::on_collision(const CollisionEvent & ev) { +	BehaviorScript & play_scr = this->get_components_by_name<BehaviorScript>("player").front(); +	BehaviorScript & end_scr = this->get_components_by_name<BehaviorScript>("player").back(); +	RefVector<Animator> animators = this->get_components_by_name<Animator>("player"); +	RefVector<ParticleEmitter> emitters +		= this->get_components_by_name<ParticleEmitter>("player"); + +	if (ev.info.other.metadata.tag == "zapper") { +		for (Animator & anim : animators) { +			anim.active = true; +			anim.set_anim(4); +			anim.data.looping = true; +			prev_anim = 0; +		} +		for (ParticleEmitter & emitter : emitters) { +			emitter.data.emission_rate = 0; +		} +		play_scr.active = false; +		end_scr.active = true; +		return true; +	} else if (ev.info.other.metadata.tag == "laser") { +		for (Animator & anim : animators) { +			anim.active = true; +			anim.set_anim(4); +			anim.data.looping = true; +			prev_anim = 0; +		} +		for (ParticleEmitter & emitter : emitters) { +			emitter.data.emission_rate = 0; +		} +		play_scr.active = false; +		end_scr.active = true; +		return true; +	} else if (ev.info.other.metadata.tag == "missile") { +		for (Animator & anim : animators) { +			anim.active = true; +			anim.set_anim(5); +			anim.data.looping = true; +			prev_anim = 0; +		} +		for (ParticleEmitter & emitter : emitters) { +			emitter.data.emission_rate = 0; +		} +		play_scr.active = false; +		end_scr.active = true; +		return true; +	} + +	return false; +} + +void PlayerScript::fixed_update(crepe::duration_t dt) { +	RefVector<Animator> animators = this->get_components_by_name<Animator>("player"); +	RefVector<ParticleEmitter> emitters +		= this->get_components_by_name<ParticleEmitter>("player"); +	Transform & transform = this->get_components_by_name<Transform>("player").front(); + +	for (ParticleEmitter & emitter : emitters) { +		emitter.data.boundary.offset = vec2(0, -transform.position.y); +	} + +	Rigidbody & rb = this->get_components_by_name<Rigidbody>("player").front(); +	if (this->get_key_state(Keycode::SPACE)) { +		rb.add_force_linear(vec2(0, -PLAYER_GRAVITY_SCALE / 2.5) * dt.count() / 0.02); +		if (prev_anim != 1) { +			for (Animator & anim : animators) { +				anim.active = true; +				anim.set_anim(1); +				anim.data.looping = true; +				prev_anim = 1; +			} +			for (ParticleEmitter & emitter : emitters) { +				emitter.data.emission_rate = 30; +			} +		} +	} else if (transform.position.y == 195) { +		if (prev_anim != 0) { +			for (Animator & anim : animators) { +				anim.active = true; +				anim.set_anim(0); +				anim.data.looping = true; +				prev_anim = 0; +			} +			for (ParticleEmitter & emitter : emitters) { +				emitter.data.emission_rate = 0; +			} +		} +	} else { +		if (prev_anim != 2) { +			for (Animator & anim : animators) { +				anim.set_anim(2); +				anim.data.looping = false; +				prev_anim = 2; +			} +			for (ParticleEmitter & emitter : emitters) { +				emitter.data.emission_rate = 0; +			} +		} +	} +} diff --git a/game/PlayerScript.h b/game/player/PlayerScript.h index 84c4f7f..d8eb098 100644 --- a/game/PlayerScript.h +++ b/game/player/PlayerScript.h @@ -1,8 +1,16 @@  #pragma once +#include <crepe/api/Event.h>  #include <crepe/api/Script.h>  class PlayerScript : public crepe::Script {  public: +	void init();  	void fixed_update(crepe::duration_t dt); + +private: +	bool on_collision(const crepe::CollisionEvent & ev); + +private: +	int prev_anim = 0;  }; diff --git a/game/player/PlayerSubScene.cpp b/game/player/PlayerSubScene.cpp new file mode 100644 index 0000000..c1e5e2f --- /dev/null +++ b/game/player/PlayerSubScene.cpp @@ -0,0 +1,153 @@ +#include "PlayerSubScene.h" +#include "PlayerEndScript.h" +#include "PlayerScript.h" + +#include "../Config.h" + +#include <crepe/api/Animator.h> +#include <crepe/api/BoxCollider.h> +#include <crepe/api/CircleCollider.h> +#include <crepe/api/GameObject.h> +#include <crepe/api/ParticleEmitter.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; + +PlayerSubScene::PlayerSubScene(Scene & scn) { +	GameObject player = scn.new_object("player", "player", vec2(-100, 200)); + +	Asset player_bullet {"asset/other_effects/effect_smgbullet.png"}; +	Sprite & player_bullet_sprite = player.add_component<Sprite>( +		player_bullet, +		Sprite::Data { +			.sorting_in_layer = SORT_IN_LAY_PLAYER, +			.order_in_layer = 3, +			.size = vec2(0, 6), +		} +	); +	player.add_component<ParticleEmitter>(player_bullet_sprite, ParticleEmitter::Data{ +		.offset = vec2(-15, 15), +		.emission_rate = 0, +		.min_speed = 300, +		.max_speed = 500, +		.min_angle = 85, +		.max_angle = 100, +		.boundary = ParticleEmitter::Boundary { +			.height = 400, +			.reset_on_exit = true, +		}, +	}); +	Asset player_bullet_x2 {"asset/other_effects/effect_smgbullet_x2.png"}; +	Sprite & player_bullet_x2_sprite = player.add_component<Sprite>( +		player_bullet_x2, +		Sprite::Data { +			.sorting_in_layer = SORT_IN_LAY_PLAYER, +			.order_in_layer = 3, +			.size = vec2(0, 12), +		} +	); +	player.add_component<ParticleEmitter>(player_bullet_x2_sprite, ParticleEmitter::Data{ +		.offset = vec2(-15, 15), +		.emission_rate = 0, +		.min_speed = 300, +		.max_speed = 500, +		.min_angle = 85, +		.max_angle = 100, +		.boundary = ParticleEmitter::Boundary { +			.height = 400, +			.reset_on_exit = true, +		}, +	}); +	Asset player_shell {"asset/other_effects/effect_rocketmgshell_TVOS.png"}; +	Sprite & player_shell_sprite = player.add_component<Sprite>( +		player_shell, +		Sprite::Data { +			.sorting_in_layer = SORT_IN_LAY_PLAYER, +			.order_in_layer = 3, +			.size = vec2(0, 12), +			.angle_offset = 90, +		} +	); +	player.add_component<ParticleEmitter>(player_shell_sprite, ParticleEmitter::Data{ +		.offset = vec2(-15, 15), +		.emission_rate = 0, +		.min_speed = 200, +		.max_speed = 500, +		.min_angle = 110, +		.max_angle = 120, +		.force_over_time = vec2(0, 1000), +		.boundary = ParticleEmitter::Boundary { +			.height = 400, +			.reset_on_exit = true, +		}, +	}); + +	Asset player_body_asset {"asset/barry/defaultBody.png"}; +	Sprite & player_body_sprite = player.add_component<Sprite>( +		player_body_asset, +		Sprite::Data { +			.sorting_in_layer = SORT_IN_LAY_PLAYER, +			.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, +		} +	); +	player.add_component<BoxCollider>(vec2(50, 50)); +	Asset player_head_asset {"asset/barry/defaultHead.png"}; +	Sprite & player_head_sprite = player.add_component<Sprite>( +		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<Animator>( +		player_head_sprite, ivec2(32, 32), uvec2(4, 8), +		Animator::Data { +			.fps = 5, +			.looping = true, +		} +	); +	player.add_component<CircleCollider>(25, vec2(0, -20)); +	Asset player_jetpack_asset {"asset/barry/jetpackDefault.png"}; +	Sprite & player_jetpack_sprite = player.add_component<Sprite>( +		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<Animator>( +		player_jetpack_sprite, ivec2(32, 44), uvec2(4, 4), +		Animator::Data { +			.fps = 5, +			.looping = true, +		} +	); +	player.add_component<BoxCollider>(vec2(40, 60), vec2(-20, 0)); +	player.add_component<Rigidbody>(Rigidbody::Data { +		.gravity_scale = PLAYER_GRAVITY_SCALE, +		.body_type = Rigidbody::BodyType::DYNAMIC, +		.linear_velocity = vec2(PLAYER_SPEED * 0.02, 0), +		.collision_layers +		= {COLL_LAY_BOT_TOP, COLL_LAY_ZAPPER, COLL_LAY_LASER, COLL_LAY_MISSILE}, +		.collision_layer = COLL_LAY_PLAYER, +	}); +	player.add_component<BehaviorScript>().set_script<PlayerScript>().active = false; +	player.add_component<BehaviorScript>().set_script<PlayerEndScript>().active = false; +} diff --git a/game/PlayerSubScene.h b/game/player/PlayerSubScene.h index bf94c32..bf94c32 100644 --- a/game/PlayerSubScene.h +++ b/game/player/PlayerSubScene.h diff --git a/src/crepe/api/Animator.h b/src/crepe/api/Animator.h index 102894d..95539d3 100644 --- a/src/crepe/api/Animator.h +++ b/src/crepe/api/Animator.h @@ -1,5 +1,6 @@  #pragma once +#include "../manager/LoopTimerManager.h"  #include "../types.h"  #include "Component.h" @@ -99,6 +100,12 @@ private:  	//! The maximum number of rows and columns inside the spritesheet  	const uvec2 grid_size; +	// the time elapsed from a frame duration +	duration_t elapsed_time = {}; + +	// frame counter +	unsigned int frame = 0; +  	//! Uses the spritesheet  	friend AnimatorSystem;  }; diff --git a/src/crepe/api/Button.cpp b/src/crepe/api/Button.cpp index 40153c9..8eadd89 100644 --- a/src/crepe/api/Button.cpp +++ b/src/crepe/api/Button.cpp @@ -2,7 +2,10 @@  namespace crepe { -Button::Button(game_object_id_t id, const vec2 & dimensions, const vec2 & offset) -	: UIObject(id, dimensions, offset) {} +Button::Button( +	game_object_id_t id, const vec2 & dimensions, const Data & data, const vec2 & offset +) +	: UIObject(id, dimensions, offset), +	  data(data) {}  } // namespace crepe diff --git a/src/crepe/api/Button.h b/src/crepe/api/Button.h index d42527e..e986c04 100644 --- a/src/crepe/api/Button.h +++ b/src/crepe/api/Button.h @@ -1,8 +1,7 @@  #pragma once -#include <functional> +#include "../types.h" -#include "Event.h"  #include "UIObject.h"  namespace crepe { @@ -21,14 +20,24 @@ namespace crepe {   */  class Button : public UIObject {  public: +	struct Data { +		//! variable indicating if transform is relative to camera(false) or world(true) +		bool world_space = false; +	}; + +public:  	/**  	 * \brief Constructs a Button with the specified game object ID and dimensions.  	 *  	 * \param id The unique ID of the game object associated with this button.  	 * \param dimensions The width and height of the UIObject  	 * \param offset The offset relative this GameObjects Transform +	 * \param data additional data the button has  	 */ -	Button(game_object_id_t id, const vec2 & dimensions, const vec2 & offset); +	Button( +		game_object_id_t id, const vec2 & dimensions, const Data & data, +		const vec2 & offset = {0, 0} +	);  	/**  	 * \brief Get the maximum number of instances for this component  	 * @@ -38,6 +47,9 @@ public:  	 */  	virtual int get_instances_max() const { return 1; } +public: +	Data data; +  private:  	//! friend relation hover variable  	friend class InputSystem; diff --git a/src/crepe/api/Text.cpp b/src/crepe/api/Text.cpp index b24f0ac..e5b623d 100644 --- a/src/crepe/api/Text.cpp +++ b/src/crepe/api/Text.cpp @@ -1,10 +1,12 @@ +#include "../types.h" +  #include "Text.h"  using namespace crepe;  Text::Text( -	game_object_id_t id, const vec2 & dimensions, const vec2 & offset, -	const std::string & font_family, const Data & data, const std::string & text +	game_object_id_t id, const vec2 & dimensions, const std::string & font_family, +	const Data & data, const vec2 & offset, const std::string & text  )  	: UIObject(id, dimensions, offset),  	  text(text), diff --git a/src/crepe/api/Text.h b/src/crepe/api/Text.h index 0289b85..8b3d53e 100644 --- a/src/crepe/api/Text.h +++ b/src/crepe/api/Text.h @@ -3,6 +3,8 @@  #include <optional>  #include <string> +#include "../types.h" +  #include "Asset.h"  #include "Color.h"  #include "UIObject.h" @@ -17,22 +19,8 @@ class Text : public UIObject {  public:  	//! Text data that does not have to be set in the constructor  	struct Data { -		/** -		 *  \brief fontsize for text rendering -		 *  -		 * \note this is not the actual font size that is loaded in. -		 *  -		 * Since SDL_TTF requires the font size when loading in the font it is not possible to switch the font size. -		 * The default font size that is loaded is set in the Config. -		 * Instead this value is used to upscale the font texture which can cause blurring or distorted text when upscaling or downscaling too much. -		 */ -		unsigned int font_size = 16; - -		//! Layer sorting level of the text -		const int sorting_in_layer = 0; - -		//! Order within the sorting text -		const int order_in_layer = 0; +		//! variable indicating if transform is relative to camera(false) or world(true) +		bool world_space = false;  		//! Label text color.  		Color text_color = Color::BLACK; @@ -49,8 +37,8 @@ public:  	 * \param font Optional font asset that can be passed or left empty.  	 */  	Text( -		game_object_id_t id, const vec2 & dimensions, const vec2 & offset, -		const std::string & font_family, const Data & data, const std::string & text = "" +		game_object_id_t id, const vec2 & dimensions, const std::string & font_family, +		const Data & data, const vec2 & offset = {0, 0}, const std::string & text = ""  	);  	//! Label text. diff --git a/src/crepe/api/UIObject.h b/src/crepe/api/UIObject.h index f1318ab..0d9b1f7 100644 --- a/src/crepe/api/UIObject.h +++ b/src/crepe/api/UIObject.h @@ -15,13 +15,11 @@ public:  	 * \param dimensions width and height of the UIObject  	 * \param offset Offset relative to the GameObject Transform  	 */ -	UIObject(game_object_id_t id, const vec2 & dimensions, const vec2 & offset); +	UIObject(game_object_id_t id, const vec2 & dimensions, const vec2 & offset = {0, 0});  	//! Width and height of the UIObject  	vec2 dimensions;  	//! Position offset relative to this GameObjects Transform  	vec2 offset; -	//! variable indicating if transform is relative to camera(false) or world(true) -	bool world_space = false;  };  } // namespace crepe diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 859f966..6c93fb2 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -69,8 +69,6 @@ SDLContext::SDLContext(Mediator & mediator) {  		throw runtime_error(format("SDL_ttf initialization failed: {}", TTF_GetError()));  	} -	SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1"); -  	mediator.sdl_context = *this;  } @@ -174,6 +172,7 @@ SDL_FRect SDLContext::get_dst_rect(const DestinationRectangleData & ctx) const {  }  void SDLContext::draw(const RenderContext & ctx) { +	SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "2");  	const Sprite::Data & data = ctx.sprite.data;  	SDL_RendererFlip render_flip  		= (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * data.flip.flip_x) @@ -206,6 +205,7 @@ void SDLContext::draw(const RenderContext & ctx) {  }  void SDLContext::draw_text(const RenderText & data) { +	SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "0");  	const Text & text = data.text;  	const Font & font = data.font; @@ -235,9 +235,16 @@ void SDLContext::draw_text(const RenderText & data) {  		= {tmp_font_texture, [](SDL_Texture * texture) { SDL_DestroyTexture(texture); }};  	vec2 size = text.dimensions * cam_aux_data.render_scale * data.transform.scale; -	vec2 screen_pos = (absoluut_pos - cam_aux_data.cam_pos + (cam_aux_data.zoomed_viewport) / 2 -					  ) * cam_aux_data.render_scale -					  - size / 2 + cam_aux_data.bar_size; +	vec2 screen_pos = absoluut_pos; +	if (text.data.world_space) { +		screen_pos = (screen_pos - cam_aux_data.cam_pos + (cam_aux_data.zoomed_viewport) / 2) +						 * cam_aux_data.render_scale +					 - size / 2 + cam_aux_data.bar_size; +	} else { +		screen_pos +			= (screen_pos + (cam_aux_data.zoomed_viewport) / 2) * cam_aux_data.render_scale +			  - size / 2 + cam_aux_data.bar_size; +	}  	SDL_FRect dstrect {  		.x = screen_pos.x, diff --git a/src/crepe/system/AnimatorSystem.cpp b/src/crepe/system/AnimatorSystem.cpp index e5ab2fa..143d5d6 100644 --- a/src/crepe/system/AnimatorSystem.cpp +++ b/src/crepe/system/AnimatorSystem.cpp @@ -1,7 +1,8 @@ +#include <chrono> +  #include "../api/Animator.h"  #include "../manager/ComponentManager.h"  #include "../manager/LoopTimerManager.h" -#include <chrono>  #include "AnimatorSystem.h" @@ -13,28 +14,31 @@ 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_time = timer.get_delta_time();  	for (Animator & a : animations) {  		if (!a.active) continue;  		if (a.data.fps == 0) continue;  		Animator::Data & ctx = a.data; -		float frame_duration = 1.0f / ctx.fps; -		int last_frame = ctx.row; +		a.elapsed_time += elapsed_time; +		duration_t frame_duration = 1000ms / ctx.fps;  		int cycle_end = (ctx.cycle_end == -1) ? a.grid_size.x : ctx.cycle_end; -		int total_frames = cycle_end - ctx.cycle_start; - -		int curr_frame = static_cast<int>(elapsed_time / frame_duration) % total_frames; +		if (a.elapsed_time >= frame_duration) { +			a.elapsed_time = 0ms; +			a.frame++; +			if (a.frame == cycle_end) { +				a.frame = ctx.cycle_start; +				if (!ctx.looping) { +					a.active = false; +					continue; +				} +			} +		} -		ctx.row = ctx.cycle_start + curr_frame; +		ctx.row = ctx.cycle_start + a.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; -		}  	}  } diff --git a/src/crepe/system/InputSystem.cpp b/src/crepe/system/InputSystem.cpp index b4a0633..be7eda6 100644 --- a/src/crepe/system/InputSystem.cpp +++ b/src/crepe/system/InputSystem.cpp @@ -1,8 +1,8 @@  #include "../api/Button.h" +#include "../api/Config.h"  #include "../facade/SDLContext.h"  #include "../manager/ComponentManager.h"  #include "../manager/EventManager.h" -#include "util/Log.h"  #include "InputSystem.h" @@ -213,10 +213,10 @@ bool InputSystem::is_mouse_inside_button(  	const Transform & cam_transform  ) {  	vec2 actual_pos = transform.position + button.offset; -	if (!button.world_space) { +	if (!button.data.world_space) {  		actual_pos += cam_transform.position;  	} -	vec2 half_dimensions = button.dimensions / 2; +	vec2 half_dimensions = button.dimensions * transform.scale / 2;  	return mouse_pos.x >= actual_pos.x - half_dimensions.x  		   && mouse_pos.x <= actual_pos.x + half_dimensions.x diff --git a/src/crepe/system/InputSystem.h b/src/crepe/system/InputSystem.h index 37311cc..be62367 100644 --- a/src/crepe/system/InputSystem.h +++ b/src/crepe/system/InputSystem.h @@ -1,12 +1,9 @@  #pragma once -#include "../api/Config.h" -#include "../facade/EventData.h" -  #include "../api/Event.h"  #include "../api/Metadata.h" +#include "../facade/EventData.h"  #include "../types.h" -#include "../util/OptionalRef.h"  #include "System.h" diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index 7942fce..e6b31a7 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -7,8 +7,8 @@  #include <crepe/api/Button.h>  #include <crepe/api/Camera.h>  #include <crepe/api/Color.h> +#include <crepe/api/Engine.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> @@ -27,7 +27,7 @@ public:  		Color color(255, 255, 255, 255); -		Asset img {"asset/texture/square.png"}; +		Asset img {"asset/spritesheet/pokemon_spritesheet.png"};  		Sprite & test_sprite = game_object.add_component<Sprite>(  			img, @@ -36,24 +36,24 @@ public:  				.flip = Sprite::FlipSettings {false, false},  				.sorting_in_layer = 2,  				.order_in_layer = 2, -				.size = {1, 1}, +				.size = {1, 0},  				.angle_offset = 0,  				.position_offset = {0, 1},  				.world_space = false,  			}  		); -		//auto & emitter			= game_object.add_component<ParticleEmitter>(test_sprite, ParticleEmitter::Data{}); -		Sprite & test_sprite1 = game_object.add_component<Sprite>( -			img, -			Sprite::Data { -				.color = color, -				.size = {1, 1}, -				.position_offset = {0, -1}, -				.world_space = false, +		auto & anim = game_object.add_component<Animator>( +			test_sprite, ivec2 {56, 56}, uvec2 {4, 4}, +			Animator::Data { +				.looping = 0,  			}  		); +		anim.set_anim(1); +		anim.pause(); +		anim.next_anim(); +  		auto & cam = game_object.add_component<Camera>(  			ivec2 {1280, 720}, vec2 {5, 5},  			Camera::Data { @@ -61,22 +61,14 @@ public:  				.postion_offset = {1000, 1000},  			}  		); - -		/* -		game_object.add_component<Text>(vec2{1, 1}, vec2{0, -0.5}, "ComicSansMS", -										Text::Data{.text_color = Color::RED}, "test TEST"); - -		game_object.add_component<Text>(vec2{1, 1}, vec2{0, 0.5}, "ComicSansMS", -										Text::Data{.text_color = Color::BLACK}, "TEST test"); -		*/  	}  	string get_name() const { return "TestScene"; };  };  int main(int argc, char * argv[]) { -	LoopManager engine; +	Engine engine;  	engine.add_scene<TestScene>(); -	engine.start(); +	engine.main();  	return 0;  } |