grounders-slowjam-2024/player/player.gd

90 lines
2.2 KiB
GDScript

# 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
# Using signals to maintain a list of currently colliding players because it simplifies
# Memory management substantially since nodes are not GC'd
# Connected nodes are the colliding nodes
signal is_bumping(src: Player)
var _is_grounded := false
signal is_grounded
func _ready():
if username == "":
username = "Player " + name
func is_on_ground() -> bool:
_is_grounded = false
is_grounded.emit(self)
var v := _is_grounded
_is_grounded = false
return v
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()
var _first_bumper: Player = null
func _bump_check(src: Player):
if src._first_bumper == null:
src._first_bumper = self
func get_first_bumper() -> Player:
_first_bumper = null
is_bumping.emit(self)
var player := _first_bumper
_first_bumper = null
return player
func _on_tag_detection_area_entered(area):
if not (area.get_parent() is Player):
return
var _other_player: Player = area.get_parent()
if is_bumping.is_connected(_bump_check):
return
is_bumping.connect(_bump_check)
func _on_tag_detection_area_exited(area):
if not (area.get_parent() is Player):
return
var _other_player: Player = area.get_parent()
if not is_bumping.is_connected(_bump_check):
return
is_bumping.disconnect(_bump_check)
func _on_tag_detection_body_entered(body):
if "grounded" in body:
if not is_grounded.is_connected(body.grounded):
is_grounded.connect(body.grounded)
func _on_tag_detection_body_exited(body):
if "grounded" in body:
if is_grounded.is_connected(body.grounded):
is_grounded.disconnect(body.grounded)