1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 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
use core::ops::AddAssign; use super::drawable::Drawable; use super::image::Image; use super::canvas::Canvas; pub struct Sprite<'a, T: Into<f64> + Copy + AddAssign> { pub texture: &'a Image, pub coords: (T, T) } impl<'a, T: Into<f64> + Copy + AddAssign> Sprite<'a, T> { pub fn new(coords: (T, T), texture: &Image) -> Sprite<T> { Sprite { coords, texture } } pub fn get_texture(&self) -> &Image { self.texture } pub fn set_texture(&mut self, texture: &'a Image) { self.texture = texture } pub fn set_x(&mut self, x: T) { self.coords.0 = x; } pub fn set_y(&mut self, y: T) { self.coords.1 = y; } pub fn set_coords(&mut self, coords: (T, T)) { self.coords = coords; } pub fn get_x(&mut self) -> T { self.coords.0 } pub fn get_y(&mut self) -> T { self.coords.1 } pub fn get_coords(&mut self) -> (T, T) { self.coords } pub fn move_by(&mut self, movement: (T, T)) { self.coords.0 += movement.0; self.coords.1 += movement.1; } } impl<'a, T: Into<f64> + Copy + AddAssign> Drawable for Sprite<'a, T> { fn draw_on_canvas(&self, canvas: &mut Canvas) { canvas.draw_image((self.coords.0.into(), self.coords.1.into()), &self.texture); } }