diff options
author | Max-001 <maxsmits21@kpnmail.nl> | 2024-12-24 12:10:28 +0100 |
---|---|---|
committer | Max-001 <maxsmits21@kpnmail.nl> | 2024-12-24 12:10:28 +0100 |
commit | 192aea3112af0b36f6f26670e66c7e24926c579a (patch) | |
tree | ab7cf094850572d4f31e945d884339551cd8b08d | |
parent | 46cc8767338578cbacb5a61bf50bbca71ea8e538 (diff) |
Added collision animations
-rw-r--r-- | game/Config.h | 3 | ||||
-rw-r--r-- | game/GameScene.cpp | 50 | ||||
-rw-r--r-- | game/player/PlayerScript.cpp | 53 | ||||
-rw-r--r-- | game/player/PlayerScript.h | 5 | ||||
-rw-r--r-- | game/player/PlayerSubScene.cpp | 3 |
5 files changed, 113 insertions, 1 deletions
diff --git a/game/Config.h b/game/Config.h index ec753df..796ad6c 100644 --- a/game/Config.h +++ b/game/Config.h @@ -13,6 +13,9 @@ 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 diff --git a/game/GameScene.cpp b/game/GameScene.cpp index ee9b7dd..821191d 100644 --- a/game/GameScene.cpp +++ b/game/GameScene.cpp @@ -66,6 +66,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(3000, 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/player/PlayerScript.cpp b/game/player/PlayerScript.cpp index 04e7264..1a5d497 100644 --- a/game/player/PlayerScript.cpp +++ b/game/player/PlayerScript.cpp @@ -9,6 +9,59 @@ 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(); + 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; + 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; + 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; + 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 diff --git a/game/player/PlayerScript.h b/game/player/PlayerScript.h index dfd02eb..d8eb098 100644 --- a/game/player/PlayerScript.h +++ b/game/player/PlayerScript.h @@ -1,11 +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 index ccf3d42..812d99a 100644 --- a/game/player/PlayerSubScene.cpp +++ b/game/player/PlayerSubScene.cpp @@ -143,7 +143,8 @@ PlayerSubScene::PlayerSubScene(Scene & scn) { .gravity_scale = 20, .body_type = Rigidbody::BodyType::DYNAMIC, .linear_velocity = vec2(100, 0), - .collision_layers = {COLL_LAY_BOT_TOP}, + .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; |