GodotComponentTest/entities/Entity.cs

61 lines
1.8 KiB
C#

using System;
using System.Diagnostics;
using Godot;
public class Entity : KinematicBody {
[Flags]
public enum EntityMaskEnum {
Obstacle = 1 << 0,
Ground = 1 << 1,
Water = 1 << 2
}
[Export(PropertyHint.Flags, "Obstacle,Ground,Water")]
public int EntityMask { get; set; }
public Vector3 Velocity { get; set; } = Vector3.Zero;
public float RotationalVelocity { get; set; } = 0;
private CollectibleComponent _collectibleComponent;
public override void _Ready() {
base._Ready();
foreach (Node node in GetChildren()) {
if (node is CollectibleComponent) {
Debug.Assert(_collectibleComponent == null);
_collectibleComponent = node as CollectibleComponent;
}
}
}
public override void _PhysicsProcess(float delta) {
base._PhysicsProcess(delta);
if (_collectibleComponent != null) {
_collectibleComponent.PhysicsProcess(delta, this);
}
}
public CollectibleComponent GetCollectibleComponent() {
return _collectibleComponent;
}
/**
* Defines the angle in plane coordinates, 0 => pointing to the right/east, pi/2 pointing up/north, range [-pi,pi].
*/
public float PlaneAngle {
get => Globals.CalcPlaneAngle(GlobalTransform);
set => GlobalTransform = new Transform(new Basis(Vector3.Up, value + Mathf.Pi * 0.5f), GlobalTranslation);
}
public float CalcShortestPlaneRotationToTargetDirection(Vector3 globalTargetDirection) {
float angleToTarget = Vector3.Right.SignedAngleTo(globalTargetDirection, Vector3.Up);
float currentAngle = PlaneAngle;
float delta = angleToTarget - currentAngle;
delta += delta > Mathf.Pi ? -Mathf.Pi * 2 : delta < -Mathf.Pi ? Mathf.Pi * 2 : 0;
return delta;
}
}