From 1d0680eebf0226823f0182bdf9d5501519e06dd3 Mon Sep 17 00:00:00 2001 From: Jan Heemstra Date: Sun, 2 May 2021 23:50:22 +0200 Subject: [PATCH] Moved handgun code to handgun --- Game/Assets/Weapons/Handgun/Handgun.gd | 78 +++++++++++++++++++------- Game/Classes/Weapon/Weapon.gd | 66 ++-------------------- Game/Game.tscn | 1 + 3 files changed, 63 insertions(+), 82 deletions(-) diff --git a/Game/Assets/Weapons/Handgun/Handgun.gd b/Game/Assets/Weapons/Handgun/Handgun.gd index 046eeee..9d4aa97 100644 --- a/Game/Assets/Weapons/Handgun/Handgun.gd +++ b/Game/Assets/Weapons/Handgun/Handgun.gd @@ -1,26 +1,64 @@ extends "res://Classes/Weapon/Weapon.gd" - -# Declare member variables here. Examples: -# var a = 2 -# var b = "text" -#var trigger_held_primary = false -#var trigger_held_secondary = false -#var is_reloading = false - func set_trigger_held_primary(active:bool): trigger_held_primary = active + + if active: + shoot(camera, true) print("trigger_active", active) -#func get_trigger_held_primary(): -# return trigger_held_primary -# print("trigger_get") - -# 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 shoot(camera, primary): + if not Automatic: + if cached_fire == true: + return + + if is_reloading: + return + + if currently_fireing == true: + cached_fire = true + yield($Model/AnimationPlayer, "animation_finished") + + # TODO: mutexes + currently_fireing = true + cached_fire = false + + if current_rounds > 0: + rpc("fire_weapon", current_rounds) + rpc("compute_bullet_flyby") + + current_rounds -= 1 + + var space_state = get_world().direct_space_state + + var crosshair_pos = get_viewport().size / 2 + + var from = camera.project_ray_origin(crosshair_pos) + var to = from + camera.project_ray_normal(crosshair_pos) * 1000 + + var result = space_state.intersect_ray(from, to) + + if "collider" in result: + var hit = result.collider + + if hit.has_method("on_hit"): + hit.rpc("on_hit", 30, result.position, player.name) + + if hit is Player: + var kill = true if hit.health <= 0 else false + + #print ("Player: kill = ", kill, " Target health: ", hit.health) + emit_signal("damage_dealt", kill) + + #print(get_signal_connection_list("damage_dealt")[0]["target"].name) + + if kill: + player.score(hit.name) + + #print(get_signal_connection_list("ammo_changed")[0]["target"].name) + emit_signal("ammo_changed", self) + + if current_rounds == 0: + reload() + + return current_rounds diff --git a/Game/Classes/Weapon/Weapon.gd b/Game/Classes/Weapon/Weapon.gd index c3b4b05..f0a733f 100644 --- a/Game/Classes/Weapon/Weapon.gd +++ b/Game/Classes/Weapon/Weapon.gd @@ -42,64 +42,6 @@ func _ready(): func switched_to_weapon(): emit_signal("ammo_changed", self) -func shoot(camera, primary, trigger_held): - if not Automatic: - if cached_fire == true: - return - - if currently_fireing == true: - cached_fire = true - yield($Model/AnimationPlayer, "animation_finished") - - # TODO: mutexes - currently_fireing = true - cached_fire = false - - while current_rounds > 0 and trigger_held: - rpc("fire_weapon", current_rounds) - rpc("compute_bullet_flyby") - - current_rounds -= 1 - - var space_state = get_world().direct_space_state - - var crosshair_pos = get_viewport().size / 2 - - var from = camera.project_ray_origin(crosshair_pos) - var to = from + camera.project_ray_normal(crosshair_pos) * 1000 - - var result = space_state.intersect_ray(from, to) - - if "collider" in result: - var hit = result.collider - - if hit.has_method("on_hit"): - hit.rpc("on_hit", 30, result.position, player.name) - - if hit is Player: - var kill = true if hit.health <= 0 else false - - #print ("Player: kill = ", kill, " Target health: ", hit.health) - emit_signal("damage_dealt", kill) - - #print(get_signal_connection_list("damage_dealt")[0]["target"].name) - - if kill: - player.score(hit.name) - - #print(get_signal_connection_list("ammo_changed")[0]["target"].name) - emit_signal("ammo_changed", self) - - if not Automatic: - break - else: - yield(get_tree().create_timer(Delay),"timeout") - - if current_rounds == 0: - reload() - - return current_rounds - sync func fire_weapon(var rounds_left): show_muzzle_flash(rounds_left) show_tracer() @@ -167,14 +109,14 @@ remote func compute_bullet_flyby(): func reload(): rpc("play_reload_animation") - currently_fireing = true + is_reloading = true cached_fire = false yield($Model/AnimationPlayer, "animation_finished") - if not cached_fire: - currently_fireing = false - + #if not cached_fire: + currently_fireing = false + is_reloading = false current_rounds = Rounds emit_signal("ammo_changed", self) diff --git a/Game/Game.tscn b/Game/Game.tscn index 0938d76..1769f57 100644 --- a/Game/Game.tscn +++ b/Game/Game.tscn @@ -326,6 +326,7 @@ __meta__ = { visible = false margin_top = -264.542 margin_bottom = -80.5416 + [connection signal="pressed" from="MenuContainer/MainMenu/QuickConnect" to="." method="join_test_server"] [connection signal="pressed" from="MenuContainer/MainMenu/Connect" to="." method="open_menu" binds= [ "ConnectMenu" ]] [connection signal="pressed" from="MenuContainer/MainMenu/Disconnect" to="." method="free_client"]