public StatefulReplayState(IStateOwner pOwner, GameplayGameState Source) { var Field = Source.PlayField; Rows = Field.RowCount; Columns = Field.ColCount; ElapsedGameTime = pOwner.GetElapsedTime(); BoardState = new StatefulReplayStateBlockInformation[Field.RowCount][]; for (int y = 0; y < Field.RowCount; y++) { BoardState[y] = new StatefulReplayStateBlockInformation[Field.ColCount]; for (int x = 0; x < BoardState[y].Length; x++) { BoardState[y][x] = new StatefulReplayStateBlockInformation(StatefulReplayStateBlockInformation.BlockInformation.Block_Empty); } } //step one: set the Active Block positions. foreach (var activegroup in Source.PlayField.BlockGroups) { int XGroup = activegroup.X; int YGroup = activegroup.Y; foreach (var iterateblock in activegroup) { int BlockX = iterateblock.X + XGroup; int BlockY = iterateblock.Y + YGroup; BoardState[BlockY][BlockX] = new StatefulReplayStateBlockInformation(StatefulReplayStateBlockInformation.BlockInformation.Block_Active); } } //step two: occupied block positions. for (int CurrentRow = 0; CurrentRow < Source.PlayField.Contents.Length; CurrentRow++) { for (int CurrentCol = 0; CurrentCol < Source.PlayField.Contents[CurrentRow].Length; CurrentCol++) { var thisBlock = Source.PlayField.Contents[CurrentRow][CurrentCol]; if (thisBlock != null) { BoardState[CurrentRow][CurrentCol].State = StatefulReplayStateBlockInformation.BlockInformation.Block_Occupied; } else { BoardState[CurrentRow][CurrentCol].State = StatefulReplayStateBlockInformation.BlockInformation.Block_Empty; } } } }
//writes the data in this Replay State to a Stream. public void WriteToStream(Stream Target) { if (!Target.CanWrite) { throw new InvalidOperationException("Cannot write to unwritable stream"); } BinaryWriter bw = new BinaryWriter(Target); bw.Write(MagicNumber); bw.Write(ElapsedGameTime.Ticks); bw.Write(Rows); bw.Write(Columns); for (int y = 0; y < Rows; y++) { for (int x = 0; x < Columns; x++) { StatefulReplayStateBlockInformation bi = BoardState[y][x]; bw.Write((int)bi.State); } } }