velocity packet

This commit is contained in:
MeexReay 2024-12-17 18:40:22 +03:00
parent d3060a1f28
commit 381f252477
3 changed files with 22 additions and 20 deletions

View file

@ -72,8 +72,8 @@
<div class="margin-25"></div>
<a href="https://github.com/MeexReay/cubicjs">репозиторий гитхаб</a>
<a href="README.md">протокол сервера</a>
<a href="https://github.com/MeexReay/cubicjs">репозиторий гитхаб</a><br>
<a href="README.md">протокол сервера</a><br>
<a href="server.py">готовая реализация сервера на Python</a>
</div>

View file

@ -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() {

View file

@ -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)))