Skip to content
This repository has been archived by the owner on Jul 1, 2024. It is now read-only.

Commit

Permalink
Make player report its existence to ECS
Browse files Browse the repository at this point in the history
  • Loading branch information
Mubelotix committed Nov 14, 2023
1 parent 40b36ae commit 5c6aee4
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions minecraft-server/src/player_handler/play.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::*;

struct PlayerHandler {
eid: Eid,
world: &'static World,
game_mode: Gamemode,
info: PlayerInfo,
Expand Down Expand Up @@ -82,6 +83,13 @@ impl PlayerHandler {
async fn on_move(&mut self) {
let new_center_chunk = self.position.chunk();

// Tell the ECS about the changes
self.world.mutate_entity(self.eid, |entity| {
let entity: &mut Entity = entity.try_as_entity_mut().unwrap(); // Cannot fail
entity.position = self.position.clone();
((), EntityChanges::position())
}).await;

// Tell the client which chunk he is in
if new_center_chunk == self.center_chunk { return };
self.send_packet(PlayClientbound::SetCenterChunk { chunk_x: VarInt(new_center_chunk.cx), chunk_z: VarInt(new_center_chunk.cz) }).await;
Expand Down Expand Up @@ -208,8 +216,10 @@ impl PlayerHandler {

pub async fn handle_player(stream: TcpStream, player_info: PlayerInfo, mut server_msg_rcvr: BroadcastReceiver<ServerMessage>, world: &'static World, mut change_receiver: MpscReceiver<WorldChange>) -> Result<(), ()> {
let (packet_sender, mut packet_receiver) = mpsc_channel(100);
let eid = world.spawn_entity(AnyEntity::Player(Player::default())).await;

let mut handler = PlayerHandler {
eid,
world,
game_mode: Gamemode::Creative,
position: Position { x: 0.0, y: 60.0, z: 0.0 },
Expand All @@ -227,6 +237,8 @@ pub async fn handle_player(stream: TcpStream, player_info: PlayerInfo, mut serve
info: player_info,
};

// TODO: player should load existing entities

for cx in -3..=3 {
for cz in -3..=3 {
handler.loaded_chunks.insert(ChunkColumnPosition { cx, cz });
Expand Down

0 comments on commit 5c6aee4

Please sign in to comment.