ソースを参照

Factor out components

master
Noelle 4年前
コミット
12cadfce69
2個のファイルの変更45行の追加47行の削除
  1. 39
    0
      src/components.rs
  2. 6
    47
      src/main.rs

+ 39
- 0
src/components.rs ファイルの表示

@@ -0,0 +1,39 @@
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 ファイルの表示

@@ -1,3 +1,5 @@
mod components;

use sdl2::pixels::Color;
use sdl2::event::Event;
use sdl2::keyboard::Keycode;
@@ -10,52 +12,9 @@ use specs_derive::Component;

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 {
use self::Direction::*;
@@ -164,13 +123,13 @@ fn main() -> Result<(), String> {
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),
right_frames: character_animation_frames(player_spritesheet, player_top_left_frame, Direction::Right),
}
};

let mut world = World::new();

world.create_entity()
.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)
.build();

読み込み中…
キャンセル
保存