Hauling Task
Added a control module which enables drones to haul items from colony to colony Added confirmation dialog for actions which require confirmation, can have fields added to it just like a PropertiesPanel Moving items between inventories now displays confirmation dialog
This commit is contained in:
parent
a901a8e829
commit
991d411c91
24 changed files with 1965 additions and 2193 deletions
|
|
@ -99,6 +99,7 @@ public abstract class PersistentBehaviorData
|
|||
Union(27, typeof(SurveyControllerData)),
|
||||
Union(28, typeof(ResourceScannerData)),
|
||||
Union(29, typeof(WanderControllerData)),
|
||||
Union(30, typeof(HaulingControllerData)),
|
||||
JsonConverter(typeof(JsonKnownTypesConverter<BehaviorData>)), JsonObject(MemberSerialization.OptIn)]
|
||||
public abstract class BehaviorData
|
||||
{
|
||||
|
|
|
|||
|
|
@ -43,6 +43,7 @@ public abstract class ControllerBase : IBehavior, IController, IInitializableBeh
|
|||
protected List<SimplifiedZoneData> Path;
|
||||
protected Guid Task;
|
||||
protected bool Moving;
|
||||
protected bool Waiting;
|
||||
|
||||
private GameContext _context;
|
||||
private Entity _entity;
|
||||
|
|
@ -50,6 +51,8 @@ public abstract class ControllerBase : IBehavior, IController, IInitializableBeh
|
|||
private MovementPhase _movementPhase;
|
||||
private Action _onFinishMoving;
|
||||
private bool _spaceworthy;
|
||||
private float _waitTime;
|
||||
private Action _onFinishWaiting;
|
||||
|
||||
public ControllerBase(GameContext context, ControllerData data, Entity entity)
|
||||
{
|
||||
|
|
@ -88,6 +91,18 @@ public abstract class ControllerBase : IBehavior, IController, IInitializableBeh
|
|||
GoHome();
|
||||
}
|
||||
}
|
||||
|
||||
if (Waiting)
|
||||
{
|
||||
_waitTime -= delta;
|
||||
if (_waitTime < 0)
|
||||
{
|
||||
Waiting = false;
|
||||
_onFinishWaiting?.Invoke();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
if (Moving)
|
||||
{
|
||||
|
|
@ -165,6 +180,13 @@ public abstract class ControllerBase : IBehavior, IController, IInitializableBeh
|
|||
}));
|
||||
}
|
||||
|
||||
public void Wait(float time, Action onFinish = null)
|
||||
{
|
||||
_waitTime = time;
|
||||
Waiting = true;
|
||||
_onFinishWaiting = onFinish;
|
||||
}
|
||||
|
||||
public void MoveTo(Entity entity, bool matchVelocity = true, Action onFinish = null)
|
||||
{
|
||||
TargetPosition = () => entity.Position;
|
||||
|
|
|
|||
163
Assets/Scripts/ServerShared/Behaviors/HaulingController.cs
Normal file
163
Assets/Scripts/ServerShared/Behaviors/HaulingController.cs
Normal file
|
|
@ -0,0 +1,163 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using MessagePack;
|
||||
using Newtonsoft.Json;
|
||||
using Unity.Mathematics;
|
||||
using static Unity.Mathematics.math;
|
||||
|
||||
[MessagePackObject, JsonObject(MemberSerialization.OptIn), EntityTypeRestriction(HullType.Ship), Order(-100)]
|
||||
public class HaulingControllerData : ControllerData
|
||||
{
|
||||
[InspectableField, JsonProperty("targetDistance"), Key(6)]
|
||||
public float DockTime = 2;
|
||||
public override IBehavior CreateInstance(GameContext context, Entity entity, Gear item)
|
||||
{
|
||||
return new HaulingController(context, this, entity, item);
|
||||
}
|
||||
}
|
||||
|
||||
public class HaulingController : ControllerBase, IBehavior, IPersistentBehavior
|
||||
{
|
||||
public override TaskType TaskType => TaskType.Haul;
|
||||
public BehaviorData Data => _data;
|
||||
|
||||
private HaulingControllerData _data;
|
||||
private GameContext _context;
|
||||
private Entity _entity;
|
||||
private Gear _item;
|
||||
private bool _taskStarted;
|
||||
private int _itemsDelivered;
|
||||
private SimpleCommodity _simpleCommodityDelivery;
|
||||
private List<CraftedItemInstance> _craftedItemDelivery = new List<CraftedItemInstance>();
|
||||
|
||||
public HaulingController(GameContext context, HaulingControllerData data, Entity entity, Gear item) : base(context, data, entity)
|
||||
{
|
||||
_context = context;
|
||||
_data = data;
|
||||
_entity = entity;
|
||||
_item = item;
|
||||
}
|
||||
|
||||
public new bool Update(float delta)
|
||||
{
|
||||
var haulingTask = _context.Cache.Get<HaulingTask>(Task);
|
||||
if (haulingTask != null)
|
||||
{
|
||||
if (!_taskStarted)
|
||||
{
|
||||
MoveTo(haulingTask.Zone, OnOriginZoneArrival);
|
||||
_taskStarted = true;
|
||||
}
|
||||
}
|
||||
return base.Update(delta);
|
||||
}
|
||||
|
||||
void OnOriginZoneArrival()
|
||||
{
|
||||
var haulingTask = _context.Cache.Get<HaulingTask>(Task);
|
||||
|
||||
_entity.SetMessage("Entering pickup phase.");
|
||||
|
||||
MoveTo(_context.Cache.Get<Entity>(haulingTask.Origin), true, OnPickup);
|
||||
}
|
||||
|
||||
void OnPickup()
|
||||
{
|
||||
var haulingTask = _context.Cache.Get<HaulingTask>(Task);
|
||||
var target = _context.ZoneEntities[_entity.Zone][haulingTask.Origin] as OrbitalEntity;
|
||||
|
||||
_context.SetParent(_entity, target);
|
||||
var itemData = _context.Cache.Get<ItemData>(haulingTask.ItemType);
|
||||
int quantity = min((int) ((_entity.Capacity - _entity.OccupiedCapacity) / itemData.Size), haulingTask.Quantity - _itemsDelivered);
|
||||
if (itemData is SimpleCommodityData)
|
||||
{
|
||||
var itemMatch = target.Cargo
|
||||
.Select(id => _context.Cache.Get<SimpleCommodity>(id))
|
||||
.FirstOrDefault(ii => ii != null && ii.Data == haulingTask.ItemType);
|
||||
if(itemMatch==null)
|
||||
FinishTask();
|
||||
_simpleCommodityDelivery = _entity.AddCargo(target.RemoveCargo(itemMatch, quantity));
|
||||
}
|
||||
else if (itemData is CraftedItemData)
|
||||
{
|
||||
var itemMatches = target.Cargo
|
||||
.Select(id => _context.Cache.Get<CraftedItemInstance>(id))
|
||||
.Where(ii => ii != null && ii.Data == haulingTask.ItemType);
|
||||
if(!itemMatches.Any())
|
||||
FinishTask();
|
||||
foreach (var match in itemMatches)
|
||||
_craftedItemDelivery.Add(_entity.AddCargo(target.RemoveCargo(match)));
|
||||
}
|
||||
Wait(_data.DockTime, OnItemObtained);
|
||||
}
|
||||
|
||||
void OnItemObtained()
|
||||
{
|
||||
var haulingTask = _context.Cache.Get<HaulingTask>(Task);
|
||||
_entity.SetMessage("Entering delivery phase.");
|
||||
|
||||
var target = _context.Cache.Get<Entity>(haulingTask.Target);
|
||||
|
||||
MoveTo(target.Zone, OnTargetZoneArrival);
|
||||
}
|
||||
|
||||
void OnTargetZoneArrival()
|
||||
{
|
||||
var haulingTask = _context.Cache.Get<HaulingTask>(Task);
|
||||
|
||||
MoveTo(_context.Cache.Get<Entity>(haulingTask.Target), true, OnDelivery);
|
||||
}
|
||||
|
||||
void OnDelivery()
|
||||
{
|
||||
var haulingTask = _context.Cache.Get<HaulingTask>(Task);
|
||||
var target = _context.ZoneEntities[_entity.Zone][haulingTask.Target] as OrbitalEntity;
|
||||
|
||||
_context.SetParent(_entity, target);
|
||||
|
||||
var itemData = _context.Cache.Get<ItemData>(haulingTask.ItemType);
|
||||
if (itemData is SimpleCommodityData)
|
||||
{
|
||||
target.AddCargo(_entity.RemoveCargo(_simpleCommodityDelivery, _simpleCommodityDelivery.Quantity));
|
||||
_itemsDelivered += _simpleCommodityDelivery.Quantity;
|
||||
_simpleCommodityDelivery = null;
|
||||
}
|
||||
else if (itemData is CraftedItemData)
|
||||
{
|
||||
foreach (var item in _craftedItemDelivery)
|
||||
target.AddCargo(_entity.RemoveCargo(item));
|
||||
_itemsDelivered += _craftedItemDelivery.Count;
|
||||
_craftedItemDelivery.Clear();
|
||||
}
|
||||
|
||||
Wait(_data.DockTime, () =>
|
||||
{
|
||||
if(_itemsDelivered == haulingTask.Quantity)
|
||||
FinishTask();
|
||||
else
|
||||
_taskStarted = false;
|
||||
});
|
||||
}
|
||||
|
||||
public PersistentBehaviorData Store()
|
||||
{
|
||||
return new HaulingControllerPersistence
|
||||
{
|
||||
Task = Task
|
||||
};
|
||||
}
|
||||
|
||||
public void Restore(PersistentBehaviorData data)
|
||||
{
|
||||
var towingControllerPersistence = data as HaulingControllerPersistence;
|
||||
Task = towingControllerPersistence.Task;
|
||||
}
|
||||
}
|
||||
|
||||
public class HaulingControllerPersistence : PersistentBehaviorData
|
||||
{
|
||||
[JsonProperty("task"), Key(0)]
|
||||
public Guid Task;
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: e8c5153aa2579174c901c552bfb495a4
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Loading…
Reference in a new issue