Get Started with the BFG Toolkit

1. Entities

public class Dot extends Entity

To start to develop a game with the BFG Toolkit is necessary to identify each element which is part of the game and make it extend Entity class.

 

public Dot(int x, int y, Bitmap img, GameState game, List<Mask> mask, SpriteMap animations, String soundName, Point soundOffset) {

super(x, y, img, game, mask, animations, soundName, soundOffset, true);

}

 

Each entity requires from one position to one 3D source and its offset relative to a position specified by parameter. (See API for more information)

 

@Override

public void onUpdate() {

super.onUpdate();

if(Input.getEvent().getX() > x){

                x++;

} else if(Input.getEvent().getX() < x){

                x--;

} else if(Input.getEvent().getY() > y){

                y++;

} else if(Input.getEvent().getY() < y){

                y--;

}

}

               

@Override

public void onDraw(Canvas canvas){

super.onDraw(canvas);

}

 

onDraw and onUpdate methods are the most relevant. In these methods you have to write the code needed to draw additional elements on the entity or to update the entity logic.

 

@Override

public void onCollision(Entity e) {

if (e instanceof Dot && this.x !=0){

this.remove(e);

}

}

 

Furthermore we have methods like onCollision, which allow us define what action carry out if our entity collides with another one.

 

@Override

public void onTimer(int timer) {

if(timer == 0)

}

 

Futhermore we can override the onTimer method, that allows define an action to do that an after a determinated set of steps. For that you have to enable a timer with Entity.setTimer(int timer, int steps); and after we make a distinction between different timers in the mentioned method.

 

@Override

public void onSavedInstance(Bundle outState, int i, int j) {

outState.putFloat("dotPosX", SIZE_DP);

outState.putFloat("dotPosY", SIZE_DP);

super.onSavedInstance(outState, i, j);

}

              

@Override

public void onRestoreInstance(Bundle savedInstanceState, int i, int j) {

x = outState.getFloat("dotPosX", 0);

y = outState.getFloat("dotPosY", 0);

super.onRestoreInstance( savedInstanceState , i, j);

}


Finally we can override methods in order to save and load information of entities to save application state just in case Android OS decide to release application resources.

 

2. GameStates

 

And now, we must identify the gamestates i.e. the different states which define our game. A set of valid GameStates could be: Splash Screen, game play, and game over. If it was a platform game each level could be modeled like a GameState, in short, each GameState specify the scope of the entities defined in the last step.

 

public class Gameplay extends GameState {

               

                private Dot myDot;

               

                public Gameplay (View v, TTS textToSpeech, Context c, Game game) {

                               super(v,c,textToSpeech, game);

                               mask = new ArrayList<Mask>();

                               Bitmap myBitmap = BitmapFactory.decodeResource(R.drawable.myDot);

                               SpriteMap animations = new SpriteMap(2, 2, myBitmap, 0);

                               aux = new ArrayList<Integer>();

                               aux.add(0);

                               aux.add(2);

                               myDot = new Dot (0,0,  null, game, mask, animations, R.sound.run ,new Point(0,0));

               

                               this.addEntity(myDot);

                }

                @Override

                public void onInit() {

                               super.onInit();

                }

                @Override

                public void onDraw(Canvas canvas) {

                     // All you want to draw on game scope

                               super.onDraw(canvas);

                }

                @Override

                public void onUpdate() {

                               // updates logic of the game

                               super.onUpdate();

          }

}

 

3. Activity Panel

 

It is necessary define a custom view that we assign to the Android activity. On it we make every drawing operation.

 

public class DotPanel extends DrawablePanel {

    private Game game;

    

    private GestureDetector mGestureDetector;

    

    public DotPanel (Game game, Context context) {

            super(context);

            this.game = game;

            mGestureDetector = new GestureDetector(new MyGestureDetector());

    }

    @Override

    public void onInitalize() {

            game.onInit();

    }

    @Override

    public void onDraw(Canvas canvas) {

            super.onDraw(canvas);

            game.onDraw(canvas);

    }

    @Override

    public void onUpdate() {

            game.onUpdate();

            if (game.isEndGame())

                this.getContext().finish();

    }

    @Override

    public boolean onTouchEvent(MotionEvent event) {

            return mGestureDetector.onTouchEvent(event);

    }

}

 

4. Create the game 

 

Finally we have to instantiate the game indicating what states contains and its relative order in onCreate method in an Android activity.

 

public class DotActivity extends Activity {

                private TTS textToSpeech;

                private Game game;

                

                @Override

                public void onCreate(Bundle savedInstanceState) {

                    super.onCreate(savedInstanceState); 

 

                    textToSpeech = (TTS) getIntent().getParcelableExtra(MainActivity.KEY_TTS);

                    textToSpeech.setContext(this);

 

                    createGame(view);

                }

 

We instantiate the view and indicate the context to TTS engine where it will have to work. After that we can instantiate the game and the view.

 

                private void createGame(DrawablePanel zarodnikView) {

                    ArrayList<Integer> order;

                    ArrayList<GameState> gameStates;

                    DrawablePanel  view = new DotGamePanel(this);

                    setContentView(view);

 

                    order = new ArrayList<Integer>();

                    order.add(0);

 

                    gameStates = new ArrayList<GameState>();

                    gameStates.add(new Gameplay(v, textToSpeech, this, game));

                    game = new Game();

                    game.initialize(gameStates, order);

                }

 

Each integer is the position inside the gamestates vector and the integer’s position indicates the execution order.

 

     Order = [0,2,1]

     GameStates = [Inicio,Fin,Gameplay]

 

In first place it executes Inicio, after Gameplay and finally, Fin.

 

                   @Override

                protected void onDestroy() {

                     super.onDestroy();

                     textToSpeech.stop();

                     Sound3DManager.getSoundManager(this).stopAllSources();

                }

 

If you want to preserve the application state you need to implement the method onSaveInstanceState of Activity:

 

                      @Override

                protected void onSaveInstanceState(Bundle outState) {

                     game.onSaveInstance(outState);

                     super.onSaveInstanceState(outState);

                }


Comments