Local Zone Generation
Added the ability to generate zones locally in order to facilitate rapidly iterating generation parameters. Then of course I tweaked the shit out of the zone generation!
This commit is contained in:
parent
4781c8f915
commit
776e08a1d1
39 changed files with 1236 additions and 2985 deletions
|
|
@ -22,6 +22,8 @@ public static class CultClient {
|
|||
private static Dictionary<Type, NotAnActionCollection> _messageCallbacks = new Dictionary<Type, NotAnActionCollection>();
|
||||
private static Guid _token;
|
||||
private static MD5 _hasher = MD5.Create();
|
||||
private static string _lastHost;
|
||||
private static int _lastPort;
|
||||
|
||||
public static bool Verified => _token != Guid.Empty;
|
||||
public static bool Connected => _client != null;
|
||||
|
|
@ -89,6 +91,8 @@ public static class CultClient {
|
|||
|
||||
public static void Connect(string host = "localhost", int port = 3075)
|
||||
{
|
||||
_lastHost = host;
|
||||
_lastPort = port;
|
||||
// Set extensions to default resolver.
|
||||
var resolver = CompositeResolver.Create(
|
||||
MathResolver.Instance,
|
||||
|
|
@ -142,12 +146,15 @@ public static class CultClient {
|
|||
{
|
||||
Logger($"Peer {peer.EndPoint.Address}:{peer.EndPoint.Port} connected.");
|
||||
_peer = peer;
|
||||
if(Verified)
|
||||
peer.Send(new VerifyMessage{Session = _token});
|
||||
};
|
||||
|
||||
listener.PeerDisconnectedEvent += (peer, info) =>
|
||||
{
|
||||
Logger($"Peer {peer.EndPoint.Address}:{peer.EndPoint.Port} disconnected: {info.Reason}.");
|
||||
_peer = null;
|
||||
Connect(_lastHost, _lastPort);
|
||||
};
|
||||
|
||||
listener.NetworkLatencyUpdateEvent +=
|
||||
|
|
|
|||
|
|
@ -114,26 +114,20 @@ public class GalaxyEditor : Editor
|
|||
if (_showResourceMaps)
|
||||
{
|
||||
EditorGUI.indentLevel++;
|
||||
foreach (var resourceDensity in galaxy.MapData.ResourceDensities.ToArray())
|
||||
foreach (var resourceDensity in galaxy.MapData.ResourceDensities)
|
||||
{
|
||||
BeginHorizontal();
|
||||
var newName = DelayedTextField(resourceDensity.Key);
|
||||
if (newName != resourceDensity.Key)
|
||||
{
|
||||
galaxy.MapData.ResourceDensities.Remove(resourceDensity.Key);
|
||||
galaxy.MapData.ResourceDensities[newName] = resourceDensity.Value;
|
||||
}
|
||||
resourceDensity.Name = DelayedTextField(resourceDensity.Name);
|
||||
if (GUILayout.Button("Inspect"))
|
||||
{
|
||||
_currentLayerName = resourceDensity.Key;
|
||||
_currentLayer = resourceDensity.Value;
|
||||
_currentLayer = resourceDensity;
|
||||
}
|
||||
EndHorizontal();
|
||||
}
|
||||
EditorGUI.indentLevel--;
|
||||
if (GUILayout.Button("Add New Resource"))
|
||||
{
|
||||
galaxy.MapData.ResourceDensities["New Resource"] = new GalaxyMapLayerData();
|
||||
galaxy.MapData.ResourceDensities.Add(new GalaxyMapLayerData(){Name = "New Resource"});
|
||||
}
|
||||
}
|
||||
else if(_currentLayer != galaxy.MapData.StarDensity)
|
||||
|
|
@ -287,10 +281,10 @@ public class GalaxyEditor : Editor
|
|||
|
||||
galaxy.MapData.GlobalData.MapLayers["StarDensity"] = galaxy.MapData.StarDensity.ID = Guid.NewGuid();
|
||||
R.Db("Aetheria").Table("Galaxy").Insert(galaxy.MapData.StarDensity).Run(_connection);
|
||||
foreach (var kvp in galaxy.MapData.ResourceDensities)
|
||||
foreach (var data in galaxy.MapData.ResourceDensities)
|
||||
{
|
||||
galaxy.MapData.GlobalData.MapLayers[kvp.Key] = kvp.Value.ID = Guid.NewGuid();
|
||||
R.Db("Aetheria").Table("Galaxy").Insert(kvp.Value).Run(_connection);
|
||||
galaxy.MapData.GlobalData.MapLayers[data.Name] = data.ID = Guid.NewGuid();
|
||||
R.Db("Aetheria").Table("Galaxy").Insert(data).Run(_connection);
|
||||
}
|
||||
|
||||
R.Db("Aetheria").Table("Galaxy").Insert(galaxy.MapData.GlobalData).Run(_connection);
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ using Unity.Mathematics;
|
|||
using static Unity.Mathematics.math;
|
||||
using static Unity.Mathematics.noise;
|
||||
|
||||
[Serializable]
|
||||
[CreateAssetMenu(menuName = "Aetheria/Galaxy")]
|
||||
public class Galaxy : ScriptableObject
|
||||
{
|
||||
|
|
@ -22,9 +23,9 @@ public class GalaxyMapData
|
|||
public GlobalData GlobalData = new GlobalData() {ID = Guid.NewGuid()};
|
||||
|
||||
[HideInInspector]
|
||||
public GalaxyMapLayerData StarDensity;
|
||||
[HideInInspector]
|
||||
public Dictionary<string,GalaxyMapLayerData> ResourceDensities = new Dictionary<string, GalaxyMapLayerData>();
|
||||
public GalaxyMapLayerData StarDensity;
|
||||
[HideInInspector]
|
||||
public List<GalaxyMapLayerData> ResourceDensities = new List<GalaxyMapLayerData>();
|
||||
[HideInInspector]
|
||||
public List<StarData> Stars = new List<StarData>();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,11 +23,6 @@ public class GameContext
|
|||
}
|
||||
}
|
||||
|
||||
public float PlanetRadius(float mass)
|
||||
{
|
||||
return pow(mass, 1 / GlobalData.RadiusPower);
|
||||
}
|
||||
|
||||
public int ItemTier(CraftedItemData itemData)
|
||||
{
|
||||
if (Tier.ContainsKey(itemData)) return Tier[itemData];
|
||||
|
|
|
|||
|
|
@ -61,6 +61,35 @@ public class GlobalData : DatabaseEntry
|
|||
|
||||
[InspectableField] [JsonProperty("mapLayers")] [Key(16)]
|
||||
public Dictionary<string, Guid> MapLayers = new Dictionary<string, Guid>();
|
||||
|
||||
[InspectableField] [JsonProperty("zoneRadiusMin")] [Key(17)]
|
||||
public float MinimumZoneRadius = 500;
|
||||
|
||||
[InspectableField] [JsonProperty("zoneRadiusMax")] [Key(18)]
|
||||
public float MaximumZoneRadius = 2000;
|
||||
|
||||
[InspectableField] [JsonProperty("zoneMassMin")] [Key(19)]
|
||||
public float MinimumZoneMass = 10000;
|
||||
|
||||
[InspectableField] [JsonProperty("zoneMassMax")] [Key(20)]
|
||||
public float MaximumZoneMass = 100000;
|
||||
|
||||
[InspectableField] [JsonProperty("orbitPeriodExponent")] [Key(21)]
|
||||
public float OrbitPeriodExponent = 1.5f;
|
||||
|
||||
[InspectableField] [JsonProperty("orbitPeriodMultiplier")] [Key(22)]
|
||||
public float OrbitPeriodMultiplier = 1f;
|
||||
|
||||
[InspectableField] [JsonProperty("gravityRadiusExponent")] [Key(23)]
|
||||
public float GravityRadiusExponent = 1.5f;
|
||||
|
||||
[InspectableField] [JsonProperty("gravityRadiusMultiplier")] [Key(24)]
|
||||
public float GravityRadiusMultiplier = 1f;
|
||||
|
||||
public float PlanetRadius(float mass)
|
||||
{
|
||||
return pow(mass, 1 / RadiusPower);
|
||||
}
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
|
|
@ -108,6 +137,8 @@ public class GalaxyMapLayerData : DatabaseEntry
|
|||
[InspectableField] [JsonProperty("noisePosition")] [Key(13)]
|
||||
public float NoisePosition = 1337;
|
||||
|
||||
[IgnoreMember] public string Name;
|
||||
|
||||
public float Evaluate(float2 uv, GlobalData data)
|
||||
{
|
||||
float2 offset = -float2(.5f, .5f)+uv;
|
||||
|
|
@ -117,7 +148,7 @@ public class GalaxyMapLayerData : DatabaseEntry
|
|||
float atan = atan2(t.y,t.x);
|
||||
float spokes = (sin(atan*data.Arms) + SpokeOffset) * SpokeScale;
|
||||
float noise = fBm(uv + float2(NoisePosition), NoiseOctaves, NoiseFrequency, NoiseOffset, NoiseAmplitude, NoiseLacunarity, NoiseGain);
|
||||
float shape = lerp(spokes - EdgeReduction * length(offset), 1, pow(circle + CoreBoostOffset, CoreBoostPower) * CoreBoost);
|
||||
float shape = lerp(spokes - EdgeReduction * length(offset), 1, pow(circle, CoreBoostPower) * CoreBoost) + CoreBoostOffset;
|
||||
float gal = max(shape - noise * saturate(circle), 0);
|
||||
|
||||
return gal;
|
||||
|
|
|
|||
|
|
@ -93,8 +93,8 @@ public class PlanetData : DatabaseEntry, INamedEntry
|
|||
[JsonProperty("zone")] [Key(4)]
|
||||
public Guid Zone;
|
||||
|
||||
[JsonProperty("radius")] [Key(5)]
|
||||
public float GravityRadius;
|
||||
// [JsonProperty("radius")] [Key(5)]
|
||||
// public float GravityRadius;
|
||||
|
||||
[IgnoreMember] public string EntryName
|
||||
{
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ using System.Globalization;
|
|||
using System.Linq;
|
||||
using MessagePack;
|
||||
using Unity.Mathematics;
|
||||
using UnityEngine;
|
||||
// TODO: USE THIS EVERYWHERE
|
||||
using static Unity.Mathematics.math;
|
||||
using Random = Unity.Mathematics.Random;
|
||||
|
|
@ -24,7 +25,61 @@ public class ZoneGenerator
|
|||
// public float GasGiantMass = 2000;
|
||||
// public float RadiusPower = 1.75f;
|
||||
|
||||
public static Planet[] GenerateEntities(GameContext context, ZoneData data, float mass, float radius)
|
||||
public static void GenerateZone(GlobalData global, ZoneData zone, GalaxyMapLayerData mass, GalaxyMapLayerData radius, out OrbitData[] orbitData, out PlanetData[] planetData)
|
||||
{
|
||||
var zoneRadius = lerp(global.MinimumZoneRadius, global.MaximumZoneRadius, radius.Evaluate(zone.Position, global));
|
||||
zone.Radius = zoneRadius;
|
||||
|
||||
var zoneMass = lerp(global.MinimumZoneMass, global.MaximumZoneMass, mass.Evaluate(zone.Position, global));
|
||||
|
||||
Debug.Log($"Generating zone at position {zone.Position} with radius {zoneRadius} and mass {zoneMass}");
|
||||
|
||||
var planets = GenerateEntities(global, zone, zoneMass, zoneRadius);
|
||||
|
||||
// Create collections to map between zone generator output and database entries
|
||||
var orbitMap = new Dictionary<Planet, OrbitData>();
|
||||
var orbitInverseMap = new Dictionary<OrbitData, Planet>();
|
||||
|
||||
// Create orbit database entries
|
||||
orbitData = planets.Select(planet =>
|
||||
{
|
||||
var data = new OrbitData()
|
||||
{
|
||||
ID = Guid.NewGuid(),
|
||||
Distance = planet.Distance,
|
||||
Period = planet.Period,
|
||||
Phase = planet.Phase,
|
||||
Zone = zone.ID
|
||||
};
|
||||
orbitMap[planet] = data;
|
||||
orbitInverseMap[data] = planet;
|
||||
return data;
|
||||
}).ToArray();
|
||||
|
||||
// Link OrbitData parents to database GUIDs
|
||||
foreach (var data in orbitData)
|
||||
data.Parent = orbitInverseMap[data].Parent != null
|
||||
? orbitMap[orbitInverseMap[data].Parent].ID
|
||||
: Guid.Empty;
|
||||
|
||||
planetData = planets.Where(p=>!p.Empty).Select(planet =>
|
||||
{
|
||||
var planetDatum = new PlanetData
|
||||
{
|
||||
Mass = planet.Mass,
|
||||
ID = Guid.NewGuid(),
|
||||
Orbit = orbitMap[planet].ID,
|
||||
Zone = zone.ID
|
||||
};
|
||||
planetDatum.Name = planetDatum.ID.ToString().Substring(0, 8);
|
||||
return planetDatum;
|
||||
}).ToArray();
|
||||
|
||||
zone.Planets = planetData.Select(pd => pd.ID).ToList();
|
||||
zone.Orbits = orbitData.Select(od => od.ID).ToList();
|
||||
}
|
||||
|
||||
public static Planet[] GenerateEntities(GlobalData global, ZoneData data, float mass, float radius)
|
||||
{
|
||||
var random = new Random();
|
||||
random.InitState(unchecked((uint)data.Name.GetHashCode()));
|
||||
|
|
@ -32,15 +87,15 @@ public class ZoneGenerator
|
|||
var root = new Planet
|
||||
{
|
||||
Random = random,
|
||||
Context = context,
|
||||
Global = global,
|
||||
Mass = mass,
|
||||
ChildDistanceMaximum = radius,
|
||||
ChildDistanceMinimum = context.PlanetRadius(mass)
|
||||
ChildDistanceMaximum = radius * .75f,
|
||||
ChildDistanceMinimum = global.PlanetRadius(mass)
|
||||
};
|
||||
|
||||
// There is some chance of generating a rosette or binary system
|
||||
// Probabilities which are fixed for the entire galaxy are in GlobalData, contained in the GameContext
|
||||
var rosette = random.NextFloat() < context.GlobalData.RosetteProbability;
|
||||
var rosette = random.NextFloat() < global.RosetteProbability;
|
||||
|
||||
if (rosette)
|
||||
{
|
||||
|
|
@ -58,7 +113,7 @@ public class ZoneGenerator
|
|||
massFraction: .1f);
|
||||
|
||||
var averageChildMass = root.Children.Sum(p => p.Mass) / root.Children.Count;
|
||||
foreach(var p in root.Children.Where(c=>c.Mass>context.GlobalData.GasGiantMass))
|
||||
foreach(var p in root.Children.Where(c=>c.Mass>global.GasGiantMass))
|
||||
{
|
||||
var m = p.Mass / averageChildMass;
|
||||
// Give each child in the rosette its own mini solar system
|
||||
|
|
@ -66,8 +121,8 @@ public class ZoneGenerator
|
|||
count: (int) (m * 5),
|
||||
massMulMin: 0.75f,
|
||||
massMulMax: 2.5f,
|
||||
distMulMin: 1 + m * .05f,
|
||||
distMulMax: 1.05f + m * .15f,
|
||||
distMulMin: 1 + m * .25f,
|
||||
distMulMax: 1.05f + m * .5f,
|
||||
jupiterJump: random.NextFloat() * random.NextFloat() * 10 + 1,
|
||||
massFraction: .5f
|
||||
);
|
||||
|
|
@ -81,7 +136,7 @@ public class ZoneGenerator
|
|||
massMulMin: 0.5f,
|
||||
massMulMax: 2.0f,
|
||||
distMulMin: 1.1f,
|
||||
distMulMax: 1.35f,
|
||||
distMulMax: 1.25f,
|
||||
jupiterJump: random.NextFloat() * random.NextFloat() * 10 + 1,
|
||||
massFraction: .25f
|
||||
);
|
||||
|
|
@ -89,20 +144,20 @@ public class ZoneGenerator
|
|||
|
||||
// Get all children that are above the satellite creation mass floor and not rosette members
|
||||
var satelliteCandidates = rosette
|
||||
? root.AllPlanets().Where(p => p.Parent != root && p.Mass > context.GlobalData.SatelliteCreationMassFloor)
|
||||
: root.AllPlanets().Where(p => p.Mass > context.GlobalData.SatelliteCreationMassFloor);
|
||||
? root.AllPlanets().Where(p => p.Parent != root && p.Mass > global.SatelliteCreationMassFloor)
|
||||
: root.AllPlanets().Where(p => p.Mass > global.SatelliteCreationMassFloor);
|
||||
|
||||
foreach (var planet in satelliteCandidates)
|
||||
{
|
||||
// There's a chance of generating satellites for each qualified planet
|
||||
if (random.NextFloat() < context.GlobalData.SatelliteCreationProbability)
|
||||
if (random.NextFloat() < global.SatelliteCreationProbability)
|
||||
{
|
||||
// Sometimes the satellite is so massive that it forms a binary system (like Earth!)
|
||||
if(random.NextFloat() < context.GlobalData.BinaryCreationProbability)
|
||||
if(random.NextFloat() < global.BinaryCreationProbability)
|
||||
planet.ExpandRosette(2);
|
||||
// Otherwise, terrestrial planets get a couple satellites while gas giants get many
|
||||
else planet.ExpandSolar(
|
||||
count: planet.Mass < context.GlobalData.GasGiantMass ? random.NextInt(1,3) : random.NextInt(4,10),
|
||||
count: planet.Mass < global.GasGiantMass ? random.NextInt(1,3) : random.NextInt(4,10),
|
||||
massMulMin: .75f,
|
||||
massMulMax: 1.5f,
|
||||
distMulMin: 1.05f,
|
||||
|
|
@ -122,7 +177,7 @@ public class ZoneGenerator
|
|||
public class Planet
|
||||
{
|
||||
public Random Random;
|
||||
public GameContext Context;
|
||||
public GlobalData Global;
|
||||
public float Distance;
|
||||
public float Phase;
|
||||
public float Mass;
|
||||
|
|
@ -143,6 +198,8 @@ public class Planet
|
|||
// https://en.wikipedia.org/wiki/Klemperer_rosette
|
||||
public void ExpandRosette(int vertices)
|
||||
{
|
||||
Debug.Log("Expanding Rosette");
|
||||
|
||||
// Rosette children replace the parent, parent orbital node is left empty
|
||||
Empty = true;
|
||||
|
||||
|
|
@ -162,23 +219,23 @@ public class Planet
|
|||
var p1 = OrbitData.Evaluate(1.0f / vertices) * dist;
|
||||
|
||||
// Maximum child distance is half the distance to the neighbor minus the neighbor's radius
|
||||
var p0ChildDist = distance(p0, p1) * proportion - Context.PlanetRadius(sharedMass * (1 - proportion));
|
||||
var p1ChildDist = distance(p0, p1) * (1 - proportion) - Context.PlanetRadius(sharedMass * proportion);
|
||||
var p0ChildDist = (distance(p0, p1) * proportion - Global.PlanetRadius(sharedMass * (1 - proportion))) * .75f;
|
||||
var p1ChildDist = (distance(p0, p1) * (1 - proportion) - Global.PlanetRadius(sharedMass * proportion)) * .75f;
|
||||
|
||||
for (int i = 0; i < vertices; i++)
|
||||
{
|
||||
var child = new Planet
|
||||
{
|
||||
Random = Random,
|
||||
Context = Context,
|
||||
Global = Global,
|
||||
Parent = this,
|
||||
Mass = sharedMass * (i % 2 == 0 ? proportion : 1 - proportion), // Masses alternate
|
||||
Distance = dist,
|
||||
Phase = (float) i / vertices,
|
||||
ChildDistanceMaximum = (i % 2 == 0 ? p0ChildDist : p1ChildDist)
|
||||
};
|
||||
child.ChildDistanceMinimum = Context.PlanetRadius(child.Mass) * 2;
|
||||
child.Period = child.Distance * child.Distance / 100;
|
||||
child.ChildDistanceMinimum = Global.PlanetRadius(child.Mass) * 2;
|
||||
child.Period = pow(child.Distance, Global.OrbitPeriodExponent) * Global.OrbitPeriodMultiplier;
|
||||
Children.Add(child);
|
||||
}
|
||||
|
||||
|
|
@ -188,6 +245,7 @@ public class Planet
|
|||
// Create children that mimic the distribution of planetary masses in the solar system
|
||||
public void ExpandSolar(int count, float massMulMin, float massMulMax, float distMulMin, float distMulMax, float jupiterJump, float massFraction)
|
||||
{
|
||||
Debug.Log("Expanding Solar");
|
||||
// Expansion is impossible when child space is full
|
||||
if (count == 0 || ChildDistanceMaximum < ChildDistanceMinimum)
|
||||
return;
|
||||
|
|
@ -222,25 +280,25 @@ public class Planet
|
|||
var oldDistances = (float[]) distances.Clone();
|
||||
for (var i = 0; i < count; i++)
|
||||
distances[i] = lerp(ChildDistanceMinimum, ChildDistanceMaximum,
|
||||
(oldDistances[i] - oldDistances[0]) / (oldDistances[count - 1] - oldDistances[0])) + Context.PlanetRadius(masses[i]);
|
||||
(oldDistances[i] - oldDistances[0]) / (oldDistances[count - 1] - oldDistances[0])) + Global.PlanetRadius(masses[i]);
|
||||
}
|
||||
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
// Only instantiate children above the mass floor
|
||||
if (masses[i] > Context.GlobalData.MassFloor)
|
||||
if (masses[i] > Global.MassFloor)
|
||||
{
|
||||
var child = new Planet
|
||||
{
|
||||
Random = Random,
|
||||
Context = Context,
|
||||
Global = Global,
|
||||
Parent = this,
|
||||
Mass = masses[i],
|
||||
Distance = distances[i],
|
||||
Phase = Random.NextFloat()
|
||||
};
|
||||
child.Period = child.Distance * child.Distance / 100;
|
||||
child.ChildDistanceMinimum = Context.PlanetRadius(child.Mass) * 2;
|
||||
child.Period = pow(child.Distance, Global.OrbitPeriodExponent) * Global.OrbitPeriodMultiplier;
|
||||
child.ChildDistanceMinimum = Global.PlanetRadius(child.Mass) * 2;
|
||||
// Maximum child distance of child is the smallest distance to either of its neighbors
|
||||
child.ChildDistanceMaximum = min(i == 0 ? child.Distance - ChildDistanceMinimum : child.Distance - distances[i - 1],
|
||||
i < count - 1 ? distances[i + 1] - child.Distance : float.PositiveInfinity);
|
||||
|
|
|
|||
|
|
@ -215,7 +215,7 @@ public class StrategyGameManager : MonoBehaviour
|
|||
_zoneObjects[planet.ID] = planetObject;
|
||||
planetObject.Label.text = planet.Name;
|
||||
planetObject.GravityMesh.gameObject.SetActive(true);
|
||||
planetObject.GravityMesh.transform.localScale = Vector3.one * (planet.GravityRadius * 2 * ZoneSizeScale);
|
||||
planetObject.GravityMesh.transform.localScale = Vector3.one * (pow(planet.Mass,_context.GlobalData.GravityRadiusExponent) * _context.GlobalData.GravityRadiusMultiplier * ZoneSizeScale);
|
||||
var depth = pow(planet.Mass, ZoneMassPower) * ZoneMassScale;
|
||||
if (depth > zoneDepth)
|
||||
zoneDepth = depth;
|
||||
|
|
|
|||
|
|
@ -14,11 +14,13 @@ public class BackgroundRenderer : MonoBehaviour
|
|||
private Material _material;
|
||||
private Material _material2;
|
||||
private float _depth;
|
||||
private float _depth2;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
_camera = GetComponent<Camera>();
|
||||
_depth = Background.position.z;
|
||||
_depth2 = Background2.position.z;
|
||||
_material = Background.GetComponent<MeshRenderer>().material;
|
||||
_material2 = Background2.GetComponent<MeshRenderer>().material;
|
||||
}
|
||||
|
|
@ -28,7 +30,8 @@ public class BackgroundRenderer : MonoBehaviour
|
|||
_material.SetVector("ParallaxDirection", transform.position);
|
||||
_material2.SetVector("ParallaxDirection", transform.position);
|
||||
_ratio = (float) Screen.width / Screen.height;
|
||||
Background2.position = Background.position = new Vector3(transform.position.x, transform.position.y, _depth);
|
||||
Background.position = new Vector3(transform.position.x, transform.position.y, _depth);
|
||||
Background2.position = new Vector3(transform.position.x, transform.position.y, _depth2);
|
||||
var size = _camera.orthographicSize;
|
||||
Background2.localScale = Background.localScale = new Vector3(size * 2 * _ratio, size * 2, 1);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@ public class ZoneRenderer : MonoBehaviour
|
|||
|
||||
var size = _camera.orthographicSize = FollowCamera.orthographicSize;
|
||||
|
||||
GravityRenderer.transform.position = new Vector3(transform.position.x, transform.position.y, 0);
|
||||
GravityRenderer.transform.position = new Vector3(transform.position.x, transform.position.y, .25f);
|
||||
GravityRenderer.transform.localScale = new Vector3(size * 2 * _ratio, size * 2, 1);
|
||||
|
||||
_camera.targetTexture = GravityTexture;
|
||||
|
|
|
|||
130
Assets/Scripts/ZoneGeneratorTest.cs
Normal file
130
Assets/Scripts/ZoneGeneratorTest.cs
Normal file
|
|
@ -0,0 +1,130 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Unity.Mathematics;
|
||||
using UnityEngine;
|
||||
using static Unity.Mathematics.math;
|
||||
using Random = UnityEngine.Random;
|
||||
|
||||
public class ZoneGeneratorTest : MonoBehaviour
|
||||
{
|
||||
public Galaxy Galaxy;
|
||||
public MeshRenderer ZoneBackground;
|
||||
public MeshRenderer ZoneBoundary;
|
||||
public ZoneObject ZoneObjectPrefab;
|
||||
public Texture2D PlanetSprite;
|
||||
public Texture2D GasGiantSprite;
|
||||
public Texture2D SunSprite;
|
||||
public float ZoneSizeScale = .01f;
|
||||
public float ZoneMassScale = .5f;
|
||||
public float ZoneMassPower = .33f;
|
||||
public float ZoneOrbitSpeed = 10;
|
||||
|
||||
private DatabaseCache _cache;
|
||||
private Dictionary<Guid, ZoneObject> _zoneObjects = new Dictionary<Guid, ZoneObject>();
|
||||
private List<ZoneObject> _wormholes = new List<ZoneObject>();
|
||||
private Dictionary<Guid, float2> _orbitPositions = new Dictionary<Guid, float2>();
|
||||
private GameContext _context;
|
||||
private Material _boundaryMaterial;
|
||||
|
||||
// Determine orbital position recursively, caching parent positions to avoid repeated calculations
|
||||
private float2 GetOrbitPosition(Guid orbit)
|
||||
{
|
||||
// Root orbit is fixed at center
|
||||
if(orbit==Guid.Empty)
|
||||
return Unity.Mathematics.float2.zero;
|
||||
|
||||
if (!_orbitPositions.ContainsKey(orbit))
|
||||
{
|
||||
var orbitData = _cache.Get<OrbitData>(orbit);
|
||||
_orbitPositions[orbit] = GetOrbitPosition(orbitData.Parent) + (orbitData.Period < .01f ? Unity.Mathematics.float2.zero :
|
||||
OrbitData.Evaluate(Time.time * ZoneOrbitSpeed / orbitData.Period + orbitData.Phase) *
|
||||
orbitData.Distance);
|
||||
}
|
||||
|
||||
var position = _orbitPositions[orbit];
|
||||
if (float.IsNaN(position.x))
|
||||
{
|
||||
Debug.Log("Orbit position is NaN, something went very wrong!");
|
||||
return Unity.Mathematics.float2.zero;
|
||||
}
|
||||
return _orbitPositions[orbit];
|
||||
}
|
||||
|
||||
void Start()
|
||||
{
|
||||
_cache = new DatabaseCache();
|
||||
_context = new GameContext(_cache);
|
||||
_boundaryMaterial = ZoneBoundary.material;
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
_orbitPositions.Clear();
|
||||
foreach (var planet in _zoneObjects)
|
||||
{
|
||||
var planetData = _cache.Get<PlanetData>(planet.Key);
|
||||
|
||||
planet.Value.transform.position = (Vector2) GetOrbitPosition(planetData.Orbit) * ZoneSizeScale;
|
||||
}
|
||||
if (Input.GetKeyDown(KeyCode.Space))
|
||||
{
|
||||
PopulateZone();
|
||||
}
|
||||
}
|
||||
|
||||
void PopulateZone()
|
||||
{
|
||||
// Delete all the existing zone contents
|
||||
foreach (var zoneObject in _zoneObjects.Values) Destroy(zoneObject.gameObject);//zoneObject.GetComponent<Prototype>().ReturnToPool();
|
||||
_zoneObjects.Clear();
|
||||
foreach (var zoneObject in _wormholes) Destroy(zoneObject.gameObject); //zoneObject.GetComponent<Prototype>().ReturnToPool();
|
||||
_wormholes.Clear();
|
||||
|
||||
float2 position;
|
||||
do
|
||||
{
|
||||
position = float2(Random.value, Random.value);
|
||||
} while (Galaxy.MapData.StarDensity.Evaluate(position, Galaxy.MapData.GlobalData) < .1f);
|
||||
|
||||
var zone = new ZoneData{Context = _context, Name = Guid.NewGuid().ToString(), Position = position};
|
||||
OrbitData[] orbits;
|
||||
PlanetData[] planets;
|
||||
ZoneGenerator.GenerateZone(
|
||||
global: Galaxy.MapData.GlobalData,
|
||||
zone: zone,
|
||||
mass: Galaxy.MapData.ResourceDensities.First(m=>m.Name=="Mass"),
|
||||
radius: Galaxy.MapData.ResourceDensities.First(m=>m.Name=="Radius"),
|
||||
orbitData: out orbits,
|
||||
planetData: out planets);
|
||||
_cache.AddAll(orbits);
|
||||
_cache.AddAll(planets);
|
||||
_cache.Add(zone);
|
||||
|
||||
float zoneDepth = 0;
|
||||
foreach (var planet in planets)
|
||||
{
|
||||
var planetObject = Instantiate(ZoneObjectPrefab, transform);
|
||||
_zoneObjects[planet.ID] = planetObject;
|
||||
planetObject.Label.text = planet.Name;
|
||||
planetObject.GravityMesh.gameObject.SetActive(true);
|
||||
planetObject.GravityMesh.transform.localScale =
|
||||
Vector3.one * (pow(planet.Mass, Galaxy.MapData.GlobalData.GravityRadiusExponent) *
|
||||
Galaxy.MapData.GlobalData.GravityRadiusMultiplier * 2 * ZoneSizeScale);
|
||||
var depth = pow(planet.Mass, ZoneMassPower) * ZoneMassScale;
|
||||
if (depth > zoneDepth)
|
||||
zoneDepth = depth;
|
||||
planetObject.GravityMesh.material.SetFloat("_Depth", depth);
|
||||
planetObject.Icon.material.SetTexture("_MainTex",
|
||||
planet.Mass > Galaxy.MapData.GlobalData.SunMass ? SunSprite :
|
||||
planet.Mass > Galaxy.MapData.GlobalData.GasGiantMass ? GasGiantSprite :
|
||||
PlanetSprite);
|
||||
}
|
||||
|
||||
var radius = (zone.Radius * ZoneSizeScale * 2);
|
||||
ZoneBackground.material.SetFloat("_ClipDistance", radius);
|
||||
ZoneBackground.material.SetFloat("_HeightRange", zoneDepth + _boundaryMaterial.GetFloat("_Depth"));
|
||||
ZoneBoundary.transform.localScale = Vector3.one * radius;
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/ZoneGeneratorTest.cs.meta
Normal file
11
Assets/Scripts/ZoneGeneratorTest.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 0ae6f9307731b8f469a018c69d95d2ba
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Loading…
Reference in a new issue