58 lines
1.4 KiB
GDScript
58 lines
1.4 KiB
GDScript
extends CharacterBody3D
|
|
|
|
@export var speed = 14
|
|
@export var fall_acc = 75
|
|
@export var jump_impulse = 50
|
|
@export var bounce_impulse = 40
|
|
|
|
var target_vel = Vector3.ZERO
|
|
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
# Move
|
|
var dir = Vector3.ZERO
|
|
if Input.is_action_pressed("move_left"):
|
|
dir.x -= 1
|
|
if Input.is_action_pressed("move_right"):
|
|
dir.x += 1
|
|
if Input.is_action_pressed("move_forward"):
|
|
dir.z -= 1
|
|
if Input.is_action_pressed("move_back"):
|
|
dir.z += 1
|
|
|
|
if dir != Vector3.ZERO:
|
|
dir = dir.normalized()
|
|
$Pivot.basis = Basis.looking_at(dir)
|
|
|
|
# Ground vel. (overwrite y later)
|
|
target_vel = dir * speed
|
|
|
|
# vert. vel.
|
|
target_vel.y = 0
|
|
if not is_on_floor():
|
|
target_vel.y -= fall_acc * delta
|
|
|
|
# Jump
|
|
if is_on_floor() and Input.is_action_just_pressed("jump"):
|
|
target_vel.y = jump_impulse
|
|
|
|
# Iterate through all collisions that occurred this frame
|
|
for index in range(get_slide_collision_count()):
|
|
var collision = get_slide_collision(index)
|
|
|
|
# Avoid calling collision again after deleting
|
|
if collision.get_collider() == null:
|
|
continue
|
|
|
|
if collision.get_collider().is_in_group("mob"):
|
|
var mob = collision.get_collider()
|
|
# hitting it from above.
|
|
if Vector3.UP.dot(collision.get_normal()) > 0.1:
|
|
mob.squash()
|
|
if !is_on_floor(): # TODO: This does not seem to work here
|
|
target_vel.y = bounce_impulse
|
|
break
|
|
|
|
velocity = target_vel
|
|
move_and_slide()
|