pub struct Object<'controller> { /* private fields */ }
Expand description
A managed object used with the OamManaged
interface.
Implementations§
source§impl Object<'_>
impl Object<'_>
sourcepub fn set_z(&mut self, z_index: i32) -> &mut Self
pub fn set_z(&mut self, z_index: i32) -> &mut Self
Sets the z position of an object. This is not a GBA concept. It causes the order of rendering to be different, thus changing whether objects are rendered above eachother.
Negative z is more towards the outside and positive z is further into the screen => an object with a more negative z is drawn on top of an object with a more positive z.
sourcepub fn is_visible(&self) -> bool
pub fn is_visible(&self) -> bool
Checks whether the object is not marked as hidden. Note that it could be off screen or completely transparent and still claimed to be visible.
sourcepub fn show(&mut self) -> &mut Self
pub fn show(&mut self) -> &mut Self
Display the sprite in Normal mode.
Examples found in repository?
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
fn all_tags(gfx: &OamManaged) {
let mut input = agb::input::ButtonController::new();
let mut objs = Vec::new();
for (i, v) in TAG_MAP.values().enumerate() {
let x = (i % 7) as i32;
let y = (i / 7) as i32;
let sprite = v.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.object_sprite(sprite);
obj.show();
obj.set_position((x * 32 + 16 - size_x / 2, y * 32 + 16 - size_y / 2).into());
objs.push((obj, v));
}
let mut count = 0;
let mut image = 0;
let vblank = agb::interrupt::VBlank::get();
loop {
vblank.wait_for_vblank();
input.update();
if input.is_just_pressed(agb::input::Button::A) {
break;
}
count += 1;
if count % 5 == 0 {
image += 1;
for (obj, tag) in objs.iter_mut() {
obj.set_sprite(gfx.sprite(tag.animation_sprite(image)));
}
gfx.commit();
}
}
}
sourcepub fn show_affine(&mut self, affine_mode: AffineMode) -> &mut Self
pub fn show_affine(&mut self, affine_mode: AffineMode) -> &mut Self
Display the sprite in Affine mode.
Examples found in repository?
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
fn all_sprites(gfx: &OamManaged, rotation_speed: Num<i32, 16>) {
let mut input = agb::input::ButtonController::new();
let mut objs = Vec::new();
let mut rotation: Num<i32, 16> = num!(0.);
let rotation_matrix = AffineMatrix::from_rotation(rotation);
let matrix = object::AffineMatrixInstance::new(rotation_matrix.to_object_wrapping());
for y in 0..9 {
for x in 0..14 {
let mut obj = gfx.object_sprite(&SPRITES[0]);
obj.set_affine_matrix(matrix.clone());
obj.show_affine(object::AffineMode::Affine);
obj.set_position((x * 16 + 8, y * 16 + 8).into());
objs.push(obj);
}
}
let mut count = 0;
let mut image = 0;
let vblank = agb::interrupt::VBlank::get();
loop {
vblank.wait_for_vblank();
input.update();
if input.is_just_pressed(agb::input::Button::A) {
break;
}
rotation += rotation_speed;
let rotation_matrix = AffineMatrix::from_rotation(rotation);
let matrix = object::AffineMatrixInstance::new(rotation_matrix.to_object_wrapping());
for obj in objs.iter_mut() {
obj.set_affine_matrix(matrix.clone());
}
count += 1;
if count % 5 == 0 {
image += 1;
image %= SPRITES.len();
for (i, obj) in objs.iter_mut().enumerate() {
let this_image = (image + i) % SPRITES.len();
obj.set_sprite(gfx.sprite(&SPRITES[this_image]));
}
}
gfx.commit();
}
}
sourcepub fn set_hflip(&mut self, flip: bool) -> &mut Self
pub fn set_hflip(&mut self, flip: bool) -> &mut Self
Sets the horizontal flip, note that this only has a visible affect in Normal mode.
Examples found in repository?
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
fn update_chicken_object(
chicken: &'_ mut Character<'_>,
gfx: &OamManaged,
state: State,
frame_count: u32,
) {
if chicken.velocity.x > 1 {
chicken.object.set_hflip(false);
} else if chicken.velocity.x < -1 {
chicken.object.set_hflip(true);
}
match state {
State::Ground => {
if chicken.velocity.x.abs() > 1 << 4 {
chicken
.object
.set_sprite(gfx.sprite(&CHICKEN_SPRITES[frame_ranger(frame_count, 1, 3, 10)]));
} else {
chicken.object.set_sprite(gfx.sprite(&CHICKEN_SPRITES[0]));
}
}
State::Upwards => {}
State::Flapping => {
chicken
.object
.set_sprite(gfx.sprite(&CHICKEN_SPRITES[frame_ranger(frame_count, 4, 5, 5)]));
}
}
let x: u16 = (chicken.position.x >> 8).try_into().unwrap();
let y: u16 = (chicken.position.y >> 8).try_into().unwrap();
chicken.object.set_x(x - 4);
chicken.object.set_y(y - 4);
}
sourcepub fn set_vflip(&mut self, flip: bool) -> &mut Self
pub fn set_vflip(&mut self, flip: bool) -> &mut Self
Sets the vertical flip, note that this only has a visible affect in Normal mode.
sourcepub fn set_priority(&mut self, priority: Priority) -> &mut Self
pub fn set_priority(&mut self, priority: Priority) -> &mut Self
Sets the priority of the object relative to the backgrounds priority.
sourcepub fn hide(&mut self) -> &mut Self
pub fn hide(&mut self) -> &mut Self
Changes the sprite mode to be hidden, can be changed to Normal or Affine
modes using show
and
show_affine
respectively.
sourcepub fn set_x(&mut self, x: u16) -> &mut Self
pub fn set_x(&mut self, x: u16) -> &mut Self
Sets the x position of the object.
Examples found in repository?
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
fn update_chicken_object(
chicken: &'_ mut Character<'_>,
gfx: &OamManaged,
state: State,
frame_count: u32,
) {
if chicken.velocity.x > 1 {
chicken.object.set_hflip(false);
} else if chicken.velocity.x < -1 {
chicken.object.set_hflip(true);
}
match state {
State::Ground => {
if chicken.velocity.x.abs() > 1 << 4 {
chicken
.object
.set_sprite(gfx.sprite(&CHICKEN_SPRITES[frame_ranger(frame_count, 1, 3, 10)]));
} else {
chicken.object.set_sprite(gfx.sprite(&CHICKEN_SPRITES[0]));
}
}
State::Upwards => {}
State::Flapping => {
chicken
.object
.set_sprite(gfx.sprite(&CHICKEN_SPRITES[frame_ranger(frame_count, 4, 5, 5)]));
}
}
let x: u16 = (chicken.position.x >> 8).try_into().unwrap();
let y: u16 = (chicken.position.y >> 8).try_into().unwrap();
chicken.object.set_x(x - 4);
chicken.object.set_y(y - 4);
}
sourcepub fn set_y(&mut self, y: u16) -> &mut Self
pub fn set_y(&mut self, y: u16) -> &mut Self
Sets the y position of the object.
Examples found in repository?
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
fn update_chicken_object(
chicken: &'_ mut Character<'_>,
gfx: &OamManaged,
state: State,
frame_count: u32,
) {
if chicken.velocity.x > 1 {
chicken.object.set_hflip(false);
} else if chicken.velocity.x < -1 {
chicken.object.set_hflip(true);
}
match state {
State::Ground => {
if chicken.velocity.x.abs() > 1 << 4 {
chicken
.object
.set_sprite(gfx.sprite(&CHICKEN_SPRITES[frame_ranger(frame_count, 1, 3, 10)]));
} else {
chicken.object.set_sprite(gfx.sprite(&CHICKEN_SPRITES[0]));
}
}
State::Upwards => {}
State::Flapping => {
chicken
.object
.set_sprite(gfx.sprite(&CHICKEN_SPRITES[frame_ranger(frame_count, 4, 5, 5)]));
}
}
let x: u16 = (chicken.position.x >> 8).try_into().unwrap();
let y: u16 = (chicken.position.y >> 8).try_into().unwrap();
chicken.object.set_x(x - 4);
chicken.object.set_y(y - 4);
}
sourcepub fn set_position(&mut self, position: Vector2D<i32>) -> &mut Self
pub fn set_position(&mut self, position: Vector2D<i32>) -> &mut Self
Sets the position of the object.
Examples found in repository?
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
fn all_sprites(gfx: &OamManaged, rotation_speed: Num<i32, 16>) {
let mut input = agb::input::ButtonController::new();
let mut objs = Vec::new();
let mut rotation: Num<i32, 16> = num!(0.);
let rotation_matrix = AffineMatrix::from_rotation(rotation);
let matrix = object::AffineMatrixInstance::new(rotation_matrix.to_object_wrapping());
for y in 0..9 {
for x in 0..14 {
let mut obj = gfx.object_sprite(&SPRITES[0]);
obj.set_affine_matrix(matrix.clone());
obj.show_affine(object::AffineMode::Affine);
obj.set_position((x * 16 + 8, y * 16 + 8).into());
objs.push(obj);
}
}
let mut count = 0;
let mut image = 0;
let vblank = agb::interrupt::VBlank::get();
loop {
vblank.wait_for_vblank();
input.update();
if input.is_just_pressed(agb::input::Button::A) {
break;
}
rotation += rotation_speed;
let rotation_matrix = AffineMatrix::from_rotation(rotation);
let matrix = object::AffineMatrixInstance::new(rotation_matrix.to_object_wrapping());
for obj in objs.iter_mut() {
obj.set_affine_matrix(matrix.clone());
}
count += 1;
if count % 5 == 0 {
image += 1;
image %= SPRITES.len();
for (i, obj) in objs.iter_mut().enumerate() {
let this_image = (image + i) % SPRITES.len();
obj.set_sprite(gfx.sprite(&SPRITES[this_image]));
}
}
gfx.commit();
}
}
fn all_tags(gfx: &OamManaged) {
let mut input = agb::input::ButtonController::new();
let mut objs = Vec::new();
for (i, v) in TAG_MAP.values().enumerate() {
let x = (i % 7) as i32;
let y = (i / 7) as i32;
let sprite = v.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.object_sprite(sprite);
obj.show();
obj.set_position((x * 32 + 16 - size_x / 2, y * 32 + 16 - size_y / 2).into());
objs.push((obj, v));
}
let mut count = 0;
let mut image = 0;
let vblank = agb::interrupt::VBlank::get();
loop {
vblank.wait_for_vblank();
input.update();
if input.is_just_pressed(agb::input::Button::A) {
break;
}
count += 1;
if count % 5 == 0 {
image += 1;
for (obj, tag) in objs.iter_mut() {
obj.set_sprite(gfx.sprite(tag.animation_sprite(image)));
}
gfx.commit();
}
}
}
sourcepub fn set_affine_matrix(
&mut self,
affine_matrix: AffineMatrixInstance
) -> &mut Self
pub fn set_affine_matrix( &mut self, affine_matrix: AffineMatrixInstance ) -> &mut Self
Sets the affine matrix. This only has an affect in Affine mode.
Examples found in repository?
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
fn all_sprites(gfx: &OamManaged, rotation_speed: Num<i32, 16>) {
let mut input = agb::input::ButtonController::new();
let mut objs = Vec::new();
let mut rotation: Num<i32, 16> = num!(0.);
let rotation_matrix = AffineMatrix::from_rotation(rotation);
let matrix = object::AffineMatrixInstance::new(rotation_matrix.to_object_wrapping());
for y in 0..9 {
for x in 0..14 {
let mut obj = gfx.object_sprite(&SPRITES[0]);
obj.set_affine_matrix(matrix.clone());
obj.show_affine(object::AffineMode::Affine);
obj.set_position((x * 16 + 8, y * 16 + 8).into());
objs.push(obj);
}
}
let mut count = 0;
let mut image = 0;
let vblank = agb::interrupt::VBlank::get();
loop {
vblank.wait_for_vblank();
input.update();
if input.is_just_pressed(agb::input::Button::A) {
break;
}
rotation += rotation_speed;
let rotation_matrix = AffineMatrix::from_rotation(rotation);
let matrix = object::AffineMatrixInstance::new(rotation_matrix.to_object_wrapping());
for obj in objs.iter_mut() {
obj.set_affine_matrix(matrix.clone());
}
count += 1;
if count % 5 == 0 {
image += 1;
image %= SPRITES.len();
for (i, obj) in objs.iter_mut().enumerate() {
let this_image = (image + i) % SPRITES.len();
obj.set_sprite(gfx.sprite(&SPRITES[this_image]));
}
}
gfx.commit();
}
}
sourcepub fn set_sprite(&mut self, sprite: SpriteVram) -> &mut Self
pub fn set_sprite(&mut self, sprite: SpriteVram) -> &mut Self
Sets the current sprite for the object.
Examples found in repository?
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
fn update_chicken_object(
chicken: &'_ mut Character<'_>,
gfx: &OamManaged,
state: State,
frame_count: u32,
) {
if chicken.velocity.x > 1 {
chicken.object.set_hflip(false);
} else if chicken.velocity.x < -1 {
chicken.object.set_hflip(true);
}
match state {
State::Ground => {
if chicken.velocity.x.abs() > 1 << 4 {
chicken
.object
.set_sprite(gfx.sprite(&CHICKEN_SPRITES[frame_ranger(frame_count, 1, 3, 10)]));
} else {
chicken.object.set_sprite(gfx.sprite(&CHICKEN_SPRITES[0]));
}
}
State::Upwards => {}
State::Flapping => {
chicken
.object
.set_sprite(gfx.sprite(&CHICKEN_SPRITES[frame_ranger(frame_count, 4, 5, 5)]));
}
}
let x: u16 = (chicken.position.x >> 8).try_into().unwrap();
let y: u16 = (chicken.position.y >> 8).try_into().unwrap();
chicken.object.set_x(x - 4);
chicken.object.set_y(y - 4);
}
More examples
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
fn all_sprites(gfx: &OamManaged, rotation_speed: Num<i32, 16>) {
let mut input = agb::input::ButtonController::new();
let mut objs = Vec::new();
let mut rotation: Num<i32, 16> = num!(0.);
let rotation_matrix = AffineMatrix::from_rotation(rotation);
let matrix = object::AffineMatrixInstance::new(rotation_matrix.to_object_wrapping());
for y in 0..9 {
for x in 0..14 {
let mut obj = gfx.object_sprite(&SPRITES[0]);
obj.set_affine_matrix(matrix.clone());
obj.show_affine(object::AffineMode::Affine);
obj.set_position((x * 16 + 8, y * 16 + 8).into());
objs.push(obj);
}
}
let mut count = 0;
let mut image = 0;
let vblank = agb::interrupt::VBlank::get();
loop {
vblank.wait_for_vblank();
input.update();
if input.is_just_pressed(agb::input::Button::A) {
break;
}
rotation += rotation_speed;
let rotation_matrix = AffineMatrix::from_rotation(rotation);
let matrix = object::AffineMatrixInstance::new(rotation_matrix.to_object_wrapping());
for obj in objs.iter_mut() {
obj.set_affine_matrix(matrix.clone());
}
count += 1;
if count % 5 == 0 {
image += 1;
image %= SPRITES.len();
for (i, obj) in objs.iter_mut().enumerate() {
let this_image = (image + i) % SPRITES.len();
obj.set_sprite(gfx.sprite(&SPRITES[this_image]));
}
}
gfx.commit();
}
}
fn all_tags(gfx: &OamManaged) {
let mut input = agb::input::ButtonController::new();
let mut objs = Vec::new();
for (i, v) in TAG_MAP.values().enumerate() {
let x = (i % 7) as i32;
let y = (i / 7) as i32;
let sprite = v.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.object_sprite(sprite);
obj.show();
obj.set_position((x * 32 + 16 - size_x / 2, y * 32 + 16 - size_y / 2).into());
objs.push((obj, v));
}
let mut count = 0;
let mut image = 0;
let vblank = agb::interrupt::VBlank::get();
loop {
vblank.wait_for_vblank();
input.update();
if input.is_just_pressed(agb::input::Button::A) {
break;
}
count += 1;
if count % 5 == 0 {
image += 1;
for (obj, tag) in objs.iter_mut() {
obj.set_sprite(gfx.sprite(tag.animation_sprite(image)));
}
gfx.commit();
}
}
}