|
|
|
@ -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"); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|