/// <summary> /// Block step /// </summary> /// <param name="block">Block</param> /// <returns>"true" if block has landed, otherwise "false"</returns> internal EBlockStepState BlockStep(out EBlock block) { EBlockStepState ret = ((lastBlockStepState == EBlockStepState.Loose) ? EBlockStepState.Loose : EBlockStepState.Nothing); block = user.Field.SelectedBlock; if ((!IsPaused) && (lastBlockStepState != EBlockStepState.Loose)) { DateTime now = DateTime.Now; double milliseconds = (now - lastBlockStepTime).TotalMilliseconds + remainingTime; double step_time = ((Level <= 100) ? (1005 - (Level * 10)) : 5); if (milliseconds >= step_time) { switch (lastBlockStepState) { case EBlockStepState.Nothing: case EBlockStepState.Move: if (MoveBlockDown()) { ret = EBlockStepState.Move; } else { ret = EBlockStepState.Land; } break; case EBlockStepState.SelectNew: if (MoveBlock(0, 0, true)) { ret = EBlockStepState.Move; } else { ret = EBlockStepState.Loose; } break; case EBlockStepState.Land: lastBlockStepState = EBlockStepState.SelectNew; ret = lastBlockStepState; break; } lastBlockStepTime = now; remainingTime = milliseconds - step_time; } } return(ret); }
/// <summary> /// Constructor /// </summary> /// <param name="user">User</param> /// <param name="gameOptions">Game options</param> internal GameManager(User user, GameOptions gameOptions) { this.user = user; GameOptions = gameOptions; Level = gameOptions.StartingLevel; lastBlockStepTime = DateTime.Now; gameManagerThread = new Thread((that) => { if (that is GameManager) { GameManager game_manager = (GameManager)that; while (game_manager.keepRunning) { EBlock block; EBlockStepState step_state = BlockStep(out block); switch (step_state) { case EBlockStepState.Nothing: break; case EBlockStepState.Move: OnBlockMoved?.Invoke(block); break; case EBlockStepState.Land: OnBlockLanded?.Invoke(block); break; case EBlockStepState.SelectNew: OnNewBlockSelected?.Invoke(block); break; case EBlockStepState.Loose: OnGameLost?.Invoke(block); break; } Thread.Sleep(2); } } }); gameManagerThread.Start(this); }