2023-09-26 21:38:02 -04:00
|
|
|
extends CharacterBody3D
|
|
|
|
|
|
|
|
# Member variables
|
2023-10-01 22:07:33 -04:00
|
|
|
const QUAKE = 0.0625
|
2023-10-01 21:44:21 -04:00
|
|
|
const g = -800 * QUAKE
|
2023-10-02 00:36:31 -04:00
|
|
|
const MAX_SPEED = 320 * QUAKE
|
|
|
|
const MAX_AIR_SPEED = 30 * QUAKE
|
2023-10-01 22:18:35 -04:00
|
|
|
const FRICTION = 4# * QUAKE
|
2023-10-01 21:44:21 -04:00
|
|
|
const STOP_SPEED = 100 * QUAKE
|
2023-10-02 00:36:31 -04:00
|
|
|
const ACCEL = 15# * QUAKE
|
|
|
|
const AIR_ACCEL = 2# * QUAKE
|
2023-10-01 21:44:21 -04:00
|
|
|
const FORWARD_SPEED = 200 * QUAKE
|
|
|
|
const SIDE_SPEED = 350 * QUAKE
|
|
|
|
const UP_SPEED = 270 * QUAKE
|
2023-10-02 00:36:31 -04:00
|
|
|
const JUMP_SPEED = 270 * QUAKE
|
2023-09-26 21:38:02 -04:00
|
|
|
|
2023-10-01 21:44:21 -04:00
|
|
|
# const DEACCEL= 8
|
2023-10-02 00:36:31 -04:00
|
|
|
# const MAX_SLOPE_ANGLE = 30https://www.youtube.com/watch?v=v3zT3Z5apaM
|
2023-09-26 21:38:02 -04:00
|
|
|
|
|
|
|
@onready
|
2023-10-02 17:28:55 -04:00
|
|
|
var _camera = $CSGMesh3D/Camera3D
|
|
|
|
@onready
|
|
|
|
var _ball = $Ball
|
|
|
|
@onready
|
|
|
|
var _collider = $CollisionShape3D
|
|
|
|
@onready
|
|
|
|
var _flashlight: SpotLight3D = $Flashlight
|
2023-09-26 21:38:02 -04:00
|
|
|
|
2023-09-27 21:55:59 -04:00
|
|
|
var _paused = false
|
|
|
|
var _noclip = false
|
|
|
|
|
|
|
|
|
|
|
|
func _ready():
|
|
|
|
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
|
|
|
|
|
2023-09-26 21:38:02 -04:00
|
|
|
|
2023-10-03 19:26:49 -04:00
|
|
|
func _process(delta):
|
|
|
|
# Do this on the render tick so it doesnt jitter at higher FPS
|
|
|
|
_ball.global_position = _camera.global_position - _camera.global_transform.basis.z * 2.0
|
|
|
|
|
|
|
|
|
2023-09-26 21:38:02 -04:00
|
|
|
func _physics_process(delta):
|
2023-10-01 21:44:21 -04:00
|
|
|
# Shamelessly stolen code from quake :)
|
2023-10-02 00:38:08 -04:00
|
|
|
|
2023-10-01 21:44:21 -04:00
|
|
|
var fmove = 0.0
|
|
|
|
var smove = 0.0
|
|
|
|
var upmove = 0.0
|
2023-09-26 21:38:02 -04:00
|
|
|
|
|
|
|
if Input.is_action_pressed("move_forward"):
|
2023-10-01 21:44:21 -04:00
|
|
|
fmove += -FORWARD_SPEED
|
2023-09-26 21:38:02 -04:00
|
|
|
if Input.is_action_pressed("move_backwards"):
|
2023-10-01 21:44:21 -04:00
|
|
|
fmove += FORWARD_SPEED
|
2023-09-26 21:38:02 -04:00
|
|
|
if Input.is_action_pressed("move_left"):
|
2023-10-01 21:44:21 -04:00
|
|
|
smove += -SIDE_SPEED
|
2023-09-26 21:38:02 -04:00
|
|
|
if Input.is_action_pressed("move_right"):
|
2023-10-01 21:44:21 -04:00
|
|
|
smove += SIDE_SPEED
|
2023-10-02 00:36:31 -04:00
|
|
|
#if Input.is_action_pressed("up"):
|
|
|
|
# upmove += UP_SPEED
|
2023-09-27 21:55:59 -04:00
|
|
|
if Input.is_action_pressed("run"):
|
2023-10-01 21:44:21 -04:00
|
|
|
fmove *= 2.0
|
|
|
|
smove *= 2.0
|
2023-10-02 00:38:08 -04:00
|
|
|
|
2023-10-01 21:44:21 -04:00
|
|
|
var wishvel = Vector3()
|
2023-10-02 17:28:55 -04:00
|
|
|
var cam_xform: Transform3D = _camera.get_global_transform()
|
2023-10-01 21:44:21 -04:00
|
|
|
var cam_basis: Basis = cam_xform.basis
|
2023-10-02 00:38:08 -04:00
|
|
|
|
2023-10-01 21:44:21 -04:00
|
|
|
wishvel = cam_basis.z * fmove + cam_basis.x * smove
|
2023-10-02 00:38:08 -04:00
|
|
|
|
2023-09-27 21:55:59 -04:00
|
|
|
if _noclip:
|
2023-10-01 22:07:33 -04:00
|
|
|
wishvel.y += upmove
|
2023-10-02 00:38:08 -04:00
|
|
|
|
2023-10-01 21:44:21 -04:00
|
|
|
var wishdir = wishvel.normalized()
|
|
|
|
var wishspeed = wishvel.length()
|
2023-10-02 00:38:08 -04:00
|
|
|
|
2023-10-01 21:44:21 -04:00
|
|
|
if wishspeed > MAX_SPEED:
|
|
|
|
wishvel *= MAX_SPEED / wishspeed
|
|
|
|
wishspeed = MAX_SPEED
|
2023-10-02 00:38:08 -04:00
|
|
|
|
2023-10-02 00:36:31 -04:00
|
|
|
if Input.is_action_pressed("jump") and is_on_floor():
|
|
|
|
velocity.y += JUMP_SPEED
|
2023-10-02 00:38:08 -04:00
|
|
|
|
2023-10-01 21:44:21 -04:00
|
|
|
var vel = velocity
|
|
|
|
if _noclip:
|
|
|
|
vel = wishvel
|
|
|
|
elif is_on_floor():
|
|
|
|
vel = _apply_friction(vel, delta)
|
|
|
|
vel = _apply_accel(vel, wishdir, wishspeed, delta)
|
|
|
|
else:
|
2023-10-02 18:56:05 -04:00
|
|
|
vel = _apply_air_accel(vel, wishvel, wishspeed, delta)
|
2023-10-02 00:38:08 -04:00
|
|
|
|
2023-09-27 21:55:59 -04:00
|
|
|
if not _noclip:
|
2023-10-01 21:44:21 -04:00
|
|
|
vel.y += g * delta
|
2023-10-02 00:38:08 -04:00
|
|
|
|
2023-10-01 21:44:21 -04:00
|
|
|
velocity = vel
|
2023-09-26 21:38:02 -04:00
|
|
|
move_and_slide()
|
2023-10-02 00:38:08 -04:00
|
|
|
|
2023-10-01 21:44:21 -04:00
|
|
|
func _apply_friction(vel: Vector3, delta: float):
|
|
|
|
var speed = sqrt(vel.x ** 2 + vel.z ** 2)
|
|
|
|
if(speed == 0.0): return vel
|
2023-10-02 00:38:08 -04:00
|
|
|
|
2023-10-01 21:44:21 -04:00
|
|
|
# TODO: edge detect friction?
|
|
|
|
var friction = FRICTION
|
2023-10-02 00:38:08 -04:00
|
|
|
|
2023-10-01 21:44:21 -04:00
|
|
|
var control = 0.0
|
|
|
|
if speed < STOP_SPEED:
|
|
|
|
control = STOP_SPEED
|
|
|
|
else:
|
|
|
|
control = speed
|
2023-10-02 00:38:08 -04:00
|
|
|
|
2023-10-01 21:44:21 -04:00
|
|
|
var newspeed = speed - delta * control * friction
|
2023-10-02 00:38:08 -04:00
|
|
|
|
2023-10-01 21:44:21 -04:00
|
|
|
if newspeed < 0.0:
|
|
|
|
newspeed = 0.0
|
2023-10-02 00:36:31 -04:00
|
|
|
else:
|
|
|
|
newspeed /= speed
|
2023-10-02 00:38:08 -04:00
|
|
|
|
2023-10-01 21:44:21 -04:00
|
|
|
return vel * newspeed
|
2023-10-02 00:38:08 -04:00
|
|
|
|
2023-10-01 21:44:21 -04:00
|
|
|
func _apply_accel(vel: Vector3, wishdir: Vector3, wishspeed: float, delta: float):
|
|
|
|
var currentspeed = vel.dot(wishdir)
|
|
|
|
var addspeed = wishspeed - currentspeed
|
|
|
|
if addspeed <= 0.0:
|
|
|
|
return vel
|
2023-10-02 00:38:08 -04:00
|
|
|
|
2023-10-01 21:44:21 -04:00
|
|
|
var accelspeed = ACCEL * delta * wishspeed
|
|
|
|
if accelspeed > addspeed:
|
|
|
|
accelspeed = addspeed
|
2023-10-02 00:38:08 -04:00
|
|
|
|
2023-10-01 21:44:21 -04:00
|
|
|
return vel + accelspeed * wishdir
|
2023-10-02 00:38:08 -04:00
|
|
|
|
2023-10-02 18:56:05 -04:00
|
|
|
func _apply_air_accel(vel: Vector3, wishvel: Vector3, wishspeed: float, delta: float):
|
2023-10-01 21:44:21 -04:00
|
|
|
var wishveloc = wishvel.normalized()
|
|
|
|
var wishspd = wishvel.length()
|
2023-10-02 00:38:08 -04:00
|
|
|
|
2023-10-02 00:36:31 -04:00
|
|
|
if wishspd > MAX_AIR_SPEED:
|
|
|
|
wishspd = MAX_AIR_SPEED
|
2023-10-02 00:38:08 -04:00
|
|
|
|
2023-10-01 21:44:21 -04:00
|
|
|
var currentspeed = vel.dot(wishveloc)
|
|
|
|
var addspeed = wishspd - currentspeed
|
|
|
|
if addspeed <= 0:
|
|
|
|
return vel
|
2023-10-02 00:38:08 -04:00
|
|
|
|
2023-10-02 18:56:05 -04:00
|
|
|
var accelspeed = AIR_ACCEL * wishspeed * delta
|
2023-10-01 21:44:21 -04:00
|
|
|
if accelspeed > addspeed:
|
|
|
|
accelspeed = addspeed
|
2023-10-02 00:38:08 -04:00
|
|
|
|
2023-10-01 21:44:21 -04:00
|
|
|
return vel + accelspeed * wishveloc
|
2023-09-26 21:38:02 -04:00
|
|
|
|
|
|
|
func _input(event):
|
2023-09-27 21:55:59 -04:00
|
|
|
if event.is_action_pressed("pause"):
|
|
|
|
_paused = not _paused
|
|
|
|
if _paused:
|
|
|
|
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
|
|
|
|
else:
|
|
|
|
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
|
2023-10-02 00:38:08 -04:00
|
|
|
|
2023-09-27 21:55:59 -04:00
|
|
|
if _paused: return
|
2023-10-02 00:38:08 -04:00
|
|
|
|
2023-09-27 21:55:59 -04:00
|
|
|
if event.is_action_pressed('noclip'):
|
|
|
|
_noclip = not _noclip
|
|
|
|
if _noclip:
|
2023-10-02 17:28:55 -04:00
|
|
|
_collider.disabled = true
|
2023-09-27 21:55:59 -04:00
|
|
|
else:
|
2023-10-02 17:28:55 -04:00
|
|
|
_collider.disabled = false
|
|
|
|
|
|
|
|
if event.is_action_pressed('flashlight'):
|
|
|
|
_flashlight.visible = not _flashlight.visible
|
2023-10-02 00:38:08 -04:00
|
|
|
|
2023-10-02 17:28:55 -04:00
|
|
|
# Camera
|
2023-09-27 21:55:59 -04:00
|
|
|
if event is InputEventMouseMotion:
|
|
|
|
rotation.y += -event.relative.x * 0.005
|
2023-10-02 17:28:55 -04:00
|
|
|
_camera.rotation.x += -event.relative.y * 0.005
|
|
|
|
_camera.rotation.x = clamp(_camera.rotation.x, deg_to_rad(-70), deg_to_rad(70))
|
|
|
|
|
|
|
|
# Rotate flashlight
|
|
|
|
_flashlight.rotation.x = _camera.rotation.x
|
2023-10-02 00:38:08 -04:00
|
|
|
|
2023-09-27 00:05:42 -04:00
|
|
|
if event.is_action_pressed("interact"):
|
|
|
|
var space_state = get_world_3d().direct_space_state
|
|
|
|
# use global coordinates, not local to node
|
2023-10-02 17:28:55 -04:00
|
|
|
var query = PhysicsRayQueryParameters3D.create(_camera.global_position, _ball.global_position)
|
2023-09-27 21:55:59 -04:00
|
|
|
# Don't collide with ourselves
|
|
|
|
query.exclude = [self]
|
2023-09-27 00:05:42 -04:00
|
|
|
var result = space_state.intersect_ray(query)
|
2023-09-27 21:55:59 -04:00
|
|
|
if result.has('collider'):
|
|
|
|
var other: Node3D = result.collider
|
|
|
|
if other.has_method('interact'):
|
|
|
|
other.interact(self)
|
2023-10-02 00:38:08 -04:00
|
|
|
|