hello ecs implementation from rltk book
This commit is contained in:
parent
bc3378505b
commit
c4d6ff944c
1
.gitignore
vendored
1
.gitignore
vendored
@ -9,4 +9,3 @@ Cargo.lock
|
||||
|
||||
# These are backup files generated by rustfmt
|
||||
**/*.rs.bk
|
||||
|
||||
|
||||
1758
Cargo.lock
generated
Normal file
1758
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
12
Cargo.toml
Normal file
12
Cargo.toml
Normal file
@ -0,0 +1,12 @@
|
||||
[package]
|
||||
name = "hellorust"
|
||||
version = "0.1.0"
|
||||
authors = ["Peter Hart <cinphart@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
rltk = { version = "0.7.0" }
|
||||
specs = "0.16.1"
|
||||
specs-derive = "0.4.1"
|
||||
72
src/main.rs
Normal file
72
src/main.rs
Normal file
@ -0,0 +1,72 @@
|
||||
use rltk::{Console, GameState, Rltk, RGB, VirtualKeyCode};
|
||||
use specs::prelude::*;
|
||||
use std::cmp::{max, min};
|
||||
|
||||
#[macro_use]
|
||||
extern crate specs_derive;
|
||||
|
||||
#[derive(Component)]
|
||||
struct Position {
|
||||
x: i32,
|
||||
y: i32,
|
||||
}
|
||||
|
||||
#[derive(Component)]
|
||||
struct Renderable {
|
||||
glyph: u8,
|
||||
fg: RGB,
|
||||
bg: RGB,
|
||||
}
|
||||
|
||||
struct State {
|
||||
ecs: World
|
||||
}
|
||||
|
||||
impl GameState for State {
|
||||
fn tick(&mut self, ctx : &mut Rltk) {
|
||||
ctx.cls();
|
||||
let positions = self.ecs.read_storage::<Position>();
|
||||
let renderables = self.ecs.read_storage::<Renderable>();
|
||||
|
||||
for (pos, render) in (&positions, &renderables).join() {
|
||||
ctx.set(pos.x, pos.y, render.fg, render.bg, render.glyph);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
use rltk::RltkBuilder;
|
||||
let context = RltkBuilder::simple80x50()
|
||||
.with_title("Roguelike Tutorial")
|
||||
.build();
|
||||
let mut gs = State {
|
||||
ecs: World::new()
|
||||
};
|
||||
gs.ecs.register::<Position>();
|
||||
gs.ecs.register::<Renderable>();
|
||||
|
||||
gs.ecs
|
||||
.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 {
|
||||
gs.ecs
|
||||
.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),
|
||||
})
|
||||
.build();
|
||||
}
|
||||
|
||||
rltk::main_loop(context, gs);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user