2020-04-09 10:33:16 +01:00
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using Unity.Mathematics;
|
|
|
|
|
using static Unity.Mathematics.math;
|
|
|
|
|
|
|
|
|
|
public class Entity
|
|
|
|
|
{
|
|
|
|
|
public Dictionary<IAnalogBehavior, float> AxisOverrides = new Dictionary<IAnalogBehavior, float>();
|
|
|
|
|
public readonly Dictionary<object, float> VisibilitySources = new Dictionary<object, float>();
|
|
|
|
|
public float Temperature;
|
|
|
|
|
public float Energy;
|
|
|
|
|
public float2 Position;
|
2020-04-23 03:31:17 +01:00
|
|
|
public float2 Direction = float2(0,1);
|
2020-04-16 03:08:18 +01:00
|
|
|
public float2 Velocity;
|
2020-04-23 03:31:17 +01:00
|
|
|
public Dictionary<Guid, List<IActivatedBehavior>> Bindings;
|
|
|
|
|
public Dictionary<IAnalogBehavior, float> Axes;
|
2020-04-09 10:33:16 +01:00
|
|
|
|
|
|
|
|
protected readonly GameContext Context;
|
|
|
|
|
protected readonly List<IBehavior> Behaviors;
|
|
|
|
|
protected readonly List<Gear> Items;
|
|
|
|
|
protected readonly List<ItemInstance> Cargo;
|
2020-04-23 03:31:17 +01:00
|
|
|
|
|
|
|
|
protected Gear Hull;
|
2020-04-09 10:33:16 +01:00
|
|
|
|
|
|
|
|
public float Mass { get; private set; }
|
|
|
|
|
public float SpecificHeat { get; private set; }
|
|
|
|
|
public float Visibility => VisibilitySources.Values.Sum();
|
|
|
|
|
|
2020-04-23 03:31:17 +01:00
|
|
|
public Entity(GameContext context, Gear hull, IEnumerable<Gear> items, IEnumerable<ItemInstance> cargo)
|
2020-04-09 10:33:16 +01:00
|
|
|
{
|
|
|
|
|
Context = context;
|
|
|
|
|
|
|
|
|
|
Items = items.ToList();
|
|
|
|
|
Cargo = cargo.ToList();
|
2020-04-23 03:31:17 +01:00
|
|
|
Hull = hull;
|
2020-04-09 10:33:16 +01:00
|
|
|
|
2020-04-23 03:31:17 +01:00
|
|
|
Mass = hull?.Mass ?? 0 +
|
|
|
|
|
Items.Sum(i => i.Mass) +
|
2020-04-09 10:33:16 +01:00
|
|
|
Cargo.Sum(ii => ii.Mass);
|
|
|
|
|
|
2020-04-23 03:31:17 +01:00
|
|
|
SpecificHeat = (hull?.HeatCapacity ?? 0) +
|
|
|
|
|
Items.Sum(i => i.HeatCapacity) +
|
2020-04-09 10:33:16 +01:00
|
|
|
Cargo.Sum(ii => ii.HeatCapacity);
|
|
|
|
|
|
|
|
|
|
Behaviors = Items
|
|
|
|
|
.Where(i=>i.ItemData.Behaviors?.Any()??false)
|
|
|
|
|
.SelectMany(i => i.ItemData.Behaviors
|
|
|
|
|
.Select(bd => bd.CreateInstance(Context, this, i)))
|
|
|
|
|
.OrderBy(b => b.GetType().GetCustomAttribute<UpdateOrderAttribute>()?.Order ?? 0).ToList();
|
|
|
|
|
|
|
|
|
|
Bindings = Behaviors
|
|
|
|
|
.Where(b => b is IActivatedBehavior)
|
|
|
|
|
.GroupBy(b => b.Item.ID, (guid, behaviors) => new {guid, behaviors})
|
|
|
|
|
.ToDictionary(g => g.guid, g => g.behaviors.Cast<IActivatedBehavior>().ToList());
|
|
|
|
|
|
|
|
|
|
Axes = Behaviors
|
|
|
|
|
.Where(b => b is IAnalogBehavior)
|
|
|
|
|
.ToDictionary(b => b as IAnalogBehavior, b => 0f);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<T> GetBehaviors<T>() where T : class, IBehavior
|
|
|
|
|
{
|
|
|
|
|
foreach (var behavior in Behaviors)
|
|
|
|
|
if (behavior is T b)
|
|
|
|
|
yield return b;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<T> GetBehaviorData<T>() where T : class, IBehaviorData
|
|
|
|
|
{
|
|
|
|
|
foreach (var behavior in Behaviors)
|
|
|
|
|
if (behavior.Data is T b)
|
|
|
|
|
yield return b;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AddHeat(float heat)
|
|
|
|
|
{
|
|
|
|
|
Temperature += heat / SpecificHeat;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual void Update(float delta)
|
|
|
|
|
{
|
|
|
|
|
foreach(var analogBehavior in Axes.Keys)
|
|
|
|
|
analogBehavior.SetAxis(AxisOverrides.ContainsKey(analogBehavior) ? AxisOverrides[analogBehavior] : Axes[analogBehavior]);
|
|
|
|
|
|
|
|
|
|
foreach(var behavior in Behaviors)
|
|
|
|
|
behavior.Update(delta);
|
|
|
|
|
|
|
|
|
|
foreach (var item in Items.Where(item => item.ItemData.Performance(Temperature) < .01f))
|
|
|
|
|
item.Durability -= delta;
|
|
|
|
|
}
|
|
|
|
|
}
|