Merge pull request #183 from corwinkuiper/failable-objects

Failable objects
This commit is contained in:
Corwin 2022-03-12 20:13:39 +00:00 committed by GitHub
commit ebd5f308e0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 78 additions and 88 deletions

View file

@ -61,9 +61,9 @@ fn main(mut gba: agb::Gba) -> ! {
let object = gba.display.object.get();
let sprite = object.get_sprite(&ChickenSprites[0]).unwrap();
let sprite = object.get_sprite(&ChickenSprites[0]);
let mut chicken = Character {
object: object.get_object(sprite).unwrap(),
object: object.get_object(sprite),
position: Vector2D {
x: (6 * 8) << 8,
y: ((7 * 8) - 4) << 8,
@ -143,23 +143,19 @@ fn update_chicken_object<'a>(
State::Ground => {
if chicken.velocity.x.abs() > 1 << 4 {
chicken.object.set_sprite(
object
.get_sprite(&ChickenSprites[frame_ranger(frame_count, 1, 3, 10)])
.unwrap(),
object.get_sprite(&ChickenSprites[frame_ranger(frame_count, 1, 3, 10)]),
);
} else {
chicken
.object
.set_sprite(object.get_sprite(&ChickenSprites[0]).unwrap());
.set_sprite(object.get_sprite(&ChickenSprites[0]));
}
}
State::Upwards => {}
State::Flapping => {
chicken.object.set_sprite(
object
.get_sprite(&ChickenSprites[frame_ranger(frame_count, 4, 5, 5)])
.unwrap(),
);
chicken
.object
.set_sprite(object.get_sprite(&ChickenSprites[frame_ranger(frame_count, 4, 5, 5)]));
}
}

View file

@ -19,9 +19,7 @@ fn all_sprites(gfx: &ObjectController) {
for y in 0..9 {
for x in 0..14 {
let mut obj = gfx
.get_object(gfx.get_sprite(&SPRITES[0]).unwrap())
.unwrap();
let mut obj = gfx.get_object(gfx.get_sprite(&SPRITES[0]));
obj.show();
obj.set_position((x * 16 + 8, y * 16 + 8).into());
objs.push(obj);
@ -49,7 +47,7 @@ fn all_sprites(gfx: &ObjectController) {
let objs_len = objs.len();
for (i, obj) in objs.iter_mut().enumerate() {
let this_image = (image + i * SPRITES.len() / objs_len) % SPRITES.len();
obj.set_sprite(gfx.get_sprite(&SPRITES[this_image]).unwrap());
obj.set_sprite(gfx.get_sprite(&SPRITES[this_image]));
obj.commit();
}
}
@ -66,7 +64,7 @@ fn all_tags(gfx: &ObjectController) {
let sprite = v.get_sprite(0);
let (size_x, size_y) = sprite.size().to_width_height();
let (size_x, size_y) = (size_x as i32, size_y as i32);
let mut obj = gfx.get_object(gfx.get_sprite(sprite).unwrap()).unwrap();
let mut obj = gfx.get_object(gfx.get_sprite(sprite));
obj.show();
obj.set_position((x * 32 + 16 - size_x / 2, y * 32 + 16 - size_y / 2).into());
objs.push((obj, v));
@ -91,7 +89,7 @@ fn all_tags(gfx: &ObjectController) {
if count % 5 == 0 {
image += 1;
for (obj, tag) in objs.iter_mut() {
obj.set_sprite(gfx.get_sprite(tag.get_animation_sprite(image)).unwrap());
obj.set_sprite(gfx.get_sprite(tag.get_animation_sprite(image)));
obj.commit();
}
}

View file

@ -362,7 +362,11 @@ impl ObjectController {
}
}
pub fn get_object<'a, 'b>(&'a self, sprite: SpriteBorrow<'b>) -> Option<Object<'b, 'a>> {
pub fn get_object<'a, 'b>(&'a self, sprite: SpriteBorrow<'b>) -> Object<'b, 'a> {
self.try_get_object(sprite).expect("No object avaliable")
}
pub fn try_get_object<'a, 'b>(&'a self, sprite: SpriteBorrow<'b>) -> Option<Object<'b, 'a>> {
let mut inner = self.free_objects.borrow_mut();
let loan = Loan {
index: inner.pop()?,
@ -386,8 +390,14 @@ impl ObjectController {
})
}
pub fn get_sprite(&self, sprite: &'static Sprite) -> Option<SpriteBorrow> {
self.sprite_controller.get_sprite(sprite)
pub fn get_sprite(&self, sprite: &'static Sprite) -> SpriteBorrow {
self.sprite_controller
.try_get_sprite(sprite)
.expect("No slot for sprite available")
}
pub fn try_get_sprite(&self, sprite: &'static Sprite) -> Option<SpriteBorrow> {
self.sprite_controller.try_get_sprite(sprite)
}
}
@ -540,7 +550,7 @@ impl SpriteController {
inner: RefCell::new(SpriteControllerInner::new()),
}
}
fn get_sprite(&self, sprite: &'static Sprite) -> Option<SpriteBorrow> {
fn try_get_sprite(&self, sprite: &'static Sprite) -> Option<SpriteBorrow> {
let mut inner = self.inner.borrow_mut();
let id = sprite.get_id();
if let Some(storage) = inner.sprite.get_mut(&id) {

View file

@ -24,10 +24,10 @@ fn main(mut gba: Gba) -> ! {
const BALL: &Tag = GRAPHICS.tags().get("Ball");
let ball_sprite = object
.get_sprite(BALL.get_sprite(0))
.try_get_sprite(BALL.get_sprite(0))
.expect("We should be able to load a sprite");
let mut ball = object
.get_object(ball_sprite)
.try_get_object(ball_sprite)
.expect("We should have enoguh space to store an object");
ball.set_x(50).set_y(50).show();

View file

@ -165,7 +165,7 @@ impl<'a> Slime<'a> {
let offset = (timer / 16) as usize;
let frame = SLIME_IDLE.get_animation_sprite(offset);
let sprite = controller.get_sprite(frame).unwrap();
let sprite = controller.get_sprite(frame);
self.enemy_info.entity.sprite.set_sprite(sprite);
@ -205,7 +205,7 @@ impl<'a> Slime<'a> {
self.state = SlimeState::Idle;
} else {
let frame = SLIME_JUMP.get_animation_sprite(offset);
let sprite = controller.get_sprite(frame).unwrap();
let sprite = controller.get_sprite(frame);
self.enemy_info.entity.sprite.set_sprite(sprite);
}
@ -231,7 +231,7 @@ impl<'a> Slime<'a> {
}
let frame = SLIME_SPLAT.get_animation_sprite(offset);
let sprite = controller.get_sprite(frame).unwrap();
let sprite = controller.get_sprite(frame);
self.enemy_info.entity.sprite.set_sprite(sprite);
}
@ -302,7 +302,7 @@ impl<'a> Snail<'a> {
}
let frame = SNAIL_IDLE.get_animation_sprite(0);
let sprite = controller.get_sprite(frame).unwrap();
let sprite = controller.get_sprite(frame);
self.enemy_info.entity.sprite.set_sprite(sprite);
if player_has_collided {
@ -322,7 +322,7 @@ impl<'a> Snail<'a> {
self.enemy_info.entity.velocity = (0, 0).into();
let frame = SNAIL_EMERGE.get_animation_sprite(offset);
let sprite = controller.get_sprite(frame).unwrap();
let sprite = controller.get_sprite(frame);
self.enemy_info.entity.sprite.set_sprite(sprite);
@ -344,7 +344,7 @@ impl<'a> Snail<'a> {
let offset = (timer - time) as usize / 8;
let frame = SNAIL_MOVE.get_animation_sprite(offset);
let sprite = controller.get_sprite(frame).unwrap();
let sprite = controller.get_sprite(frame);
self.enemy_info.entity.sprite.set_sprite(sprite);
@ -378,7 +378,7 @@ impl<'a> Snail<'a> {
}
let frame = SNAIL_EMERGE.get_animation_sprite(offset);
let sprite = controller.get_sprite(frame).unwrap();
let sprite = controller.get_sprite(frame);
self.enemy_info.entity.sprite.set_sprite(sprite);
self.enemy_info.entity.velocity = (0, 0).into();
@ -407,7 +407,7 @@ impl<'a> Snail<'a> {
return UpdateState::Remove;
};
let sprite = controller.get_sprite(frame).unwrap();
let sprite = controller.get_sprite(frame);
self.enemy_info.entity.sprite.set_sprite(sprite);
self.enemy_info.entity.velocity = (0, 0).into();

View file

@ -114,8 +114,8 @@ pub struct Entity<'a> {
impl<'a> Entity<'a> {
pub fn new(object: &'a ObjectController, collision_mask: Vector2D<u16>) -> Self {
let dummy_sprite = object.get_sprite(WALKING.get_sprite(0)).unwrap();
let mut sprite = object.get_object(dummy_sprite).unwrap();
let dummy_sprite = object.get_sprite(WALKING.get_sprite(0));
let mut sprite = object.get_object(dummy_sprite);
sprite.set_priority(Priority::P1);
Entity {
sprite,
@ -357,9 +357,9 @@ impl<'a> Player<'a> {
wizard
.sprite
.set_sprite(controller.get_sprite(HAT_SPIN_1.get_sprite(0)).unwrap());
.set_sprite(controller.get_sprite(HAT_SPIN_1.get_sprite(0)));
hat.sprite
.set_sprite(controller.get_sprite(HAT_SPIN_1.get_sprite(0)).unwrap());
.set_sprite(controller.get_sprite(HAT_SPIN_1.get_sprite(0)));
wizard.sprite.show();
hat.sprite.show();
@ -465,7 +465,7 @@ impl<'a> Player<'a> {
self.wizard_frame = offset as u8;
let frame = WALKING.get_animation_sprite(offset);
let sprite = controller.get_sprite(frame).unwrap();
let sprite = controller.get_sprite(frame);
self.wizard.sprite.set_sprite(sprite);
}
@ -475,7 +475,7 @@ impl<'a> Player<'a> {
self.wizard_frame = 5;
let frame = JUMPING.get_animation_sprite(0);
let sprite = controller.get_sprite(frame).unwrap();
let sprite = controller.get_sprite(frame);
self.wizard.sprite.set_sprite(sprite);
} else if self.wizard.velocity.y > FixedNumberType::new(1) / 16 {
@ -490,7 +490,7 @@ impl<'a> Player<'a> {
self.wizard_frame = 0;
let frame = FALLING.get_animation_sprite(offset);
let sprite = controller.get_sprite(frame).unwrap();
let sprite = controller.get_sprite(frame);
self.wizard.sprite.set_sprite(sprite);
}
@ -511,13 +511,13 @@ impl<'a> Player<'a> {
self.wizard.sprite.set_hflip(true);
self.hat
.sprite
.set_sprite(controller.get_sprite(hat_base_tile.get_sprite(5)).unwrap());
.set_sprite(controller.get_sprite(hat_base_tile.get_sprite(5)));
}
agb::input::Tri::Positive => {
self.wizard.sprite.set_hflip(false);
self.hat
.sprite
.set_sprite(controller.get_sprite(hat_base_tile.get_sprite(0)).unwrap());
.set_sprite(controller.get_sprite(hat_base_tile.get_sprite(0)));
}
_ => {}
}
@ -549,9 +549,7 @@ impl<'a> Player<'a> {
let hat_sprite_offset = (timer / hat_sprite_divider) as usize;
self.hat.sprite.set_sprite(
controller
.get_sprite(hat_base_tile.get_animation_sprite(hat_sprite_offset))
.unwrap(),
controller.get_sprite(hat_base_tile.get_animation_sprite(hat_sprite_offset)),
);
if self.hat_slow_counter < 30 && self.hat.velocity.magnitude() < 2.into() {
@ -584,9 +582,7 @@ impl<'a> Player<'a> {
}
HatState::WizardTowards => {
self.hat.sprite.set_sprite(
controller
.get_sprite(hat_base_tile.get_animation_sprite(timer as usize / 2))
.unwrap(),
controller.get_sprite(hat_base_tile.get_animation_sprite(timer as usize / 2)),
);
let distance_vector =
self.hat.position - self.wizard.position + hat_resting_position;
@ -687,7 +683,7 @@ impl<'a, 'b, 'c> PlayingLevel<'a, 'b> {
self.timer += 1;
let frame = PLAYER_DEATH.get_animation_sprite(self.timer as usize / 8);
let sprite = controller.get_sprite(frame).unwrap();
let sprite = controller.get_sprite(frame);
self.player.wizard.velocity += (0.into(), FixedNumberType::new(1) / 32).into();
self.player.wizard.position += self.player.wizard.velocity;

View file

@ -162,10 +162,8 @@ struct Entity<'a> {
impl<'a> Entity<'a> {
fn new(object_controller: &'a ObjectController, collision_mask: Rect<u16>) -> Self {
let s = object_controller
.get_sprite(LONGSWORD_IDLE.get_sprite(0))
.unwrap();
let mut sprite = object_controller.get_object(s).unwrap();
let s = object_controller.get_sprite(LONGSWORD_IDLE.get_sprite(0));
let mut sprite = object_controller.get_object(s);
sprite.set_priority(Priority::P1);
Entity {
sprite,
@ -539,9 +537,7 @@ impl<'a> Player<'a> {
object_controller,
Rect::new((0_u16, 0_u16).into(), (4_u16, 12_u16).into()),
);
let s = object_controller
.get_sprite(LONGSWORD_IDLE.get_sprite(0))
.unwrap();
let s = object_controller.get_sprite(LONGSWORD_IDLE.get_sprite(0));
entity.sprite.set_sprite(s);
entity.sprite.show();
entity.position = (144, 0).into();
@ -596,13 +592,11 @@ impl<'a> Player<'a> {
self.entity.velocity.x += self.sword.ground_walk_force() * x as i32;
if self.entity.velocity.x.abs() > Number::new(1) / 10 {
let sprite = controller
.get_sprite(self.sword.walk_animation(self.sprite_offset))
.unwrap();
.get_sprite(self.sword.walk_animation(self.sprite_offset));
self.entity.sprite.set_sprite(sprite);
} else {
let sprite = controller
.get_sprite(self.sword.idle_animation(self.sprite_offset))
.unwrap();
.get_sprite(self.sword.idle_animation(self.sprite_offset));
self.entity.sprite.set_sprite(sprite);
}
@ -622,9 +616,8 @@ impl<'a> Player<'a> {
let frame = self.sword.attack_frame(*a);
self.fudge_factor.x = self.sword.fudge(frame) * self.facing as i32;
let tag = self.sword.attack_tag();
let sprite = controller
.get_sprite(tag.get_animation_sprite(frame as usize))
.unwrap();
let sprite =
controller.get_sprite(tag.get_animation_sprite(frame as usize));
self.entity.sprite.set_sprite(sprite);
hurtbox = self.sword.ground_attack_hurtbox(frame);
@ -638,9 +631,8 @@ impl<'a> Player<'a> {
let frame = self.sword.hold_frame();
self.fudge_factor.x = self.sword.fudge(frame) * self.facing as i32;
let tag = self.sword.attack_tag();
let sprite = controller
.get_sprite(tag.get_animation_sprite(frame as usize))
.unwrap();
let sprite =
controller.get_sprite(tag.get_animation_sprite(frame as usize));
self.entity.sprite.set_sprite(sprite);
if *a == 0 {
self.attack_timer = AttackTimer::Idle;
@ -665,9 +657,8 @@ impl<'a> Player<'a> {
2
};
let tag = self.sword.jump_tag();
let sprite = controller
.get_sprite(tag.get_animation_sprite(frame as usize))
.unwrap();
let sprite =
controller.get_sprite(tag.get_animation_sprite(frame as usize));
self.entity.sprite.set_sprite(sprite);
if x != Tri::Zero {
@ -689,9 +680,8 @@ impl<'a> Player<'a> {
*a -= 1;
let frame = self.sword.jump_attack_frame(*a);
let tag = self.sword.jump_attack_tag();
let sprite = controller
.get_sprite(tag.get_animation_sprite(frame as usize))
.unwrap();
let sprite =
controller.get_sprite(tag.get_animation_sprite(frame as usize));
self.entity.sprite.set_sprite(sprite);
hurtbox = self.sword.air_attack_hurtbox(frame);
@ -854,7 +844,7 @@ impl BatData {
}
let sprite = BAT_IDLE.get_sprite(self.sprite_offset as usize / 8);
let sprite = controller.get_sprite(sprite).unwrap();
let sprite = controller.get_sprite(sprite);
entity.sprite.set_sprite(sprite);
@ -889,7 +879,7 @@ impl BatData {
}
let sprite = BAT_IDLE.get_sprite(self.sprite_offset as usize / 2);
let sprite = controller.get_sprite(sprite).unwrap();
let sprite = controller.get_sprite(sprite);
entity.sprite.set_sprite(sprite);
@ -916,7 +906,7 @@ impl BatData {
BatState::Dead => {
const BAT_DEAD: &Tag = TAG_MAP.get("bat dead");
let sprite = BAT_DEAD.get_sprite(0);
let sprite = controller.get_sprite(sprite).unwrap();
let sprite = controller.get_sprite(sprite);
entity.sprite.set_sprite(sprite);
@ -984,7 +974,7 @@ impl SlimeData {
const IDLE: &Tag = TAG_MAP.get("slime idle");
let sprite = IDLE.get_sprite(self.sprite_offset as usize / 16);
let sprite = controller.get_sprite(sprite).unwrap();
let sprite = controller.get_sprite(sprite);
entity.sprite.set_sprite(sprite);
@ -1024,7 +1014,7 @@ impl SlimeData {
const CHASE: &Tag = TAG_MAP.get("Slime jump");
let sprite = CHASE.get_sprite(frame as usize);
let sprite = controller.get_sprite(sprite).unwrap();
let sprite = controller.get_sprite(sprite);
entity.sprite.set_sprite(sprite);
@ -1054,7 +1044,7 @@ impl SlimeData {
if *count < 5 * 4 {
const DEATH: &Tag = TAG_MAP.get("Slime death");
let sprite = DEATH.get_sprite(*count as usize / 4);
let sprite = controller.get_sprite(sprite).unwrap();
let sprite = controller.get_sprite(sprite);
entity.sprite.set_sprite(sprite);
*count += 1;
@ -1122,7 +1112,7 @@ impl MiniFlameData {
}
} else {
let sprite = ANGRY.get_animation_sprite(self.sprite_offset as usize / 8);
let sprite = controller.get_sprite(sprite).unwrap();
let sprite = controller.get_sprite(sprite);
entity.sprite.set_sprite(sprite);
entity.velocity = (0.into(), Number::new(-1) / Number::new(4)).into();
@ -1170,7 +1160,7 @@ impl MiniFlameData {
}
let sprite = ANGRY.get_animation_sprite(self.sprite_offset as usize / 2);
let sprite = controller.get_sprite(sprite).unwrap();
let sprite = controller.get_sprite(sprite);
entity.sprite.set_sprite(sprite);
}
MiniFlameState::Dead => {
@ -1182,7 +1172,7 @@ impl MiniFlameData {
const DEATH: &Tag = TAG_MAP.get("angry boss dead");
let sprite = DEATH.get_animation_sprite(self.sprite_offset as usize / 12);
let sprite = controller.get_sprite(sprite).unwrap();
let sprite = controller.get_sprite(sprite);
entity.sprite.set_sprite(sprite);
self.sprite_offset += 1;
@ -1243,7 +1233,7 @@ impl EmuData {
const IDLE: &Tag = TAG_MAP.get("emu - idle");
let sprite = IDLE.get_sprite(self.sprite_offset as usize / 16);
let sprite = controller.get_sprite(sprite).unwrap();
let sprite = controller.get_sprite(sprite);
entity.sprite.set_sprite(sprite);
if (entity.position.y - player.entity.position.y).abs() < 10.into() {
@ -1290,7 +1280,7 @@ impl EmuData {
const WALK: &Tag = TAG_MAP.get("emu-walk");
let sprite = WALK.get_sprite(self.sprite_offset as usize / 2);
let sprite = controller.get_sprite(sprite).unwrap();
let sprite = controller.get_sprite(sprite);
entity.sprite.set_sprite(sprite);
let gravity: Number = 1.into();
@ -1345,7 +1335,7 @@ impl EmuData {
const DEATH: &Tag = TAG_MAP.get("emu - die");
let sprite = DEATH.get_animation_sprite(self.sprite_offset as usize / 4);
let sprite = controller.get_sprite(sprite).unwrap();
let sprite = controller.get_sprite(sprite);
entity.sprite.set_sprite(sprite);
self.sprite_offset += 1;
@ -1415,7 +1405,7 @@ impl<'a> Enemy<'a> {
let mut entity = Entity::new(object_controller, enemy_data.collision_mask());
let sprite = enemy_data.sprite();
let sprite = object_controller.get_sprite(sprite).unwrap();
let sprite = object_controller.get_sprite(sprite);
entity.sprite.set_sprite(sprite);
entity.sprite.show();
@ -1479,7 +1469,7 @@ impl ParticleData {
const DUST: &Tag = TAG_MAP.get("dust");
let sprite = DUST.get_sprite(*frame as usize / 3);
let sprite = controller.get_sprite(sprite).unwrap();
let sprite = controller.get_sprite(sprite);
entity.sprite.set_sprite(sprite);
@ -1493,7 +1483,7 @@ impl ParticleData {
const HEALTH: &Tag = TAG_MAP.get("Heath");
let sprite = HEALTH.get_animation_sprite(*frame as usize / 3);
let sprite = controller.get_sprite(sprite).unwrap();
let sprite = controller.get_sprite(sprite);
entity.sprite.set_sprite(sprite);
@ -1519,7 +1509,7 @@ impl ParticleData {
ParticleData::BossHealer(frame, target) => {
const HEALTH: &Tag = TAG_MAP.get("Heath");
let sprite = HEALTH.get_animation_sprite(*frame as usize / 3);
let sprite = controller.get_sprite(sprite).unwrap();
let sprite = controller.get_sprite(sprite);
entity.sprite.set_sprite(sprite);
@ -1683,7 +1673,7 @@ impl<'a> FollowingBoss<'a> {
const BOSS: &Tag = TAG_MAP.get("happy boss");
let sprite = BOSS.get_animation_sprite(frame as usize);
let sprite = controller.get_sprite(sprite).unwrap();
let sprite = controller.get_sprite(sprite);
self.entity.sprite.set_sprite(sprite);
@ -1819,7 +1809,7 @@ impl<'a> Boss<'a> {
const BOSS: &Tag = TAG_MAP.get("Boss");
let sprite = BOSS.get_animation_sprite(frame as usize);
let sprite = object_controller.get_sprite(sprite).unwrap();
let sprite = object_controller.get_sprite(sprite);
self.entity.sprite.set_sprite(sprite);