Snake game in Unity 5.
-
The GameBootstrap instantiates
GameContext
. -
The GameContext then binds various signals to various commands. Namely:
BindDataSignal
toBindDataCommand
BindInputHandlerSignal
toBindInputHandlerCommand
SetupMainMenuSignal
toSetupMainMenuCommand
StartGameSignal
toStartGameCommand
GameOverSignal
toGameOverCommand
-
Upon launch, the
GameContext
dispatches theBindDataSignal
and theShowSignal
to show the main menu. -
The BindDataCommand is invoked which then binds the interfaces of
Grid
,Pellet
,Snake
andGame
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. -
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. -
When the
GameMediator
invokes the method to show the main menu, it also dispatches theSetupMainMenuSignal
. -
The SetupMainMenuCommand is invoked by the
SetupMainMenuSignal
, whose sole purpose is to set up the high score if its available, by dispatching theSetHighScoreSignal
. -
The MainMenuMediator which is active by now listens for the
SetHighScoreSignal
, and if it has been dispatched by theSetupMainMenuCommand
, shows the high score text label in the main menu, else it keeps it in the deactive state. -
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.
-
Whichever option is picked from the above list, the
BindInputHandlerSignal
is dispatched with the proper type, and the BindInputHandlerCommand is invoked. This properly binds theIInputHandler
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. -
If the user chooses one of the two AI options, the BoundedPathfinder or the UnboundedPathfinder are bound to the
IPathfinder
which theAIHandler
uses to find the path for theSnake
. Both these pathfinders inherit from the AbstractPathfinder which implements an A* algorithm to find the optimum path. -
Now that all values are set, the
StartGameSignal
is dispatched, and the gameplay starts. Scores are updated with the dispatch of theUpdateScoreSignal
. The Game class holds the game loop, while the implementations ofIInputHandler
handle the input in the game appropriately. -
When the snake collides with itself, the
GameOverSignal
is dispatched which invokes the GameOverCommand which resets the game and shows the after game screen.
-
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.
- 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
- 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
- InputHandler
- Service
- HighScoreService
- LocalDataConfigService
- View
- MainMenuView
- MainMenuMediator
- InGameView
- InGameMediator
- AfterGameView
- AfterGameMediator
- GameView
- GameMediator
- GameBootstrap
- Abstract