Browse Source

Display item selection

master
Garrit Franke 3 years ago
parent
commit
49c940e3fa
  1. 48
      src/main.rs
  2. 10
      src/map.rs

48
src/main.rs

@ -17,6 +17,7 @@ struct State {
loaded_chunks: Box<HashMap<(i32, i32), Chunk>>,
world_pos: (i32, i32),
player_position: usize,
selected_object: Option<TileType>,
visible: Vec<bool>,
}
@ -49,6 +50,7 @@ impl State {
loaded_chunks: Box::new(chunks),
player_position: (25 * 80) + 40, // Equivalent to point2d_to_index(40, 25) but we haven't initialized it yet
visible: vec![false; 80 * 50],
selected_object: None,
};
state
@ -103,12 +105,23 @@ impl State {
}
}
}
fn select_object(&mut self, pos: Point) {
let idx = self.point2d_to_index(pos);
match self.view.get(idx) {
Some(tile) => self.selected_object = Some(*tile),
None => {}
}
}
}
// Implement the game loop
impl GameState for State {
#[allow(non_snake_case)]
fn tick(&mut self, ctx: &mut Rltk) {
// We'll use batched drawing
let mut draw_batch = DrawBatch::new();
match ctx.key {
None => {} // Nothing happened
Some(key) => {
@ -159,7 +172,7 @@ impl GameState for State {
}
// Clear the screen
ctx.cls();
draw_batch.cls();
// Iterate the map array, incrementing coordinates as we go.
let mut y = 0;
@ -181,7 +194,12 @@ impl GameState for State {
if !self.visible[i] {
fg = fg.to_greyscale();
}
ctx.print_color(x, y, fg, RGB::from_f32(0., 0., 0.), glyph);
draw_batch.print_color(
Point::new(x, y),
glyph,
ColorPair::new(fg, RGB::from_f32(0., 0., 0.)),
);
// Move the coordinates
x += 1;
@ -190,16 +208,32 @@ impl GameState for State {
y += 1;
}
}
if ctx.left_click {
let mouse_pos = ctx.mouse_pos();
self.select_object(Point::from(mouse_pos));
}
// Display selected object below the screen
let selected_object_text = match self.selected_object {
Some(obj) => format!("{}", obj),
None => "".to_string(),
};
draw_batch.print_color(
Point::new(0, 51),
selected_object_text,
ColorPair::new(RGB::from_f32(1.0, 1.0, 0.0), RGB::from_f32(0., 0., 0.)),
);
// Render the player @ symbol
let ppos = self.index_to_point2d(self.player_position);
ctx.print_color(
ppos.x,
ppos.y,
RGB::from_f32(1.0, 1.0, 0.0),
RGB::from_f32(0., 0., 0.),
draw_batch.print_color(
Point::new(ppos.x, ppos.y),
"@",
ColorPair::new(RGB::from_f32(1.0, 1.0, 0.0), RGB::from_f32(0., 0., 0.)),
);
draw_batch.submit(0).expect("Batch error");
render_draw_buffer(ctx).expect("Render error");
}
}

10
src/map.rs

@ -1,4 +1,5 @@
use rltk::RandomNumberGenerator;
use std::fmt::Display;
pub type Chunk = Vec<TileType>;
@ -8,6 +9,15 @@ pub enum TileType {
Floor,
}
impl Display for TileType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
match self {
Self::Floor => f.write_str("Floor"),
Self::Wall => f.write_str("Wall"),
}
}
}
pub fn new_chunk(seed: u64) -> Chunk {
let mut map = vec![TileType::Floor; 80 * 50];
let count = map.len();

Loading…
Cancel
Save