From b8d3289c08652ef11b1532afadeecce73a97bd3d Mon Sep 17 00:00:00 2001 From: ghostlyzsh Date: Mon, 27 Nov 2023 00:25:07 -0600 Subject: [PATCH] input added, but thats it --- server/src/component.rs | 9 +++++ server/src/main.rs | 13 +++++++- server/src/packet.rs | 6 ++++ starkingdoms-client/src/hub.ts | 51 +++++++++++++++++++++++++++++ starkingdoms-client/src/main.ts | 9 +++++ starkingdoms-client/src/protocol.ts | 11 +++++-- 6 files changed, 96 insertions(+), 3 deletions(-) diff --git a/server/src/component.rs b/server/src/component.rs index 004ee56be93011cfa9ae90c3e66907debed4ea1a..79ae11d40ff17c7f6f822b57eea923c92cfce95c 100644 --- a/server/src/component.rs +++ b/server/src/component.rs @@ -13,10 +13,19 @@ pub enum PartType { Hearty, } +#[derive(Component, Clone, Copy, Serialize, Deserialize, Debug, Default)] +pub struct Input { + pub up: bool, + pub down: bool, + pub left: bool, + pub right: bool, +} + #[derive(Component)] pub struct Player { pub addr: SocketAddr, pub username: String, + pub input: Input, } #[derive(Bundle)] diff --git a/server/src/main.rs b/server/src/main.rs index bf065117ba90ebd7ae7e099f7dd514ed92d2fa33..f35dd18cff768fff089bf99c452ce909c079194b 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -63,7 +63,7 @@ fn spawn_planets(mut commands: Commands) { fn on_message( mut commands: Commands, planet_query: Query<(Entity, &PlanetType, &Transform)>, - player_query: Query<(Entity, &Player)>, + mut player_query: Query<(Entity, &mut Player)>, part_query: Query<(Entity, &PartType, &Transform)>, mut packet_recv: Local>, mut packet_event_send: ResMut>, @@ -95,6 +95,7 @@ fn on_message( player: Player { addr: *addr, username: username.to_string(), + input: component::Input::default(), }, }) .insert(Collider::cuboid(25.0 / SCALE, 25.0 / SCALE)) @@ -229,6 +230,16 @@ fn on_message( event_queue.push(ServerEvent::Broadcast(MessageType::Text, buf)); } } + Packet::PlayerInput { up, down, left, right } => { + for (_, mut q_player) in &mut player_query { + if q_player.addr == *addr { + q_player.input.up = up; + q_player.input.down = down; + q_player.input.left = left; + q_player.input.right = right; + } + } + } _ => continue, } } diff --git a/server/src/packet.rs b/server/src/packet.rs index e3ece073dffd9d3e6dfd0c5336c8917dbf95257b..d4b03db2813e184f2d0036b7082624cab8502e7b 100644 --- a/server/src/packet.rs +++ b/server/src/packet.rs @@ -50,6 +50,12 @@ pub enum Packet { target: Option, content: String, }, + PlayerInput { + up: bool, + down: bool, + left: bool, + right: bool, + }, // clientbound SpawnPlayer { id: u32, diff --git a/starkingdoms-client/src/hub.ts b/starkingdoms-client/src/hub.ts index e6cb7a52fa445d13d60e08c32fee84a286c27fcb..f94a8be1693a2f17d76ed63195f1b29900a8fb6e 100644 --- a/starkingdoms-client/src/hub.ts +++ b/starkingdoms-client/src/hub.ts @@ -51,6 +51,57 @@ export async function hub_connect(url: string, username: string): Promise { + // currently, input packet is sent on any key down. fix that + if(e.key == "ArrowUp" || e.key == "w") { + global.up = true; + } + if(e.key == "ArrowDown" || e.key == "s") { + global.down = true; + } + if(e.key == "ArrowLeft" || e.key == "a") { + global.left = true; + } + if(e.key == "ArrowRight" || e.key == "d") { + global.right = true; + } + let input_packet: Packet = { + t: PacketType.PlayerInput, + c: { + up: global.up, + down: global.down, + left: global.left, + right: global.right, + } + } + sendPacket(client, input_packet); + } + document.onkeyup = (e) => { + if(e.key == "ArrowUp" || e.key == "w") { + global.up = false; + } + if(e.key == "ArrowDown" || e.key == "s") { + global.down = false; + } + if(e.key == "ArrowLeft" || e.key == "a") { + global.left = false; + } + if(e.key == "ArrowRight" || e.key == "d") { + global.right = false; + } + let input_packet: Packet = { + t: PacketType.PlayerInput, + c: { + up: global.up, + down: global.down, + left: global.left, + right: global.right, + } + } + sendPacket(client, input_packet); + } + document.getElementById("chatentry")!.onkeydown = (e) => { if (e.key === 'Enter') { let value = (document.getElementById("chatentry")!).value; diff --git a/starkingdoms-client/src/main.ts b/starkingdoms-client/src/main.ts index 72de7961a651021ac0316506b588578d2c9d2785..3c4899559b177a518d38655cda6f5d7cef320bb5 100644 --- a/starkingdoms-client/src/main.ts +++ b/starkingdoms-client/src/main.ts @@ -27,6 +27,11 @@ export interface GlobalData { parts_map: Map, + up: boolean, + down: boolean, + left: boolean, + right: boolean, + rendering: GlobalRendering | null } @@ -46,6 +51,10 @@ export const global: GlobalData = { inverse_players_map: new Map(), planets_map: new Map(), parts_map: new Map(), + up: false, + down: false, + left: false, + right: false, rendering: null } diff --git a/starkingdoms-client/src/protocol.ts b/starkingdoms-client/src/protocol.ts index 0ada7e73c6bba7847f0058f1cdfd22c2bf793c2d..13d17c3b0afc9360af16321a5193a65359b50910 100644 --- a/starkingdoms-client/src/protocol.ts +++ b/starkingdoms-client/src/protocol.ts @@ -32,6 +32,12 @@ export interface PlanetPositionsPacket { export interface PartPositionsPacket { parts: [number, Part][] } +export interface PlayerInputPacket { + up: boolean, + down: boolean, + left: boolean, + right: boolean, +} export interface PlayerListPacket { players: [number, string][] } @@ -58,6 +64,7 @@ export enum PacketType { // serverbound ClientLogin = "ClientLogin", SendMessage = "SendMessage", + PlayerInput = "PlayerInput", // clientbound SpawnPlayer = "SpawnPlayer", PlayerList = "PlayerList", @@ -69,10 +76,10 @@ export enum PacketType { export interface Packet { t: PacketType, - c: ClientLoginPacket | SpawnPlayerPacket | PlayerListPacket | PlanetPositionsPacket | PartPositionsPacket | PlayerLeavePacket | SendMessagePacket | MessagePacket + c: ClientLoginPacket | SpawnPlayerPacket | PlayerListPacket | PlanetPositionsPacket | PartPositionsPacket | PlayerLeavePacket | SendMessagePacket | MessagePacket | PlayerInputPacket } -export const SERVERBOUND = [PacketType.ClientLogin, PacketType.SendMessage]; +export const SERVERBOUND = [PacketType.ClientLogin, PacketType.SendMessage, PacketType.PlayerInput]; export const CLIENTBOUND = [PacketType.SpawnPlayer, PacketType.PlayerList, PacketType.PlanetPositions, PacketType.PartPositions, PacketType.PlayerLeave, PacketType.Message]; export enum Direction {