Fixed some rpc calls with the player

pull/1/head
DAMO238 2020-08-16 13:11:59 +01:00
parent b5a55ff3bd
commit 1e89282734
8 changed files with 182 additions and 11 deletions

View File

@ -1,6 +1,13 @@
[gd_scene load_steps=2 format=2]
[gd_scene load_steps=3 format=2]
[ext_resource path="res://Scripts/Player.gd" type="Script" id=1]
[node name="Player" type="Spatial"]
[sub_resource type="CapsuleShape" id=1]
radius = 0.1
height = 0.5
[node name="Player" type="RigidBody"]
script = ExtResource( 1 )
[node name="CollisionShape" type="CollisionShape" parent="."]
shape = SubResource( 1 )

View File

@ -9,13 +9,26 @@ func init() -> void:
for player_id in get_parent().ids:
get_parent().console_print('Placing player with id: ' + String(player_id))
players[player_id] = Player.instance()
players[player_id].set_name('player' + String(player_id))
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)
players[player_id].set_translation(generate_translation())
players[player_id].set_rotation(generate_rotation())
get_parent().console_print('Player Coords: ' + String(players[player_id].get_translation()) + String(players[player_id].get_rotation()) )
rpc('place_player', player_id, players[player_id].get_translation(), players[player_id].get_rotation())
players[player_id].init()
func _process(delta : float) -> void:
var all_ready = true
for player in players.values():
if !player.is_ready:
all_ready = false
if all_ready:
for player in players.values():
player.is_ready = false
player.send_thrust_plan()
func generate_translation() -> Vector3:
return Vector3(0,0,0)
return Vector3(rand_range(-10,10),rand_range(-10,10),rand_range(-10,10))
func generate_rotation() -> Vector3:
return Vector3(0,0,1)
return Vector3(rand_range(-1,1),rand_range(-1,1),rand_range(-1,1)).normalized()

42
Scripts/Plan.gd Normal file
View File

@ -0,0 +1,42 @@
class_name Plan
const TURN_TIME : float = 5.0
var elements = [] # All elements of the plan thus far
var current_elements = [] # All elements of the next 5 seconds
func current_time() -> float:
var total_time : float = 0.0
for current_element in current_elements:
var _element : PlanElement = (current_element as PlanElement)
total_time += _element.time
return total_time
func add_element(element : PlanElement) -> bool:
var total_time : float = self.current_time()
if total_time + element.time > TURN_TIME:
return false
current_elements.append(element)
return true
func remove_last_element() -> void:
current_elements.pop_back()
func remove_all_elements() -> void:
current_elements.clear()
func new_turn() -> void:
for current_element in current_elements:
elements.push_back(current_elements.pop_back())
func sanity_check() -> bool:
# Returns true if sane and false if insane
var total_time : float = 0.0
for current_element in current_elements:
var element : PlanElement = (current_element as PlanElement)
total_time += element.time
if !element.sanity_check():
return false
if total_time > TURN_TIME:
return false
return true

6
Scripts/PlanElement.gd Normal file
View File

@ -0,0 +1,6 @@
class_name PlanElement
var time = 0.0
func sanity_check() -> bool:
return true # Needs to overridden in child classes

View File

@ -1 +1,71 @@
extends Spatial
extends RigidBody
const TIME_STEP : float = 0.01
var thrust_plan : Plan = Plan.new()
var is_ready : bool = false
var running_plan : bool = false
var plan_time : float = 0.0
var last_translation : Vector3 = get_translation()
var last_rotation : Vector3 = get_rotation()
var enable_physics : bool = false
func init():
last_rotation = get_rotation()
last_translation = get_translation()
enable_physics = true
remote func add_thrust_element(time: float, linear_thrust : Vector3, rotational_thrust : Vector3) -> void:
var element : ThrustElement = ThrustElement.new()
element.time = time
element.linear_thrust = linear_thrust
element.rotational_thrust = rotational_thrust
get_parent().get_parent().console_print("Adding thrust element with the following params: time = " + String(time) + ", lin = " + String(linear_thrust) + ", rot = " + String(rotational_thrust) + ".")
thrust_plan.add_element(element)
func send_thrust_plan() -> void:
get_parent().get_parent().console_print("Sending plan...")
for element in thrust_plan.current_elements:
rpc("add_thrust_element", element.time, element.linear_thrust, element.rotational_thrust)
rpc("r_end_turn")
thrust_plan.new_turn()
remote func end_turn() -> void:
get_parent().get_parent().console_print("Setting ready status to true...")
is_ready = true
func _get_current_plan_element(all_elements) -> PlanElement:
var summed_time : float = 0.0
for element in all_elements:
summed_time += element.time
if summed_time > plan_time:
return element
return null
func _get_all_thrust_elements():
var all_elements = []
all_elements = thrust_plan.elements
for element in thrust_plan.current_elements:
all_elements.append(element)
return all_elements
func _physics_process(delta : float) -> void:
if !enable_physics:
return
if !running_plan:
translation = last_translation
rotation = last_rotation
return
var all_thrust_elements = _get_all_thrust_elements()
var current_thrust_element : ThrustElement = _get_current_plan_element(all_thrust_elements)
if current_thrust_element == null:
last_translation = translation
last_rotation = rotation
running_plan = false
return
add_central_force(self.transform.basis.xform(current_thrust_element.linear_thrust))
add_torque(self.transform.basis.xform(current_thrust_element.rotational_thrust))
plan_time += delta

View File

@ -1,7 +1,7 @@
extends Node
const MAX_PLAYERS : int = 1
const MAX_PLAYERS : int = 2
const PORT : int = 9374
const Arena = preload("res://Scenes/Arena.tscn")

16
Scripts/ThrustElement.gd Normal file
View File

@ -0,0 +1,16 @@
extends "res://Scripts/PlanElement.gd"
class_name ThrustElement
const MAX_LINEAR_THRUST : int = 1
const MAX_ROTATIONAL_THRUST : int = 1
var linear_thrust : Vector3 = Vector3(0, 0, 0)
var rotational_thrust : Vector3 = Vector3(0, 0, 0)
func sanity_check() -> bool:
if linear_thrust.length() > MAX_LINEAR_THRUST:
return false
if rotational_thrust.length() > MAX_ROTATIONAL_THRUST:
return false
return true

View File

@ -8,9 +8,26 @@
config_version=4
_global_script_classes=[ ]
_global_script_classes=[ {
"base": "Reference",
"class": "Plan",
"language": "GDScript",
"path": "res://Scripts/Plan.gd"
}, {
"base": "Reference",
"class": "PlanElement",
"language": "GDScript",
"path": "res://Scripts/PlanElement.gd"
}, {
"base": "Reference",
"class": "ThrustElement",
"language": "GDScript",
"path": "res://Scripts/ThrustElement.gd"
} ]
_global_script_class_icons={
"Plan": "",
"PlanElement": "",
"ThrustElement": ""
}
[application]