Преглед на файлове

Define animations

master
Noelle преди 4 години
родител
ревизия
d5467a4131
променени са 1 файла, в които са добавени 62 реда и са изтрити 26 реда
  1. 62
    26
      src/main.rs

+ 62
- 26
src/main.rs Целия файл

@@ -2,15 +2,15 @@ use sdl2::pixels::Color;
use sdl2::event::Event;
use sdl2::keyboard::Keycode;
use sdl2::render::{WindowCanvas, Texture};
use sdl2::image::{self, LoadTexture, InitFlag};
use sdl2::rect::{Point, Rect};
use sdl2::image::{self, LoadTexture, InitFlag};

use specs::prelude::*;
use specs_derive::Component;

use std::time::Duration;

const PLAYER_MOVEMENT_SPEED: i32 = 5;
const PLAYER_MOVEMENT_SPEED: i32 = 20;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Direction {
@@ -31,7 +31,7 @@ struct Velocity {
direction: Direction,
}

#[derive(Component, Debug)]
#[derive(Component, Debug, Clone)]
#[storage(VecStorage)]
struct Sprite {
spritesheet: usize,
@@ -67,12 +67,32 @@ fn direction_spritesheet_row(direction: Direction) -> i32 {
}
}

fn render(
canvas: &mut WindowCanvas,
color: Color,
fn character_animation_frames(spritesheet: usize, top_left_frame: Rect, direction: Direction) -> Vec<Sprite> {
let (frame_width, frame_height) = top_left_frame.size();
let y_offset = top_left_frame.y() + frame_height as i32 * direction_spritesheet_row(direction);

let mut frames = Vec::new();
for i in 0..3 {
frames.push(Sprite {
spritesheet,
region: Rect::new(
top_left_frame.x() + frame_width as i32 * i,
y_offset,
frame_width,
frame_height,
)
})
}

return frames
}

fn render (
canvas: &mut WindowCanvas,
color: Color,
texture: &Texture,
player: &Player
) -> Result<(), String> {
) -> Result<(), String> {
canvas.set_draw_color(color);
canvas.clear();

@@ -109,7 +129,7 @@ fn update_player(player: &mut Player) {
},
Down => {
player.position = player.position.offset(0, player.speed);
},
}
}

if player.speed != 0 {
@@ -120,26 +140,40 @@ fn update_player(player: &mut Player) {
fn main() -> Result<(), String> {
let sdl_context = sdl2::init()?;
let video_subsystem = sdl_context.video()?;
let _image_context = image::init(InitFlag::PNG | InitFlag::JPG)?;
let _image_content = image::init(InitFlag::PNG | InitFlag::JPG)?;

let window = video_subsystem.window("game tutorial", 800, 600)
.position_centered()
.build()
.expect("could not initialize video subsystem");

let mut canvas = window.into_canvas().build()
.expect("could not make a canvas");
let mut canvas = window.into_canvas().build().expect("could not make a canvas");

let texture_creator = canvas.texture_creator();
let texture = texture_creator.load_texture("assets/bardo.png")?;

let mut player = Player {
position: Point::new(0,0),
sprite: Rect::new(0, 0, 26, 36),
speed: 0,
direction: Direction::Right,
let textures = [
texture_creator.load_texture("assets/bardo.png")?,
];

let player_spritesheet = 0;
let player_top_left_frame = Rect::new(0, 0, 26, 36);

let player_animation = MovementAnimation {
current_frame: 0,
};
up_frames: character_animation_frames(player_spritesheet, player_top_left_frame, Direction::Up),
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(player_animation.right_frames[0].clone())
.with(player_animation)
.build();

let mut event_pump = sdl_context.event_pump()?;
let mut i = 0;
@@ -150,19 +184,19 @@ fn main() -> Result<(), String> {
Event::KeyDown { keycode: Some(Keycode::Escape), .. } => {
break 'running;
},
Event::KeyDown { keycode: Some(Keycode::Left), .. } => {
Event::KeyDown { keycode: Some(Keycode::Left), repeat: false, .. } => {
player.speed = PLAYER_MOVEMENT_SPEED;
player.direction = Direction::Left;
},
Event::KeyDown { keycode: Some(Keycode::Right), .. } => {
Event::KeyDown { keycode: Some(Keycode::Right), repeat: false, .. } => {
player.speed = PLAYER_MOVEMENT_SPEED;
player.direction = Direction::Right;
},
Event::KeyDown { keycode: Some(Keycode::Up), .. } => {
Event::KeyDown { keycode: Some(Keycode::Up), repeat: false, .. } => {
player.speed = PLAYER_MOVEMENT_SPEED;
player.direction = Direction::Up;
},
Event::KeyDown { keycode: Some(Keycode::Down), .. } => {
Event::KeyDown { keycode: Some(Keycode::Down), repeat: false, .. } => {
player.speed = PLAYER_MOVEMENT_SPEED;
player.direction = Direction::Down;
},
@@ -179,10 +213,12 @@ fn main() -> Result<(), String> {
i = (i + 1) % 255;
update_player(&mut player);

render(&mut canvas, Color::RGB(i, 64, 255-i), &texture, &player)?;
render(&mut canvas, Color::RGB(i, 64, 255 - i), &texture, &player)?;

::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 20));

::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60));
}


Ok(())
}
}

Loading…
Отказ
Запис