环境
- Time 2022-11-29
- WSL-Ubuntu 22.04
- RLTK 0.8.7
前言
说明
参考:https://bfnightly.bracketproductions.com/rustbook
目标
使用键盘来控制角色在窗口中进行移动。
Component
#[derive(Component)]
struct Position {
x: i32,
y: i32,
}
#[derive(Component)]
struct Renderable {
glyph: rltk::FontCharType,
fg: RGB,
bg: RGB,
}
#[derive(Component)]
struct LeftWalker {}
#[derive(Component, Debug)]
struct Player {}
System
impl<'a> System<'a> for LeftWalker {
type SystemData = (ReadStorage<'a, LeftWalker>, WriteStorage<'a, Position>);
fn run(&mut self, (lefty, mut pos): Self::SystemData) {
for (_lefty, pos) in (&lefty, &mut pos).join() {
pos.x -= 1;
if pos.x < 0 {
pos.x = 79;
}
}
}
}
fn move_player(delta_x: i32, delta_y: i32, world: &mut World) {
let mut positions = world.write_storage::<Position>();
let mut players = world.write_storage::<Player>();
for (_player, pos) in (&mut players, &mut positions).join() {
pos.x = min(79, max(0, pos.x + delta_x));
pos.y = min(49, max(0, pos.y + delta_y));
}
}
fn player_input(state: &mut State, context: &mut Rltk) {
if let Some(key) = context.key {
match key {
VirtualKeyCode::Left => move_player(-1, 0, &mut state.world),
VirtualKeyCode::Right => move_player(1, 0, &mut state.world),
VirtualKeyCode::Up => move_player(0, -1, &mut state.world),
VirtualKeyCode::Down => move_player(0, 1, &mut state.world),
_ => {}
}
}
}
GameState
struct State {
world: World,
}
impl GameState for State {
fn tick(&mut self, ctx: &mut Rltk) {
ctx.cls();
player_input(self, ctx);
self.run_systems();
let positions = self.world.read_storage::<Position>();
let renderables = self.world.read_storage::<Renderable>();
for (pos, render) in (&positions, &renderables).join() {
ctx.set(pos.x, pos.y, render.fg, render.bg, render.glyph);
}
}
}
impl State {
fn run_systems(&mut self) {
let mut lw = LeftWalker {};
lw.run_now(&self.world);
self.world.maintain();
}
}
main
fn main() -> rltk::BError {
let context = rltk::RltkBuilder::simple80x50()
.with_title("冒险游戏")
.build()?;
let mut state = State {
world: World::new(),
};
state.world.register::<Position>();
state.world.register::<Renderable>();
state.world.register::<LeftWalker>();
state.world.register::<Player>();
state
.world
.create_entity()
.with(Position { x: 40, y: 25 })
.with(Renderable {
glyph: rltk::to_cp437('@'),
fg: RGB::named(rltk::YELLOW),
bg: RGB::named(rltk::BLACK),
})
.with(Player {})
.build();
for i in 0..10 {
state
.world
.create_entity()
.with(Position { x: i * 7, y: 20 })
.with(Renderable {
glyph: rltk::to_cp437('☺'),
fg: RGB::named(rltk::RED),
bg: RGB::named(rltk::BLACK),
})
.with(LeftWalker {})
.build();
}
rltk::main_loop(context, state)
}
效果
总结
使用键盘来控制角色移动。
附录
源码
use rltk::prelude::*;
use specs::prelude::*;
use specs_derive::Component;
struct State {
world: World,
}
impl GameState for State {
fn tick(&mut self, ctx: &mut Rltk) {
ctx.cls();
self.run_systems();
let positions = self.world.read_storage::<Position>();
let renderables = self.world.read_storage::<Renderable>();
for (pos, render) in (&positions, &renderables).join() {
ctx.set(pos.x, pos.y, render.fg, render.bg, render.glyph);
}
}
}
impl State {
fn run_systems(&mut self) {
let mut lw = LeftWalker {};
lw.run_now(&self.world);
self.world.maintain();
}
}
fn main() -> rltk::BError {
let context = rltk::RltkBuilder::simple80x50()
.with_title("冒险游戏")
.build()?;
let mut state = State {
world: World::new(),
};
state.world.register::<Position>();
state.world.register::<Renderable>();
state.world.register::<LeftWalker>();
state
.world
.create_entity()
.with(Position { x: 40, y: 25 })
.with(Renderable {
glyph: rltk::to_cp437('@'),
fg: RGB::named(rltk::YELLOW),
bg: RGB::named(rltk::BLACK),
})
.build();
for i in 0..10 {
state
.world
.create_entity()
.with(Position { x: i * 7, y: 20 })
.with(Renderable {
glyph: rltk::to_cp437('☺'),
fg: RGB::named(rltk::RED),
bg: RGB::named(rltk::BLACK),
})
.with(LeftWalker {})
.build();
}
rltk::main_loop(context, state)
}
#[derive(Component)]
struct Position {
x: i32,
y: i32,
}
#[derive(Component)]
struct Renderable {
glyph: rltk::FontCharType,
fg: RGB,
bg: RGB,
}
#[derive(Component)]
struct LeftWalker {}
impl<'a> System<'a> for LeftWalker {
type SystemData = (ReadStorage<'a, LeftWalker>, WriteStorage<'a, Position>);
fn run(&mut self, (lefty, mut pos): Self::SystemData) {
for (_lefty, pos) in (&lefty, &mut pos).join() {
pos.x -= 1;
if pos.x < 0 {
pos.x = 79;
}
}
}
}
标签:RLTK,角色,mut,self,0237,pos,state,world,rltk
From: https://www.cnblogs.com/jiangbo4444/p/18363127