grounders-slowjam-2024/player/player.gd

101 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())
var player := get_first_bumper()
if player != null:
tag.emit(player)
2024-05-31 11:59:57 -06:00
# 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
2024-06-03 11:05:52 -06:00
signal check_bumping(src: Player)
2024-05-31 11:59:57 -06:00
var _is_grounded := false
2024-06-03 11:05:52 -06:00
signal check_grounded
2024-05-31 11:59:57 -06:00
2024-05-31 21:59:49 -06:00
func _ready():
if username == "":
username = "Player " + name
2024-05-31 11:59:57 -06:00
func is_on_ground() -> bool:
_is_grounded = false
2024-06-03 11:05:52 -06:00
check_grounded.emit(self)
2024-05-31 11:59:57 -06:00
var v := _is_grounded
_is_grounded = false
return v
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
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
2024-06-03 11:05:52 -06:00
check_bumping.emit(self)
2024-05-31 11:59:57 -06:00
var player := _first_bumper
_first_bumper = null
return player
func _on_tag_detection_area_entered(area):
if not (area.get_parent() is Player):
return
2024-05-31 16:47:56 -06:00
var _other_player: Player = area.get_parent()
2024-06-03 11:05:52 -06:00
if check_bumping.is_connected(_bump_check):
2024-05-31 11:59:57 -06:00
return
2024-06-03 11:05:52 -06:00
check_bumping.connect(_bump_check)
2024-05-31 11:59:57 -06:00
func _on_tag_detection_area_exited(area):
if not (area.get_parent() is Player):
return
2024-05-31 16:47:56 -06:00
var _other_player: Player = area.get_parent()
2024-06-03 11:05:52 -06:00
if not check_bumping.is_connected(_bump_check):
2024-05-31 11:59:57 -06:00
return
2024-06-03 11:05:52 -06:00
check_bumping.disconnect(_bump_check)
2024-05-31 11:59:57 -06:00
func _on_tag_detection_body_entered(body):
2024-05-31 21:59:49 -06:00
if "grounded" in body:
2024-06-03 11:05:52 -06:00
if not check_grounded.is_connected(body.grounded):
check_grounded.connect(body.grounded)
2024-05-31 11:59:57 -06:00
func _on_tag_detection_body_exited(body):
2024-05-31 21:59:49 -06:00
if "grounded" in body:
2024-06-03 11:05:52 -06:00
if check_grounded.is_connected(body.grounded):
check_grounded.disconnect(body.grounded)