From 1ba11f7ebc4dc8bd2f3ad540777c7345deb6615a Mon Sep 17 00:00:00 2001 From: Naliwe GS Date: Fri, 5 Aug 2016 14:53:09 +0200 Subject: [PATCH] Almost done on cellular automaton. TODO: find better delegates --- .idea.igem-quantifly/.idea/workspace.xml | 394 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------------------------------------------------------------------------------ .idea.igem-quantifly/riderModule.iml | 3 +++ Assets/Materials/Ground - Block.mat | Bin 0 -> 5028 bytes Assets/Materials/Ground - Block.mat.meta | 8 ++++++++ Assets/Prefabs/BlockGround.prefab | Bin 0 -> 6720 bytes Assets/Prefabs/BlockGround.prefab.meta | 8 ++++++++ Assets/Scripts/GameController.cs | 14 +++++++++++++- Assets/Scripts/MapGeneration/CellularAutomaton.cs | 118 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- Assets/Scripts/Utils/Map.cs | 8 ++++++++ Assets/Scripts/Utils/ObjectPool.cs | 4 ---- 10 files changed, 436 insertions(+), 121 deletions(-) create mode 100644 Assets/Materials/Ground - Block.mat create mode 100644 Assets/Materials/Ground - Block.mat.meta create mode 100644 Assets/Prefabs/BlockGround.prefab create mode 100644 Assets/Prefabs/BlockGround.prefab.meta diff --git a/.idea.igem-quantifly/.idea/workspace.xml b/.idea.igem-quantifly/.idea/workspace.xml index 969160e..591d499 100644 --- a/.idea.igem-quantifly/.idea/workspace.xml +++ b/.idea.igem-quantifly/.idea/workspace.xml @@ -3,6 +3,12 @@ + + + + + + @@ -28,12 +34,26 @@ - - + + - - - + + + + + + + + + + + + + + + + + @@ -43,18 +63,18 @@ - - + + - - + + - - - - - - + + + + + + @@ -71,18 +91,20 @@ - @@ -129,16 +151,105 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - + @@ -178,11 +289,13 @@ + + - @@ -194,29 +307,29 @@ - + - - - + - - + + + + + + - - @@ -254,153 +367,206 @@ - - + - - - + + + + + + + + + + + + + + + + + + + + + - + - - + + - - - - - - + + + + + + - + - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - + + - - + + + - - - + + - + - - - - - - - - - - + + + - + - - + + - - - - - + + + + + + - + - - + + - - - - + + + + + - + - - + + - - - - - - + + + + + - + - - + + - - - + + + + + + - + - - + + - - - - - - + + + + + + + + + + + + + + + + + diff --git a/.idea.igem-quantifly/riderModule.iml b/.idea.igem-quantifly/riderModule.iml index fa0f0f3..b3d692f 100644 --- a/.idea.igem-quantifly/riderModule.iml +++ b/.idea.igem-quantifly/riderModule.iml @@ -1,6 +1,9 @@ + + + diff --git a/Assets/Materials/Ground - Block.mat b/Assets/Materials/Ground - Block.mat new file mode 100644 index 0000000..3b8c134 Binary files /dev/null and b/Assets/Materials/Ground - Block.mat differ diff --git a/Assets/Materials/Ground - Block.mat.meta b/Assets/Materials/Ground - Block.mat.meta new file mode 100644 index 0000000..b61db5f --- /dev/null +++ b/Assets/Materials/Ground - Block.mat.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 868ebc93d24347047afd4a4810d2a648 +timeCreated: 1470316413 +licenseType: Free +NativeFormatImporter: + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Prefabs/BlockGround.prefab b/Assets/Prefabs/BlockGround.prefab new file mode 100644 index 0000000..0c6caf8 Binary files /dev/null and b/Assets/Prefabs/BlockGround.prefab differ diff --git a/Assets/Prefabs/BlockGround.prefab.meta b/Assets/Prefabs/BlockGround.prefab.meta new file mode 100644 index 0000000..e8f2bf3 --- /dev/null +++ b/Assets/Prefabs/BlockGround.prefab.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 0c5304ecebff7f94ba6393ce4aa43046 +timeCreated: 1470316536 +licenseType: Free +NativeFormatImporter: + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/GameController.cs b/Assets/Scripts/GameController.cs index 0625276..8d59ea5 100644 --- a/Assets/Scripts/GameController.cs +++ b/Assets/Scripts/GameController.cs @@ -1,4 +1,5 @@ -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 diff --git a/Assets/Scripts/MapGeneration/CellularAutomaton.cs b/Assets/Scripts/MapGeneration/CellularAutomaton.cs index 0a84635..cf1717c 100644 --- a/Assets/Scripts/MapGeneration/CellularAutomaton.cs +++ b/Assets/Scripts/MapGeneration/CellularAutomaton.cs @@ -1,12 +1,126 @@ -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 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 _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 {new Cell(startX, startY, targetType)}; + } + + #endregion + + #region Methods + + public void Step() + { + var newCells = new List(); + + foreach (var cell in _toProcess) + { + if (!_canGrowRule(cell) || !_willGrowRule(cell)) + continue; + + Grow(_pickTargetRule(_map, cell), _targetType); + + newCells.Add(cell); + } + + _toProcess = newCells; + } + + public static IEnumerable GetNeighbors(Map map, Cell origin) + { + var ret = new List(); + + 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 } diff --git a/Assets/Scripts/Utils/Map.cs b/Assets/Scripts/Utils/Map.cs index 67323c5..fed9c54 100644 --- a/Assets/Scripts/Utils/Map.cs +++ b/Assets/Scripts/Utils/Map.cs @@ -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 diff --git a/Assets/Scripts/Utils/ObjectPool.cs b/Assets/Scripts/Utils/ObjectPool.cs index b426306..78e1ae4 100644 --- a/Assets/Scripts/Utils/ObjectPool.cs +++ b/Assets/Scripts/Utils/ObjectPool.cs @@ -14,10 +14,6 @@ namespace Assets.Scripts.Utils public ObjectPool(int size) { Size = size; - } - - public void Start() - { Pool = new List(); for (var i = 0; i < Pool.Count; i++) -- libgit2 0.27.0