Creating Custom GameScenes

The GameScene is a conceptual container for related game elements. There are various ways to implement the use of scenes, with a common pattern being the use of individual scenes for the title screen, main game screen, and main menu screen.

The Chopper-Game Example uses a single scene to manage main game, with a "Level Manager" game object responsible for the actual game-play management (that is, what current level is, how many enemies there are, etc.). That scene object looks like:

Imports GdiGaming

Public Class ExampleScene
    Inherits GameScene

    Private _LevelManager As LevelManager
    Public ReadOnly Property LevelManager As LevelManager
        Get
            Return _LevelManager
        End Get
    End Property

    Public Overrides ReadOnly Property Name As String
        Get
            Return "Example Scene 1"
        End Get
    End Property

    Protected _Enemies As New List(Of EnemySpaceship)
    Public ReadOnly Property Enemies As List(Of EnemySpaceship)
        Get
            Return _Enemies
        End Get
    End Property

    Protected _Player As PlayerChopper
    Public ReadOnly Property Player As PlayerChopper
        Get
            Return _Player
        End Get
    End Property

    Protected Overrides Sub OnLoad(ByVal e As GdiGaming.GameEngineEventArgs)
        Backgrounds.Add(New CloudyBackground)

        _Player = New PlayerChopper
        _Player.Position = New Vector2(320, 400)
        Objects.Add(_Player)

        Dim mainHud As New HudLayer
        HudLayers.Add(mainHud)

        Dim lifeLabel As New PlayerLifeLabel(_Player)
        lifeLabel.Position -= New Vector2(0, e.Engine.Canvas.Height / 2 * 0.9)
        mainHud.Labels.Add(lifeLabel)

        _LevelManager = New LevelManager(Me)
        Objects.Add(_LevelManager)

        MyBase.OnLoad(e)
    End Sub
End Class

Notice that the primary work of the scene is to setup all of the related game elements in the OnLoad method. This is where you create instances of your custom GameObjects and add them to their respective containers.

It is often easiest to begin with the main game screen as a single scene and then add in additional scenes as needed. However, a large multi-level game may want to start with defining individual scenes for each level, especially if the assets used are going to vary a lot (for example, moving from a forest level to a fire level to an ice level).

The GameEngine executes the current scene, so changing the CurrentSceneName property on the GameEngine causes a new scene to load. In this way it is quite easy to change scenes by having your game objects update the CurrentSceneName; so a HUD Label OnMouseClick might set the Engine.CurrentSceneName = "Menu" or colliding with a game object might change the CurrentSceneName to "Level2", allowing the player to "warp" to a new level.

GameScene Containers

The GameScene has collections for Objects, Backgrounds, and HudLayers. Each collection, or "container", is processed on its own. This means that all Backgrounds are updated, followed by Objects, followed by HudLayers and when drawn each container is individually sorted by z-order. So a background will always be behind an object, which will always be behind a HUD element.

Last edited Dec 30, 2011 at 1:16 AM by ReedKimble, version 4

Comments

Shadowblitz16 Aug 16, 2016 at 11:07 PM 
your LevelManager isn't reconized
"Type Level Manager is not Defined"