71 lines
2.0 KiB
C#
71 lines
2.0 KiB
C#
using Godot;
|
|
using System;
|
|
|
|
public class Player : Entity
|
|
{
|
|
// public members
|
|
public Vector3 TargetPosition = Vector3.Zero;
|
|
|
|
// private members
|
|
private MovableComponent _movable;
|
|
private Spatial _geometry;
|
|
private WorldInfoComponent _worldInfo;
|
|
private Vector2 _offsetCoord = Vector2.Zero;
|
|
private GroundMotionComponent _groundMotion;
|
|
|
|
// Called when the node enters the scene tree for the first time.
|
|
public override void _Ready()
|
|
{
|
|
_groundMotion = new GroundMotionComponent();
|
|
|
|
_movable = (MovableComponent)FindNode("Movable", false);
|
|
if (_movable != null)
|
|
{
|
|
_movable.Connect("OrientationUpdated", this, nameof(OnOrientationUpdated));
|
|
}
|
|
|
|
_geometry = (Spatial)FindNode("Geometry", false);
|
|
|
|
_worldInfo = (WorldInfoComponent)FindNode("WorldInfo", false);
|
|
}
|
|
|
|
|
|
public override void _Process(float _delta)
|
|
{
|
|
base._Process(_delta);
|
|
_groundMotion.Process(_delta, this, TargetPosition);
|
|
}
|
|
|
|
public void OnPositionUpdated(Vector3 newPosition)
|
|
{
|
|
if (_worldInfo != null)
|
|
{
|
|
Vector2 new_offset_coord = _worldInfo.TileWorld.WorldToOffsetCoords(newPosition);
|
|
float tile_height = _worldInfo.TileWorld.GetHeightAtOffset(new_offset_coord);
|
|
if (_offsetCoord != new_offset_coord)
|
|
{
|
|
GD.Print("New offset coord " + new_offset_coord);
|
|
_offsetCoord = new_offset_coord;
|
|
|
|
}
|
|
|
|
if (_movable != null)
|
|
{
|
|
if (_movable.currentPosition.y < tile_height)
|
|
{
|
|
_movable.currentPosition.y = tile_height;
|
|
_movable.currentVelocity.y = 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
Transform transform = Transform;
|
|
transform.origin = newPosition;
|
|
Transform = transform;
|
|
}
|
|
|
|
private void OnOrientationUpdated(float newOrientation)
|
|
{
|
|
_geometry.Transform = new Transform(new Quat(Vector3.Up, newOrientation), Vector3.Zero);
|
|
}
|
|
} |