wasm_game_lib/graphics/
sprite.rs

1use core::ops::AddAssign;
2use super::drawable::Drawable;
3use super::image::Image;
4use super::canvas::Canvas;
5
6/// Use a Sprite for an object on your game which can move.
7/// 
8/// # Example
9/// 
10/// ```rust
11/// use wasm_game_lib::graphics::image::Image;
12/// use wasm_game_lib::graphics::sprite::Sprite;
13/// # use wasm_game_lib::graphics::window::Window;
14/// # async fn test() {
15/// # let (window, mut canvas) = Window::init(); 
16/// 
17/// // load a texture from the web
18/// let ferris_texture = Image::load("https://rustacean.net/assets/cuddlyferris.svg").await.unwrap();
19/// 
20/// // create a sprite
21/// let ferris = Sprite::<u32>::new((0,0), &ferris_texture, (0,0));
22/// 
23/// // draw the sprite on a canvas
24/// canvas.draw(&ferris);
25/// # }
26pub struct Sprite<'a, T: Into<f64> + Copy + AddAssign> {
27    /// The texture of the Sprite
28    pub texture: &'a Image,
29    /// Where the Sprite is located on the screen
30    pub coords: (T, T),
31    /// The point on the texture which is considered as the center of the Sprite.
32    /// The coordinates of this point must be relative to the top-left corner of the object.
33    pub origin: (T, T)
34}
35
36impl<'a, T: Into<f64> + Copy + AddAssign> Sprite<'a, T> {
37    /// Create a new Sprite.
38    /// 
39    /// # Example
40    /// 
41    /// ```rust
42    /// use wasm_game_lib::graphics::image::Image;
43    /// use wasm_game_lib::graphics::sprite::Sprite;
44    /// # async fn test() {
45    /// // load a texture from the web
46    /// let ferris_texture = Image::load("https://rustacean.net/assets/cuddlyferris.svg").await.unwrap();
47    /// 
48    /// // create a sprite
49    /// let ferris = Sprite::<u32>::new((0,0), &ferris_texture, (0,0));
50    /// # }
51    /// ```
52    pub fn new(coords: (T, T), texture: &Image, origin: (T, T)) -> Sprite<T> {
53        Sprite {
54            coords,
55            texture,
56            origin
57        }
58    }
59
60    /// Set the origin.
61    /// The origin is the point on the texture which is considered as the center of the Sprite.
62    /// The coordinates of this point must be relative to the top-left corner of the object.
63    pub fn set_origin(&mut self, origin: (T, T)) {
64        self.origin = origin
65    }
66
67    /// Return the origin.
68    /// The origin is the point on the texture which is considered as the center of the Sprite.
69    /// The coordinates of this point must be relative to the top-left corner of the object.
70    pub fn get_origin(&self) -> (T, T) {
71        self.origin
72    }
73
74    /// Return the texture.
75    pub fn get_texture(&self) -> &Image {
76        self.texture
77    }
78
79    /// Set the texture
80    pub fn set_texture(&mut self, texture: &'a Image) {
81        self.texture = texture
82    }
83
84    /// Set the x coordinate.
85    pub fn set_x(&mut self, x: T) {
86        self.coords.0 = x;
87    }
88
89    /// Set the y coordinate.
90    pub fn set_y(&mut self, y: T) {
91        self.coords.1 = y;
92    }
93
94    /// Set the coordinates.
95    pub fn set_coords(&mut self, coords: (T, T)) {
96        self.coords = coords;
97    }
98
99    /// Return the x coordinate.
100    pub fn get_x(&mut self) -> T {
101        self.coords.0
102    }
103
104    /// Return the y coordinate.
105    pub fn get_y(&mut self) -> T {
106        self.coords.1
107    }
108
109    /// Return the coordinates.
110    pub fn get_coords(&mut self) -> (T, T) {
111        self.coords
112    }
113
114    /// Add a value to the actual coordinates.
115    /// 
116    /// # Example
117    /// 
118    /// ```rust
119    /// use wasm_game_lib::graphics::image::Image;
120    /// use wasm_game_lib::graphics::sprite::Sprite;
121    /// # async fn test() {
122    /// # let texture = Image::load("https://rustacean.net/assets/cuddlyferris.svg").await.unwrap();
123    /// # let mut sprite = Sprite::<u32>::new((0,0), &texture, (0,0));
124    /// // move a sprite one pixel right and two pixels down.
125    /// sprite.move_by((1,2));
126    /// # }
127    /// ```
128    pub fn move_by(&mut self, movement: (T, T)) {
129        self.coords.0 += movement.0;
130        self.coords.1 += movement.1;
131    }
132}
133
134impl<'a, T: Into<f64> + Copy + AddAssign> Drawable for Sprite<'a, T> {
135    fn draw_on_canvas(&self, canvas: &mut Canvas) {
136        canvas.draw_image((self.coords.0.into() - self.origin.0.into(), self.coords.1.into() - self.origin.1.into()), &self.texture);
137    }
138}