From 900cd007df62f1316330344ebfbdc1a9ad49868f Mon Sep 17 00:00:00 2001 From: Corwin Date: Sat, 5 Mar 2022 17:50:37 +0000 Subject: [PATCH] pull out constants --- agb/src/display/object.rs | 9 +++- .../the-hat-chooses-the-wizard/src/enemies.rs | 44 ++++++++--------- .../the-hat-chooses-the-wizard/src/main.rs | 48 +++++++++---------- 3 files changed, 50 insertions(+), 51 deletions(-) diff --git a/agb/src/display/object.rs b/agb/src/display/object.rs index b4ca788e..7398cf59 100644 --- a/agb/src/display/object.rs +++ b/agb/src/display/object.rs @@ -99,7 +99,7 @@ impl TagMap { pub const fn new(tags: &'static [(&'static str, Tag)]) -> TagMap { Self { tags } } - pub const fn get(&'static self, tag: &str) -> Option<&'static Tag> { + pub const fn try_get(&'static self, tag: &str) -> Option<&'static Tag> { let mut i = 0; while i < self.tags.len() { let s = self.tags[i].0; @@ -112,6 +112,13 @@ impl TagMap { None } + pub const fn get(&'static self, tag: &str) -> &'static Tag { + let t = self.try_get(tag); + match t { + Some(t) => t, + None => panic!("The requested tag does not exist"), + } + } pub fn values(&self) -> impl Iterator { self.tags.iter().map(|x| &x.1) } diff --git a/examples/the-hat-chooses-the-wizard/src/enemies.rs b/examples/the-hat-chooses-the-wizard/src/enemies.rs index c30c0ebe..da9df438 100644 --- a/examples/the-hat-chooses-the-wizard/src/enemies.rs +++ b/examples/the-hat-chooses-the-wizard/src/enemies.rs @@ -2,10 +2,19 @@ use crate::TAG_MAP; use super::{sfx::SfxPlayer, Entity, FixedNumberType, HatState, Level}; use agb::{ - display::object::{ObjectController, Size}, + display::object::{ObjectController, Size, Tag}, fixnum::Vector2D, }; +const SLIME_IDLE: &Tag = TAG_MAP.get("Slime Idle"); +const SLIME_JUMP: &Tag = TAG_MAP.get("Slime Jump"); +const SLIME_SPLAT: &Tag = TAG_MAP.get("Slime splat"); + +const SNAIL_EMERGE: &Tag = TAG_MAP.get("Snail Emerge"); +const SNAIL_MOVE: &Tag = TAG_MAP.get("Snail Move"); +const SNAIL_DEATH: &Tag = TAG_MAP.get("Snail Death"); +const SNAIL_IDLE: &Tag = TAG_MAP.get("Snail Idle"); + enum UpdateState { Nothing, KillPlayer, @@ -155,8 +164,7 @@ impl<'a> Slime<'a> { SlimeState::Idle => { let offset = (timer / 16) as usize; - let tag = TAG_MAP.get("Slime Idle").unwrap(); - let frame = tag.get_animation_sprite(offset); + let frame = SLIME_IDLE.get_animation_sprite(offset); let sprite = controller.get_sprite(frame).unwrap(); self.enemy_info.entity.sprite.set_sprite(sprite); @@ -196,8 +204,7 @@ impl<'a> Slime<'a> { self.enemy_info.entity.velocity = (0, 0).into(); self.state = SlimeState::Idle; } else { - let tag = TAG_MAP.get("Slime Jump").unwrap(); - let frame = tag.get_animation_sprite(offset); + let frame = SLIME_JUMP.get_animation_sprite(offset); let sprite = controller.get_sprite(frame).unwrap(); self.enemy_info.entity.sprite.set_sprite(sprite); @@ -223,8 +230,7 @@ impl<'a> Slime<'a> { return UpdateState::Remove; } - let tag = TAG_MAP.get("Slime splat").unwrap(); - let frame = tag.get_animation_sprite(offset); + let frame = SLIME_SPLAT.get_animation_sprite(offset); let sprite = controller.get_sprite(frame).unwrap(); self.enemy_info.entity.sprite.set_sprite(sprite); @@ -295,8 +301,7 @@ impl<'a> Snail<'a> { } } - let tag = TAG_MAP.get("Snail Idle").unwrap(); - let frame = tag.get_animation_sprite(0); + let frame = SNAIL_IDLE.get_animation_sprite(0); let sprite = controller.get_sprite(frame).unwrap(); self.enemy_info.entity.sprite.set_sprite(sprite); @@ -316,8 +321,7 @@ impl<'a> Snail<'a> { } self.enemy_info.entity.velocity = (0, 0).into(); - let tag = TAG_MAP.get("Snail Emerge").unwrap(); - let frame = tag.get_animation_sprite(offset); + let frame = SNAIL_EMERGE.get_animation_sprite(offset); let sprite = controller.get_sprite(frame).unwrap(); self.enemy_info.entity.sprite.set_sprite(sprite); @@ -339,8 +343,7 @@ impl<'a> Snail<'a> { let offset = (timer - time) as usize / 8; - let tag = TAG_MAP.get("Snail Move").unwrap(); - let frame = tag.get_animation_sprite(offset); + let frame = SNAIL_MOVE.get_animation_sprite(offset); let sprite = controller.get_sprite(frame).unwrap(); self.enemy_info.entity.sprite.set_sprite(sprite); @@ -374,8 +377,7 @@ impl<'a> Snail<'a> { self.state = SnailState::Idle(timer); } - let tag = TAG_MAP.get("Snail Emerge").unwrap(); - let frame = tag.get_animation_sprite(offset); + let frame = SNAIL_EMERGE.get_animation_sprite(offset); let sprite = controller.get_sprite(frame).unwrap(); self.enemy_info.entity.sprite.set_sprite(sprite); @@ -396,17 +398,11 @@ impl<'a> Snail<'a> { let offset = (timer - time) as usize / 4; let frame = if offset < 5 { - TAG_MAP - .get("Snail Emerge") - .unwrap() - .get_animation_sprite(5 - offset) + SNAIL_EMERGE.get_animation_sprite(5 - offset) } else if offset == 5 { - TAG_MAP.get("Snail Idle").unwrap().get_animation_sprite(0) + SNAIL_IDLE.get_animation_sprite(0) } else if offset < 5 + 7 { - TAG_MAP - .get("Snail Death") - .unwrap() - .get_animation_sprite(offset - 5) + SNAIL_DEATH.get_animation_sprite(offset - 5) } else { return UpdateState::Remove; }; diff --git a/examples/the-hat-chooses-the-wizard/src/main.rs b/examples/the-hat-chooses-the-wizard/src/main.rs index 262154e2..ef347aea 100644 --- a/examples/the-hat-chooses-the-wizard/src/main.rs +++ b/examples/the-hat-chooses-the-wizard/src/main.rs @@ -85,7 +85,7 @@ agb::include_gfx!("gfx/tile_sheet.toml"); use agb::{ display::{ background::BackgroundRegular, - object::{Object, ObjectController, Sprite, TagMap}, + object::{Object, ObjectController, Sprite, Tag, TagMap}, Priority, HEIGHT, WIDTH, }, fixnum::{FixedNum, Vector2D}, @@ -93,9 +93,16 @@ use agb::{ }; const SPRITE_TAGS: (&[Sprite], &TagMap) = agb::include_aseprite!("gfx/sprites.aseprite"); -const SPRITES: &[Sprite] = SPRITE_TAGS.0; const TAG_MAP: &TagMap = SPRITE_TAGS.1; +const WALKING: &Tag = TAG_MAP.get("Walking"); +const JUMPING: &Tag = TAG_MAP.get("Jumping"); +const FALLING: &Tag = TAG_MAP.get("Falling"); +const PLAYER_DEATH: &Tag = TAG_MAP.get("Player Death"); +const HAT_SPIN_1: &Tag = TAG_MAP.get("HatSpin"); +const HAT_SPIN_2: &Tag = TAG_MAP.get("HatSpin2"); +const HAT_SPIN_3: &Tag = TAG_MAP.get("HatSpin3"); + type FixedNumberType = FixedNum<10>; pub struct Entity<'a> { @@ -107,9 +114,7 @@ pub struct Entity<'a> { impl<'a> Entity<'a> { pub fn new(object: &'a ObjectController, collision_mask: Vector2D) -> Self { - let dummy_sprite = object - .get_sprite(TAG_MAP.get("Walking").unwrap().get_sprite(0)) - .unwrap(); + let dummy_sprite = object.get_sprite(WALKING.get_sprite(0)).unwrap(); let mut sprite = object.get_object(dummy_sprite).unwrap(); sprite.set_priority(Priority::P1); Entity { @@ -350,16 +355,11 @@ impl<'a> Player<'a> { let mut wizard = Entity::new(controller, (6_u16, 14_u16).into()); let mut hat = Entity::new(controller, (6_u16, 6_u16).into()); - wizard.sprite.set_sprite( - controller - .get_sprite(TAG_MAP.get("Walking").unwrap().get_sprite(0)) - .unwrap(), - ); - hat.sprite.set_sprite( - controller - .get_sprite(TAG_MAP.get("HatSpin").unwrap().get_sprite(0)) - .unwrap(), - ); + wizard + .sprite + .set_sprite(controller.get_sprite(HAT_SPIN_1.get_sprite(0)).unwrap()); + hat.sprite + .set_sprite(controller.get_sprite(HAT_SPIN_1.get_sprite(0)).unwrap()); wizard.sprite.show(); hat.sprite.show(); @@ -464,8 +464,7 @@ impl<'a> Player<'a> { let offset = (ping_pong(timer / 16, 4)) as usize; self.wizard_frame = offset as u8; - let walk = TAG_MAP.get("Walking").unwrap(); - let frame = walk.get_animation_sprite(offset); + let frame = WALKING.get_animation_sprite(offset); let sprite = controller.get_sprite(frame).unwrap(); self.wizard.sprite.set_sprite(sprite); @@ -475,8 +474,7 @@ impl<'a> Player<'a> { // going up self.wizard_frame = 5; - let walk = TAG_MAP.get("Jumping").unwrap(); - let frame = walk.get_animation_sprite(0); + let frame = JUMPING.get_animation_sprite(0); let sprite = controller.get_sprite(frame).unwrap(); self.wizard.sprite.set_sprite(sprite); @@ -491,8 +489,7 @@ impl<'a> Player<'a> { self.wizard_frame = 0; - let walk = TAG_MAP.get("Falling").unwrap(); - let frame = walk.get_animation_sprite(offset); + let frame = FALLING.get_animation_sprite(offset); let sprite = controller.get_sprite(frame).unwrap(); self.wizard.sprite.set_sprite(sprite); @@ -504,9 +501,9 @@ impl<'a> Player<'a> { } let hat_base_tile = match self.num_recalls { - 0 => TAG_MAP.get("HatSpin").unwrap(), - 1 => TAG_MAP.get("HatSpin2").unwrap(), - _ => TAG_MAP.get("HatSpin3").unwrap(), + 0 => HAT_SPIN_1, + 1 => HAT_SPIN_2, + _ => HAT_SPIN_3, }; match self.facing { @@ -689,8 +686,7 @@ impl<'a, 'b, 'c> PlayingLevel<'a, 'b> { fn dead_update(&mut self, controller: &'a ObjectController) -> bool { self.timer += 1; - let tag = TAG_MAP.get("Player Death").unwrap(); - let frame = tag.get_animation_sprite(self.timer as usize / 8); + let frame = PLAYER_DEATH.get_animation_sprite(self.timer as usize / 8); let sprite = controller.get_sprite(frame).unwrap(); self.player.wizard.velocity += (0.into(), FixedNumberType::new(1) / 32).into();