grounders-slowjam-2024/player/player.gd

87 lines
2.2 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
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-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
signal is_bumping(src: Player)
var _is_grounded := false
signal is_grounded
func is_on_ground() -> bool:
_is_grounded = false
is_grounded.emit()
var v := _is_grounded
_is_grounded = false
return v
2024-05-25 10:10:24 -06:00
func _physics_process(delta):
2024-05-31 16:47:56 -06:00
print(is_on_ground())
2024-05-25 10:10:24 -06:00
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
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
2024-05-31 16:47:56 -06:00
var _other_player: Player = area.get_parent()
2024-05-31 11:59:57 -06:00
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
2024-05-31 16:47:56 -06:00
var _other_player: Player = area.get_parent()
2024-05-31 11:59:57 -06:00
if not is_bumping.is_connected(_bump_check):
return
is_bumping.disconnect(_bump_check)
func _on_tag_detection_body_entered(body):
if body.is_in_group("ground"):
2024-05-31 16:47:56 -06:00
print("fuck")
2024-05-31 11:59:57 -06:00
if not is_grounded.is_connected(body.grounded):
is_grounded.connect(body.grounded)
func _on_tag_detection_body_exited(body):
if body.is_in_group("ground"):
if is_grounded.is_connected(body.grounded):
is_grounded.disconnect(body.grounded)