2022-12-02 21:09:40 +01:00
|
|
|
using Godot;
|
|
|
|
using System;
|
2023-04-28 14:19:48 +02:00
|
|
|
using System.Collections.Generic;
|
2023-06-23 15:39:07 +02:00
|
|
|
using System.Diagnostics;
|
2023-04-28 14:19:48 +02:00
|
|
|
using System.Linq;
|
2023-06-26 22:34:48 +02:00
|
|
|
using GodotComponentTest.components;
|
2023-06-23 15:39:07 +02:00
|
|
|
using GodotComponentTest.entities;
|
2022-12-02 21:09:40 +01:00
|
|
|
|
2023-06-23 15:39:07 +02:00
|
|
|
public class Player : Entity, IInteractionInterface
|
2022-12-02 21:09:40 +01:00
|
|
|
{
|
2022-12-28 21:57:34 +01:00
|
|
|
// public members
|
|
|
|
public Vector3 TargetPosition = Vector3.Zero;
|
2023-06-23 15:39:07 +02:00
|
|
|
public int goldCount = 0;
|
2023-01-04 21:29:42 +01:00
|
|
|
|
2023-02-12 16:30:56 +01:00
|
|
|
public TaskQueueComponent TaskQueueComponent;
|
|
|
|
|
2023-01-04 21:29:42 +01:00
|
|
|
public NavigationComponent Navigation
|
|
|
|
{
|
|
|
|
get { return _navigationComponent; }
|
|
|
|
}
|
|
|
|
|
2023-06-23 15:39:07 +02:00
|
|
|
[Signal]
|
|
|
|
delegate void GoldCountChanged(int goldCount);
|
|
|
|
|
2022-12-28 21:57:34 +01:00
|
|
|
// private members
|
2022-12-28 16:22:53 +01:00
|
|
|
private WorldInfoComponent _worldInfo;
|
2022-12-28 21:57:34 +01:00
|
|
|
private GroundMotionComponent _groundMotion;
|
2023-01-03 17:46:55 +01:00
|
|
|
private NavigationComponent _navigationComponent;
|
2023-04-28 14:19:48 +02:00
|
|
|
private Area _itemAttractorArea;
|
|
|
|
private Area _itemPickupArea;
|
|
|
|
private List<Node> _attractedItemList = new List<Node>();
|
2023-06-23 15:39:07 +02:00
|
|
|
private BoneAttachment _toolAttachement;
|
|
|
|
private AnimationPlayer _playerAnimationPlayer;
|
2023-06-26 22:34:48 +02:00
|
|
|
private AnimationTree _animationTree;
|
|
|
|
private InteractionComponent _interactionComponent;
|
|
|
|
public InteractionComponent InteractionComponent
|
|
|
|
{
|
|
|
|
get => _interactionComponent;
|
|
|
|
set => _interactionComponent = value;
|
|
|
|
}
|
2022-12-02 21:09:40 +01:00
|
|
|
|
|
|
|
// Called when the node enters the scene tree for the first time.
|
|
|
|
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);
|
|
|
|
_navigationComponent = (NavigationComponent)FindNode("Navigation", false);
|
|
|
|
_navigationComponent.TileWorld = _worldInfo.TileWorld;
|
2023-02-12 16:30:56 +01:00
|
|
|
TaskQueueComponent = new TaskQueueComponent();
|
2023-04-28 14:19:48 +02:00
|
|
|
|
|
|
|
_itemAttractorArea = (Area)FindNode("ItemAttractorArea", false);
|
|
|
|
if (_itemAttractorArea == null)
|
|
|
|
{
|
|
|
|
GD.PushWarning("No ItemAttractorArea node found for " + this.GetClass());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_itemAttractorArea.Connect("body_entered", this, nameof(OnItemAttractorBodyEntered));
|
|
|
|
_itemAttractorArea.Connect("body_exited", this, nameof(OnItemAttractorBodyExited));
|
|
|
|
}
|
|
|
|
|
|
|
|
_itemPickupArea = (Area)FindNode("ItemPickupArea", false);
|
|
|
|
if (_itemPickupArea == null)
|
|
|
|
{
|
|
|
|
GD.PushWarning("No ItemPickupArea node found for " + this.GetClass());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_itemPickupArea.Connect("body_entered", this, nameof(OnItemPickupAreaBodyEntered));
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
AnimationNodeStateMachinePlayback stateMachine = (AnimationNodeStateMachinePlayback)_animationTree.Get("parameters/playback");
|
|
|
|
stateMachine.Start("Idle");
|
2023-06-23 15:39:07 +02:00
|
|
|
|
|
|
|
_toolAttachement = (BoneAttachment)FindNode("ToolAttachement");
|
|
|
|
if (_toolAttachement == null)
|
|
|
|
{
|
|
|
|
GD.PushWarning("No ToolAttachement found!");
|
|
|
|
}
|
2022-12-02 21:09:40 +01:00
|
|
|
}
|
|
|
|
|
2022-12-28 21:57:34 +01:00
|
|
|
|
2022-12-31 18:36:52 +01:00
|
|
|
public override void _PhysicsProcess(float delta)
|
2022-12-28 21:57:34 +01:00
|
|
|
{
|
2022-12-31 18:36:52 +01:00
|
|
|
base._PhysicsProcess(delta);
|
2023-01-03 17:46:55 +01:00
|
|
|
|
2023-01-04 21:29:42 +01:00
|
|
|
if (_navigationComponent == null)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2023-01-03 17:46:55 +01:00
|
|
|
|
2023-02-12 16:30:56 +01:00
|
|
|
if (TaskQueueComponent != null && TaskQueueComponent.Queue.Count > 0)
|
|
|
|
{
|
2023-02-12 21:10:28 +01:00
|
|
|
TaskQueueComponent.Process(this, delta);
|
|
|
|
|
|
|
|
if (TaskQueueComponent.Queue.Count > 0)
|
2023-02-12 16:30:56 +01:00
|
|
|
{
|
2023-02-12 21:10:28 +01:00
|
|
|
var currentTask = TaskQueueComponent.Queue.Peek();
|
|
|
|
if (currentTask is TaskQueueComponent.NavigationTask)
|
|
|
|
{
|
|
|
|
TaskQueueComponent.NavigationTask navigationTask = (TaskQueueComponent.NavigationTask)currentTask;
|
2023-05-03 12:10:01 +02:00
|
|
|
_navigationComponent.PlanDirectPath(GlobalTransform, navigationTask.NavigationPoint);
|
2023-02-12 21:10:28 +01:00
|
|
|
}
|
2023-02-12 16:30:56 +01:00
|
|
|
}
|
|
|
|
}
|
2023-01-03 17:46:55 +01:00
|
|
|
|
2023-02-12 16:30:56 +01:00
|
|
|
_navigationComponent.UpdateCurrentGoal(GlobalTransform);
|
|
|
|
_groundMotion.PhysicsProcess(delta, this, _navigationComponent.CurrentGoalPositionWorld, _navigationComponent.CurrentGoalOrientationWorld, _worldInfo.TileWorld);
|
2023-01-03 17:46:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void _Process(float delta)
|
|
|
|
{
|
2023-01-04 21:29:42 +01:00
|
|
|
if (_navigationComponent != null)
|
2023-01-03 17:46:55 +01:00
|
|
|
{
|
2023-02-12 16:30:56 +01:00
|
|
|
_navigationComponent.UpdateCurrentGoal(GlobalTransform);
|
2023-01-03 17:46:55 +01:00
|
|
|
}
|
2023-04-28 14:19:48 +02:00
|
|
|
|
|
|
|
foreach (Node node in _attractedItemList)
|
|
|
|
{
|
2023-06-23 15:39:07 +02:00
|
|
|
if (node is GoldBar)
|
2023-04-28 14:19:48 +02:00
|
|
|
{
|
2023-06-23 15:39:07 +02:00
|
|
|
GoldBar bar = (GoldBar)node;
|
2023-04-28 14:19:48 +02:00
|
|
|
bar.SetTarget(GlobalTransform.origin);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void OnItemAttractorBodyEntered(Node node)
|
|
|
|
{
|
|
|
|
GD.Print("Item entered " + node);
|
|
|
|
_attractedItemList.Add(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void OnItemAttractorBodyExited(Node node)
|
|
|
|
{
|
|
|
|
GD.Print("Item exited " + node);
|
2023-06-23 15:39:07 +02:00
|
|
|
|
|
|
|
if (node is GoldBar)
|
2023-04-28 14:19:48 +02:00
|
|
|
{
|
2023-06-23 15:39:07 +02:00
|
|
|
GoldBar bar = (GoldBar)node;
|
2023-04-28 14:19:48 +02:00
|
|
|
bar.UnsetTarget();
|
|
|
|
}
|
|
|
|
_attractedItemList.Remove(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void OnItemPickupAreaBodyEntered(Node body)
|
|
|
|
{
|
2023-06-23 15:39:07 +02:00
|
|
|
GD.Print("Picking up item: " + body.Name);
|
|
|
|
|
|
|
|
if (body is Axe)
|
|
|
|
{
|
|
|
|
SetActiveTool("Axe");
|
|
|
|
}
|
|
|
|
|
2023-04-28 14:19:48 +02:00
|
|
|
if (body is GoldBar)
|
|
|
|
{
|
2023-06-23 15:39:07 +02:00
|
|
|
goldCount++;
|
|
|
|
EmitSignal("GoldCountChanged", goldCount);
|
2023-04-28 14:19:48 +02:00
|
|
|
}
|
2023-06-23 15:39:07 +02:00
|
|
|
|
|
|
|
body.QueueFree();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetActiveTool(String toolName)
|
|
|
|
{
|
|
|
|
Debug.Assert(_toolAttachement != null);
|
|
|
|
if (toolName == "Axe")
|
|
|
|
{
|
|
|
|
_toolAttachement.Visible = true;
|
|
|
|
} else if (toolName == "")
|
|
|
|
{
|
|
|
|
_toolAttachement.Visible = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void OnInteractionStart()
|
|
|
|
{
|
2023-06-26 22:34:48 +02:00
|
|
|
AnimationNodeStateMachinePlayback stateMachine = (AnimationNodeStateMachinePlayback)_animationTree.Get("parameters/playback");
|
|
|
|
Debug.Assert(stateMachine != null);
|
|
|
|
|
|
|
|
if (_interactionComponent.TargetEntity is Chest)
|
|
|
|
{
|
|
|
|
GD.Print("Player Opening Box");
|
|
|
|
stateMachine.Travel("Interaction");
|
|
|
|
} else if (_interactionComponent.TargetEntity is Tree)
|
|
|
|
{
|
|
|
|
GD.Print("Player Chopping Tree");
|
|
|
|
stateMachine.Travel("Hit");
|
|
|
|
}
|
2023-06-23 15:39:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void OnInteractionEnd()
|
|
|
|
{
|
|
|
|
GD.Print("Player Stopping Interaction");
|
2023-06-26 22:34:48 +02:00
|
|
|
AnimationNodeStateMachinePlayback stateMachine = (AnimationNodeStateMachinePlayback)_animationTree.Get("parameters/playback");
|
|
|
|
Debug.Assert(stateMachine != null);
|
|
|
|
stateMachine.Travel("Idle");
|
2022-12-06 21:06:25 +01:00
|
|
|
}
|
2022-12-28 16:22:53 +01:00
|
|
|
}
|