环境
- 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 {}
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;
}
}
}
}
GameState
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();
}
}
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
.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)
}
效果
总结
将前一节渲染的字符进行了向左移动。
附录
源码
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,rltk,mut,self,渲染,pos,0236,state,world
From: https://www.cnblogs.com/jiangbo4444/p/18363125