fuzzyworld

godot-shooter / commit

Slightly reworked bullet and how it flies.

614e7fb1cd40ed6a76de2f6e7a07b79aec1cdf41 · Pavel Safronov · 2023-10-31 10:17:36 -0700 · parents b856550ad41a

Slightly reworked bullet and how it flies.
Adding a Utils script that can be used to log time-stamped messages.
 Bullet.gd   | 15 +++++++++++++--
 Bullet.tscn |  5 +++++
 Enemy.gd    |  9 ++++++++-
 Enemy.tscn  |  9 +++++++++
 Player.gd   |  3 ---
 Player.tscn |  1 -
 Utils.gd    | 13 +++++++++++++
 7 files changed, 48 insertions(+), 7 deletions(-)

diff --git a/Bullet.gd b/Bullet.gd
index e8bbb98..e99ed1d 100644
--- a/Bullet.gd
+++ b/Bullet.gd
@@ -1,15 +1,26 @@
 extends Area2D
 
-var speed = 750
+const utils = preload("res://Utils.gd")
+export var lifetime_seconds = 2
+export var speed = 200
+
+func _ready() -> void:
+	$Timer.wait_time = lifetime_seconds
+	$Timer.start()
+	utils.log("timer", $Timer)
 
 func _physics_process(delta: float) -> void:
 	position += transform.y * speed * delta
+	
+func on_expiration():
+	utils.log("killing bullet")
+	queue_free()
 
 func on_bullet_body_entered(body):
 	var tree = get_tree()
 	var players = tree.get_nodes_in_group("player")
 	var player = players[0]
 	if body.has_method("hurt") && body != player:
-		print("removing bullet")
+		utils.log("removing bullet")
 		body.hurt()
 		queue_free()
diff --git a/Bullet.tscn b/Bullet.tscn
index 161b6f6..307d6ef 100644
--- a/Bullet.tscn
+++ b/Bullet.tscn
@@ -21,4 +21,9 @@ visible = false
 position = Vector2( 0, -0.5 )
 shape = SubResource( 1 )
 
+[node name="Timer" type="Timer" parent="."]
+one_shot = true
+autostart = true
+
 [connection signal="body_entered" from="." to="." method="on_bullet_body_entered"]
+[connection signal="timeout" from="Timer" to="." method="on_expiration"]
diff --git a/Enemy.gd b/Enemy.gd
index f0666ae..0ab478e 100644
--- a/Enemy.gd
+++ b/Enemy.gd
@@ -2,6 +2,7 @@ extends KinematicBody2D
 
 onready var ray_cast_2d: RayCast2D = $RayCast2D
 onready var scene_tree: SceneTree = get_tree()
+const utils = preload("res://Utils.gd")
 
 export var move_speed = 100
 export var health: int = randi() % 8 + 2
@@ -11,6 +12,10 @@ var velocity = Vector2.ZERO
 
 var dead = false
 
+func _process(delta: float) -> void:
+	$Graphics/Alive/Container/HealthLabel.text = "%s / %s" % [health, max_health]
+	$Graphics/Alive/Container.global_rotation = 0
+
 func _physics_process(delta: float) -> void:
 	if dead:
 		return
@@ -20,7 +25,7 @@ func _physics_process(delta: float) -> void:
 
 	var dir_to_player = global_position.direction_to(player.global_position)
 	var angle_to_player = dir_to_player.angle() + PI / 2.0
-	var vel = dir_to_player * move_speed	
+	var vel = dir_to_player * move_speed
 	global_rotation = angle_to_player
 	velocity = move_and_slide(velocity)
 	
@@ -34,6 +39,7 @@ func _physics_process(delta: float) -> void:
 func hurt():
 	if dead:
 		return
+	utils.log("zombie is hurt, health was ", health)
 	health -= 1
 	if health <= 0:
 		kill()
@@ -41,6 +47,7 @@ func hurt():
 func kill():
 	if dead:
 		return
+	utils.log("zombie is dead")
 	dead = true
 	$Graphics/Alive.hide()
 	$CollisionPolygon2D.disabled = true
diff --git a/Enemy.tscn b/Enemy.tscn
index 026bd1d..c3ab696 100644
--- a/Enemy.tscn
+++ b/Enemy.tscn
@@ -11,6 +11,15 @@ script = ExtResource( 2 )
 [node name="Alive" type="Sprite" parent="Graphics"]
 texture = ExtResource( 1 )
 
+[node name="Container" type="Node2D" parent="Graphics/Alive"]
+
+[node name="HealthLabel" type="Label" parent="Graphics/Alive/Container"]
+margin_left = -25.0
+margin_top = 37.0
+margin_right = 25.0
+margin_bottom = 51.0
+text = "HEALTH"
+
 [node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="."]
 visible = false
 polygon = PoolVector2Array( 0, -23, -7, 5, -12, 9, -19, 9, -23, 14, -23, 19, -19, 23, -14, 23, -9, 18, -9, 14, -10, 12, -6, 9, 0, 15, 6, 9, 10, 12, 9, 14, 9, 19, 13, 23, 18, 23, 23, 18, 23, 14, 19, 10, 13, 10, 7, 5 )
diff --git a/Player.gd b/Player.gd
index 78c8443..f075ecf 100644
--- a/Player.gd
+++ b/Player.gd
@@ -3,7 +3,6 @@ extends KinematicBody2D
 export var move_speed = 200
 export var health = 8
 export var max_health = 10
-export var muzzle_velocity = 50
 export var shot_period_seconds = .1
 export var bullet: PackedScene
 
@@ -63,14 +62,12 @@ func hurt():
 func shoot():
 	if !can_fire:
 		return
-	print("shooting!")
 	can_fire = false
 	$ShotTimer.wait_time = shot_period_seconds
 	$ShotTimer.start()
 	var b = bullet.instance()
 	owner.add_child(b)
 	b.transform = $Barrel.global_transform
-#	b.velocity = Vector2(0, muzzle_velocity)
 	
 func allow_fire():
 	can_fire = true
diff --git a/Player.tscn b/Player.tscn
index edcf212..e88b837 100644
--- a/Player.tscn
+++ b/Player.tscn
@@ -8,7 +8,6 @@
 [node name="Player" type="KinematicBody2D"]
 rotation = -0.000165944
 script = ExtResource( 3 )
-shot_period_seconds = 0.2
 bullet = ExtResource( 4 )
 
 [node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="."]
diff --git a/Utils.gd b/Utils.gd
new file mode 100644
index 0000000..5424ca4
--- /dev/null
+++ b/Utils.gd
@@ -0,0 +1,13 @@
+# Note: `tool` is optional but without it there are no error reporting in the editor
+tool
+
+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
+	text += "> "
+	for argument in [arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9]:
+		if argument != null:
+			var argText = "%s " % argument
+			text += argText
+	print(text)