From c970ab9362af72b707f077cc29539c5f5cd7cce3 Mon Sep 17 00:00:00 2001 From: Corwin Date: Mon, 17 Apr 2023 23:31:05 +0100 Subject: [PATCH] fix positioning issues due --- examples/the-purple-night/src/lib.rs | 62 ++++++++++------------------ 1 file changed, 21 insertions(+), 41 deletions(-) diff --git a/examples/the-purple-night/src/lib.rs b/examples/the-purple-night/src/lib.rs index 4325f87..a65be82 100644 --- a/examples/the-purple-night/src/lib.rs +++ b/examples/the-purple-night/src/lib.rs @@ -21,7 +21,7 @@ use agb::{ }, Priority, HEIGHT, WIDTH, }, - fixnum::{FixedNum, Rect, Vector2D}, + fixnum::{num, FixedNum, Rect, Vector2D}, input::{Button, ButtonController, Tri}, interrupt::VBlank, rng, @@ -159,12 +159,12 @@ struct Entity<'a> { sprite: Object<'a>, position: Vector2D, velocity: Vector2D, - collision_mask: Rect, + collision_mask: Rect, visible: bool, } impl<'a> Entity<'a> { - fn new(object_controller: &'a ObjectController, collision_mask: Rect) -> Self { + fn new(object_controller: &'a ObjectController, collision_mask: Rect) -> Self { let s = object_controller.sprite(LONG_SWORD_IDLE.sprite(0)); let mut sprite = object_controller.object(s); sprite.set_priority(Priority::P1); @@ -213,18 +213,7 @@ impl<'a> Entity<'a> { } fn collider(&self) -> Rect { - let mut number_collision: Rect = Rect::new( - ( - self.collision_mask.position.x as i32, - self.collision_mask.position.y as i32, - ) - .into(), - ( - self.collision_mask.size.x as i32, - self.collision_mask.size.y as i32, - ) - .into(), - ); + let mut number_collision = self.collision_mask; number_collision.position = self.position + number_collision.position - number_collision.size / 2; number_collision @@ -273,11 +262,12 @@ impl<'a> Entity<'a> { (final_distance, has_collided) } - fn commit_with_fudge(&mut self, offset: Vector2D, fudge: Vector2D) { + fn commit_with_fudge(&mut self, offset: Vector2D, fudge: Vector2D) { if !self.visible { self.sprite.hide(); } else { - let position = (self.position - offset).floor() + fudge; + let position = + (self.position - offset + fudge + Vector2D::new(num!(0.5), num!(0.5))).floor(); self.sprite.set_position(position - (8, 8).into()); if position.x < -8 || position.x > WIDTH + 8 @@ -527,17 +517,14 @@ struct Player<'a> { attack_timer: AttackTimer, damage_cooldown: u16, sword: SwordState, - fudge_factor: Vector2D, + fudge_factor: Vector2D, hurtbox: Option>, controllable: bool, } impl<'a> Player<'a> { fn new(object_controller: &'a ObjectController<'a>) -> Player { - let mut entity = Entity::new( - object_controller, - Rect::new((0_u16, 0_u16).into(), (4_u16, 12_u16).into()), - ); + let mut entity = Entity::new(object_controller, Rect::new((0, 0).into(), (5, 12).into())); let s = object_controller.sprite(LONG_SWORD_IDLE.sprite(0)); entity.sprite.set_sprite(s); entity.sprite.show(); @@ -613,7 +600,7 @@ impl<'a> Player<'a> { AttackTimer::Attack(a) => { *a -= 1; let frame = self.sword.attack_frame(*a); - self.fudge_factor.x = self.sword.fudge(frame) * self.facing as i32; + self.fudge_factor.x += self.sword.fudge(frame) * self.facing as i32; let tag = self.sword.attack_tag(); let sprite = controller.sprite(tag.animation_sprite(frame as usize)); self.entity.sprite.set_sprite(sprite); @@ -627,7 +614,7 @@ impl<'a> Player<'a> { AttackTimer::Cooldown(a) => { *a -= 1; let frame = self.sword.hold_frame(); - self.fudge_factor.x = self.sword.fudge(frame) * self.facing as i32; + self.fudge_factor.x += self.sword.fudge(frame) * self.facing as i32; let tag = self.sword.attack_tag(); let sprite = controller.sprite(tag.animation_sprite(frame as usize)); self.entity.sprite.set_sprite(sprite); @@ -695,6 +682,8 @@ impl<'a> Player<'a> { let gravity = gravity / 16; self.entity.velocity.y += gravity; + self.fudge_factor.x -= num!(1.5) * (self.facing as i32); + let fudge_number = (self.fudge_factor.x, self.fudge_factor.y).into(); // convert the hurtbox to a location in the game @@ -1351,12 +1340,12 @@ enum UpdateInstruction { } impl EnemyData { - fn collision_mask(&self) -> Rect { + fn collision_mask(&self) -> Rect { match self { - EnemyData::Slime(_) => Rect::new((0u16, 0u16).into(), (4u16, 11u16).into()), - EnemyData::Bat(_) => Rect::new((0u16, 0u16).into(), (12u16, 4u16).into()), - EnemyData::MiniFlame(_) => Rect::new((0u16, 0u16).into(), (12u16, 12u16).into()), - EnemyData::Emu(_) => Rect::new((0u16, 0u16).into(), (7u16, 11u16).into()), + EnemyData::Slime(_) => Rect::new((0.into(), num!(1.5)).into(), (4, 11).into()), + EnemyData::Bat(_) => Rect::new((0, 0).into(), (12, 4).into()), + EnemyData::MiniFlame(_) => Rect::new((0, 0).into(), (12, 12).into()), + EnemyData::Emu(_) => Rect::new((0, 0).into(), (7, 11).into()), } } @@ -1533,10 +1522,7 @@ impl<'a> Particle<'a> { particle_data: ParticleData, position: Vector2D, ) -> Self { - let mut entity = Entity::new( - object_controller, - Rect::new((0u16, 0u16).into(), (0u16, 0u16).into()), - ); + let mut entity = Entity::new(object_controller, Rect::new((0, 0).into(), (0, 0).into())); entity.position = position; @@ -1611,10 +1597,7 @@ struct FollowingBoss<'a> { impl<'a> FollowingBoss<'a> { fn new(object_controller: &'a ObjectController, position: Vector2D) -> Self { - let mut entity = Entity::new( - object_controller, - Rect::new((0_u16, 0_u16).into(), (0_u16, 0_u16).into()), - ); + let mut entity = Entity::new(object_controller, Rect::new((0, 0).into(), (0, 0).into())); entity.position = position; Self { @@ -1695,10 +1678,7 @@ enum BossInstruction { impl<'a> Boss<'a> { fn new(object_controller: &'a ObjectController, screen_coords: Vector2D) -> Self { - let mut entity = Entity::new( - object_controller, - Rect::new((0_u16, 0_u16).into(), (28_u16, 28_u16).into()), - ); + let mut entity = Entity::new(object_controller, Rect::new((0, 0).into(), (28, 28).into())); entity.position = screen_coords + (144, 136).into(); Self { entity,