Skip to content

Commit ec5ae04

Browse files
committed
feat: add dungeon crawl with graphic
1 parent ed81b35 commit ec5ae04

File tree

5 files changed

+76
-18
lines changed

5 files changed

+76
-18
lines changed
Loading
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use crate::prelude::*;
2+
3+
pub struct Camera {
4+
pub left_x: i32,
5+
pub right_x: i32,
6+
pub top_y: i32,
7+
pub bottom_y: i32,
8+
}
9+
10+
impl Camera {
11+
pub fn new(player_position: Point) -> Self {
12+
Self {
13+
left_x: player_position.x - DISPLAY_WIDTH / 2,
14+
right_x: player_position.x + DISPLAY_WIDTH / 2,
15+
top_y: player_position.y - DISPLAY_HEIGHT / 2,
16+
bottom_y: player_position.y + DISPLAY_HEIGHT / 2,
17+
}
18+
}
19+
20+
pub fn on_player_move(&mut self, player_position: Point) {
21+
self.left_x = player_position.x - DISPLAY_WIDTH / 2;
22+
self.right_x = player_position.x + DISPLAY_WIDTH / 2;
23+
self.top_y = player_position.y - DISPLAY_HEIGHT / 2;
24+
self.bottom_y = player_position.y + DISPLAY_HEIGHT / 2;
25+
}
26+
}

books/hands-on-rust/dungeoncrawl/src/main.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
1+
mod camera;
12
mod map;
23
mod map_builder;
34
mod player;
45
mod prelude {
56
pub use bracket_lib::prelude::*;
67
pub const SCREEN_WIDTH: i32 = 80;
78
pub const SCREEN_HEIGHT: i32 = 50;
9+
pub use crate::camera::*;
810
pub use crate::map::*;
911
pub use crate::map_builder::*;
1012
pub use crate::player::*;
13+
pub const DISPLAY_WIDTH: i32 = SCREEN_WIDTH / 2;
14+
pub const DISPLAY_HEIGHT: i32 = SCREEN_HEIGHT / 2;
1115
}
1216

1317
use prelude::*;
1418

1519
struct State {
1620
map: Map,
1721
player: Player,
22+
camera: Camera,
1823
}
1924

2025
impl State {
@@ -24,23 +29,33 @@ impl State {
2429
Self {
2530
map: map_builder.map,
2631
player: Player::new(map_builder.player_start),
32+
camera: Camera::new(map_builder.player_start),
2733
}
2834
}
2935
}
3036

3137
impl GameState for State {
3238
fn tick(&mut self, ctx: &mut BTerm) {
39+
ctx.set_active_console(0);
3340
ctx.cls();
34-
self.player.update(ctx, &self.map);
35-
self.map.render(ctx);
36-
self.player.render(ctx);
41+
ctx.set_active_console(1);
42+
ctx.cls();
43+
self.player.update(ctx, &self.map, &mut self.camera);
44+
self.map.render(ctx, &self.camera);
45+
self.player.render(ctx, &self.camera);
3746
}
3847
}
3948

4049
fn main() -> BError {
41-
let context = BTermBuilder::simple80x50()
50+
let context = BTermBuilder::new()
4251
.with_title("Dungeon Crawler")
4352
.with_fps_cap(30.0)
53+
.with_dimensions(DISPLAY_WIDTH, DISPLAY_HEIGHT)
54+
.with_tile_dimensions(32, 32)
55+
.with_resource_path("resources/")
56+
.with_font("dungeonfont.png", 32, 32)
57+
.with_simple_console(DISPLAY_WIDTH, DISPLAY_HEIGHT, "dungeonfont.png")
58+
.with_simple_console_no_bg(DISPLAY_WIDTH, DISPLAY_HEIGHT, "dungeonfont.png")
4459
.build()?;
4560

4661
main_loop(context, State::new())

books/hands-on-rust/dungeoncrawl/src/map.rs

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,31 @@ impl Map {
2222
}
2323
}
2424

25-
pub fn render(&self, ctx: &mut BTerm) {
26-
for y in 0..SCREEN_HEIGHT {
27-
for x in 0..SCREEN_WIDTH {
28-
let idx = map_idx(x, y);
29-
match self.tiles[idx] {
30-
TileType::Floor => {
31-
ctx.set(x, y, YELLOW, BLACK, to_cp437('.'));
32-
}
33-
TileType::Wall => {
34-
ctx.set(x, y, GREEN, BLACK, to_cp437('#'));
25+
pub fn render(&self, ctx: &mut BTerm, camera: &Camera) {
26+
ctx.set_active_console(0);
27+
for y in camera.top_y..camera.bottom_y {
28+
for x in camera.left_x..camera.right_x {
29+
if self.in_bounds(Point::new(x, y)) {
30+
let idx = map_idx(x, y);
31+
match self.tiles[idx] {
32+
TileType::Floor => {
33+
ctx.set(
34+
x - camera.left_x,
35+
y - camera.top_y,
36+
WHITE,
37+
BLACK,
38+
to_cp437('.'),
39+
);
40+
}
41+
TileType::Wall => {
42+
ctx.set(
43+
x - camera.left_x,
44+
y - camera.top_y,
45+
WHITE,
46+
BLACK,
47+
to_cp437('#'),
48+
);
49+
}
3550
}
3651
}
3752
}

books/hands-on-rust/dungeoncrawl/src/player.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,18 @@ impl Player {
99
Self { position }
1010
}
1111

12-
pub fn render(&self, ctx: &mut BTerm) {
12+
pub fn render(&self, ctx: &mut BTerm, camera: &Camera) {
13+
ctx.set_active_console(1);
1314
ctx.set(
14-
self.position.x,
15-
self.position.y,
15+
self.position.x - camera.left_x,
16+
self.position.y - camera.top_y,
1617
WHITE,
1718
BLACK,
1819
to_cp437('@'),
1920
);
2021
}
2122

22-
pub fn update(&mut self, ctx: &mut BTerm, map: &Map) {
23+
pub fn update(&mut self, ctx: &mut BTerm, map: &Map, camera: &mut Camera) {
2324
if let Some(key) = ctx.key {
2425
let delta = match key {
2526
VirtualKeyCode::Left => Point::new(-1, 0),
@@ -32,6 +33,7 @@ impl Player {
3233
let new_position = self.position + delta;
3334
if map.can_enter_tile(new_position) {
3435
self.position = new_position;
36+
camera.on_player_move(new_position);
3537
}
3638
}
3739
}

0 commit comments

Comments
 (0)