I am trying to design an architecture for a SceneKit iOS game. I've shown below two rough high level object diagrams/graphs of my current preferred ideas.
I am only looking at the high level architecture at the moment, i.e. managing transition between Menu/GameLevel/Config/Paused app states and making the app data-driven, so that it can handle multiple levels, etc. Once I get this working, then I'll address lower level architecture, e.g. for the GameLevel state I'll have a GameLevelModel object to represent an actual game level logic.
Would be great to hear which one looks more promising, any obvious pitfalls or things to avoid?
Version A) MVC based, with Custom Container & Nested VCs
I'm trying to stay close to the MVC paradigm with this version.
The behaviour of the app changes significantly between its different states (Menu/GameLevel/Config/etc) so I'll be using a permanent container view controller that will manage/transition between several nested VCs, in a parent-child relationship as per Apple's "View Controller Programming Guide for iOS". These will be effectively entire MVCs.
Because none of Apple's container controllers (UINavigation-, UISplitScreen-, UITabBar-) are suitable (I need the entire screen) I'll have a custom container VC (RootViewController) that will have one nested child VC at all times. The View of each child VC will be fully covering the container's one at all times. The transition between child VCs will be managed by the RootVC and driven by its StateMachine.
Each child VC (effectively entire MVC) is created each time when its corresponding state becomes current and then deallocated when it is replaced with next state's one and no longer needed. Similar to how UINavigationController deals with it's contained VCs.
My only worry with this architecture is that it seems a bit heavy. SceneKit looks like it's designed to work with a single SCNView and to transition between multiple SCNScenes when we need to change the 3D content. This gives more transition options as well.
--------------------------
| RootViewController |
--------------------------
| | | | |
------------------ | | | | | ------------------
| GameLevelsData |----- | | | -------| SCNView |
------------------ | | | ------------------
------------------ | | |
| PlayerData |-------- | |
------------------ | |
| |
---------------- |
| StateMachine | |
---------------- |
| |
|-(*MenuState) |
|-(LevelState) |
|-(ConfigState) |
|
|
------------------------
| MenuViewController |
------------------------
----------------- | | ------------------
| MenuData |------ -------| MenuSCNView |
----------------- ------------------
(child VCs for other States, Level-/Config-/etc are created as needed)
|
------------------------
| LevelViewController |
------------------------
------------------ | | ------------------
| GameLevelModel |------ -------| LevelSCNView |
------------------ ------------------
Version B) Scene-based (one VC, multiple Scenes)
I'll have only one View Controller (GameViewController) with its View (SCNView) and they will last for the entire app lifecycle. (I will not have container and nested VCs here.)
The change in behaviour and content between app states (Menu/GameLevel/Config/etc) is achieved by the only View transitioning between Scenes and different TouchHandlers (UIResponder subclasses for each state) given First Responder status when relevant.
The (Menu-, GameLevel-, etc) TouchHandler classes will only override touchesBegan/Moved/Ended
and canBecomeFirstResponder
so that I can intercept touch events without bloating the single controller and view. None of my other objects are UIResponders. I've not tested exactly this part yet.
With each app state the required objects for that state will be created, like corresponding Scene, TouchHandler, (GameLevelModel with game level logic when in GameLevelState), etc and deallocated when next state is entered.
My current issue with this one is, that when using SKTransition to transition between multile SCNScenes on the single SCNView, after 5-20 transitions there is memory leakage. I tried resolving by simplifying the scene, removing all actions, nodes before deallocating but nothing helps. The only way to avoid leakage is avoiding SKTransition and directly assigning scenes to the View's scene property scnView.scene = scnScene
. So this will require me to animate my own transitions.
--------------------------
| GameViewController |
--------------------------
| | | |
------------------ | | | | ------------------
| GameLevelsData |----- | | -------| SCNView |
------------------ | | ------------------
------------------ | | |
| PlayerData |-------- | |
------------------ | |
| |
---------------- |
| StateMachine | |
---------------- |
| ------------------
|-(*MenuState)--------- | MenuSCNScene |
|-(LevelState) | | ------------------
|-(ConfigState) | |
| |
-------------------- | |
| MenuTouchHandler |---| |
-------------------- |
----------------- |
| MenuData |---------
-----------------
(When entering, each state creates objects of corresponding
dedicated subclasses for its Model, TouchHandler and Scene)
| | |
-------------------- | | -------------------
| LevelTouchHandler |---| | | LevelSCNScene |
-------------------- | -------------------
------------------ |
| GameLevelModel |---------
------------------
Thanks in advance.