Local AI Testing & Interactive Tech Tab

Refactored game loop to allow full local testing
Added AI controller abstractions
Added ability to save database to local copy for offline testing
Tech Tab is now interactive, has properties panel
This commit is contained in:
hyperion51 2020-04-23 03:31:17 +01:00
commit af88788e4f
50 changed files with 5799 additions and 712 deletions

View file

@ -0,0 +1,7 @@
using System.Collections;
using System.Collections.Generic;
public abstract class AgentBehavior
{
public abstract void Update(float delta);
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 87f587a591f7ba14f96f34108ca4e611
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,43 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using static Unity.Mathematics.math;
using Random = Unity.Mathematics.Random;
public class AgentController
{
public Entity Entity { get; }
public EntityAgent EntityAgent { get; }
public GameContext Context { get; }
public ZoneData Zone { get; set; }
private Locomotion _locomotion;
private Random _random = new Random((uint) (DateTime.Now.Ticks%uint.MaxValue));
public AgentController(GameContext context, ZoneData zone, Entity entity)
{
Entity = entity;
Zone = zone;
Context = context;
EntityAgent = new EntityAgent(context, zone, entity);
_locomotion = new Locomotion(entity);
EntityAgent.CurrentBehavior = _locomotion;
RandomTarget();
}
public void Update(float delta)
{
EntityAgent.Update(delta);
var distance = length(_locomotion.Objective.Position - Entity.Position);
Context.Log($"Agent Distance: {distance}");
if(distance < 20)
RandomTarget();
}
private void RandomTarget()
{
var entities = Context.ZoneEntities(Zone).ToArray();
_locomotion.Objective = entities[_random.NextInt(entities.Length)];
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: ee1e3350676acb044bd87c1d76c535a4
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,23 @@
using System.Collections;
using System.Collections.Generic;
public class EntityAgent
{
public AgentBehavior CurrentBehavior;
public Entity Entity { get; }
public GameContext Context { get; }
public ZoneData Zone { get; set; }
public EntityAgent(GameContext context, ZoneData zone, Entity entity)
{
Entity = entity;
Zone = zone;
Context = context;
}
public void Update(float delta)
{
CurrentBehavior.Update(delta);
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: fdefbc1e15541d8448061e9d97e44648
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,37 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Unity.Mathematics;
using static Unity.Mathematics.math;
public class Locomotion : AgentBehavior
{
public Entity Objective;
private Entity _entity;
private IAnalogBehavior _thrust;
private IAnalogBehavior _turning;
private const float Sensitivity = 1;
public Locomotion(Entity entity)
{
_entity = entity;
_thrust = entity.Axes.Keys.FirstOrDefault(x => x is Thruster);
_turning = entity.Axes.Keys.FirstOrDefault(x => x is Turning);
}
public override void Update(float delta)
{
if (Objective != null && _thrust != null && _turning != null)
{
float2 diff = Objective.Position - _entity.Position;
var directionAngle = atan2(diff.y, diff.x) / PI;
var currentAngle = atan2(_entity.Direction.y, _entity.Direction.x) / PI;
var d1 = directionAngle - currentAngle;
var d2 = directionAngle - 2 - currentAngle;
var d3 = directionAngle + 2 - currentAngle;
var angleDiff = abs(d1) < abs(d2) ? abs(d1) < abs(d3) ? d1 : d3 : abs(d2) < abs(d3) ? d2 : d3;
_entity.Axes[_turning] = clamp(-angleDiff * Sensitivity, -1, 1);
_entity.Axes[_thrust] = 1;
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c3af5050375d08242a52bdf7f7de558e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: