Hello,
i trying to figure out the best way to make collision check on my code. I would like to add it into onWalk and using getNextPosition to check if the position is walkAble. Monsters & Players share the same logic.
public class Creature {
protected enum Direction {
NORTH, EAST, SOUTH, WEST
}
protected final String name;
protected Direction direction;
private Vector2 targetPosition;
protected Vector2 position;
protected float speed;
private Texture texture = new Texture("badlogic.jpg");
protected boolean isMoving = false;
public Creature(String name, Vector2 position) {
this.name = name;
this.position = position;
targetPosition = new Vector2();
speed = 4f;
direction = Direction.SOUTH;
}
public void onThink(float deltaTime) {
if (isMoving) {
onWalk(direction, deltaTime);
}
}
protected void onWalk(Direction direction, float deltaTime) {
if (targetPosition.isZero()) {
targetPosition.set(getNextPosition(direction, position));
}
if (position.dst(targetPosition) <= 0.1f) {
isMoving = false;
position.set(targetPosition);
targetPosition.setZero();
return;
}
switch (direction) {
case NORTH:
position.y += speed * deltaTime;
break;
case EAST:
position.x += speed * deltaTime;
break;
case SOUTH:
position.y -= speed * deltaTime;
break;
case WEST:
position.x -= speed * deltaTime;
break;
default:
break;
}
}
public Vector2 getNextPosition(Direction dir, Vector2 pos) {
pos = new Vector2(pos.x, pos.y);
switch (dir) {
case NORTH:
pos.y++;
break;
case EAST:
pos.x++;
break;
case SOUTH:
pos.y--;
break;
case WEST:
pos.x--;
break;
default:
break;
}
return pos;
}
public Vector2 getPosition() {
return position;
}
public boolean isCreature() {
return true;
}
public Texture getTexture() {
return texture;
}
public boolean isPlayer() {
return false;
}
public boolean isMonster() {
return false;
}
}