Android Open Source - game-api-android Vis






From Project

Back to project page game-api-android.

License

The source code is released under:

MIT License

If you think the Android project game-api-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.android.vissenspel;
// ww  w  .  ja  v a 2 s  .c  om
import java.util.ArrayList;
import java.util.List;

import android.gameengine.icadroids.input.MotionSensor;
import android.gameengine.icadroids.input.OnScreenButtons;
import android.gameengine.icadroids.input.TouchInput;
import android.gameengine.icadroids.objects.GameObject;
import android.gameengine.icadroids.objects.MoveableGameObject;
import android.gameengine.icadroids.objects.collisions.ICollision;
import android.gameengine.icadroids.objects.collisions.TileCollision;
import android.graphics.Point;

/**
 * Vis is the player object of the game. It swims through the game, eats
 * strawberries and avoids the monster (although right now, nothing bad happens
 * when the monster gets you, just i.b.)
 * 
 * @author Paul Bergervoet
 */
public class Vis extends MoveableGameObject implements ICollision
{

  /**
   * Reference to the game itself
   */
  private Vissenkom mygame;
  
  /**
   * Total score from strawberries eaten
   */
  private int score;

  /**
   * Constructor of Vis
   */
  public Vis(Vissenkom mygame)
  {
    this.mygame = mygame;
    
    setSprite("fishframes", 4);
    setFriction(0.05);

    setSpeed(8);
    setDirection(90);

    score = 0;
  }

  /**
   * update 'Vis': handle collisions and input from buttons / motion sensor
   * 
   * @see android.gameengine.icadroids.objects.MoveableGameObject#update()
   */
  @Override
  public void update()
  {
    super.update();
    // collisions with objects
    ArrayList<GameObject> gebotst = getCollidedObjects();
    if (gebotst != null)
    {
      for (GameObject g : gebotst)
      {
        if (g instanceof Strawberry)
        {
          score = score + ((Strawberry) g).getPoints();
          // Log.d("hapje!!!", "score is nu " + score);
          mygame.deleteGameObject(g);
        } else if (g instanceof Monster)
        {
          // Log.d("Gepakt", "Ai, wat nu...");
        }
      }
    }
    // Handle input. Both on screen buttons and tilting are supported.
    // Buttons take precedence.
    boolean buttonPressed = false;
    if (OnScreenButtons.dPadUp || OnScreenButtons.dPadDown
        || OnScreenButtons.dPadLeft || OnScreenButtons.dPadRight)
    {
      buttonPressed = true;
    }

    if (OnScreenButtons.dPadUp || (MotionSensor.tiltUp && !buttonPressed))
    {
      setDirectionSpeed(0, 8);
      setFrameNumber(1);
    }
    if (OnScreenButtons.dPadDown
        || (MotionSensor.tiltDown && !buttonPressed))
    {
      setDirectionSpeed(180, 8);
      setFrameNumber(3);
    }
    if (OnScreenButtons.dPadRight
        || (MotionSensor.tiltRight && !buttonPressed))
    {
      setDirectionSpeed(90, 8);
      setFrameNumber(0);
    }
    if (OnScreenButtons.dPadLeft
        || (MotionSensor.tiltLeft && !buttonPressed))
    {
      setDirectionSpeed(270, 8);
      setFrameNumber(2);
    }
    
    // Example of how to use the touch screen: Vis swims towards touch location.
    // To use this, comment out the input from OnScreenButtons and MotionSensor 
    // and switch the use-settings in class Vissenkom
    /*
       // get readings from the TouchInput
       float targetX = TouchInput.xPos;
       float targetY = TouchInput.xPos;
       // When using the viewport, translate screen locations to game world 
       Point p = mygame.translateToGamePosition(targetX, targetY);
       // Move in the direction of the point that has been touched
      setSpeed(8);
       moveTowardsAPoint(p.x, p.y);
    */ 
  }

  /**
   * Handle tile collisions: Vis bounces off tiles of type 1 only.
   * 
   * @see android.gameengine.icadroids.objects.collisions.ICollision#collisionOccurred(java.util.List)
   */
  @Override
  public void collisionOccurred(List<TileCollision> collidedTiles)
  {
    // Do we know for certain that the for-each loop goes through the list
    // front to end?
    // If not, we have to use a different iterator!
    for (TileCollision tc : collidedTiles)
    {
      if (tc.theTile.getTileType() == 1)
      {
        moveUpToTileSide(tc);
        setSpeed(0);
        return; // might be considered ugly by some colleagues...
      }
    }
  }

  /**
   * Get the score
   * 
   * @return current value of score
   */
  public int getScore()
  {
    return score;
  }

}




Java Source Code List

android.gameengine.icadroids.alarms.Alarm.java
android.gameengine.icadroids.alarms.IAlarm.java
android.gameengine.icadroids.dashboard.DashboardImageView.java
android.gameengine.icadroids.dashboard.DashboardTextView.java
android.gameengine.icadroids.engine.GameEngine.java
android.gameengine.icadroids.engine.GameFPSCounter.java
android.gameengine.icadroids.engine.GameThread.java
android.gameengine.icadroids.engine.GameView.java
android.gameengine.icadroids.engine.Viewport.java
android.gameengine.icadroids.forms.GameForm.java
android.gameengine.icadroids.forms.IFormInput.java
android.gameengine.icadroids.forms.ViewCreator.java
android.gameengine.icadroids.forms.ViewRemover.java
android.gameengine.icadroids.input.MotionSensor.java
android.gameengine.icadroids.input.OnScreenButton.java
android.gameengine.icadroids.input.OnScreenButtons.java
android.gameengine.icadroids.input.TouchInput.java
android.gameengine.icadroids.objects.GameObject.java
android.gameengine.icadroids.objects.MoveableGameObject.java
android.gameengine.icadroids.objects.collisions.CollidingObject.java
android.gameengine.icadroids.objects.collisions.ICollision.java
android.gameengine.icadroids.objects.collisions.TileCollision.java
android.gameengine.icadroids.objects.graphics.AnimatedSprite.java
android.gameengine.icadroids.objects.graphics.Sprite.java
android.gameengine.icadroids.persistence.GamePersistence.java
android.gameengine.icadroids.sound.GameSound.java
android.gameengine.icadroids.sound.MusicPlayer.java
android.gameengine.icadroids.tiles.GameTiles.java
android.gameengine.icadroids.tiles.Tile.java
com.android.vissenspel.Monster.java
com.android.vissenspel.StrawberryControler.java
com.android.vissenspel.Strawberry.java
com.android.vissenspel.Vis.java
com.android.vissenspel.Vissenkom.java
game.MyAndroidGame.java
testGames.AndroidCraft_demo.java
testGames.FormTest.java
testGames.Player.java
testGames.TestGameBas.java
testGames.TestGameLex.java
testGames.TestGameRoel.java
testGames.gameEngineTest.DebugEngine.java
testGames.gameEngineTest.GameEngineTestGame.java
testGames.gameEngineTest.TestGameObject.java
testGames.gameEngineTest.debugObject.java
testGames.gameEngineTest.randomObject.java
testGames.testGameObjectBas.java
testGames.testGame.java