Control Modules & Job System
Agent behaviors are no longer hard coded but instead created by equippable control modules Game Context has a job system which assigns appropriate tasks to drone control modules
This commit is contained in:
parent
6aeb2ae280
commit
ab8aa5fb9a
27 changed files with 480 additions and 190 deletions
|
|
@ -13,9 +13,6 @@ public interface IBehavior
|
|||
{
|
||||
void Initialize();
|
||||
void Update(float delta);
|
||||
Entity Entity { get; }
|
||||
Gear Item { get; }
|
||||
GameContext Context { get; }
|
||||
BehaviorData Data { get; }
|
||||
}
|
||||
|
||||
|
|
@ -36,6 +33,13 @@ public interface IPersistentBehavior//<T> where T : IBehavior
|
|||
void Restore(PersistentBehaviorData data);
|
||||
}
|
||||
|
||||
public interface IController
|
||||
{
|
||||
bool Available { get; }
|
||||
TaskType JobType { get; }
|
||||
void AssignTask(Guid task);
|
||||
}
|
||||
|
||||
[MessagePackObject,
|
||||
Union(0, typeof(FactoryPersistence)),
|
||||
JsonObject(MemberSerialization.OptIn),
|
||||
|
|
@ -58,6 +62,8 @@ public abstract class PersistentBehaviorData
|
|||
Union(10, typeof(VelocityConversionData)),
|
||||
Union(11, typeof(VelocityLimitData)),
|
||||
Union(12, typeof(FactoryData)),
|
||||
Union(13, typeof(PatrolControllerData)),
|
||||
Union(14, typeof(TowingControllerData)),
|
||||
JsonConverter(typeof(JsonKnownTypesConverter<BehaviorData>)), JsonObject(MemberSerialization.OptIn)]
|
||||
public abstract class BehaviorData
|
||||
{
|
||||
|
|
|
|||
25
Assets/Scripts/ServerShared/Behaviors/Controller.cs
Normal file
25
Assets/Scripts/ServerShared/Behaviors/Controller.cs
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using MessagePack;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
[Union(0, typeof(TowingControllerData))]
|
||||
public abstract class ControllerData : BehaviorData
|
||||
{
|
||||
[InspectableField, JsonProperty("thrustSensitivity"), Key(0)]
|
||||
public float ThrustSensitivity = 1;
|
||||
|
||||
[InspectableField, JsonProperty("thrustSpecificity"), Key(1)]
|
||||
public float ThrustSpecificity = 8;
|
||||
|
||||
[InspectableField, JsonProperty("turningSensitivity"), Key(2)]
|
||||
public float TurningSensitivity = 1;
|
||||
|
||||
[InspectableField, JsonProperty("tangentSensitivity"), Key(3)]
|
||||
public float TangentSensitivity = 4;
|
||||
}
|
||||
|
||||
// public abstract class Controller
|
||||
// {
|
||||
//
|
||||
// }
|
||||
11
Assets/Scripts/ServerShared/Behaviors/Controller.cs.meta
Normal file
11
Assets/Scripts/ServerShared/Behaviors/Controller.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 0bd22c9213b660e4d845f7a751f18848
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
68
Assets/Scripts/ServerShared/Behaviors/PatrolController.cs
Normal file
68
Assets/Scripts/ServerShared/Behaviors/PatrolController.cs
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using MessagePack;
|
||||
using Newtonsoft.Json;
|
||||
using Unity.Mathematics;
|
||||
using static Unity.Mathematics.math;
|
||||
|
||||
[MessagePackObject, JsonObject(MemberSerialization.OptIn), EntityTypeRestriction(HullType.Ship)]
|
||||
public class PatrolControllerData : ControllerData
|
||||
{
|
||||
[InspectableField, JsonProperty("targetDistance"), Key(4)]
|
||||
public float TargetDistance = 10;
|
||||
|
||||
public override IBehavior CreateInstance(GameContext context, Entity entity, Gear item)
|
||||
{
|
||||
return new PatrolController(context, this, entity, item);
|
||||
}
|
||||
}
|
||||
|
||||
public class PatrolController : IBehavior, IController
|
||||
{
|
||||
public TaskType JobType => TaskType.Tow;
|
||||
public bool Available => false;
|
||||
public BehaviorData Data => _data;
|
||||
|
||||
private PatrolControllerData _data;
|
||||
private GameContext _context;
|
||||
private Entity _entity;
|
||||
private Gear _item;
|
||||
private Guid _towingTask;
|
||||
private Locomotion _locomotion;
|
||||
private Guid _targetOrbit;
|
||||
|
||||
public PatrolController(GameContext context, PatrolControllerData data, Entity entity, Gear item)
|
||||
{
|
||||
_context = context;
|
||||
_data = data;
|
||||
_entity = entity;
|
||||
_item = item;
|
||||
}
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
_locomotion = new Locomotion(_context, _entity, _data);
|
||||
RandomTarget();
|
||||
}
|
||||
|
||||
public void Update(float delta)
|
||||
{
|
||||
_locomotion.Objective = _context.GetOrbitPosition(_targetOrbit);
|
||||
_locomotion.Update(delta);
|
||||
|
||||
if(length(_entity.Position - _locomotion.Objective) < _data.TargetDistance)
|
||||
RandomTarget();
|
||||
}
|
||||
|
||||
public void AssignTask(Guid task)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
private void RandomTarget()
|
||||
{
|
||||
var entities = _context.ZonePlanets[_entity.Zone];
|
||||
_targetOrbit = _context.Cache.Get<PlanetData>(entities[_context.Random.NextInt(entities.Length)]).Orbit;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: cc2d16773614011488d30e38edf7c3bb
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -26,12 +26,14 @@ public class Thruster : IAnalogBehavior
|
|||
public Entity Entity { get; }
|
||||
public Gear Item { get; }
|
||||
public GameContext Context { get; }
|
||||
|
||||
public float Thrust { get; private set; }
|
||||
|
||||
public BehaviorData Data => _data;
|
||||
|
||||
private ThrusterData _data;
|
||||
|
||||
private float _thrust;
|
||||
private float _input;
|
||||
|
||||
public Thruster(GameContext context, ThrusterData data, Entity entity, Gear item)
|
||||
{
|
||||
|
|
@ -43,7 +45,7 @@ public class Thruster : IAnalogBehavior
|
|||
|
||||
public void SetAxis(float value)
|
||||
{
|
||||
_thrust = saturate(value);
|
||||
_input = saturate(value);
|
||||
}
|
||||
|
||||
public void Initialize()
|
||||
|
|
@ -52,8 +54,9 @@ public class Thruster : IAnalogBehavior
|
|||
|
||||
public void Update(float delta)
|
||||
{
|
||||
Entity.Velocity += Entity.Direction * _thrust * Context.Evaluate(_data.Thrust, Item, Entity) / Entity.Mass * delta;
|
||||
Entity.AddHeat(_thrust * Context.Evaluate(_data.Heat, Item, Entity) * delta);
|
||||
Entity.VisibilitySources[this] = _thrust * Context.Evaluate(_data.Visibility, Item, Entity);
|
||||
Thrust = Context.Evaluate(_data.Thrust, Item, Entity);
|
||||
Entity.Velocity += Entity.Direction * _input * Thrust / Entity.Mass * delta;
|
||||
Entity.AddHeat(_input * Context.Evaluate(_data.Heat, Item, Entity) * delta);
|
||||
Entity.VisibilitySources[this] = _input * Context.Evaluate(_data.Visibility, Item, Entity);
|
||||
}
|
||||
}
|
||||
69
Assets/Scripts/ServerShared/Behaviors/TowingController.cs
Normal file
69
Assets/Scripts/ServerShared/Behaviors/TowingController.cs
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using MessagePack;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
[MessagePackObject, JsonObject(MemberSerialization.OptIn), EntityTypeRestriction(HullType.Ship)]
|
||||
public class TowingControllerData : ControllerData
|
||||
{
|
||||
public override IBehavior CreateInstance(GameContext context, Entity entity, Gear item)
|
||||
{
|
||||
return new TowingController(context, this, entity, item);
|
||||
}
|
||||
}
|
||||
|
||||
public class TowingController : IBehavior, IPersistentBehavior, IController
|
||||
{
|
||||
public TaskType JobType => TaskType.Tow;
|
||||
public bool Available => _towingTask != Guid.Empty;
|
||||
public BehaviorData Data => _data;
|
||||
|
||||
private TowingControllerData _data;
|
||||
private GameContext _context;
|
||||
private Entity _entity;
|
||||
private Gear _item;
|
||||
private Guid _towingTask;
|
||||
|
||||
public TowingController(GameContext context, TowingControllerData data, Entity entity, Gear item)
|
||||
{
|
||||
_context = context;
|
||||
_data = data;
|
||||
_entity = entity;
|
||||
_item = item;
|
||||
}
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void Update(float delta)
|
||||
{
|
||||
}
|
||||
|
||||
public PersistentBehaviorData Store()
|
||||
{
|
||||
return new TowingControllerPersistence
|
||||
{
|
||||
TowingTask = _towingTask
|
||||
};
|
||||
}
|
||||
|
||||
public void Restore(PersistentBehaviorData data)
|
||||
{
|
||||
var towingControllerPersistence = data as TowingControllerPersistence;
|
||||
_towingTask = towingControllerPersistence.TowingTask;
|
||||
}
|
||||
|
||||
public void AssignTask(Guid task)
|
||||
{
|
||||
_towingTask = task;
|
||||
}
|
||||
}
|
||||
|
||||
public class TowingControllerPersistence : PersistentBehaviorData
|
||||
{
|
||||
[JsonProperty("towingTask"), Key(0)]
|
||||
public Guid TowingTask;
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: de134a549a7d86f43a3590b78a2edeee
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -26,12 +26,14 @@ public class Turning : IAnalogBehavior
|
|||
public Entity Entity { get; }
|
||||
public Gear Item { get; }
|
||||
public GameContext Context { get; }
|
||||
|
||||
public float Torque { get; private set; }
|
||||
|
||||
public BehaviorData Data => _data;
|
||||
|
||||
private TurningData _data;
|
||||
|
||||
private float _turning;
|
||||
private float _input;
|
||||
|
||||
public Turning(GameContext context, TurningData data, Entity entity, Gear item)
|
||||
{
|
||||
|
|
@ -43,7 +45,7 @@ public class Turning : IAnalogBehavior
|
|||
|
||||
public void SetAxis(float value)
|
||||
{
|
||||
_turning = clamp(value, -1, 1);
|
||||
_input = clamp(value, -1, 1);
|
||||
}
|
||||
|
||||
public void Initialize()
|
||||
|
|
@ -52,8 +54,9 @@ public class Turning : IAnalogBehavior
|
|||
|
||||
public void Update(float delta)
|
||||
{
|
||||
Entity.Direction = mul(Entity.Direction, Unity.Mathematics.float2x2.Rotate(_turning * Context.Evaluate(_data.Torque, Item, Entity) / Entity.Mass * delta));
|
||||
Entity.AddHeat(abs(_turning) * Context.Evaluate(_data.Heat, Item, Entity) * delta);
|
||||
Entity.VisibilitySources[this] = abs(_turning) * Context.Evaluate(_data.Visibility, Item, Entity);
|
||||
Torque = Context.Evaluate(_data.Torque, Item, Entity);
|
||||
Entity.Direction = mul(Entity.Direction, Unity.Mathematics.float2x2.Rotate(_input * Torque / Entity.Mass * delta));
|
||||
Entity.AddHeat(abs(_input) * Context.Evaluate(_data.Heat, Item, Entity) * delta);
|
||||
Entity.VisibilitySources[this] = abs(_input) * Context.Evaluate(_data.Visibility, Item, Entity);
|
||||
}
|
||||
}
|
||||
|
|
@ -21,6 +21,8 @@ public class VelocityLimit : IBehavior
|
|||
public Entity Entity { get; }
|
||||
public Gear Item { get; }
|
||||
public GameContext Context { get; }
|
||||
|
||||
public float Limit { get; private set; }
|
||||
|
||||
public BehaviorData Data => _data;
|
||||
|
||||
|
|
@ -40,8 +42,8 @@ public class VelocityLimit : IBehavior
|
|||
|
||||
public void Update(float delta)
|
||||
{
|
||||
var limit = Context.Evaluate(_data.TopSpeed, Item, Entity);
|
||||
if (length(Entity.Velocity) > limit)
|
||||
Entity.Velocity = normalize(Entity.Velocity) * limit;
|
||||
Limit = Context.Evaluate(_data.TopSpeed, Item, Entity);
|
||||
if (length(Entity.Velocity) > Limit)
|
||||
Entity.Velocity = normalize(Entity.Velocity) * Limit;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue