Adds Player controller

This commit is contained in:
Michel 2025-01-31 22:54:00 +01:00
commit ab3890896d
35 changed files with 26403 additions and 0 deletions

View file

@ -0,0 +1,24 @@
extends Camera3D
@export var PlayerNode: Node3D;
@export var turnSpeed: float = 0.25;
@export var XRotationClamp: Vector2 = Vector2(-90, 90)
var currentRotation: Vector2 = Vector2(0,0);
func _ready() -> void:
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED;
func _input(event: InputEvent) -> void:
if event is not InputEventMouseMotion:
return
var inputRotation = event.relative
self.currentRotation += Vector2(inputRotation.y, inputRotation.x) * self.turnSpeed * -1;
self.currentRotation.x = clamp(currentRotation.x, XRotationClamp.x, XRotationClamp.y);
self.rotation_degrees.x = currentRotation.x;
self.PlayerNode.rotation_degrees.y = currentRotation.y

44
scripts/player/player.gd Normal file
View file

@ -0,0 +1,44 @@
extends CharacterBody3D
@onready var animationtree: AnimationTree = $Camera3D/Weapon/AnimationPlayer/AnimationTree
const SPEED: float = 5.0
const FALLOFF_SPEED: float = 0.5
const JUMP_VELOCITY: float = 4.5
var lastDirection = Vector2(0,0)
# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity: float = ProjectSettings.get_setting("physics/3d/default_gravity")
func _physics_process(delta: float) -> void:
# Add the gravity.
if not is_on_floor():
velocity.y -= gravity * delta
# Handle Jump.
if Input.is_action_just_pressed("player_jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var input_dir := Input.get_vector("player_right", "player_left", "player_down", "player_up")
var direction := (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
var falloffSpeed = FALLOFF_SPEED
if not is_on_floor():
falloffSpeed = 0.02;
if direction:
velocity.x = direction.x * SPEED
velocity.z = direction.z * SPEED
else:
velocity.x = move_toward(velocity.x, 0, falloffSpeed)
velocity.z = move_toward(velocity.z, 0, falloffSpeed)
move_and_slide()
lastDirection.x = move_toward(lastDirection.x, input_dir.x, 0.05);
lastDirection.y = move_toward(lastDirection.y, input_dir.y, 0.05);
animationtree.set('parameters/Movement/blend_position', lastDirection);

25
scripts/player/weapon.gd Normal file
View file

@ -0,0 +1,25 @@
extends Node3D
@onready var animationtree: AnimationTree = $AnimationPlayer/AnimationTree
@onready var timer: Timer = $CooldownTimer
var statemachine: AnimationNodeStateMachinePlayback
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
self.statemachine = animationtree['parameters/playback']
func _input(event: InputEvent) -> void:
if event is not InputEventMouseButton:
return
if not event.is_action_pressed('player_shoot'):
return
if self.timer.time_left > 0:
return
self.animationtree.set('parameters/Fire/blend_position', randf_range(0, 2))
self.statemachine.travel('Fire')
self.timer.start()

View file

@ -0,0 +1,3 @@
extends AnimationPlayer
@export var animationTree: AnimationTree;