From ead2b579176629600a05d26afd6fc02444072158 Mon Sep 17 00:00:00 2001 From: Quantum Date: Sun, 1 Oct 2023 21:44:21 -0400 Subject: [PATCH] First draft Quake movement Pure shit --- Commentary.tscn | 2 +- Debug.gd | 2 +- Player.gd | 145 ++++++++++++++++++++++++++++++++++++------------ world.tscn | 8 +++ 4 files changed, 118 insertions(+), 39 deletions(-) diff --git a/Commentary.tscn b/Commentary.tscn index 7f7d921..e3cde70 100644 --- a/Commentary.tscn +++ b/Commentary.tscn @@ -33,7 +33,7 @@ size = Vector3(1, 1.5, 1) script = ExtResource("1_omu2m") [node name="Koob" type="CSGMesh3D" parent="."] -transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.20345, 0) +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.816788, 0) mesh = SubResource("BoxMesh_sbsxh") material = SubResource("StandardMaterial3D_8bjqg") diff --git a/Debug.gd b/Debug.gd index 1af1a13..f302e75 100644 --- a/Debug.gd +++ b/Debug.gd @@ -8,7 +8,7 @@ func _ready(): # Called every frame. 'delta' is the elapsed time since the previous frame. func _process(delta): - pass + $Speed.text = 'Speed: {0}'.format([$Player.velocity.length()]) func _input(event: InputEvent): diff --git a/Player.gd b/Player.gd index a450bef..9a23af1 100644 --- a/Player.gd +++ b/Player.gd @@ -1,14 +1,18 @@ extends CharacterBody3D # Member variables -var walk_speed = 1.0 +const QUAKE = 8 * 0.013888888888888888 +const g = -800 * QUAKE +const MAX_SPEED = 2000 * QUAKE +const FRICTION = 4 * QUAKE +const STOP_SPEED = 100 * QUAKE +const ACCEL= 10 * QUAKE +const FORWARD_SPEED = 200 * QUAKE +const SIDE_SPEED = 350 * QUAKE +const UP_SPEED = 270 * QUAKE -var g = -9.8 -const MAX_SPEED = 5 -const JUMP_SPEED = 5 -const ACCEL= 8 -const DEACCEL= 8 -const MAX_SLOPE_ANGLE = 30 +# const DEACCEL= 8 +# const MAX_SLOPE_ANGLE = 30 @onready var camera = $CSGMesh3D/Camera3D @@ -22,48 +26,115 @@ func _ready(): func _physics_process(delta): - var move_dir = Vector3() # Where does the player intend to walk to - var cam_xform = camera.get_global_transform() + # Shamelessly stolen code from quake :) + + var fmove = 0.0 + var smove = 0.0 + var upmove = 0.0 if Input.is_action_pressed("move_forward"): - move_dir += -cam_xform.basis.z + fmove += -FORWARD_SPEED if Input.is_action_pressed("move_backwards"): - move_dir += cam_xform.basis.z + fmove += FORWARD_SPEED if Input.is_action_pressed("move_left"): - move_dir += -cam_xform.basis.x + smove += -SIDE_SPEED if Input.is_action_pressed("move_right"): - move_dir += cam_xform.basis.x - - if not _noclip: - move_dir.y = 0 - move_dir = move_dir.normalized() - - var target = move_dir * MAX_SPEED + smove += SIDE_SPEED + if Input.is_action_pressed("jump"): + upmove += UP_SPEED if Input.is_action_pressed("run"): - target *= 5 - - var accel - if move_dir.dot(target) > 0: - accel = ACCEL - else: - accel = DEACCEL - - var new_vel = velocity.lerp(target, accel * delta) + fmove *= 2.0 + smove *= 2.0 + upmove *= 2.0 + + var wishvel = Vector3() + var cam_xform: Transform3D = camera.get_global_transform() + var cam_basis: Basis = cam_xform.basis + + wishvel = cam_basis.z * fmove + cam_basis.x * smove - velocity.x = new_vel.x - velocity.z = new_vel.z if _noclip: - velocity.y = new_vel.y - - if not _noclip: - velocity.y += g * delta + wishvel.y = upmove +# else: +# wishvel.y = upmove + + var wishdir = wishvel.normalized() + var wishspeed = wishvel.length() + + if wishspeed > MAX_SPEED: + wishvel *= MAX_SPEED / wishspeed + wishspeed = MAX_SPEED + + if is_on_floor(): + velocity.y += upmove + var vel = velocity + if _noclip: + vel = wishvel + elif is_on_floor(): + vel = _apply_friction(vel, delta) + vel = _apply_accel(vel, wishdir, wishspeed, delta) + else: + vel = _apply_air_accel(vel, wishvel, wishspeed, delta) - if is_on_floor() and Input.is_action_pressed("jump"): - velocity.y += JUMP_SPEED - + if not _noclip: + vel.y += g * delta + + velocity = vel move_and_slide() $Ball.global_position = camera.global_position - camera.global_transform.basis.z * 2.0 + +func _apply_friction(vel: Vector3, delta: float): + var speed = sqrt(vel.x ** 2 + vel.z ** 2) + if(speed == 0.0): return vel + + # TODO: edge detect friction? + var friction = FRICTION + + var control = 0.0 + if speed < STOP_SPEED: + control = STOP_SPEED + else: + control = speed + + var newspeed = speed - delta * control * friction + + if newspeed < 0.0: + newspeed = 0.0 + + newspeed /= speed + + return vel * newspeed + +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 + + var accelspeed = ACCEL * delta * wishspeed + if accelspeed > addspeed: + accelspeed = addspeed + + return vel + accelspeed * wishdir + +func _apply_air_accel(vel: Vector3, wishvel: Vector3, wishspeed: float, delta: float): + var wishveloc = wishvel.normalized() + var wishspd = wishvel.length() + + if wishspd > 30 * QUAKE: + wishspd = 30 * QUAKE + + var currentspeed = vel.dot(wishveloc) + var addspeed = wishspd - currentspeed + if addspeed <= 0: + return vel + + var accelspeed = ACCEL * wishspeed * delta + if accelspeed > addspeed: + accelspeed = addspeed + + return vel + accelspeed * wishveloc func _input(event): if event.is_action_pressed("pause"): diff --git a/world.tscn b/world.tscn index cf6f51b..0ece8bd 100644 --- a/world.tscn +++ b/world.tscn @@ -42,6 +42,7 @@ offset_left = 21.0 offset_top = 2.0 offset_right = 214.0 offset_bottom = 118.0 +text = "KEY OVERLAY" label_settings = SubResource("LabelSettings_1j5am") uppercase = true script = ExtResource("6_crgb3") @@ -50,3 +51,10 @@ script = ExtResource("6_crgb3") [node name="hazardcourse" parent="." instance=ExtResource("8_t5ilw")] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -81.92, 1.28, -16.64) + +[node name="Speed" type="Label" parent="."] +offset_left = 23.0 +offset_top = 99.0 +offset_right = 113.0 +offset_bottom = 136.0 +text = "SPEED"