Build-Your-Own-World / byow / Core / Creature.java
Creature.java
Raw
package byow.Core;

import byow.TileEngine.TETile;
import byow.TileEngine.Tileset;

public class Creature {
    int x;
    int y;

    Creature(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public static TETile[][] move(int x, int y, TETile[][] world) {
        String direction;
        if (x == 0) {
            if (y == -1) {
                direction = "down";
            } else {
                direction = "up";
            }
        } else {
            if (x == 1) {
                direction = "right";
            } else {
                direction = "left";
            }
        }
        // if the movement is out of bounds, don't do this
        for (int i = 0; i < Rectangle.MAP_WIDTH; i++) {
            for (int j = 0; j < Rectangle.MAP_HEIGHT; j++) {
                if (world[i][j] == Tileset.AVATAR) {
                    if (direction.equals("left")) {
                        if (world[i - 1][j] != Tileset.WALL) {
                            world[i][j] = Tileset.FLOOR;
                            world[i - 1][j] = Tileset.AVATAR;
                            return world;
                        }
                    } else if (direction.equals("right")) {
                        if (world[i + 1][j] != Tileset.WALL) {
                            world[i][j] = Tileset.FLOOR;
                            world[i + 1][j] = Tileset.AVATAR;
                            return world;
                        }
                    } else if (direction.equals("up")) {
                        if (world[i][j + 1] != Tileset.WALL) {
                            world[i][j] = Tileset.FLOOR;
                            world[i][j + 1] = Tileset.AVATAR;
                            return world;
                        }
                    } else {
                        if (world[i][j - 1] != Tileset.WALL) {
                            world[i][j] = Tileset.FLOOR;
                            world[i][j - 1] = Tileset.AVATAR;
                            return world;
                        }
                    }
                }
            }
        }
        return world;
    }

    // if we implement npcs, make separate Ais for player avatar and npcs?
    public static int getAvatarX(TETile[][] world) {
        for (int i = 0; i < Rectangle.MAP_WIDTH; i++) {
            for (int j = 0; j < Rectangle.MAP_HEIGHT; j++) {
                if (world[i][j] == Tileset.AVATAR) {
                    return i;
                }
            }
        }
        return 5;
    }


    public static int getAvatarY(TETile[][] world) {
        for (int i = 0; i < Rectangle.MAP_WIDTH; i++) {
            for (int j = 0; j < Rectangle.MAP_HEIGHT; j++) {
                if (world[i][j] == Tileset.AVATAR) {
                    return j;
                }
            }
        }
        return 5;
    }

    public static void clearAvatar(TETile[][] world) {
        for (int i = 0; i < Rectangle.MAP_WIDTH; i++) {
            for (int j = 0; j < Rectangle.MAP_HEIGHT; j++) {
                if (world[i][j] == Tileset.AVATAR) {
                    world[i][j] = Tileset.FLOOR;
                    return;
                }
            }
        }
    }
}