Skip to content

Commit

Permalink
working on tree growth!
Browse files Browse the repository at this point in the history
  • Loading branch information
AIP21 committed May 14, 2023
1 parent 05e5afa commit ff62827
Show file tree
Hide file tree
Showing 49 changed files with 1,000 additions and 712 deletions.
11 changes: 7 additions & 4 deletions Assets/DataStructure/Scripts/DataStructure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ public class DataStructure : MonoBehaviour, ITickableSystem
#region Public
[Header("Grids")]
public Vector2 OverallSize;

[Tooltip("Highest level grid is the largest grid and has the highest index")]
public List<GridLevel> Levels;
public List<Grid> Grids { get; private set; }

[SerializeField]
private List<Grid> grids = new List<Grid>();
public List<Grid> Grids { get {return grids;} }

[Header("Data")]
public List<GameObject> InitializingObjects = new List<GameObject>();
Expand All @@ -43,6 +45,7 @@ public class DataStructure : MonoBehaviour, ITickableSystem
public int TickInterval { get { return 0; } }
public int ticksSinceLastTick { get; set; }
public bool willTickNow { get; set; }
public bool shouldTick { get { return true; } }
#endregion

#region Debug
Expand Down Expand Up @@ -175,8 +178,8 @@ private void populateGrids(List<GridLevel> levels)
}

// Assign the parent cell to the container cell
cell.parentCell = containerCell;
containerCell.childCells.Add(cell);
cell.ParentCell = containerCell;
containerCell.ChildCells.Add(cell);
}
}

Expand Down Expand Up @@ -502,7 +505,7 @@ private int receiveCellDataFromWriter(IWriteCellData writer)
int level = dataPointer.Item2;

addCellData(level, name, dataToAdd[dataPointer]);

writes++;
}

Expand Down
55 changes: 18 additions & 37 deletions Assets/DataStructure/Scripts/DataStructureDebug.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,42 +53,20 @@ public class DataStructureDebug : MonoBehaviour

public void Start()
{
Debug.Log("Performing tests on data structure...");
Stopwatch st = new Stopwatch();
st.Start();

foreach (Grid grid in ds.Grids)
{
gridColors.Add(grid, UnityEngine.Random.ColorHSV());
}
}

public void FixedUpdate()
{
if (DoQueryTest)
{
DoQueryTest = false;
performQueryTest(QueryTestCount);
}

// if (DoAddDataTest)
// {
// performAddDataTest(AddDataTestCount);
// }

// if (DoRemoveDataTest)
// {
// performRemoveDataTest(RemoveDataTestCount);
// }

// if (DoModifyDataTest)
// {
// performModifyDataTest(ModifyDataTestCount);
// }

st.Stop();

Debug.Log("All tests completed in " + st.ElapsedMilliseconds + " ms");
}

public void FixedUpdate()
{
if (QuerySingleCellTest && QuerySingleCellPosition != lastTestPos)
{
lastTestPos = QuerySingleCellPosition;
Expand All @@ -100,15 +78,18 @@ public void FixedUpdate()
public void performQueryTest(int count)
{
Debug.Log("Performing " + count + " random queries on " + (TestLevel == -1 ? "random level" : ("level " + TestLevel)) + "...");
int startTime = DateTime.Now.Millisecond;

Stopwatch st = new Stopwatch();
st.Start();

for (int i = 0; i < count; i++)
{
Vector2 testPos = new Vector2(UnityEngine.Random.Range(0, ds.OverallSize.x), UnityEngine.Random.Range(0, ds.OverallSize.y));
performDebugQuery(testPos, TestLevel == -1 ? UnityEngine.Random.Range(0, ds.Grids.Count - 1) : TestLevel, false);
}
st.Stop();

Debug.Log(count + " random queries successfully completed in " + (DateTime.Now.Millisecond - startTime) + " ms");
Debug.Log(count + " random queries successfully completed in " + st.Elapsed.TotalMilliseconds + " ms");
}

private void performDebugQuery(Vector2 queryPos, int level = -1, bool fetchData = true)
Expand Down Expand Up @@ -278,12 +259,12 @@ private void OnDrawGizmos()
{
Gizmos.color = Color.green;

Vector3 center = new Vector3(cell.center.x, (cell.level.Level - 1) * LevelZShift, cell.center.y);
Vector3 center = new Vector3(cell.center.x, (cell.Level.Level - 1) * LevelZShift, cell.center.y);

Gizmos.DrawLine(center + new Vector3(-cell.level.CellSize.x / 2, 0, -cell.level.CellSize.y / 2), center + new Vector3(cell.level.CellSize.x / 2, 0, -cell.level.CellSize.y / 2));
Gizmos.DrawLine(center + new Vector3(cell.level.CellSize.x / 2, 0, -cell.level.CellSize.y / 2), center + new Vector3(cell.level.CellSize.x / 2, 0, cell.level.CellSize.y / 2));
Gizmos.DrawLine(center + new Vector3(cell.level.CellSize.x / 2, 0, cell.level.CellSize.y / 2), center + new Vector3(-cell.level.CellSize.x / 2, 0, cell.level.CellSize.y / 2));
Gizmos.DrawLine(center + new Vector3(-cell.level.CellSize.x / 2, 0, cell.level.CellSize.y / 2), center + new Vector3(-cell.level.CellSize.x / 2, 0, -cell.level.CellSize.y / 2));
Gizmos.DrawLine(center + new Vector3(-cell.Level.CellSize.x / 2, 0, -cell.Level.CellSize.y / 2), center + new Vector3(cell.Level.CellSize.x / 2, 0, -cell.Level.CellSize.y / 2));
Gizmos.DrawLine(center + new Vector3(cell.Level.CellSize.x / 2, 0, -cell.Level.CellSize.y / 2), center + new Vector3(cell.Level.CellSize.x / 2, 0, cell.Level.CellSize.y / 2));
Gizmos.DrawLine(center + new Vector3(cell.Level.CellSize.x / 2, 0, cell.Level.CellSize.y / 2), center + new Vector3(-cell.Level.CellSize.x / 2, 0, cell.Level.CellSize.y / 2));
Gizmos.DrawLine(center + new Vector3(-cell.Level.CellSize.x / 2, 0, cell.Level.CellSize.y / 2), center + new Vector3(-cell.Level.CellSize.x / 2, 0, -cell.Level.CellSize.y / 2));
}
}
}
Expand All @@ -305,9 +286,9 @@ public void debugDrawGrid(Grid toDraw, float levelZShift)
{
// For each neighbor, draw a small line to the edge of this cell
Gizmos.color = Color.white;
for (int i = 0; i < cell.neighbors.Length; i++)
for (int i = 0; i < cell.Neighbors.Length; i++)
{
if (cell.neighbors[i] != null)
if (cell.Neighbors[i] != null)
{
if (i == 0)
{ // above
Expand All @@ -332,10 +313,10 @@ public void debugDrawGrid(Grid toDraw, float levelZShift)
if (DrawParentLines)
{
// Draw a line to the parent cell
if (cell.parentCell != null)
if (cell.ParentCell != null)
{
Gizmos.color = Color.red;
Gizmos.DrawLine(center, new Vector3(cell.parentCell.center.x, (cell.parentCell.level.Level - 1) * levelZShift, cell.parentCell.center.y));
Gizmos.DrawLine(center, new Vector3(cell.ParentCell.center.x, (cell.ParentCell.Level.Level - 1) * levelZShift, cell.ParentCell.center.y));
}
}
}
Expand Down
40 changes: 26 additions & 14 deletions Assets/DataStructure/Scripts/Grid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class Grid
public Grid childGrid;

private List<GridCell> cells;
private List<GridCell> childCells;
// private List<GridCell> childCells;

public int xCellCount, yCellCount;

Expand All @@ -32,7 +32,7 @@ public Grid(Vector2 size, GridLevel gridLevel)
this.gridLevel = gridLevel;

this.cells = new List<GridCell>();
this.childCells = new List<GridCell>();
// this.childCells = new List<GridCell>();

this.xCellCount = (int)(size.x / gridLevel.CellSize.x);
this.yCellCount = (int)(size.y / gridLevel.CellSize.y);
Expand All @@ -55,21 +55,21 @@ private void populate()
// Assign this cell's neighbors to the cell above and to the left
if (y > 0)
{
cell.neighbors[0] = cells[cells.Count - xCellCount - 1];
cell.Neighbors[0] = cells[cells.Count - xCellCount - 1];
}
if (x > 0)
{
cell.neighbors[1] = cells[cells.Count - 2];
cell.Neighbors[1] = cells[cells.Count - 2];
}

// Assign the cell above and to the left's neighbors to this cell
if (y > 0)
{
cells[cells.Count - xCellCount - 1].neighbors[2] = cell;
cells[cells.Count - xCellCount - 1].Neighbors[2] = cell;
}
if (x > 0)
{
cells[cells.Count - 2].neighbors[3] = cell;
cells[cells.Count - 2].Neighbors[3] = cell;
}
}
}
Expand Down Expand Up @@ -257,7 +257,7 @@ public void SetGridData(string dataName, object data)
{
gridData[dataName].SetData(data);
}
else
else // TODO: Maybe add a thing here to create a new entry if it doesn't exist?
Debug.Log("Grid data \"" + dataName + "\" does not exist.");
}

Expand Down Expand Up @@ -372,22 +372,34 @@ public void RemoveCellData(string dataName)
#endregion
}

[Serializable]
// [Serializable]
public class GridCell
{
#region Public
public Vector2 center;
public Bounds bounds;
public GridLevel level;
private GridLevel level;
[HideInInspector]
public GridLevel Level { get { return level; } }

// References
public GridCell parentCell;
public List<GridCell> childCells = new List<GridCell>();
public GridCell[] neighbors = new GridCell[4]; // 0 = above, 1 = left, 2 = below, 3 = right
private GridCell parentCell;
[HideInInspector]
public GridCell ParentCell { get { return parentCell; } set { parentCell = value; } }

[HideInInspector]
private List<GridCell> childCells = new List<GridCell>();
[HideInInspector]
public List<GridCell> ChildCells { get { return childCells; } }

private GridCell[] neighbors = new GridCell[4]; // 0 = above, 1 = left, 2 = below, 3 = right
[HideInInspector]
public GridCell[] Neighbors { get { return neighbors; } } // 0 = above, 1 = left, 2 = below, 3 = right
#endregion

#region Private
// Data
[SerializeField]
private GenericDictionary<string, List<AbstractCellData>> data = new GenericDictionary<string, List<AbstractCellData>>();
#endregion

Expand All @@ -406,8 +418,8 @@ public bool Contains(Vector2 queryPos)
return false;
}

return (queryPos.x >= center.x - level.CellSize.x / 2 && queryPos.x <= center.x + level.CellSize.x / 2 &&
queryPos.y >= center.y - level.CellSize.y / 2 && queryPos.y <= center.y + level.CellSize.y / 2);
return (queryPos.x >= center.x - Level.CellSize.x / 2 && queryPos.x <= center.x + Level.CellSize.x / 2 &&
queryPos.y >= center.y - Level.CellSize.y / 2 && queryPos.y <= center.y + Level.CellSize.y / 2);
}

public GridCell GetChild(Vector2 queryPos)
Expand Down
Loading

0 comments on commit ff62827

Please sign in to comment.