141 lines
3.0 KiB
GDScript3
141 lines
3.0 KiB
GDScript3
|
extends Spatial
|
||
|
|
||
|
var swimming = false
|
||
|
var playing = true
|
||
|
|
||
|
var p : KinematicBody
|
||
|
var c : Spatial
|
||
|
|
||
|
# Movement
|
||
|
const IDLE = 0
|
||
|
|
||
|
const RUN = 1 # default movement
|
||
|
const SPRINT = 2
|
||
|
const WALK = 3
|
||
|
|
||
|
var movement_state = IDLE
|
||
|
const JUMP_STR = 10
|
||
|
|
||
|
var velocity = Vector3()
|
||
|
var yaw = 0
|
||
|
var pitch = 0
|
||
|
var is_moving = false
|
||
|
var view_sensitivity = 0.15
|
||
|
|
||
|
const ACCEL = 0.5
|
||
|
const DEACCEL = 0.8
|
||
|
|
||
|
var gravity = -40
|
||
|
|
||
|
var run_speed = 8
|
||
|
var sprint_speed = 10
|
||
|
var walk_speed = 2.7
|
||
|
|
||
|
var move_speed = run_speed
|
||
|
|
||
|
#slope variables
|
||
|
const MAX_SLOPE_ANGLE = 60
|
||
|
|
||
|
func _ready():
|
||
|
p = $FishingPlayer
|
||
|
c = $FishingPlayer/Yaw/Camera
|
||
|
|
||
|
var direction = Vector3()
|
||
|
func _physics_process(delta):
|
||
|
if not playing:
|
||
|
return
|
||
|
var up = Input.is_action_pressed("ui_up")
|
||
|
var down = Input.is_action_pressed("ui_down")
|
||
|
var left = Input.is_action_pressed("ui_left")
|
||
|
var right = Input.is_action_pressed("ui_right")
|
||
|
|
||
|
var jump = Input.is_action_pressed("jump")
|
||
|
|
||
|
var sprint = Input.is_action_pressed("sprint")
|
||
|
var walk = Input.is_action_pressed("walk")
|
||
|
var aim = c.get_camera_transform().basis
|
||
|
|
||
|
direction = Vector3()
|
||
|
|
||
|
if up:
|
||
|
direction -= aim[2]
|
||
|
if down:
|
||
|
direction += aim[2]
|
||
|
if left:
|
||
|
direction -= aim[0]
|
||
|
if right:
|
||
|
direction += aim[0]
|
||
|
|
||
|
if up or right or left or down: # IS MOVING?
|
||
|
movement_state = RUN
|
||
|
move_speed = run_speed
|
||
|
if up or right or left: # IS MOVING FORWARDS?
|
||
|
if sprint:
|
||
|
movement_state = SPRINT
|
||
|
move_speed = sprint_speed
|
||
|
elif walk:
|
||
|
movement_state = WALK
|
||
|
move_speed = walk_speed
|
||
|
else: # IS NOT MOVING?
|
||
|
movement_state = IDLE
|
||
|
|
||
|
var normal = $FishingPlayer/floor_check.get_collision_normal()
|
||
|
|
||
|
if p.is_on_floor():
|
||
|
velocity = velocity - velocity.dot(normal) * normal
|
||
|
|
||
|
if jump:
|
||
|
if movement_state == SPRINT:
|
||
|
velocity.y += JUMP_STR * 1.1 # Jump higher if sprinting
|
||
|
else:
|
||
|
velocity.y += JUMP_STR
|
||
|
else:
|
||
|
_apply_gravity(delta)
|
||
|
|
||
|
if velocity.x > 0 or velocity.x < 0 and is_moving == false:
|
||
|
is_moving = true
|
||
|
else:
|
||
|
is_moving = false
|
||
|
if velocity.y > 0 or velocity.y < 0 and is_moving == false:
|
||
|
is_moving = true
|
||
|
else:
|
||
|
is_moving = false
|
||
|
if velocity.z > 0 or velocity.z < 0 and is_moving == false:
|
||
|
is_moving = true
|
||
|
else:
|
||
|
is_moving = false
|
||
|
pass
|
||
|
|
||
|
direction.y = 0
|
||
|
#Normalize direction
|
||
|
direction = direction.normalized()
|
||
|
|
||
|
var hvel = velocity
|
||
|
hvel.y = 0
|
||
|
var target = direction * move_speed
|
||
|
var accel
|
||
|
if(direction.dot(hvel) > 0):
|
||
|
accel = ACCEL
|
||
|
else:
|
||
|
accel = DEACCEL
|
||
|
|
||
|
hvel = hvel.linear_interpolate(target, accel * move_speed * delta)
|
||
|
velocity.x = hvel.x
|
||
|
velocity.z = hvel.z
|
||
|
|
||
|
velocity = p.move_and_slide(velocity, Vector3(0, 1, 0), 0.05, 4, deg2rad(MAX_SLOPE_ANGLE))
|
||
|
|
||
|
func _apply_gravity(delta):
|
||
|
velocity.y += delta * gravity
|
||
|
|
||
|
func _input(event):
|
||
|
forward_input(event)
|
||
|
func forward_input(event):
|
||
|
if not playing:
|
||
|
return
|
||
|
if event is InputEventMouseMotion:
|
||
|
yaw = fmod(yaw - event.relative.x * view_sensitivity, 360)
|
||
|
pitch = max(min(pitch - event.relative.y * view_sensitivity, 89), -89)
|
||
|
$FishingPlayer/Yaw.rotation = Vector3(0, deg2rad(yaw), 0)
|
||
|
$FishingPlayer/Yaw/Camera.rotation = Vector3(deg2rad(pitch), 0, 0)
|