Skip to content
This repository has been archived by the owner on Jul 3, 2024. It is now read-only.

Commit

Permalink
Fixed serialization of Hexcoords
Browse files Browse the repository at this point in the history
  • Loading branch information
Amatsugu committed Jun 13, 2020
1 parent e4938d5 commit e56a214
Show file tree
Hide file tree
Showing 21 changed files with 83 additions and 80 deletions.
6 changes: 5 additions & 1 deletion Assets/Scripts/Data Store/ConduitGraph.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Amatsugu.Phos.DataStore;

using Newtonsoft.Json;

using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -331,7 +333,7 @@ PathNode BestFScore(HashSet<PathNode> nodes)
List<Vector3> path = new List<Vector3>();
do
{
var cPos = cur.node.conduitPos.world;
var cPos = cur.node.conduitPos.WorldPos;
cPos.y = cur.node.height;
path.Add(cPos);
cur = cur.src;
Expand All @@ -352,7 +354,9 @@ public class ConduitNode
internal int[] _connections;

public int ConnectionCount { get; private set; }
[JsonIgnore]
public bool IsEmpty => ConnectionCount == 0;
[JsonIgnore]
public bool IsFull => ConnectionCount == maxConnections;

public ConduitNode(int id, HexCoords pos, float height, int maxConnections = 6)
Expand Down
4 changes: 2 additions & 2 deletions Assets/Scripts/Game Logic/Systems/PathFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public struct PathNode : IComparer<PathNode>, IEquatable<PathNode>
public PathNode(HexCoords coords, float height, int g, float f = 0)
{
this.coords = coords;
surfacePoint = coords.world;
surfacePoint = coords.WorldPos;
surfacePoint.y = height;
G = g;
isCreated = true;
Expand All @@ -172,7 +172,7 @@ public void GetNeighbors(ref NativeArray<HexCoords> neighbors, float innerRadius
{
for (int i = 0; i < 6; i++)
{
var n = coords.GetNeighbor(i, innerRadius);
var n = coords.GetNeighbor(i);
if (navData.ContainsKey(n))
neighbors[i] = n;
else
Expand Down
4 changes: 2 additions & 2 deletions Assets/Scripts/Game Logic/UI/GameUI/UIActionsPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,14 @@ private void IssueMoveOrder(Tile tile)
var occupiedSet = new HashSet<HexCoords>();
var openSet = new HashSet<HexCoords>();

var openTiles = HexCoords.SpiralSelect(tile.Coords, r, innerRadius: _map.innerRadius);
var openTiles = HexCoords.SpiralSelect(tile.Coords, r);
for (int i = 0; i < openTiles.Length; i++)
openSet.Add(openTiles[i]);
for (int i = 0; i < orderedUnits.Length; i++)
{
for (int j = 0; j < openTiles.Length; j++)
{
var footprint = HexCoords.SpiralSelect(openTiles[j], ((IMoveable)orderedUnits[i]).GetSize(), innerRadius: _map.innerRadius);
var footprint = HexCoords.SpiralSelect(openTiles[j], ((IMoveable)orderedUnits[i]).GetSize());
if (IsValidFootPrint(footprint, openSet, occupiedSet))
{
for (int x = 0; x < footprint.Length; x++)
Expand Down
8 changes: 4 additions & 4 deletions Assets/Scripts/Game Logic/UI/GameUI/UISelectionPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,10 @@ public void UpdateState()
private void DisplaySelectionRect()
{
//Drag Select
var p0 = _start.Coords.world;
var p1 = HexCoords.OffsetToWorldPosXZ(_end.Coords.offsetCoords.x, _start.Coords.offsetCoords.y, _map.innerRadius, _map.tileEdgeLength);
var p2 = HexCoords.OffsetToWorldPosXZ(_start.Coords.offsetCoords.x, _end.Coords.offsetCoords.y, _map.innerRadius, _map.tileEdgeLength);
var p3 = _end.Coords.world;
var p0 = _start.Coords.WorldPos;
var p1 = HexCoords.OffsetToWorldPosXZ(_end.Coords.OffsetCoords.x, _start.Coords.OffsetCoords.y, _map.innerRadius, _map.tileEdgeLength);
var p2 = HexCoords.OffsetToWorldPosXZ(_start.Coords.OffsetCoords.x, _end.Coords.OffsetCoords.y, _map.innerRadius, _map.tileEdgeLength);
var p3 = _end.Coords.WorldPos;
#if DEBUG
p0.y = p1.y = p2.y = p3.y = (_start.Height + _end.Height) / 2f;
Debug.DrawLine(p0, p1, Color.white);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private void GrowIndicators(MeshEntity indicatorMesh, int count)

public void ShowRange(Tile center, int range, MeshEntityRotatable border)
{
var ring = HexCoords.SelectRing(center.Coords, range, center.map.innerRadius);
var ring = HexCoords.SelectRing(center.Coords, range);
for (int i = 0; i < ring.Length; i++)
{
//TODO: Figure out how to render this
Expand Down Expand Up @@ -145,7 +145,7 @@ public void ShowLines(MeshEntityRotatable line, Vector3 src, List<ConduitNode> n
if (i >= _renderedEntities[line])
_EM.RemoveComponent<FrozenRenderSceneTag>(_indicatorEntities[line][i]);

var pos = nodes[j].conduitPos.world + new float3(0, nodes[j].height, 0);
var pos = nodes[j].conduitPos.WorldPos + new float3(0, nodes[j].height, 0);
LineFactory.UpdateStaticLine(_indicatorEntities[line][i], src, pos, thiccness);
c++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class PlacementValidator : ScriptableObject

public virtual bool ValidatePlacement(Map map, HexCoords pos, BuildingTileEntity buildingTile, IndicatorManager indicatorManager)
{
var tilesToOccupy = HexCoords.SpiralSelect(pos, buildingTile.size, innerRadius: map.innerRadius);
var tilesToOccupy = HexCoords.SpiralSelect(pos, buildingTile.size);
bool outOfBounds = false;
for (int i = 0; i < tilesToOccupy.Length; i++)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public override bool ValidatePlacement(Map map, HexCoords pos, BuildingTileEntit
indicatorManager.floatingText.rectTransform.position = GameRegistry.Camera.WorldToScreenPoint(map[pos].SurfacePoint) + new Vector3(0, 15);
bool cannotGather = _resInRange.Count > 0;
bool cannotPlace = false;
var tilesToOccupy = HexCoords.SpiralSelect(pos, buildingTile.size, innerRadius: map.innerRadius);
var tilesToOccupy = HexCoords.SpiralSelect(pos, buildingTile.size);
foreach (var tiles in _resTiles)
{
for (int i = 0; i < tiles.Value.Count; i++)
Expand Down
66 changes: 33 additions & 33 deletions Assets/Scripts/Map/HexCoords.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,26 @@ public struct HexCoords : IEquatable<HexCoords>
public readonly float edgeLength;

//World Pos
public readonly float3 world;
[JsonIgnore]
public float3 WorldPos {
get
{
var (worldX, worldZ) = OffsetToWorldPos(OffsetCoords.x, OffsetCoords.y, HexCoords.CalculateInnerRadius(edgeLength), edgeLength);
return new float3(worldX, 0, worldZ);
}
}

//Offsets
public readonly int2 offsetCoords;
[JsonIgnore]
public int2 OffsetCoords => new int2(X + Y /2, Y);

public readonly bool isCreated;

public HexCoords(int x, int y, float edgeLength, float? innerRadius = null)
public HexCoords(int x, int y, float edgeLength)
{
this.X = x;
this.Y = y;
this.edgeLength = edgeLength;
var innerR = (innerRadius ?? Mathf.Sqrt(3f) / 2f * this.edgeLength);
offsetCoords.x = x + y / 2;
offsetCoords.y = y;
//worldX = (offsetX + offsetZ * .5f - offsetZ / 2) * (innerRadius * 2f);
//worldZ = offsetZ * (this.edgeLength * 1.5f);
var (worldX, worldZ) = OffsetToWorldPos(offsetCoords.x, offsetCoords.y, innerR, edgeLength);

world = new float3(worldX, 0, worldZ);
isCreated = true;
}

Expand Down Expand Up @@ -77,7 +77,7 @@ public static HexCoords FromPosition(Vector3 position, float edgeLength = 1)
x -= offset;
int iX = Mathf.RoundToInt(x);
int iY = Mathf.RoundToInt(-x - z);
return new HexCoords(iX, iY, edgeLength, innerRadius);
return new HexCoords(iX, iY, edgeLength);
}

public HexCoords ToChunkLocalCoord()
Expand All @@ -86,11 +86,11 @@ public HexCoords ToChunkLocalCoord()
return ToChunkLocalCoord(x, z);
}

public HexCoords TranslateOffset(int x, int z) => FromOffsetCoords(offsetCoords.x + x, offsetCoords.y + z, edgeLength);
public HexCoords TranslateOffset(int x, int z) => FromOffsetCoords(OffsetCoords.x + x, OffsetCoords.y + z, edgeLength);

public HexCoords ToChunkLocalCoord(int chunkX, int chunkZ) => FromOffsetCoords(offsetCoords.x - (chunkX * MapChunk.SIZE), offsetCoords.y - (chunkZ * MapChunk.SIZE), edgeLength);
public HexCoords ToChunkLocalCoord(int chunkX, int chunkZ) => FromOffsetCoords(OffsetCoords.x - (chunkX * MapChunk.SIZE), OffsetCoords.y - (chunkZ * MapChunk.SIZE), edgeLength);

public (int chunkX, int chunkZ) GetChunkPos() => (Mathf.FloorToInt((float)offsetCoords.x / MapChunk.SIZE), Mathf.FloorToInt((float)offsetCoords.y / MapChunk.SIZE));
public (int chunkX, int chunkZ) GetChunkPos() => (Mathf.FloorToInt((float)OffsetCoords.x / MapChunk.SIZE), Mathf.FloorToInt((float)OffsetCoords.y / MapChunk.SIZE));

public int GetChunkIndex(int width)
{
Expand All @@ -104,9 +104,9 @@ public int GetChunkIndex(int width)

public int Distance(HexCoords b) => (math.abs(X - b.X) + math.abs(Y - b.Y) + math.abs(Z - b.Z)) / 2;

public float DistanceToSq(HexCoords b) => math.lengthsq(world - b.world);
public float DistanceToSq(HexCoords b) => math.lengthsq(WorldPos - b.WorldPos);

public static float DistanceSq(HexCoords a, HexCoords b) => math.lengthsq(a.world - b.world);
public static float DistanceSq(HexCoords a, HexCoords b) => math.lengthsq(a.WorldPos - b.WorldPos);

public static (float X, float Z) OffsetToWorldPos(int x, int z, float innerRadius, float edgeLength)
{
Expand Down Expand Up @@ -168,7 +168,7 @@ public static HexCoords[] HexSelect(HexCoords center, int radius, bool excludeCe
return selection;
}

public static HexCoords[] SpiralSelect(HexCoords center, int radius, bool excludeCenter = false, float? innerRadius = null)
public static HexCoords[] SpiralSelect(HexCoords center, int radius, bool excludeCenter = false)
{
int count = GetTileCount(radius);
if (excludeCenter)
Expand All @@ -179,30 +179,30 @@ public static HexCoords[] SpiralSelect(HexCoords center, int radius, bool exclud
selection[c++] = center;
for (int k = 0; k <= radius; k++)
{
var item = center.Scale(4, k, innerRadius);
var item = center.Scale(4, k);
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < k; j++)
{
selection[c++] = item;
item = item.GetNeighbor(i, innerRadius);
item = item.GetNeighbor(i);
}
}
}
return selection;
}

public static HexCoords[] SelectRing(HexCoords center, int radius, float? innerRadius = null)
public static HexCoords[] SelectRing(HexCoords center, int radius)
{
var items = new HexCoords[6 * radius];
int c = 0;
var item = center.Scale(4, radius, innerRadius);
var item = center.Scale(4, radius);
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < radius; j++)
{
items[c++] = item;
item = item.GetNeighbor(i, innerRadius);
item = item.GetNeighbor(i);
}
}
return items;
Expand All @@ -218,31 +218,31 @@ public static HexCoords[] SelectRing(HexCoords center, int radius, float? innerR
new int3( 0, -1, +1),
};

public HexCoords Scale(int dir, int radius, float? innerRadius = null)
public HexCoords Scale(int dir, int radius)
{
var s = DIRECTIONS[dir] * radius;
return new HexCoords(X + s.x, Y + s.y, edgeLength, innerRadius);
return new HexCoords(X + s.x, Y + s.y, edgeLength);
}

public HexCoords GetNeighbor(int dir, float? innerRadius = null)
public HexCoords GetNeighbor(int dir)
{
var d = DIRECTIONS[dir];
return new HexCoords(X + d.x, Y + d.y, edgeLength, innerRadius);
return new HexCoords(X + d.x, Y + d.y, edgeLength);
}

public static HexCoords[] GetNeighbors(HexCoords center, float? innerRadius = null)
public static HexCoords[] GetNeighbors(HexCoords center)
{
HexCoords[] neighbors = new HexCoords[6];
for (int i = 0; i < 6; i++)
neighbors[i] = center.GetNeighbor(i, innerRadius);
neighbors[i] = center.GetNeighbor(i);
return neighbors;
}

public bool IsInBounds(int height, int widht)
{
if (0 > offsetCoords.y || height <= offsetCoords.y)
if (0 > OffsetCoords.y || height <= OffsetCoords.y)
return false;
if (0 > offsetCoords.x || widht <= offsetCoords.x)
if (0 > OffsetCoords.x || widht <= OffsetCoords.x)
return false;
return true;
}
Expand All @@ -252,8 +252,8 @@ public bool IsInBounds(int height, int widht)
public override int GetHashCode()
{
int hash = 23;
hash = hash * prime + offsetCoords.x;
hash = hash * prime + offsetCoords.y;
hash = hash * prime + OffsetCoords.x;
hash = hash * prime + OffsetCoords.y;
return hash;
}

Expand Down
18 changes: 9 additions & 9 deletions Assets/Scripts/Map/Map.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public Tile this[HexCoords coord]
{
get
{
return this[new HexCoords(x, y, tileEdgeLength, innerRadius)];
return this[new HexCoords(x, y, tileEdgeLength)];
}
}

Expand Down Expand Up @@ -178,7 +178,7 @@ public List<Tile> HexSelect(HexCoords center, int radius, bool excludeCenter = f
selection.Add(this[center]);
return selection;
}
var coords = HexCoords.SpiralSelect(center, radius, excludeCenter, innerRadius);
var coords = HexCoords.SpiralSelect(center, radius, excludeCenter);
for (int i = 0; i < coords.Length; i++)
selection.Add(this[coords[i]]);

Expand All @@ -190,7 +190,7 @@ public void HexSelectForEach(HexCoords center, int radius, Action<Tile> action,
radius = Mathf.Abs(radius);
if (radius == 0)
return;
var coords = HexCoords.SpiralSelect(center, radius, excludeCenter, innerRadius);
var coords = HexCoords.SpiralSelect(center, radius, excludeCenter);
for (int i = 0; i < coords.Length; i++)
{
var t = this[coords[i]];
Expand All @@ -205,7 +205,7 @@ public void HexSelectForEach(HexCoords center, int radius, Func<Tile, bool> acti
radius = Mathf.Abs(radius);
if (radius == 0)
return;
var coords = HexCoords.SpiralSelect(center, radius, excludeCenter, innerRadius);
var coords = HexCoords.SpiralSelect(center, radius, excludeCenter);
for (int i = 0; i < coords.Length; i++)
{
var t = this[coords[i]];
Expand Down Expand Up @@ -260,8 +260,8 @@ public List<Tile> CircularSelect(HexCoords center, float radius)
{
for (float z = -radius; z < radius; z++)
{
var p = HexCoords.FromPosition(new Vector3(x + center.world.x, 0, z + center.world.z), innerRadius);
var d = Mathf.Pow(p.world.x - center.world.x, 2) + Mathf.Pow(p.world.z - center.world.z, 2);
var p = HexCoords.FromPosition(new Vector3(x + center.WorldPos.x, 0, z + center.WorldPos.z), innerRadius);
var d = Mathf.Pow(p.WorldPos.x - center.WorldPos.x, 2) + Mathf.Pow(p.WorldPos.z - center.WorldPos.z, 2);
if (d <= radius * radius)
{
var t = this[p];
Expand Down Expand Up @@ -300,7 +300,7 @@ public void CircularFlatten(HexCoords center, float innerRadius, float outerRadi
var outerSelection = CircularSelect(center, outerRadius).Except(innerSelection);
foreach (var tile in outerSelection)
{
var d = math.pow(center.world.x - tile.Coords.world.x, 2) + math.pow(center.world.z - tile.Coords.world.z, 2);
var d = math.pow(center.WorldPos.x - tile.Coords.WorldPos.x, 2) + math.pow(center.WorldPos.z - tile.Coords.WorldPos.z, 2);
d -= innerRadius * innerRadius;
d = MathUtils.Remap(d, 0, (outerRadius * outerRadius) - (innerRadius * innerRadius), 0, 1);
tile.UpdateHeight(math.lerp(tile.Height, height, 1 - d));
Expand Down Expand Up @@ -329,7 +329,7 @@ public void HexFlatten(HexCoords center, int innerRadius, int outerRadius, Flatt
outerSelection = outerSelection.Where(t => !t.IsUnderwater);
foreach (var tile in outerSelection)
{
var d = math.pow(center.world.x - tile.Coords.world.x, 2) + Mathf.Pow(center.world.z - tile.Coords.world.z, 2);
var d = math.pow(center.WorldPos.x - tile.Coords.WorldPos.x, 2) + Mathf.Pow(center.WorldPos.z - tile.Coords.WorldPos.z, 2);
d -= innerRadius * innerRadius * longDiagonal;
d = MathUtils.Remap(d, 0, (outerRadius * outerRadius * longDiagonal) - (innerRadius * innerRadius * longDiagonal), 0, 1);
tile.UpdateHeight(math.lerp(tile.Height, height, 1 - d));
Expand Down Expand Up @@ -366,7 +366,7 @@ public void RevertTile(Tile tile)

public int GetDistance(HexCoords a, HexCoords b)
{
var dst = math.length(a.world - b.world);
var dst = math.length(a.WorldPos - b.WorldPos);
var tileDist = Mathf.RoundToInt(dst / (innerRadius * 2));
return tileDist;
}
Expand Down
4 changes: 2 additions & 2 deletions Assets/Scripts/Map/MapChunk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public MapChunk(Map map, int offsetX, int offsetZ, float tileEdgeLength, float s
var worldCoord = HexCoords.FromOffsetCoords(offsetX * SIZE, offsetZ * SIZE, tileEdgeLength);
_bounds = new Bounds
{
min = worldCoord.world,
max = worldCoord.world + new float3(SIZE * shortDiagonal, 100, SIZE * 1.5f)
min = worldCoord.WorldPos,
max = worldCoord.WorldPos + new float3(SIZE * shortDiagonal, 100, SIZE * 1.5f)
};
_chunkTiles = new NativeArray<Entity>(SIZE * SIZE, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
}
Expand Down
5 changes: 2 additions & 3 deletions Assets/Scripts/Map/SerializedMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public class SerializedMap
public string name;
public SerializedTile[] tiles;
public SerializedUnit[] units;
[JsonIgnore] //TODO: solve issues with serialization of float3
public SerializedConduitGrapth conduitGrapth;

public Map Deserialize(TileDatabase tileDb, UnitDatabase unitDb)
Expand All @@ -38,7 +37,7 @@ public Map Deserialize(TileDatabase tileDb, UnitDatabase unitDb)
for (int i = 0; i < tiles.Length; i++)
{
var curTile = tiles[i];
var pos = new HexCoords(curTile.x, curTile.y, tileEdgeLength, map.innerRadius);
var pos = new HexCoords(curTile.x, curTile.y, tileEdgeLength);
map[pos] = tileDb.tileEntites[curTile.tileId].tile.CreateTile(map, pos, curTile.height);
if (curTile.origTile != -1)
map[pos].originalTile = tileDb.tileEntites[curTile.origTile].tile;
Expand All @@ -49,7 +48,7 @@ public Map Deserialize(TileDatabase tileDb, UnitDatabase unitDb)
for (int i = 0; i < units.Length; i++)
{
var sUnit = units[i];
map.AddUnit(unitDb.unitEntites[sUnit.unitId].unit, map[new HexCoords(sUnit.x, sUnit.y, tileEdgeLength, map.innerRadius)], sUnit.faction);
map.AddUnit(unitDb.unitEntites[sUnit.unitId].unit, map[new HexCoords(sUnit.x, sUnit.y, tileEdgeLength)], sUnit.faction);
}
return map;
}
Expand Down
Loading

0 comments on commit e56a214

Please sign in to comment.