Пример #1
0
public class Enemy {
  private int power, speedX, speedY, centerX, centerY;
  private Background bg = GameScreen.getBg1();
  public Rect r = new Rect(0, 0, 0, 0);
  public int health = 5;

  private int movementSpeedX, movementSpeedY;

  public void update() {
    follow();
    centerX += speedX;
    centerY += speedY;

    speedX = bg.getSpeedX() * 5 + movementSpeedX;
    speedY = movementSpeedY;
    r.set(centerX - 25, centerY - 25, 50, 60);
    if (r.intersects(r, Robot.yellowRed)) {
      checkCollision();
    }
  }

  private void follow() {
    if (centerX < -95 || centerX > 810) {
      movementSpeedX = 0;
    } else if (Math.abs(Robot.getCenterX() - centerX) < 5) {
      movementSpeedX = 0;
    } else {
      if (Robot.getCenterX() >= centerX) {
        movementSpeedX = 1;
      } else {
        movementSpeedX = -1;
      }
    }
    if (centerY < 0 || centerY > 480) {
      movementSpeedY = 0;
    } else if (Math.abs(Robot.getCenterY() - centerY) < 5) {
      movementSpeedY = 0;
    } else {
      if (Robot.getCenterY() >= centerY) {
        movementSpeedY = 1;
      } else {
        movementSpeedY = -1;
      }
    }
  }

  private void checkCollision() {
    if (r.intersects(r, Robot.bodyUpper)
        || r.intersects(r, Robot.bodyLower)
        || r.intersects(r, Robot.armStageLeft)
        || r.intersects(r, Robot.armStageRight)) {
      System.out.println("collision");
    }
  }

  public void die() {}

  public void attack() {}

  public int getPower() {
    return power;
  }

  public int getSpeedX() {
    return speedX;
  }

  public int getCenterX() {
    return centerX;
  }

  public int getCenterY() {
    return centerY;
  }

  public Background getBg() {
    return bg;
  }

  public void setPower(int power) {
    this.power = power;
  }

  public void setSpeedX(int speedX) {
    this.speedX = speedX;
  }

  public void setCenterX(int centerX) {
    this.centerX = centerX;
  }

  public void setCenterY(int centerY) {
    this.centerY = centerY;
  }

  public void setBg(Background bg) {
    this.bg = bg;
  }
}
Пример #2
0
public class Robot {

  // Constants are Here
  final int JUMPSPEED = -15;
  final int MOVESPEED = 5;

  private int centerX = 100;
  private int centerY = 377;
  private boolean jumped = false;
  private boolean movingLeft = false;
  private boolean movingRight = false;
  private boolean ducked = false;
  private boolean readyToFire = true;

  private int speedX = 0;
  private int speedY = 0;
  public static Rect rect = new Rect(0, 0, 0, 0);
  public static Rect rect2 = new Rect(0, 0, 0, 0);
  public static Rect rect3 = new Rect(0, 0, 0, 0);
  public static Rect rect4 = new Rect(0, 0, 0, 0);
  public static Rect yellowRed = new Rect(0, 0, 0, 0);

  public static Rect footleft = new Rect(0, 0, 0, 0);
  public static Rect footright = new Rect(0, 0, 0, 0);

  private Background bg1 = GameScreen.getBg1();
  private Background bg2 = GameScreen.getBg2();

  private ArrayList<Projectile> projectiles = new ArrayList<Projectile>();

  public void update() {
    // Moves Character or Scrolls Background accordingly.

    if (speedX < 0) {
      centerX += speedX;
    }
    if (speedX == 0 || speedX < 0) {
      bg1.setSpeedX(0);
      bg2.setSpeedX(0);
    }
    if (centerX <= 200 && speedX > 0) {
      centerX += speedX;
    }
    if (speedX > 0 && centerX > 200) {
      bg1.setSpeedX(-MOVESPEED / 5);
      bg2.setSpeedX(-MOVESPEED / 5);
    }

    // Updates Y Position
    centerY += speedY;

    // Handles Jumping

    speedY += 1;

    if (speedY > 3) {
      jumped = true;
    }

    // Prevents going beyond X coordinate of 0
    if (centerX + speedX <= 60) {
      centerX = 61;
    }

    rect.set(centerX - 34, centerY - 63, centerX + 34, centerY);
    rect2.set(rect.left, rect.top + 63, rect.left + 68, rect.top + 128);
    rect3.set(rect.left - 26, rect.top + 32, rect.left, rect.top + 52);
    rect4.set(rect.left + 68, rect.top + 32, rect.left + 94, rect.top + 52);
    yellowRed.set(centerX - 110, centerY - 110, centerX + 70, centerY + 70);
    footleft.set(centerX - 50, centerY + 20, centerX, centerY + 35);
    footright.set(centerX, centerY + 20, centerX + 50, centerY + 35);
  }

  public void moveRight() {
    if (ducked == false) {
      speedX = MOVESPEED;
    }
  }

  public void moveLeft() {
    if (ducked == false) {
      speedX = -MOVESPEED;
    }
  }

  public void stopRight() {
    setMovingRight(false);
    stop();
  }

  public void stopLeft() {
    setMovingLeft(false);
    stop();
  }

  private void stop() {
    if (isMovingRight() == false && isMovingLeft() == false) {
      speedX = 0;
    }

    if (isMovingRight() == false && isMovingLeft() == true) {
      moveLeft();
    }

    if (isMovingRight() == true && isMovingLeft() == false) {
      moveRight();
    }
  }

  public void jump() {
    if (jumped == false) {
      speedY = JUMPSPEED;
      jumped = true;
    }
  }

  public void shoot() {
    if (readyToFire) {
      Projectile p = new Projectile(centerX + 50, centerY - 25);
      projectiles.add(p);
    }
  }

  public int getCenterX() {
    return centerX;
  }

  public int getCenterY() {
    return centerY;
  }

  public boolean isJumped() {
    return jumped;
  }

  public int getSpeedX() {
    return speedX;
  }

  public int getSpeedY() {
    return speedY;
  }

  public void setCenterX(int centerX) {
    this.centerX = centerX;
  }

  public void setCenterY(int centerY) {
    this.centerY = centerY;
  }

  public void setJumped(boolean jumped) {
    this.jumped = jumped;
  }

  public void setSpeedX(int speedX) {
    this.speedX = speedX;
  }

  public void setSpeedY(int speedY) {
    this.speedY = speedY;
  }

  public boolean isDucked() {
    return ducked;
  }

  public void setDucked(boolean ducked) {
    this.ducked = ducked;
  }

  public boolean isMovingRight() {
    return movingRight;
  }

  public void setMovingRight(boolean movingRight) {
    this.movingRight = movingRight;
  }

  public boolean isMovingLeft() {
    return movingLeft;
  }

  public void setMovingLeft(boolean movingLeft) {
    this.movingLeft = movingLeft;
  }

  public ArrayList getProjectiles() {
    return projectiles;
  }

  public boolean isReadyToFire() {
    return readyToFire;
  }

  public void setReadyToFire(boolean readyToFire) {
    this.readyToFire = readyToFire;
  }
}