grounders-slowjam-2024/player/player.gd

102 lines
2.5 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
# 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_bumper()
if player != null:
tag.emit(player)
# 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 check_bumping(src: Player)
var _is_grounded := false
signal check_grounded
func _ready():
if username == "":
username = "Player " + name
$Sphere.set_instance_shader_parameter("color", Color.DARK_ORANGE)
func is_on_ground() -> bool:
_is_grounded = false
check_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
check_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 check_bumping.is_connected(_bump_check):
return
check_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 check_bumping.is_connected(_bump_check):
return
check_bumping.disconnect(_bump_check)
func _on_tag_detection_body_entered(body):
if "grounded" in body:
if not check_grounded.is_connected(body.grounded):
check_grounded.connect(body.grounded)
func _on_tag_detection_body_exited(body):
if "grounded" in body:
if check_grounded.is_connected(body.grounded):
check_grounded.disconnect(body.grounded)