Skip to content

Commit

Permalink
Added texture loading to multiplayer
Browse files Browse the repository at this point in the history
  • Loading branch information
mandaw2014 committed Apr 20, 2022
1 parent ba6f146 commit 5b389eb
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ A Garage where you can change the colour of the car

Multiplayer

So far I have been working on this game for a month. Feel free to leave comments or feedback on how I can improve the game
Feel free to leave comments or feedback on how I can improve the game

# Installation

Expand Down
3 changes: 2 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from tracks.grass_track import GrassTrack
from tracks.snow_track import SnowTrack

# application.development_mode = False
application.development_mode = False

app = Ursina()
window.borderless = False
Expand Down Expand Up @@ -70,5 +70,6 @@ def input(key):
if car.multiplayer_update:
multiplayer.client.send_message("MyPosition", tuple(multiplayer.car.position))
multiplayer.client.send_message("MyRotation", tuple(multiplayer.car.rotation))
multiplayer.client.send_message("MyTexture", str(multiplayer.car.texture))

app.run()
4 changes: 4 additions & 0 deletions multiplayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def __init__(self, car):
self.players = {}
self.players_target_pos = {}
self.players_target_rot = {}
self.players_target_tex = {}

self.selfId = -1

Expand All @@ -28,6 +29,7 @@ def onReplicatedVariableCreated(variable):
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_tex[variable_name] = "./assets/garage/car-red.png"
self.players[variable_name] = CarRepresentation((-80, -30, 15), (0, 90, 0))

if self.selfId == int(variable.content["id"]):
Expand All @@ -38,6 +40,7 @@ def onReplicatedVariableCreated(variable):
def onReplicatedVariableUpdated(variable):
self.players_target_pos[variable.name] = variable.content["position"]
self.players_target_rot[variable.name] = variable.content["rotation"]
self.players_target_tex[variable.name] = variable.content["texture"]

@self.easy.event
def onReplicatedVariableRemoved(variable):
Expand All @@ -52,5 +55,6 @@ 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].texture = f"{self.players_target_tex[p]}"

self.easy.process_net_events()
6 changes: 5 additions & 1 deletion server.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
def onClientConnected(client):
easy.create_replicated_variable(
f"player_{client.id}",
{ "type" : "player", "id" : client.id, "position": (0, 0, 0), "rotation" : (0, 0, 0)}
{ "type" : "player", "id" : client.id, "position": (0, 0, 0), "rotation" : (0, 0, 0), "texture" : "car-red.png"}
)
print(f"{client} connected!")
client.send_message("GetId", client.id)
Expand All @@ -30,5 +30,9 @@ def MyPosition(client, newpos):
def MyRotation(client, newrot):
easy.update_replicated_variable_by_name(f"player_{client.id}", "rotation", newrot)

@server.event
def MyTexture(client, newtex):
easy.update_replicated_variable_by_name(f"player_{client.id}", "texture", newtex)

while True:
easy.process_net_events()

0 comments on commit 5b389eb

Please sign in to comment.