Use builder style for ObjectStandard

This commit is contained in:
GBA bot 2022-01-02 11:14:18 +00:00
parent cd1c71fc5e
commit b5ff4991fa
3 changed files with 68 additions and 43 deletions

View file

@ -91,49 +91,77 @@ impl ObjectStandard<'_> {
} }
/// Sets the x coordinate of the sprite on screen. /// Sets the x coordinate of the sprite on screen.
pub fn set_x(&mut self, x: u16) { pub fn set_x(&mut self, x: u16) -> &mut Self {
self.attributes.set_x(x) self.attributes.set_x(x);
}
/// Sets the y coordinate of the sprite on screen. self
pub fn set_y(&mut self, y: u16) {
self.attributes.set_y(y)
}
/// Sets the index of the tile to use as the sprite. Potentially a temporary function.
pub fn set_tile_id(&mut self, id: u16) {
self.attributes.set_tile_id(id)
}
/// Sets whether the sprite is horizontally mirrored or not.
pub fn set_hflip(&mut self, hflip: bool) {
self.attributes.set_hflip(hflip)
}
/// Sets the sprite size, will read tiles in x major order to construct this.
pub fn set_sprite_size(&mut self, size: Size) {
self.attributes.set_size(size);
}
/// Show the object on screen.
pub fn show(&mut self) {
self.attributes.set_mode(Mode::Normal)
}
/// Hide the object and do not render.
pub fn hide(&mut self) {
self.attributes.set_mode(Mode::Hidden)
} }
pub fn set_palette(&mut self, palette: u16) { /// Sets the y coordinate of the sprite on screen.
pub fn set_y(&mut self, y: u16) -> &mut Self {
self.attributes.set_y(y);
self
}
/// Sets the index of the tile to use as the sprite. Potentially a temporary function.
pub fn set_tile_id(&mut self, id: u16) -> &mut Self {
self.attributes.set_tile_id(id);
self
}
/// Sets whether the sprite is horizontally mirrored or not.
pub fn set_hflip(&mut self, hflip: bool) -> &mut Self {
self.attributes.set_hflip(hflip);
self
}
/// Sets the sprite size, will read tiles in x major order to construct this.
pub fn set_sprite_size(&mut self, size: Size) -> &mut Self {
self.attributes.set_size(size);
self
}
/// Show the object on screen.
pub fn show(&mut self) -> &mut Self {
self.attributes.set_mode(Mode::Normal);
self
}
/// Hide the object and do not render.
pub fn hide(&mut self) -> &mut Self {
self.attributes.set_mode(Mode::Hidden);
self
}
/// Sets the palette to use for this sprite
pub fn set_palette(&mut self, palette: u16) -> &mut Self {
self.attributes.set_palette(palette); self.attributes.set_palette(palette);
self
} }
/// Sets the x and y position of the object, performing casts as nessesary /// Sets the x and y position of the object, performing casts as nessesary
/// to fit within the bits allocated for this purpose. /// to fit within the bits allocated for this purpose.
pub fn set_position(&mut self, position: Vector2D<i32>) { pub fn set_position(&mut self, position: Vector2D<i32>) -> &mut Self {
let x = position.x as u16; let x = position.x as u16;
let y = position.y as u16; let y = position.y as u16;
self.attributes.set_x(x); self.attributes.set_x(x);
self.attributes.set_y(y); self.attributes.set_y(y);
self
} }
pub fn set_priority(&mut self, p: Priority) { /// Sets the priority (used for z ordering) of this sprite
self.attributes.set_priority(p) pub fn set_priority(&mut self, p: Priority) -> &mut Self {
self.attributes.set_priority(p);
self
} }
} }

View file

@ -47,15 +47,12 @@ fn main() -> ! {
let mut ball = object.get_object_standard(); let mut ball = object.get_object_standard();
ball.set_x(50); ball.set_x(50)
ball.set_y(50); .set_y(50)
.set_sprite_size(Size::S16x16)
ball.set_sprite_size(Size::S16x16); .set_tile_id(4 * 2)
.show()
ball.set_tile_id(4 * 2); .commit();
ball.show();
ball.commit();
loop {} loop {}
} }

View file

@ -1578,24 +1578,24 @@ impl<'a> FollowingBoss<'a> {
} }
let frame = (self.timer / 8) % 12; let frame = (self.timer / 8) % 12;
self.entity.sprite.set_tile_id((125 + frame as u16) * 4) self.entity.sprite.set_tile_id((125 + frame as u16) * 4);
} else if self.timer < 120 { } else if self.timer < 120 {
let frame = (self.timer / 20) % 12; let frame = (self.timer / 20) % 12;
self.entity.sprite.set_tile_id((125 + frame as u16) * 4) self.entity.sprite.set_tile_id((125 + frame as u16) * 4);
} else if self.following { } else if self.following {
self.entity.velocity = difference / 16; self.entity.velocity = difference / 16;
if difference.manhattan_distance() < 20.into() { if difference.manhattan_distance() < 20.into() {
self.following = false; self.following = false;
} }
let frame = (self.timer / 8) % 12; let frame = (self.timer / 8) % 12;
self.entity.sprite.set_tile_id((125 + frame as u16) * 4) self.entity.sprite.set_tile_id((125 + frame as u16) * 4);
} else { } else {
self.entity.velocity = (0, 0).into(); self.entity.velocity = (0, 0).into();
if difference.manhattan_distance() > 60.into() { if difference.manhattan_distance() > 60.into() {
self.following = true; self.following = true;
} }
let frame = (self.timer / 16) % 12; let frame = (self.timer / 16) % 12;
self.entity.sprite.set_tile_id((125 + frame as u16) * 4) self.entity.sprite.set_tile_id((125 + frame as u16) * 4);
} }
self.entity.update_position_without_collision(); self.entity.update_position_without_collision();
} }