void move_towards_target(Enemy monster) { if (monster.getTarget() == null) { return; } int dist_x = monster.getTarget().loc_x - (monster.getX() + (monster.getWidth() / 2)) / game_state.tile_engine.getTileSize(); int dist_y = monster.getTarget().loc_y - (monster.getY() + (monster.getHeight())) / game_state.tile_engine.getTileSize(); if (dist_x == 0 && dist_y == 0) { monster.nextTarget(); return; } if (Math.Abs(dist_x) > Math.Abs(dist_y)) { //Advance in the X direction if (dist_x > 0) { monster.setX(monster.getX() + monster.getSpeed()); monster.setDirection(PlayerDir.RIGHT); } else { monster.setX(monster.getX() - monster.getSpeed()); monster.setDirection(PlayerDir.LEFT); } } else { //Advance in the Y direction if (dist_y > 0) { monster.setY(monster.getY() + monster.getSpeed()); monster.setDirection(PlayerDir.DOWN); } else { monster.setY(monster.getY() - monster.getSpeed()); monster.setDirection(PlayerDir.UP); } } if (monster.getLastDirection() != monster.getDirection()) { monster.getSprite().StartAnimating((int)monster.getDirection() * 3, ((int)monster.getDirection() * 3) + 2); } }
void move_towards_target(Enemy monster) { if (monster.getTarget() == null) { return; } int dist_x = monster.getTarget().loc_x - (monster.getX()+(monster.getWidth()/2))/game_state.tile_engine.getTileSize(); int dist_y = monster.getTarget().loc_y - (monster.getY()+(monster.getHeight())) / game_state.tile_engine.getTileSize(); if (dist_x == 0 && dist_y == 0) { monster.nextTarget(); return; } if (Math.Abs(dist_x) > Math.Abs(dist_y)) { //Advance in the X direction if (dist_x > 0) { monster.setX(monster.getX() + monster.getSpeed()); monster.setDirection(PlayerDir.RIGHT); } else { monster.setX(monster.getX() - monster.getSpeed()); monster.setDirection(PlayerDir.LEFT); } } else { //Advance in the Y direction if (dist_y > 0) { monster.setY(monster.getY() + monster.getSpeed()); monster.setDirection(PlayerDir.DOWN); } else { monster.setY(monster.getY() - monster.getSpeed()); monster.setDirection(PlayerDir.UP); } } if (monster.getLastDirection() != monster.getDirection()) { monster.getSprite().StartAnimating((int)monster.getDirection() * 3, ((int)monster.getDirection() * 3) + 2); } }