You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
82 lines
2.7 KiB
Python
82 lines
2.7 KiB
Python
"""
|
|
NPC
|
|
exists in a room
|
|
broadcasts the the same join, leave, move, talk, intro events
|
|
is rendered like and avatar by the client
|
|
is controlled by the server
|
|
"""
|
|
from flask_socketio import emit, send
|
|
|
|
|
|
class NPC ():
|
|
|
|
def __init__(self, io, o, gameState):
|
|
print(f"Create NPC {o['id']} in room {o['room']} nickNamed {o['nickName']}")
|
|
self.io = io
|
|
self.gameState = gameState
|
|
self.id = o['id']
|
|
self.nickName = o['nickName']
|
|
self.room = o['room']
|
|
self.avatar = o['avatar']
|
|
self.colors = o['colors']
|
|
self.x = o['x'] * 2;
|
|
self.y = o['y'] * 2;
|
|
self.destinationX = o['x']
|
|
self.destinationY = o['y']
|
|
|
|
if o['labelColor']:
|
|
self.labelColor = o['labelColor']
|
|
else:
|
|
self.labelColor = "#FFFFFF" # oops server doesn't know about colors
|
|
|
|
# add to NPC list
|
|
self.gameState['NPCs'][self.id] = self
|
|
|
|
def sendIntroTo (self, pId):
|
|
""" mimicks the emission from players """
|
|
print(f"HELLO I'm {self.nickName} in {self.room}, pId: {pId}")
|
|
# If I'm not the new player send an introduction to the new player
|
|
# slight issue, server doesn't compute movements so if moving it appears at the destination
|
|
# a way to solve this would be to save the time of the movement and lerp it
|
|
self.io.emit("onIntro", {
|
|
'id': self.id,
|
|
'nickName': self.nickName,
|
|
'colors': self.colors,
|
|
'avatar': self.avatar,
|
|
'room': self.room,
|
|
'x': self.destinationX,
|
|
'y': self.destinationY,
|
|
'destinationX': self.destinationX,
|
|
'destinationY': self.destinationY
|
|
}, room=pId)
|
|
|
|
def move (self, dx, dy):
|
|
print(f"HELLO I'm {self.nickName} and I move to {dx} {dy} in room: {self.room}")
|
|
# broadcast the movement to everybody in the room
|
|
# it doesn't check if the position is valid
|
|
|
|
self.io.emit("playerMoved", {
|
|
'id': self.id,
|
|
'x': self.x,
|
|
'y': self.y,
|
|
'destinationX': dx,
|
|
'destinationY': dy
|
|
}, room=self.room)
|
|
# update for future intros
|
|
self.destinationX = self.x = dx
|
|
self.destinationY = self.y = dy
|
|
|
|
def talk (self, message):
|
|
self.io.emit("playerTalked", {
|
|
'id': self.id,
|
|
'color': self.labelColor,
|
|
'message': message,
|
|
'x': self.x,
|
|
'y': self.y
|
|
}, room=self.room)
|
|
|
|
def delete (self):
|
|
self.io.emit("playerLeft", { 'id': self.id, 'room': self.room, 'disconnect': true }, room=self.room)
|
|
del self.gameState['NPCs'][self.id]
|
|
|