using Godot; using System; using System.Collections.Generic; using System.Linq; public class Player : Entity { // public members public Vector3 TargetPosition = Vector3.Zero; public TaskQueueComponent TaskQueueComponent; public NavigationComponent Navigation { get { return _navigationComponent; } } // private members private WorldInfoComponent _worldInfo; private GroundMotionComponent _groundMotion; private NavigationComponent _navigationComponent; private Area _itemAttractorArea; private Area _itemPickupArea; private List _attractedItemList = new List(); // Called when the node enters the scene tree for the first time. public override void _Ready() { _groundMotion = new GroundMotionComponent(); _worldInfo = (WorldInfoComponent)FindNode("WorldInfo", false); _navigationComponent = (NavigationComponent)FindNode("Navigation", false); _navigationComponent.TileWorld = _worldInfo.TileWorld; TaskQueueComponent = new TaskQueueComponent(); _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)); } } public override void _PhysicsProcess(float delta) { base._PhysicsProcess(delta); if (_navigationComponent == null) { return; } if (TaskQueueComponent != null && TaskQueueComponent.Queue.Count > 0) { TaskQueueComponent.Process(this, delta); if (TaskQueueComponent.Queue.Count > 0) { var currentTask = TaskQueueComponent.Queue.Peek(); if (currentTask is TaskQueueComponent.NavigationTask) { TaskQueueComponent.NavigationTask navigationTask = (TaskQueueComponent.NavigationTask)currentTask; _navigationComponent.PlanDirectPath(GlobalTransform, navigationTask.NavigationPoint); } } } _navigationComponent.UpdateCurrentGoal(GlobalTransform); _groundMotion.PhysicsProcess(delta, this, _navigationComponent.CurrentGoalPositionWorld, _navigationComponent.CurrentGoalOrientationWorld, _worldInfo.TileWorld); } public override void _Process(float delta) { if (_navigationComponent != null) { _navigationComponent.UpdateCurrentGoal(GlobalTransform); } foreach (Node node in _attractedItemList) { GoldBar bar = (GoldBar)node; if (bar != null) { 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); GoldBar bar = (GoldBar)node; if (bar != null) { bar.UnsetTarget(); } _attractedItemList.Remove(node); } public void OnItemPickupAreaBodyEntered(Node body) { GD.Print("Picking up item: " + body); if (body is GoldBar) { GoldBar bar = (GoldBar)body; bar.QueueFree(); } } }