grounders-slowjam-2024/player/player.gd

93 lines
2.5 KiB
GDScript3
Raw Normal View History

2024-05-25 10:10:24 -06:00
# Automatically Generated From Builtin CharacterBody Template
extends CharacterBody3D
2024-05-31 11:59:57 -06:00
class_name Player
2024-05-25 10:10:24 -06:00
@export var SPEED = 5.0
@export var JUMP_VELOCITY = 4.5
2024-05-31 21:59:49 -06:00
var username: String = ""
2024-05-25 10:10:24 -06:00
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
2024-06-03 11:05:52 -06:00
# Signals fire continously every frame they're true
signal tag(player: Player)
signal ground(v: bool)
func _process(_delta):
ground.emit(is_on_ground())
2024-06-04 22:08:24 -06:00
var player := get_first_tagging()
2024-06-03 11:05:52 -06:00
if player != null:
2024-06-04 22:08:24 -06:00
print("tag")
2024-06-03 11:05:52 -06:00
tag.emit(player)
2024-06-04 22:08:24 -06:00
# Signals being abused as weak hashsets
# Never emit
signal player_tagging_hashset
signal ground_touching_hashset
2024-05-31 11:59:57 -06:00
2024-05-31 21:59:49 -06:00
func _ready():
if username == "":
username = "Player " + name
2024-06-04 10:40:01 -06:00
$Sphere.set_instance_shader_parameter("color", Color.DARK_ORANGE)
2024-05-31 21:59:49 -06:00
2024-05-31 11:59:57 -06:00
func is_on_ground() -> bool:
2024-06-04 22:08:24 -06:00
return player_tagging_hashset.get_connections().size() > 0
2024-05-31 11:59:57 -06:00
2024-05-25 10:10:24 -06:00
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()
2024-05-31 11:59:57 -06:00
2024-06-04 22:08:24 -06:00
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()
2024-05-31 11:59:57 -06:00
func _on_tag_detection_area_entered(area):
2024-06-04 22:08:24 -06:00
if not (area.get_parent() is Player) or area.get_parent() == self:
2024-05-31 11:59:57 -06:00
return
2024-06-04 22:08:24 -06:00
var other_player: Player = area.get_parent()
if player_tagging_hashset.is_connected(other_player.id):
2024-05-31 11:59:57 -06:00
return
2024-06-04 22:08:24 -06:00
player_tagging_hashset.connect(other_player.id)
2024-05-31 11:59:57 -06:00
func _on_tag_detection_area_exited(area):
2024-06-04 22:08:24 -06:00
if not (area.get_parent() is Player) or area.get_parent() == self:
2024-05-31 11:59:57 -06:00
return
2024-06-04 22:08:24 -06:00
var other_player: Player = area.get_parent()
if not player_tagging_hashset.is_connected(other_player.id):
2024-05-31 11:59:57 -06:00
return
2024-06-04 22:08:24 -06:00
player_tagging_hashset.disconnect(other_player.id)
2024-05-31 11:59:57 -06:00
2024-06-04 22:08:24 -06:00
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)
2024-05-31 11:59:57 -06:00
2024-06-04 22:08:24 -06:00
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)