diff options
Diffstat (limited to 'game/player')
-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 | 16 | ||||
-rw-r--r-- | game/player/PlayerSubScene.cpp | 153 | ||||
-rw-r--r-- | game/player/PlayerSubScene.h | 10 |
6 files changed, 408 insertions, 0 deletions
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/player/PlayerScript.h b/game/player/PlayerScript.h new file mode 100644 index 0000000..d8eb098 --- /dev/null +++ b/game/player/PlayerScript.h @@ -0,0 +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/player/PlayerSubScene.h b/game/player/PlayerSubScene.h new file mode 100644 index 0000000..bf94c32 --- /dev/null +++ b/game/player/PlayerSubScene.h @@ -0,0 +1,10 @@ +#pragma once + +namespace crepe { +class Scene; +} + +class PlayerSubScene { +public: + PlayerSubScene(crepe::Scene & scn); +}; |