GodotComponentTest/entities/Player.cs

68 lines
1.9 KiB
C#
Raw Normal View History

2022-12-02 21:09:40 +01:00
using Godot;
using System;
public class Player : Entity
2022-12-02 21:09:40 +01:00
{
// public members
public Vector3 TargetPosition = Vector3.Zero;
public NavigationComponent Navigation
{
get { return _navigationComponent; }
}
// private members
2022-12-04 20:51:02 +01:00
private MovableComponent _movable;
private Spatial _geometry;
private WorldInfoComponent _worldInfo;
private Vector2 _offsetCoord = Vector2.Zero;
private GroundMotionComponent _groundMotion;
private NavigationComponent _navigationComponent;
2022-12-02 21:09:40 +01:00
// 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;
2022-12-04 21:54:44 +01:00
_movable = (MovableComponent)FindNode("Movable", false);
2022-12-04 20:51:02 +01:00
if (_movable != null)
{
_movable.Connect("OrientationUpdated", this, nameof(OnOrientationUpdated));
2022-12-04 20:51:02 +01:00
}
_geometry = (Spatial)FindNode("Geometry", false);
2022-12-02 21:09:40 +01:00
}
2022-12-31 18:36:52 +01:00
public override void _PhysicsProcess(float delta)
{
2022-12-31 18:36:52 +01:00
base._PhysicsProcess(delta);
if (_navigationComponent == null)
{
return;
}
_navigationComponent.UpdateCurrentGoal(GlobalTranslation);
_groundMotion.PhysicsProcess(delta, this, _navigationComponent.CurrentGoalWorld, _worldInfo.TileWorld);
}
public override void _Process(float delta)
{
if (_navigationComponent != null)
{
_navigationComponent.UpdateCurrentGoal(GlobalTranslation);
}
}
2023-01-04 22:49:00 +01:00
private void OnOrientationUpdated(float newOrientation)
{
_geometry.Transform = new Transform(new Quat(Vector3.Up, newOrientation), Vector3.Zero);
}
}