Class GameSceneView

All Implemented Interfaces:
Animation, Editable, StyleListener, Iterable<Component>

public class GameSceneView extends GameView

A turnkey GameView that plays a GameLevel: it wires the whole lifecycle so a generated game scene can be a thin subclass that only adds behavior.

  • 2D levels are realized into the scene immediately.
  • board levels are realized on the first sized frame, once an IsoProjection can be fitted to the view.
  • 3D levels set up the perspective camera and the primary light, and build a Model per element in GameView#onSetup(com.codename1.gpu.GraphicsDevice) (the only place the GPU device exists); override #buildModels(GraphicsDevice) to supply real meshes instead of the default per-element cube.

Game logic goes in #onUpdate(double) (a no-op by default) -- the scene's sprite animations are advanced by the renderer, so do not call Scene#update(double).

  • Constructor Details

  • Method Details

    • getLevel

      public GameLevel getLevel()
    • getCatalog

      public AssetCatalog getCatalog()
    • update

      protected void update(double deltaSeconds)
      Advance the game by the given amount of time. Called once per frame (or repeatedly at a fixed interval when #setFixedTimestep(double) is used). Realizes a board level lazily once the view has a size, then defers to #onUpdate(double).
      Specified by:
      update in class GameView
    • onUpdate

      protected void onUpdate(double deltaSeconds)
      Override to advance game logic each frame. The default does nothing.
    • getProjection

      public IsoProjection getProjection()
      The isometric projection used to place a board level (valid after the first sized frame).
    • elementOf

      protected GameElement elementOf(Sprite sprite)
      The GameElement a sprite was realized from -- its Sprite#getUserData() -- or null if the sprite did not come from a level element. This is the bridge back to the typed lives/value/speed numbers you set in the editor.
    • findByName

      protected Sprite findByName(String name)
      The first sprite in the scene whose source element has the given name (the name typed in the Inspector), or null. The generated scene initializes a field per named element with this.
    • findByAsset

      protected Sprite findByAsset(String assetId)
      The first sprite stamped from the given asset id (e.g. "player", "coin"), or null.
    • findAllByAsset

      protected List<Sprite> findAllByAsset(String assetId)
      Every sprite stamped from the given asset id, in z-order -- handy for a "collect all the coins" loop. Never null (empty when none match).
    • overlaps

      protected boolean overlaps(Sprite a, Sprite b)
      Whether two sprites' bounding boxes overlap -- a null-safe shorthand for Sprite#intersects(Sprite), the usual "did they touch?" collision test.
    • getScore

      public int getScore()
      The running score (starts at zero). Add to it with #addScore(int).
    • setScore

      public void setScore(int score)
    • addScore

      public int addScore(int delta)
      Adds delta to the score and returns the new total.
    • getLives

      public int getLives()
      Remaining lives, or -1 until #setLives(int) is called. The generated scene seeds this from the player's lives property.
    • setLives

      public void setLives(int lives)
    • loseLife

      public int loseLife()
      Decrements the life count (never below zero) and returns what remains.
    • isGameOver

      public boolean isGameOver()
      True once #setLives(int) has been given a count that has since reached zero -- the standard "game over" test (false while lives are uninitialized).
    • setArcadeBehavior

      public GameSceneView setArcadeBehavior(boolean on)
      Enables (or disables) the built-in 2D arcade behavior -- gravity, run/jump, tile collision, enemy patrol and pickups -- that the editor preview shows. Off by default. Has no effect on board or 3D levels.
    • isArcadeBehavior

      public boolean isArcadeBehavior()
    • setFollowCamera

      public GameSceneView setFollowCamera(boolean on)
      Whether the arcade behavior scrolls the scene camera to follow the player (on by default) -- this is what makes a parallax background scroll.
    • updateArcade

      protected void updateArcade(double deltaSeconds)
      Runs one arcade frame: player physics, enemy patrol, pickups, and the follow camera. Called automatically before #onUpdate(double) when #setArcadeBehavior(boolean) is on and the level is 2D.
    • arcadePlayer

      protected Sprite arcadePlayer()
      The player sprite the arcade behavior drives: an element flagged player = true wins, else the first player/hero asset. Cached; also seeds #setLives(int) from the player's lives property on first resolve.
    • updatePlayer

      protected void updatePlayer(Sprite player, double deltaSeconds)
      Default player physics: run with Left/Right, jump on Up/Fire (edge-triggered), gravity from the level's gravity property, jump height from the player's jumpHeight property, stopped by solid tiles, respawning if it falls out. Override to change movement, or #setArcadeBehavior(boolean) off to opt out.
    • updateEnemies

      protected void updateEnemies(double deltaSeconds)
      Default enemy behavior: every #isEnemy(String) sprite patrols horizontally at its speed property, turning at walls and level edges.
    • checkPickups

      protected void checkPickups(Sprite player)
      Default pickups: when the player overlaps a #isCollectible(String) sprite, #onPickup(Sprite) fires (scores and consumes it); when it overlaps an #isEnemy(String) sprite, #onPlayerHit(Sprite) fires (costs a life).
    • onPickup

      protected boolean onPickup(Sprite item)
      Called when the player touches a collectible. The default adds the item's value property to the score and returns true to consume it. Override for power-ups: read the asset id / properties, change state, and return whether to remove the item.
    • onPlayerHit

      protected void onPlayerHit(Sprite enemy)
      Called when the player touches an enemy. The default costs a life and respawns the player at its start. Override for HP, knockback, invulnerability frames, checkpoints or a death screen.
    • isCollectible

      protected boolean isCollectible(String assetId)
      Whether an asset id is a collectible (the default arcade behavior scores and removes it). Defaults to coin/gem/star/token; override to add yours.
    • isEnemy

      protected boolean isEnemy(String assetId)
      Whether an asset id is an enemy (the default arcade behavior patrols it and costs a life on contact). Defaults to slime/enemy*/npc*; override yours.
    • isSolidAt

      protected boolean isSolidAt(double cx, double cy, double hw, double hh)
      Whether a box centered at (cx, cy) with half-size (hw, hh) overlaps any solid tile (a non-empty cell of a visible tile layer). The collision query the default player/enemy physics uses; override for one-way platforms etc.
    • onSetup

      protected void onSetup(GraphicsDevice device)

      Override to allocate GPU resources (meshes, textures, Models) once the GPU device is ready. Invoked once on the render thread before the first frame -- the only place you can call com.codename1.gpu.Primitives / com.codename1.gpu.GltfLoader, which need the device. The default does nothing.

      Parameters
      • device: the GPU device for creating meshes, textures and buffers
      For a 3D level, configures the camera + light and builds the models.
      Overrides:
      onSetup in class GameView
    • buildModels

      protected void buildModels(GraphicsDevice device)
      Builds the 3D models for the level. The default gives every element a unit cube (com.codename1.gpu.Primitives#cube) shaded with its asset's base color and placed/scaled from the element transform. Override to load real meshes (e.g. with com.codename1.gpu.GltfLoader) keyed off the element's asset id.