initial commit
This commit is contained in:
297
scripts/player.gd
Normal file
297
scripts/player.gd
Normal file
@@ -0,0 +1,297 @@
|
||||
extends CharacterBody3D
|
||||
|
||||
|
||||
var speed
|
||||
var double_jump = true
|
||||
var gravity = 9.8
|
||||
var mouse_input : Vector2
|
||||
|
||||
var rng = RandomNumberGenerator.new()
|
||||
|
||||
@export_group("Player Movement")
|
||||
@export var WALK_SPEED = 7.0
|
||||
@export var SPRINT_SPEED = 15.0
|
||||
@export var JUMP_VELOCITY = 10
|
||||
@export var SENSITIVITY = .01
|
||||
@export var sensitivity_shift = true
|
||||
var start_sensitivity
|
||||
@export_subgroup("Head Bob & Gun Sway")
|
||||
@export var BOB_FREQ = 2.0
|
||||
@export var BOB_AMP = 0.08
|
||||
@export var t_bob = 1.0
|
||||
@export var weapon_holder : Node3D
|
||||
@export var weapon_sway_amount : float = 5
|
||||
@export var weapon_rotation_amount : float = 1
|
||||
@export_subgroup("FOV")
|
||||
@export var BASE_FOV : float = 80
|
||||
@export var FOV_CHANGE = 1.5
|
||||
|
||||
@export_group("Gun")
|
||||
@export var gun : Node
|
||||
|
||||
@onready var gun_ray = $Head/Camera3D/GunRay
|
||||
|
||||
@onready var bullet_ray = $Head/Camera3D/BulletRay
|
||||
var instance_bullet
|
||||
var instance_casing
|
||||
var instance_mag
|
||||
var reloading = false
|
||||
var ammo
|
||||
var ammo_reserve
|
||||
var bullet_damage
|
||||
var def_weapon_holder_pos : Vector3
|
||||
var gun_folded = false
|
||||
var recoiling_fade = 0
|
||||
var bullet_destination
|
||||
var gun_fire_pitch_starting
|
||||
|
||||
# Slow Down Variables
|
||||
const SLOWSPEED = .2
|
||||
const MAX_STAMINA = 1800
|
||||
const STAMINA_DRAIN = 100
|
||||
var remaining_stamina = MAX_STAMINA
|
||||
# Pickups
|
||||
var picked_up = false
|
||||
var picked_up_text
|
||||
var pickup_announce = load("res://assets/pickup_announce.tscn")
|
||||
var pickupmsg
|
||||
|
||||
@onready var crosshair = $Head/Camera3D/Crosshair
|
||||
@onready var head = $Head
|
||||
@onready var camera = $Head/Camera3D
|
||||
@onready var world_environment = $"../WorldEnvironment"
|
||||
@onready var pickup_sound = $Audio/PickupSound
|
||||
@onready var ear_wind = $Audio/EarWind
|
||||
|
||||
|
||||
func _ready():
|
||||
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
|
||||
#Set up gun variables
|
||||
def_weapon_holder_pos = weapon_holder.position
|
||||
ammo = gun.max_ammo
|
||||
ammo_reserve = gun.max_ammo * gun.start_mags
|
||||
bullet_damage = gun.bullet_damage
|
||||
start_sensitivity = SENSITIVITY
|
||||
gun_fire_pitch_starting = gun.audio_fire.pitch_scale
|
||||
|
||||
func _unhandled_input(event):
|
||||
if event is InputEventMouseMotion:
|
||||
self.rotate_y(-event.relative.x * SENSITIVITY)
|
||||
camera.rotate_x(-event.relative.y * SENSITIVITY)
|
||||
camera.rotation.x = clamp(camera.rotation.x, deg_to_rad(-90), deg_to_rad(60))
|
||||
mouse_input = event.relative
|
||||
|
||||
|
||||
func _physics_process(delta):
|
||||
# Add the gravity.
|
||||
if not is_on_floor():
|
||||
velocity.y -= gravity * delta
|
||||
else:
|
||||
double_jump = true
|
||||
|
||||
# Handle jump.
|
||||
if Input.is_action_just_pressed("jump") and is_on_floor():
|
||||
velocity.y += JUMP_VELOCITY
|
||||
elif Input.is_action_just_pressed("jump") and double_jump == true:
|
||||
velocity.y += JUMP_VELOCITY
|
||||
double_jump = false
|
||||
|
||||
# Handle Sprint
|
||||
if Input.is_action_pressed("sprint") and is_on_floor():
|
||||
speed = SPRINT_SPEED
|
||||
else:
|
||||
speed = WALK_SPEED
|
||||
|
||||
# Get the input direction and handle the movement/deceleration.
|
||||
var input_dir = Input.get_vector("move_left", "move_right", "move_up", "move_down")
|
||||
var direction = (self.transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
|
||||
if is_on_floor():
|
||||
if direction:
|
||||
velocity.x = direction.x * speed
|
||||
velocity.z = direction.z * speed
|
||||
else:
|
||||
velocity.x = lerp(velocity.x, direction.x * speed, delta * 3.0)
|
||||
velocity.z = lerp(velocity.z, direction.z * speed, delta * 3.0)
|
||||
else:
|
||||
velocity.x = lerp(velocity.x, direction.x * speed, delta * 3.0)
|
||||
velocity.z = lerp(velocity.z, direction.z * speed, delta * 3.0)
|
||||
|
||||
# Head bob
|
||||
t_bob += delta * velocity.length() * float(is_on_floor())
|
||||
camera.transform.origin = _headbob(t_bob)
|
||||
|
||||
# FOV
|
||||
var velocity_clamped = clamp(velocity.length(), 0.5, SPRINT_SPEED * 2)
|
||||
var target_fov = BASE_FOV + FOV_CHANGE * velocity_clamped
|
||||
camera.fov = lerp(camera.fov, target_fov, delta * 8)
|
||||
|
||||
# Land sound
|
||||
|
||||
|
||||
# Game Speed
|
||||
if Input.is_action_pressed("slow_down") and remaining_stamina >0:
|
||||
Engine.time_scale = lerp(0, 1, SLOWSPEED)
|
||||
gun.random_spread_amt = 0
|
||||
AudioServer.set_bus_effect_enabled(0,0,true)
|
||||
camera.fov = lerp(camera.fov, camera.fov * .98, delta * 100)
|
||||
if sensitivity_shift == true:
|
||||
SENSITIVITY = lerp(SENSITIVITY, SENSITIVITY * .999, delta * 100)
|
||||
if remaining_stamina >0:
|
||||
remaining_stamina -= 1000 * delta
|
||||
else:
|
||||
Engine.time_scale = 1
|
||||
gun.random_spread_amt = gun.random_spread_start
|
||||
AudioServer.set_bus_effect_enabled(0,0,false)
|
||||
if sensitivity_shift == true:
|
||||
camera.fov = lerp(camera.fov, BASE_FOV, delta * .5)
|
||||
SENSITIVITY = start_sensitivity
|
||||
if remaining_stamina < MAX_STAMINA:
|
||||
remaining_stamina += STAMINA_DRAIN * delta
|
||||
elif remaining_stamina > MAX_STAMINA * 1.01:
|
||||
remaining_stamina -= (STAMINA_DRAIN)/2 * delta
|
||||
|
||||
# Reloading
|
||||
if Input.is_action_just_pressed("reload"):
|
||||
if ammo < gun.max_ammo and reloading == false and ammo_reserve > 0:
|
||||
reloading = true
|
||||
gun.anim_player.play("reload")
|
||||
gun.audio_reload.play()
|
||||
instance_mag = gun.mag.instantiate()
|
||||
await get_tree().create_timer(.28).timeout
|
||||
instance_mag.position = gun.mag_ejector.global_position
|
||||
instance_mag.transform.basis = gun.mag_ejector.global_transform.basis
|
||||
get_parent().add_child(instance_mag)
|
||||
if gun.anim_player.is_playing() and gun.anim_player.current_animation == "reload":
|
||||
if ammo == 0:
|
||||
ammo = 0
|
||||
else:
|
||||
ammo = 1
|
||||
if Input.is_action_pressed("inspect") and !gun.anim_player.is_playing():
|
||||
gun.anim_player.play("inspect")
|
||||
|
||||
# Shooting
|
||||
if Input.is_action_pressed("shoot"):
|
||||
if ammo > 0:
|
||||
if !gun.anim_player.is_playing():
|
||||
ammo -= 1
|
||||
#RECOIL fix later to happen over a period of time
|
||||
camera.rotation.x = clamp(lerp(camera.rotation.x, camera.rotation.x + gun.recoil_amount, delta * 10), deg_to_rad(-90), deg_to_rad(60))
|
||||
#(ADD PLAYER KICK HERE. RELATIVE TO GUN POSITION)
|
||||
gun.audio_fire.pitch_scale = 1 + rng.randf_range(-.2,.2)
|
||||
gun.audio_fire.play()
|
||||
gun.anim_player.play("shoot")
|
||||
|
||||
# shoot real bullet from camera
|
||||
if gun_folded == false:
|
||||
instance_bullet = gun.bullet.instantiate()
|
||||
instance_bullet.position = bullet_ray.global_position
|
||||
instance_bullet.transform.basis = bullet_ray.global_transform.basis
|
||||
instance_bullet.bullet_speed = gun.bullet_speed
|
||||
instance_bullet.bullet_drop = gun.bullet_drop
|
||||
instance_bullet.random_spread_amt = gun.random_spread_amt
|
||||
instance_bullet.gun = gun
|
||||
get_parent().add_child(instance_bullet)
|
||||
else:
|
||||
instance_bullet = gun.bullet.instantiate()
|
||||
instance_bullet.position = gun.barrel_raycast.global_position
|
||||
instance_bullet.transform.basis = gun.barrel_raycast.global_transform.basis
|
||||
instance_bullet.bullet_speed = gun.bullet_speed
|
||||
instance_bullet.bullet_drop = gun.bullet_drop
|
||||
instance_bullet.random_spread_amt = gun.random_spread_amt
|
||||
instance_bullet.gun = gun
|
||||
get_parent().add_child(instance_bullet)
|
||||
|
||||
# Casing transform
|
||||
instance_casing = gun.casing.instantiate()
|
||||
instance_casing.position = gun.casing_ejector.global_position
|
||||
instance_casing.transform.basis = gun.casing_ejector.global_transform.basis
|
||||
|
||||
get_parent().add_child(instance_casing)
|
||||
elif !gun.anim_player.is_playing():
|
||||
gun.anim_player.play("empty")
|
||||
gun.audio_empty.play()
|
||||
|
||||
|
||||
|
||||
# Gun folding out of the way
|
||||
if gun_ray.is_colliding() and !gun_ray.get_collider().is_in_group("player"):
|
||||
#check distance to wall later ?
|
||||
gun.rotation = lerp(gun.rotation, Vector3(1, 1, -.25), delta * 10)
|
||||
gun.position = lerp(gun.position, Vector3(gun.start_position.x-.25,gun.start_position.y +.2,gun.start_position.z +.6),(delta*10))
|
||||
gun_folded = true
|
||||
elif !gun_ray.is_colliding():
|
||||
gun.rotation = lerp(gun.rotation, gun.start_rotation,delta*7)
|
||||
gun.position = lerp(gun.position, gun.start_position,delta*7)
|
||||
gun_folded = false
|
||||
|
||||
move_and_slide()
|
||||
weapon_tilt(input_dir.x, delta)
|
||||
weapon_sway(delta)
|
||||
weapon_bob(velocity.length(), delta)
|
||||
|
||||
func _headbob(time) -> Vector3:
|
||||
var pos = Vector3.ZERO
|
||||
pos.y = sin(time * BOB_FREQ) * BOB_AMP
|
||||
pos.x = cos(time * BOB_FREQ / 2) * BOB_AMP
|
||||
return pos
|
||||
|
||||
|
||||
func _on_pick_up_detection_body_entered(body):
|
||||
if body.pickupType == "stamina":
|
||||
pickupmsg = pickup_announce.instantiate()
|
||||
pickupmsg.pickuptext = "stamina"
|
||||
get_parent().add_child(pickupmsg)
|
||||
remaining_stamina += (body.rand_amt/100) * MAX_STAMINA
|
||||
pickup_sound.pitch_scale = 1 + rng.randf_range(-.3,.3)
|
||||
pickup_sound.play()
|
||||
body.queue_free()
|
||||
elif body.pickupType == "ammo":
|
||||
pickupmsg = pickup_announce.instantiate()
|
||||
pickupmsg.pickuptext = "ammo"
|
||||
get_parent().add_child(pickupmsg)
|
||||
ammo_reserve += int((body.rand_amt/100) * gun.max_ammo)
|
||||
picked_up = true
|
||||
picked_up_text = "ammo"
|
||||
pickup_sound.pitch_scale = 1 + rng.randf_range(-.3,.3)
|
||||
pickup_sound.play()
|
||||
body.queue_free()
|
||||
elif body.pickupType == "jump":
|
||||
pickupmsg = pickup_announce.instantiate()
|
||||
pickupmsg.pickuptext = "jump"
|
||||
get_parent().add_child(pickupmsg)
|
||||
double_jump = true
|
||||
picked_up = true
|
||||
picked_up_text = "double jump"
|
||||
pickup_sound.pitch_scale = 1 + rng.randf_range(-.3,.3)
|
||||
pickup_sound.play()
|
||||
body.queue_free()
|
||||
|
||||
|
||||
func _on_pick_up_magnet_body_entered(body):
|
||||
if body.is_in_group("pickup"):
|
||||
print("pickup")
|
||||
|
||||
func weapon_tilt(input_x, delta):
|
||||
if weapon_holder:
|
||||
weapon_holder.rotation.z = lerp(weapon_holder.rotation.z, -input_x * weapon_rotation_amount * 10, 10 * delta)
|
||||
|
||||
func weapon_sway(delta):
|
||||
mouse_input = lerp(mouse_input, Vector2.ZERO, 10 * delta)
|
||||
weapon_holder.rotation.x = lerp(weapon_holder.rotation.x, mouse_input.y * weapon_rotation_amount, 10 * delta)
|
||||
weapon_holder.rotation.y = lerp(weapon_holder.rotation.y, mouse_input.x * weapon_rotation_amount, 10 * delta)
|
||||
|
||||
#crosshair.position.x = lerp(crosshair.position.x, mouse_input.x * 100, 2 * delta)
|
||||
|
||||
#crosshair.size.x = lerp(crosshair.size.x, clamp((abs(mouse_input.x * 10) + abs(mouse_input.y) * 1000),0.0,100.0), 1 * delta)
|
||||
#crosshair.size.y = lerp(crosshair.size.y, clamp((abs(mouse_input.x * 10) + abs(mouse_input.y) * 1000),0.0,100.0), 1 * delta)
|
||||
|
||||
func weapon_bob(vel : float, delta):
|
||||
if weapon_holder:
|
||||
if vel > 2 and is_on_floor():
|
||||
var bob_amount : float = 0.05
|
||||
var bob_freq : float = 0.01
|
||||
weapon_holder.position.y = lerp(weapon_holder.position.y, def_weapon_holder_pos.y + sin(Time.get_ticks_msec() * bob_freq) * bob_amount, 10 * delta)
|
||||
weapon_holder.position.x = lerp(weapon_holder.position.x, def_weapon_holder_pos.x + sin(Time.get_ticks_msec() * bob_freq) * bob_amount, 10 * delta)
|
||||
else:
|
||||
weapon_holder.position.y = lerp(weapon_holder.position.y, def_weapon_holder_pos.y, .1 * delta)
|
||||
weapon_holder.position.x = lerp(weapon_holder.position.x, def_weapon_holder_pos.x, .1 * delta)
|
||||
Reference in New Issue
Block a user