walk-aroundy-thing
This commit is contained in:
parent
5d386e72ba
commit
95a2ffc13e
|
@ -629,6 +629,7 @@ dependencies = [
|
||||||
name = "mw-rl"
|
name = "mw-rl"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"rand 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"rltk 0.2.5 (git+https://github.com/thebracket/rltk_rs)",
|
"rltk 0.2.5 (git+https://github.com/thebracket/rltk_rs)",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,7 @@ keywords = ["game", "roguelike", "comedy", "satire"]
|
||||||
license = "GPLv3"
|
license = "GPLv3"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
rand = "0.7"
|
||||||
rltk = { git = "https://github.com/thebracket/rltk_rs" }
|
rltk = { git = "https://github.com/thebracket/rltk_rs" }
|
||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
Trying my hand at building a roguelike with `rltk`, the roguelike toolkit.
|
Trying my hand at building a roguelike with `rltk`, the roguelike toolkit.
|
||||||
|
|
||||||
Right now it's just a simple window that says `MORTAL WOMBAT MFERS`. For a silly screencast, [click here](https://tildegit.org/MortalWombat/mw-rl/src/branch/master/assets/mw-rl-screencast.mp4)
|
Right now it's just some arbitrary walk-aroundy-thing. For a silly screencast, [click here](https://tildegit.org/MortalWombat/mw-rl/raw/branch/master/assets/mw-rl-screencast.mp4)
|
||||||
|
|
||||||
```
|
```
|
||||||
$ git clone https://tildegit.org/mortalwombat/mw-rl.git
|
$ git clone https://tildegit.org/mortalwombat/mw-rl.git
|
||||||
|
|
Binary file not shown.
167
src/main.rs
167
src/main.rs
|
@ -1,64 +1,141 @@
|
||||||
use rltk::{Console, GameState, Rltk, RGB};
|
use rand::Rng;
|
||||||
|
use rltk::{Console, GameState, Rltk, VirtualKeyCode, RGB};
|
||||||
|
|
||||||
|
#[derive(PartialEq, Copy, Clone)]
|
||||||
|
enum TileType {
|
||||||
|
Wall,
|
||||||
|
Floor,
|
||||||
|
}
|
||||||
|
|
||||||
struct State {
|
struct State {
|
||||||
y: i32,
|
map: Vec<TileType>,
|
||||||
descending: bool,
|
player_position: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn xy_idx(x: i32, y: i32) -> usize {
|
||||||
|
(y as usize * 80) + x as usize
|
||||||
|
}
|
||||||
|
|
||||||
|
fn idx_xy(idx: usize) -> (i32, i32) {
|
||||||
|
(idx as i32 % 80, idx as i32 / 80)
|
||||||
|
}
|
||||||
|
|
||||||
|
impl State {
|
||||||
|
fn new() -> State {
|
||||||
|
let mut state = State {
|
||||||
|
map: vec![TileType::Floor; 80 * 50],
|
||||||
|
player_position: xy_idx(40, 25),
|
||||||
|
};
|
||||||
|
|
||||||
|
(0..80).into_iter().for_each(|x| {
|
||||||
|
state.map[xy_idx(x, 0)] = TileType::Wall;
|
||||||
|
state.map[xy_idx(x, 49)] = TileType::Wall;
|
||||||
|
});
|
||||||
|
|
||||||
|
(0..50).into_iter().for_each(|y| {
|
||||||
|
state.map[xy_idx(0, y)] = TileType::Wall;
|
||||||
|
state.map[xy_idx(79, y)] = TileType::Wall;
|
||||||
|
});
|
||||||
|
|
||||||
|
let mut rng = rand::thread_rng();
|
||||||
|
|
||||||
|
(0..400).into_iter().for_each(|_| {
|
||||||
|
let x = rng.gen_range(1, 79);
|
||||||
|
let y = rng.gen_range(1, 49);
|
||||||
|
let idx = xy_idx(x, y);
|
||||||
|
|
||||||
|
if state.player_position != idx {
|
||||||
|
state.map[idx] = TileType::Wall;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
state
|
||||||
|
}
|
||||||
|
|
||||||
|
fn move_player(&mut self, dx: i32, dy: i32) {
|
||||||
|
let cur_pos = idx_xy(self.player_position);
|
||||||
|
let new_pos = (cur_pos.0 + dx, cur_pos.1 + dy);
|
||||||
|
let new_idx = xy_idx(new_pos.0, new_pos.1);
|
||||||
|
if self.map[new_idx] == TileType::Floor {
|
||||||
|
self.player_position = new_idx;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl GameState for State {
|
impl GameState for State {
|
||||||
fn tick(&mut self, context: &mut Rltk) {
|
fn tick(&mut self, ctx: &mut Rltk) {
|
||||||
let col1 = RGB::named(rltk::CYAN);
|
match ctx.key {
|
||||||
let col2 = RGB::named(rltk::YELLOW);
|
None => {}
|
||||||
let pct: f32 = self.y as f32 / 50.0;
|
Some(key) => {
|
||||||
let fg = col1.lerp(col2, pct);
|
match key {
|
||||||
|
//numpad
|
||||||
|
VirtualKeyCode::Numpad8 => self.move_player(0, -1),
|
||||||
|
VirtualKeyCode::Numpad4 => self.move_player(-1, 0),
|
||||||
|
VirtualKeyCode::Numpad6 => self.move_player(1, 0),
|
||||||
|
VirtualKeyCode::Numpad2 => self.move_player(0, 1),
|
||||||
|
|
||||||
context.cls();
|
//diag
|
||||||
context.print_color(
|
VirtualKeyCode::Numpad7 => self.move_player(-1, -1),
|
||||||
1,
|
VirtualKeyCode::Numpad9 => self.move_player(1, -1),
|
||||||
self.y,
|
VirtualKeyCode::Numpad1 => self.move_player(-1, 1),
|
||||||
fg,
|
VirtualKeyCode::Numpad3 => self.move_player(1, 1),
|
||||||
RGB::named(rltk::BLACK),
|
|
||||||
"MORTAL WOMBAT MFERS",
|
|
||||||
);
|
|
||||||
|
|
||||||
if self.descending {
|
//arrows
|
||||||
self.y += 1;
|
VirtualKeyCode::Up => self.move_player(0, -1),
|
||||||
if self.y > 48 {
|
VirtualKeyCode::Down => self.move_player(0, 1),
|
||||||
self.descending = false;
|
VirtualKeyCode::Left => self.move_player(-1, 0),
|
||||||
}
|
VirtualKeyCode::Right => self.move_player(1, 0),
|
||||||
} else {
|
_ => {}
|
||||||
self.y -= 1;
|
}
|
||||||
if self.y < 1 {
|
|
||||||
self.descending = true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
context.draw_box(
|
ctx.cls();
|
||||||
39,
|
|
||||||
0,
|
|
||||||
20,
|
|
||||||
3,
|
|
||||||
RGB::named(rltk::WHITE),
|
|
||||||
RGB::named(rltk::BLACK),
|
|
||||||
);
|
|
||||||
|
|
||||||
context.print_color(
|
let mut y = 0;
|
||||||
40,
|
let mut x = 0;
|
||||||
2,
|
&self.map.iter().for_each(|tile| {
|
||||||
RGB::named(rltk::CYAN),
|
match tile {
|
||||||
RGB::named(rltk::BLACK),
|
TileType::Floor => {
|
||||||
&format!("Frame time: {}ms", context.frame_time_ms),
|
ctx.print_color(
|
||||||
)
|
x,
|
||||||
|
y,
|
||||||
|
RGB::from_f32(0.5, 0.5, 0.5),
|
||||||
|
RGB::from_f32(0., 0., 0.),
|
||||||
|
".",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
TileType::Wall => {
|
||||||
|
ctx.print_color(
|
||||||
|
x,
|
||||||
|
y,
|
||||||
|
RGB::from_f32(0.0, 1.0, 0.0),
|
||||||
|
RGB::from_f32(0., 0., 0.),
|
||||||
|
"#",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
x += 1;
|
||||||
|
if x > 79 {
|
||||||
|
x = 0;
|
||||||
|
y += 1;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
let ppos = idx_xy(self.player_position);
|
||||||
|
ctx.print_color(
|
||||||
|
ppos.0,
|
||||||
|
ppos.1,
|
||||||
|
RGB::from_f32(1.0, 1.0, 0.0),
|
||||||
|
RGB::from_f32(0., 0., 0.),
|
||||||
|
"@",
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let context =
|
let context =
|
||||||
Rltk::init_simple8x8(80, 50, "Mortal Wombat", "resources");
|
Rltk::init_simple8x8(80, 50, "Mortal Wombat", "resources");
|
||||||
let gs = State {
|
let gs = State::new();
|
||||||
y: 1,
|
|
||||||
descending: true,
|
|
||||||
};
|
|
||||||
|
|
||||||
rltk::main_loop(context, gs);
|
rltk::main_loop(context, gs);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue