Commit 1ba11f7e by Naliwe GS

Almost done on cellular automaton. TODO: find better delegates

parent 150aa995
<?xml version="1.0" encoding="UTF-8"?>
<module type="RIDER_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$USER_HOME$/AppData/Local/JetBrains/Shared/v06/DecompilerCache/decompiler/F6949EC5-53D4-4DE3-9F54-5BAB8EBF397B/bf/c7df5298/Vector2.cs">
<sourceFolder url="file://$USER_HOME$/AppData/Local/JetBrains/Shared/v06/DecompilerCache/decompiler/F6949EC5-53D4-4DE3-9F54-5BAB8EBF397B/bf/c7df5298/Vector2.cs" isTestSource="false" />
</content>
<content url="file://$MODULE_DIR$/..">
<sourceFolder url="file://$MODULE_DIR$/.." isTestSource="false" />
</content>
......
fileFormatVersion: 2
guid: 868ebc93d24347047afd4a4810d2a648
timeCreated: 1470316413
licenseType: Free
NativeFormatImporter:
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: 0c5304ecebff7f94ba6393ce4aa43046
timeCreated: 1470316536
licenseType: Free
NativeFormatImporter:
userData:
assetBundleName:
assetBundleVariant:
using Assets.Scripts.Utils;
using Assets.Scripts.MapGeneration;
using Assets.Scripts.Utils;
using UnityEngine;
namespace Assets.Scripts
......@@ -7,19 +8,30 @@ namespace Assets.Scripts
{
public static int PoolSize = 256;
private Map _map;
private ObjectPool _pooledWalls;
private ObjectPool _pooledActors;
private ObjectPool _pooledProjectiles;
private CellularAutomaton _ca;
void Start()
{
_map = new Map(128, 128);
_pooledActors = new ObjectPool(PoolSize);
_pooledProjectiles = new ObjectPool(PoolSize);
_pooledWalls = new ObjectPool(PoolSize);
_ca = new CellularAutomaton(_map, TileType.Wall);
}
void Update()
{
_ca.Step();
}
}
}
\ No newline at end of file
using Assets.Scripts.Utils;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Assets.Scripts.Utils;
using UnityEngine;
namespace Assets.Scripts.MapGeneration
{
internal class DefaultCellDelegates
{
public static bool DefaultCanGrow(Cell cell)
{
return true;
}
public static bool DefaultWillGrow(Cell cell)
{
return true;
}
public static Cell DefaultPickTarget(Map map, Cell origin)
{
return CellularAutomaton.GetNeighbors(map, origin).FirstOrDefault();
}
}
public class CellularAutomaton
{
#region Delegates
public delegate IEnumerable<Cell> Filter(Map map);
public delegate bool CanGrow(Cell cell);
public delegate bool WillGrow(Cell cell);
public delegate Cell Picktarget(Map map, Cell origin);
#endregion
#region Fields
Map _map;
private Map _map;
private List<Cell> _toProcess;
private CanGrow _canGrowRule;
private WillGrow _willGrowRule;
private Picktarget _pickTargetRule;
private TileType _targetType;
#endregion
#region Ctors
public CellularAutomaton(Map map, TileType targetType, int startX = 0, int startY = 0)
{
_map = map;
_targetType = targetType;
_canGrowRule = DefaultCellDelegates.DefaultCanGrow;
_willGrowRule = DefaultCellDelegates.DefaultWillGrow;
_pickTargetRule = DefaultCellDelegates.DefaultPickTarget;
_toProcess = new List<Cell> {new Cell(startX, startY, targetType)};
}
#endregion
#region Methods
public void Step()
{
var newCells = new List<Cell>();
foreach (var cell in _toProcess)
{
if (!_canGrowRule(cell) || !_willGrowRule(cell))
continue;
Grow(_pickTargetRule(_map, cell), _targetType);
newCells.Add(cell);
}
_toProcess = newCells;
}
public static IEnumerable<Cell> GetNeighbors(Map map, Cell origin)
{
var ret = new List<Cell>();
for (var x = origin.Position.x - 1; x <= origin.Position.x + 1; x++)
{
for (var y = origin.Position.y - 1; y <= origin.Position.y + 1; y++)
{
Debug.Log(string.Format("Processing cell {0}:{1}", x, y));
if ((Math.Abs(x - origin.Position.x) > .1f || Math.Abs(y - origin.Position.y) > .1f)
&& IsInMapRange(map, (int) x, (int) y) && map[(uint) x, (uint) y] != origin.Type)
{
ret.Add(new Cell((int) x, (int) y, map[(uint) x, (uint) y]));
}
}
}
Debug.Log(string.Format("{0}", ret.Count));
return ret;
}
private void Grow(Cell targetCell, TileType targetType)
{
targetCell.Type = targetType;
}
private static bool IsInMapRange(Map map, int x, int y)
{
Debug.Log(string.Format("Is cell {0}:{1} in map range", x, y));
return (x >= 0 && x < map.Columns &&
y >= 0 && y < map.Rows);
}
#endregion
}
......
......@@ -26,6 +26,14 @@
Rows = rows;
_map = new TileType[Columns, Rows];
for (int x = 0; x < _map.GetLength(0); x++)
{
for (int y = 0; y < _map.GetLength(1); y++)
{
_map[x, y] = TileType.Default;
}
}
}
#endregion
......
......@@ -14,10 +14,6 @@ namespace Assets.Scripts.Utils
public ObjectPool(int size)
{
Size = size;
}
public void Start()
{
Pool = new List<GameObject>();
for (var i = 0; i < Pool.Count; i++)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment