From 381f252477506933cce17659abae866d6bbac4c2 Mon Sep 17 00:00:00 2001 From: MeexReay Date: Tue, 17 Dec 2024 18:40:22 +0300 Subject: [PATCH] velocity packet --- index.html | 4 ++-- script.js | 22 +++++++++++----------- server.py | 16 +++++++++------- 3 files changed, 22 insertions(+), 20 deletions(-) diff --git a/index.html b/index.html index d647c47..e6507be 100644 --- a/index.html +++ b/index.html @@ -72,8 +72,8 @@
- репозиторий гитхаб - протокол сервера + репозиторий гитхаб
+ протокол сервера
готовая реализация сервера на Python diff --git a/script.js b/script.js index 97cf421..ee95ff1 100644 --- a/script.js +++ b/script.js @@ -433,15 +433,15 @@ class MainPlayer extends Player { if (packet_id == "P") { let x = parseFloat(packet_data[0]) let y = parseFloat(packet_data[1]) - if (Math.abs(x - this.x) > 2) this.x = x - if (Math.abs(y - this.y) > 2) this.y = y + if (Math.abs(x - this.x) > 0.5) this.x = x + if (Math.abs(y - this.y) > 0.5) this.y = y } if (packet_id == "V") { let x = parseFloat(packet_data[0]) let y = parseFloat(packet_data[1]) - if (Math.abs(x - this.velocity_x) > 2) this.velocity_x = x - if (Math.abs(y - this.velocity_y) > 2) this.velocity_y = y + if (Math.abs(x - this.velocity_x) > 0.5) this.velocity_x = x + if (Math.abs(y - this.velocity_y) > 0.5) this.velocity_y = y } if (packet_id == "S") { @@ -523,24 +523,24 @@ class MainPlayer extends Player { return } this.socket.send(id+params.join("\n")) + console.log(id, params) } send_velocity_packet(x, y) { - if (x != 0 || y != 0) return + if (x == 0 && y == 0) return this.send_packet("V", x, y) } tick() { super.tick(false) - let vel_x = this.controls_x * this.walk_speed - let vel_y = 0 + let vel_x = this.velocity_x + let vel_y = this.velocity_y - this.velocity_x += vel_x + this.velocity_x += this.controls_x * this.walk_speed if (this.controls_jump && this.on_ground) { - vel_y = this.jump_speed - this.velocity_y += vel_y + this.velocity_y += this.jump_speed this.on_ground = false } else { this.velocity_y -= this.gravity_speed @@ -550,7 +550,7 @@ class MainPlayer extends Player { ticksAlive++ - this.send_velocity_packet(vel_x, vel_y) + this.send_velocity_packet(this.velocity_x-vel_x, this.velocity_y-vel_y) } render() { diff --git a/server.py b/server.py index fc82aed..9b92ee2 100644 --- a/server.py +++ b/server.py @@ -164,12 +164,12 @@ class Player(Block): # pass async def render(self): - # self.vel_x *= 0.5 - # self.vel_y *= 0.5 - # self.x += self.vel_x - # self.y += self.vel_y - await self.setVel(self.vel_x * 0.5, self.vel_y * 0.5) - await self.setPos(self.x + self.vel_x, self.y + self.vel_y) + self.vel_x *= 0.5 + self.vel_y *= 0.5 + self.x += self.vel_x + self.y += self.vel_y + # await self.setVel(self.vel_x * 0.5, self.vel_y * 0.5) + # await self.setPos(self.x + self.vel_x, self.y + self.vel_y) return self.vel_x != 0 or self.vel_y != 0 def toStatement(self, add=True): @@ -193,7 +193,9 @@ def current_milli_time(): async def readPacket(websocket: ServerConnection) -> tuple[str, list[str]]: data = await websocket.recv() - return data[0], data[1:].splitlines() + id,data = data[0], data[1:].splitlines() + print(id, data) + return id,data async def writePacket(websocket: ServerConnection, packet_id: str, packet_data: list[str]): await websocket.send(packet_id + ("\n".join(packet_data)))