Android Open Source - Stickman Friendly






From Project

Back to project page Stickman.

License

The source code is released under:

Apache License

If you think the Android project Stickman 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.wireframe.stickman;
// w  w w.j a  v a  2 s . c o m
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;

public class Friendly extends AICharacter{
  private Player player = null;

  public Friendly(float x, float y, float z, float x2, float y2) {
    this(new Vector3(x,y,z), new Vector2(x2,y2));
  }

  public Friendly(Vector3 position, Vector2 size) {
    super("friendly", position, size);
  }

  public void update(){
    
    // Move Character
    super.update();
    
    // Make sure we are alive
    if( !isAlive() ) return;
    
    // Only follow player if we don't have a target
    if( target != null ){
      return;
    }
    
    
    // Move towards player?
    if( player != null ){
      if( canSee(player) ){
        if( !playerIsNear(player) ) {
          followPlayer();
          return;
        }
        else if( moving() ) {
          stopMoving();
          return;
        }
      }
      else{
        player = null;
      }
    }
    else{
      // Where is the player?
      lookForPlayer();
      return;
    }
  }  

  private void lookForPlayer() {
    Player worldPlayer = World.getPlayer();
    
    // Check if the player is on the same Y
    if( (int)worldPlayer.getPosition().y != (int)getPosition().y ){
      return;
    }
    
    // Check if he's within 200 pixels on X
    if((int) worldPlayer.getPosition().x < (int)getPosition().x-200 &&
        (int)worldPlayer.getPosition().x > (int)getPosition().x+200){
      return;
    }
    
    
    // Can see the player
    player = worldPlayer;
  }

  private void followPlayer(){
    if( playerIsToTheLeft(player) ){
      if( movingRight() ) stopMoveingRight();
      if( !movingLeft() ) moveLeft();
    }
    else if( playerIsToTheRight(player) ){
      if( !movingRight() ) moveRight();
      if( movingLeft() ) stopMoveingLeft();
    }
  }

  private boolean playerIsNear(Player player){
    return getPosition().x + getWidth() >= player.getPosition().x &&
        getPosition().x <= player.getPosition().x + player.getWidth();
  }
  
  private boolean playerIsToTheRight(Player player){
    return player.getPosition().x > this.getPosition().x;
  }
  
  private boolean playerIsToTheLeft(Player player){
    return player.getPosition().x < this.getPosition().x;
  }
}




Java Source Code List

com.wireframe.stickman.AICharacter.java
com.wireframe.stickman.AndroidGUI.java
com.wireframe.stickman.Brick.java
com.wireframe.stickman.Character.java
com.wireframe.stickman.Door.java
com.wireframe.stickman.Enemy.java
com.wireframe.stickman.FloatingText.java
com.wireframe.stickman.Friendly.java
com.wireframe.stickman.GUI.java
com.wireframe.stickman.GameObject.java
com.wireframe.stickman.GameRules.java
com.wireframe.stickman.Health.java
com.wireframe.stickman.Interactable.java
com.wireframe.stickman.Ladder.java
com.wireframe.stickman.Player.java
com.wireframe.stickman.Resources.java
com.wireframe.stickman.SmallButton.java
com.wireframe.stickman.Spawner.java
com.wireframe.stickman.Spikes.java
com.wireframe.stickman.StickmanGame.java
com.wireframe.stickman.StickmanResources.java
com.wireframe.stickman.Tile.java
com.wireframe.stickman.Toggleable.java
com.wireframe.stickman.WaterShore.java
com.wireframe.stickman.Water.java
com.wireframe.stickman.World.java
com.wireframe.stickman.android.AndroidLauncher.java
com.wireframe.stickman.desktop.DesktopLauncher.java
com.wireframe.stickman.desktop.EditorLauncher.java
com.wireframe.stickman.editor.MapMaker.java
com.wireframe.stickman.editor.MappingPanel.java
com.wireframe.stickman.editor.PlacedTile.java
com.wireframe.stickman.editor.SelectionPanel.java
com.wireframe.stickman.editor.SelectionTile.java
com.wireframe.stickman.editor.SizingPanel.java