Added Multiplayer Support
This commit is contained in:
parent
beb2b93613
commit
09c345785c
11 changed files with 338 additions and 46 deletions
10
scripts/create.gd
Normal file
10
scripts/create.gd
Normal file
|
@ -0,0 +1,10 @@
|
|||
extends Control
|
||||
|
||||
|
||||
func _ready():
|
||||
Server.create_game()
|
||||
|
||||
|
||||
func _on_start_pressed():
|
||||
Server.send_playerinfo(get_tree().root.get_node("create/player_customization/name").text, multiplayer.get_unique_id())
|
||||
Server.start_game.rpc(Server.players)
|
|
@ -1,6 +1,9 @@
|
|||
extends Node
|
||||
|
||||
var computers = 0
|
||||
var players = 0
|
||||
|
||||
func _process(delta):
|
||||
print(computers)
|
||||
pass
|
||||
|
||||
|
||||
|
|
14
scripts/join.gd
Normal file
14
scripts/join.gd
Normal file
|
@ -0,0 +1,14 @@
|
|||
extends Control
|
||||
|
||||
|
||||
|
||||
func _on_join_pressed():
|
||||
if $player_customization/name.text != "" or $player_customization/ip.text != "":
|
||||
Server.join_game($player_customization/ip.text)
|
||||
$player_customization/join.hide()
|
||||
$player_customization/join.disabled = true
|
||||
$player_customization/Timer.start()
|
||||
|
||||
|
||||
func _on_timer_timeout():
|
||||
Server.send_playerinfo.rpc($player_customization/name.text, multiplayer.get_unique_id())
|
20
scripts/main_menu.gd
Normal file
20
scripts/main_menu.gd
Normal file
|
@ -0,0 +1,20 @@
|
|||
extends Control
|
||||
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready():
|
||||
pass # Replace with function body.
|
||||
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _process(delta):
|
||||
pass
|
||||
|
||||
|
||||
func _on_create_pressed():
|
||||
get_tree().change_scene_to_file("res://menus/create.tscn")
|
||||
|
||||
|
||||
func _on_join_pressed():
|
||||
get_tree().change_scene_to_file("res://menus/join.tscn")
|
||||
|
|
@ -4,50 +4,67 @@ extends CharacterBody3D
|
|||
const SPEED = 5.0
|
||||
const JUMP_VELOCITY = 4.5
|
||||
var zoom = 0
|
||||
var player_no
|
||||
var enabled = false
|
||||
|
||||
|
||||
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
|
||||
|
||||
func _ready():
|
||||
player_no = Game.players
|
||||
Game.players += 1
|
||||
print(Server.players)
|
||||
print(Server.players_numbered[player_no])
|
||||
print(multiplayer.get_unique_id())
|
||||
if Server.players_numbered[player_no] == multiplayer.get_unique_id():
|
||||
enabled = true
|
||||
$cam_y/Camera3D.current = true
|
||||
|
||||
func _physics_process(delta):
|
||||
|
||||
if not is_on_floor():
|
||||
velocity.y -= gravity * delta
|
||||
|
||||
|
||||
if Input.is_action_just_pressed("jump") and is_on_floor():
|
||||
velocity.y = JUMP_VELOCITY
|
||||
if enabled:
|
||||
|
||||
var input_dir = Input.get_vector("left", "right", "forwards", "backwards")
|
||||
var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
|
||||
if direction:
|
||||
velocity.x = direction.x * SPEED
|
||||
velocity.z = direction.z * SPEED
|
||||
else:
|
||||
velocity.x = move_toward(velocity.x, 0, SPEED)
|
||||
velocity.z = move_toward(velocity.z, 0, SPEED)
|
||||
if not is_on_floor():
|
||||
velocity.y -= gravity * delta
|
||||
|
||||
move_and_slide()
|
||||
|
||||
if Input.is_action_just_pressed("jump") and is_on_floor():
|
||||
velocity.y = JUMP_VELOCITY
|
||||
|
||||
var input_dir = Input.get_vector("left", "right", "forwards", "backwards")
|
||||
var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
|
||||
if direction:
|
||||
velocity.x = direction.x * SPEED
|
||||
velocity.z = direction.z * SPEED
|
||||
else:
|
||||
velocity.x = move_toward(velocity.x, 0, SPEED)
|
||||
velocity.z = move_toward(velocity.z, 0, SPEED)
|
||||
|
||||
move_and_slide()
|
||||
Server.sync_player.rpc(name, position, rotation)
|
||||
|
||||
func _input(event):
|
||||
if event is InputEventMouseMotion and (Input.is_action_pressed("cam_look") or $cam_y/Camera3D.position.z == 0):
|
||||
var camera_rotation = event.relative * 0.01
|
||||
if $cam_y.rotation.x <= 1.6 && camera_rotation.y <= 0:
|
||||
$cam_y.rotate(Vector3.RIGHT, -camera_rotation.y)
|
||||
elif $cam_y.rotation.x >= -1.6 && camera_rotation.y >= 0:
|
||||
$cam_y.rotate(Vector3.RIGHT, -camera_rotation.y)
|
||||
if $cam_y.rotation.x >= 1.6:
|
||||
$cam_y.rotation.x = 1.6
|
||||
elif $cam_y.rotation.x <= -1.6:
|
||||
$cam_y.rotation.x = -1.6
|
||||
rotate(Vector3.DOWN, camera_rotation.x)
|
||||
if enabled:
|
||||
|
||||
if event is InputEventMouseMotion and (Input.is_action_pressed("cam_look") or $cam_y/Camera3D.position.z == 0):
|
||||
var camera_rotation = event.relative * 0.01
|
||||
if $cam_y.rotation.x <= 1.6 && camera_rotation.y <= 0:
|
||||
$cam_y.rotate(Vector3.RIGHT, -camera_rotation.y)
|
||||
elif $cam_y.rotation.x >= -1.6 && camera_rotation.y >= 0:
|
||||
$cam_y.rotate(Vector3.RIGHT, -camera_rotation.y)
|
||||
if $cam_y.rotation.x >= 1.6:
|
||||
$cam_y.rotation.x = 1.6
|
||||
elif $cam_y.rotation.x <= -1.6:
|
||||
$cam_y.rotation.x = -1.6
|
||||
rotate(Vector3.DOWN, camera_rotation.x)
|
||||
|
||||
func _unhandled_input(event):
|
||||
if event.is_action_pressed("zoom_in") && $cam_y/Camera3D.position.z > 0:
|
||||
zoom = -1
|
||||
$cam_y/Camera3D.position.z += zoom
|
||||
elif event.is_action_pressed("zoom_out") && $cam_y/Camera3D.position.z <= 20:
|
||||
zoom = 1
|
||||
$cam_y/Camera3D.position.z += zoom
|
||||
else:
|
||||
zoom = 0
|
||||
if enabled:
|
||||
|
||||
if event.is_action_pressed("zoom_in") && $cam_y/Camera3D.position.z > 0:
|
||||
zoom = -1
|
||||
$cam_y/Camera3D.position.z += zoom
|
||||
elif event.is_action_pressed("zoom_out") && $cam_y/Camera3D.position.z <= 20:
|
||||
zoom = 1
|
||||
$cam_y/Camera3D.position.z += zoom
|
||||
else:
|
||||
zoom = 0
|
||||
|
|
57
scripts/server.gd
Normal file
57
scripts/server.gd
Normal file
|
@ -0,0 +1,57 @@
|
|||
extends Node
|
||||
|
||||
const DEFAULT_SERVER_IP = "127.0.0.1"
|
||||
const PORT = 36969
|
||||
const MAX_CONNECTIONS = 16
|
||||
|
||||
var players = {}
|
||||
var players_numbered = []
|
||||
var label
|
||||
var map
|
||||
var character = preload("res://objects/player.tscn")
|
||||
|
||||
func join_game(ip):
|
||||
var peer = ENetMultiplayerPeer.new()
|
||||
var error = peer.create_client(ip, PORT)
|
||||
if error:
|
||||
print("error")
|
||||
return error
|
||||
multiplayer.multiplayer_peer = peer
|
||||
|
||||
|
||||
func create_game():
|
||||
var peer = ENetMultiplayerPeer.new()
|
||||
var error = peer.create_server(PORT, MAX_CONNECTIONS)
|
||||
if error:
|
||||
print("error")
|
||||
return error
|
||||
multiplayer.multiplayer_peer = peer
|
||||
print("done")
|
||||
|
||||
@rpc("any_peer", "call_remote", "reliable")
|
||||
func send_playerinfo(name, id):
|
||||
if multiplayer.is_server():
|
||||
players[id] = name
|
||||
label = get_tree().root.get_node("create/player_list/list")
|
||||
for w in players:
|
||||
label.text = str(label.text +"\n" + players[w])
|
||||
|
||||
@rpc("authority", "call_local", "reliable")
|
||||
func start_game(server_players):
|
||||
players = server_players
|
||||
players_numbered = players.keys()
|
||||
get_tree().change_scene_to_file("res://maps/base_map.tscn")
|
||||
map = get_tree().root.get_node(".")
|
||||
var i = 0
|
||||
for w in players:
|
||||
var player = character.instantiate()
|
||||
player.name = "player" + str(i)
|
||||
i += 1
|
||||
player.position.z = -i*1.5
|
||||
map.add_child(player)
|
||||
|
||||
@rpc("any_peer", "call_remote", "unreliable")
|
||||
func sync_player(node_name, position, rotation):
|
||||
var current_character = get_tree().root.get_node("./"+node_name)
|
||||
current_character.position = position
|
||||
current_character.rotation = rotation
|
Loading…
Add table
Add a link
Reference in a new issue