Added scene switching capabilities and player placement

pull/1/head
DAMO238 2020-03-01 23:18:56 +00:00
parent 345044a431
commit b5a55ff3bd
5 changed files with 56 additions and 2 deletions

6
Scenes/Arena.tscn Normal file
View File

@ -0,0 +1,6 @@
[gd_scene load_steps=2 format=2]
[ext_resource path="res://Scripts/Arena.gd" type="Script" id=1]
[node name="Arena" type="Spatial"]
script = ExtResource( 1 )

6
Scenes/Player.tscn Normal file
View File

@ -0,0 +1,6 @@
[gd_scene load_steps=2 format=2]
[ext_resource path="res://Scripts/Player.gd" type="Script" id=1]
[node name="Player" type="Spatial"]
script = ExtResource( 1 )

21
Scripts/Arena.gd Normal file
View File

@ -0,0 +1,21 @@
extends Spatial
var players = {} # keys are String, values are Players
const Player = preload("res://Scenes/Player.tscn")
func init() -> void:
yield(get_tree(), "idle_frame")
get_parent().console_print('Starting arena inilisation...')
for player_id in get_parent().ids:
get_parent().console_print('Placing player with id: ' + String(player_id))
players[player_id] = Player.instance()
add_child(players[player_id])
players[player_id].translation = generate_translation()
players[player_id].rotation = generate_rotation()
rpc('place_player', player_id, players[player_id].translation, players[player_id].rotation)
func generate_translation() -> Vector3:
return Vector3(0,0,0)
func generate_rotation() -> Vector3:
return Vector3(0,0,1)

1
Scripts/Player.gd Normal file
View File

@ -0,0 +1 @@
extends Spatial

View File

@ -1,14 +1,18 @@
extends Node extends Node
const MAX_PLAYERS : int = 2 const MAX_PLAYERS : int = 1
const PORT : int = 9374 const PORT : int = 9374
const Arena = preload("res://Scenes/Arena.tscn")
var peer : NetworkedMultiplayerENet = null
var ids = [] var ids = []
var console_output : TextEdit = null var console_output : TextEdit = null
var ready_ids = []
func _ready() -> void: func _ready() -> void:
var peer : NetworkedMultiplayerENet = NetworkedMultiplayerENet.new() peer = NetworkedMultiplayerENet.new()
peer.create_server( PORT, MAX_PLAYERS ) peer.create_server( PORT, MAX_PLAYERS )
get_tree().set_network_peer( peer ) get_tree().set_network_peer( peer )
@ -27,12 +31,28 @@ func _peer_connected(id : int) -> void:
return return
ids.append(id) ids.append(id)
self.console_print("Player has connected with id " + String(id)) self.console_print("Player has connected with id " + String(id))
if len(ids) == MAX_PLAYERS:
console_print('Got required players... Ready to start game...')
peer.refuse_new_connections = true
rpc('start_game')
# TODO: When we reach max players -> start the game # TODO: When we reach max players -> start the game
func _peer_disconnected(id : int) -> void: func _peer_disconnected(id : int) -> void:
ids.erase(id) ids.erase(id)
self.console_print("Player has disconnected with id " + String(id)) self.console_print("Player has disconnected with id " + String(id))
remote func client_ready(id : int) -> void:
if id in ready_ids:
return
ready_ids.append(id)
if len(ready_ids) == MAX_PLAYERS:
start_game()
func start_game() -> void:
var arena = Arena.instance()
add_child(arena)
arena.init()
console_print('Starting Game...')
# Called every frame. 'delta' is the elapsed time since the previous frame. # Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta): #func _process(delta):
# pass # pass