-
Notifications
You must be signed in to change notification settings - Fork 21
/
multiplayer.py
122 lines (106 loc) · 5.93 KB
/
multiplayer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
from ursinanetworking import *
from ursina import Entity, Vec3, color, destroy
from car import CarRepresentation, CarUsername
class Multiplayer(Entity):
def __init__(self, car):
self.car = car
# If the input filed doesn't equal IP and PORT (the defaults) create Client
if str(self.car.ip.text) != "IP" and str(self.car.port.text) != "PORT":
self.client = UrsinaNetworkingClient(self.car.ip.text, int(self.car.port.text))
self.easy = EasyUrsinaNetworkingClient(self.client)
# Player target values
self.players = {}
self.players_target_name = {}
self.players_target_pos = {}
self.players_target_rot = {}
self.players_target_model = {}
self.players_target_tex = {}
self.players_target_score = {}
self.players_target_cos = {}
self.selfId = -1
@self.client.event
def GetId(id):
self.selfId = id
print(f"My ID is : {self.selfId}")
@self.easy.event
def onReplicatedVariableCreated(variable):
variable_name = variable.name
variable_type = variable.content["type"]
if variable_type == "player":
self.players_target_pos[variable_name] = Vec3(-80, -30, 15)
self.players_target_rot[variable_name] = Vec3(0, 90, 0)
self.players_target_model[variable_name] = "./assets/cars/sports-car.obj"
self.players_target_tex[variable_name] = "./assets/cars/garage/sports-car/sports-red.png"
self.players_target_name[variable_name] = "Guest"
self.players_target_score[variable_name] = 0.0
self.players_target_cos[variable_name] = "none"
self.players[variable_name] = CarRepresentation(self.car, (-80, -30, 15), (0, 90, 0))
self.players[variable_name].text_object = CarUsername(self.players[variable_name])
if self.selfId == int(variable.content["id"]):
self.players[variable_name].color = color.red
self.players[variable_name].visible = False
@self.easy.event
def onReplicatedVariableUpdated(variable):
self.players_target_pos[variable.name] = variable.content["position"]
self.players_target_rot[variable.name] = variable.content["rotation"]
self.players_target_model[variable.name] = variable.content["model"]
self.players_target_tex[variable.name] = variable.content["texture"]
self.players_target_name[variable.name] = variable.content["username"]
self.players_target_score[variable.name] = variable.content["highscore"]
self.players_target_cos[variable.name] = variable.content["cosmetic"]
@self.easy.event
def onReplicatedVariableRemoved(variable):
variable_name = variable.name
variable_type = variable.content["type"]
if variable_type == "player":
destroy(self.players[variable_name])
del self.players[variable_name]
def update_multiplayer(self):
for p in self.players:
self.players[p].position += (Vec3(self.players_target_pos[p]) - self.players[p].position) / 25
self.players[p].rotation += (Vec3(self.players_target_rot[p]) - self.players[p].rotation) / 25
self.players[p].model = f"{self.players_target_model[p]}"
self.players[p].texture = f"{self.players_target_tex[p]}"
self.players[p].text_object.text = f"{self.players_target_name[p]}"
self.players[p].highscore = f"{self.players_target_score[p]}"
if self.players_target_cos[p] == "viking":
for cosmetic in self.players[p].cosmetics:
cosmetic.disable()
self.players[p].viking_helmet.enable()
elif self.players_target_cos[p] == "duck":
for cosmetic in self.players[p].cosmetics:
cosmetic.disable()
self.players[p].duck.enable()
elif self.players_target_cos[p] == "banana":
for cosmetic in self.players[p].cosmetics:
cosmetic.disable()
self.players[p].banana.enable()
elif self.players_target_cos[p] == "surfinbird":
for cosmetic in self.players[p].cosmetics:
cosmetic.disable()
self.players[p].surfinbird.enable()
if self.car.enabled == False:
self.players[p].disable()
elif self.car.enabled == True:
self.players[p].enable()
if "player_0" in self.players:
self.car.leaderboard_01 = str(self.players["player_0"].text_object.text) + " | " + str(self.players["player_0"].highscore)
else:
self.car.leaderboard_01 = ""
if "player_1" in self.players:
self.car.leaderboard_02 = str(self.players["player_1"].text_object.text) + " | " + str(self.players["player_1"].highscore)
else:
self.car.leaderboard_02 = ""
if "player_2" in self.players:
self.car.leaderboard_03 = str(self.players["player_2"].text_object.text) + " | " + str(self.players["player_2"].highscore)
else:
self.car.leaderboard_03 = ""
if "player_3" in self.players:
self.car.leaderboard_04 = str(self.players["player_3"].text_object.text) + " | " + str(self.players["player_3"].highscore)
else:
self.car.leaderboard_04 = ""
if "player_4" in self.players:
self.car.leaderboard_05 = str(self.players["player_4"].text_object.text) + " | " + str(self.players["player_4"].highscore)
else:
self.car.leaderboard_05 = ""
self.easy.process_net_events()