2023-04-28 14:19:48 +02:00
|
|
|
using System.Collections.Generic;
|
2023-06-23 15:39:07 +02:00
|
|
|
using System.Diagnostics;
|
2023-11-10 11:11:08 +01:00
|
|
|
using Godot;
|
2023-06-26 22:34:48 +02:00
|
|
|
using GodotComponentTest.components;
|
2023-06-23 15:39:07 +02:00
|
|
|
using GodotComponentTest.entities;
|
2023-07-07 17:27:05 +02:00
|
|
|
using GodotComponentTest.utils;
|
2022-12-02 21:09:40 +01:00
|
|
|
|
2023-11-18 22:32:57 +01:00
|
|
|
public class Player : Entity, IInteractionInterface {
|
2022-12-28 21:57:34 +01:00
|
|
|
// public members
|
2023-11-10 11:11:08 +01:00
|
|
|
[Export] public NodePath WorldNode;
|
2023-01-04 21:29:42 +01:00
|
|
|
|
2024-01-01 15:56:56 +01:00
|
|
|
public int WoodCount;
|
2023-11-10 11:11:08 +01:00
|
|
|
public int GoldCount;
|
2023-02-12 16:30:56 +01:00
|
|
|
public TaskQueueComponent TaskQueueComponent;
|
2023-11-10 11:11:08 +01:00
|
|
|
public NavigationComponent NavigationComponent { get; private set; }
|
|
|
|
public InteractionComponent InteractionComponent { get; set; }
|
2023-01-04 21:29:42 +01:00
|
|
|
|
2024-01-01 15:56:56 +01:00
|
|
|
[Signal]
|
|
|
|
private delegate void WoodCountChanged(int woodCount);
|
|
|
|
|
2023-06-23 15:39:07 +02:00
|
|
|
[Signal]
|
2023-11-10 11:11:08 +01:00
|
|
|
private delegate void GoldCountChanged(int goldCount);
|
2023-08-28 18:22:28 +02:00
|
|
|
|
2022-12-28 21:57:34 +01:00
|
|
|
// private members
|
2022-12-28 16:22:53 +01:00
|
|
|
private WorldInfoComponent _worldInfo;
|
2023-11-10 11:11:08 +01:00
|
|
|
private readonly List<Node> _attractedItemList = new();
|
|
|
|
private AnimationTree _animationTree;
|
|
|
|
private DebugGeometry _debugGeometry;
|
2022-12-28 21:57:34 +01:00
|
|
|
private GroundMotionComponent _groundMotion;
|
2023-04-28 14:19:48 +02:00
|
|
|
private Area _itemAttractorArea;
|
|
|
|
private Area _itemPickupArea;
|
2023-06-23 15:39:07 +02:00
|
|
|
private AnimationPlayer _playerAnimationPlayer;
|
2023-11-10 11:11:08 +01:00
|
|
|
private BoneAttachment _toolAttachement;
|
2022-12-02 21:09:40 +01:00
|
|
|
|
|
|
|
// Called when the node enters the scene tree for the first time.
|
2023-11-18 22:32:57 +01:00
|
|
|
public override void _Ready() {
|
2022-12-28 21:57:34 +01:00
|
|
|
_groundMotion = new GroundMotionComponent();
|
2023-01-04 21:29:42 +01:00
|
|
|
_worldInfo = (WorldInfoComponent)FindNode("WorldInfo", false);
|
2023-11-10 11:11:08 +01:00
|
|
|
NavigationComponent = (NavigationComponent)FindNode("Navigation", false);
|
|
|
|
NavigationComponent.World = GetNode<World>(WorldNode);
|
2023-02-12 16:30:56 +01:00
|
|
|
TaskQueueComponent = new TaskQueueComponent();
|
2023-08-28 18:22:28 +02:00
|
|
|
|
2023-04-28 14:19:48 +02:00
|
|
|
_itemAttractorArea = (Area)FindNode("ItemAttractorArea", false);
|
2023-11-18 22:32:57 +01:00
|
|
|
if (_itemAttractorArea == null) {
|
2023-11-10 11:11:08 +01:00
|
|
|
GD.PushWarning("No ItemAttractorArea node found for " + GetClass());
|
2023-11-18 22:32:57 +01:00
|
|
|
} else {
|
2023-04-28 14:19:48 +02:00
|
|
|
_itemAttractorArea.Connect("body_entered", this, nameof(OnItemAttractorBodyEntered));
|
|
|
|
_itemAttractorArea.Connect("body_exited", this, nameof(OnItemAttractorBodyExited));
|
|
|
|
}
|
2023-08-28 18:22:28 +02:00
|
|
|
|
2023-04-28 14:19:48 +02:00
|
|
|
_itemPickupArea = (Area)FindNode("ItemPickupArea", false);
|
2023-11-18 22:32:57 +01:00
|
|
|
if (_itemPickupArea == null) {
|
2023-11-10 11:11:08 +01:00
|
|
|
GD.PushWarning("No ItemPickupArea node found for " + GetClass());
|
2023-11-18 22:32:57 +01:00
|
|
|
} else {
|
2023-04-28 14:19:48 +02:00
|
|
|
_itemPickupArea.Connect("body_entered", this, nameof(OnItemPickupAreaBodyEntered));
|
2023-11-18 22:32:57 +01:00
|
|
|
}
|
2023-04-28 14:19:48 +02:00
|
|
|
|
2023-06-26 22:34:48 +02:00
|
|
|
_playerAnimationPlayer = GetNode<AnimationPlayer>("Geometry/AnimationPlayer");
|
2023-06-23 15:39:07 +02:00
|
|
|
Debug.Assert(_playerAnimationPlayer != null);
|
2023-06-26 22:34:48 +02:00
|
|
|
_animationTree = GetNode<AnimationTree>("Geometry/AnimationTree");
|
|
|
|
Debug.Assert(_animationTree != null);
|
2023-11-18 22:32:57 +01:00
|
|
|
AnimationNodeStateMachinePlayback stateMachine =
|
2023-08-28 18:22:28 +02:00
|
|
|
(AnimationNodeStateMachinePlayback)_animationTree.Get("parameters/playback");
|
2023-06-26 22:34:48 +02:00
|
|
|
stateMachine.Start("Idle");
|
2023-08-28 18:22:28 +02:00
|
|
|
|
2023-06-23 15:39:07 +02:00
|
|
|
_toolAttachement = (BoneAttachment)FindNode("ToolAttachement");
|
2023-11-18 22:32:57 +01:00
|
|
|
if (_toolAttachement == null) {
|
2023-11-13 20:10:05 +01:00
|
|
|
GD.PushWarning("No ToolAttachement found!");
|
|
|
|
}
|
2023-07-07 17:27:05 +02:00
|
|
|
|
|
|
|
_debugGeometry = (DebugGeometry)FindNode("DebugGeometry");
|
2022-12-02 21:09:40 +01:00
|
|
|
}
|
|
|
|
|
2022-12-28 21:57:34 +01:00
|
|
|
|
2023-11-18 22:32:57 +01:00
|
|
|
public override void _PhysicsProcess(float delta) {
|
2022-12-31 18:36:52 +01:00
|
|
|
base._PhysicsProcess(delta);
|
2023-01-03 17:46:55 +01:00
|
|
|
|
2023-11-18 22:32:57 +01:00
|
|
|
if (NavigationComponent == null) {
|
2023-01-04 21:29:42 +01:00
|
|
|
return;
|
|
|
|
}
|
2023-08-28 18:22:28 +02:00
|
|
|
|
2023-11-18 22:32:57 +01:00
|
|
|
if (TaskQueueComponent != null && TaskQueueComponent.Queue.Count > 0) {
|
2023-02-12 21:10:28 +01:00
|
|
|
TaskQueueComponent.Process(this, delta);
|
|
|
|
|
2023-11-18 22:32:57 +01:00
|
|
|
if (TaskQueueComponent.Queue.Count > 0) {
|
|
|
|
TaskQueueComponent.Task currentTask = TaskQueueComponent.Queue.Peek();
|
2023-07-07 17:27:05 +02:00
|
|
|
TaskQueueComponent.NavigationTask navigationTask = currentTask as TaskQueueComponent.NavigationTask;
|
2023-11-18 22:32:57 +01:00
|
|
|
if (navigationTask != null && navigationTask.PlanningComplete == false) {
|
2023-07-16 20:38:39 +02:00
|
|
|
// _navigationComponent.PlanGridPath(this, GlobalTransform, navigationTask.NavigationPoint);
|
2023-11-10 11:11:08 +01:00
|
|
|
NavigationComponent.PlanSmoothedPath(this, GlobalTransform, navigationTask.NavigationPoint);
|
2023-08-28 18:22:28 +02:00
|
|
|
|
2023-11-10 11:11:08 +01:00
|
|
|
NavigationComponent.ActivatePlannedPath();
|
2023-07-07 17:27:05 +02:00
|
|
|
navigationTask.PlanningComplete = true;
|
2023-02-12 21:10:28 +01:00
|
|
|
}
|
2023-08-28 18:22:28 +02:00
|
|
|
|
2023-11-10 11:11:08 +01:00
|
|
|
NavigationComponent.UpdateCurrentGoal(GlobalTransform);
|
|
|
|
_groundMotion.PhysicsProcess(delta, this, NavigationComponent.CurrentGoalPositionWorld,
|
2023-11-10 12:22:17 +01:00
|
|
|
NavigationComponent.CurrentGoalAngleWorld, _worldInfo.World);
|
2023-11-10 11:11:08 +01:00
|
|
|
|
2023-11-18 22:32:57 +01:00
|
|
|
if (NavigationComponent.IsGoalReached()) {
|
2023-11-10 12:22:17 +01:00
|
|
|
navigationTask.NavigationPoint = new NavigationPoint(GlobalTransform);
|
2023-11-18 22:32:57 +01:00
|
|
|
}
|
2023-02-12 16:30:56 +01:00
|
|
|
}
|
|
|
|
}
|
2023-01-03 17:46:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-11-18 22:32:57 +01:00
|
|
|
public override void _Process(float delta) {
|
|
|
|
if (NavigationComponent != null) {
|
2023-11-10 11:11:08 +01:00
|
|
|
NavigationComponent.UpdateCurrentGoal(GlobalTransform);
|
2023-01-03 17:46:55 +01:00
|
|
|
}
|
2023-08-28 18:22:28 +02:00
|
|
|
|
2023-11-18 22:32:57 +01:00
|
|
|
foreach (Node node in _attractedItemList) {
|
|
|
|
if (node is GoldBar) {
|
2023-08-28 18:22:28 +02:00
|
|
|
GoldBar bar = (GoldBar)node;
|
2023-04-28 14:19:48 +02:00
|
|
|
bar.SetTarget(GlobalTransform.origin);
|
|
|
|
}
|
2024-01-01 15:56:56 +01:00
|
|
|
|
|
|
|
if (node is Wood) {
|
|
|
|
Wood wood = (Wood)node;
|
|
|
|
wood.SetTarget(GlobalTransform.origin);
|
|
|
|
}
|
2023-04-28 14:19:48 +02:00
|
|
|
}
|
2023-08-28 18:22:28 +02:00
|
|
|
|
2023-07-07 17:27:05 +02:00
|
|
|
UpdateDebugGeometry();
|
|
|
|
}
|
|
|
|
|
2023-11-18 22:32:57 +01:00
|
|
|
public void UpdateDebugGeometry() {
|
|
|
|
if (_debugGeometry == null || _debugGeometry.Visible == false) {
|
2023-07-07 17:27:05 +02:00
|
|
|
return;
|
|
|
|
}
|
2023-08-28 18:22:28 +02:00
|
|
|
|
2023-07-07 17:27:05 +02:00
|
|
|
_debugGeometry.Clear();
|
|
|
|
_debugGeometry.GlobalTransform = Transform.Identity;
|
2023-08-28 18:22:28 +02:00
|
|
|
|
2023-11-18 22:32:57 +01:00
|
|
|
if (NavigationComponent != null) {
|
2023-11-10 11:11:08 +01:00
|
|
|
NavigationComponent.DebugDraw(this, _debugGeometry);
|
2023-07-07 17:27:05 +02:00
|
|
|
}
|
2023-04-28 14:19:48 +02:00
|
|
|
}
|
|
|
|
|
2023-11-18 22:32:57 +01:00
|
|
|
public void OnItemAttractorBodyEntered(Node node) {
|
2023-04-28 14:19:48 +02:00
|
|
|
_attractedItemList.Add(node);
|
|
|
|
}
|
2023-08-28 18:22:28 +02:00
|
|
|
|
2023-11-18 22:32:57 +01:00
|
|
|
public void OnItemAttractorBodyExited(Node node) {
|
|
|
|
if (node is GoldBar) {
|
2023-06-23 15:39:07 +02:00
|
|
|
GoldBar bar = (GoldBar)node;
|
2023-04-28 14:19:48 +02:00
|
|
|
bar.UnsetTarget();
|
|
|
|
}
|
2023-08-28 18:22:28 +02:00
|
|
|
|
2024-01-01 15:56:56 +01:00
|
|
|
if (node is Wood) {
|
|
|
|
Wood wood = (Wood)node;
|
|
|
|
wood.UnsetTarget();
|
|
|
|
}
|
|
|
|
|
2023-04-28 14:19:48 +02:00
|
|
|
_attractedItemList.Remove(node);
|
|
|
|
}
|
|
|
|
|
2023-11-18 22:32:57 +01:00
|
|
|
public void OnItemPickupAreaBodyEntered(Node body) {
|
2023-06-23 15:39:07 +02:00
|
|
|
GD.Print("Picking up item: " + body.Name);
|
|
|
|
|
2023-11-18 22:32:57 +01:00
|
|
|
if (body is Axe) {
|
2023-06-23 15:39:07 +02:00
|
|
|
SetActiveTool("Axe");
|
|
|
|
}
|
|
|
|
|
2023-11-18 22:32:57 +01:00
|
|
|
if (body is GoldBar) {
|
2023-07-09 22:17:55 +02:00
|
|
|
GoldCount++;
|
|
|
|
EmitSignal("GoldCountChanged", GoldCount);
|
2024-01-01 15:56:56 +01:00
|
|
|
} else if (body is Wood) {
|
|
|
|
WoodCount++;
|
|
|
|
EmitSignal("WoodCountChanged", WoodCount);
|
2023-04-28 14:19:48 +02:00
|
|
|
}
|
2023-08-28 18:22:28 +02:00
|
|
|
|
2023-06-23 15:39:07 +02:00
|
|
|
body.QueueFree();
|
|
|
|
}
|
|
|
|
|
2023-11-18 22:32:57 +01:00
|
|
|
public void SetActiveTool(string toolName) {
|
2023-06-23 15:39:07 +02:00
|
|
|
Debug.Assert(_toolAttachement != null);
|
2023-11-18 22:32:57 +01:00
|
|
|
if (toolName == "Axe") {
|
2023-06-23 15:39:07 +02:00
|
|
|
_toolAttachement.Visible = true;
|
2023-11-18 22:32:57 +01:00
|
|
|
} else if (toolName == "") {
|
2023-06-23 15:39:07 +02:00
|
|
|
_toolAttachement.Visible = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-18 22:32:57 +01:00
|
|
|
public void OnInteractionStart() {
|
2023-08-28 18:22:28 +02:00
|
|
|
AnimationNodeStateMachinePlayback stateMachine =
|
|
|
|
(AnimationNodeStateMachinePlayback)_animationTree.Get("parameters/playback");
|
2023-06-26 22:34:48 +02:00
|
|
|
Debug.Assert(stateMachine != null);
|
2023-08-28 18:22:28 +02:00
|
|
|
|
2023-11-18 22:32:57 +01:00
|
|
|
if (InteractionComponent.TargetEntity is Chest) {
|
2023-06-26 22:34:48 +02:00
|
|
|
GD.Print("Player Opening Box");
|
|
|
|
stateMachine.Travel("Interaction");
|
2023-11-18 22:32:57 +01:00
|
|
|
} else if (InteractionComponent.TargetEntity is Tree) {
|
2023-06-26 22:34:48 +02:00
|
|
|
GD.Print("Player Chopping Tree");
|
|
|
|
stateMachine.Travel("Hit");
|
|
|
|
}
|
2023-06-23 15:39:07 +02:00
|
|
|
}
|
|
|
|
|
2023-11-18 22:32:57 +01:00
|
|
|
public void OnInteractionEnd() {
|
2023-06-23 15:39:07 +02:00
|
|
|
GD.Print("Player Stopping Interaction");
|
2023-08-28 18:22:28 +02:00
|
|
|
AnimationNodeStateMachinePlayback stateMachine =
|
|
|
|
(AnimationNodeStateMachinePlayback)_animationTree.Get("parameters/playback");
|
2023-06-26 22:34:48 +02:00
|
|
|
Debug.Assert(stateMachine != null);
|
|
|
|
stateMachine.Travel("Idle");
|
2022-12-06 21:06:25 +01:00
|
|
|
}
|
2022-12-28 16:22:53 +01:00
|
|
|
}
|