# Automatically Generated From Builtin CharacterBody Template extends CharacterBody3D class_name Player @export var SPEED = 5.0 @export var JUMP_VELOCITY = 4.5 var username: String = "" var gravity = ProjectSettings.get_setting("physics/3d/default_gravity") # Set by camera child, switch to signals if becomes too spaghetti var movement_dir: Vector2 = Vector2.ZERO # Signals fire continously every frame they're true signal tag(player: Player) signal ground(v: bool) func _process(_delta): ground.emit(is_on_ground()) var player := get_first_tagging() if player != null: print("tag") tag.emit(player) # Signals being abused as weak hashsets # Never emit signal player_tagging_hashset signal ground_touching_hashset func _ready(): if username == "": username = "Player " + name $Sphere.set_instance_shader_parameter("color", Color.DARK_ORANGE) func is_on_ground() -> bool: return player_tagging_hashset.get_connections().size() > 0 func _physics_process(delta): if not is_on_floor(): velocity.y -= gravity * delta if Input.is_action_just_pressed("ui_accept") and is_on_floor(): velocity.y = JUMP_VELOCITY if movement_dir: velocity.x = movement_dir.x * SPEED velocity.z = movement_dir.y * SPEED else: velocity.x = move_toward(velocity.x, 0, SPEED) velocity.z = move_toward(velocity.z, 0, SPEED) move_and_slide() func id(): return self func get_first_tagging() -> Player: var connections := player_tagging_hashset.get_connections() if connections.size() == 0: return null return connections[0]["callable"].call() func _on_tag_detection_area_entered(area): if not (area.get_parent() is Player) or area.get_parent() == self: return var other_player: Player = area.get_parent() if player_tagging_hashset.is_connected(other_player.id): return player_tagging_hashset.connect(other_player.id) func _on_tag_detection_area_exited(area): if not (area.get_parent() is Player) or area.get_parent() == self: return var other_player: Player = area.get_parent() if not player_tagging_hashset.is_connected(other_player.id): return player_tagging_hashset.disconnect(other_player.id) func _on_ground_detection_node_entered(node: Node): if node.is_in_group("ground"): if not ground_touching_hashset.is_connected(node.get_tree): ground_touching_hashset.connect(node.get_tree) func _on_ground_detection_node_exited(node: Node): if node.is_in_group("ground"): if ground_touching_hashset.is_connected(node.get_tree): ground_touching_hashset.disconnect(node.get_tree)