First draft Quake movement

Pure shit
This commit is contained in:
Thomas Muller 2023-10-01 21:44:21 -04:00
parent a627998aed
commit ead2b57917
4 changed files with 118 additions and 39 deletions

View file

@ -33,7 +33,7 @@ size = Vector3(1, 1.5, 1)
script = ExtResource("1_omu2m") script = ExtResource("1_omu2m")
[node name="Koob" type="CSGMesh3D" parent="."] [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") mesh = SubResource("BoxMesh_sbsxh")
material = SubResource("StandardMaterial3D_8bjqg") material = SubResource("StandardMaterial3D_8bjqg")

View file

@ -8,7 +8,7 @@ func _ready():
# Called every frame. 'delta' is the elapsed time since the previous frame. # Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta): func _process(delta):
pass $Speed.text = 'Speed: {0}'.format([$Player.velocity.length()])
func _input(event: InputEvent): func _input(event: InputEvent):

137
Player.gd
View file

@ -1,14 +1,18 @@
extends CharacterBody3D extends CharacterBody3D
# Member variables # 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 DEACCEL= 8
const MAX_SPEED = 5 # const MAX_SLOPE_ANGLE = 30
const JUMP_SPEED = 5
const ACCEL= 8
const DEACCEL= 8
const MAX_SLOPE_ANGLE = 30
@onready @onready
var camera = $CSGMesh3D/Camera3D var camera = $CSGMesh3D/Camera3D
@ -22,49 +26,116 @@ func _ready():
func _physics_process(delta): func _physics_process(delta):
var move_dir = Vector3() # Where does the player intend to walk to # Shamelessly stolen code from quake :)
var cam_xform = camera.get_global_transform()
var fmove = 0.0
var smove = 0.0
var upmove = 0.0
if Input.is_action_pressed("move_forward"): if Input.is_action_pressed("move_forward"):
move_dir += -cam_xform.basis.z fmove += -FORWARD_SPEED
if Input.is_action_pressed("move_backwards"): if Input.is_action_pressed("move_backwards"):
move_dir += cam_xform.basis.z fmove += FORWARD_SPEED
if Input.is_action_pressed("move_left"): if Input.is_action_pressed("move_left"):
move_dir += -cam_xform.basis.x smove += -SIDE_SPEED
if Input.is_action_pressed("move_right"): if Input.is_action_pressed("move_right"):
move_dir += cam_xform.basis.x smove += SIDE_SPEED
if Input.is_action_pressed("jump"):
if not _noclip: upmove += UP_SPEED
move_dir.y = 0
move_dir = move_dir.normalized()
var target = move_dir * MAX_SPEED
if Input.is_action_pressed("run"): if Input.is_action_pressed("run"):
target *= 5 fmove *= 2.0
smove *= 2.0
upmove *= 2.0
var accel var wishvel = Vector3()
if move_dir.dot(target) > 0: var cam_xform: Transform3D = camera.get_global_transform()
accel = ACCEL var cam_basis: Basis = cam_xform.basis
else:
accel = DEACCEL
var new_vel = velocity.lerp(target, accel * delta) wishvel = cam_basis.z * fmove + cam_basis.x * smove
velocity.x = new_vel.x
velocity.z = new_vel.z
if _noclip: if _noclip:
velocity.y = new_vel.y 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 not _noclip: if not _noclip:
velocity.y += g * delta vel.y += g * delta
if is_on_floor() and Input.is_action_pressed("jump"):
velocity.y += JUMP_SPEED
velocity = vel
move_and_slide() move_and_slide()
$Ball.global_position = camera.global_position - camera.global_transform.basis.z * 2.0 $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): func _input(event):
if event.is_action_pressed("pause"): if event.is_action_pressed("pause"):
_paused = not _paused _paused = not _paused

View file

@ -42,6 +42,7 @@ offset_left = 21.0
offset_top = 2.0 offset_top = 2.0
offset_right = 214.0 offset_right = 214.0
offset_bottom = 118.0 offset_bottom = 118.0
text = "KEY OVERLAY"
label_settings = SubResource("LabelSettings_1j5am") label_settings = SubResource("LabelSettings_1j5am")
uppercase = true uppercase = true
script = ExtResource("6_crgb3") script = ExtResource("6_crgb3")
@ -50,3 +51,10 @@ script = ExtResource("6_crgb3")
[node name="hazardcourse" parent="." instance=ExtResource("8_t5ilw")] [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) 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"