Skip to content

Commit

Permalink
Added server join menu
Browse files Browse the repository at this point in the history
  • Loading branch information
mandaw2014 committed Apr 19, 2022
1 parent bae451f commit 96883b0
Show file tree
Hide file tree
Showing 6 changed files with 333 additions and 270 deletions.
397 changes: 193 additions & 204 deletions car.py

Large diffs are not rendered by default.

72 changes: 16 additions & 56 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
from ursinanetworking import *
from direct.stdpy import thread

from car import Car, CarRepresentation
from car import Car

from multiplayer import Multiplayer

from main_menu import MainMenu

Expand Down Expand Up @@ -39,7 +41,8 @@ def load_car_textures():
car.grass_track = grass_track
car.snow_track = snow_track

car.multiplayer = True
car.multiplayer = False
car.multiplayer_update = False

main_menu = MainMenu(car, sand_track, grass_track, snow_track)

Expand All @@ -48,67 +51,24 @@ def load_car_textures():

Sky()

if car.multiplayer == True:
client = UrsinaNetworkingClient("localhost", 25565)
easy = EasyUrsinaNetworkingClient(client)

players = {}
players_target_pos = {}
players_target_rot = {}

selfId = -1

@client.event
def GetId(id):
global selfId
selfId = id
print(f"My ID is : {selfId}")

@easy.event
def onReplicatedVariableCreated(variable):
global client
variable_name = variable.name
variable_type = variable.content["type"]

if variable_type == "player":
players_target_pos[variable_name] = Vec3(-80, -30, 15)
players_target_rot[variable_name] = Vec3(0, 90, 0)
players[variable_name] = CarRepresentation((-80, -30, 15), (0, 90, 0))

if selfId == int(variable.content["id"]):
players[variable_name].color = color.red
players[variable_name].visible = False

@easy.event
def onReplicatedVariableUpdated(variable):
players_target_pos[variable.name] = variable.content["position"]
players_target_rot[variable.name] = variable.content["rotation"]

@easy.event
def onReplicatedVariableRemoved(variable):
variable_name = variable.name
variable_type = variable.content["type"]

if variable_type == "player":
destroy(players[variable_name])
del players[variable_name]

def update():
if car.multiplayer == True:
for p in players:
players[p].position += (Vec3(players_target_pos[p]) - players[p].position) / 25
players[p].rotation += (Vec3(players_target_rot[p]) - players[p].rotation) / 25
global multiplayer
multiplayer = Multiplayer(car)
car.multiplayer_update = True
car.multiplayer = False

easy.process_net_events()
if car.multiplayer_update:
multiplayer.update_multiplayer()

def input(key):
if car.multiplayer == True:
client.send_message("MyPosition", tuple(car.position))
client.send_message("MyRotation", tuple(car.rotation))

if main_menu.main_menu.enabled == False and main_menu.settings_menu.enabled == False and main_menu.maps_menu.enabled == False and main_menu.garage_menu.enabled == False and main_menu.controls_menu.enabled == False:
if main_menu.main_menu.enabled == False and main_menu.server_menu.enabled == False and main_menu.settings_menu.enabled == False and main_menu.maps_menu.enabled == False and main_menu.garage_menu.enabled == False and main_menu.controls_menu.enabled == False:
if key == "escape":
main_menu.pause_menu.enabled = not main_menu.pause_menu.enabled
mouse.locked = not mouse.locked

if car.multiplayer_update:
multiplayer.client.send_message("MyPosition", tuple(multiplayer.car.position))
multiplayer.client.send_message("MyRotation", tuple(multiplayer.car.rotation))

app.run()
68 changes: 61 additions & 7 deletions main_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,51 @@ def __init__(self, car, sand_track, grass_track, snow_track):
parent = camera.ui
)

self.main_menu = Entity(parent = self, enabled = True)
self.server_menu = Entity(parent = self, enabled = True)
self.main_menu = Entity(parent = self, enabled = False)
self.maps_menu = Entity(parent = self, enabled = False)
self.settings_menu = Entity(parent = self, enabled = False)
self.controls_menu = Entity(parent = self, enabled = False)
self.garage_menu = Entity(parent = self, enabled = False)
self.pause_menu = Entity(parent = self, enabled = False)
self.car = car

# Server Menu

def join_server():
car.multiplayer = True
self.server_menu.disable()
self.main_menu.enable()
grass_track.enable()
snow_track.disable()
self.car.position = (0, 0, 4)
camera.rotation = (35, -20, 0)
self.car.camera_follow.offset = (20, 40, -50)
self.car.disable()

def single_player():
car.multiplayer = False
self.server_menu.disable()
self.main_menu.enable()
grass_track.enable()
snow_track.disable()
self.car.position = (0, 0, 4)
camera.rotation = (35, -20, 0)
self.car.camera_follow.offset = (20, 40, -50)
self.car.disable()

self.car.enable()
self.car.position = (-3, -44.5, 92)
grass_track.disable()
snow_track.enable()

car.ip = InputField(default_value = "localhost", limit_content_to = "0123456789.localhost", color = color.black, alpha = 100, y = 0.22, parent = self.server_menu)
multiplayer_button = Button(text = "J o i n", color = color.light_gray, highlight_color = color.gray, scale_y = 0.1, scale_x = 0.3, y = 0.1, parent = self.server_menu)
single_player_button = Button(text = "S i n g l e p l a y e r", color = color.light_gray, highlight_color = color.gray, scale_y = 0.1, scale_x = 0.3, y = -0.02, parent = self.server_menu)

multiplayer_button.on_click = Func(join_server)
single_player_button.on_click = Func(single_player)

# Main Menu

title = Entity(model = "quad", scale = (0.5, 0.2, 0.2), texture = "rally-logo", parent = self.main_menu, y = 0.3)
Expand All @@ -39,7 +76,6 @@ def sand_track_func():
self.car.position = (0, 0, 4)
self.car.rotation = (0, 65, 0)
self.car.reset_count_timer.enable()
self.car.garage_mode = False
camera.position = (-80, -30, 15)
sand_track.enable()
grass_track.disable()
Expand Down Expand Up @@ -90,7 +126,6 @@ def grass_track_func():
self.car.position = (-80, -30, 15)
self.car.rotation = (0, 90, 0)
self.car.reset_count_timer.enable()
self.car.garage_mode = False
grass_track.enable()
sand_track.disable()

Expand Down Expand Up @@ -142,7 +177,6 @@ def snow_track_func():
self.car.position = (-5, -35, 90)
self.car.rotation = (0, 90, 0)
self.car.reset_count_timer.enable()
self.car.garage_mode = False
grass_track.disable()
sand_track.disable()
snow_track.enable()
Expand Down Expand Up @@ -356,11 +390,12 @@ def main_menu():
def back_garage():
self.garage_menu.disable()
self.main_menu.enable()
self.car.garage_mode = False
self.car.position = (0, 0, 4)
camera.rotation = (35, -20, 0)
self.car.camera_follow.offset = (20, 40, -50)
self.car.disable()
grass_track.enable()
sand_track.disable()

with open(self.car.highscore_path_grass, "r") as hs:
self.car.highscore_count = hs.read()
Expand All @@ -372,7 +407,9 @@ def garage_button_func():
self.main_menu.disable()
self.car.enable()
self.car.garage_mode = True
self.car.position = (-3, -42.6, 15)
self.car.position = (-32, -48.4, -45)
grass_track.disable()
sand_track.enable()

def change_color(color):
"""
Expand All @@ -398,4 +435,21 @@ def change_color(color):
green_button.on_click = Func(change_color, "green")
orange_button.on_click = Func(change_color, "orange")
black_button.on_click = Func(change_color, "black")
white_button.on_click = Func(change_color, "white")
white_button.on_click = Func(change_color, "white")

def update(self):
if self.server_menu.enabled == True:
if not held_keys["right mouse"]:
self.car.rotation_y += 15 * time.dt
else:
self.car.rotation_y = mouse.x * 500
self.car.camera_follow.offset = (-25, 8, 0)
camera.rotation = (14, 90, 0)

if self.garage_menu.enabled == True:
if not held_keys["right mouse"]:
self.car.rotation_y += 15 * time.dt
else:
self.car.rotation_y = mouse.x * 500
self.car.camera_follow.offset = (-25, 5, 3)
camera.rotation = (10, 90, 0)
56 changes: 56 additions & 0 deletions multiplayer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from ursinanetworking import *
from ursina import *
from car import CarRepresentation

class Multiplayer(Entity):
def __init__(self, car):
self.car = car

self.client = UrsinaNetworkingClient(self.car.ip.text, 25565)
self.easy = EasyUrsinaNetworkingClient(self.client)

self.players = {}
self.players_target_pos = {}
self.players_target_rot = {}

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[variable_name] = CarRepresentation((-80, -30, 15), (0, 90, 0))

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.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.easy.process_net_events()
9 changes: 6 additions & 3 deletions server.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from ursina import *
from ursinanetworking import *

print("Hello from the Server!")
print("\nHello from the Rally Server!")

server = UrsinaNetworkingServer("localhost", 25565)
print("\nPlease enter the ip (use 'localhost' for localhost)")

ip = input("\nIP: ")

server = UrsinaNetworkingServer(ip, 25565)
easy = EasyUrsinaNetworkingServer(server)

@server.event
Expand Down
1 change: 1 addition & 0 deletions tracks/grass_track.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def __init__(self, car):
self.wall_trigger = Entity(model = "cube", position = (25, -40.2, 65), collider = "box", rotation = (0, 90, 0), scale = (40, 20, 3), visible = False)
self.wall_trigger_ramp = Entity(model = "cube", position = (-82, -34, -64), collider = "box", rotation = (0, 90, 0), scale = (40, 20, 3), visible = False)

self.finish_line.disable()
self.boundaries.disable()
self.wall1.disable()
self.wall2.disable()
Expand Down

0 comments on commit 96883b0

Please sign in to comment.