From d1b46fb9e00541bd58d23313ff3c180484bc0943 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Sat, 24 Feb 2024 15:30:41 +0100 Subject: [PATCH] Added controller activation signal --- client/Main.gd | 2 +- client/enemy/Enemy.gd | 7 +------ client/network/PlayerAdd.gd | 20 +++++++++++--------- client/network/PlayerJump.gd | 5 ++++- client/network/PlayerUseSkill.gd | 5 ++++- client/player/Character.gd | 2 ++ client/player/controller/Controller.gd | 6 +++++- client/player/movement/MovementComponent.gd | 9 ++++++--- client/player/rotation/RotationComponent.gd | 5 ++++- client/player/skills/SkillsComponent.gd | 5 ++++- 10 files changed, 42 insertions(+), 24 deletions(-) diff --git a/client/Main.gd b/client/Main.gd index c6810b3..832f7eb 100644 --- a/client/Main.gd +++ b/client/Main.gd @@ -16,7 +16,7 @@ func _ready(): Global.account.id = "test" Global.account.name = "Local" Global.account.auth_token = "test" - Global.player = %PlayerAdd.spawn_player(Global.account.id) + Global.player = %PlayerAdd.spawn_player(Global.account.id, Global.account.name, Vector3.ZERO) %Login.success.emit() else: %Login.send_login() diff --git a/client/enemy/Enemy.gd b/client/enemy/Enemy.gd index d8742a6..0f201f5 100644 --- a/client/enemy/Enemy.gd +++ b/client/enemy/Enemy.gd @@ -1,7 +1,2 @@ class_name Enemy -extends Character - -func _init(): - controller = EnemyController.new() - add_child(controller) - controller.owner = self \ No newline at end of file +extends Character \ No newline at end of file diff --git a/client/network/PlayerAdd.gd b/client/network/PlayerAdd.gd index 7006a2b..b711ef2 100644 --- a/client/network/PlayerAdd.gd +++ b/client/network/PlayerAdd.gd @@ -21,10 +21,9 @@ func handle_packet(data: PackedByteArray): %Client.log("Add player: %s %s @ %v" % [player_id, player_name, server_position]) - var player := spawn_player(player_id, server_position) - player.set_player_name(player_name) + spawn_player(player_id, player_name, server_position) -func spawn_player(id: String, position: Vector3) -> Player: +func spawn_player(id: String, nick: String, position: Vector3) -> Player: var player: Player if Global.players.has(id): @@ -32,20 +31,23 @@ func spawn_player(id: String, position: Vector3) -> Player: if id == Global.account.id: Global.player = player - + return player - + player = player_scene.instantiate() player.id = id player.position = position + var controller: Controller if id == Global.account.id: - player.controller = PlayerController.new(player) + controller = PlayerController.new(player) Global.player = player main_player_spawned.emit(player) else: - player.controller = ProxyController.new(player) - - player.add_child(player.controller) + controller = ProxyController.new(player) + + player.set_player_name(nick) + player.add_child(controller) Global.players.add(player) + controller.control(player) return player diff --git a/client/network/PlayerJump.gd b/client/network/PlayerJump.gd index 5d2aba2..056d555 100644 --- a/client/network/PlayerJump.gd +++ b/client/network/PlayerJump.gd @@ -4,7 +4,10 @@ func _ready(): %PlayerAdd.main_player_spawned.connect(on_main_player_spawned) func on_main_player_spawned(player: Player): - player.controller.jumped.connect(on_jump) + player.controlled.connect(on_main_player_controlled) + +func on_main_player_controlled(controller: Controller): + controller.jumped.connect(on_jump) func on_jump(): var buffer := StreamPeerBuffer.new() diff --git a/client/network/PlayerUseSkill.gd b/client/network/PlayerUseSkill.gd index 8b4b579..c698ea0 100644 --- a/client/network/PlayerUseSkill.gd +++ b/client/network/PlayerUseSkill.gd @@ -4,7 +4,10 @@ func _ready(): %PlayerAdd.main_player_spawned.connect(on_main_player_spawned) func on_main_player_spawned(player: Player): - player.controller.used_skill.connect(on_skill_used) + player.controlled.connect(on_main_player_controlled) + +func on_main_player_controlled(controller: Controller): + controller.used_skill.connect(on_skill_used) func on_skill_used(slot: int): var buffer := StreamPeerBuffer.new() diff --git a/client/player/Character.gd b/client/player/Character.gd index abe9340..87d4511 100644 --- a/client/player/Character.gd +++ b/client/player/Character.gd @@ -1,4 +1,6 @@ class_name Character extends CharacterBody3D +signal controlled(Controller) + var controller: Controller \ No newline at end of file diff --git a/client/player/controller/Controller.gd b/client/player/controller/Controller.gd index 1d30f67..ad82ed6 100644 --- a/client/player/controller/Controller.gd +++ b/client/player/controller/Controller.gd @@ -3,4 +3,8 @@ extends Node signal direction_changed(direction: Vector3) signal jumped -signal used_skill(slot: int) \ No newline at end of file +signal used_skill(slot: int) + +func control(character: Character): + character.controller = self + character.controlled.emit(self) diff --git a/client/player/movement/MovementComponent.gd b/client/player/movement/MovementComponent.gd index c2cfd10..fa98c01 100644 --- a/client/player/movement/MovementComponent.gd +++ b/client/player/movement/MovementComponent.gd @@ -10,10 +10,13 @@ var direction: Vector3 var gravity: float func _ready(): - 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") + body = owner as Character + body.controlled.connect(on_controlled) + +func on_controlled(controller: Controller): + controller.direction_changed.connect(on_direction_changed) + controller.jumped.connect(jump) func _physics_process(delta): if direction: diff --git a/client/player/rotation/RotationComponent.gd b/client/player/rotation/RotationComponent.gd index 116d5a6..3273b81 100644 --- a/client/player/rotation/RotationComponent.gd +++ b/client/player/rotation/RotationComponent.gd @@ -9,11 +9,14 @@ var angle: float func _ready(): assert(root, "Rotation root needs to be set") - owner.controller.direction_changed.connect(on_direction_changed) + (owner as Character).controlled.connect(on_controlled) func _process(delta): root.rotation.y = Math.damp_angle(root.rotation.y, angle, rotation_speed * delta) +func on_controlled(controller: Controller): + controller.direction_changed.connect(on_direction_changed) + func on_direction_changed(new_direction: Vector3): direction = new_direction diff --git a/client/player/skills/SkillsComponent.gd b/client/player/skills/SkillsComponent.gd index 3e0c255..2832970 100644 --- a/client/player/skills/SkillsComponent.gd +++ b/client/player/skills/SkillsComponent.gd @@ -7,7 +7,10 @@ var character: Character func _ready(): character = owner - character.controller.used_skill.connect(use_skill) + character.controlled.connect(on_controlled) + +func on_controlled(controller: Controller): + controller.used_skill.connect(use_skill) func use_skill(slot: int): if slot < 0 || slot >= skills.size():