public WorldLogic(World world, RectangularSpriteGrid grid, Vector2i passerbyPositionInIndices) { this.world = world; this.grid = grid; cells = grid.Cells as RectangularSpriteCell[, ]; SpawnPasserby(cells[passerbyPositionInIndices.Y, passerbyPositionInIndices.X]); SpawnHome(cells[grid.CellCount.Y / 2, grid.CellCount.X / 2]); logicState = WorldLogicState.Check; }
public WorldLogic(World world, WorldGrid grid, Vector2i startInIndices, Vector2i finishInIndices, PlayInterface playInterface) { this.world = world; this.grid = grid; this.playInterface = playInterface; cells = grid.Cells as Cell[, ]; this.startInIndices = startInIndices; this.finishInIndices = finishInIndices; graph = new Graph(); for (var y = 0; y < grid.CellCount.Y; y++) { for (var x = 0; x < grid.CellCount.X; x++) { var neighbors = GetNeighbors(new Vector2i(x, y)); if (neighbors != null) { switch (neighbors.Count) { case 1: graph.AddVertex(cells[y, x], new Dictionary <Cell, int>() { { neighbors[0], neighbors[0].Weight } }); break; case 2: graph.AddVertex(cells[y, x], new Dictionary <Cell, int>() { { neighbors[0], neighbors[0].Weight }, { neighbors[1], neighbors[1].Weight } }); break; case 3: graph.AddVertex(cells[y, x], new Dictionary <Cell, int>() { { neighbors[0], neighbors[0].Weight }, { neighbors[1], neighbors[1].Weight }, { neighbors[2], neighbors[2].Weight } }); break; case 4: graph.AddVertex(cells[y, x], new Dictionary <Cell, int>() { { neighbors[0], neighbors[0].Weight }, { neighbors[1], neighbors[1].Weight }, { neighbors[2], neighbors[2].Weight }, { neighbors[3], neighbors[3].Weight } }); break; } } } } path = new List <Cell>(); SpawnStart(cells[startInIndices.Y, startInIndices.X]); SpawnMan(cells[startInIndices.Y, startInIndices.X]); SpawnFinish(cells[finishInIndices.Y, finishInIndices.X]); logicState = WorldLogicState.FindingPath; }
public WorldLogic(AbstractWorld world, AbstractRectangularClickableGrid grid) { this.world = world; this.grid = grid; cells = grid.Cells as Cell[, ]; selectedCells = new Cell[2]; logicState = WorldLogicState.FindAndDeleteMatches; for (var y = 0; y < grid.CellCount.Y; y++) { for (int x = 0; x < grid.CellCount.X; x++) { SpawnDonut(cells[y, x]); cells[y, x].Clicked += UpdateSelectedCellsList; } } }
public override void Update(float dt) { switch (logicState) { case WorldLogicState.Check: if (world.Passerby.Position == world.Home.Position) { // save yes return; } else { if (world.StepsCount == 0) { // save no return; } else { logicState = WorldLogicState.SelectingDirection; world.StepsCount--; return; } } case WorldLogicState.SelectingDirection: var randomValue = MathF.RandomInt(0, 3); MoveDirection direction = (MoveDirection)randomValue; Vector2i directionVector = Vector2i.Down; switch (randomValue) { case 0: directionVector = Vector2i.Down; break; case 1: directionVector = Vector2i.Up; break; case 2: directionVector = Vector2i.Left; break; case 3: directionVector = Vector2i.Right; break; } for (var y = 0; y < grid.CellCount.Y; y++) { for (var x = 0; x < grid.CellCount.X; x++) { if (cells[y, x].Position == world.Passerby.Position) { PasserbyMoveToCell(cells[y + directionVector.Y, x + directionVector.X], direction); logicState = WorldLogicState.Moving; return; } } } return; case WorldLogicState.Moving: if (!IsPasserbyMoving()) { logicState = WorldLogicState.Check; return; } return; } }
public override void Update(float dt) { switch (logicState) { case WorldLogicState.MovingOnPath: if (!world.Man.IsMoving) { currentCellOnMoving++; if (currentCellOnMoving < path.Count) { for (var y = 0; y < grid.CellCount.Y; y++) { for (var x = 0; x < grid.CellCount.X; x++) { if (cells[y, x] == path[currentCellOnMoving - 1]) { MoveDirection direction = MoveDirection.Down; if (y != grid.CellCount.Y && cells[y + 1, x] == path[currentCellOnMoving]) { direction = MoveDirection.Down; } else if (y != 0 && cells[y - 1, x] == path[currentCellOnMoving]) { direction = MoveDirection.Up; } else if (x != 0 && cells[y, x - 1] == path[currentCellOnMoving]) { direction = MoveDirection.Left; } else if (x != grid.CellCount.X - 1 && cells[y, x + 1] == path[currentCellOnMoving]) { direction = MoveDirection.Right; } ManMoveToCell(path[currentCellOnMoving], direction); return; } } } } else { logicState = WorldLogicState.Done; return; } } return; case WorldLogicState.FindingPath: path = graph.ShortestPath(cells[startInIndices.Y, startInIndices.X], cells[finishInIndices.Y, finishInIndices.X]); path.Add(cells[startInIndices.Y, startInIndices.X]); path.Reverse(); logicState = WorldLogicState.Visualization; return; case WorldLogicState.Visualization: visualisationPauseTimer -= dt; if (visualisationPauseTimer < 0) { if (visualisationAlgorithmActionCell < graph.AlgorithmActions.Count) { graph.AlgorithmActions[visualisationAlgorithmActionCell].Cell.Sprite.Color = graph.AlgorithmActions[visualisationAlgorithmActionCell].Color; visualisationAlgorithmActionCell++; } else { logicState = WorldLogicState.MovingOnPath; } visualisationPauseTimer = ResourceLoader.VisualisationPauseTime; } return; case WorldLogicState.Done: Initializer.GameView.Center = new Vector2f(Initializer.WindowSize.X / 2, Initializer.WindowSize.Y / 2); if (playInterface.DoneButton == null) { playInterface.CreateDoneButton(); } return; } }
public override void Update(float dt) { switch (logicState) { case WorldLogicState.DonutsMoving: if (!AreDonutsMoving()) { logicState = WorldLogicState.FindAndDeleteMatches; return; } return; case WorldLogicState.FindAndDeleteMatches: var matches = GetMatchesList(); if (matches.Count != 0) { DeleteMatches(matches); if ( selectedCells[0] != null && selectedCells[1] != null ) { selectedCells[0].CellState = ClickableCellState.Normal; selectedCells[1].CellState = ClickableCellState.Normal; selectedCells[0] = null; selectedCells[1] = null; } logicState = WorldLogicState.MoveToEmptyAndSpawn; return; } else if ( selectedCells[0] != null && selectedCells[1] != null ) { RebindDonutAndMoveToNewOwner(selectedCells[0], selectedCells[1]); RebindDonutAndMoveToNewOwner(selectedCells[1], selectedCells[0]); selectedCells[0].CellState = ClickableCellState.Normal; selectedCells[1].CellState = ClickableCellState.Normal; selectedCells[0] = null; selectedCells[1] = null; logicState = WorldLogicState.DonutsMoving; for (var y = 0; y < grid.CellCount.Y; y++) { for (var x = 0; x < grid.CellCount.X; x++) { cells[y, x].CellState = ClickableCellState.Disabled; } } return; } else { for (var y = 0; y < grid.CellCount.Y; y++) { for (var x = 0; x < grid.CellCount.X; x++) { cells[y, x].CellState = ClickableCellState.Normal; } } logicState = WorldLogicState.WaitInput; return; } case WorldLogicState.MoveToEmptyAndSpawn: MoveDonutsToEmptyCells(); SpawnAndMoveToEmptyCells(); logicState = WorldLogicState.DonutsMoving; for (var y = 0; y < grid.CellCount.Y; y++) { for (var x = 0; x < grid.CellCount.X; x++) { cells[y, x].CellState = ClickableCellState.Disabled; } } return; case WorldLogicState.WaitInput: if ( selectedCells[0] != null && selectedCells[1] != null ) { RebindDonutAndMoveToNewOwner(selectedCells[0], selectedCells[1]); RebindDonutAndMoveToNewOwner(selectedCells[1], selectedCells[0]); logicState = WorldLogicState.DonutsMoving; for (var y = 0; y < grid.CellCount.Y; y++) { for (var x = 0; x < grid.CellCount.X; x++) { cells[y, x].CellState = ClickableCellState.Disabled; } } return; } return; } }