SlimeoosOdyssey/godot/Player.gd

153 lines
4.2 KiB
GDScript3
Raw Normal View History

2023-08-18 20:59:38 -06:00
extends CharacterBody3D
2023-08-20 12:13:54 -06:00
const SPEED = 0.4
2023-08-18 20:59:38 -06:00
const JUMP_VELOCITY = 4.5
2023-08-18 23:15:14 -06:00
@export var horiz_sens = 0.25
@export var vert_sens = 0.25
2023-08-18 20:59:38 -06:00
2023-08-20 13:24:04 -06:00
var LOWER_CAMERA_LIMIT = -60
var UPPER_CAMERA_LIMIT = -11
2023-08-19 15:42:56 -06:00
2023-08-19 20:18:07 -06:00
var target: WeakRef = weakref(null)
2023-08-19 14:49:36 -06:00
var holding: WeakRef = weakref(null)
2023-08-18 20:59:38 -06:00
# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
2023-08-20 14:59:32 -06:00
@onready var pause_menu = $PauseMenu
2023-08-18 21:27:39 -06:00
func _ready():
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
2023-08-20 13:24:04 -06:00
$CameraMount.global_rotation.x = clampf($CameraMount.global_rotation.x, deg_to_rad(LOWER_CAMERA_LIMIT), deg_to_rad(UPPER_CAMERA_LIMIT))
2023-08-18 23:15:14 -06:00
2023-08-19 11:58:20 -06:00
func _process(_delta):
2023-08-18 23:15:14 -06:00
# Brings your mouse out of the window if you press escape. Add pause screen function here?
2023-08-20 14:59:32 -06:00
if Input.is_action_just_pressed("Pause"):
$PauseMenu.pause()
2023-08-18 23:15:14 -06:00
2023-08-19 20:18:07 -06:00
var obj = holding.get_ref()
if obj:
obj.global_transform.origin = $grape_man.get_pickup_bone_location()
2023-08-20 12:31:24 -06:00
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)
2023-08-20 13:24:04 -06:00
$CameraMount.global_rotation.x = clampf($CameraMount.global_rotation.x, deg_to_rad(LOWER_CAMERA_LIMIT), deg_to_rad(UPPER_CAMERA_LIMIT))
2023-08-20 12:31:24 -06:00
2023-08-18 21:27:39 -06:00
func _input(event):
2023-08-20 14:59:32 -06:00
2023-08-18 21:27:39 -06:00
# Handle the rotation of the camera using mouse movement.
if event is InputEventMouseMotion:
2023-08-18 23:15:14 -06:00
rotate_y(deg_to_rad(-event.relative.x * horiz_sens))
2023-08-19 12:27:23 -06:00
$CameraMount.rotate_x(deg_to_rad(-event.relative.y * vert_sens))
2023-08-19 20:42:02 -06:00
var roty = deg_to_rad(event.relative.x * horiz_sens)
$grape_man.rotate_y(roty)
2023-08-20 13:24:04 -06:00
$CameraMount.global_rotation.x = clampf($CameraMount.global_rotation.x, deg_to_rad(LOWER_CAMERA_LIMIT), deg_to_rad(UPPER_CAMERA_LIMIT))
2023-08-20 12:31:24 -06:00
2023-08-19 15:42:56 -06:00
if Input.is_action_just_pressed("ui_accept"):
var obj = holding.get_ref()
if obj:
2023-08-19 20:18:07 -06:00
drop()
2023-08-19 18:18:34 -06:00
$grape_man/drop.play()
2023-08-19 15:42:56 -06:00
else:
2023-08-19 18:58:08 -06:00
$grape_man/pickup.play()
2023-08-19 15:42:56 -06:00
$grape_man.pickup()
2023-08-20 11:48:25 -06:00
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()
2023-08-19 14:49:36 -06:00
2023-08-18 20:59:38 -06:00
func _physics_process(delta):
2023-08-19 15:42:56 -06:00
if $grape_man.pickup_animation_in_progress:
return
2023-08-18 20:59:38 -06:00
# 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")
2023-08-19 15:42:56 -06:00
2023-08-18 20:59:38 -06:00
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
2023-08-19 12:27:23 -06:00
$grape_man.look_at(Vector3(
$grape_man.global_position.x - velocity.x,
$grape_man.global_position.y,
$grape_man.global_position.z - velocity.z,
))
2023-08-19 15:42:56 -06:00
if holding.get_ref():
$grape_man.walk_hold()
else:
$grape_man.walk_empty()
2023-08-18 20:59:38 -06:00
else:
2023-08-19 15:42:56 -06:00
if holding.get_ref():
$grape_man.idle_hold()
else:
$grape_man.idle_empty()
2023-08-18 20:59:38 -06:00
velocity.x = move_toward(velocity.x, 0, SPEED)
velocity.z = move_toward(velocity.z, 0, SPEED)
move_and_slide()
2023-08-19 15:42:56 -06:00
2023-08-19 20:18:07 -06:00
func drop():
2023-08-20 15:00:54 -06:00
var obj: Node3D = holding.get_ref()
2023-08-19 20:18:07 -06:00
if not obj:
print("dropped, but wasn't holding anything?")
return
remove_collision_exception_with(obj)
2023-08-20 15:00:54 -06:00
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))
2023-08-19 20:18:07 -06:00
obj.set_process(true)
holding = weakref(null)
2023-08-19 15:42:56 -06:00
func _on_grape_man_anim_check_grab(_position):
2023-08-19 20:18:07 -06:00
var obj = target.get_ref()
if not obj:
2023-08-20 00:08:31 -06:00
$grape_man/sad.play()
2023-08-19 20:18:07 -06:00
return
2023-08-20 00:08:31 -06:00
$grape_man/chuckle.play()
2023-08-19 20:18:07 -06:00
holding = target
target = weakref(null)
add_collision_exception_with(obj)
obj.set_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):
2023-08-19 20:56:23 -06:00
if not target.get_ref() or holding.get_ref() or "is_holdable" not in body:
2023-08-19 20:18:07 -06:00
return
target = weakref(null)