2022-12-02 21:09:40 +01:00
|
|
|
using Godot;
|
|
|
|
using System;
|
|
|
|
|
|
|
|
public class Player : KinematicBody
|
|
|
|
{
|
2022-12-28 16:22:53 +01:00
|
|
|
// other members
|
2022-12-04 20:51:02 +01:00
|
|
|
private MovableComponent _movable;
|
2022-12-06 21:06:25 +01:00
|
|
|
private Spatial _geometry;
|
2022-12-28 16:22:53 +01:00
|
|
|
private WorldInfoComponent _worldInfo;
|
|
|
|
private Vector2 _offsetCoord = Vector2.Zero;
|
2022-12-02 21:09:40 +01:00
|
|
|
|
|
|
|
// Called when the node enters the scene tree for the first time.
|
|
|
|
public override void _Ready()
|
|
|
|
{
|
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("PositionUpdated", this, nameof(OnPositionUpdated));
|
2022-12-06 21:06:25 +01:00
|
|
|
_movable.Connect("OrientationUpdated", this, nameof(OnOrientationUpdated));
|
2022-12-04 20:51:02 +01:00
|
|
|
}
|
2022-12-06 21:06:25 +01:00
|
|
|
|
|
|
|
_geometry = (Spatial)FindNode("Geometry", false);
|
2022-12-28 16:22:53 +01:00
|
|
|
|
|
|
|
_worldInfo = (WorldInfoComponent)FindNode("WorldInfo", false);
|
2022-12-02 21:09:40 +01:00
|
|
|
}
|
|
|
|
|
2022-12-28 16:22:53 +01:00
|
|
|
public void OnPositionUpdated(Vector3 newPosition)
|
2022-12-04 20:51:02 +01:00
|
|
|
{
|
2022-12-28 16:22:53 +01:00
|
|
|
if (_worldInfo != null)
|
|
|
|
{
|
|
|
|
Vector2 new_offset_coord = _worldInfo.TileWorld.WorldToOffsetCoords(newPosition);
|
|
|
|
if (_offsetCoord != new_offset_coord)
|
|
|
|
{
|
|
|
|
GD.Print("New offset coord " + new_offset_coord);
|
|
|
|
_offsetCoord = new_offset_coord;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_movable != null)
|
|
|
|
{
|
|
|
|
_movable.targetPosition.y = _worldInfo.TileWorld.GetHeightAtOffset(new_offset_coord);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-04 20:51:02 +01:00
|
|
|
Transform transform = Transform;
|
|
|
|
transform.origin = newPosition;
|
|
|
|
Transform = transform;
|
|
|
|
}
|
2022-12-28 16:22:53 +01:00
|
|
|
|
2022-12-06 21:06:25 +01:00
|
|
|
private void OnOrientationUpdated(float newOrientation)
|
|
|
|
{
|
2022-12-28 16:22:53 +01:00
|
|
|
_geometry.Transform = new Transform(new Quat(Vector3.Up, newOrientation), Vector3.Zero);
|
2022-12-06 21:06:25 +01:00
|
|
|
}
|
2022-12-28 16:22:53 +01:00
|
|
|
}
|