2023-11-25 12:14:15 +00:00
|
|
|
extends CharacterBody3D
|
2023-11-25 12:24:15 +00:00
|
|
|
class_name Player
|
2023-11-25 12:14:15 +00:00
|
|
|
|
|
|
|
|
2023-11-25 14:41:53 +00:00
|
|
|
@export var input_accel: float = 7.0
|
2023-11-25 12:49:33 +00:00
|
|
|
@onready var current_input_accel = input_accel
|
|
|
|
|
|
|
|
@onready var gravity_accel: Vector3 = ProjectSettings.get_setting("physics/3d/default_gravity") * ProjectSettings.get_setting("physics/3d/default_gravity_vector")
|
2023-11-25 12:55:40 +00:00
|
|
|
@export var jump_impulse: Vector3 = Vector3.UP * 5;
|
2023-11-25 14:41:53 +00:00
|
|
|
@export var friction: float = 0.8
|
2023-11-25 12:49:33 +00:00
|
|
|
|
|
|
|
@export var max_jumps = 1
|
|
|
|
@onready var current_jumps = max_jumps
|
|
|
|
|
2023-11-25 13:14:59 +00:00
|
|
|
@export var collision_normal_max_y_for_floor: float = 0.9
|
|
|
|
|
2023-11-25 12:49:33 +00:00
|
|
|
|
|
|
|
func refill_jumps():
|
|
|
|
current_jumps = max_jumps
|
|
|
|
|
|
|
|
|
|
|
|
func do_jump():
|
|
|
|
current_jumps -= 1
|
|
|
|
velocity += jump_impulse
|
2023-11-25 12:14:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func _physics_process(delta):
|
2023-11-25 12:49:33 +00:00
|
|
|
# Refilla salto
|
|
|
|
if is_on_floor():
|
|
|
|
refill_jumps()
|
|
|
|
|
|
|
|
# Rileva salto
|
|
|
|
if Input.is_action_just_pressed("jump") and current_jumps > 0:
|
|
|
|
do_jump()
|
|
|
|
|
|
|
|
# Applica gravità
|
2023-11-25 12:51:24 +00:00
|
|
|
velocity += gravity_accel * delta
|
2023-11-25 12:49:33 +00:00
|
|
|
|
|
|
|
# Applica input
|
|
|
|
var input_dir = Input.get_vector("move_left", "move_right", "move_up", "move_down")
|
|
|
|
var input_change = input_dir * input_accel
|
2023-11-25 12:51:24 +00:00
|
|
|
velocity.x += input_change.x * delta
|
|
|
|
velocity.z += input_change.y * delta
|
2023-11-25 14:41:53 +00:00
|
|
|
|
|
|
|
if is_on_floor():
|
|
|
|
velocity /= (1 + delta * friction)
|
|
|
|
|
|
|
|
if velocity.length() < 0.3 and input_dir.is_zero_approx():
|
|
|
|
velocity *= 0
|
2023-11-25 12:49:33 +00:00
|
|
|
|
2023-11-25 12:14:15 +00:00
|
|
|
move_and_slide()
|
2023-11-25 13:14:59 +00:00
|
|
|
|
2023-11-25 14:39:37 +00:00
|
|
|
rotation.y = input_dir.normalized().angle_to(Vector2.UP)
|
2023-11-25 14:02:01 +00:00
|
|
|
|
2023-11-25 13:14:59 +00:00
|
|
|
# Gestisci collisioni
|
|
|
|
for collision_idx in range(get_slide_collision_count()):
|
|
|
|
var collision = get_slide_collision(collision_idx)
|
|
|
|
if collision.get_normal().y > collision_normal_max_y_for_floor:
|
|
|
|
continue
|
|
|
|
print("bonk")
|
|
|
|
|
2023-11-25 14:41:53 +00:00
|
|
|
|