Improved component system
This commit is contained in:
4
client/player/Character.gd
Normal file
4
client/player/Character.gd
Normal file
@ -0,0 +1,4 @@
|
||||
class_name Character
|
||||
extends CharacterBody3D
|
||||
|
||||
var controller: Controller
|
@ -1,29 +1,10 @@
|
||||
class_name Player
|
||||
extends CharacterBody3D
|
||||
extends Character
|
||||
|
||||
signal skill_used(skill: Skill)
|
||||
signal dashed
|
||||
signal jumped
|
||||
signal name_changed(new_name: String)
|
||||
signal direction_changed
|
||||
|
||||
@export var skills: Array[Skill]
|
||||
|
||||
var id: String
|
||||
var controller: Controller
|
||||
|
||||
func use_skill(slot: int):
|
||||
if get_node("State").current == StateComponent.State.Skill:
|
||||
return
|
||||
|
||||
skill_used.emit(skills[slot])
|
||||
|
||||
func jump():
|
||||
jumped.emit()
|
||||
|
||||
func set_direction(direction: Vector3):
|
||||
direction_changed.emit(direction)
|
||||
|
||||
func set_player_name(new_name: String):
|
||||
name = new_name
|
||||
name_changed.emit(new_name)
|
||||
name_changed.emit(new_name)
|
@ -1,4 +1,4 @@
|
||||
[gd_scene load_steps=31 format=3 uid="uid://2lcnu3dy54lx"]
|
||||
[gd_scene load_steps=29 format=3 uid="uid://2lcnu3dy54lx"]
|
||||
|
||||
[ext_resource type="Script" path="res://player/Player.gd" id="1_8gebs"]
|
||||
[ext_resource type="PackedScene" uid="uid://c8j7t4yg7anb0" path="res://assets/female/Female.blend" id="2_8nah6"]
|
||||
@ -6,9 +6,7 @@
|
||||
[ext_resource type="Resource" uid="uid://yaq8ui3f6fwa" path="res://skill/slash/slash.tres" id="2_x58e1"]
|
||||
[ext_resource type="Resource" uid="uid://ba1filjaldakv" path="res://skill/spin/spin.tres" id="3_l76ly"]
|
||||
[ext_resource type="PackedScene" uid="uid://cgqbkj8wbcatv" path="res://assets/hair/PonyTail.blend" id="3_umw6q"]
|
||||
[ext_resource type="FontFile" uid="uid://b7mov13kwi8u8" path="res://assets/font/ubuntu_nf_regular.ttf" id="4_76ehj"]
|
||||
[ext_resource type="Skin" uid="uid://bbqyiue1vj37f" path="res://assets/hoodie/Hoodie_Skin.tres" id="4_b1tg1"]
|
||||
[ext_resource type="Resource" uid="uid://1vmnijk8ap6b" path="res://skill/thrust/thrust.tres" id="4_s8cf2"]
|
||||
[ext_resource type="ArrayMesh" uid="uid://dbmluwi2atit" path="res://assets/hoodie/Hoodie_Mesh.res" id="5_mkrgn"]
|
||||
[ext_resource type="Resource" uid="uid://cnusbw23jf672" path="res://skill/dash/dash.tres" id="5_pnues"]
|
||||
[ext_resource type="PackedScene" uid="uid://6jpnl6c4fdvo" path="res://player/hud/HUDComponent.tscn" id="7_fwgtd"]
|
||||
@ -38,7 +36,6 @@ height = 1.6
|
||||
collision_layer = 512
|
||||
collision_mask = 769
|
||||
script = ExtResource("1_8gebs")
|
||||
skills = Array[Resource("res://skill/Skill.gd")]([ExtResource("2_x58e1"), ExtResource("3_l76ly"), ExtResource("4_s8cf2"), ExtResource("5_pnues")])
|
||||
|
||||
[node name="Model" type="Node3D" parent="."]
|
||||
|
||||
@ -77,19 +74,6 @@ transform = Transform3D(-4.37114e-08, 1, 0, -1, -4.37114e-08, 0, 0, 0, 1, 0.1976
|
||||
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.8, 0)
|
||||
shape = SubResource("CapsuleShape3D_2f50n")
|
||||
|
||||
[node name="Label" type="Label3D" parent="."]
|
||||
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 2, 0)
|
||||
visible = false
|
||||
pixel_size = 0.0005
|
||||
billboard = 1
|
||||
double_sided = false
|
||||
alpha_antialiasing_mode = 1
|
||||
outline_modulate = Color(0.0627451, 0.0627451, 0.0627451, 0.498039)
|
||||
text = "Player"
|
||||
font = ExtResource("4_76ehj")
|
||||
font_size = 256
|
||||
outline_size = 32
|
||||
|
||||
[node name="HUD" parent="." node_paths=PackedStringArray("health") instance=ExtResource("7_fwgtd")]
|
||||
layers = 512
|
||||
health = NodePath("../Health")
|
||||
@ -121,6 +105,7 @@ libraries = {
|
||||
}
|
||||
|
||||
[node name="Skills" parent="." instance=ExtResource("14_6idcf")]
|
||||
skills = Array[Resource("res://skill/Skill.gd")]([ExtResource("2_x58e1"), ExtResource("3_l76ly"), null, ExtResource("5_pnues")])
|
||||
|
||||
[node name="State" parent="." instance=ExtResource("28_0i0of")]
|
||||
|
||||
|
@ -5,7 +5,7 @@ var player: AnimationPlayer
|
||||
var state: StateComponent
|
||||
|
||||
func _ready():
|
||||
state = owner.find_child("State")
|
||||
state = owner.get_node("State")
|
||||
state.transitioned.connect(on_transition)
|
||||
player = $AnimationPlayer
|
||||
|
||||
|
@ -1,2 +1,6 @@
|
||||
class_name Controller
|
||||
extends Node
|
||||
extends Node
|
||||
|
||||
signal direction_changed(direction: Vector3)
|
||||
signal jumped
|
||||
signal used_skill(slot: int)
|
@ -1,32 +1,34 @@
|
||||
class_name PlayerController
|
||||
extends Controller
|
||||
|
||||
## The character that we're controlling.
|
||||
var player: Player
|
||||
|
||||
## We need the movement component to check if we can perform certain actions.
|
||||
var movement: MovementComponent
|
||||
var skills: SkillsComponent
|
||||
|
||||
func _init(new_player: Player):
|
||||
player = new_player
|
||||
movement = player.find_child("Movement")
|
||||
movement = player.get_node("Movement")
|
||||
skills = player.get_node("Skills")
|
||||
name = "Controller"
|
||||
|
||||
func _unhandled_input(event):
|
||||
func _unhandled_input(event: InputEvent):
|
||||
if Global.interacting_with_ui:
|
||||
return
|
||||
|
||||
# Calculate the direction
|
||||
update_direction()
|
||||
update_actions(event)
|
||||
|
||||
func update_direction():
|
||||
var move := Input.get_vector("move_left", "move_right", "move_forward", "move_backward")
|
||||
var direction := (Global.camera.global_basis * Vector3(move.x, 0, move.y))
|
||||
direction.y = 0
|
||||
direction = direction.normalized()
|
||||
|
||||
# Notify components
|
||||
player.set_direction(direction)
|
||||
direction_changed.emit(direction)
|
||||
|
||||
func update_actions(event: InputEvent):
|
||||
if event.is_action_pressed("jump") && movement && movement.can_jump():
|
||||
player.jump()
|
||||
jumped.emit()
|
||||
|
||||
for i in range(4):
|
||||
if event.is_action_pressed("skill_%d" % (i + 1)):
|
||||
player.use_skill(i)
|
||||
used_skill.emit(i)
|
||||
|
@ -9,6 +9,7 @@ var server_position: Vector3
|
||||
|
||||
func _init(new_player: Player):
|
||||
player = new_player
|
||||
name = "Controller"
|
||||
|
||||
func _ready():
|
||||
server_position = player.position
|
||||
@ -17,9 +18,9 @@ func _process(_delta):
|
||||
var move := server_position - player.position
|
||||
move.y = 0.0
|
||||
|
||||
if move.length_squared() < 0.01:
|
||||
player.set_direction(Vector3.ZERO)
|
||||
if move.length_squared() < 0.02:
|
||||
direction_changed.emit(Vector3.ZERO)
|
||||
return
|
||||
|
||||
var direction := Vector3(move.x, 0, move.z).normalized()
|
||||
player.set_direction(direction)
|
||||
direction_changed.emit(direction)
|
@ -9,7 +9,7 @@ var health: HealthComponent
|
||||
func _ready():
|
||||
health = owner.health
|
||||
health.value_changed.connect(on_health_changed)
|
||||
|
||||
|
||||
func on_health_changed():
|
||||
value = health.value / health.max_value
|
||||
|
||||
|
@ -5,18 +5,14 @@ extends Node
|
||||
@export var jump_velocity := 4.5
|
||||
@export var deceleration := 0.75
|
||||
|
||||
var body: CharacterBody3D
|
||||
var body: Character
|
||||
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
|
||||
body = owner as Character
|
||||
body.controller.direction_changed.connect(on_direction_changed)
|
||||
body.controller.jumped.connect(jump)
|
||||
gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
|
||||
|
||||
func _physics_process(delta):
|
||||
|
@ -9,17 +9,13 @@ 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()
|
||||
owner.controller.direction_changed.connect(on_direction_changed)
|
||||
|
||||
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
|
||||
direction = new_direction
|
||||
|
||||
if direction != Vector3.ZERO:
|
||||
angle = atan2(direction.x, direction.z)
|
@ -1,15 +1,20 @@
|
||||
class_name SkillsComponent
|
||||
extends Node
|
||||
|
||||
var player: Player
|
||||
@export var skills: Array[Skill]
|
||||
|
||||
func _ready():
|
||||
player = owner
|
||||
player.skill_used.connect(use_skill)
|
||||
var character := owner as Character
|
||||
character.controller.used_skill.connect(use_skill)
|
||||
|
||||
func use_skill(slot: int):
|
||||
if slot < 0 || slot >= skills.size():
|
||||
return
|
||||
|
||||
var skill := skills[slot]
|
||||
|
||||
func use_skill(skill: Skill):
|
||||
if !skill:
|
||||
return
|
||||
|
||||
var scene := skill.scene.instantiate()
|
||||
player.add_child(scene)
|
||||
owner.add_child(scene)
|
||||
|
@ -17,7 +17,7 @@ var _current: State
|
||||
|
||||
func _ready():
|
||||
current = State.Idle
|
||||
movement = owner.find_child("Movement")
|
||||
movement = owner.get_node("Movement")
|
||||
|
||||
func _process(_delta):
|
||||
if current == State.Skill:
|
||||
|
Reference in New Issue
Block a user