From 3f1f4b1811cb93eb4dc290840d708973e0991c18 Mon Sep 17 00:00:00 2001 From: Thayol Date: Wed, 26 Feb 2025 16:17:14 +0900 Subject: [PATCH] add attacks and death --- scenes/chaser.tscn | 25 ++++++++++++++++++++++++- scenes/runner.tscn | 4 ++++ scripts/runner.gd | 7 +++++-- scripts/states/dead.gd | 5 +++++ scripts/states/fall.gd | 1 + scripts/states/idle.gd | 6 +++--- scripts/states/lunge.gd | 35 +++++++++++++++++++++++++++++++++-- scripts/states/run.gd | 7 ++++--- scripts/states/walk.gd | 7 ++++--- 9 files changed, 83 insertions(+), 14 deletions(-) diff --git a/scenes/chaser.tscn b/scenes/chaser.tscn index 980165e..2497eca 100644 --- a/scenes/chaser.tscn +++ b/scenes/chaser.tscn @@ -20,6 +20,12 @@ properties/1/replication_mode = 1 properties/2/path = NodePath(".:server_rotation") properties/2/spawn = true properties/2/replication_mode = 1 +properties/3/path = NodePath("RotationBase/Attack:visible") +properties/3/spawn = true +properties/3/replication_mode = 1 +properties/4/path = NodePath(".:dead") +properties/4/spawn = true +properties/4/replication_mode = 1 [sub_resource type="SceneReplicationConfig" id="SceneReplicationConfig_1agtp"] properties/0/path = NodePath("Input:direction") @@ -77,6 +83,7 @@ unique_name_in_owner = true transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.5, 0) [node name="Collider" type="CollisionShape3D" parent="."] +unique_name_in_owner = true transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0) shape = SubResource("CapsuleShape3D_j6tb3") @@ -105,8 +112,9 @@ transform = Transform3D(0.1, 0, 0, 0, 0.1, 0, 0, 0, 0.1, -0.1, 0.6, -0.25) mesh = SubResource("SphereMesh_1gltg") [node name="Attack" type="Area3D" parent="RotationBase"] +unique_name_in_owner = true transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, -0.75) -monitoring = false +visible = false monitorable = false [node name="CollisionShape3D" type="CollisionShape3D" parent="RotationBase/Attack"] @@ -117,6 +125,19 @@ transparency = 0.8 mesh = SubResource("BoxMesh_phaav") surface_material_override/0 = SubResource("StandardMaterial3D_ssauw") +[node name="AttackTimer" type="Timer" parent="RotationBase/Attack"] +unique_name_in_owner = true +process_callback = 0 +wait_time = 0.35 +one_shot = true + +[node name="AttackCooldown" type="Timer" parent="RotationBase/Attack"] +unique_name_in_owner = true +process_callback = 0 +wait_time = 2.0 +one_shot = true +autostart = true + [node name="FloatingCamera" type="Node" parent="."] [node name="CameraPlatform" type="Node3D" parent="FloatingCamera"] @@ -154,3 +175,5 @@ unique_name_in_owner = true script = ExtResource("9_nqccg") [connection signal="delta_synchronized" from="Sync" to="." method="_on_sync_delta_synchronized"] +[connection signal="body_entered" from="RotationBase/Attack" to="StateMachine/Lunge" method="_on_attack_body_entered"] +[connection signal="body_exited" from="RotationBase/Attack" to="StateMachine/Lunge" method="_on_attack_body_exited"] diff --git a/scenes/runner.tscn b/scenes/runner.tscn index 8d071ba..1e6846a 100644 --- a/scenes/runner.tscn +++ b/scenes/runner.tscn @@ -20,6 +20,9 @@ properties/1/replication_mode = 1 properties/2/path = NodePath(".:server_rotation") properties/2/spawn = true properties/2/replication_mode = 1 +properties/3/path = NodePath(".:dead") +properties/3/spawn = true +properties/3/replication_mode = 1 [sub_resource type="SceneReplicationConfig" id="SceneReplicationConfig_1agtp"] properties/0/path = NodePath("Input:direction") @@ -68,6 +71,7 @@ unique_name_in_owner = true transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.5, 0) [node name="Collider" type="CollisionShape3D" parent="."] +unique_name_in_owner = true transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0) shape = SubResource("CapsuleShape3D_j6tb3") diff --git a/scripts/runner.gd b/scripts/runner.gd index a96101e..eb35cab 100644 --- a/scripts/runner.gd +++ b/scripts/runner.gd @@ -29,11 +29,14 @@ extends CharacterBody3D @export_group("Server variables") @export var server_position := Vector3.ZERO @export var server_rotation := Vector3.ZERO +@export var player := true +@export var dead := false @onready var _camera_pivot: Node3D = %CameraPivot @onready var _camera_platform: Node3D = %CameraPlatform @onready var _camera: Node3D = %Camera @onready var rotation_base: Node3D = %RotationBase +@onready var collider: Node3D = %Collider var camera_input_direction := Vector2.ZERO var last_direction := Vector3.FORWARD @@ -108,9 +111,9 @@ func _on_sync_delta_synchronized() -> void: position = server_position -func apply_input_velocity(delta: float, input_direction: Vector2, speed: float, acceleration: float) -> void: +func apply_input_velocity(delta: float, input_direction: Vector2, speed: float, acc: float) -> void: var target_velocity = Vector3(input_direction.x * speed, velocity.y, input_direction.y * speed) - velocity = velocity.move_toward(target_velocity, acceleration * delta) + Main.gravity_velocity + velocity = velocity.move_toward(target_velocity, acc * delta) + Main.gravity_velocity move_and_slide() if input_direction.length() >= 0.1: diff --git a/scripts/states/dead.gd b/scripts/states/dead.gd index fa8f890..67fa051 100644 --- a/scripts/states/dead.gd +++ b/scripts/states/dead.gd @@ -1,12 +1,17 @@ extends State func enter() -> void: + super() subject.rotation_base.global_rotation.z = PI / 2 + subject.collider.disabled = true func exit() -> void: + super() subject.rotation_base.global_rotation.z = 0 + subject.collider.disabled = false func process_physics(_delta: float) -> State: if not multiplayer.is_server(): return + if not subject.dead: return %Idle return diff --git a/scripts/states/fall.gd b/scripts/states/fall.gd index dfff4fd..031e155 100644 --- a/scripts/states/fall.gd +++ b/scripts/states/fall.gd @@ -6,6 +6,7 @@ func process_physics(delta: float) -> State: subject.apply_input_velocity(delta, Vector2.ZERO, 0.0, subject.air_deceleration) if not subject.is_on_floor(): return + if has_node("%Dead") and subject.dead: return %Dead if %Input.direction.length() < 0.05: return %Idle if has_node("%Walk") and %Input.walk: return %Walk return %Run diff --git a/scripts/states/idle.gd b/scripts/states/idle.gd index a8da571..fe9ec9d 100644 --- a/scripts/states/idle.gd +++ b/scripts/states/idle.gd @@ -3,12 +3,12 @@ extends State func process_physics(delta: float) -> State: if not Main.is_server_or_predicting(subject.player_id, subject.client_prediction): return + var input_direction = %Input.direction subject.apply_input_velocity(delta, Vector2.ZERO, 0.0, subject.acceleration) if not subject.is_on_floor(): return %Fall - if has_node("%Lunge") and %Input.primary_interact: return %Lunge # TODO: Lunge attacks currently do nothing so Chasers can't move when they click in - - var input_direction = %Input.direction + if has_node("%Dead") and subject.dead: return %Dead + if has_node("%Lunge") and %Input.primary_interact and (not has_node("%AttackCooldown") or %AttackCooldown.time_left == 0): return %Lunge if input_direction.length() < 0.05: return if has_node("%Walk") and %Input.walk: return %Walk return %Run diff --git a/scripts/states/lunge.gd b/scripts/states/lunge.gd index 96f5562..d1180ff 100644 --- a/scripts/states/lunge.gd +++ b/scripts/states/lunge.gd @@ -1,11 +1,42 @@ extends State +var hit = false +var players_in_area = {} + +func enter() -> void: + super() + if has_node("%Attack"): %Attack.visible = true + if has_node("%AttackTimer"): %AttackTimer.start() + if has_node("%AttackCooldown"): %AttackCooldown.start() + hit = false + +func exit() -> void: + super() + if has_node("%Attack"): %Attack.visible = false + func process_physics(delta: float) -> State: if not Main.is_server_or_predicting(subject.player_id, subject.client_prediction): return - var input_direction = %Input.direction if subject.is_on_floor() else Vector2.ZERO subject.apply_input_velocity(delta, input_direction, subject.run_speed, subject.acceleration if subject.is_on_floor() else subject.air_deceleration) + if players_in_area.size() > 0: + var hit_player = players_in_area[players_in_area.keys()[0]] + hit_player.dead = true + hit = true + + if has_node("%AttackTimer") and %AttackTimer.time_left > 0 and not hit: return + if not subject.is_on_floor(): return %Fall + if input_direction.length() < 0.05: return %Idle if has_node("%Walk") and %Input.walk: return %Walk - return + return %Run + +func _on_attack_body_entered(body: Node3D) -> void: + if body == subject: return + if not "player" in body or not body.player: return + if body.dead: return + + players_in_area[body.name] = body + +func _on_attack_body_exited(body: Node3D) -> void: + players_in_area.erase(body.name) diff --git a/scripts/states/run.gd b/scripts/states/run.gd index 7dc119c..0330062 100644 --- a/scripts/states/run.gd +++ b/scripts/states/run.gd @@ -4,10 +4,11 @@ func process_physics(delta: float) -> State: if not Main.is_server_or_predicting(subject.player_id, subject.client_prediction): return var input_direction = %Input.direction - if input_direction.length() < 0.05: return %Idle - subject.apply_input_velocity(delta, input_direction, subject.run_speed, subject.acceleration) - if has_node("%Walk") and %Input.walk: return %Walk if not subject.is_on_floor(): return %Fall + if has_node("%Dead") and subject.dead: return %Dead + if has_node("%Lunge") and %Input.primary_interact and (not has_node("%AttackCooldown") or %AttackCooldown.time_left == 0): return %Lunge + if input_direction.length() < 0.05: return %Idle + if has_node("%Walk") and %Input.walk: return %Walk return diff --git a/scripts/states/walk.gd b/scripts/states/walk.gd index 7f1272a..09d4704 100644 --- a/scripts/states/walk.gd +++ b/scripts/states/walk.gd @@ -4,10 +4,11 @@ func process_physics(delta: float) -> State: if not Main.is_server_or_predicting(subject.player_id, subject.client_prediction): return var input_direction = %Input.direction - if input_direction.length() < 0.05: return %Idle - subject.apply_input_velocity(delta, input_direction, subject.walk_speed, subject.acceleration) - if not %Input.walk: return %Run if not subject.is_on_floor(): return %Fall + if has_node("%Dead") and subject.dead: return %Dead + if has_node("%Lunge") and %Input.primary_interact and (not has_node("%AttackCooldown") or %AttackCooldown.time_left == 0): return %Lunge + if input_direction.length() < 0.05: return %Idle + if not %Input.walk: return %Run return