Added weapons systems

pull/1/head
DAMO238 2020-08-29 17:33:30 +01:00
parent 1e89282734
commit 40b23a78a0
5 changed files with 116 additions and 9 deletions

View File

@ -11,3 +11,11 @@ script = ExtResource( 1 )
[node name="CollisionShape" type="CollisionShape" parent="."] [node name="CollisionShape" type="CollisionShape" parent="."]
shape = SubResource( 1 ) shape = SubResource( 1 )
[node name="GunContainer" type="Spatial" parent="."]
transform = Transform( 2.65431e-15, 1.62921e-08, 0.1, 0.1, -1.62921e-08, 0, 1.62921e-08, 0.1, -1.62921e-08, 0, -0.22, 0.383 )
[node name="RayCast" type="RayCast" parent="GunContainer"]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.769, 0 )
enabled = true
cast_to = Vector3( 0, 500, 0 )

View File

@ -2,6 +2,7 @@ extends Spatial
var players = {} # keys are String, values are Players var players = {} # keys are String, values are Players
const Player = preload("res://Scenes/Player.tscn") const Player = preload("res://Scenes/Player.tscn")
var tmp = false
func init() -> void: func init() -> void:
yield(get_tree(), "idle_frame") yield(get_tree(), "idle_frame")
@ -20,15 +21,21 @@ func init() -> void:
func _process(delta : float) -> void: func _process(delta : float) -> void:
var all_ready = true var all_ready = true
for player in players.values(): for player in players.values():
if !player.is_ready: if !player.is_ready and player.is_alive:
all_ready = false all_ready = false
if all_ready: if all_ready:
for player in players.values(): for player in players.values():
player.is_ready = false player.is_ready = false
player.send_thrust_plan() player.calculate_plans()
func generate_translation() -> Vector3: func generate_translation() -> Vector3:
return Vector3(rand_range(-10,10),rand_range(-10,10),rand_range(-10,10)) #return Vector3(rand_range(-10,10),rand_range(-10,10),rand_range(-10,10))
return Vector3(0,0,rand_range(-10,10))
func generate_rotation() -> Vector3: func generate_rotation() -> Vector3:
return Vector3(rand_range(-1,1),rand_range(-1,1),rand_range(-1,1)).normalized() #return Vector3(rand_range(-1,1),rand_range(-1,1),rand_range(-1,1)).normalized()
return Vector3(0,0,0)
func play_full_plans():
for player in players.values():
player.play_full_plan()

View File

@ -1,19 +1,35 @@
extends RigidBody extends RigidBody
const TIME_STEP : float = 0.01 const TIME_STEP : float = 0.01
const BASE_DPS : float = 300.0
var thrust_plan : Plan = Plan.new() var thrust_plan : Plan = Plan.new()
var weapons_plan : Plan = Plan.new()
var is_ready : bool = false var is_ready : bool = false
var running_plan : bool = false var running_plan : bool = false
var plan_time : float = 0.0 var plan_time : float = 0.0
var health : float = 100.0
var is_alive : bool = true
var time_of_death : float = -1
var initial_translation : Vector3 = get_translation()
var initial_rotation : Vector3 = get_rotation()
var initial_velocity : Vector3 = Vector3(0,0,0)
var initial_rotational_velocity : Vector3 = Vector3(0,0,0)
var last_translation : Vector3 = get_translation() var last_translation : Vector3 = get_translation()
var last_rotation : Vector3 = get_rotation() var last_rotation : Vector3 = get_rotation()
var enable_physics : bool = false var enable_physics : bool = false
var waiting_for_completion : bool = false
func init(): func init():
last_rotation = get_rotation() last_rotation = get_rotation()
last_translation = get_translation() last_translation = get_translation()
initial_rotation = get_rotation()
initial_translation = get_translation()
enable_physics = true enable_physics = true
health = 100
remote func add_thrust_element(time: float, linear_thrust : Vector3, rotational_thrust : Vector3) -> void: remote func add_thrust_element(time: float, linear_thrust : Vector3, rotational_thrust : Vector3) -> void:
var element : ThrustElement = ThrustElement.new() var element : ThrustElement = ThrustElement.new()
@ -23,20 +39,28 @@ remote func add_thrust_element(time: float, linear_thrust : Vector3, rotational_
get_parent().get_parent().console_print("Adding thrust element with the following params: time = " + String(time) + ", lin = " + String(linear_thrust) + ", rot = " + String(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) thrust_plan.add_element(element)
remote func add_weapons_element(time: float, firing: bool) -> void:
var element : WeaponsElement = WeaponsElement.new()
element.time = time
element.firing = firing
get_parent().get_parent().console_print("Adding weapons element with the following params: time = " + String(time) + ", firing = "+ String(firing))
weapons_plan.add_element(element)
func send_thrust_plan() -> void: func send_all_plans() -> void:
get_parent().get_parent().console_print("Sending plan...") get_parent().get_parent().console_print("Sending plan...")
for element in thrust_plan.current_elements: for element in thrust_plan.current_elements:
rpc("add_thrust_element", element.time, element.linear_thrust, element.rotational_thrust) rpc("add_thrust_element", element.time, element.linear_thrust, element.rotational_thrust)
rpc("r_end_turn") for element in weapons_plan.current_elements:
rpc("add_weapons_element", element.time, element.firing)
get_parent().get_parent().console_print("Time of death is " + String(time_of_death) + " with health " + String(health) + ".")
rpc("r_end_turn", time_of_death)
thrust_plan.new_turn() thrust_plan.new_turn()
weapons_plan.new_turn()
remote func end_turn() -> void: remote func end_turn() -> void:
get_parent().get_parent().console_print("Setting ready status to true...") get_parent().get_parent().console_print("Setting ready status to true...")
is_ready = true is_ready = true
func _get_current_plan_element(all_elements) -> PlanElement: func _get_current_plan_element(all_elements) -> PlanElement:
var summed_time : float = 0.0 var summed_time : float = 0.0
for element in all_elements: for element in all_elements:
@ -52,10 +76,21 @@ func _get_all_thrust_elements():
all_elements.append(element) all_elements.append(element)
return all_elements return all_elements
func _get_all_weapons_elements():
var all_elements = []
for element in weapons_plan.elements:
all_elements.append(element)
for element in weapons_plan.current_elements:
all_elements.append(element)
return all_elements
func _physics_process(delta : float) -> void: func _physics_process(delta : float) -> void:
if !enable_physics: if !enable_physics:
return return
if !running_plan: if !running_plan:
if waiting_for_completion:
send_all_plans()
waiting_for_completion = false
translation = last_translation translation = last_translation
rotation = last_rotation rotation = last_rotation
return return
@ -68,4 +103,46 @@ func _physics_process(delta : float) -> void:
return return
add_central_force(self.transform.basis.xform(current_thrust_element.linear_thrust)) add_central_force(self.transform.basis.xform(current_thrust_element.linear_thrust))
add_torque(self.transform.basis.xform(current_thrust_element.rotational_thrust)) add_torque(self.transform.basis.xform(current_thrust_element.rotational_thrust))
var all_weapons_elements = _get_all_weapons_elements()
var current_weapons_element : WeaponsElement = _get_current_plan_element(all_weapons_elements)
if current_weapons_element == null:
running_plan = false
return
if current_weapons_element.firing and is_alive:
fire(delta, plan_time)
plan_time += delta plan_time += delta
func fire(delta : float, time : float) -> void:
if $GunContainer/RayCast.is_colliding():
var target : RigidBody = $GunContainer/RayCast.get_collider()
target.take_damage(BASE_DPS * delta, time)
func take_damage(damage : float, time : float) -> void:
get_parent().get_parent().console_print("tmp")
health -= damage
if health < 0:
self.die(time)
func die(time : float) -> void:
is_alive = false
$CollisionShape.disabled = true
time_of_death = time
func play_all_plans():
get_parent().play_full_plans()
func play_full_plan():
set_linear_velocity(initial_velocity)
set_angular_velocity(initial_rotational_velocity)
set_translation(initial_translation)
set_rotation(initial_rotation)
running_plan = true
plan_time = 0.0
is_alive = true
$CollisionShape.disabled = false
func calculate_plans():
play_full_plan()
waiting_for_completion = true

View File

@ -0,0 +1,9 @@
extends "res://Scripts/PlanElement.gd"
class_name WeaponsElement
var firing : bool = false
func sanity_check() -> bool:
return true

View File

@ -23,11 +23,17 @@ _global_script_classes=[ {
"class": "ThrustElement", "class": "ThrustElement",
"language": "GDScript", "language": "GDScript",
"path": "res://Scripts/ThrustElement.gd" "path": "res://Scripts/ThrustElement.gd"
}, {
"base": "Reference",
"class": "WeaponsElement",
"language": "GDScript",
"path": "res://Scripts/WeaponsElement.gd"
} ] } ]
_global_script_class_icons={ _global_script_class_icons={
"Plan": "", "Plan": "",
"PlanElement": "", "PlanElement": "",
"ThrustElement": "" "ThrustElement": "",
"WeaponsElement": ""
} }
[application] [application]