Classe Entity completo:
package com.estudos.entities;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.util.Comparator;
import java.util.List;
import java.util.Random;
import com.estudos.main.Game;
import com.estudos.world.Camera;
import com.estudos.world.Node;
import com.estudos.world.Vector2i;
import com.estudos.world.World;
public class Entity {
//public static final BufferedImage MOEDA_SPRITE = null;
public static BufferedImage NUVEM = Game.spritesheet.getSprite(48, 0, 16, 16);
public static BufferedImage[] PLAYER_SPRITE_RIGHT = {Game.spritesheet.getSprite(4*16, 0,16,16), Game.spritesheet.getSprite(5*16, 0,16,16)};
public static BufferedImage [] PLAYER_SPRITE_LEFT = {Game.spritesheet.getSprite(6*16, 0,16,16), Game.spritesheet.getSprite(7*16, 0,16,16)};
public static BufferedImage ENEMY_RIGHT = Game.spritesheet.getSprite(128, 0, 16,16);
public static BufferedImage ENEMY_LEFT = Game.spritesheet.getSprite(144, 0, 16,16);
public static BufferedImage MOEDA_EN = Game.spritesheet.getSprite(16,0,16,16);
protected double x;
protected double y;
protected int width;
protected int height;
protected double speed;
public int depth;
protected List<Node> path;
public boolean debug = false;
protected BufferedImage sprite;
public static Random rand = new Random();
public Entity(double x,double y,int width,int height,double speed,BufferedImage sprite){
this.x = x;
this.y = y;
this.speed = speed;
this.width = width;
this.height = height;
this.sprite = sprite;
}
public static Comparator<Entity> nodeSorter = new Comparator<Entity>() {
@Override
public int compare(Entity n0,Entity n1) {
if(n1.depth < n0.depth)
return +1;
if(n1.depth > n0.depth)
return -1;
return 0;
}
};
public void updateCamera() {
Camera.x = Camera.clamp(this.getX() - (Game.WIDTH/2),0,World.WIDTH*16 - Game.WIDTH);
Camera.y = Camera.clamp(this.getY() - (Game.HEIGHT/2),0,World.HEIGHT*16 - Game.HEIGHT);
}
public void setX(int newX) {
this.x = newX;
}
public void setY(int newY) {
this.y = newY;
}
public int getX() {
return (int)this.x;
}
public int getY() {
return (int)this.y;
}
public int getWidth() {
return this.width;
}
public int getHeight() {
return this.height;
}
public void tick(){}
public double calculateDistance(int x1,int y1,int x2,int y2) {
return Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
public void followPath(List<Node> path) {
if(path != null) {
if(path.size() > 0) {
Vector2i target = path.get(path.size() - 1).tile;
//xprev = x;
//yprev = y;
if(x < target.x * 16) {
x++;
}else if(x > target.x * 16) {
x--;
}
if(y < target.y * 16) {
y++;
}else if(y > target.y * 16) {
y--;
}
if(x == target.x * 16 && y == target.y * 16) {
path.remove(path.size() - 1);
}
}
}
}
public static boolean isColidding(Entity e1,Entity e2){
Rectangle e1Mask = new Rectangle(e1.getX(),e1.getY(),e1.getWidth(),e1.getHeight());
Rectangle e2Mask = new Rectangle(e2.getX(),e2.getY(),e2.getWidth(),e2.getHeight());
return e1Mask.intersects(e2Mask);
}
public void render(Graphics g) {
g.drawImage(sprite,this.getX() - Camera.x,this.getY() - Camera.y,null);
//g.setColor(Color.red);
//g.fillRect(this.getX() + maskx - Camera.x,this.getY() + masky - Camera.y,mwidth,mheight);
}
}
e
Enemy completo:
package com.estudos.entities;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import com.estudos.world.World;
public class Enemy extends Entity {
public boolean right = true, left = false;
public int vida = 3;
public Enemy(double x, double y, int width, int height, double speed, BufferedImage sprite) {
super(x, y, width, height, speed, sprite);
}
public void tick() {
if (World.isFree((int) x, (int) (y + 1))) {
y += 1;
} else {
if (right) {
if (World.isFree((int) (x + speed), (int) y)) {
x += speed;
if (World.isFree((int) (x + 16), (int) y + 1)) {
right = false;
left = true;
}
} else {
right = false;
left = true;
}
}
if (left) {
if (World.isFree((int) (x - speed), (int) y)) {
x -= speed;
if (World.isFree((int) (x - 16), (int) y + 1)) {
right = true;
left = false;
}
} else {
right = true;
left = false;
}
}
}
}
public void render(Graphics g) {
if (right)
sprite = Entity.ENEMY_RIGHT;
else if (left)
sprite = Entity.ENEMY_LEFT;
super.render(g);
}
}