Godot Nature of Code  1.2.0
Nature of Code implemented in Godot Engine
Public Member Functions | Protected Member Functions | Protected Attributes | Properties | List of all members
Automata.CellularAutomata2D< TCell, T > Class Template Reference

Cellular automata 2D. More...

Inherits Node2D.

Public Member Functions

 CellularAutomata2D ()
 Create a default cellular automata with a cell scale of 20. More...
 
 CellularAutomata2D (int scale)
 Create a default cellular automata with a custom cell scale. More...
 
virtual void RandomizeGrid ()
 Randomize grid. More...
 

Protected Member Functions

virtual Vector2 GetAutomataBounds ()
 Get automata bounds. More...
 
virtual void InitializeGrid ()
 Initialize grid. More...
 
virtual void InitializeCell (TCell cell, int i, int j)
 Initialize cell. More...
 
virtual void ReviveCellAtScreenPos (Vector2 pos)
 Revive cell at screen position. More...
 
virtual int GetAliveNeighborsFromCell (int x, int y)
 Get alive neighbors from cell position. More...
 
virtual T ApplyRules (int x, int y)
 Apply rules on cell and return next state. More...
 
virtual void Generate ()
 Create a new generation. More...
 

Protected Attributes

TCell[] _grid
 Grid nodes More...
 
int _scale
 Cell scale More...
 
int _rows
 Row count More...
 
int _cols
 Cols count More...
 
int _generation
 Current generation More...
 
Node2D _gridContainer
 Grid container More...
 

Properties

float WaitTime [get, set]
 Wait time More...
 
Color CellColor = Colors.LightBlue [get, set]
 Cell color More...
 
bool Paused [get, set]
 Paused More...
 
bool HighlightTransitions [get, set]
 Highlight when a cell is transitioning from life to death More...
 
bool CenterAlignedGrid [get, set]
 Align grid to the center More...
 
bool HideGUI [get, set]
 Hide GUI More...
 
TouchBehaviorEnum TouchBehavior [get, set]
 Touch behavior More...
 
WrapBehaviorEnum WrapBehavior [get, set]
 Wrap behavior More...
 

Detailed Description

Cellular automata 2D.

Type Constraints
TCell :Cell<T> 
TCell :new() 
T :System.IEquatable<T> 

Definition at line 160 of file CellularAutomata2D.cs.

Constructor & Destructor Documentation

◆ CellularAutomata2D() [1/2]

Create a default cellular automata with a cell scale of 20.

Definition at line 235 of file CellularAutomata2D.cs.

235 : this(20) { }

◆ CellularAutomata2D() [2/2]

Automata.CellularAutomata2D< TCell, T >.CellularAutomata2D ( int  scale)
inline

Create a default cellular automata with a custom cell scale.

Parameters
scaleScale

Definition at line 241 of file CellularAutomata2D.cs.

242  {
243  Name = "CellularAutomata2D";
244  _scale = scale;
245  _gridContainer = new Node2D()
246  {
247  Name = "GridContainer"
248  };
249  }
Node2D _gridContainer
Grid container

Member Function Documentation

◆ ApplyRules()

virtual T Automata.CellularAutomata2D< TCell, T >.ApplyRules ( int  x,
int  y 
)
inlineprotectedvirtual

Apply rules on cell and return next state.

Parameters
xX position
yY position
Returns
Next state

Definition at line 464 of file CellularAutomata2D.cs.

465  {
466  var neighbors = GetAliveNeighborsFromCell(x, y);
467  var cell = _grid[x + (y * _cols)];
468  var state = cell.State;
469  var isAlive = cell.IsAlive();
470 
471  if (isAlive && (neighbors < 2 || neighbors > 3))
472  {
473  return cell.GetDeadValue();
474  }
475  else if (!isAlive && neighbors == 3)
476  {
477  return cell.GetAliveValue();
478  }
479  else
480  {
481  return state;
482  }
483  }
virtual int GetAliveNeighborsFromCell(int x, int y)
Get alive neighbors from cell position.

◆ Generate()

virtual void Automata.CellularAutomata2D< TCell, T >.Generate ( )
inlineprotectedvirtual

Create a new generation.

Definition at line 488 of file CellularAutomata2D.cs.

489  {
490  int offset = (WrapBehavior == WrapBehaviorEnum.Wrap) ? 0 : 1;
491 
492  for (int j = offset; j < _rows - offset; ++j)
493  {
494  for (int i = offset; i < _cols - offset; ++i)
495  {
496  _grid[i + (j * _cols)].PreviousState = _grid[i + (j * _cols)].State;
497  }
498  }
499 
500  for (int j = offset; j < _rows - offset; ++j)
501  {
502  for (int i = offset; i < _cols - offset; ++i)
503  {
504  _grid[i + (j * _cols)].State = ApplyRules(i, j);
505  }
506  }
507  }
WrapBehaviorEnum WrapBehavior
Wrap behavior
virtual T ApplyRules(int x, int y)
Apply rules on cell and return next state.
WrapBehaviorEnum
Wrap behavior enum. /summary>

◆ GetAliveNeighborsFromCell()

virtual int Automata.CellularAutomata2D< TCell, T >.GetAliveNeighborsFromCell ( int  x,
int  y 
)
inlineprotectedvirtual

Get alive neighbors from cell position.

Parameters
xX position
yY position
Returns
Alive neighbors count

Definition at line 441 of file CellularAutomata2D.cs.

442  {
443  int count = 0;
444 
445  for (int j = -1; j <= 1; ++j)
446  {
447  for (int i = -1; i <= 1; ++i)
448  {
449  int cellX = (WrapBehavior == WrapBehaviorEnum.Wrap) ? Mathf.PosMod(x + i, _cols) : x + i;
450  int cellY = (WrapBehavior == WrapBehaviorEnum.Wrap) ? Mathf.PosMod(y + j, _rows) : y + j;
451  count += (_grid[cellX + (cellY * _cols)].WasAlive()) ? 1 : 0;
452  }
453  }
454 
455  return count - ((_grid[x + (y * _cols)].WasAlive()) ? 1 : 0);
456  }

◆ GetAutomataBounds()

virtual Vector2 Automata.CellularAutomata2D< TCell, T >.GetAutomataBounds ( )
inlineprotectedvirtual

Get automata bounds.

Returns
Bounds

Definition at line 364 of file CellularAutomata2D.cs.

365  {
366  return GetViewportRect().Size;
367  }

◆ InitializeCell()

virtual void Automata.CellularAutomata2D< TCell, T >.InitializeCell ( TCell  cell,
int  i,
int  j 
)
inlineprotectedvirtual

Initialize cell.

Parameters
cellCell
iX position
jY position

Definition at line 404 of file CellularAutomata2D.cs.

405  {
406  cell.State = cell.PreviousState = cell.GetDeadValue();
407  }

◆ InitializeGrid()

virtual void Automata.CellularAutomata2D< TCell, T >.InitializeGrid ( )
inlineprotectedvirtual

Initialize grid.

Definition at line 372 of file CellularAutomata2D.cs.

373  {
374  var size = GetAutomataBounds();
375  _cols = (int)size.x / _scale;
376  _rows = (int)size.y / _scale;
377  _grid = new TCell[_cols * _rows];
378 
379  for (int j = 0; j < _rows; ++j)
380  {
381  for (int i = 0; i < _cols; ++i)
382  {
383  var currPos = i + (j * _cols);
384  var cell = new TCell()
385  {
386  Position = new Vector2(i * _scale, j * _scale),
387  Size = new Vector2(_scale, _scale),
390  };
391  InitializeCell(cell, i, j);
392  _gridContainer.AddChild(cell);
393  _grid[currPos] = cell;
394  }
395  }
396  }
virtual void InitializeCell(TCell cell, int i, int j)
Initialize cell.
bool HighlightTransitions
Highlight when a cell is transitioning from life to death
virtual Vector2 GetAutomataBounds()
Get automata bounds.

◆ RandomizeGrid()

virtual void Automata.CellularAutomata2D< TCell, T >.RandomizeGrid ( )
inlinevirtual

Randomize grid.

Definition at line 254 of file CellularAutomata2D.cs.

255  {
256  _generation = 0;
257  int offset = (WrapBehavior == WrapBehaviorEnum.Wrap) ? 0 : 1;
258 
259  for (int j = offset; j < _rows - offset; ++j)
260  {
261  for (int i = offset; i < _cols - offset; ++i)
262  {
263  var currPos = i + (j * _cols);
264  var cell = _grid[currPos];
265  cell.State = cell.PreviousState = cell.RandomizeState();
266  }
267  }
268  }
int _generation
Current generation

◆ ReviveCellAtScreenPos()

virtual void Automata.CellularAutomata2D< TCell, T >.ReviveCellAtScreenPos ( Vector2  pos)
inlineprotectedvirtual

Revive cell at screen position.

Parameters
posScreen position

Definition at line 413 of file CellularAutomata2D.cs.

414  {
415  // Split position depending on scale
416  var bounds = GetAutomataBounds();
417  var gridPosition = _gridContainer.Position;
418  var automataRect = new Rect2(gridPosition, bounds);
419  if (!automataRect.HasPoint(pos))
420  {
421  return;
422  }
423 
424  // Get index
425  var idx = (pos - gridPosition) / _scale;
426  int offset = (WrapBehavior == WrapBehaviorEnum.Wrap) ? 0 : 1;
427  int x = Mathf.Min(Mathf.Max(offset, (int)idx.x), _cols - 1 - offset);
428  int y = Mathf.Min(Mathf.Max(offset, (int)idx.y), _rows - 1 - offset);
429 
430  var cell = _grid[x + (y * _cols)];
431  cell.PreviousState = cell.GetDeadValue();
432  cell.State = cell.GetAliveValue();
433  }

Member Data Documentation

◆ _cols

int Automata.CellularAutomata2D< TCell, T >._cols
protected

Cols count

Definition at line 217 of file CellularAutomata2D.cs.

◆ _generation

int Automata.CellularAutomata2D< TCell, T >._generation
protected

Current generation

Definition at line 220 of file CellularAutomata2D.cs.

◆ _grid

TCell [] Automata.CellularAutomata2D< TCell, T >._grid
protected

Grid nodes

Definition at line 208 of file CellularAutomata2D.cs.

◆ _gridContainer

Node2D Automata.CellularAutomata2D< TCell, T >._gridContainer
protected

Grid container

Definition at line 223 of file CellularAutomata2D.cs.

◆ _rows

int Automata.CellularAutomata2D< TCell, T >._rows
protected

Row count

Definition at line 214 of file CellularAutomata2D.cs.

◆ _scale

int Automata.CellularAutomata2D< TCell, T >._scale
protected

Cell scale

Definition at line 211 of file CellularAutomata2D.cs.

Property Documentation

◆ CellColor

Color Automata.CellularAutomata2D< TCell, T >.CellColor = Colors.LightBlue
getset

Cell color

Definition at line 179 of file CellularAutomata2D.cs.

179 { get; set; } = Colors.LightBlue;

◆ CenterAlignedGrid

bool Automata.CellularAutomata2D< TCell, T >.CenterAlignedGrid
getset

Align grid to the center

Definition at line 188 of file CellularAutomata2D.cs.

188 { get; set; }

◆ HideGUI

bool Automata.CellularAutomata2D< TCell, T >.HideGUI
getset

Hide GUI

Definition at line 191 of file CellularAutomata2D.cs.

192  {
193  get => _hideGui;
194  set
195  {
196  _hideGui = value;
197  SetGUIVisibility(!value);
198  }
199  }

◆ HighlightTransitions

bool Automata.CellularAutomata2D< TCell, T >.HighlightTransitions
getset

Highlight when a cell is transitioning from life to death

Definition at line 185 of file CellularAutomata2D.cs.

185 { set; get; }

◆ Paused

bool Automata.CellularAutomata2D< TCell, T >.Paused
getset

Paused

Definition at line 182 of file CellularAutomata2D.cs.

182 { get; set; }

◆ TouchBehavior

TouchBehaviorEnum Automata.CellularAutomata2D< TCell, T >.TouchBehavior
getset

Touch behavior

Definition at line 202 of file CellularAutomata2D.cs.

202 { get; set; }

◆ WaitTime

float Automata.CellularAutomata2D< TCell, T >.WaitTime
getset

Wait time

Definition at line 165 of file CellularAutomata2D.cs.

166  {
167  get => _waitTime;
168  set
169  {
170  _waitTime = value;
171  if (_timer != null)
172  {
173  _timer.WaitTime = value;
174  }
175  }
176  }

◆ WrapBehavior

WrapBehaviorEnum Automata.CellularAutomata2D< TCell, T >.WrapBehavior
getset

Wrap behavior

Definition at line 205 of file CellularAutomata2D.cs.

205 { get; set; }

The documentation for this class was generated from the following file: