GodotComponentTest/entities/Player.cs

34 lines
955 B
C#

using Godot;
using System;
public class Player : KinematicBody
{
private MovableComponent _movable;
private Spatial _geometry;
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
_movable = (MovableComponent)FindNode("Movable", false);
if (_movable != null)
{
_movable.Connect("PositionUpdated", this, nameof(OnPositionUpdated));
_movable.Connect("OrientationUpdated", this, nameof(OnOrientationUpdated));
}
_geometry = (Spatial)FindNode("Geometry", false);
}
private void OnPositionUpdated(Vector3 newPosition)
{
Transform transform = Transform;
transform.origin = newPosition;
Transform = transform;
}
private void OnOrientationUpdated(float newOrientation)
{
_geometry.Transform = new Transform (new Quat(Vector3.Up, newOrientation), Vector3.Zero);
}
}