diff options
| -rw-r--r-- | game/Config.h | 16 | ||||
| -rw-r--r-- | game/EngineConfig.h | 19 | ||||
| -rw-r--r-- | game/background/StartSubScene.cpp | 48 | ||||
| -rw-r--r-- | game/main.cpp | 2 | ||||
| -rw-r--r-- | game/player/PlayerScript.cpp | 39 | ||||
| -rw-r--r-- | game/player/PlayerScript.h | 9 | ||||
| -rw-r--r-- | game/player/PlayerSubScene.cpp | 2 | 
7 files changed, 94 insertions, 41 deletions
| diff --git a/game/Config.h b/game/Config.h index 8fa41ba..c6bbfda 100644 --- a/game/Config.h +++ b/game/Config.h @@ -1,16 +1,6 @@  #pragma once -#include "types.h" - -#include <crepe/api/Config.h> -static const crepe::Config ENGINE_CONFIG { -	.log { -		.level = crepe::Log::Level::DEBUG, -	}, -	.window_settings { -		.window_title = "Jetpack joyride clone", -	}, -}; +#include "types.h"  static constexpr int SORT_IN_LAY_BACK_BACKGROUND = 3; // For all scenes  static constexpr int SORT_IN_LAY_BACKGROUND = 4; // For all scenes @@ -59,7 +49,9 @@ static constexpr const char * DISTANCE_RUN = "distance_run";  // Player config  static constexpr const char * PLAYER_NAME = "player";  static constexpr int PLAYER_SPEED = 7500; // In game units -static constexpr int PLAYER_GRAVITY_SCALE = 60; // In game units +static constexpr float PLAYER_GRAVITY_SCALE = 3; // factor +static constexpr float PLAYER_HELP_KICK_SCALE = 0.2; // factor +static constexpr float PLAYER_HELP_KICK_MAX = 0.3; // factor  static constexpr const char * CAMERA_NAME = "camera";  // Jetpack particles diff --git a/game/EngineConfig.h b/game/EngineConfig.h new file mode 100644 index 0000000..6a03a14 --- /dev/null +++ b/game/EngineConfig.h @@ -0,0 +1,19 @@ +#pragma once + +#include "Config.h" + +#include <crepe/api/Config.h> + +static const crepe::Config ENGINE_CONFIG { +	.log { +		.level = crepe::Log::Level::DEBUG, +	}, +	.physics { +		// this division factor is now the amount of seconds it approximately takes to naturally +		// fall from the ceiling to floor +		.gravity = HALLWAY_HEIGHT / 0.5, +	}, +	.window_settings { +		.window_title = "Jetpack joyride clone", +	}, +}; diff --git a/game/background/StartSubScene.cpp b/game/background/StartSubScene.cpp index d68287b..d2d30ea 100644 --- a/game/background/StartSubScene.cpp +++ b/game/background/StartSubScene.cpp @@ -199,8 +199,8 @@ void StartSubScene::create_wall_fragments(crepe::Scene & scn, float begin_x) {  	);  	frag_1_sprite.active = false;  	Rigidbody & frag_1_rb = frag_1.add_component<Rigidbody>(Rigidbody::Data { -		.gravity_scale = 10, -		.linear_velocity = vec2(400, 400), +		.gravity_scale = 1.0, +		.linear_velocity = vec2(400, -400),  		.linear_velocity_coefficient = vec2(0.5, 0.6),  		.angular_velocity = 500,  		.angular_velocity_coefficient = 0.55, @@ -223,8 +223,8 @@ void StartSubScene::create_wall_fragments(crepe::Scene & scn, float begin_x) {  	);  	frag_2_sprite.active = false;  	Rigidbody & frag_2_rb = frag_2.add_component<Rigidbody>(Rigidbody::Data { -		.gravity_scale = 20, -		.linear_velocity = vec2(400, 400), +		.gravity_scale = 1.0, +		.linear_velocity = vec2(400, -400),  		.linear_velocity_coefficient = vec2(0.35, 0.4),  		.angular_velocity = 400,  		.angular_velocity_coefficient = 0.55, @@ -247,8 +247,8 @@ void StartSubScene::create_wall_fragments(crepe::Scene & scn, float begin_x) {  	);  	frag_3_sprite.active = false;  	Rigidbody & frag_3_rb = frag_3.add_component<Rigidbody>(Rigidbody::Data { -		.gravity_scale = 30, -		.linear_velocity = vec2(400, 400), +		.gravity_scale = 1.0, +		.linear_velocity = vec2(400, -400),  		.linear_velocity_coefficient = vec2(0.3, 0.3),  		.angular_velocity = 300,  		.angular_velocity_coefficient = 0.55, @@ -271,8 +271,8 @@ void StartSubScene::create_wall_fragments(crepe::Scene & scn, float begin_x) {  	);  	frag_4_sprite.active = false;  	Rigidbody & frag_4_rb = frag_4.add_component<Rigidbody>(Rigidbody::Data { -		.gravity_scale = 40, -		.linear_velocity = vec2(700, 400), +		.gravity_scale = 1.0, +		.linear_velocity = vec2(700, -400),  		.linear_velocity_coefficient = vec2(0.2, 0.2),  		.angular_velocity = 200,  		.angular_velocity_coefficient = 0.55, @@ -295,8 +295,8 @@ void StartSubScene::create_wall_fragments(crepe::Scene & scn, float begin_x) {  	);  	frag_5_sprite.active = false;  	Rigidbody & frag_5_rb = frag_5.add_component<Rigidbody>(Rigidbody::Data { -		.gravity_scale = 50, -		.linear_velocity = vec2(600, 800), +		.gravity_scale = 1.0, +		.linear_velocity = vec2(600, -800),  		.linear_velocity_coefficient = vec2(0.25, 0.15),  		.angular_velocity = 100,  		.angular_velocity_coefficient = 0.55, @@ -319,8 +319,8 @@ void StartSubScene::create_wall_fragments(crepe::Scene & scn, float begin_x) {  	);  	frag_6_sprite.active = false;  	Rigidbody & frag_6_rb = frag_6.add_component<Rigidbody>(Rigidbody::Data { -		.gravity_scale = 30, -		.linear_velocity = vec2(300, 800), +		.gravity_scale = 1.0, +		.linear_velocity = vec2(300, -800),  		.linear_velocity_coefficient = vec2(0.35, 0.25),  		.angular_velocity = 100,  		.angular_velocity_coefficient = 0.55, @@ -343,8 +343,8 @@ void StartSubScene::create_wall_fragments(crepe::Scene & scn, float begin_x) {  	);  	frag_7_sprite.active = false;  	Rigidbody & frag_7_rb = frag_7.add_component<Rigidbody>(Rigidbody::Data { -		.gravity_scale = 20, -		.linear_velocity = vec2(400, 500), +		.gravity_scale = 1.0, +		.linear_velocity = vec2(400, -500),  		.linear_velocity_coefficient = vec2(0.45, 0.6),  		.angular_velocity = 800,  		.angular_velocity_coefficient = 0.55, @@ -367,8 +367,8 @@ void StartSubScene::create_wall_fragments(crepe::Scene & scn, float begin_x) {  	);  	frag_8_sprite.active = false;  	Rigidbody & frag_8_rb = frag_8.add_component<Rigidbody>(Rigidbody::Data { -		.gravity_scale = 30, -		.linear_velocity = vec2(400, 400), +		.gravity_scale = 1.0, +		.linear_velocity = vec2(400, -400),  		.linear_velocity_coefficient = vec2(0.5, 0.6),  		.angular_velocity = 500,  		.angular_velocity_coefficient = 0.55, @@ -391,8 +391,8 @@ void StartSubScene::create_wall_fragments(crepe::Scene & scn, float begin_x) {  	);  	frag_9_sprite.active = false;  	Rigidbody & frag_9_rb = frag_9.add_component<Rigidbody>(Rigidbody::Data { -		.gravity_scale = 40, -		.linear_velocity = vec2(200, 400), +		.gravity_scale = 1.0, +		.linear_velocity = vec2(200, -400),  		.linear_velocity_coefficient = vec2(0.5, 0.25),  		.angular_velocity = 500,  		.angular_velocity_coefficient = 0.55, @@ -415,8 +415,8 @@ void StartSubScene::create_wall_fragments(crepe::Scene & scn, float begin_x) {  	);  	frag_10_sprite.active = false;  	Rigidbody & frag_10_rb = frag_10.add_component<Rigidbody>(Rigidbody::Data { -		.gravity_scale = 50, -		.linear_velocity = vec2(400, 900), +		.gravity_scale = 1.0, +		.linear_velocity = vec2(400, -900),  		.linear_velocity_coefficient = vec2(0.35, 0.4),  		.angular_velocity = 300,  		.angular_velocity_coefficient = 0.55, @@ -439,8 +439,8 @@ void StartSubScene::create_wall_fragments(crepe::Scene & scn, float begin_x) {  	);  	frag_11_sprite.active = false;  	Rigidbody & frag_11_rb = frag_11.add_component<Rigidbody>(Rigidbody::Data { -		.gravity_scale = 60, -		.linear_velocity = vec2(600, 800), +		.gravity_scale = 1.0, +		.linear_velocity = vec2(600, -800),  		.linear_velocity_coefficient = vec2(0.3, 0.3),  		.angular_velocity = 200,  		.angular_velocity_coefficient = 0.55, @@ -463,8 +463,8 @@ void StartSubScene::create_wall_fragments(crepe::Scene & scn, float begin_x) {  	);  	frag_12_sprite.active = false;  	Rigidbody & frag_12_rb = frag_12.add_component<Rigidbody>(Rigidbody::Data { -		.gravity_scale = 70, -		.linear_velocity = vec2(500, 800), +		.gravity_scale = 1.0, +		.linear_velocity = vec2(500, -800),  		.linear_velocity_coefficient = vec2(0.25, 0.15),  		.angular_velocity = 100,  		.angular_velocity_coefficient = 0.55, diff --git a/game/main.cpp b/game/main.cpp index 751cbe5..95cb35c 100644 --- a/game/main.cpp +++ b/game/main.cpp @@ -3,7 +3,7 @@  #include <crepe/api/Engine.h>  #include <crepe/api/Script.h> -#include "Config.h" +#include "EngineConfig.h"  #include "GameScene.h"  #include "PreviewScene.h"  #include "menus/mainmenu/MainMenuScene.h" diff --git a/game/player/PlayerScript.cpp b/game/player/PlayerScript.cpp index fadca9c..c3843ca 100644 --- a/game/player/PlayerScript.cpp +++ b/game/player/PlayerScript.cpp @@ -18,8 +18,38 @@ void PlayerScript::init() {  	subscribe<CollisionEvent>([this](const CollisionEvent & ev) -> bool {  		return this->on_collision(ev);  	}); +	subscribe<KeyPressEvent>([this](const KeyPressEvent & ev) -> bool { +		if (ev.repeat) return false; +		return this->on_key_down(ev); +	}); +	subscribe<KeyReleaseEvent>([this](const KeyReleaseEvent & ev) -> bool { +		return this->on_key_up(ev); +	});  	this->last_fired = std::chrono::steady_clock::now(); +	this->body = get_component<Rigidbody>();  } + +bool PlayerScript::on_key_down(const KeyPressEvent & ev) { +	const vec2 UP = {0, -1}; +	this->help_kick(UP); +	return false; +} + +bool PlayerScript::on_key_up(const KeyReleaseEvent & ev) { +	const vec2 DOWN = {0, 1}; +	this->help_kick(DOWN); +	return false; +} + +void PlayerScript::help_kick(const vec2 & direction) { +	// softly "kick" the player (at start/end of flight) +	vec2 & velocity = this->body->data.linear_velocity; +	float kick_amount = std::min( +		velocity.length() * PLAYER_HELP_KICK_SCALE, engine_gravity * PLAYER_HELP_KICK_MAX +	); +	velocity += direction * kick_amount; +} +  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(); @@ -94,7 +124,7 @@ void PlayerScript::fixed_update(crepe::duration_t dt) {  		emitter.data.boundary.offset = vec2(0, -transform.position.y);  	} -	Rigidbody & rb = this->get_components_by_name<Rigidbody>("player").front(); +	Rigidbody & rb = this->body;  	if (this->get_key_state(Keycode::ENTER)) {  		auto now = std::chrono::steady_clock::now(); @@ -105,7 +135,10 @@ void PlayerScript::fixed_update(crepe::duration_t dt) {  		}  	}  	if (this->get_key_state(Keycode::SPACE)) { -		rb.add_force_linear(vec2(0, -PLAYER_GRAVITY_SCALE / 2.5) * dt.count() / 0.02); +		rb.add_force_linear( +			vec2(0, -1) * (engine_gravity * PLAYER_GRAVITY_SCALE * dt.count()) +		); +  		if (prev_anim != 1) {  			for (Animator & anim : animators) {  				anim.active = true; @@ -127,7 +160,7 @@ void PlayerScript::fixed_update(crepe::duration_t dt) {  			current_jetpack_sound = 0;  		}  	} else if (transform.position.y == 195) { -		Rigidbody & rb = this->get_components_by_name<Rigidbody>("player").front(); +		Rigidbody & rb = this->body;  		if (prev_anim != 0 && rb.data.linear_velocity.x != 0) {  			for (Animator & anim : animators) {  				anim.active = true; diff --git a/game/player/PlayerScript.h b/game/player/PlayerScript.h index 0b77caa..6a7dedb 100644 --- a/game/player/PlayerScript.h +++ b/game/player/PlayerScript.h @@ -1,8 +1,11 @@  #pragma once +#include "util/OptionalRef.h"  #include <chrono> +#include <crepe/api/Config.h>  #include <crepe/api/Event.h>  #include <crepe/api/Script.h> +  class PlayerScript : public crepe::Script {  public:  	void init(); @@ -10,8 +13,11 @@ public:  private:  	bool on_collision(const crepe::CollisionEvent & ev); +	bool on_key_down(const crepe::KeyPressEvent & ev); +	bool on_key_up(const crepe::KeyReleaseEvent & ev);  	// bool on_key_up(const crepe::KeyReleaseEvent& ev);  	void shoot(const crepe::vec2 & location, float angle); +	void help_kick(const crepe::vec2 & direction);  private:  	int prev_anim = 0; @@ -20,4 +26,7 @@ private:  	std::chrono::duration<float> shot_delay = std::chrono::duration<float>(0.5);  	std::chrono::duration<float> switch_delay = std::chrono::duration<float>(0.01);  	int current_jetpack_sound = 0; + +	float & engine_gravity = crepe::Config::get_instance().physics.gravity; +	crepe::OptionalRef<crepe::Rigidbody> body;  }; diff --git a/game/player/PlayerSubScene.cpp b/game/player/PlayerSubScene.cpp index dfcd763..4d17c47 100644 --- a/game/player/PlayerSubScene.cpp +++ b/game/player/PlayerSubScene.cpp @@ -145,7 +145,7 @@ PlayerSubScene::PlayerSubScene(Scene & scn) {  	);  	player.add_component<BoxCollider>(vec2(40, 50), vec2(-20, 0));  	player.add_component<Rigidbody>(Rigidbody::Data { -		.gravity_scale = PLAYER_GRAVITY_SCALE, +		.gravity_scale = 1.0,  		.body_type = Rigidbody::BodyType::DYNAMIC,  		.linear_velocity = vec2(PLAYER_SPEED * 0.02, 0),  		.collision_layers |