Package com.thecherno.cherno.engine.entity.mob

Source Code of com.thecherno.cherno.engine.entity.mob.Mob

package com.thecherno.cherno.engine.entity.mob;

import com.thecherno.cherno.engine.entity.Entity;
import com.thecherno.cherno.engine.level.TiledLevel;

public abstract class Mob extends Entity {

  public void move(int xa, int ya) {
    if (xa != 0 && ya != 0) {
      move(xa, 0);
      move(0, ya);
      return;
    }
    if (!collides(xa, ya)) {
      x += xa;
      y += ya;
    }
  }

  private boolean collides(int xa, int ya) {
    if (level instanceof TiledLevel) {
      TiledLevel level = (TiledLevel) this.level;
      for (int c = 0; c < 4; c++) {
        int xt = ((x + xa) - c % 2 * getWidth()) / level.getTileSize();
        int yt = ((y + ya) - c / 2 * getHeight()) / level.getTileSize();
        // System.out.println(xt + ", " + yt);
        if (level.getTile(xt, yt) == null) continue;
        if (level.getTile(xt, yt).solid()) {
          return true;
        }
      }
    }
    return false;
  }

}
TOP

Related Classes of com.thecherno.cherno.engine.entity.mob.Mob

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.