Exemplo n.º 1
0
        // test whether this input is permitted in this game
        bool IsValidInGame(InputEvent inevent)
        {
            switch (inevent)
            {
            case InputEvent.Init:
            case InputEvent.Tick:
                return(true);

            case InputEvent.Action:
            case InputEvent.Reaction:
                return(GameDef.GetSetting(OptionSetting.action, false)); // CHECK:

            case InputEvent.Up:
            case InputEvent.Left:
            case InputEvent.Down:
            case InputEvent.Right:
                return(GameDef.GetSetting(OptionSetting.arrows, false)); // CHECK:

            case InputEvent.Restart:
                return(GameDef.GetSetting(OptionSetting.restart, true));

            case InputEvent.Undo:
                return(GameDef.GetSetting(OptionSetting.undo, true));

            case InputEvent.Hover:
                return(GameDef.GetSetting(OptionSetting.hover, false));

            case InputEvent.Fire1:
            case InputEvent.Fire2:
            case InputEvent.Fire3:
                return(GameDef.GetSetting(OptionSetting.click, false));
            }
            return(false);
        }
Exemplo n.º 2
0
 internal void DebugLog(string format, params object[] args)
 {
     if (GameDef.GetSetting(OptionSetting.debug, false))
     {
         _out.WriteLine(String.Format(format, args));
     }
     else
     {
         Logger.WriteLine(1, "% " + format, args);
     }
 }
Exemplo n.º 3
0
 // load and initialise some level
 bool LoadLevel(int levelindex)
 {
     if (_levelindex == -1)
     {
         VerboseLog("Start game", levelindex);
         CheckTrigger(SoundTrigger.Startgame);
     }
     VerboseLog("Load level={0}", levelindex);
     if (levelindex < 0)
     {
         return(false);
     }
     _levelindex = levelindex;
     if (_levelindex >= GameDef.LevelIndex.Count)
     {
         _modelstate = ModelState.Finished;
         if (Logger.Level >= 2)
         {
             ShowLevel("loadlevel");
         }
         CheckTrigger(SoundTrigger.Endgame);
     }
     else
     {
         var levelinfo = GameDef.LevelIndex[_levelindex];
         if (levelinfo.Item1 == LevelKind.Message)
         {
             _messages.Add(GameDef.Messages[levelinfo.Item2]); // BUG: could be non-empty?
             _modelstate = ModelState.Message;
             CheckTrigger(SoundTrigger.Showmessage);
         }
         else
         {
             var level = GameDef.Levels[levelinfo.Item2];
             _states = new List <GameState> {
                 GameState.Create(this, level, null)
             };
             _modelstate = ModelState.Level;
             if (Logger.Level >= 2)
             {
                 ShowLevel("loadlevel");
             }
             SetCheckpoint(CurrentLevel);
             if (GameDef.GetSetting(OptionSetting.run_rules_on_level_start, false))
             {
                 AcceptInputs("init");
             }
             // take checkpoint after run rules
             SetCheckpoint(CurrentLevel ?? level);
             CheckTrigger(SoundTrigger.Startlevel);
         }
     }
     return(true);
 }
Exemplo n.º 4
0
 internal void StatsLog(string format, params object[] args)
 {
     if (GameDef.GetSetting(OptionSetting.statistics_logging, false))
     {
         _out.WriteLine(String.Format(format, args));
     }
     else
     {
         Logger.WriteLine(1, "% " + format, args);
     }
 }
Exemplo n.º 5
0
 internal void VerboseLog(string format, params object[] args)
 {
     if (GameDef.GetSetting(OptionSetting.verbose_logging, false))
     {
         _verboselog.Add(String.Format(format, args));
     }
     else
     {
         Logger.WriteLine(1, "% " + format, args);
     }
 }
Exemplo n.º 6
0
        // input event requires application of rules to determine next state
        void SetNextState(InputEvent input, int?intparam)
        {
            if (_modelstate == ModelState.Level && !IsValidInGame(input))
            {
                return;
            }

            // model state determines what to do with input
            switch (_modelstate)
            {
            case ModelState.Againing:
            case ModelState.Level:
                ApplyRules(input, intparam);
                if (_states.Last().IsAgaining)
                {
                    _modelstate = ModelState.Againing;
                    if (GameDef.GetSetting(OptionSetting.pause_on_again, false))
                    {
                        break;
                    }
                    while (_states.Last().IsAgaining)
                    {
                        if (Logger.Level >= 3)
                        {
                            ShowLevel("againing");
                        }
                        ApplyRules(InputEvent.Tick, null);
                    }
                }
                CheckNextState();
                break;

            // message level display ended, now find what's next
            case ModelState.Message:
                CheckNextState();
                break;

            case ModelState.EndLevel:
                LoadLevel(_levelindex + 1); // sets state directly
                break;

            case ModelState.Failed:
                break;

            default:
                throw Error.Assert("bad model state {0}", _modelstate);
            }
        }
Exemplo n.º 7
0
 void CheckNextState()
 {
     if (CurrentLevelKind == LevelKind.Message)
     {
         LoadLevel(_levelindex + 1);
     }
     else if (!_states.Last().IsFinished)
     {
         _modelstate = ModelState.Level;
     }
     else if (GameDef.GetSetting(OptionSetting.pause_at_end_level, false))
     {
         _modelstate = ModelState.EndLevel;
     }
     else
     {
         LoadLevel(_levelindex + 1);
     }
 }
Exemplo n.º 8
0
        // apply the result to decide what to do with the new state
        // sounds and messages not preserved
        void ApplyResult(GameState newstate)
        {
            var currentstate = _states.Last();

            // use the result to decide what to do with the new state
            switch (newstate.Result)
            {
            case StateResult.Cancel:
                VerboseLog("Cancel, keep current state");
                CheckTrigger(SoundTrigger.Cancel);
                if (currentstate.IsAgaining)
                {
                    _states.Remove(currentstate);
                }
                break; // as if nothing happened

            case StateResult.Restart:
                VerboseLog("Restart from checkpoint");
                CheckTrigger(SoundTrigger.Restart);
                Restart(newstate);
                break;

            case StateResult.Undo:
                if (_states.Count > 1)
                {
                    VerboseLog("Undo, discard current state");
                    CheckTrigger(SoundTrigger.Undo);
                    _states.Remove(currentstate);
                }
                else
                {
                    VerboseLog("Undo ignored");
                }
                break;

            case StateResult.Reset:
                VerboseLog("Reset, reload level");
                LoadLevel(_levelindex);
                break;

            case StateResult.Againing:
                if (currentstate.IsAgaining)
                {
                    _states.Remove(currentstate);
                }
                newstate.AgainCount = currentstate.AgainCount + 1;
                if (newstate.AgainCount >= 100)
                {
                    throw Error.Fatal("too many agains");
                }
                VerboseLog("Run rules again on current state");
                CheckTrigger(SoundTrigger.Again);
                Logger.WriteLine(2, "Again count {0}", newstate.AgainCount);
                _states.Add(newstate);
                break;

            case StateResult.Finished:
            case StateResult.Success:
                if (currentstate.IsAgaining || _initialinput)
                {
                    _states.Remove(currentstate);
                }
                // treat this combo as a cancel
                if (_playermovement &&
                    GameDef.GetSetting(OptionSetting.require_player_movement, false) &&
                    newstate.PlayerIndexes.SequenceEqual(currentstate.PlayerIndexes))
                {
                    VerboseLog("Player did not move, discard current state");
                    CheckTrigger(SoundTrigger.Cancel);
                }
                else
                {
                    VerboseLog("Move complete");
                    _states.Add(newstate);
                    if (newstate.Result == StateResult.Finished)
                    {
                        VerboseLog("Level complete");
                        CheckTrigger(SoundTrigger.Win);
                    }
                }
                break;

            default:
                throw Error.Assert("bad result {0}", newstate.Result);
            }
        }