A Minecraft style clone in Godot
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

162 lines
5.8 KiB

class_name Player
extends CharacterBody3D
@export var camera_acceleration: float = 10.0 ## The acceleration used when lerping the camera position when crouching and standing
@export var crouch_height: float = 0.8 ## This should be where the head/camera would be located
@export var jump_count: int = 1 ## Number of jumps, reset when on the floor
@export var gravity: float = 9.8
@export var mouse_sensitivity: Vector2 = Vector2(0.1, 0.3) ## Horizontal and vertical mouse sensitivity
@export var standing_height: float = 1.8 ## This should be where the head/camera would be located
@export var throw_velocity: float = 10.0
@export_group("Movement")
@export var acceleration: float = 10.0
@export var crouch_speed: float = 3.0
@export var jump_velocity: float = 4.5
@export var sprint_speed: float = 8.0
@export var sprint_toggle: bool = false ## Enables toggle sprint instead of hold. When enabled, sprinting will also be disabled when velocity is zero
@export var walk_speed: float = 5.0
@onready var camera: Camera3D = $Head/Camera3D
@onready var block_mesh: BlockMesh = $Head/Camera3D/BlockMesh
@onready var head: Node3D = $Head
@onready var collision_shape_standing: CollisionShape3D = $CollisionShapeStanding
@onready var collision_shape_crouching: CollisionShape3D = $CollisionShapeCrouching
@onready var ray_cast_crouch: RayCast3D = $RayCastCrouch
var current_height: float = 0.0
var current_jump_count: int = 0
var current_speed: float = walk_speed
var direction: Vector3 = Vector3.ZERO
var is_crouching: bool = false
var is_sprinting: bool = false
func _input(event: InputEvent) -> void:
if event is InputEventMouseMotion:
_handle_mouse_movement(event.relative)
elif event.is_action_pressed("throw_item"): # TODO: Move to state?
EntityManager.drop_block.emit(InventoryManager.quick_slot_item_id, head.global_position, -head.global_basis.z, throw_velocity)
func _physics_process(delta: float) -> void:
is_crouching = Input.is_action_pressed("crouch") or ray_cast_crouch.is_colliding()
_set_is_sprinting()
_apply_gravity(delta)
_handle_jumping()
_set_current_speed()
_set_direction(delta)
_handle_movement(delta)
_handle_crouching(delta)
move_and_slide()
func _ready() -> void:
GameSettingsManager.graphics_fov_changed.connect(_on_graphics_fov_changed)
GameSettingsManager.game_options_held_block_ui_changed.connect(_on_game_options_held_block_ui_changed)
InventoryManager.quick_slot_item_changed.connect(_on_quick_slot_item_changed)
SignalManager.resume_game.connect(_on_resume_game)
SignalManager.open_pause_menu.connect(_on_open_pause_menu)
SignalManager.hide_ui.connect(_on_hide_ui)
SignalManager.show_ui.connect(_on_show_ui)
_update_held_block_mesh(InventoryManager.quick_slot_item_id)
func _apply_gravity(delta: float) -> void:
if is_on_floor(): return
velocity.y -= gravity * delta
## Toggle the crouching and standing collision shapes[br]
## Set the head height to the crouching or standing height
func _handle_crouching(delta: float) -> void:
collision_shape_crouching.disabled = !is_crouching
collision_shape_standing.disabled = is_crouching
current_height = crouch_height if is_crouching else standing_height
head.position.y = lerp(head.position.y, current_height, camera_acceleration * delta)
func _handle_jumping() -> void:
if is_on_floor():
current_jump_count = 0
if not is_on_floor() and current_jump_count >= jump_count:
return
elif Input.is_action_just_pressed("jump"):
current_jump_count += 1
velocity.y = jump_velocity
## Rotate the player and head to follow mouse movement
func _handle_mouse_movement(mouse_position: Vector2) -> void:
rotate_y(deg_to_rad(-mouse_position.x * mouse_sensitivity.x)) # Rotate player horizontally
head.rotate_x(deg_to_rad(-mouse_position.y * mouse_sensitivity.y)) # Rotate head vertically
head.rotation.x = clamp(head.rotation.x, deg_to_rad(-90), deg_to_rad(90))
func _handle_movement(delta: float) -> void:
if direction:
velocity.x = direction.x * current_speed
velocity.z = direction.z * current_speed
else:
velocity.x = move_toward(velocity.x, 0, current_speed * delta)
velocity.z = move_toward(velocity.z, 0, current_speed * delta)
func _set_current_speed() -> void:
if !is_on_floor(): return # Keep speed in air
if is_crouching:
current_speed = crouch_speed
elif is_sprinting:
current_speed = sprint_speed
else:
current_speed = walk_speed
func _set_direction(delta: float) -> void:
var input_dir: Vector2 = Input.get_vector("move_left", "move_right", "move_forward", "move_backward")
direction = lerp(
direction,
(transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized(),
acceleration * delta
)
func _set_is_sprinting() -> void:
if is_crouching:
is_sprinting = false
elif sprint_toggle:
if Input.is_action_just_pressed("run") and not is_sprinting:
is_sprinting = true # Toggled sprint on
elif Input.is_action_just_pressed("run") and is_sprinting:
is_sprinting = false # Toggled sprint off
elif velocity == Vector3.ZERO:
is_sprinting = false
else:
is_sprinting = Input.is_action_pressed("run")
func _update_held_block_mesh(item_id: String) -> void:
block_mesh.visible = GameSettingsManager.settings.game_options.enable_held_block
if GameSettingsManager.settings.game_options.enable_held_block:
block_mesh.apply_material(DBItems.data[item_id].material_texture)
func _on_game_options_held_block_ui_changed(toggled: bool) -> void:
block_mesh.visible = toggled
func _on_graphics_fov_changed(fov: int) -> void:
camera.fov = fov
func _on_quick_slot_item_changed(item_id: String) -> void:
_update_held_block_mesh(item_id)
func _on_open_pause_menu() -> void:
block_mesh.visible = false
func _on_resume_game() -> void:
block_mesh.visible = GameSettingsManager.settings.game_options.enable_held_block
func _on_hide_ui() -> void:
block_mesh.visible = false
func _on_show_ui() -> void:
block_mesh.visible = true and GameSettingsManager.settings.game_options.enable_held_block