clean code and fix tagging system

This commit is contained in:
Spencer Killen 2024-06-04 22:08:24 -06:00
parent aaee834a61
commit df09636af9
Signed by: sjkillen
GPG Key ID: 3AF3117BA6FBB75B
5 changed files with 58 additions and 71 deletions

View File

@ -1,8 +0,0 @@
extends Node3D
func grounded(src: Player):
src._is_grounded = true

View File

@ -9,7 +9,6 @@ enum GameState {
} }
var game_state: GameState = GameState.WAITING var game_state: GameState = GameState.WAITING
var safe_player: Player
var game_tagger: Player var game_tagger: Player
signal state_change(old: GameState, new: GameState) signal state_change(old: GameState, new: GameState)
@ -20,7 +19,7 @@ signal state_leave_tagger_ungrounded
signal state_enter_tagger_change(new_tagger: Player) signal state_enter_tagger_change(new_tagger: Player)
signal state_leave_tagger_change signal state_leave_tagger_change
const MIN_PLAYERS := 3 const MIN_PLAYERS := 2
const global_msg_1 := "Waiting for at least {min_players} players to join" const global_msg_1 := "Waiting for at least {min_players} players to join"
const global_msg_2 := "Game is starting soon, {tagger_username} is it!" const global_msg_2 := "Game is starting soon, {tagger_username} is it!"
const global_msg_3 := "{tagger_username} is it! Run!" const global_msg_3 := "{tagger_username} is it! Run!"
@ -53,7 +52,6 @@ func not_enough_players() -> bool:
return false return false
func change_to_random_tagger(): func change_to_random_tagger():
safe_player = null
game_tagger = %Players.get_children().pick_random() game_tagger = %Players.get_children().pick_random()
_change_state(GameState.TAGGER_CHANGE) _change_state(GameState.TAGGER_CHANGE)
@ -68,7 +66,6 @@ func tagger_left_game():
"tagger_username": tagger_username})) "tagger_username": tagger_username}))
func set_tagger(who: Player): func set_tagger(who: Player):
safe_player = game_tagger
game_tagger = who game_tagger = who
_change_state(GameState.TAGGER_CHANGE) _change_state(GameState.TAGGER_CHANGE)
@ -76,19 +73,13 @@ func _change_state(new_state: GameState):
var id := -1 var id := -1
if game_tagger != null: if game_tagger != null:
id = game_tagger.get_multiplayer_authority() id = game_tagger.get_multiplayer_authority()
var safe_player_id := -1 _change_state_full.rpc(new_state, id)
if safe_player != null:
safe_player_id = safe_player.get_multiplayer_authority()
_change_state_full.rpc(new_state, id, safe_player_id)
@rpc("any_peer", "reliable", "call_local") @rpc("any_peer", "reliable", "call_local")
func _change_state_full(new_state: GameState, new_tagger_id: int, new_safe_player_id: int): func _change_state_full(new_state: GameState, new_tagger_id: int):
game_tagger = null game_tagger = null
safe_player = null
if new_tagger_id != -1: if new_tagger_id != -1:
game_tagger = find_player_by_id(new_tagger_id) game_tagger = find_player_by_id(new_tagger_id)
if new_safe_player_id != -1:
safe_player = find_player_by_id(new_safe_player_id)
if game_state == new_state: if game_state == new_state:
return return
if new_state == GameState.WAITING: if new_state == GameState.WAITING:
@ -114,7 +105,7 @@ func _change_state_full(new_state: GameState, new_tagger_id: int, new_safe_playe
func tagging_possible(_tagger: Player, tagee: Player) -> bool: func tagging_possible(_tagger: Player, tagee: Player) -> bool:
if game_state != GameState.TAGGER_GROUNDED and game_state != GameState.TAGGER_UNGROUNDED: if game_state != GameState.TAGGER_GROUNDED and game_state != GameState.TAGGER_UNGROUNDED:
return false return false
return safe_player != tagee return true
func _ready(): func _ready():
# Hacky way to start level without going though multiplayer screens # Hacky way to start level without going though multiplayer screens
@ -126,6 +117,12 @@ func player_ground(v: bool, player: Player):
if game_tagger == player: if game_tagger == player:
set_tagger_grounded(v) set_tagger_grounded(v)
func teleport(who: Player):
if not who.is_multiplayer_authority():
push_error("Don't have the authority to teleport")
return
who.position = Vector3.ZERO
func player_tag(who: Player, player: Player): func player_tag(who: Player, player: Player):
if not tagging_possible(player, who) or game_tagger != player: if not tagging_possible(player, who) or game_tagger != player:
return return
@ -134,6 +131,7 @@ func player_tag(who: Player, player: Player):
push_global_message.rpc(global_msg_4.format({ push_global_message.rpc(global_msg_4.format({
"old_tagger_username": old_tagger_username, "old_tagger_username": old_tagger_username,
"tagger_username": tagger_username})) "tagger_username": tagger_username}))
teleport(player)
set_tagger(who) set_tagger(who)
func start_game(): func start_game():

View File

@ -1,4 +1,4 @@
[gd_scene load_steps=14 format=3 uid="uid://b00brfkibo5cj"] [gd_scene load_steps=13 format=3 uid="uid://b00brfkibo5cj"]
[ext_resource type="PackedScene" uid="uid://bq654gwim6col" path="res://level/level.glb" id="1_s37in"] [ext_resource type="PackedScene" uid="uid://bq654gwim6col" path="res://level/level.glb" id="1_s37in"]
[ext_resource type="Environment" uid="uid://covjrwmk4rplw" path="res://level/world_environment.tres" id="2_ptkl6"] [ext_resource type="Environment" uid="uid://covjrwmk4rplw" path="res://level/world_environment.tres" id="2_ptkl6"]
@ -6,7 +6,6 @@
[ext_resource type="Script" path="res://addons/smoother/smoother.gd" id="5_2tyle"] [ext_resource type="Script" path="res://addons/smoother/smoother.gd" id="5_2tyle"]
[ext_resource type="Script" path="res://level/PlayerSpawner.gd" id="6_7ww0m"] [ext_resource type="Script" path="res://level/PlayerSpawner.gd" id="6_7ww0m"]
[ext_resource type="MeshLibrary" uid="uid://cgh6y5j8wgi36" path="res://level/mesh_library/level_mesh_library.glb" id="6_d34iv"] [ext_resource type="MeshLibrary" uid="uid://cgh6y5j8wgi36" path="res://level/mesh_library/level_mesh_library.glb" id="6_d34iv"]
[ext_resource type="Script" path="res://level/Ground.gd" id="8_yu1ir"]
[sub_resource type="WorldBoundaryShape3D" id="WorldBoundaryShape3D_ujmev"] [sub_resource type="WorldBoundaryShape3D" id="WorldBoundaryShape3D_ujmev"]
margin = 2.067 margin = 2.067
@ -18,6 +17,7 @@ size = Vector3(2, 0.1, 2)
size = Vector2(100, 100) size = Vector2(100, 100)
[sub_resource type="BoxShape3D" id="BoxShape3D_m3lo5"] [sub_resource type="BoxShape3D" id="BoxShape3D_m3lo5"]
size = Vector3(100, 100, 1)
[sub_resource type="SceneReplicationConfig" id="SceneReplicationConfig_285vp"] [sub_resource type="SceneReplicationConfig" id="SceneReplicationConfig_285vp"]
@ -51,7 +51,7 @@ visible = false
[node name="Players" type="Node3D" parent="." index="3"] [node name="Players" type="Node3D" parent="." index="3"]
unique_name_in_owner = true unique_name_in_owner = true
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 11.4823, 12, 16.4239) transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 4.78702, 68.686, -4.61699)
[node name="PlayerSpawner" type="MultiplayerSpawner" parent="." index="4"] [node name="PlayerSpawner" type="MultiplayerSpawner" parent="." index="4"]
unique_name_in_owner = true unique_name_in_owner = true
@ -81,7 +81,7 @@ data = {
} }
metadata/_editor_floor_ = Vector3(0, 2, 0) metadata/_editor_floor_ = Vector3(0, 2, 0)
[node name="MeshInstance3D" type="MeshInstance3D" parent="." index="8"] [node name="MeshInstance3D" type="MeshInstance3D" parent="." index="8" groups=["ground"]]
transform = Transform3D(1, 0, 0, 0, -4.37114e-08, 1, 0, -1, -4.37114e-08, 0.001, 0, 0) transform = Transform3D(1, 0, 0, 0, -4.37114e-08, 1, 0, -1, -4.37114e-08, 0.001, 0, 0)
mesh = SubResource("QuadMesh_8lqeb") mesh = SubResource("QuadMesh_8lqeb")
skeleton = NodePath("../Cube") skeleton = NodePath("../Cube")
@ -89,7 +89,7 @@ skeleton = NodePath("../Cube")
[node name="StaticBody3D" type="StaticBody3D" parent="MeshInstance3D" index="0"] [node name="StaticBody3D" type="StaticBody3D" parent="MeshInstance3D" index="0"]
[node name="CollisionShape3D" type="CollisionShape3D" parent="MeshInstance3D/StaticBody3D" index="0"] [node name="CollisionShape3D" type="CollisionShape3D" parent="MeshInstance3D/StaticBody3D" index="0"]
transform = Transform3D(100, 0, 0, 0, 100, 0, 0, 0, 1, 0, 0, -0.5) transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -0.5)
shape = SubResource("BoxShape3D_m3lo5") shape = SubResource("BoxShape3D_m3lo5")
[node name="MultiplayerSynchronizer" type="MultiplayerSynchronizer" parent="." index="9"] [node name="MultiplayerSynchronizer" type="MultiplayerSynchronizer" parent="." index="9"]
@ -107,12 +107,11 @@ layout_mode = 0
offset_right = 40.0 offset_right = 40.0
offset_bottom = 23.0 offset_bottom = 23.0
[node name="Ground" type="Area3D" parent="." index="11"] [node name="Ground" type="Area3D" parent="." index="11" groups=["ground"]]
transform = Transform3D(9, 0, 0, 0, 9, 0, 0, 0, 9, 4.85232, 7.93442, -4.42363) transform = Transform3D(9, 0, 0, 0, 9, 0, 0, 0, 9, 4.85232, 7.93442, -4.42363)
[node name="CollisionShape3D" type="CollisionShape3D" parent="Ground" index="0"] [node name="CollisionShape3D" type="CollisionShape3D" parent="Ground" index="0"]
shape = SubResource("BoxShape3D_0y3ka") shape = SubResource("BoxShape3D_0y3ka")
script = ExtResource("8_yu1ir")
[connection signal="despawned" from="PlayerSpawner" to="." method="client_remove_player"] [connection signal="despawned" from="PlayerSpawner" to="." method="client_remove_player"]
[connection signal="spawned" from="PlayerSpawner" to="." method="client_add_player"] [connection signal="spawned" from="PlayerSpawner" to="." method="client_add_player"]

View File

@ -19,16 +19,15 @@ signal ground(v: bool)
func _process(_delta): func _process(_delta):
ground.emit(is_on_ground()) ground.emit(is_on_ground())
var player := get_first_bumper() var player := get_first_tagging()
if player != null: if player != null:
print("tag")
tag.emit(player) tag.emit(player)
# Using signals to maintain a list of currently colliding players because it simplifies # Signals being abused as weak hashsets
# Memory management substantially since nodes are not GC'd # Never emit
# Connected nodes are the colliding nodes signal player_tagging_hashset
signal check_bumping(src: Player) signal ground_touching_hashset
var _is_grounded := false
signal check_grounded
func _ready(): func _ready():
if username == "": if username == "":
@ -36,11 +35,7 @@ func _ready():
$Sphere.set_instance_shader_parameter("color", Color.DARK_ORANGE) $Sphere.set_instance_shader_parameter("color", Color.DARK_ORANGE)
func is_on_ground() -> bool: func is_on_ground() -> bool:
_is_grounded = false return player_tagging_hashset.get_connections().size() > 0
check_grounded.emit(self)
var v := _is_grounded
_is_grounded = false
return v
func _physics_process(delta): func _physics_process(delta):
@ -59,43 +54,39 @@ func _physics_process(delta):
move_and_slide() move_and_slide()
var _first_bumper: Player = null func id():
return self
func _bump_check(src: Player): func get_first_tagging() -> Player:
if src._first_bumper == null: var connections := player_tagging_hashset.get_connections()
src._first_bumper = self if connections.size() == 0:
return null
func get_first_bumper() -> Player: return connections[0]["callable"].call()
_first_bumper = null
check_bumping.emit(self)
var player := _first_bumper
_first_bumper = null
return player
func _on_tag_detection_area_entered(area): func _on_tag_detection_area_entered(area):
if not (area.get_parent() is Player): if not (area.get_parent() is Player) or area.get_parent() == self:
return return
var _other_player: Player = area.get_parent() var other_player: Player = area.get_parent()
if check_bumping.is_connected(_bump_check): if player_tagging_hashset.is_connected(other_player.id):
return return
check_bumping.connect(_bump_check) player_tagging_hashset.connect(other_player.id)
func _on_tag_detection_area_exited(area): func _on_tag_detection_area_exited(area):
if not (area.get_parent() is Player): if not (area.get_parent() is Player) or area.get_parent() == self:
return return
var _other_player: Player = area.get_parent() var other_player: Player = area.get_parent()
if not check_bumping.is_connected(_bump_check): if not player_tagging_hashset.is_connected(other_player.id):
return return
check_bumping.disconnect(_bump_check) player_tagging_hashset.disconnect(other_player.id)
func _on_tag_detection_body_entered(body): func _on_ground_detection_node_entered(node: Node):
if "grounded" in body: if node.is_in_group("ground"):
if not check_grounded.is_connected(body.grounded): if not ground_touching_hashset.is_connected(node.get_tree):
check_grounded.connect(body.grounded) ground_touching_hashset.connect(node.get_tree)
func _on_tag_detection_body_exited(body): func _on_ground_detection_node_exited(node: Node):
if "grounded" in body: if node.is_in_group("ground"):
if check_grounded.is_connected(body.grounded): if ground_touching_hashset.is_connected(node.get_tree):
check_grounded.disconnect(body.grounded) ground_touching_hashset.disconnect(node.get_tree)

View File

@ -32,7 +32,14 @@ collision_mask = 8388609
[node name="CollisionShape3D" type="CollisionShape3D" parent="TagDetection" index="0"] [node name="CollisionShape3D" type="CollisionShape3D" parent="TagDetection" index="0"]
shape = SubResource("SphereShape3D_ylark") shape = SubResource("SphereShape3D_ylark")
[node name="GroundDetection" type="Area3D" parent="." index="4"]
[node name="CollisionShape3D" type="CollisionShape3D" parent="GroundDetection" index="0"]
shape = SubResource("SphereShape3D_ylark")
[connection signal="area_entered" from="TagDetection" to="." method="_on_tag_detection_area_entered"] [connection signal="area_entered" from="TagDetection" to="." method="_on_tag_detection_area_entered"]
[connection signal="area_exited" from="TagDetection" to="." method="_on_tag_detection_area_exited"] [connection signal="area_exited" from="TagDetection" to="." method="_on_tag_detection_area_exited"]
[connection signal="body_entered" from="TagDetection" to="." method="_on_tag_detection_body_entered"] [connection signal="area_entered" from="GroundDetection" to="." method="_on_ground_detection_node_entered"]
[connection signal="body_exited" from="TagDetection" to="." method="_on_tag_detection_body_exited"] [connection signal="area_exited" from="GroundDetection" to="." method="_on_ground_detection_node_exited"]
[connection signal="body_entered" from="GroundDetection" to="." method="_on_ground_detection_node_entered"]
[connection signal="body_exited" from="GroundDetection" to="." method="_on_ground_detection_node_exited"]