grunk/src/player/player.gd

82 lines
2.1 KiB
GDScript3
Raw Normal View History

2025-02-28 15:20:52 -07:00
class_name Player extends CharacterBody3D
const RUN_SPEED := 80.0
const SPRINT_SPEED := 160.0
2025-03-01 14:40:02 -07:00
const AIR_SPEED := 10.0
2025-02-28 15:20:52 -07:00
const JUMP_FORCE := 4.5
const GROUND_FRICTION := 0.3
const AIR_FRICTION := 0.03
2025-03-01 19:02:13 -07:00
const SPRAY_SCALE := 16
2025-03-01 14:40:02 -07:00
2025-02-28 15:20:52 -07:00
var gravity: Vector3 = (
ProjectSettings.get_setting("physics/3d/default_gravity")
* ProjectSettings.get_setting("physics/3d/default_gravity_vector")
)
@onready var camera_pivot: Node3D = %CameraPivot
2025-03-01 16:59:39 -07:00
@onready var spray_muzzle: Marker3D = %SprayMuzzle
2025-03-01 19:02:13 -07:00
@onready var raycast: RayCast3D = %RayCast3D
2025-02-28 15:20:52 -07:00
2025-03-01 14:40:02 -07:00
@onready var spray_effect: MeshInstance3D = %SprayEffect
2025-02-28 15:20:52 -07:00
func get_speed() -> float:
if is_on_floor():
if Input.is_action_pressed("sprint"):
return SPRINT_SPEED
return RUN_SPEED
return AIR_SPEED
func get_friction() -> float:
if is_on_floor():
return GROUND_FRICTION
return AIR_FRICTION
func _scale_spray_point(point: Vector3) -> float:
return point.distance_to(spray_muzzle.global_position) * SPRAY_SCALE
2025-03-01 14:40:02 -07:00
func fire_spray() -> void:
2025-03-01 19:02:13 -07:00
if raycast.is_colliding():
var collider := raycast.get_collider()
if collider is GunkBody:
var point := raycast.get_collision_point()
var point_scale := point.distance_to(spray_muzzle.global_position) * SPRAY_SCALE
(collider as GunkBody).paint_continuous(
point, raycast.get_collision_normal(), point_scale
)
2025-03-01 14:40:02 -07:00
2025-02-28 15:20:52 -07:00
func _physics_process(delta: float) -> void:
2025-03-01 14:40:02 -07:00
if Input.is_action_pressed("fire"):
fire_spray()
spray_effect.visible = true
else:
spray_effect.visible = false
2025-02-28 15:20:52 -07:00
# Gravity
if not is_on_floor():
velocity += gravity * delta
# Input movement
var input_dir := Input.get_vector("move_left", "move_right", "move_forward", "move_back")
var direction := (
(camera_pivot.global_basis * Vector3(input_dir.x, 0, input_dir.y) * Vector3(1, 0, 1))
. normalized()
)
var movement := direction * get_speed() * delta
velocity.x += movement.x
velocity.z += movement.z
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = JUMP_FORCE
# Friction
var friction := get_friction()
velocity.x = lerpf(velocity.x, 0, friction)
velocity.z = lerpf(velocity.z, 0, friction)
move_and_slide()