First_Person_Slapper/Player.gd

164 lines
4.0 KiB
GDScript3
Raw Normal View History

extends KinematicBody
2022-10-11 21:57:37 -04:00
export var speed = 20
2022-10-11 10:08:50 -04:00
2022-10-11 09:26:29 -04:00
export var h_acceleration = 6
export var air_acceleration = 1
export var normal_acceleration = 6
2022-10-12 21:16:54 -04:00
export var gravity = 50
export var jump = 15
2022-10-11 14:57:56 -04:00
export var slapkill = 0.8
2022-10-11 12:48:08 -04:00
var slap = 0.0
2022-10-11 13:08:21 -04:00
var chargeSlap = false
2022-10-11 19:11:46 -04:00
var isSlap = false
var can_slap = true
2022-10-11 16:32:48 -04:00
var isStep = false
var stepAcc = 0
2022-10-11 09:26:29 -04:00
export var mouse_sensitivity = 0.03
var bottom_boudaries = -20
2022-10-11 09:26:29 -04:00
var full_contact = false
var direction = Vector3()
var h_velocity = Vector3()
var movement = Vector3()
var gravity_vec = Vector3()
onready var head = $Head
2022-10-11 19:11:46 -04:00
onready var camera = $Head/Camera
onready var point_to = $Head/Camera/PointTo
2022-10-11 09:26:29 -04:00
onready var ground_check = $GroundCheck
onready var slap_gauge = $HUD/PlayerLayer/ActualHUD/SlapGauge
2022-10-11 14:58:23 -04:00
onready var slap_animator = $HandMovement
onready var Menu = $HUD/MenuLayer/Start
2022-10-11 19:11:46 -04:00
onready var hitbox = $Head/Camera/HitBox
onready var slap_cooldown = $SlapCooldown
2022-10-11 19:03:11 -04:00
onready var AudioCharge = $AudioCharge
2022-10-12 09:10:44 -04:00
onready var Whoosh = $AudioWhoosh
2022-10-11 16:32:48 -04:00
onready var Steps = [ $Step1, $Step2, $Step3, $Step4 ]
var rng = RandomNumberGenerator.new()
func _ready():
2022-10-11 16:32:48 -04:00
rng.randomize()
2022-10-11 15:07:07 -04:00
slap_animator.current_animation = "basic_slap"
2022-10-12 08:45:13 -04:00
slap_animator.playback_speed = 30
2022-10-11 15:07:07 -04:00
func _input(event):
if event is InputEventMouseMotion:
rotate_y(deg2rad(-event.relative.x * mouse_sensitivity))
head.rotate_x(deg2rad(-event.relative.y * mouse_sensitivity))
head.rotation.x = clamp(head.rotation.x, deg2rad(-89), deg2rad(89))
2022-10-11 09:26:29 -04:00
func _physics_process(delta):
2022-10-11 14:32:39 -04:00
if Input.is_action_just_pressed("escape"):
2022-10-11 19:03:11 -04:00
Menu.openMenu();
2022-10-11 14:32:39 -04:00
if global_transform.origin.y < bottom_boudaries:
global_transform.origin.x = 0
global_transform.origin.y = 10
global_transform.origin.z = 0
2022-10-11 09:26:29 -04:00
direction = Vector3()
full_contact = ground_check.is_colliding()
if not is_on_floor():
gravity_vec += Vector3.DOWN * gravity * delta
h_acceleration = air_acceleration
2022-10-11 09:26:29 -04:00
elif is_on_floor() and full_contact:
gravity_vec = -get_floor_normal() * gravity
h_acceleration = normal_acceleration
2022-10-11 16:32:48 -04:00
isStep = true
2022-10-11 09:26:29 -04:00
else:
gravity_vec = -get_floor_normal()
h_acceleration = normal_acceleration
2022-10-11 16:32:48 -04:00
2022-10-11 09:26:29 -04:00
if Input.is_action_just_pressed("jump") and (is_on_floor() or ground_check.is_colliding()):
gravity_vec = Vector3.UP * jump
2022-10-11 19:11:46 -04:00
if Input.is_action_just_pressed("crouch"):
head.translate(Vector3.DOWN)
2022-10-12 22:54:52 -04:00
speed = 10
2022-10-11 19:11:46 -04:00
if Input.is_action_just_released("crouch"):
head.translate(Vector3.UP)
2022-10-12 22:54:52 -04:00
speed = 20
2022-10-11 09:26:29 -04:00
if Input.is_action_pressed("move_forward"):
direction -= transform.basis.z
elif Input.is_action_pressed("move_backward"):
direction += transform.basis.z
if Input.is_action_pressed("move_left"):
direction -= transform.basis.x
elif Input.is_action_pressed("move_right"):
direction += transform.basis.x
2022-10-11 12:48:08 -04:00
2022-10-11 16:32:48 -04:00
2022-10-11 13:08:21 -04:00
# Slappening
if Input.is_action_pressed("slap"):
2022-10-11 19:03:11 -04:00
if !chargeSlap:
AudioCharge.play();
if slap < 0.01:
chargeSlap = true;
2022-10-11 13:08:21 -04:00
elif Input.is_action_just_released("slap"):
2022-10-11 14:57:56 -04:00
if chargeSlap:
2022-10-12 09:10:44 -04:00
isSlap = true
Whoosh.play()
chargeSlap = false
can_slap = false
slap_cooldown.start(0.25)
2022-10-11 19:03:11 -04:00
AudioCharge.stop();
2022-10-11 13:08:21 -04:00
else:
isSlap = false;
2022-10-11 14:57:56 -04:00
2022-10-12 08:38:28 -04:00
if (chargeSlap):
slap += delta * 1.5;
else:
slap -= delta * 8.0;
slap = clamp(slap, 0.0, 1.0)
slap_gauge.fill = slap
if !isSlap and can_slap:
slap_animator.seek(slap, true)
if isSlap:
slap_animator.play()
slap_gauge.fill = slap;
slap_gauge.kill = slapkill;
2022-10-11 21:21:27 -04:00
2022-10-11 14:57:56 -04:00
if isSlap:
2022-10-12 09:10:44 -04:00
isSlap = false;
2022-10-11 19:57:16 -04:00
hitbox.monitorable = true
hitbox.slap_level = slap
2022-10-11 21:21:27 -04:00
hitbox.slap_kill = slapkill
2022-10-11 19:57:16 -04:00
hitbox.slap_vector = point_to.get_global_transform().origin - camera.get_global_transform().origin
2022-10-11 09:26:29 -04:00
direction = direction.normalized()
2022-10-11 16:32:48 -04:00
if isStep:
stepAcc += (abs(direction.x) + abs(direction.z)) * delta
else:
stepAcc = 0.0
if (stepAcc > 0.3):
stepAcc -= 0.45
Steps[rng.randi_range(0,3)].play();
2022-10-11 09:26:29 -04:00
h_velocity = h_velocity.linear_interpolate(direction * speed, h_acceleration * delta)
movement.z = h_velocity.z + gravity_vec.z
movement.x = h_velocity.x + gravity_vec.x
movement.y = gravity_vec.y
2022-10-11 14:58:23 -04:00
# warning-ignore:return_value_discarded
2022-10-11 09:26:29 -04:00
move_and_slide(movement, Vector3.UP)
2022-10-11 19:11:46 -04:00
func _on_SlapCooldown_timeout():
can_slap = true
slap_cooldown.stop()
hitbox.monitorable = false