Browse Source

Factor out components

master
Noelle 4 years ago
parent
commit
12cadfce69
2 changed files with 45 additions and 47 deletions
  1. 39
    0
      src/components.rs
  2. 6
    47
      src/main.rs

+ 39
- 0
src/components.rs View File

use specs::prelude::*;
use specs_derive::Component;
use sdl2::rect::{Point, Rect};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Direction {
Up,
Down,
Left,
Right,
}

#[derive(Component, Debug)]
#[storage(VecStorage)]
pub struct Position(Point);

#[derive(Component, Debug)]
#[storage(VecStorage)]
pub struct Velocity {
pub speed: i32,
pub direction: Direction,
}

#[derive(Component, Debug, Clone)]
#[storage(VecStorage)]
pub struct Sprite {
pub spritesheet: usize,
pub region: Rect,
}

#[derive(Component, Debug)]
#[storage(VecStorage)]
pub struct MovementAnimation {
pub current_frame: usize,
pub up_frames: Vec<Sprite>,
pub down_frames: Vec<Sprite>,
pub left_frames: Vec<Sprite>,
pub right_frames: Vec<Sprite>,
}

+ 6
- 47
src/main.rs View File

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();

Loading…
Cancel
Save