Proper scenekit architecture for multi level/scree

2020-08-04 10:18发布

问题:

I'm new to scenekit, so when I started my first project I noticed it's slightly different from spritekit. Although not ideal, I used to use 1 gameviewcontroller and 1 gamescene in spritekit to code my entire game. (YES, this includes a menu, game/levels, settings, etc...)

I'm now wondering since I'm using SCNScenes (which are just loaded from the viewcontroller), I do not want my 1 view controller to be swamped with code. What is the best way to layout a game? Should i have 1 viewcontroller per SCNScene? (switching to a different view controller for: main menu, game level 1, game level 2, game level 3, game over, settings, etc..)

This may not have been worded perfectly, but i appreciate the help!

Aidan

回答1:

My typical layout for games with multiple levels, maps, interactive game play menus:

MainMenuVC SelectLevelVC, SelectMapVC, etc.

GameVC (Hud, score, in-game menus), DefenseVC (like an overlay, choose weapon)

EndWaveVC, EndGameVC

GameControl() - Timers, array iteration, check wave completed, nextWave, cleanup, etc.

Data - shared instance (arrays, switches) for data shared between VC’s and game objects

BaseObjects such as (defenses, explosions, sounds, movement, targeting)

GameNodes (all node creation and storage, model loading, particle loading, camera, lights)

I landed here though many iterations. There are some tradeoffs, but by separating VC’s from data and scenekit things can stay pretty clean. It also enhances the ability to do threading and timers in only a few places when needed.

The sharedInstance of Data allows game objects to iterate through arrays to find targets and objects, such as a defense trying to find an enemy and vice versa. When game objects need to start talking to each other, this is usually where game design starts going off the tracks. So, I iterate loops via timers in GameControl, sharing Data, and that keeps it pretty clean. Since I’m doing loops in one place, I can avoid trampling other objects while they are updating and also get some clean measurements of timing and control FPS.

Hope that helps.