Towing
Created control module abstraction, vastly simplifying current and future control module code Created context menu option for creating towing tasks
This commit is contained in:
parent
698263ce63
commit
139c593ccd
21 changed files with 685 additions and 291 deletions
|
|
@ -52,7 +52,7 @@ public interface IController
|
|||
bool Available { get; }
|
||||
TaskType TaskType { get; }
|
||||
Guid Zone { get; }
|
||||
void AssignTask(Guid task, List<SimplifiedZoneData> path);
|
||||
void AssignTask(Guid task);
|
||||
}
|
||||
|
||||
[MessagePackObject,
|
||||
|
|
|
|||
|
|
@ -3,6 +3,9 @@ using System.Collections;
|
|||
using System.Collections.Generic;
|
||||
using MessagePack;
|
||||
using Newtonsoft.Json;
|
||||
using Unity.Mathematics;
|
||||
using static Unity.Mathematics.math;
|
||||
using float2 = Unity.Mathematics.float2;
|
||||
|
||||
[Union(0, typeof(TowingControllerData))]
|
||||
public abstract class ControllerData : BehaviorData
|
||||
|
|
@ -18,27 +21,171 @@ public abstract class ControllerData : BehaviorData
|
|||
|
||||
[InspectableField, JsonProperty("tangentSensitivity"), Key(4)]
|
||||
public float TangentSensitivity = 4;
|
||||
|
||||
[InspectableField, JsonProperty("targetDistance"), Key(5)]
|
||||
public float TargetDistance = 10;
|
||||
}
|
||||
|
||||
// public abstract class Controller : IController
|
||||
// {
|
||||
// public abstract bool Available { get; }
|
||||
// public abstract TaskType TaskType { get; }
|
||||
// public Guid Zone { get; }
|
||||
//
|
||||
// private GameContext _context;
|
||||
// private Entity _entity;
|
||||
// private Guid _task;
|
||||
// private List<SimplifiedZoneData> _path;
|
||||
//
|
||||
// public Controller(GameContext context, Entity entity)
|
||||
// {
|
||||
//
|
||||
// }
|
||||
//
|
||||
// public void AssignTask(Guid task, List<SimplifiedZoneData> path)
|
||||
// {
|
||||
// _task = task;
|
||||
// _path = path;
|
||||
// }
|
||||
// }
|
||||
public abstract class ControllerBase : IBehavior, IController, IInitializableBehavior
|
||||
{
|
||||
public Guid HomeEntity;
|
||||
public bool Available => Task == Guid.Empty;
|
||||
public abstract TaskType TaskType { get; }
|
||||
public Guid Zone => _entity.Zone;
|
||||
public BehaviorData Data => _controllerData;
|
||||
|
||||
protected Locomotion Locomotion;
|
||||
protected VelocityMatch VelocityMatch;
|
||||
protected Func<float2> TargetPosition;
|
||||
protected Func<float2> TargetVelocity;
|
||||
protected bool MatchVelocity;
|
||||
protected List<SimplifiedZoneData> Path;
|
||||
protected Guid Task;
|
||||
|
||||
private GameContext _context;
|
||||
private Entity _entity;
|
||||
private ControllerData _controllerData;
|
||||
private MovementPhase _movementPhase;
|
||||
private bool _moving;
|
||||
private Action _onFinishMoving;
|
||||
|
||||
public ControllerBase(GameContext context, ControllerData data, Entity entity)
|
||||
{
|
||||
_context = context;
|
||||
_controllerData = data;
|
||||
_entity = entity;
|
||||
}
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
Locomotion = new Locomotion(_context, _entity, _controllerData);
|
||||
VelocityMatch = new VelocityMatch(_context, _entity, _controllerData);
|
||||
|
||||
_context.CorporationControllers[_entity.Corporation].Add(this);
|
||||
}
|
||||
|
||||
public void AssignTask(Guid task)
|
||||
{
|
||||
Task = task;
|
||||
_moving = false;
|
||||
}
|
||||
|
||||
public bool Update(float delta)
|
||||
{
|
||||
if (Available)
|
||||
{
|
||||
// No task is assigned and we're at home, do nothing!
|
||||
if (_entity.Parent == HomeEntity)
|
||||
return false;
|
||||
|
||||
// No task is assigned, but we're not home, go home!
|
||||
var homeEntity = _context.Cache.Get<Entity>(HomeEntity);
|
||||
if (!_moving)
|
||||
{
|
||||
if (Zone == homeEntity.Zone) MoveTo(homeEntity, true, () =>
|
||||
_context.SetParent(_entity, homeEntity));
|
||||
else MoveTo(homeEntity.Zone, () => MoveTo(homeEntity, true, () =>
|
||||
_context.SetParent(_entity, homeEntity)));
|
||||
}
|
||||
}
|
||||
|
||||
if (_moving)
|
||||
{
|
||||
var distance = length(TargetPosition() - _entity.Position);
|
||||
if (MatchVelocity)
|
||||
{
|
||||
VelocityMatch.TargetVelocity = TargetVelocity();
|
||||
if (_movementPhase == MovementPhase.Locomotion)
|
||||
{
|
||||
var matchDistanceTime = VelocityMatch.MatchDistanceTime;
|
||||
Locomotion.Objective = TargetPosition() + TargetVelocity() * matchDistanceTime.y;
|
||||
Locomotion.Update(delta);
|
||||
|
||||
if (distance < matchDistanceTime.x)
|
||||
{
|
||||
_movementPhase = MovementPhase.Slowdown;
|
||||
_context.Log($"Controller {_entity.Name} has entered slowdown phase.");
|
||||
VelocityMatch.Clear();
|
||||
VelocityMatch.OnMatch += () =>
|
||||
{
|
||||
_moving = false;
|
||||
_onFinishMoving();
|
||||
};
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
VelocityMatch.Update(delta);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Locomotion.Objective = TargetPosition();
|
||||
Locomotion.Update(delta);
|
||||
if (distance < _controllerData.TargetDistance)
|
||||
{
|
||||
_moving = false;
|
||||
_onFinishMoving();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void MoveTo(Entity entity, bool matchVelocity = true, Action onFinish = null)
|
||||
{
|
||||
TargetPosition = () => entity.Position;
|
||||
TargetVelocity = () => entity.Velocity;
|
||||
MatchVelocity = matchVelocity;
|
||||
_movementPhase = MovementPhase.Locomotion;
|
||||
_moving = true;
|
||||
_onFinishMoving = onFinish;
|
||||
}
|
||||
|
||||
public void MoveTo(float2 position, bool matchVelocity = true, Action onFinish = null)
|
||||
{
|
||||
TargetPosition = () => position;
|
||||
TargetVelocity = () => float2.zero;
|
||||
MatchVelocity = matchVelocity;
|
||||
_movementPhase = MovementPhase.Locomotion;
|
||||
_moving = true;
|
||||
_onFinishMoving = onFinish;
|
||||
}
|
||||
|
||||
public void MoveTo(Func<float2> position, Func<float2> velocity, Action onFinish = null)
|
||||
{
|
||||
TargetPosition = position;
|
||||
TargetVelocity = velocity;
|
||||
MatchVelocity = true;
|
||||
_movementPhase = MovementPhase.Locomotion;
|
||||
_moving = true;
|
||||
_onFinishMoving = onFinish;
|
||||
}
|
||||
|
||||
public void MoveTo(Guid zone, Action onFinish = null)
|
||||
{
|
||||
var path = _context.FindPath(_context.GalaxyZones[Zone], _context.GalaxyZones[zone]);
|
||||
|
||||
void OnNextZone()
|
||||
{
|
||||
path.RemoveAt(0);
|
||||
if (path.Count > 0)
|
||||
{
|
||||
MoveTo(_context.WormholePosition(_entity.Zone, Path[0].ZoneID), false, OnNextZone);
|
||||
}
|
||||
else
|
||||
{
|
||||
onFinish?.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
OnNextZone();
|
||||
}
|
||||
}
|
||||
|
||||
public enum MovementPhase
|
||||
{
|
||||
Locomotion,
|
||||
Slowdown
|
||||
}
|
||||
|
|
@ -9,9 +9,6 @@ using static Unity.Mathematics.math;
|
|||
[MessagePackObject, JsonObject(MemberSerialization.OptIn), EntityTypeRestriction(HullType.Ship), Order(-100)]
|
||||
public class PatrolControllerData : ControllerData
|
||||
{
|
||||
[InspectableField, JsonProperty("targetDistance"), Key(5)]
|
||||
public float TargetDistance = 10;
|
||||
|
||||
public override IBehavior CreateInstance(GameContext context, Entity entity, Gear item)
|
||||
{
|
||||
return new PatrolController(context, this, entity, item);
|
||||
|
|
@ -58,7 +55,7 @@ public class PatrolController : IBehavior, IController, IInitializableBehavior
|
|||
return true;
|
||||
}
|
||||
|
||||
public void AssignTask(Guid task, List<SimplifiedZoneData> path)
|
||||
public void AssignTask(Guid task)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,156 +16,84 @@ public class TowingControllerData : ControllerData
|
|||
}
|
||||
}
|
||||
|
||||
public class TowingController : IBehavior, IPersistentBehavior, IController, IInitializableBehavior
|
||||
public class TowingController : ControllerBase, IBehavior, IPersistentBehavior
|
||||
{
|
||||
public TaskType TaskType => TaskType.Tow;
|
||||
public bool Available => _towingTask != Guid.Empty;
|
||||
public Guid Zone => _entity.Zone;
|
||||
public override TaskType TaskType => TaskType.Tow;
|
||||
public BehaviorData Data => _data;
|
||||
|
||||
private TowingControllerData _data;
|
||||
private GameContext _context;
|
||||
private Entity _entity;
|
||||
private Gear _item;
|
||||
private Guid _towingTask;
|
||||
private List<SimplifiedZoneData> _path;
|
||||
private TowingPhase _towingPhase = TowingPhase.Pickup;
|
||||
private MovementPhase _movementPhase = MovementPhase.Locomotion;
|
||||
private Locomotion _locomotion;
|
||||
private VelocityMatch _velocityMatch;
|
||||
private bool _taskStarted;
|
||||
|
||||
public TowingController(GameContext context, TowingControllerData data, Entity entity, Gear item)
|
||||
public TowingController(GameContext context, TowingControllerData data, Entity entity, Gear item) : base(context, data, entity)
|
||||
{
|
||||
_context = context;
|
||||
_data = data;
|
||||
_entity = entity;
|
||||
_item = item;
|
||||
}
|
||||
|
||||
public void Initialize()
|
||||
|
||||
public new bool Update(float delta)
|
||||
{
|
||||
_locomotion = new Locomotion(_context, _entity, _data);
|
||||
_velocityMatch = new VelocityMatch(_context, _entity, _data);
|
||||
}
|
||||
|
||||
public bool Update(float delta)
|
||||
{
|
||||
var towingTask = _context.Cache.Get<StationTowing>(_towingTask);
|
||||
if (towingTask == null)
|
||||
return false;
|
||||
|
||||
if (_path.Any())
|
||||
var towingTask = _context.Cache.Get<StationTowing>(Task);
|
||||
if (towingTask != null)
|
||||
{
|
||||
_locomotion.Update(delta);
|
||||
if(_entity.Parent!=Guid.Empty)
|
||||
_context.RemoveParent(_entity);
|
||||
|
||||
if (length(_entity.Position - _locomotion.Objective) < _context.GlobalData.WarpDistance)
|
||||
if (!_taskStarted)
|
||||
{
|
||||
_context.Warp(_entity, _path[0].ZoneID);
|
||||
_path.RemoveAt(0);
|
||||
if(_path.Any())
|
||||
SetWormholeDestination();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var destination = float2(0);
|
||||
var velocity = float2(0);
|
||||
if (_towingPhase == TowingPhase.Pickup)
|
||||
{
|
||||
destination = _context.ZoneEntities[_entity.Zone][towingTask.Station].Position;
|
||||
velocity = _context.ZoneEntities[_entity.Zone][towingTask.Station].Velocity;
|
||||
}
|
||||
else
|
||||
{
|
||||
var orbitParent = _context.GetOrbitPosition(towingTask.OrbitParent);
|
||||
var parentToUs = _entity.Position - orbitParent;
|
||||
var nearestPointInOrbit = orbitParent + normalize(parentToUs) * towingTask.OrbitDistance;
|
||||
destination = nearestPointInOrbit;
|
||||
velocity = _context.GetOrbitVelocity(towingTask.OrbitParent);
|
||||
}
|
||||
|
||||
_velocityMatch.TargetVelocity = velocity;
|
||||
if (_movementPhase == MovementPhase.Locomotion)
|
||||
{
|
||||
var matchDistanceTime = _velocityMatch.MatchDistanceTime;
|
||||
_locomotion.Objective = destination + velocity * matchDistanceTime.y;
|
||||
_locomotion.Update(delta);
|
||||
|
||||
var distance = length(destination - _entity.Position);
|
||||
if (distance < matchDistanceTime.x)
|
||||
MoveTo(towingTask.Zone, () =>
|
||||
{
|
||||
_movementPhase = MovementPhase.Slowdown;
|
||||
_velocityMatch.Clear();
|
||||
if (_towingPhase == TowingPhase.Pickup)
|
||||
_velocityMatch.OnMatch += () =>
|
||||
_context.Log($"Towing Controller {_entity.Name} has entered pickup phase.");
|
||||
MoveTo(_context.Cache.Get<Entity>(towingTask.Station), true, () =>
|
||||
{
|
||||
var target = _context.ZoneEntities[_entity.Zone][towingTask.Station] as OrbitalEntity;
|
||||
_context.SetParent(target, _entity);
|
||||
_context.Log($"Towing Controller {_entity.Name} has entered delivery phase.");
|
||||
MoveTo(() =>
|
||||
{
|
||||
var target = _context.ZoneEntities[_entity.Zone][towingTask.Station];
|
||||
_context.SetParent(target, _entity);
|
||||
_towingPhase = TowingPhase.Delivery;
|
||||
_movementPhase = MovementPhase.Locomotion;
|
||||
};
|
||||
else
|
||||
_velocityMatch.OnMatch += () =>
|
||||
var orbitParent = _context.GetOrbitPosition(towingTask.OrbitParent);
|
||||
var parentToUs = _entity.Position - orbitParent;
|
||||
return orbitParent + normalize(parentToUs) * towingTask.OrbitDistance;
|
||||
}, () => _context.GetOrbitVelocity(towingTask.OrbitParent), () =>
|
||||
{
|
||||
var target = _context.ZoneEntities[_entity.Zone][towingTask.Station] as OrbitalEntity;
|
||||
_context.RemoveParent(target);
|
||||
_context.Log($"Towing Controller {_entity.Name} has delivered the target. Returning Home.");
|
||||
|
||||
var orbit = _context.CreateOrbit(towingTask.OrbitParent, _entity.Position);
|
||||
target.OrbitData = orbit.ID;
|
||||
};
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_velocityMatch.Update(delta);
|
||||
|
||||
Task = Guid.Empty;
|
||||
_taskStarted = false;
|
||||
});
|
||||
});
|
||||
});
|
||||
_taskStarted = true;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
return base.Update(delta);
|
||||
}
|
||||
|
||||
public PersistentBehaviorData Store()
|
||||
{
|
||||
return new TowingControllerPersistence
|
||||
{
|
||||
TowingTask = _towingTask
|
||||
Task = Task
|
||||
};
|
||||
}
|
||||
|
||||
public void Restore(PersistentBehaviorData data)
|
||||
{
|
||||
var towingControllerPersistence = data as TowingControllerPersistence;
|
||||
_towingTask = towingControllerPersistence.TowingTask;
|
||||
}
|
||||
|
||||
public void AssignTask(Guid task, List<SimplifiedZoneData> path)
|
||||
{
|
||||
_towingTask = task;
|
||||
_path = path;
|
||||
_path.RemoveAt(0);
|
||||
if(_path.Any())
|
||||
SetWormholeDestination();
|
||||
}
|
||||
|
||||
private void SetWormholeDestination()
|
||||
{
|
||||
_locomotion.Objective = _context.WormholePosition(_entity.Zone, _path[0].ZoneID);
|
||||
Task = towingControllerPersistence.Task;
|
||||
}
|
||||
}
|
||||
|
||||
public class TowingControllerPersistence : PersistentBehaviorData
|
||||
{
|
||||
[JsonProperty("towingTask"), Key(0)]
|
||||
public Guid TowingTask;
|
||||
}
|
||||
|
||||
public enum TowingPhase
|
||||
{
|
||||
Pickup,
|
||||
Delivery
|
||||
}
|
||||
|
||||
public enum MovementPhase
|
||||
{
|
||||
Locomotion,
|
||||
Slowdown
|
||||
[JsonProperty("task"), Key(0)]
|
||||
public Guid Task;
|
||||
}
|
||||
Loading…
Reference in a new issue