Skip to content

Commit

Permalink
CellularAutomaton refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
BasmanovDaniil committed Jan 10, 2021
1 parent 75dd208 commit da27b2e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 35 deletions.
48 changes: 15 additions & 33 deletions Runtime/CellularAutomaton/CellularAutomaton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,17 @@ public struct Config
}

public NativeArray2D<bool> cells;
public NativeArray2D<bool> copy;
public Config config;
public int simulationSteps;

private Config config;
private NativeArray2D<bool> copy;

public CellularAutomaton(Config config)
public CellularAutomaton(Config config, int simulationSteps = 1)
{
Assert.IsTrue(config.width > 0);
Assert.IsTrue(config.height > 0);

this.config = config;
this.simulationSteps = simulationSteps;
cells = new NativeArray2D<bool>(config.width, config.height, Allocator.Persistent);
copy = new NativeArray2D<bool>(config.width, config.height, Allocator.Persistent);

Expand All @@ -58,43 +59,24 @@ public void Dispose()
}
}

public void Simulate(int generations)
public void Execute()
{
for (int i = 0; i < generations; i++)
for (int i = 0; i < simulationSteps; i++)
{
Simulate();
}
}

public void Simulate()
{
PTUtils.Swap(ref cells, ref copy);
for (int x = 0; x < config.width; x++)
{
for (int y = 0; y < config.height; y++)
PTUtils.Swap(ref cells, ref copy);
for (int x = 0; x < config.width; x++)
{
int aliveCells = CountAliveNeighbourCells(x, y);

if (!copy[x, y])
for (int y = 0; y < config.height; y++)
{
if (config.ruleset.CanSpawn(aliveCells))
{
cells[x, y] = true;
}
else
{
cells[x, y] = false;
}
}
else
{
if (!config.ruleset.CanSurvive(aliveCells))
int aliveCells = CountAliveNeighbourCells(x, y);

if (copy[x, y])
{
cells[x, y] = false;
cells[x, y] = config.ruleset.CanSurvive(aliveCells);
}
else
{
cells[x, y] = true;
cells[x, y] = config.ruleset.CanSpawn(aliveCells);
}
}
}
Expand Down
16 changes: 14 additions & 2 deletions Samples/CellularAutomata/CellularAutomatonExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ public class CellularAutomatonExample : ConfiguratorBase
private CellularAutomaton automaton;
private Color deadColor;
private Color aliveColor;
private TextControl header;

#region Controls

private TextControl header;
private Dictionary<RulesetName, Ruleset> nameToRuleset = new Dictionary<RulesetName, Ruleset>
{
{RulesetName.Life, Ruleset.life},
Expand All @@ -36,12 +38,16 @@ public class CellularAutomatonExample : ConfiguratorBase
{RulesetName.Majority, Ruleset.majority},
};

#endregion Controls

private void Awake()
{
pixels = new Color[config.width*config.height];
texture = PTUtils.CreateTexture(config.width, config.height, Color.clear);
image.texture = texture;

#region Controls

header = InstantiateControl<TextControl>(leftPanel);
header.transform.SetAsFirstSibling();

Expand Down Expand Up @@ -70,13 +76,15 @@ private void Awake()

InstantiateControl<ButtonControl>(leftPanel).Initialize("Generate", Generate);

#endregion Controls

Generate();
SetupSkyboxAndPalette();
}

private void Update()
{
automaton.Simulate();
automaton.Execute();

for (int x = 0; x < config.width; x++)
{
Expand Down Expand Up @@ -108,6 +116,8 @@ private void Generate()
aliveColor = GetMainColor();
}

#region Controls

private void SelectRuleset(RulesetName rulesetName)
{
config.ruleset = nameToRuleset[rulesetName];
Expand Down Expand Up @@ -141,5 +151,7 @@ private enum RulesetName
Anneal,
Majority,
}

#endregion Controls
}
}

0 comments on commit da27b2e

Please sign in to comment.