Skip to main content

Entity

Struct Entity 

Source
pub struct Entity {
    pub pos: Pnt,
    pub frames: Vec<Vec<u32>>,
    pub dim: (usize, usize),
    pub anim_rate: (usize, usize),
    /* private fields */
}

Fields§

§pos: Pnt§frames: Vec<Vec<u32>>§dim: (usize, usize)§anim_rate: (usize, usize)

Implementations§

Source§

impl Entity

Source

pub fn new( pos: Pnt, frames: Vec<Vec<u32>>, dim: (usize, usize), anim_rate: usize, ) -> Self

Examples found in repository?
examples/simple.rs (line 14)
3fn main() -> FigResult<()> {
4    // Initialize the canvas and input
5    let mut canvas = Canvas::new("simple example", (320, 320), 60)?;
6    let mut input = InputManager::new();
7
8    // Load assets from assets.tgz
9    let mut assets = AssetLoader::from_tar("examples/assets.tgz");
10    // You can also load assets from the assets directory
11    // let mut assets = AssetLoader::from_dir("examples/assets");
12
13    // Create some entities, using assets from the tarball
14    let mut bop = Entity::new((200.0, 200.0), vec![assets.load_png("bop.png")?], (8, 8), 20);
15    let mut blob = Entity::new((100.0, 100.0), assets.load_png_dir("blob")?, (8, 8), 20);
16
17    // We want to control blob with WASD or arrow keys
18    input.wasd();
19    input.arrows();
20
21    // We would also like to exit when ESC is pressed
22    input.add(Key::Escape);
23
24    // Main event loop (do something more interesting here)
25    while canvas.is_open() {
26        // Handle keypress events
27        for key in &input.keys {
28            if canvas.key_down(*key) {
29                match key {
30                    Key::W | Key::Up => blob.mov(0.0, -1.0),
31                    Key::A | Key::Left => blob.mov(-1.0, 0.0),
32                    Key::S | Key::Down => blob.mov(0.0, 1.0),
33                    Key::D | Key::Right => blob.mov(1.0, 0.0),
34                    Key::Escape => return Ok(()),
35                    _ => (),
36                }
37            }
38        }
39
40        // Update entities (advance animation, etc)
41        bop.update();
42        blob.update();
43
44        // Update the canvas with the entities
45        canvas.clear();
46        canvas.draw(&bop);
47        canvas.draw(&blob);
48        canvas.update()?;
49    }
50
51    Ok(())
52}
Source

pub fn next_frame(&mut self)

Source

pub fn get_frame(&self) -> Vec<u32>

Source

pub fn mov(&mut self, dx: f32, dy: f32)

Examples found in repository?
examples/simple.rs (line 30)
3fn main() -> FigResult<()> {
4    // Initialize the canvas and input
5    let mut canvas = Canvas::new("simple example", (320, 320), 60)?;
6    let mut input = InputManager::new();
7
8    // Load assets from assets.tgz
9    let mut assets = AssetLoader::from_tar("examples/assets.tgz");
10    // You can also load assets from the assets directory
11    // let mut assets = AssetLoader::from_dir("examples/assets");
12
13    // Create some entities, using assets from the tarball
14    let mut bop = Entity::new((200.0, 200.0), vec![assets.load_png("bop.png")?], (8, 8), 20);
15    let mut blob = Entity::new((100.0, 100.0), assets.load_png_dir("blob")?, (8, 8), 20);
16
17    // We want to control blob with WASD or arrow keys
18    input.wasd();
19    input.arrows();
20
21    // We would also like to exit when ESC is pressed
22    input.add(Key::Escape);
23
24    // Main event loop (do something more interesting here)
25    while canvas.is_open() {
26        // Handle keypress events
27        for key in &input.keys {
28            if canvas.key_down(*key) {
29                match key {
30                    Key::W | Key::Up => blob.mov(0.0, -1.0),
31                    Key::A | Key::Left => blob.mov(-1.0, 0.0),
32                    Key::S | Key::Down => blob.mov(0.0, 1.0),
33                    Key::D | Key::Right => blob.mov(1.0, 0.0),
34                    Key::Escape => return Ok(()),
35                    _ => (),
36                }
37            }
38        }
39
40        // Update entities (advance animation, etc)
41        bop.update();
42        blob.update();
43
44        // Update the canvas with the entities
45        canvas.clear();
46        canvas.draw(&bop);
47        canvas.draw(&blob);
48        canvas.update()?;
49    }
50
51    Ok(())
52}
Source

pub fn update(&mut self)

Examples found in repository?
examples/simple.rs (line 41)
3fn main() -> FigResult<()> {
4    // Initialize the canvas and input
5    let mut canvas = Canvas::new("simple example", (320, 320), 60)?;
6    let mut input = InputManager::new();
7
8    // Load assets from assets.tgz
9    let mut assets = AssetLoader::from_tar("examples/assets.tgz");
10    // You can also load assets from the assets directory
11    // let mut assets = AssetLoader::from_dir("examples/assets");
12
13    // Create some entities, using assets from the tarball
14    let mut bop = Entity::new((200.0, 200.0), vec![assets.load_png("bop.png")?], (8, 8), 20);
15    let mut blob = Entity::new((100.0, 100.0), assets.load_png_dir("blob")?, (8, 8), 20);
16
17    // We want to control blob with WASD or arrow keys
18    input.wasd();
19    input.arrows();
20
21    // We would also like to exit when ESC is pressed
22    input.add(Key::Escape);
23
24    // Main event loop (do something more interesting here)
25    while canvas.is_open() {
26        // Handle keypress events
27        for key in &input.keys {
28            if canvas.key_down(*key) {
29                match key {
30                    Key::W | Key::Up => blob.mov(0.0, -1.0),
31                    Key::A | Key::Left => blob.mov(-1.0, 0.0),
32                    Key::S | Key::Down => blob.mov(0.0, 1.0),
33                    Key::D | Key::Right => blob.mov(1.0, 0.0),
34                    Key::Escape => return Ok(()),
35                    _ => (),
36                }
37            }
38        }
39
40        // Update entities (advance animation, etc)
41        bop.update();
42        blob.update();
43
44        // Update the canvas with the entities
45        canvas.clear();
46        canvas.draw(&bop);
47        canvas.draw(&blob);
48        canvas.update()?;
49    }
50
51    Ok(())
52}

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.