Skip to content

AmanAgnihotri/Snake

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

54 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Snake

Snake game in Unity 5.

General Walkthrough

  1. The GameBootstrap instantiates GameContext.

  2. The GameContext then binds various signals to various commands. Namely:

  • BindDataSignal to BindDataCommand
  • BindInputHandlerSignal to BindInputHandlerCommand
  • SetupMainMenuSignal to SetupMainMenuCommand
  • StartGameSignal to StartGameCommand
  • GameOverSignal to GameOverCommand
  1. Upon launch, the GameContext dispatches the BindDataSignal and the ShowSignal to show the main menu.

  2. The BindDataCommand is invoked which then binds the interfaces of Grid, Pellet, Snake and Game to the configured values which it retrieves from the IDataConfigService which in turn is bound to LocalDataConfigService which loads the data config from the locally present DataConfig JSON file, and dispatches it back to the command for it to use.

  3. The ShowSignal is retrieved by the GameMediator which then shows the screen based on the value that the signal was dispatched with, in this case - the main menu.

  4. When the GameMediator invokes the method to show the main menu, it also dispatches the SetupMainMenuSignal.

  5. The SetupMainMenuCommand is invoked by the SetupMainMenuSignal, whose sole purpose is to set up the high score if its available, by dispatching the SetHighScoreSignal.

  6. The MainMenuMediator which is active by now listens for the SetHighScoreSignal, and if it has been dispatched by the SetupMainMenuCommand, shows the high score text label in the main menu, else it keeps it in the deactive state.

  7. From here on, with the main menu showing, the user has three options to choose from:

  • Play
    • This option lets the user play the game.
  • Bounded AI
    • This option lets the AI control the game with a bounded pathfinder.
  • Unbounded AI
    • This option lets the AI control the game with an unbounded pathfinder.
  1. Whichever option is picked from the above list, the BindInputHandlerSignal is dispatched with the proper type, and the BindInputHandlerCommand is invoked. This properly binds the IInputHandler with the right handler, whether it be KeyboardHandler if the user is on PC, or the SwipeHandler if the user is on some mobile device, or the AIHandler if an AI option is picked.

  2. If the user chooses one of the two AI options, the BoundedPathfinder or the UnboundedPathfinder are bound to the IPathfinder which the AIHandler uses to find the path for the Snake. Both these pathfinders inherit from the AbstractPathfinder which implements an A* algorithm to find the optimum path.

  3. Now that all values are set, the StartGameSignal is dispatched, and the gameplay starts. Scores are updated with the dispatch of the UpdateScoreSignal. The Game class holds the game loop, while the implementations of IInputHandler handle the input in the game appropriately.

  4. When the snake collides with itself, the GameOverSignal is dispatched which invokes the GameOverCommand which resets the game and shows the after game screen.

Input

  • Keyboard

    • Use W, A, S, D keys to move the snake around.
    • Pressing the Escape key during the gameplay would bring the game back to the main menu.
  • Swipe

    • On mobile devices, swiping Up, Left, Down or Right would get the snake moving in that direction.
    • Pressin the back button of the device during the gameplay would bring the game back to the main menu.

AI

  • AIHandler is the third way to control the snake. It uses one of the two implemented pathfinders to make the snake move on the grid.
  • The BoundedPathfinder is restricted to the bounds of the grid, and doesn't move beyond the grid to come out from the other side, even if doing so would yield a shorter path for the snake to travel.
  • The UnboundedPathfinder is not restricted to the above contraint and so this pathfinder would utilise the boundless nature of the grid to reach the pellet sooner, if such path is available.
  • Both of these pathfinders implement from the AbstractPathfinder which carries with it the crux of the pathfinder implementation. Its the one that implements the A* algorithm.

Game Scripts Structure

  • Game
    • Abstract
      • AbstractInputHandler
      • AbstractPathfinder
      • UserInputHandler
    • Config
      • GameContext
      • GameSignals
    • Controller
      • BindDataCommand
      • BindInputHandlerCommand
      • StartGameCommand
      • SetupMainMenuCommand
      • GameOverCommand
    • Data Containers
      • DataConfig
      • Position
    • Enum
      • Direction
      • InputHandlerType
      • ShowType
    • Interface
      • IGrid
      • ISnake
      • IPellet
      • IGame
      • IPathfinder
      • IInputHandler
      • IRoutineRunner
      • IHighScoreService
      • IDataConfigService
    • Model
      • InputHandler
        • AIHandler
        • KeyboardHandler
        • SwipeHandler
      • Pathfinder
        • BoundedPathfinder
        • UnboundedPathfinder
      • Cell
      • Grid
      • Snake
      • Pellet
      • Game
      • RoutineRunner
    • Service
      • HighScoreService
      • LocalDataConfigService
    • View
      • MainMenuView
      • MainMenuMediator
      • InGameView
      • InGameMediator
      • AfterGameView
      • AfterGameMediator
      • GameView
      • GameMediator
    • GameBootstrap

About

Snake game in Unity 5.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages