From: Stephen Seo Date: Sun, 4 Oct 2020 04:25:51 +0000 (+0900) Subject: Preserve door states, add door sfx X-Git-Url: https://git.seodisparate.com/gitweb?a=commitdiff_plain;h=b356262ac25db4f28b85caaf6d928631f7d281b3;p=LudumDare47_StuckInALoop Preserve door states, add door sfx --- diff --git a/resources/door.ogg b/resources/door.ogg new file mode 100644 index 0000000..0c47ab1 Binary files /dev/null and b/resources/door.ogg differ diff --git a/src/scenes/mainscene.rs b/src/scenes/mainscene.rs index 79cf0c7..4d814f2 100644 --- a/src/scenes/mainscene.rs +++ b/src/scenes/mainscene.rs @@ -1,4 +1,5 @@ use std::cell::RefCell; +use std::collections::HashMap; use std::rc::Rc; use ggez::audio::{SoundSource, Source}; @@ -41,6 +42,11 @@ enum WalkingState { Right, } +#[derive(Copy, Clone, PartialEq, Eq, Hash)] +enum DoorIDs { + LeftOfPod, +} + pub struct MainScene { font: Font, player: Rc>, @@ -65,19 +71,24 @@ pub struct MainScene { interact_text: Text, doors: Vec, door_text: Text, + door_sfx: Source, + door_states: HashMap, } impl MainScene { pub fn new(ctx: &mut Context, font: Font, player: Rc>) -> Self { let mut music = Source::new(ctx, "/music00.ogg").unwrap(); music.set_repeat(true); - // music.play().unwrap(); let mut current_text = Text::new(""); current_text.set_font(font, Scale::uniform(26f32)); let mut interact_text = Text::new("[E] or Left Click to Interact"); interact_text.set_font(font, Scale::uniform(20f32)); let mut door_text = Text::new("[W] or Right Click to enter door"); door_text.set_font(font, Scale::uniform(20f32)); + + let door_states = HashMap::new(); + // door_states.insert(DoorIDs::LeftOfPod, false); + Self { font, player, @@ -102,6 +113,8 @@ impl MainScene { interact_text, doors: Vec::new(), door_text, + door_sfx: Source::new(ctx, "/door.ogg").unwrap(), + door_states, } } @@ -131,6 +144,9 @@ impl MainScene { self.doors.clear(); self.doors .push(Door::new(false, 300f32, 600f32 - 160f32 - 50f32, 0)); + if let Some(true) = self.door_states.get(&DoorIDs::LeftOfPod) { + self.doors[0].set_open(true); + } } } } @@ -189,12 +205,25 @@ impl MainScene { } } - fn use_interactable(&mut self, itype: InteractableType) { + fn use_interactable(&mut self, itype: InteractableType) -> GameResult<()> { match itype { InteractableType::Door(id) => { - self.doors[id].toggle_open(); + match self.room { + Room::StasisPod => (), + Room::LeftOfPod => { + if self.door_states.contains_key(&DoorIDs::LeftOfPod) { + *self.door_states.get_mut(&DoorIDs::LeftOfPod).unwrap() = + self.doors[id].toggle_open(); + } else { + self.door_states + .insert(DoorIDs::LeftOfPod, self.doors[id].toggle_open()); + } + } + } + self.door_sfx.play()?; } } + Ok(()) } } @@ -449,7 +478,7 @@ impl EventHandler for MainScene { } } if let Some(it) = itype { - self.use_interactable(it); + self.use_interactable(it).unwrap(); } else if self.player.borrow().x > x { self.walking_state = WalkingState::Left; } else if self.player.borrow().x + 64f32 < x { @@ -513,7 +542,7 @@ impl EventHandler for MainScene { } } if let Some(it) = itype { - self.use_interactable(it); + self.use_interactable(it).unwrap(); } } }