diff --git a/agb/src/display/object.rs b/agb/src/display/object.rs index 83a72051..4b8301bb 100644 --- a/agb/src/display/object.rs +++ b/agb/src/display/object.rs @@ -91,49 +91,77 @@ impl ObjectStandard<'_> { } /// Sets the x coordinate of the sprite on screen. - pub fn set_x(&mut self, x: u16) { - self.attributes.set_x(x) - } - /// Sets the y coordinate of the sprite on screen. - 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_x(&mut self, x: u16) -> &mut Self { + self.attributes.set_x(x); + + self } - 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 } /// Sets the x and y position of the object, performing casts as nessesary /// to fit within the bits allocated for this purpose. - pub fn set_position(&mut self, position: Vector2D) { + pub fn set_position(&mut self, position: Vector2D) -> &mut Self { let x = position.x as u16; let y = position.y as u16; self.attributes.set_x(x); self.attributes.set_y(y); + + self } - pub fn set_priority(&mut self, p: Priority) { - self.attributes.set_priority(p) + /// Sets the priority (used for z ordering) of this sprite + pub fn set_priority(&mut self, p: Priority) -> &mut Self { + self.attributes.set_priority(p); + + self } } diff --git a/book/games/pong/src/main.rs b/book/games/pong/src/main.rs index fde37805..0df2e5da 100644 --- a/book/games/pong/src/main.rs +++ b/book/games/pong/src/main.rs @@ -47,15 +47,12 @@ fn main() -> ! { let mut ball = object.get_object_standard(); - ball.set_x(50); - ball.set_y(50); - - ball.set_sprite_size(Size::S16x16); - - ball.set_tile_id(4 * 2); - - ball.show(); - ball.commit(); + ball.set_x(50) + .set_y(50) + .set_sprite_size(Size::S16x16) + .set_tile_id(4 * 2) + .show() + .commit(); loop {} } diff --git a/examples/the-purple-night/src/main.rs b/examples/the-purple-night/src/main.rs index 118c1196..170f6108 100644 --- a/examples/the-purple-night/src/main.rs +++ b/examples/the-purple-night/src/main.rs @@ -1578,24 +1578,24 @@ impl<'a> FollowingBoss<'a> { } 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 { 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 { self.entity.velocity = difference / 16; if difference.manhattan_distance() < 20.into() { self.following = false; } 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 { self.entity.velocity = (0, 0).into(); if difference.manhattan_distance() > 60.into() { self.following = true; } 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(); }