Mining
New Mining Controller Behavior and Mining Tool Behavior, tweaked a bunch of asteroid related stuff
This commit is contained in:
parent
7b33c21cd7
commit
399b933a38
22 changed files with 609 additions and 58 deletions
|
|
@ -89,6 +89,8 @@ public abstract class PersistentBehaviorData
|
|||
Union(22, typeof(VisibilityData)),
|
||||
Union(23, typeof(ThermotoggleData)),
|
||||
Union(24, typeof(EnergyDrawData)),
|
||||
Union(25, typeof(MiningControllerData)),
|
||||
Union(26, typeof(MiningToolData)),
|
||||
JsonConverter(typeof(JsonKnownTypesConverter<BehaviorData>)), JsonObject(MemberSerialization.OptIn)]
|
||||
public abstract class BehaviorData
|
||||
{
|
||||
|
|
|
|||
|
|
@ -41,12 +41,12 @@ public abstract class ControllerBase : IBehavior, IController, IInitializableBeh
|
|||
protected bool MatchVelocity;
|
||||
protected List<SimplifiedZoneData> Path;
|
||||
protected Guid Task;
|
||||
protected bool Moving;
|
||||
|
||||
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)
|
||||
|
|
@ -67,7 +67,7 @@ public abstract class ControllerBase : IBehavior, IController, IInitializableBeh
|
|||
public void AssignTask(Guid task)
|
||||
{
|
||||
Task = task;
|
||||
_moving = false;
|
||||
Moving = false;
|
||||
}
|
||||
|
||||
public bool Update(float delta)
|
||||
|
|
@ -79,18 +79,17 @@ public abstract class ControllerBase : IBehavior, IController, IInitializableBeh
|
|||
return false;
|
||||
|
||||
// No task is assigned, but we're not home, go home!
|
||||
var homeEntity = _context.Cache.Get<Entity>(HomeEntity);
|
||||
if (!_moving)
|
||||
if (!Moving)
|
||||
{
|
||||
if (Zone == homeEntity.Zone) MoveTo(homeEntity, true, () =>
|
||||
_context.SetParent(_entity, homeEntity));
|
||||
else MoveTo(homeEntity.Zone, () => MoveTo(homeEntity, true, () =>
|
||||
_context.SetParent(_entity, homeEntity)));
|
||||
GoHome();
|
||||
}
|
||||
}
|
||||
|
||||
if (_moving)
|
||||
if (Moving)
|
||||
{
|
||||
if(_entity.Parent!=Guid.Empty)
|
||||
_context.RemoveParent(_entity);
|
||||
|
||||
var distance = length(TargetPosition() - _entity.Position);
|
||||
if (MatchVelocity)
|
||||
{
|
||||
|
|
@ -108,8 +107,8 @@ public abstract class ControllerBase : IBehavior, IController, IInitializableBeh
|
|||
VelocityMatch.Clear();
|
||||
VelocityMatch.OnMatch += () =>
|
||||
{
|
||||
_moving = false;
|
||||
_onFinishMoving();
|
||||
Moving = false;
|
||||
_onFinishMoving?.Invoke();
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
@ -124,7 +123,7 @@ public abstract class ControllerBase : IBehavior, IController, IInitializableBeh
|
|||
Locomotion.Update(delta);
|
||||
if (distance < _controllerData.TargetDistance)
|
||||
{
|
||||
_moving = false;
|
||||
Moving = false;
|
||||
_onFinishMoving();
|
||||
}
|
||||
}
|
||||
|
|
@ -133,13 +132,39 @@ public abstract class ControllerBase : IBehavior, IController, IInitializableBeh
|
|||
return true;
|
||||
}
|
||||
|
||||
public void FinishTask()
|
||||
{
|
||||
if (Task == Guid.Empty)
|
||||
return;
|
||||
|
||||
var task = _context.Cache.Get(Task);
|
||||
_context.Cache.Delete(task);
|
||||
_context.Cache.Get<Corporation>(_entity.Corporation).Tasks.Remove(task.ID);
|
||||
Task = Guid.Empty;
|
||||
}
|
||||
|
||||
public void GoHome(Action onFinish = null)
|
||||
{
|
||||
var homeEntity = _context.Cache.Get<Entity>(HomeEntity);
|
||||
if (Zone == homeEntity.Zone) MoveTo(homeEntity, true, () =>
|
||||
{
|
||||
_context.SetParent(_entity, homeEntity);
|
||||
onFinish?.Invoke();
|
||||
});
|
||||
else MoveTo(homeEntity.Zone, () => MoveTo(homeEntity, true, () =>
|
||||
{
|
||||
_context.SetParent(_entity, homeEntity);
|
||||
onFinish?.Invoke();
|
||||
}));
|
||||
}
|
||||
|
||||
public void MoveTo(Entity entity, bool matchVelocity = true, Action onFinish = null)
|
||||
{
|
||||
TargetPosition = () => entity.Position;
|
||||
TargetVelocity = () => entity.Velocity;
|
||||
MatchVelocity = matchVelocity;
|
||||
_movementPhase = MovementPhase.Locomotion;
|
||||
_moving = true;
|
||||
Moving = true;
|
||||
_onFinishMoving = onFinish;
|
||||
}
|
||||
|
||||
|
|
@ -149,7 +174,7 @@ public abstract class ControllerBase : IBehavior, IController, IInitializableBeh
|
|||
TargetVelocity = () => float2.zero;
|
||||
MatchVelocity = matchVelocity;
|
||||
_movementPhase = MovementPhase.Locomotion;
|
||||
_moving = true;
|
||||
Moving = true;
|
||||
_onFinishMoving = onFinish;
|
||||
}
|
||||
|
||||
|
|
@ -159,7 +184,7 @@ public abstract class ControllerBase : IBehavior, IController, IInitializableBeh
|
|||
TargetVelocity = velocity;
|
||||
MatchVelocity = true;
|
||||
_movementPhase = MovementPhase.Locomotion;
|
||||
_moving = true;
|
||||
Moving = true;
|
||||
_onFinishMoving = onFinish;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -109,7 +109,7 @@ public class Factory : IBehavior, IPersistentBehavior
|
|||
else
|
||||
{
|
||||
var simpleCommodityData = blueprintItemData as SimpleCommodityData;
|
||||
var simpleCommodityInstance = Entity.Cargo.FirstOrDefault(i => i == simpleCommodityData.ID);
|
||||
var simpleCommodityInstance = Entity.Cargo.FirstOrDefault(i => Context.Cache.Get<ItemInstance>(i).Data == simpleCommodityData.ID);
|
||||
if (simpleCommodityInstance == Guid.Empty)
|
||||
{
|
||||
var newSimpleCommodity = new SimpleCommodity
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@ public class Hitscan : IBehavior
|
|||
private float _firingVisibility;
|
||||
|
||||
public BehaviorData Data => _data;
|
||||
public float Range { get; private set; }
|
||||
|
||||
public Hitscan(GameContext context, HitscanData c, Entity entity, Gear item)
|
||||
{
|
||||
|
|
@ -36,6 +37,7 @@ public class Hitscan : IBehavior
|
|||
public bool Update(float delta)
|
||||
{
|
||||
// TODO: Implement hitscan weapons!
|
||||
Range = Context.Evaluate(_data.Range, Item, Entity);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
|||
130
Assets/Scripts/ServerShared/Behaviors/MiningController.cs
Normal file
130
Assets/Scripts/ServerShared/Behaviors/MiningController.cs
Normal file
|
|
@ -0,0 +1,130 @@
|
|||
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 MiningControllerData : ControllerData
|
||||
{
|
||||
public override IBehavior CreateInstance(GameContext context, Entity entity, Gear item)
|
||||
{
|
||||
return new MiningController(context, this, entity, item);
|
||||
}
|
||||
}
|
||||
|
||||
public class MiningController : ControllerBase, IBehavior, IPersistentBehavior, IInitializableBehavior
|
||||
{
|
||||
public override TaskType TaskType => TaskType.Mine;
|
||||
public BehaviorData Data => _data;
|
||||
|
||||
private MiningControllerData _data;
|
||||
private GameContext _context;
|
||||
private Entity _entity;
|
||||
private Gear _item;
|
||||
private bool _taskStarted;
|
||||
private int _asteroid = -1;
|
||||
private MiningTool _miningTool;
|
||||
private Switch _toolSwitch;
|
||||
|
||||
public MiningController(GameContext context, MiningControllerData data, Entity entity, Gear item) : base(context, data, entity)
|
||||
{
|
||||
_context = context;
|
||||
_data = data;
|
||||
_entity = entity;
|
||||
_item = item;
|
||||
}
|
||||
|
||||
public new void Initialize()
|
||||
{
|
||||
_miningTool = _entity.GetBehaviors<MiningTool>().First();
|
||||
_toolSwitch = _entity.GetSwitch(_miningTool);
|
||||
base.Initialize();
|
||||
}
|
||||
|
||||
public new bool Update(float delta)
|
||||
{
|
||||
var miningTask = _context.Cache.Get<Mining>(Task);
|
||||
if (miningTask != null)
|
||||
{
|
||||
if (!_taskStarted)
|
||||
{
|
||||
NextAsteroid();
|
||||
_taskStarted = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!Moving)
|
||||
{
|
||||
if (_entity.OccupiedCapacity < _entity.Capacity - 1)
|
||||
{
|
||||
var asteroidTransform = _context.GetAsteroidTransform(miningTask.Asteroids, _asteroid);
|
||||
if (length(_entity.Position - asteroidTransform.xy) - asteroidTransform.w > _miningTool.Range)
|
||||
{
|
||||
_context.Log($"Mining Controller {_entity.Name} is out of range, moving to target asteroid.");
|
||||
MoveTo(() => _context.GetAsteroidTransform(miningTask.Asteroids, _asteroid).xy,
|
||||
() => _context.GetAsteroidVelocity(miningTask.Asteroids, _asteroid));
|
||||
_toolSwitch.Activated = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (_context.AsteroidExists(miningTask.Asteroids, _asteroid))
|
||||
{
|
||||
_miningTool.AsteroidBelt = miningTask.Asteroids;
|
||||
_miningTool.Asteroid = _asteroid;
|
||||
_toolSwitch.Activated = true;
|
||||
}
|
||||
else NextAsteroid();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_context.Log($"Mining Controller {_entity.Name} is out of cargo space. Returning home to offload cargo.");
|
||||
GoHome(() =>
|
||||
{
|
||||
var homeEntity = _context.Cache.Get<Entity>(HomeEntity);
|
||||
if (!_entity.Cargo.ToArray().All(ii =>
|
||||
_context.MoveCargo(_entity, homeEntity, _context.Cache.Get<ItemInstance>(ii))))
|
||||
{
|
||||
_context.Log($"Mining Controller {_entity.Name} has run out of cargo space in parent. Closing Task.");
|
||||
FinishTask();
|
||||
_taskStarted = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return base.Update(delta);
|
||||
}
|
||||
|
||||
private void NextAsteroid()
|
||||
{
|
||||
var miningTask = _context.Cache.Get<Mining>(Task);
|
||||
_asteroid = _context.NearestAsteroid(miningTask.Asteroids, _entity.Position);
|
||||
_context.Log($"Mining Controller {_entity.Name} selecting new asteroid.");
|
||||
}
|
||||
|
||||
public PersistentBehaviorData Store()
|
||||
{
|
||||
return new MiningControllerPersistence
|
||||
{
|
||||
Task = Task
|
||||
};
|
||||
}
|
||||
|
||||
public void Restore(PersistentBehaviorData data)
|
||||
{
|
||||
var miningControllerPersistence = data as MiningControllerPersistence;
|
||||
Task = miningControllerPersistence.Task;
|
||||
}
|
||||
}
|
||||
|
||||
public class MiningControllerPersistence : PersistentBehaviorData
|
||||
{
|
||||
[JsonProperty("task"), Key(0)]
|
||||
public Guid Task;
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: b476455dca4333e4da76388aae46ac5e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
71
Assets/Scripts/ServerShared/Behaviors/MiningTool.cs
Normal file
71
Assets/Scripts/ServerShared/Behaviors/MiningTool.cs
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
using MessagePack;
|
||||
using Newtonsoft.Json;
|
||||
using Unity.Mathematics;
|
||||
using static Unity.Mathematics.math;
|
||||
|
||||
[InspectableField, MessagePackObject, JsonObject(MemberSerialization.OptIn), Order(10)]
|
||||
public class MiningToolData : BehaviorData
|
||||
{
|
||||
[InspectableField, JsonProperty("dps"), Key(1)]
|
||||
public PerformanceStat DamagePerSecond = new PerformanceStat();
|
||||
|
||||
[InspectableField, JsonProperty("efficiency"), Key(2)]
|
||||
public PerformanceStat Efficiency = new PerformanceStat();
|
||||
|
||||
[InspectableField, JsonProperty("penetration"), Key(3)]
|
||||
public PerformanceStat Penetration = new PerformanceStat();
|
||||
|
||||
[InspectableField, JsonProperty("range"), Key(4)]
|
||||
public PerformanceStat Range = new PerformanceStat();
|
||||
|
||||
public override IBehavior CreateInstance(GameContext context, Entity entity, Gear item)
|
||||
{
|
||||
return new MiningTool(context, this, entity, item);
|
||||
}
|
||||
}
|
||||
|
||||
public class MiningTool : IBehavior
|
||||
{
|
||||
public Guid AsteroidBelt;
|
||||
public int Asteroid;
|
||||
|
||||
private MiningToolData _data;
|
||||
|
||||
private Entity Entity { get; }
|
||||
private Gear Item { get; }
|
||||
private GameContext Context { get; }
|
||||
|
||||
public BehaviorData Data => _data;
|
||||
public float Range { get; private set; }
|
||||
|
||||
public MiningTool(GameContext context, MiningToolData data, Entity entity, Gear item)
|
||||
{
|
||||
_data = data;
|
||||
Entity = entity;
|
||||
Item = item;
|
||||
Context = context;
|
||||
}
|
||||
|
||||
public bool Update(float delta)
|
||||
{
|
||||
Range = Context.Evaluate(_data.Range, Item, Entity);
|
||||
var asteroidTransform = Context.GetAsteroidTransform(AsteroidBelt, Asteroid);
|
||||
if (AsteroidBelt != Guid.Empty &&
|
||||
Context.AsteroidExists(AsteroidBelt, Asteroid) &&
|
||||
length(Entity.Position - asteroidTransform.xy) - asteroidTransform.w < Range)
|
||||
{
|
||||
Context.MineAsteroid(
|
||||
Entity,
|
||||
AsteroidBelt,
|
||||
Asteroid,
|
||||
Context.Evaluate(_data.DamagePerSecond, Item, Entity) * delta,
|
||||
Context.Evaluate(_data.Efficiency, Item, Entity),
|
||||
Context.Evaluate(_data.Penetration, Item, Entity));
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/ServerShared/Behaviors/MiningTool.cs.meta
Normal file
11
Assets/Scripts/ServerShared/Behaviors/MiningTool.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: a7eebfaae57b2304580ef7e4a2740993
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -17,7 +17,7 @@ public class PatrolControllerData : ControllerData
|
|||
|
||||
public class PatrolController : IBehavior, IController, IInitializableBehavior
|
||||
{
|
||||
public TaskType TaskType => TaskType.Tow;
|
||||
public TaskType TaskType => TaskType.None;
|
||||
public bool Available => false;
|
||||
public Guid Zone => _entity.Zone;
|
||||
public BehaviorData Data => _data;
|
||||
|
|
@ -26,7 +26,6 @@ public class PatrolController : IBehavior, IController, IInitializableBehavior
|
|||
private GameContext _context;
|
||||
private Entity _entity;
|
||||
private Gear _item;
|
||||
private Guid _towingTask;
|
||||
private Locomotion _locomotion;
|
||||
private Guid _targetOrbit;
|
||||
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ public class Switch : IBehavior
|
|||
|
||||
public BehaviorData Data => _data;
|
||||
|
||||
private bool Activated { get; set; }
|
||||
public bool Activated { get; set; }
|
||||
|
||||
public Switch(GameContext context, SwitchData data, Entity entity, Gear item)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -40,9 +40,6 @@ public class TowingController : ControllerBase, IBehavior, IPersistentBehavior
|
|||
var towingTask = _context.Cache.Get<StationTowing>(Task);
|
||||
if (towingTask != null)
|
||||
{
|
||||
if(_entity.Parent!=Guid.Empty)
|
||||
_context.RemoveParent(_entity);
|
||||
|
||||
if (!_taskStarted)
|
||||
{
|
||||
MoveTo(towingTask.Zone, () =>
|
||||
|
|
@ -66,7 +63,7 @@ public class TowingController : ControllerBase, IBehavior, IPersistentBehavior
|
|||
var orbit = _context.CreateOrbit(towingTask.OrbitParent, _entity.Position);
|
||||
target.OrbitData = orbit.ID;
|
||||
|
||||
Task = Guid.Empty;
|
||||
FinishTask();
|
||||
_taskStarted = false;
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue