The Map
This commit is contained in:
parent
5b7a331c7f
commit
57bdd8de1e
110
src/main.rs
110
src/main.rs
@ -1,16 +1,76 @@
|
||||
#![allow(unused_imports)]
|
||||
#![allow(unused_variables)]
|
||||
#![allow(unused_mut)]
|
||||
#![allow(dead_code)]
|
||||
|
||||
use tcod::colors::*;
|
||||
use tcod::console::*;
|
||||
|
||||
const MAP_WIDTH: i32 = 80;
|
||||
const MAP_HEIGHT: i32 = 45;
|
||||
const SCREEN_WIDTH: i32 = 80;
|
||||
const SCREEN_HEIGHT: i32 = 50;
|
||||
|
||||
const FPS: i32 = 20;
|
||||
const COLOR_DARK_WALL: Color = Color { r: 0, g: 0, b: 100 };
|
||||
const COLOR_DARK_GROUND: Color = Color {
|
||||
r: 50,
|
||||
g: 50,
|
||||
b: 150,
|
||||
};
|
||||
|
||||
struct Tcod {
|
||||
root: Root,
|
||||
con: Offscreen,
|
||||
}
|
||||
|
||||
fn handle_keys(tcod: &mut Tcod, player_x: &mut i32, player_y: &mut i32) -> bool {
|
||||
struct Object {
|
||||
x: i32,
|
||||
y: i32,
|
||||
char: char,
|
||||
color: Color,
|
||||
}
|
||||
|
||||
impl Object {
|
||||
pub fn new(x: i32, y: i32, char: char, color: Color) -> Self {
|
||||
Object { x, y, char, color }
|
||||
}
|
||||
|
||||
// Move by given amount
|
||||
pub fn move_by(&mut self, dx: i32, dy: i32) {
|
||||
self.x += dx;
|
||||
self.y += dy;
|
||||
}
|
||||
|
||||
// Set the color and then draw the character that represents this object at its position
|
||||
pub fn draw(&self, con: &mut dyn Console) {
|
||||
con.set_default_foreground(self.color);
|
||||
con.put_char(self.x, self.y, self.char, BackgroundFlag::None);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
struct Tile {
|
||||
blocked: bool,
|
||||
block_sight: bool,
|
||||
}
|
||||
|
||||
impl Tile {
|
||||
pub fn empty() -> Self {
|
||||
Tile {
|
||||
blocked: false,
|
||||
block_sight: false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn wall() -> Self {
|
||||
Tile {
|
||||
blocked: true,
|
||||
block_sight: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_keys(tcod: &mut Tcod, player: &mut Object) -> bool {
|
||||
use tcod::input::Key;
|
||||
use tcod::input::KeyCode::*;
|
||||
|
||||
@ -28,10 +88,10 @@ fn handle_keys(tcod: &mut Tcod, player_x: &mut i32, player_y: &mut i32) -> bool
|
||||
}
|
||||
Key { code: Escape, .. } => return true, // exit game
|
||||
|
||||
Key { code: Up, .. } => *player_y -= 1,
|
||||
Key { code: Down, .. } => *player_y += 1,
|
||||
Key { code: Left, .. } => *player_x -= 1,
|
||||
Key { code: Right, .. } => *player_x += 1,
|
||||
Key { code: Up, .. } => player.move_by(0, -1),
|
||||
Key { code: Down, .. } => player.move_by(0, 1),
|
||||
Key { code: Left, .. } => player.move_by(-1, 0),
|
||||
Key { code: Right, .. } => player.move_by(1, 0),
|
||||
|
||||
_ => {}
|
||||
}
|
||||
@ -48,19 +108,39 @@ fn main() {
|
||||
.title("Rust/libtcod tutorial")
|
||||
.init();
|
||||
|
||||
let mut tcod = Tcod { root };
|
||||
let con = Offscreen::new(SCREEN_WIDTH, SCREEN_HEIGHT);
|
||||
|
||||
let mut player_x = SCREEN_WIDTH / 2;
|
||||
let mut player_y = SCREEN_HEIGHT / 2;
|
||||
let mut tcod = Tcod { root, con };
|
||||
|
||||
// Create player object
|
||||
let player = Object::new(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, '@', WHITE);
|
||||
|
||||
// // Create an NPC
|
||||
let npc = Object::new(SCREEN_WIDTH / 2 - 5, SCREEN_HEIGHT / 2, '@', YELLOW);
|
||||
|
||||
// List of objects in the world
|
||||
let mut objects = [player, npc];
|
||||
|
||||
while !tcod.root.window_closed() {
|
||||
tcod.root.set_default_foreground(WHITE);
|
||||
tcod.root.clear();
|
||||
tcod.root
|
||||
.put_char(player_x, player_y, '@', BackgroundFlag::None);
|
||||
tcod.con.clear();
|
||||
|
||||
for object in &objects {
|
||||
object.draw(&mut tcod.con)
|
||||
}
|
||||
|
||||
blit(
|
||||
&tcod.con,
|
||||
(0, 0),
|
||||
(SCREEN_WIDTH, SCREEN_HEIGHT),
|
||||
&mut tcod.root,
|
||||
(0, 0),
|
||||
1.0,
|
||||
1.0,
|
||||
);
|
||||
tcod.root.flush();
|
||||
tcod.root.wait_for_keypress(true);
|
||||
let exit = handle_keys(&mut tcod, &mut player_x, &mut player_y);
|
||||
|
||||
let player = &mut objects[0];
|
||||
let exit = handle_keys(&mut tcod, player);
|
||||
if exit {
|
||||
break;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user