mirror of https://github.com/zyllian/classics.git synced 2025-05-10 04:46:40 -07:00

update spawn config to use yaw and orientation

This commit is contained in:
Zoey 2024-04-23 21:52:34 -07:00
parent 5e8fe5a753
commit 87a2111751
No known key found for this signature in database
GPG key ID: 8611B896D1AAFAF2
2 changed files with 41 additions and 12 deletions

View file

@ -21,7 +21,7 @@ pub struct ServerConfig {
/// the level's size
pub level_size: ConfigCoordinates,
/// the level's spawn point
pub spawn: Option<ConfigCoordinates>,
pub spawn: Option<ConfigCoordinatesWithOrientation>,
/// the method to generate the server's level with
pub generation: LevelGeneration,
/// the server should auto save the world every X minutes, 0 to disable
@ -65,6 +65,18 @@ pub struct ConfigCoordinates {
pub z: usize,
}
/// coordinates stored in config including orientation
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ConfigCoordinatesWithOrientation {
/// the inner coordinates
#[serde(flatten)]
pub coords: ConfigCoordinates,
/// the orientation's yaw
pub yaw: u8,
/// the orientation's pitch
pub pitch: u8,
}
/// enum for the different kinds of server protection
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(untagged)]

View file

@ -176,11 +176,10 @@ async fn handle_stream_inner(
.copied()
.unwrap_or_default();
let player = Player {
let mut player = Player {
_addr: addr,
id: *own_id, // TODO: actually assign user ids
username,
// TODO: properly assign spawn stuff
x: zero,
y: zero,
z: zero,
@ -203,23 +202,41 @@ async fn handle_stream_inner(
.extend(build_level_packets(&data.level).into_iter());
let username = player.username.clone();
data.players.push(player);
let (spawn_x, spawn_y, spawn_z) =
let (spawn_x, spawn_y, spawn_z, spawn_yaw, spawn_pitch) =
if let Some(spawn) = &data.config.spawn {
(spawn.x, spawn.y, spawn.z)
(
spawn.coords.x,
spawn.coords.y,
spawn.coords.z,
spawn.yaw,
spawn.pitch,
)
} else {
(16, data.level.y_size / 2 + 2, 16)
(16, data.level.y_size / 2 + 2, 16, 0, 0)
};
let (spawn_x, spawn_y, spawn_z) = (
f16::from_f32(spawn_x as f32 + 0.5),
f16::from_f32(spawn_y as f32),
f16::from_f32(spawn_z as f32 + 0.5),
);
player.x = spawn_x;
player.y = spawn_y;
player.z = spawn_z;
player.yaw = spawn_yaw;
player.pitch = spawn_pitch;
data.players.push(player);
let spawn_packet = ServerPacket::SpawnPlayer {
player_id: *own_id,
player_name: username.clone(),
x: f16::from_f32(spawn_x as f32 + 0.5),
y: f16::from_f32(spawn_y as f32),
z: f16::from_f32(spawn_z as f32 + 0.5),
yaw: 0,
pitch: 0,
x: spawn_x,
y: spawn_y,
z: spawn_z,
yaw: spawn_yaw,
pitch: spawn_pitch,
};
let message_packet = ServerPacket::Message {
player_id: *own_id,