This is now my render method:
public void render() {
Graphics2D bbg = (Graphics2D) this.backBuffer.getGraphics();
BufferedImage image = null;
this.player = this.game.getTileMap().getPlayer();
this.units = this.game.getTileMap().getUnits();
this.inventory = player.getInventory();
nullTile = new Tile(position, "WATER");
for(int y = 0; y < 24; y++){
for(int x = 0; x < 42; x++){
position.setX(x + player.getPosition().getX()-xOffset);
position.setY(y + player.getPosition().getY()-yOffset);
//Draw tiles
Tile tile = this.game.getTileMap().getTileAt(position);
if(tile != null){
image = tile.getImage();
}else{
image = nullTile.getImage();
}
//Draw items
Item item = this.game.getTileMap().getItemAt(position);
if(item != null){
image = item.getImage();
}
//Draw buildings
Building building = this.game.getTileMap().getBuildingAt(position);
if(building != null){
image = building.getImage();
}
//Old draw units
/**
Unit unit = this.game.getTileMap().getUnitAt(position);
if(unit != null){
image = unit.getImage();
yOffset = -40; //Unit height
}
**/
bbg.drawImage(image, x*tileDimension, y*tileDimension, this);
}
}
for(Unit unit : units){
image = unit.getImage();
bbg.drawImage(image, unit.getPosition().getX() + unit.getCurrentPixelX() + (xOffset * tileDimension), unit.getPosition().getY() + unit.getCurrentPixelY() + (yOffset * tileDimension) - unitHeightOffset, this);
}
bbg.drawImage(inventory.getImage(), 400, 480, this);
bbg.setColor(Color.WHITE);
bbg.fillRect(0, 580, 40, 20);
bbg.setColor(Color.BLACK);
bbg.drawString(String.valueOf(gui.getFps()), 10, 595);
bbg.dispose();
}
My move method:
public boolean moveUnitToAnimated(Position from, Position to){
Tween tween = new Tween();
if(isAllowedUnitPosition(to)){
Unit unit = this.tileMap.getUnitAt(from);
this.tileMap.removeUnit(from);
unit.setDirection(new Vector(to.getX() + from.getX(), to.getY() + from.getY()));
//Going left
if(to.getX() < from.getX() && to.getY() == from.getY()){
tween.tweenUnitLeft(unit);
}
//Going right
else if(to.getX() > from.getX() && to.getY() == from.getY()){
tween.tweenUnitLRight(unit);
}
//Going up
else if(to.getX() == from.getX() && to.getY() < from.getY()){
tween.tweenUnitUp(unit);
}
//Going down
else if(to.getX() == from.getX() && to.getY() > from.getY()){
tween.tweenUnitDown(unit);
}
this.tileMap.addUnitAt(to, unit);
this.tileMap.getTileAt(from).setOccupied(false);
this.tileMap.getTileAt(to).setOccupied(true);
return true;
}
return false;
}
And my tween class:
public class Tween {
public void tweenUnitLeft(Unit unit){
for(int i = 20; i > 0; i--){
unit.setCurrentPixelX(i);
}
unit.setPosition(new Position(unit.getPosition().getX() - 1, unit.getPosition().getY()));
unit.setCurrentPixelX(0);
}
public void tweenUnitLRight(Unit unit){
for(int i = 0; i < 0; i++){
unit.setCurrentPixelX(i);
}
unit.setPosition(new Position(unit.getPosition().getX() + 1, unit.getPosition().getY()));
unit.setCurrentPixelX(0);
}
public void tweenUnitUp(Unit unit){
for(int i = 20; i > 0; i--){
unit.setCurrentPixelY(i);
}
unit.setPosition(new Position(unit.getPosition().getX(), unit.getPosition().getY() - 1));
unit.setCurrentPixelY(0);
}
public void tweenUnitDown(Unit unit){
for(int i = 0; i < 20; i++){
unit.setCurrentPixelX(i);
}
unit.setPosition(new Position(unit.getPosition().getX(), unit.getPosition().getY() + 1));
unit.setCurrentPixelY(0);
}
}
Is that anything what you meant? 