GodotComponentTest/entities/Player.cs

80 lines
2.4 KiB
C#

using Godot;
using System;
public class Player : Entity
{
// public members
public Vector3 TargetPosition = Vector3.Zero;
public TaskQueueComponent TaskQueueComponent;
public NavigationComponent Navigation
{
get { return _navigationComponent; }
}
// private members
private MovableComponent _movable;
private Spatial _geometry;
private WorldInfoComponent _worldInfo;
private GroundMotionComponent _groundMotion;
private NavigationComponent _navigationComponent;
// 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();
_movable = (MovableComponent)FindNode("Movable", false);
if (_movable != null)
{
_movable.Connect("OrientationUpdated", this, nameof(OnOrientationUpdated));
}
_geometry = (Spatial)FindNode("Geometry", false);
}
public override void _PhysicsProcess(float delta)
{
base._PhysicsProcess(delta);
if (_navigationComponent == null)
{
return;
}
if (TaskQueueComponent != null && TaskQueueComponent.Queue.Count > 0)
{
var currentTask = TaskQueueComponent.Queue.Peek();
if (currentTask is TaskQueueComponent.NavigationTask)
{
TaskQueueComponent.NavigationTask navigationTask = (TaskQueueComponent.NavigationTask)TaskQueueComponent.Queue.Dequeue();
}
}
_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);
}
}
private void OnOrientationUpdated(float newOrientation)
{
_geometry.Transform = new Transform(new Quat(Vector3.Up, newOrientation), Vector3.Zero);
}
}