public World(Vector2Int tableSize, CellTypeBank ctb) { this.OnWorldChange = null; this.CellTypeBank = ctb; this.TableSize = tableSize; CurrentTable = new CellTable(this.TableSize, CellTypeBank.GetDefaultCellType()); NextIterationTable = new CellTable(CurrentTable); Delta = new HashSet <Vector2Int>(); Steps = 0; }
public CellTable(CellTable ct) { Size = ct.Size; DefaultCellType = ct.DefaultCellType; Cells = new ExistingCell[Size.X, Size.Y]; for (int i = 0; i < Size.X; i++) { for (int j = 0; j < Size.Y; j++) { PlaceCell(new Vector2Int(i, j), ct.Cells[i, j].CellType); } } }
public void RestoreSnapshot(CellTable memento) { lock (this) { Delta.Clear(); CurrentTable = memento; NextIterationTable = new CellTable(this.TableSize, memento.DefaultCellType); for (int i = 0; i < TableSize.X; i++) { for (int j = 0; j < TableSize.Y; j++) { Delta.Add(new Vector2Int(i, j)); } } Steps = 0; ApplyChanges(); } }
public event System.EventHandler <DrawWorldArgs> OnWorldChange; //Observer pattern /// <summary> /// Step for a simulation. /// </summary> public void Step() { lock (this) { Stopwatch t = new Stopwatch(); t.Start(); //Step and execution of all rules for (int i = 0; i < TableSize.X; i++) { for (int j = 0; j < TableSize.Y; j++) { CurrentTable.Cells[i, j].ExecuteRules(this); } } //Clears up all naive changes that turned out not to change anything HashSet <Vector2Int> CellTypeChange = new HashSet <Vector2Int>(); foreach (Vector2Int change in Delta) { if (CurrentTable.Cells[change.X, change.Y].CellType != NextIterationTable.Cells[change.X, change.Y].CellType) { CellTypeChange.Add(change); } } Delta = new HashSet <Vector2Int>(CellTypeChange); //Sets up next iteration and sends all changes to the view CurrentTable = NextIterationTable; NextIterationTable = new CellTable(CurrentTable); Steps++; ApplyChanges(); t.Stop(); //TODO: DEBUG MODE //Console.WriteLine("Model has been updated in " + t.Elapsed.TotalSeconds + "s"); } }
//We don't use original world because other components may modify it without our knowledge public DrawWorldArgs(World world) { CellTable = world.CurrentTable; Delta = new HashSet <Vector2Int>(world.Delta); Steps = world.Steps; }