argh
This commit is contained in:
parent
381f252477
commit
d1de965ae4
4 changed files with 37 additions and 8 deletions
|
@ -2,6 +2,11 @@
|
|||
|
||||
сайт: https://meex.lol/cubic/
|
||||
|
||||
## роадмап
|
||||
|
||||
- пакет на отправку позиции от клиента
|
||||
- пакет на отправку текста на экран от сервера
|
||||
|
||||
## протокол
|
||||
|
||||
работает через вебсокеты \
|
||||
|
@ -52,6 +57,7 @@
|
|||
отправить мир [W]: {изм. мира}, {изм. мира}, ...
|
||||
отправить все типы блоков: [B]: {тип_1}, ..., {тип_9}
|
||||
отправить сообщение [M]: {сообщение}, {сообщение}, ...
|
||||
корректировка движение [R]: {x}, {y}, {vel_x}, {vel_y}
|
||||
```
|
||||
|
||||
**список кнопок которые может отправить игрок через отдельный пакет:**
|
||||
|
|
|
@ -50,7 +50,7 @@
|
|||
|
||||
<h3>список серверов:</h3>
|
||||
|
||||
<ul>
|
||||
<ul style="list-style: thai;">
|
||||
<li>meex.lol</li>
|
||||
<li>других пока нет</li>
|
||||
</ul>
|
||||
|
|
15
script.js
15
script.js
|
@ -409,6 +409,7 @@ class MainPlayer extends Player {
|
|||
recv_packet(packet) {
|
||||
let packet_id = packet[0]
|
||||
let packet_data = packet.slice(1).split("\n")
|
||||
console.log(packet_id, packet_data)
|
||||
|
||||
// console.log(packet_id, packet_data)
|
||||
|
||||
|
@ -430,18 +431,23 @@ class MainPlayer extends Player {
|
|||
chatMessages.unshift(...packet_data)
|
||||
}
|
||||
|
||||
if (packet_id == "R") {
|
||||
this.velocity_x = parseFloat(packet_data[0]) - this.x + parseFloat(packet_data[2])
|
||||
this.velocity_y = parseFloat(packet_data[1]) - this.y + parseFloat(packet_data[3])
|
||||
}
|
||||
|
||||
if (packet_id == "P") {
|
||||
let x = parseFloat(packet_data[0])
|
||||
let y = parseFloat(packet_data[1])
|
||||
if (Math.abs(x - this.x) > 0.5) this.x = x
|
||||
if (Math.abs(y - this.y) > 0.5) this.y = y
|
||||
this.x = x
|
||||
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) > 0.5) this.velocity_x = x
|
||||
if (Math.abs(y - this.velocity_y) > 0.5) this.velocity_y = y
|
||||
this.velocity_x = x
|
||||
this.velocity_y = y
|
||||
}
|
||||
|
||||
if (packet_id == "S") {
|
||||
|
@ -523,7 +529,6 @@ class MainPlayer extends Player {
|
|||
return
|
||||
}
|
||||
this.socket.send(id+params.join("\n"))
|
||||
console.log(id, params)
|
||||
}
|
||||
|
||||
send_velocity_packet(x, y) {
|
||||
|
|
22
server.py
22
server.py
|
@ -90,7 +90,7 @@ class Player(Block):
|
|||
self.vel_x = x
|
||||
self.vel_y = y
|
||||
|
||||
await writePacket(self.websocket, "V", [str(x), str(y)])
|
||||
await self.sendVel(x, y)
|
||||
|
||||
async def setPos(self, x, y):
|
||||
if x == self.x and y == self.y: return
|
||||
|
@ -98,6 +98,12 @@ class Player(Block):
|
|||
self.x = x
|
||||
self.y = y
|
||||
|
||||
await self.sendPos(x, y)
|
||||
|
||||
async def sendVel(self, x, y):
|
||||
await writePacket(self.websocket, "V", [str(x), str(y)])
|
||||
|
||||
async def sendPos(self, x, y):
|
||||
await writePacket(self.websocket, "P", [str(x), str(y)])
|
||||
|
||||
async def sendMessage(self, message):
|
||||
|
@ -172,6 +178,9 @@ class Player(Block):
|
|||
# await self.setPos(self.x + self.vel_x, self.y + self.vel_y)
|
||||
return self.vel_x != 0 or self.vel_y != 0
|
||||
|
||||
async def keepAlive(self):
|
||||
await writePacket(self.websocket, "R", [str(self.x), str(self.y), str(self.vel_x), str(self.vel_y)])
|
||||
|
||||
def toStatement(self, add=True):
|
||||
return f"P1{self.name},{self.x},{self.y},{self.vel_x},{self.vel_y},{self.color}" if add else f"P0{self.name}"
|
||||
|
||||
|
@ -236,7 +245,7 @@ async def handler(websocket: ServerConnection):
|
|||
if packet_id == "V":
|
||||
vel_x, vel_y = float(packet_data[0]), float(packet_data[1])
|
||||
vel_x = max(min(vel_x, player.walk_speed), -player.walk_speed)
|
||||
vel_y = max(min(vel_x, player.jump_speed), 0)
|
||||
vel_y = max(min(vel_y, player.jump_speed), 0)
|
||||
|
||||
player.vel_x += vel_x
|
||||
|
||||
|
@ -244,6 +253,8 @@ async def handler(websocket: ServerConnection):
|
|||
player.vel_y += vel_y
|
||||
player.on_ground = False
|
||||
|
||||
await player.sendToPlayers()
|
||||
|
||||
if packet_id == "K":
|
||||
key,pressed = packet_data
|
||||
pressed = pressed == "1"
|
||||
|
@ -329,6 +340,12 @@ async def tickTimer():
|
|||
await b.tick()
|
||||
await asyncio.sleep(1/20)
|
||||
|
||||
async def keepAliveTimer():
|
||||
while True:
|
||||
for b in getPlayers():
|
||||
await b.keepAlive()
|
||||
await asyncio.sleep(1)
|
||||
|
||||
async def renderTimer():
|
||||
while True:
|
||||
for p in getPlayers():
|
||||
|
@ -337,6 +354,7 @@ async def renderTimer():
|
|||
|
||||
async def main():
|
||||
asyncio.get_event_loop().create_task(tickTimer())
|
||||
asyncio.get_event_loop().create_task(keepAliveTimer())
|
||||
asyncio.get_event_loop().create_task(renderTimer())
|
||||
async with serve(handler, HOST, PORT) as server:
|
||||
print(f"started server on {HOST}:{PORT}")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue