SlimeoosOdyssey/godot/Player.gd

161 lines
4.4 KiB
GDScript

extends CharacterBody3D
const SPEED = 0.4
const JUMP_VELOCITY = 4.5
@export var horiz_sens = 0.25
@export var vert_sens = 0.25
var LOWER_CAMERA_LIMIT = -60
var UPPER_CAMERA_LIMIT = -11
var target: WeakRef = weakref(null)
var holding: WeakRef = weakref(null)
# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
@onready var pause_menu = $PauseMenu
func _ready():
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
$CameraMount.global_rotation.x = clampf($CameraMount.global_rotation.x, deg_to_rad(LOWER_CAMERA_LIMIT), deg_to_rad(UPPER_CAMERA_LIMIT))
func _process(_delta):
var obj = holding.get_ref()
if obj:
obj.global_transform.origin = $grape_man.get_pickup_bone_location()
var event = Input.get_vector("camera_left", "camera_right", "camera_up", "camera_down") * 4
rotate_y(deg_to_rad(-event.x * horiz_sens))
$CameraMount.rotate_x(deg_to_rad(-event.y * vert_sens))
var roty = deg_to_rad(event.x * horiz_sens)
$grape_man.rotate_y(roty)
$CameraMount.global_rotation.x = clampf($CameraMount.global_rotation.x, deg_to_rad(LOWER_CAMERA_LIMIT), deg_to_rad(UPPER_CAMERA_LIMIT))
func _input(event):
# Handle the rotation of the camera using mouse movement.
if event is InputEventMouseMotion:
rotate_y(deg_to_rad(-event.relative.x * horiz_sens))
$CameraMount.rotate_x(deg_to_rad(-event.relative.y * vert_sens))
var roty = deg_to_rad(event.relative.x * horiz_sens)
$grape_man.rotate_y(roty)
$CameraMount.global_rotation.x = clampf($CameraMount.global_rotation.x, deg_to_rad(LOWER_CAMERA_LIMIT), deg_to_rad(UPPER_CAMERA_LIMIT))
if Input.is_action_just_pressed("ui_accept"):
var obj = holding.get_ref()
if obj:
drop()
$grape_man/drop.play()
else:
$grape_man/pickup.play()
$grape_man.pickup()
if Input.is_action_just_pressed("Soundboard"):
var sound_list = (["res://Sounds/jingle.ogg",
"res://Sounds/yawn.ogg",
"res://Sounds/woohoo.ogg",
"res://Sounds/slimeu.ogg"])
var sound = sound_list[randi() % sound_list.size()]
$grape_man/soundboard.stream = load(sound)
$grape_man/soundboard.play()
func _physics_process(delta):
if $grape_man.pickup_animation_in_progress:
return
# Add the gravity.
if not is_on_floor():
velocity.y -= gravity * delta
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var input_dir = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
if direction:
velocity.x = direction.x * SPEED
velocity.z = direction.z * SPEED
$grape_man.look_at(Vector3(
$grape_man.global_position.x - velocity.x,
$grape_man.global_position.y,
$grape_man.global_position.z - velocity.z,
))
if holding.get_ref():
$grape_man.walk_hold()
else:
$grape_man.walk_empty()
else:
if holding.get_ref():
$grape_man.idle_hold()
else:
$grape_man.idle_empty()
velocity.x = move_toward(velocity.x, 0, SPEED)
velocity.z = move_toward(velocity.z, 0, SPEED)
move_and_slide()
var push = 0.01
# after calling move_and_slide()
for index in get_slide_collision_count():
var c = get_slide_collision(index)
var collision = c.get_collider()
if "is_pushable" not in collision:
continue
collision.apply_central_impulse(-c.get_normal() * push)
func drop():
var obj: Node3D = holding.get_ref()
if not obj:
print("dropped, but wasn't holding anything?")
return
remove_collision_exception_with(obj)
obj.look_at(Vector3(
$grape_man.global_position.x,
obj.global_position.y,
$grape_man.global_position.z,
))
obj.rotate_y(deg_to_rad(-90))
obj.translate_object_local(Vector3(0.125, 0.0, 0.0))
obj.set_process(true)
obj.set_physics_process(true)
holding = weakref(null)
func _on_grape_man_anim_check_grab(_position):
var obj = target.get_ref()
if not obj:
$grape_man/sad.play()
return
$grape_man/chuckle.play()
holding = target
target = weakref(null)
add_collision_exception_with(obj)
obj.set_process(false)
obj.set_physics_process(false)
func _on_holster_enter(body):
if target.get_ref() or holding.get_ref() or "is_holdable" not in body:
return
target = weakref(body)
func _on_holster_exit(body):
if not target.get_ref() or holding.get_ref() or "is_holdable" not in body:
return
target = weakref(null)