Android Open Source - AmazingGame Map Input Handler






From Project

Back to project page AmazingGame.

License

The source code is released under:

GNU General Public License

If you think the Android project AmazingGame 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 game.input;
//from ww w. ja  v  a  2s  .c  o m
import game.world.GameMap;
import game.world.GameRenderer;

import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;

public class MapInputHandler implements InputProcessor
{
  private GameRenderer renderer;
  private GameMap map;
  
  private boolean mouseDown = false;
  private long downStartTime = 0;
  
  private int mouseMoveX;
  private int mouseMoveY;
  
  private OrthographicCamera cam;
  
  public MapInputHandler(GameRenderer renderer, GameMap map)
  {
    this.renderer = renderer;
    this.map = map;
    cam = renderer.getCam();
  }
  
  @Override
  public boolean keyDown(int keycode)
  {
    return false;
  }

  @Override
  public boolean keyUp(int keycode)
  {
    return false;
  }

  @Override
  public boolean keyTyped(char character)
  {
    if(character == 'f')
    {
      renderer.toggleFPS();
    }
    else if(character == ' ')
    {
      map.endTurn();
    }
    return false;
  }

  @Override
  public boolean touchDown(int screenX, int screenY, int pointer, int button)
  {
    mouseDown = true;
    downStartTime = System.nanoTime();
    
    return false;
  }

  @Override
  public boolean touchUp(int screenX, int screenY, int pointer, int button)
  {
    long endTime = System.nanoTime();
    if((endTime-downStartTime) <= (.25*1000*1000*1000) && mouseDown)
    {
      if(button == 0)
      {
        Vector3 v3 = new Vector3(screenX, screenY, 0);
        renderer.getCam().unproject(v3);
        map.selectTile((int)(v3.x+32)/64, (int)(v3.y+32)/64);
        mouseDown = false;
      }
    }
    return true;
  }

  @Override
  public boolean touchDragged(int screenX, int screenY, int pointer)
  {
//    System.out.println("X - > " + (screenX-mouseMoveX) + " Y - > " + (screenY-mouseMoveY));
    
    cam.translate((screenX-mouseMoveX)*-1, (screenY-mouseMoveY)*-1);
    cam.update();
    mouseMoveX= screenX;
    mouseMoveY = screenY;
    return true;
  }

  @Override
  public boolean mouseMoved(int screenX, int screenY)
  {
    mouseMoveX= screenX;
    mouseMoveY = screenY;
    return false;
  }

  @Override
  public boolean scrolled(int amount)
  {    
    if(amount == -1)
      cam.zoom -= (cam.zoom*.1) + .1;
    if(amount == 1)
      cam.zoom += (cam.zoom*.1) + .1;
//    System.out.println(cam.zoom);
    cam.update();
    return false;
  }

}




Java Source Code List

game.IOSLauncher.java
game.MyGdxGame.java
game.android.AndroidLauncher.java
game.client.HtmlLauncher.java
game.desktop.DesktopLauncher.java
game.generator.MapGenerator.java
game.helpers.AssetLoader.java
game.helpers.Constants.java
game.helpers.MapHelper.java
game.helpers.TileClickHelper.java
game.input.MapInputHandler.java
game.objects.BlurpleBuilding.java
game.objects.BurnedMoose.java
game.objects.Grass.java
game.objects.PoopDesert.java
game.objects.River.java
game.objects.TileSprite.java
game.objects.Tree.java
game.objects.Unit.java
game.objects.Wall.java
game.objects.Water.java
game.screens.GameScreen.java
game.world.GameMap.java
game.world.GameRenderer.java
game.world.PlayerManager.java