fuzzyworld

godot-shooter / commit

Move shooting logic into the weapon class.

e9f81b98bcdc6bd62cda19ebab26df05ca6b0223 · Pavel Safronov · 2023-11-29 15:18:33 -0800 · parents 3782f0cfd143

Move shooting logic into the weapon class.
 actors/Player.gd   | 32 +++-----------------------------
 actors/Player.tscn |  6 ------
 scenes/World.gd    |  3 ++-
 utils/Utils.gd     | 11 +++++++++--
 weapons/Weapon.gd  | 24 ++++++++++++++++++++++++
 5 files changed, 38 insertions(+), 38 deletions(-)

diff --git a/actors/Player.gd b/actors/Player.gd
index 2d7c4de..e342128 100644
--- a/actors/Player.gd
+++ b/actors/Player.gd
@@ -32,7 +32,6 @@ onready var pause_always_show_intro_check = $UI/PauseMenu/CenterContainer/PanelC
 onready var pause_show_items_in_radar_check = $UI/PauseMenu/CenterContainer/PanelContainer/VBoxContainer/TabContainer/Pref/ShowItemsInRadarCheckBox
 onready var health_label = $Graphics/Alive/StaticControls/HealthLabel
 onready var trails = $Graphics/Alive/Trails
-onready var shot_timer = $ShotTimer
 onready var death_screen = $UI/DeathScreen
 onready var final_score_label = $UI/DeathScreen/CenterContainer/PanelContainer/VBoxContainer/FinalScoreLabel
 onready var player_to_target = $Graphics/Alive/PlayerToTarget
@@ -43,7 +42,6 @@ const PopupControl = preload("res://utils/Popup.tscn")
 var weapon: Weapon = create_default_weapon()
 var dead = false
 var velocity: Vector2 = Vector2.UP * move_speed
-var can_fire = true
 var settings: Settings
 
 func filter_invalid_enemies(enemy: Enemy) -> bool:
@@ -206,35 +204,11 @@ func turn_towards(dest: Vector2, _delta: float):
 	var dir_diff = to_dir - curr_dir
 	var change = dir_diff * turn_rate * move_speed
 	velocity = (velocity + change).normalized() * move_speed
-
-func shoot():
-	if !can_fire:
-		return
-	can_fire = false
-	shot_timer.wait_time = weapon.shot_period_seconds
-	shot_timer.start()
-	var particle = weapon.particle.instance()
-	owner.add_child(particle)
-	particle.transform = $Barrel.global_transform
 	
 func shoot_at(pos: Vector2):
-	if !can_fire:
-		return
-	can_fire = false
-	if weapon.ammo != INF:
-		if weapon.ammo <= 0:
-			weapon = create_default_weapon()
-		weapon.ammo -= 1
-	shot_timer.wait_time = weapon.cooldown_seconds
-	shot_timer.start()
-	var particle = weapon.particle.instance()
-	particle.damage_amount = weapon.damage_amount
-	owner.add_child(particle)
-	particle.global_position = global_position
-	particle.global_rotation = global_position.angle_to_point(pos) + PI / 2.0
-	
-func allow_fire():
-	can_fire = true
+	if weapon.ammo != INF && weapon.ammo <= 0:
+		weapon = create_default_weapon()
+	weapon.try_shoot(owner, $Barrel, pos)
 
 func set_objective(objective: String) -> void:
 	hud.objective = objective
diff --git a/actors/Player.tscn b/actors/Player.tscn
index 9735896..90948e7 100644
--- a/actors/Player.tscn
+++ b/actors/Player.tscn
@@ -372,12 +372,7 @@ text = "Replay!"
 [node name="Camera2D" type="Camera2D" parent="."]
 current = true
 
-[node name="ShotTimer" type="Timer" parent="."]
-wait_time = 0.5
-autostart = true
-
 [node name="Barrel" type="Position2D" parent="."]
-position = Vector2( 0, -20 )
 rotation = -3.14044
 __meta__ = {
 "_gizmo_extents_": 14.2
@@ -392,4 +387,3 @@ __meta__ = {
 [connection signal="pressed" from="UI/PauseMenu/CenterContainer/PanelContainer/VBoxContainer/TabContainer/Pref/ShowItemsInRadarCheckBox" to="." method="_on_ShowItemsInRadarCheckBox_pressed"]
 [connection signal="pressed" from="UI/PauseMenu/CenterContainer/PanelContainer/VBoxContainer/TabContainer/About/LinkButton" to="." method="_on_LinkButton_pressed"]
 [connection signal="pressed" from="UI/PauseMenu/CenterContainer/PanelContainer/VBoxContainer/ResumeButton" to="." method="_on_ResumeButton_pressed"]
-[connection signal="timeout" from="ShotTimer" to="." method="allow_fire"]
diff --git a/scenes/World.gd b/scenes/World.gd
index 64748d1..6b10aac 100644
--- a/scenes/World.gd
+++ b/scenes/World.gd
@@ -186,7 +186,8 @@ func _on_Player_debug() -> void:
 #	player.kill()
 #	completed_objectives = max_objectives
 #	player.debug()
-	weapon_consumed(Weapons.create_rocket_gun())
+#	weapon_consumed(Weapons.create_rocket_gun())
+#	weapon_consumed(Weapons.create_gattling_gun())
 	pass # Replace with function body.
 
 func _on_Player_destination_set() -> void:
diff --git a/utils/Utils.gd b/utils/Utils.gd
index 9623c56..a8bb2e3 100644
--- a/utils/Utils.gd
+++ b/utils/Utils.gd
@@ -34,8 +34,15 @@ static func load_user_settings(path = SettingsClass.settings_path) -> Settings:
 
 static func log(arg1 = null, arg2 = null, arg3 = null, arg4 = null, arg5 = null, arg6 = null, arg7 = null, arg8 = null, arg9 = null):
 	var text = ""
-	var now = Time.get_datetime_string_from_system(false, true)
-	text += now
+	var now_unix_time = Time.get_unix_time_from_system()
+	var now_dict = Time.get_datetime_dict_from_unix_time(now_unix_time)
+	var seconds_decimal = now_unix_time - floor(now_unix_time)
+	if seconds_decimal >= 1:
+		seconds_decimal = 0
+	var seconds_decimal_text = "%.6f" % seconds_decimal
+	seconds_decimal_text = seconds_decimal_text.replace("0.", "")
+	var now_text = "%s:%s:%s.%s" % [now_dict["hour"], now_dict["minute"], now_dict["second"], seconds_decimal_text]
+	text += now_text
 	text += "> "
 	for argument in [arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9]:
 		if argument != null:
diff --git a/weapons/Weapon.gd b/weapons/Weapon.gd
index 359b386..a026fae 100644
--- a/weapons/Weapon.gd
+++ b/weapons/Weapon.gd
@@ -9,3 +9,27 @@ var damage_amount = 1
 var ammo = INF
 var name = "Laser"
 var reload_size = 1
+var last_shoot_time: float = 0
+
+const Utils = preload("res://utils/Utils.gd")
+
+func try_shoot(owner: Node2D, barrel: Node2D, target: Vector2) -> void:
+	var has_ammo = ammo == INF or ammo > 0
+	if not has_ammo:
+		return
+
+	var now = Time.get_unix_time_from_system()
+	var sec_since_shot = now - last_shoot_time
+	var can_shoot = sec_since_shot > cooldown_seconds
+	if not can_shoot:
+		return
+
+	if ammo != INF:
+		ammo = ammo - 1
+
+	var particle_instance = particle.instance()
+	owner.add_child(particle_instance)
+	particle_instance.transform = barrel.global_transform
+	particle_instance.damage_amount = self.damage_amount
+	particle_instance.global_rotation = barrel.global_position.angle_to_point(target) + PI / 2.0
+	last_shoot_time = now