Skip to content

Basics 2 (Game logic)

Matheus Pimenta edited this page Mar 29, 2014 · 54 revisions

#Tutorial navigation

  • [Previous: Basics 1 (Entry point)](Basics 1 (Entry point))
  • Current: Basics 2 (Game logic)
  • [Next: Basics 3 (Game components)](Basics 3 (Game components))

#GameScene

Through the four abstract methods declared in this class, the engine is able to run the main loop by managing a stack of GameScene objects. To run the engine, it is necessary to pass a concrete implementation of this class in the initial settings (see the previous tutorial). The abstract methods are:

  • GameScene.update() This method is intended to update the current state of a scene, like move physics, handle input, check if the game is over, play sounds, change the background music, etc. The engine will call this method once per frame, for every scene, starting from stack bottom.
  • GameScene.render() This method is intended to render all things that make up the scene, like rendering background, player, monsters, texts using custom fonts, etc. The engine will call this method once per frame, for every scene, starting from stack bottom.
  • GameScene.wakeup(Object... args) This method will be called every time a scene asks to be popped from the stack and there is a scene awaiting to stay again in the top of the stack, so the destroyed scene can pass arguments to the remaining one.
  • GameScene.destroy() This method is called to close whatever is necessary inside a scene. It will be called when a scene leaves the stack because of change or pop operations.

This class also provides a protected member of AssetManager named assets, to load images, sounds, etc., and it will be covered later.

There are two protected boolean flags to tell the engine if it should call two methods:

  • GameScene.frozen If this flag is true, the engine will not call GameScene.update() nor GameScene.render().
  • GameScene.visible If this flag is true, the engine will call GameScene.render() even if GameScene.frozen is true.

#GameObject

This abstract class represents a piece that makes up a scene. It can be extended by classes like Player, Enemy, Item, Background, etc. This class was created to support the class presented in the next section, which is an almost-concrete implementation of GameScene, but this class can also be used in scene classes that directly extend from GameScene. This class represents a piece of the scene that can execute operations very similar to the operations of GameScene described above. It also has four abstract methods:

  • GameObject.update() This method has the same purpose of GameScene.update(), it should update things created inside of the game object.
  • GameObject.render(GameRenderers renderers) This method has the same purpose of GameScene.render(), it should render things created inside of the game object. The purpose of renderers parameter will be covered later.
  • GameObject.wakeup(Object... args) This method has the same purpose of GameScene.wakeup(Object... args), it should wakeup things created inside of the game object.
  • GameObject.destroy() This method has the same purpose of GameScene.destroy(), it should destroy things created inside of the game object.

This class is also a container of other GameObject instances. If a GameObject holds another GameObject, the first is related to the second as its "parent". For this reason, the GameObject class also provides five concrete methods to execute operations all over the object tree. These methods are:

  • GameObject.add(GameObject o) This method adds an object to the private container.
  • GameObject.updateTree() This method updates the object itself and all its subtrees.
  • GameObject.renderTree(GameRenderers renderers) This method renders the object itself and all its subtrees.
  • GameObject.wakeupTree(Object... args) This method wakeups the object itself and all its subtrees.
  • GameObject.destroyTree() This method destroys the object itself and all its subtrees.

The GameObject class also provides a protected boolean flag named destroy. This flag is used inside GameObject tree methods to avoid wasting CPU with dead objects and to remove these dead GameObject subtrees. It is also used in ContainerScene with the same purposes.

This class also has the protected boolean flags GameObject.frozen and GameObject.visible, which work exactly the same way as the flags of GameScene, except for the fact that the parent of the GameObject will use these flags instead of the engine.

#ContainerScene

A ContainerScene is an implementation of a GameScene. This class is just a container for GameObject trees. The only task, necessary and sufficient, when extending this class, is to implement a constructor that creates GameObject and puts them in the private container. The four abstract methods of GameScene are implemented identically to the four tree methods of GameObject. A ContainerScene.add(GameObject o) method is also provided.

#Full working code

package mygame.scene;

import mygame.object.MyPlayer;

import org.unbiquitous.uImpala.engine.core.ContainerScene;

public class MyFirstScene extends ContainerScene {
  public MyFirstScene() {
    add(new MyPlayer(assets));
  }
}
package mygame.object;

import org.unbiquitous.uImpala.engine.asset.AssetManager;
import org.unbiquitous.uImpala.engine.core.GameObject;
import org.unbiquitous.uImpala.engine.core.GameRenderers;

public class MyPlayer extends GameObject {
  public MyPlayer(AssetManager assets) {
    // TODO
  }

  protected void update() {
    if (false) // if user pressed 'esc'
      frozen = !frozen;
  }

  protected void render(GameRenderers renderers) {
    // TODO
  }

  protected void wakeup(Object... args) {
    // TODO
  }

  protected void destroy() {
    // TODO
  }
}

#Tutorial navigation

  • [Previous: Basics 1 (Entry point)](Basics 1 (Entry point))
  • Current: Basics 2 (Game logic)
  • [Next: Basics 3 (Game components)](Basics 3 (Game components))
Clone this wiki locally