Created
November 1, 2019 10:36
-
-
Save Skaruts/add881c8f0fc9475588032840ae7550c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extends KinematicBody | |
onready var head := $Head | |
onready var camera := $Head/Camera | |
onready var weapon := $Weapon | |
onready var frob_ray := $Head/Camera/FrobRay | |
onready var coll_stand := $CollisionStand | |
onready var coll_crouch := $CollisionCrouch | |
#export var fly_mode:bool = false | |
export var speed:float = 2 | |
export var running_mult:float = 2 | |
export var crouch_speed:float = 1 | |
export var acceleration:float = 10 | |
export var gravity:float = 0.6# 0.98 | |
export var jump_power:float = 10 | |
export var crouch_jump_power:float = 7.5 | |
export var mouse_sensitivity:float = 0.3 | |
var camera_pitch := 0.0 | |
var velocity := Vector3() | |
var sprinting:bool = false | |
var crouching:bool = false | |
var pressing_crouch:bool = false | |
var crouch_is_toggle:bool = true | |
var sprint_is_toggle:bool = false | |
var current_frob_object = null | |
# helpers to prevent from standing up when stop crouching underneath something | |
onready var crouch_ray_center := $CrouchHeadRays/Ray5 | |
onready var crouch_ray1 := $CrouchHeadRays/Ray1 | |
onready var crouch_ray2 := $CrouchHeadRays/Ray2 | |
onready var crouch_ray3 := $CrouchHeadRays/Ray3 | |
onready var crouch_ray4 := $CrouchHeadRays/Ray4 | |
func _ready() -> void: | |
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED) | |
func _input(event: InputEvent) -> void: | |
if event is InputEventMouseButton and Input.get_mouse_mode() == Input.MOUSE_MODE_VISIBLE: | |
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED) | |
if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED: | |
if event is InputEventMouseMotion: | |
head.rotate_y(deg2rad(-event.relative.x * mouse_sensitivity)) | |
var delta_pitch = event.relative.y * mouse_sensitivity | |
if camera_pitch + delta_pitch > -90 and camera_pitch + delta_pitch < 90: | |
camera.rotate_x(deg2rad(-delta_pitch)) | |
camera_pitch += delta_pitch | |
elif event.is_action_pressed("attack2"): interact() | |
elif event.is_action_pressed("reload"): weapon.reload() | |
# TODO: move all this logic out of here | |
elif event.is_action_pressed("crouch"): | |
if not crouch_is_toggle: | |
pressing_crouch = true | |
crouch() | |
else: | |
if crouching: | |
pressing_crouch = false | |
stand() | |
else: | |
pressing_crouch = true | |
crouch() | |
elif event.is_action_released("crouch"): | |
if not crouch_is_toggle: | |
pressing_crouch = false | |
stand() | |
elif event.is_action_pressed("sprint"): | |
if not sprint_is_toggle: | |
sprinting = true | |
else: | |
sprinting = not sprinting | |
elif event.is_action_released("sprint"): | |
if not sprint_is_toggle: | |
sprinting = false | |
elif event.is_action_pressed("lean left"): | |
$AnimationPlayer.play("lean left") | |
elif event.is_action_pressed("lean right"): | |
$AnimationPlayer.play("lean right") | |
elif event.is_action_released("lean left"): | |
$AnimationPlayer.play_backwards("lean left") | |
elif event.is_action_released("lean right"): | |
$AnimationPlayer.play_backwards("lean right") | |
func crouch(): | |
crouching = true | |
$AnimationPlayer.play("crouch") | |
func stand(): | |
if not crouch_ray_center.is_colliding()\ | |
and not crouch_ray1.is_colliding()\ | |
and not crouch_ray2.is_colliding()\ | |
and not crouch_ray3.is_colliding()\ | |
and not crouch_ray4.is_colliding(): | |
crouching = false | |
$AnimationPlayer.play_backwards("crouch") | |
func interact(): | |
if current_frob_object != null: | |
current_frob_object.interact() | |
func check_for_frobable_object(): | |
if frob_ray.is_colliding(): | |
var collider = frob_ray.get_collider().get_parent() | |
# print("%s, %s, %s" % [collider.name, collider is Interactible, current_frob_object]) | |
if collider is Interactible: | |
current_frob_object = collider | |
current_frob_object.highlight(true) | |
else: | |
if current_frob_object != null: | |
current_frob_object.highlight(false) | |
current_frob_object = null | |
func _physics_process(delta: float) -> void: | |
if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED: | |
if crouching and not pressing_crouch: stand() | |
if Input.is_action_just_pressed("ui_cancel"): | |
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE) | |
check_for_frobable_object() | |
if Input.is_action_pressed("attack1"): | |
weapon.shoot() | |
var basis:Basis | |
# TODO: move this to a fly() function | |
# if fly_mode: basis = camera.global_transform.basis | |
# else: | |
basis = head.global_transform.basis | |
var dir := Vector3() | |
if Input.is_action_pressed("move_forward"): dir -= basis.z | |
if Input.is_action_pressed("move_backward"): dir += basis.z | |
if Input.is_action_pressed("move_left"): dir -= basis.x | |
if Input.is_action_pressed("move_right"): dir += basis.x | |
dir = dir.normalized() | |
var current_speed = speed | |
if crouching: current_speed = crouch_speed | |
if sprinting: current_speed *= running_mult | |
# velocity.x = dir.x*current_speed | |
# velocity.z = dir.z*current_speed | |
velocity.x = lerp(velocity.x, dir.x*current_speed, acceleration*delta) | |
velocity.z = lerp(velocity.z, dir.z*current_speed, acceleration*delta) | |
velocity.y -= gravity | |
velocity = move_and_slide(velocity, Vector3.UP, true, 4, deg2rad(70)) | |
var current_jump_power = jump_power | |
if crouching: current_jump_power = crouch_jump_power | |
if Input.is_action_just_pressed("jump") and is_on_floor(): | |
velocity.y += current_jump_power | |
# else: | |
# camera.do_bobbing(delta, dir) | |
if dir.length() > 0 and is_on_floor(): | |
$AnimationPlayer2.play("bobbing") | |
else: | |
$AnimationPlayer2.stop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment