|
|
|
|
|
|
|
|
|
|
|
mod components; |
|
|
|
|
|
|
|
|
use sdl2::pixels::Color; |
|
|
use sdl2::pixels::Color; |
|
|
use sdl2::event::Event; |
|
|
use sdl2::event::Event; |
|
|
use sdl2::keyboard::Keycode; |
|
|
use sdl2::keyboard::Keycode; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use std::time::Duration; |
|
|
use std::time::Duration; |
|
|
|
|
|
|
|
|
const PLAYER_MOVEMENT_SPEED: i32 = 20; |
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
|
|
|
|
|
enum Direction { |
|
|
|
|
|
Up, |
|
|
|
|
|
Down, |
|
|
|
|
|
Left, |
|
|
|
|
|
Right, |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[derive(Component, Debug)] |
|
|
|
|
|
#[storage(VecStorage)] |
|
|
|
|
|
struct Position(Point); |
|
|
|
|
|
|
|
|
|
|
|
#[derive(Component, Debug)] |
|
|
|
|
|
#[storage(VecStorage)] |
|
|
|
|
|
struct Velocity { |
|
|
|
|
|
speed: i32, |
|
|
|
|
|
direction: Direction, |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[derive(Component, Debug, Clone)] |
|
|
|
|
|
#[storage(VecStorage)] |
|
|
|
|
|
struct Sprite { |
|
|
|
|
|
spritesheet: usize, |
|
|
|
|
|
region: Rect, |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[derive(Component, Debug)] |
|
|
|
|
|
#[storage(VecStorage)] |
|
|
|
|
|
struct MovementAnimation { |
|
|
|
|
|
current_frame: usize, |
|
|
|
|
|
up_frames: Vec<Sprite>, |
|
|
|
|
|
down_frames: Vec<Sprite>, |
|
|
|
|
|
left_frames: Vec<Sprite>, |
|
|
|
|
|
right_frames: Vec<Sprite>, |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
use crate::components::*; |
|
|
|
|
|
|
|
|
#[derive(Debug)] |
|
|
|
|
|
struct Player { |
|
|
|
|
|
position: Point, |
|
|
|
|
|
sprite: Rect, |
|
|
|
|
|
speed: i32, |
|
|
|
|
|
direction: Direction, |
|
|
|
|
|
current_frame: i32, |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
const PLAYER_MOVEMENT_SPEED: i32 = 20; |
|
|
|
|
|
|
|
|
fn direction_spritesheet_row(direction: Direction) -> i32 { |
|
|
fn direction_spritesheet_row(direction: Direction) -> i32 { |
|
|
use self::Direction::*; |
|
|
use self::Direction::*; |
|
|
|
|
|
|
|
|
down_frames: character_animation_frames(player_spritesheet, player_top_left_frame, Direction::Down), |
|
|
down_frames: character_animation_frames(player_spritesheet, player_top_left_frame, Direction::Down), |
|
|
left_frames: character_animation_frames(player_spritesheet, player_top_left_frame, Direction::Left), |
|
|
left_frames: character_animation_frames(player_spritesheet, player_top_left_frame, Direction::Left), |
|
|
right_frames: character_animation_frames(player_spritesheet, player_top_left_frame, Direction::Right), |
|
|
right_frames: character_animation_frames(player_spritesheet, player_top_left_frame, Direction::Right), |
|
|
} |
|
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
let mut world = World::new(); |
|
|
let mut world = World::new(); |
|
|
|
|
|
|
|
|
world.create_entity() |
|
|
world.create_entity() |
|
|
.with(Position(Point::new(0, 0))) |
|
|
.with(Position(Point::new(0, 0))) |
|
|
.with(Velocity {speed: 0, direction: Direction::Right}), |
|
|
|
|
|
|
|
|
.with(Velocity {speed: 0, direction: Direction::Right}) |
|
|
.with(player_animation.right_frames[0].clone()) |
|
|
.with(player_animation.right_frames[0].clone()) |
|
|
.with(player_animation) |
|
|
.with(player_animation) |
|
|
.build(); |
|
|
.build(); |