コード例 #1
0
ファイル: Tile.java プロジェクト: Jcaron429/OneLifeOnline
public abstract class Tile {

  public static final Tile[] tiles = new Tile[256];
  public static final Tile VOID =
      new BasicSolidTile(0, 0, 0, Colors.get(000, -1, -1, -1), 0xFF000000);
  public static final Tile STONE =
      new BasicSolidTile(1, 1, 0, Colors.get(-1, 333, -1, -1), 0xFF555555);
  public static final Tile GRASS = new BasicTile(2, 2, 0, Colors.get(-1, 131, 141, -1), 0xFF00FF00);
  public static final Tile TREE =
      new BasicSolidTile(3, 1, 31, Colors.get(131, 020, 050, 321), 0xFF005525);
  public static final Tile WATER =
      new AnimatedTile(
          4,
          new int[][] {{0, 5}, {1, 5}, {2, 5}, {1, 5}},
          Colors.get(-1, 004, 115, -1),
          0xFF0000FF,
          1000);

  protected byte id;
  protected boolean solid;
  protected boolean emitter;
  private int levelColor;

  public Tile(int id, boolean isSolid, boolean isEmitter, int levelColor) {
    this.id = (byte) id;
    if (tiles[id] != null) throw new RuntimeException("Duplicate tile id on" + id);
    this.solid = isSolid;
    this.emitter = isEmitter;
    this.levelColor = levelColor;
    tiles[id] = this;
  }

  public byte getId() {
    return id;
  }

  public boolean isSolid() {
    return solid;
  }

  public boolean isEmitter() {
    return emitter;
  }

  public int getLevelColor() {
    return levelColor;
  }

  public abstract void tick();

  public abstract void render(Screen screen, Level level, int x, int y);
}
コード例 #2
0
public class Player extends Mob {

  // input handler for moving player
  private InputHandler input;
  // colors the player (look in spritesheet)
  private int color = Colors.get(-1, 111, 300, 543);
  // health variable
  public static int health = 13;
  // for rendering hearts (look in font class)
  private String life = ("###");
  // to center hearts
  private int adjustment = 5;

  ////////////////////////////////////////////////////////////
  // Constructor assigns player to level, location for spawn,
  // and gives the player an input handler
  ///////////////////////////////////////////////////////////
  public Player(Level level, int x, int y, InputHandler input) {
    // creates the player
    super(level, "Player", x, y, 1);
    this.input = input;
  }

  // tick updates player and moves
  public void tick() {
    int xa = 0;
    int ya = 0;

    // the following should move the entire screen...
    if (input.up.isPressed()) {
      ya--;
    }
    if (input.down.isPressed()) {
      ya++;
    }
    if (input.left.isPressed()) {
      xa--;
    }
    if (input.right.isPressed()) {
      xa++;
    }

    // stop moving
    if (xa != 0 || ya != 0) {
      move(xa, ya);
      isMoving = true;
    } else {
      isMoving = false;
    }
    this.scale = 1;
  }

  // renders player with spritesheet info
  public void render(Screen screen) {
    // location of player on sprite sheet (first tile)
    // player is made out of 4 tiles
    int xTile = 0;
    int yTile = 28;

    //////////////////////////////////////////////////////////////////
    // Entire section flips player sprite image based on direction
    int walkingSpeed = 3;
    int flipTop = (numSteps >> walkingSpeed) & 1;
    int flipBottom = (numSteps >> walkingSpeed) & 1;

    if (movingDir == 1) {
      xTile += 2;
    } else if (movingDir > 1) {
      xTile += 4 + ((numSteps >> walkingSpeed) & 1) * 2;
      flipTop = (movingDir - 1) % 2;
    }
    ///////////////////////////////////////////////////////////////////

    // modifier variable
    int modifier = 8 * scale;
    int xOffset = x - modifier / 2;
    int yOffset = y - modifier / 2 - 4;

    /* multiply by 32 to get actual tile in sprite sheet*/
    // upper body of player
    screen.render(
        xOffset + (modifier * flipTop), yOffset, xTile + yTile * 32, color, flipTop, scale);
    screen.render(
        xOffset + modifier - (modifier * flipTop),
        yOffset,
        (xTile + 1) + yTile * 32,
        color,
        flipTop,
        scale);

    // lower body of player
    screen.render(
        xOffset + (modifier * flipBottom),
        yOffset + modifier,
        xTile + (yTile + 1) * 32,
        color,
        flipBottom,
        scale);
    screen.render(
        xOffset + modifier - (modifier * flipBottom),
        yOffset + modifier,
        (xTile + 1) + (yTile + 1) * 32,
        color,
        flipBottom,
        scale);

    // changes hearts above player based on life total
    if (health == 13) {
      life = "###";
      adjustment = 5;
    } else if (health == 9) {
      life = "##";
      adjustment = 0;
    } else if (health == 5) {
      life = "#";
      adjustment = -3;
    }

    // renders life (in the form of hearts) above player
    Font.render(life, screen, xOffset - adjustment, yOffset - 10, Colors.get(-1, -1, -1, 257), 1);
  }

  // For Solid Detection
  public boolean hasCollided(int xa, int ya) {
    int xMin = 0;
    int xMax = 7;
    int yMin = 3;
    int yMax = 7;

    for (int x = xMin; x < xMax; x++) {
      if (isSolidTile(xa, ya, x, yMin)) return true;
    }
    for (int x = xMin; x < xMax; x++) {
      if (isSolidTile(xa, ya, x, yMax)) return true;
    }
    for (int y = yMin; y < yMax; y++) {
      if (isSolidTile(xa, ya, xMin, y)) return true;
    }
    for (int y = yMin; y < yMax; y++) {
      if (isSolidTile(xa, ya, xMax, y)) return true;
    }
    return false;
  }

  // For Spike Detection
  public boolean isDamaged(int xa, int ya) {
    int xMin = 0;
    int xMax = 7;
    int yMin = 3;
    int yMax = 7;

    for (int x = xMin; x < xMax; x++) {
      if (isSpikeTile(xa, ya, x, yMin)) return true;
    }
    for (int x = xMin; x < xMax; x++) {
      if (isSpikeTile(xa, ya, x, yMax)) return true;
    }
    for (int y = yMin; y < yMax; y++) {
      if (isSpikeTile(xa, ya, xMin, y)) return true;
    }
    for (int y = yMin; y < yMax; y++) {
      if (isSpikeTile(xa, ya, xMax, y)) return true;
    }
    return false;
  }

  // for chest detection
  public boolean isWin(int xa, int ya) {
    int xMin = 0;
    int xMax = 7;
    int yMin = 3;
    int yMax = 7;

    for (int x = xMin; x < xMax; x++) {
      if (isChestTile(xa, ya, x, yMin)) return true;
    }
    for (int x = xMin; x < xMax; x++) {
      if (isChestTile(xa, ya, x, yMax)) return true;
    }
    for (int y = yMin; y < yMax; y++) {
      if (isChestTile(xa, ya, xMin, y)) return true;
    }
    for (int y = yMin; y < yMax; y++) {
      if (isChestTile(xa, ya, xMax, y)) return true;
    }
    return false;
  }

  // returns health
  public static int getHealth() {
    return health;
    // We made this static so it is accessible from the Mob class
  }

  // sets health
  public static void setHealth(int Health) {
    health = Health;
    // It's static for the same reason as getHealth
  }
}
コード例 #3
0
  // renders player with spritesheet info
  public void render(Screen screen) {
    // location of player on sprite sheet (first tile)
    // player is made out of 4 tiles
    int xTile = 0;
    int yTile = 28;

    //////////////////////////////////////////////////////////////////
    // Entire section flips player sprite image based on direction
    int walkingSpeed = 3;
    int flipTop = (numSteps >> walkingSpeed) & 1;
    int flipBottom = (numSteps >> walkingSpeed) & 1;

    if (movingDir == 1) {
      xTile += 2;
    } else if (movingDir > 1) {
      xTile += 4 + ((numSteps >> walkingSpeed) & 1) * 2;
      flipTop = (movingDir - 1) % 2;
    }
    ///////////////////////////////////////////////////////////////////

    // modifier variable
    int modifier = 8 * scale;
    int xOffset = x - modifier / 2;
    int yOffset = y - modifier / 2 - 4;

    /* multiply by 32 to get actual tile in sprite sheet*/
    // upper body of player
    screen.render(
        xOffset + (modifier * flipTop), yOffset, xTile + yTile * 32, color, flipTop, scale);
    screen.render(
        xOffset + modifier - (modifier * flipTop),
        yOffset,
        (xTile + 1) + yTile * 32,
        color,
        flipTop,
        scale);

    // lower body of player
    screen.render(
        xOffset + (modifier * flipBottom),
        yOffset + modifier,
        xTile + (yTile + 1) * 32,
        color,
        flipBottom,
        scale);
    screen.render(
        xOffset + modifier - (modifier * flipBottom),
        yOffset + modifier,
        (xTile + 1) + (yTile + 1) * 32,
        color,
        flipBottom,
        scale);

    // changes hearts above player based on life total
    if (health == 13) {
      life = "###";
      adjustment = 5;
    } else if (health == 9) {
      life = "##";
      adjustment = 0;
    } else if (health == 5) {
      life = "#";
      adjustment = -3;
    }

    // renders life (in the form of hearts) above player
    Font.render(life, screen, xOffset - adjustment, yOffset - 10, Colors.get(-1, -1, -1, 257), 1);
  }