mirror of
https://github.com/Xevion/Pac-Man.git
synced 2025-12-06 05:15:49 -06:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 724878dc17 | |||
| 274404b9ea |
53
src/animation.rs
Normal file
53
src/animation.rs
Normal file
@@ -0,0 +1,53 @@
|
||||
use sdl2::{
|
||||
rect::Rect,
|
||||
render::{Canvas, Texture},
|
||||
video::Window,
|
||||
};
|
||||
|
||||
pub struct AnimatedTexture<'a> {
|
||||
raw_texture: &'a Texture<'a>,
|
||||
current_frame: u32,
|
||||
frame_count: u32,
|
||||
frame_width: u32,
|
||||
frame_height: u32,
|
||||
}
|
||||
|
||||
impl<'a> AnimatedTexture<'a> {
|
||||
pub fn new(
|
||||
texture: &'a Texture<'a>,
|
||||
frame_count: u32,
|
||||
frame_width: u32,
|
||||
frame_height: u32,
|
||||
) -> Self {
|
||||
AnimatedTexture {
|
||||
raw_texture: texture,
|
||||
current_frame: 0,
|
||||
frame_count,
|
||||
frame_width,
|
||||
frame_height,
|
||||
}
|
||||
}
|
||||
|
||||
fn next_frame(&mut self) {
|
||||
self.current_frame = (self.current_frame + 1) % self.frame_count;
|
||||
}
|
||||
|
||||
fn get_frame_rect(&self) -> Rect {
|
||||
Rect::new(
|
||||
(self.current_frame * self.frame_width) as i32,
|
||||
0,
|
||||
self.frame_width,
|
||||
self.frame_height,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn render(&mut self, canvas: &mut Canvas<Window>, position: (i32, i32)) {
|
||||
let frame_rect = self.get_frame_rect();
|
||||
let position_rect = Rect::new(position.0, position.1, self.frame_width, self.frame_height);
|
||||
canvas
|
||||
.copy(&self.raw_texture, frame_rect, position_rect)
|
||||
.expect("Could not render sprite on canvas");
|
||||
|
||||
self.next_frame();
|
||||
}
|
||||
}
|
||||
6
src/direction.rs
Normal file
6
src/direction.rs
Normal file
@@ -0,0 +1,6 @@
|
||||
pub enum Direction {
|
||||
Up,
|
||||
Down,
|
||||
Left,
|
||||
Right,
|
||||
}
|
||||
8
src/entity.rs
Normal file
8
src/entity.rs
Normal file
@@ -0,0 +1,8 @@
|
||||
pub trait Entity {
|
||||
// Returns true if the entity is colliding with the other entity
|
||||
fn is_colliding(&self, other: &dyn Entity) -> bool;
|
||||
// Returns the absolute position of the entity
|
||||
fn position(&self) -> (i32, i32);
|
||||
// Returns the cell position of the entity (XY position within the grid)
|
||||
fn cell_position(&self) -> (u32, u32);
|
||||
}
|
||||
Reference in New Issue
Block a user