Main Menu
Added Main Menu scene Tweaked world map to incorporate secondary colors in zone icons + glowing links Entity Serialization is now generic Zone Serialization now includes entities Fixed bug where wormhole traversal would unbind from player entity Game Manager no longer spawns entities (loadout file format changed) Wrote extension to randomly select weighted items from collections (for generating enemies)
This commit is contained in:
parent
1aef932162
commit
e6181c11dc
53 changed files with 3763 additions and 49169 deletions
|
|
@ -5,19 +5,11 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using JsonKnownTypes;
|
||||
using MessagePack;
|
||||
using Newtonsoft.Json;
|
||||
using UniRx;
|
||||
using Unity.Mathematics;
|
||||
using static Unity.Mathematics.math;
|
||||
using int2 = Unity.Mathematics.int2;
|
||||
|
||||
[MessagePackObject,
|
||||
Union(0, typeof(Ship)),
|
||||
Union(1, typeof(OrbitalEntity)),
|
||||
JsonObject(MemberSerialization.OptIn), JsonConverter(typeof(JsonKnownTypesConverter<Entity>))]
|
||||
public abstract class Entity
|
||||
{
|
||||
public Zone Zone;
|
||||
|
|
|
|||
|
|
@ -6,53 +6,15 @@ using MessagePack;
|
|||
using RethinkDb.Driver.Ast;
|
||||
using Unity.Mathematics;
|
||||
|
||||
[MessagePackObject]
|
||||
public class EntityPack
|
||||
public static class EntitySerializer
|
||||
{
|
||||
[Key(0)]
|
||||
public string Name;
|
||||
|
||||
[Key(1)]
|
||||
public EquippableItem Hull;
|
||||
|
||||
[Key(2)]
|
||||
public (int2 position, EquippableItem item)[] Equipment;
|
||||
|
||||
[Key(3)]
|
||||
public (int2 position, EquippableItem item)[] CargoBays;
|
||||
|
||||
[Key(4)]
|
||||
public (int2 position, EquippableItem item)[] DockingBays;
|
||||
|
||||
[Key(5)]
|
||||
public Dictionary<int2, PersistentBehaviorData[]> PersistedBehaviors;
|
||||
|
||||
[Key(6)]
|
||||
public float[,] Temperature;
|
||||
|
||||
[Key(7)]
|
||||
public float[,] Armor;
|
||||
|
||||
[Key(8)]
|
||||
public bool2[,] Conductivity;
|
||||
|
||||
[Key(10)]
|
||||
public int[] DockingBayAssignments;
|
||||
|
||||
[Key(11)]
|
||||
public (int2 position, ItemInstance item)[][] CargoContents;
|
||||
|
||||
[Key(12)]
|
||||
public (int2 position, ItemInstance item)[][] DockingBayContents;
|
||||
|
||||
[Key(13)]
|
||||
public EntityPack[] Children;
|
||||
|
||||
private int _price;
|
||||
|
||||
public static EntityPack Pack(Entity entity)
|
||||
{
|
||||
var pack = new EntityPack();
|
||||
EntityPack pack;
|
||||
if (entity is OrbitalEntity orbital)
|
||||
pack = new OrbitalEntityPack {Orbit = orbital.OrbitData};
|
||||
else pack = new ShipPack();
|
||||
|
||||
// Filter item behavior collections by those with any persistent behaviors
|
||||
// For each item create an object containing the item position and a list of persistent behaviors
|
||||
|
|
@ -80,16 +42,27 @@ public class EntityPack
|
|||
return pack;
|
||||
}
|
||||
|
||||
public static Ship Unpack(ItemManager itemManager, Zone zone, EntityPack pack, bool instantiate = false)
|
||||
public static Entity Unpack(ItemManager itemManager, Zone zone, EntityPack pack, bool instantiate = false)
|
||||
{
|
||||
return pack switch
|
||||
{
|
||||
ShipPack shipPack => Unpack(itemManager, zone, shipPack, instantiate),
|
||||
OrbitalEntityPack orbitalEntityPack => Unpack(itemManager, zone, orbitalEntityPack, instantiate),
|
||||
_ => null
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
public static Ship Unpack(ItemManager itemManager, Zone zone, ShipPack pack, bool instantiate = false)
|
||||
{
|
||||
var entity = new Ship(itemManager, zone, instantiate ? (EquippableItem) itemManager.Instantiate(pack.Hull) : pack.Hull);
|
||||
Restore(itemManager, zone, pack, entity, instantiate);
|
||||
return entity;
|
||||
}
|
||||
|
||||
public static OrbitalEntity Unpack(ItemManager itemManager, Zone zone, EntityPack pack, Guid orbit, bool instantiate = false)
|
||||
public static OrbitalEntity Unpack(ItemManager itemManager, Zone zone, OrbitalEntityPack pack, bool instantiate = false)
|
||||
{
|
||||
var entity = new OrbitalEntity(itemManager, zone, instantiate ? (EquippableItem) itemManager.Instantiate(pack.Hull) : pack.Hull, orbit);
|
||||
var entity = new OrbitalEntity(itemManager, zone, instantiate ? (EquippableItem) itemManager.Instantiate(pack.Hull) : pack.Hull, pack.Orbit);
|
||||
Restore(itemManager, zone, pack, entity, instantiate);
|
||||
return entity;
|
||||
}
|
||||
|
|
@ -144,6 +117,63 @@ public class EntityPack
|
|||
foreach(var v in hullData.Shape.Coordinates)
|
||||
entity.HullConductivity[v.x,v.y] = pack.Conductivity[v.x,v.y];
|
||||
}
|
||||
}
|
||||
|
||||
[MessagePackObject]
|
||||
public class ShipPack : EntityPack { }
|
||||
|
||||
[MessagePackObject]
|
||||
public class OrbitalEntityPack : EntityPack
|
||||
{
|
||||
[Key(14)]
|
||||
public Guid Orbit;
|
||||
}
|
||||
|
||||
[MessagePackObject,
|
||||
Union(0, typeof(OrbitalEntityPack)),
|
||||
Union(1, typeof(ShipPack))]
|
||||
public class EntityPack
|
||||
{
|
||||
[Key(0)]
|
||||
public string Name;
|
||||
|
||||
[Key(1)]
|
||||
public EquippableItem Hull;
|
||||
|
||||
[Key(2)]
|
||||
public (int2 position, EquippableItem item)[] Equipment;
|
||||
|
||||
[Key(3)]
|
||||
public (int2 position, EquippableItem item)[] CargoBays;
|
||||
|
||||
[Key(4)]
|
||||
public (int2 position, EquippableItem item)[] DockingBays;
|
||||
|
||||
[Key(5)]
|
||||
public Dictionary<int2, PersistentBehaviorData[]> PersistedBehaviors;
|
||||
|
||||
[Key(6)]
|
||||
public float[,] Temperature;
|
||||
|
||||
[Key(7)]
|
||||
public float[,] Armor;
|
||||
|
||||
[Key(8)]
|
||||
public bool2[,] Conductivity;
|
||||
|
||||
[Key(10)]
|
||||
public int[] DockingBayAssignments;
|
||||
|
||||
[Key(11)]
|
||||
public (int2 position, ItemInstance item)[][] CargoContents;
|
||||
|
||||
[Key(12)]
|
||||
public (int2 position, ItemInstance item)[][] DockingBayContents;
|
||||
|
||||
[Key(13)]
|
||||
public EntityPack[] Children;
|
||||
|
||||
private int _price;
|
||||
|
||||
public int Price(ItemManager itemManager)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -30,12 +30,34 @@ public static class Extensions
|
|||
peer.Send(MessagePackSerializer.Serialize(message as Message), method);
|
||||
}
|
||||
|
||||
// public static float Performance(this EquippableItemData itemData, float temperature)
|
||||
// {
|
||||
// return saturate(itemData.HeatPerformanceCurve.Evaluate(saturate(
|
||||
// (temperature - itemData.MinimumTemperature) /
|
||||
// (itemData.MaximumTemperature - itemData.MinimumTemperature))));
|
||||
// }
|
||||
public static T[] WeightedRandomElements<T>(this IEnumerable<T> collection, ref Random random, Func<T, float> weightFunction, int count)
|
||||
{
|
||||
var weights = new Dictionary<T, float>(collection.Count());
|
||||
var totalWeight = 0f;
|
||||
foreach (var x in collection)
|
||||
{
|
||||
weights[x] = weightFunction(x);
|
||||
totalWeight += weights[x];
|
||||
}
|
||||
|
||||
var elements = new T[count];
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
var targetWeight = random.NextFloat(totalWeight);
|
||||
var accumWeight = 0f;
|
||||
foreach (var x in collection)
|
||||
{
|
||||
accumWeight += weights[x];
|
||||
if (accumWeight > targetWeight)
|
||||
{
|
||||
elements[i] = x;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return elements;
|
||||
}
|
||||
|
||||
// Thanks, https://stackoverflow.com/a/48599119
|
||||
public static bool ByteArrayCompare(ReadOnlySpan<byte> a1, ReadOnlySpan<byte> a2)
|
||||
|
|
|
|||
|
|
@ -29,6 +29,7 @@ public class Zone
|
|||
|
||||
private HashSet<Guid> _updatedOrbits = new HashSet<Guid>();
|
||||
|
||||
private ItemManager _itemManager;
|
||||
private float _time;
|
||||
private Random _random;
|
||||
public List<Agent> Agents = new List<Agent>();
|
||||
|
|
@ -36,8 +37,9 @@ public class Zone
|
|||
public ZoneData Data { get; }
|
||||
private List<Task> BeltUpdates = new List<Task>();
|
||||
|
||||
public Zone(PlanetSettings settings, ZonePack pack)
|
||||
public Zone(ItemManager itemManager, PlanetSettings settings, ZonePack pack)
|
||||
{
|
||||
_itemManager = itemManager;
|
||||
Data = pack.Data;
|
||||
Settings = settings;
|
||||
_random = new Random(Convert.ToUInt32(abs(Data.ID.GetHashCode())));
|
||||
|
|
@ -63,8 +65,11 @@ public class Zone
|
|||
break;
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var entity in pack.Entities) Entities.Add(entity);
|
||||
|
||||
foreach (var entity in pack.Entities)
|
||||
{
|
||||
Entities.Add(EntitySerializer.Unpack(_itemManager, this, entity));
|
||||
}
|
||||
|
||||
// TODO: Associate planets with stored entities for planetary colonies
|
||||
}
|
||||
|
|
@ -74,7 +79,7 @@ public class Zone
|
|||
return new ZonePack
|
||||
{
|
||||
Data = Data,
|
||||
//Entities = Entities.ToList(),
|
||||
Entities = Entities.Select(EntitySerializer.Pack).ToList(),
|
||||
Orbits = Orbits.Values.Select(o=>o.Data).ToList(),
|
||||
Planets = Planets.Values.ToList()
|
||||
};
|
||||
|
|
|
|||
|
|
@ -60,7 +60,7 @@ public class ZonePack : INamedEntry
|
|||
public List<OrbitData> Orbits = new List<OrbitData>();
|
||||
|
||||
[JsonProperty("entities"), Key(3)]
|
||||
public List<Entity> Entities = new List<Entity>();
|
||||
public List<EntityPack> Entities = new List<EntityPack>();
|
||||
|
||||
[IgnoreMember] public string EntryName
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue