From 8b8f94ae7ff9133beacfd44d8a53a162adda6537 Mon Sep 17 00:00:00 2001 From: Corwin Date: Tue, 29 Aug 2023 21:44:28 +0100 Subject: [PATCH] teleporters --- examples/the-dungeon-puzzlers-lament/build.rs | 3 ++ .../src/game/simulation/entity.rs | 50 +++++++++++++++---- .../the-dungeon-puzzlers-lament/src/sfx.rs | 2 + 3 files changed, 46 insertions(+), 9 deletions(-) diff --git a/examples/the-dungeon-puzzlers-lament/build.rs b/examples/the-dungeon-puzzlers-lament/build.rs index 184c26ef..6ff0d31c 100644 --- a/examples/the-dungeon-puzzlers-lament/build.rs +++ b/examples/the-dungeon-puzzlers-lament/build.rs @@ -121,6 +121,7 @@ enum Entity { Ice, MovableBlock, Glove, + Teleporter, } impl FromStr for Entity { @@ -147,6 +148,7 @@ impl FromStr for Entity { "ICE" => Ice, "BLOCK" => MovableBlock, "GLOVE" => Glove, + "TELEPORTER" => Teleporter, _ => return Err(()), }) } @@ -174,6 +176,7 @@ impl quote::ToTokens for Entity { Ice => quote!(Item::Ice), MovableBlock => quote!(Item::MovableBlock), Glove => quote!(Item::Glove), + Teleporter => quote!(Item::Teleporter), }) } } diff --git a/examples/the-dungeon-puzzlers-lament/src/game/simulation/entity.rs b/examples/the-dungeon-puzzlers-lament/src/game/simulation/entity.rs index 35835401..48a7208c 100644 --- a/examples/the-dungeon-puzzlers-lament/src/game/simulation/entity.rs +++ b/examples/the-dungeon-puzzlers-lament/src/game/simulation/entity.rs @@ -44,6 +44,21 @@ impl ActionResult { } } +fn remove_move_animation_for_entity( + animations: &mut Vec, + entity_key: EntityKey, +) { + if let Some(existing_animation) = animations.iter().position(|x| { + if let AnimationInstruction::Move(entity, _, _) = x { + *entity == entity_key + } else { + false + } + }) { + animations.swap_remove(existing_animation); + } +} + struct HasMoved(bool); struct WantsToMoveAgain(bool); @@ -338,17 +353,32 @@ impl EntityMap { break; } OverlapResolution::MoveAgain => { - if let Some(existing_animation) = animations.iter().position(|x| { - if let AnimationInstruction::Move(entity, _, _) = x { - *entity == entity_to_update_key - } else { - false - } - }) { - animations.swap_remove(existing_animation); - } + remove_move_animation_for_entity(animations, entity_to_update_key); should_move_again = true; } + OverlapResolution::Teleport => { + // find other teleporter + let other_teleporter = self.map.iter().find(|(entity_key, entity)| { + *entity_key != other_entity_key + && matches!(entity.entity, EntityType::Teleporter) + }); + + if let Some((_other_teleporter_key, other_teleporter)) = other_teleporter { + let location_to_teleport_to = other_teleporter.location; + if self.whats_at(location_to_teleport_to).count() != 0 { + //ok, we can teleport + remove_move_animation_for_entity(animations, entity_to_update_key); + animations.push(AnimationInstruction::Move( + entity_to_update_key, + location_to_teleport_to, + Some(SoundEffect::TeleportEffect), + )); + if let Some(entity) = self.map.get_mut(entity_to_update_key) { + entity.location = location_to_teleport_to; + } + } + } + } } } ( @@ -440,6 +470,7 @@ enum OverlapResolution { ToggleSystem(SwitchSystem), Die, MoveAgain, + Teleport, } fn resolve_spikes(switable: &Switchable) -> OverlapResolution { @@ -461,6 +492,7 @@ fn resolve_overlap(me: &Entity, other: &Entity) -> OverlapResolution { (_, EntityType::Switch(switch)) => OverlapResolution::ToggleSystem(switch.system), (_, EntityType::Enemy(_) | EntityType::Hero(_)) => OverlapResolution::Die, (_, EntityType::Ice) => OverlapResolution::MoveAgain, + (_, EntityType::Teleporter) => OverlapResolution::Teleport, _ => OverlapResolution::CoExist, } diff --git a/examples/the-dungeon-puzzlers-lament/src/sfx.rs b/examples/the-dungeon-puzzlers-lament/src/sfx.rs index b4b9cd18..47c1c1ea 100644 --- a/examples/the-dungeon-puzzlers-lament/src/sfx.rs +++ b/examples/the-dungeon-puzzlers-lament/src/sfx.rs @@ -74,6 +74,7 @@ impl<'a> Sfx<'a> { SoundEffect::SwordDrop => {} SoundEffect::SwitchedDoorToggle => {} SoundEffect::SpikesToggle => {} + SoundEffect::TeleportEffect => {} } } } @@ -94,4 +95,5 @@ pub enum SoundEffect { SwitchedDoorToggle, SpikesToggle, WallHit, + TeleportEffect, }