Skip to content

Commit 47f67dd

Browse files
feat: collide players (#13)
* feat: implemented collisions between player entities * chore: refactored method locations into appropriate modules * chore: limited pub methods to pub(crate) * feat: added DisplayDetails to entity updates
1 parent a0ec9cd commit 47f67dd

File tree

24 files changed

+884
-727
lines changed

24 files changed

+884
-727
lines changed

rustyhack_client/src/consts.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
pub const VIEWPORT_WIDTH: u32 = 41;
2-
pub const VIEWPORT_HEIGHT: u32 = 15;
3-
pub const TARGET_FPS: u32 = 10;
4-
pub const LOG_NAME: &str = "rustyhack_client.log";
5-
pub const GAME_TITLE: &str = "Rustyhack MMO";
6-
pub const VALID_NAME_REGEX: &str = "^[[:alpha:]]+$";
1+
pub(crate) const VIEWPORT_WIDTH: u32 = 41;
2+
pub(crate) const VIEWPORT_HEIGHT: u32 = 15;
3+
pub(crate) const TARGET_FPS: u32 = 10;
4+
pub(crate) const LOG_NAME: &str = "rustyhack_client.log";
5+
pub(crate) const GAME_TITLE: &str = "Rustyhack MMO";
6+
pub(crate) const VALID_NAME_REGEX: &str = "^[[:alpha:]]+$";

rustyhack_client/src/engine.rs

Lines changed: 0 additions & 284 deletions
This file was deleted.

rustyhack_client/src/game.rs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
mod map_handler;
2+
mod new_player;
3+
mod updates_handler;
4+
pub(crate) mod viewport;
5+
6+
use crate::consts::{GAME_TITLE, TARGET_FPS, VIEWPORT_HEIGHT, VIEWPORT_WIDTH};
7+
use crate::game::viewport::Viewport;
8+
use crate::networking::message_handler;
9+
use console_engine::{ConsoleEngine, KeyCode, KeyModifiers};
10+
use crossbeam_channel::{Receiver, Sender};
11+
use laminar::{Packet, SocketEvent};
12+
use rustyhack_lib::message_handler::player_message::EntityUpdates;
13+
use std::collections::HashMap;
14+
use std::process;
15+
16+
pub(crate) fn run(
17+
sender: Sender<Packet>,
18+
receiver: Receiver<SocketEvent>,
19+
server_addr: &str,
20+
client_addr: &str,
21+
player_name: &str,
22+
) {
23+
let (player_update_sender, player_update_receiver) = crossbeam_channel::unbounded();
24+
let (entity_update_sender, entity_update_receiver) = crossbeam_channel::unbounded();
25+
debug!("Spawned thread channels.");
26+
let local_sender = sender.clone();
27+
28+
message_handler::spawn_message_handler_thread(
29+
sender,
30+
receiver,
31+
player_update_sender,
32+
entity_update_sender,
33+
);
34+
35+
let all_maps =
36+
map_handler::request_all_maps_data(&local_sender, &server_addr, &player_update_receiver);
37+
38+
let mut player = new_player::send_new_player_request(
39+
&local_sender,
40+
player_name,
41+
&server_addr,
42+
&client_addr,
43+
&player_update_receiver,
44+
);
45+
46+
let mut viewport = Viewport::new(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, TARGET_FPS);
47+
48+
let mut console =
49+
console_engine::ConsoleEngine::init(viewport.width, viewport.height, viewport.target_fps);
50+
console.set_title(GAME_TITLE);
51+
info!("Initialised console engine.");
52+
53+
let mut other_entities = EntityUpdates {
54+
position_updates: HashMap::new(),
55+
display_details: HashMap::new(),
56+
};
57+
58+
info!("Starting game loop");
59+
loop {
60+
console.wait_frame();
61+
console.clear_screen();
62+
63+
debug!("About to send player velocity update.");
64+
updates_handler::send_player_updates(&local_sender, &console, &player, &server_addr);
65+
66+
debug!("About to wait for entity updates from server.");
67+
player =
68+
updates_handler::check_for_received_player_updates(&player_update_receiver, player);
69+
other_entities = updates_handler::check_for_received_entity_updates(
70+
&entity_update_receiver,
71+
other_entities,
72+
);
73+
74+
viewport.draw_viewport_contents(
75+
&mut console,
76+
&player,
77+
all_maps.get(&player.position.map).unwrap_or_else(|| {
78+
error!(
79+
"There is no map for current player position: {}",
80+
&player.position.map
81+
);
82+
process::exit(1);
83+
}),
84+
&other_entities,
85+
);
86+
87+
if should_quit(&console) {
88+
info!("Ctrl-q detected - quitting app.");
89+
break;
90+
}
91+
}
92+
}
93+
94+
fn should_quit(console: &ConsoleEngine) -> bool {
95+
console.is_key_pressed_with_modifier(KeyCode::Char('q'), KeyModifiers::CONTROL)
96+
}

0 commit comments

Comments
 (0)