Improved component system

This commit is contained in:
2024-02-07 23:04:19 +01:00
parent 97087ad03e
commit c4a9da0880
22 changed files with 263 additions and 188 deletions

View File

@ -1,48 +1,2 @@
class_name Character
extends CharacterBody3D
signal jumped
@export var model: Node3D
@export var move_speed: float = 4.5
@export var rotation_speed: float = 20.0
const JUMP_VELOCITY := 4.5
const DECELERATE := 0.75
var direction: Vector3
var angle: float
var gravity: float = ProjectSettings.get_setting("physics/3d/default_gravity")
var controller: Node
func _process(delta):
if direction != Vector3.ZERO:
angle = atan2(direction.x, direction.z)
model.rotation.y = lerp_angle(model.rotation.y, angle, rotation_speed * delta)
func _physics_process(delta):
if direction:
velocity.x = direction.x * move_speed
velocity.z = direction.z * move_speed
else:
velocity.x *= DECELERATE
velocity.z *= DECELERATE
if !is_on_floor():
velocity.y -= gravity * delta
move_and_slide()
func jump():
if !is_on_floor():
return
velocity.y = JUMP_VELOCITY
jumped.emit()
func dash():
print("dash")
func attack():
print("attack")
extends CharacterBody3D

View File

@ -0,0 +1,55 @@
class_name AnimationComponent
extends Node
const RESET = "human/RESET"
var animation_player: AnimationPlayer
var movement: MovementComponent
var next_animation: StringName = RESET
var skip: int
func _ready():
var player := owner as Player
player.dashed.connect(dash)
player.attacked.connect(attack)
movement = player.find_child("Movement")
animation_player = $AnimationPlayer
func _process(_delta):
if skip == 0:
play_movement()
if animation_player.current_animation == next_animation:
return
animation_player.play(next_animation)
func play_movement():
if !movement:
play(RESET)
return
if movement.body.velocity.y > 0:
play("human/jump")
elif movement.body.velocity.y < 0:
play("human/fall")
elif movement.direction != Vector3.ZERO:
play("human/run-fast")
else:
play("human/idle")
func dash():
play_with_duration("human/roll", 1.0)
func attack():
play_with_duration("human/slash", 1.0)
func play(action_name: StringName):
next_animation = action_name
func play_with_duration(action_name: StringName, duration: float):
next_animation = action_name
skip += 1
await get_tree().create_timer(duration).timeout
skip -= 1

View File

@ -0,0 +1,9 @@
[gd_scene load_steps=2 format=3 uid="uid://bivxnxwi863o0"]
[ext_resource type="Script" path="res://character/animation/AnimationComponent.gd" id="1_lenw3"]
[node name="Animation" type="Node"]
script = ExtResource("1_lenw3")
[node name="AnimationPlayer" type="AnimationPlayer" parent="."]
playback_default_blend_time = 0.2

View File

@ -2,10 +2,11 @@ class_name HealthComponent
extends Node
@export var max_health: float
var health: float
func _ready():
pass
health = max_health
func take_damage(attack: Attack):
health -= attack.damage

View File

@ -2,5 +2,5 @@
[ext_resource type="Script" path="res://character/health/HealthComponent.gd" id="1_403dm"]
[node name="HealthComponent" type="Node"]
[node name="Health" type="Node"]
script = ExtResource("1_403dm")

View File

@ -0,0 +1,42 @@
class_name MovementComponent
extends Node
@export var move_speed := 4.5
@export var jump_velocity := 4.5
@export var deceleration := 0.75
var body: CharacterBody3D
var direction: Vector3
var gravity: float
func _ready():
if owner.has_signal("direction_changed"):
owner.direction_changed.connect(on_direction_changed)
if owner.has_signal("jumped"):
owner.jumped.connect(jump)
body = owner as CharacterBody3D
gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
func _physics_process(delta):
if direction:
body.velocity.x = direction.x * move_speed
body.velocity.z = direction.z * move_speed
else:
body.velocity.x *= deceleration
body.velocity.z *= deceleration
if !body.is_on_floor():
body.velocity.y -= gravity * delta
body.move_and_slide()
func on_direction_changed(new_direction: Vector3):
direction = new_direction
func can_jump() -> bool:
return body.is_on_floor()
func jump():
body.velocity.y = jump_velocity

View File

@ -0,0 +1,6 @@
[gd_scene load_steps=2 format=3 uid="uid://x102pryt2s5a"]
[ext_resource type="Script" path="res://character/movement/MovementComponent.gd" id="1_2gnmd"]
[node name="Movement" type="Node"]
script = ExtResource("1_2gnmd")

View File

@ -0,0 +1,25 @@
class_name RotationComponent
extends Node
@export var root: Node3D
@export var rotation_speed: float = 20.0
var direction: Vector3
var angle: float
func _ready():
assert(root, "Rotation root needs to be set")
if owner.has_signal("direction_changed"):
owner.direction_changed.connect(on_direction_changed)
update_configuration_warnings()
func _process(delta):
if direction != Vector3.ZERO:
angle = atan2(direction.x, direction.z)
root.rotation.y = lerp_angle(root.rotation.y, angle, rotation_speed * delta)
func on_direction_changed(new_direction: Vector3):
direction = new_direction

View File

@ -0,0 +1,6 @@
[gd_scene load_steps=2 format=3 uid="uid://d0onbq0ad1ap4"]
[ext_resource type="Script" path="res://character/rotation/RotationComponent.gd" id="1_j1874"]
[node name="Rotation" type="Node"]
script = ExtResource("1_j1874")