class Game {
    constructor() {
        this.tiles = 500;
        this.hills = 1500;
        this.runonce = true;
        this.toggleMovement = false;
        this.image = new Images();
    }
    Start() {
        this.canvas = document.getElementsByTagName("canvas")[0];
        this.context = this.canvas.getContext('2d');
        this.canvas.width = window.innerWidth;
        this.canvas.height = (window.innerHeight - 64);
        if (!this.runonce) {
            this.world.destroyTimer();
            this.runonce = true;
        }
        this.setUpNewGame();
        requestAnimationFrame(() => this.update());
    }
    update() {
        this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
        this.world.updateWorldDisplay();
    }
    setUpNewGame() {
        this.world = new World(this.tiles, this.hills);
        this.world.setupWorld();
        this.player = new Player(this.world, 'player1', 0);
        this.player.spawnPlayerInWorld();
        this.runonce = false;
    }
    keyPress(event) {
        switch (event.keyCode) {
            case 69:
                this.player.Action();
                break;
            case 38:
                if (this.toggleMovement == true) {
                    this.player.playerMovement('up');
                }
                else {
                    this.world.camMovement(this.player.getID(), 'up');
                }
                break;
            case 39:
                if (this.toggleMovement == true) {
                    this.player.playerMovement('right');
                }
                else {
                    this.world.camMovement(this.player.getID(), 'right');
                }
                break;
            case 40:
                if (this.toggleMovement == true) {
                    this.player.playerMovement('down');
                }
                else {
                    this.world.camMovement(this.player.getID(), 'down');
                }
                break;
            case 37:
                if (this.toggleMovement == true) {
                    this.player.playerMovement('left');
                }
                else {
                    this.world.camMovement(this.player.getID(), 'left');
                }
                break;
            case 67:
                if (this.toggleMovement == true) {
                    this.toggleMovement = false;
                }
                else {
                    this.toggleMovement = true;
                }
                break;
            case 73:
                this.world.mapZoom('in');
                break;
            case 79:
                this.world.mapZoom('out');
                break;
        }
    }
    clickedMouse(event) {
        var ex = event.pageX - canvas.offsetLeft, ey = event.pageY - canvas.offsetTop;
        this.world.getPlayerClickedTileInfo(this.player.getID(), ex, ey);
    }
}
class Images {
    constructor(preload = true) {
        this.imgurl = [
            "css/icon/smile2.png",
            "css/icon/desert.png", "css/icon/desertplains.png",
            "css/icon/plains.png", "css/icon/plains2.png",
            "css/icon/tree.png", "css/icon/tree2.png",
            "css/icon/hill.png", "css/icon/hill2.png",
            "css/icon/mntn.png", "css/icon/mntn2.png", "css/icon/mntn3.png", "css/icon/snow.png",
            "css/icon/town.png", "css/icon/outpost.png",
            "css/icon/road_l_hor.png", "css/icon/road_l_ver.png",
            "css/icon/road_l_int.png", "css/icon/road_l_int_e.png",
            "css/icon/road_l_int_n.png", "css/icon/road_l_int_s.png",
            "css/icon/road_l_int_w.png", "css/icon/road_l_cur_ne.png",
            "css/icon/road_l_cur_nw.png", "css/icon/road_l_cur_se.png",
            "css/icon/road_l_cur_sw.png",
            "css/icon/smile3.png",
            "css/icon/bush.png", "css/icon/bush2.png", "css/icon/bush3.png",
            "css/icon/tree_cut.png", "css/icon/tree2_cut.png"
        ];
        if (preload) {
            for (var i = 0; i < this.imgurl.length; i++) {
                document.getElementById('preload').innerHTML += "<img src='" + this.imgurl[i] + "'/>";
            }
        }
    }
    getTileImage(id) {
        return this.imgurl[id];
    }
}
var canvas = document.getElementById("cvboard");
var g = new Game();
window.addEventListener("load", function () {
    g.Start();
});
document.addEventListener("keydown", function (event) {
    g.keyPress(event);
});
canvas.addEventListener('click', function (event) {
    g.clickedMouse(event);
});
function generate() {
    g.Start();
}
function closeWindow() {
    document.getElementById('setup').innerHTML = "";
    document.getElementById('setup').style.display = "none";
    return true;
}
class Player {
    constructor(w, name, id) {
        this.pid = 0;
        this.tree1 = 5;
        this.tree2 = 6;
        this.town = 13;
        this.world = w;
        this.pname = name;
        this.pid = id;
    }
    getWorld() {
        return this.world;
    }
    getName() {
        return this.pname;
    }
    getID() {
        return this.pid;
    }
    Action() {
        this.playerDoAction();
    }
    playerMovement(dir) {
        this.movePlayerInWorld(dir);
    }
    spawnPlayerInWorld() {
        let spawnlocs = this.world.getSpawnLocations(), randspawn = Math.floor(Math.random() * (spawnlocs.length));
        this.world.addPlayer(this.pid);
        this.world.setPlayerWorldPos(this.pid, spawnlocs[randspawn][0], spawnlocs[randspawn][1]);
        this.world.setPlayerCamera(this.pid, spawnlocs[randspawn][0] - 8, spawnlocs[randspawn][1] - 8);
        console.log("pID: " + this.pid + ", cam-pos-start=" + this.world.getPlayerCamX(this.pid) + "-" + this.world.getPlayerCamY(this.pid) + ", player-spawn-count:" + randspawn);
    }
    playerDoAction() {
        let pX = this.world.getPlayerWorldX(this.pid), pY = this.world.getPlayerWorldY(this.pid), ignore = false;
        if (this.world.getPlayerTile() == this.tree1 || this.world.getPlayerTile() == this.tree2) {
            if (this.world.getPlayerTile() == this.tree1) {
                console.log("cutting tree (inventory adding here)");
                return this.world.setTileType(pX, pY, 30);
            }
            else {
                console.log("cutting tree (inventory adding here)");
                return this.world.setTileType(pX, pY, 31);
            }
        }
        else if (this.isTileTypeAroundPlayer(this.tree1) || this.isTileTypeAroundPlayer(this.tree2)) {
            let ret = new Object();
            ret = this.world.searchForTileTypeReturnClosest(pX, pY, 5);
            console.log("Checklist " + ret);
            console.log("cutting nearby tree (inventory adding here)");
            return this.world.setTileType(pX, pY, 30);
        }
        if (this.isTileTypeAroundPlayer(this.town)) {
            return console.log("a town interaction here (placeholder)");
        }
        else {
            return console.log("nothing to interact with! (placeholder)");
        }
    }
    isTileTypeAroundPlayer(tiletype) {
        let pX = this.world.getPlayerWorldX(this.pid), pY = this.world.getPlayerWorldY(this.pid);
        return this.world.searchForTileType(pX, pY, tiletype);
    }
    movePlayerInWorld(dir) {
        let pX = this.world.getPlayerWorldX(this.pid), pY = this.world.getPlayerWorldY(this.pid);
        if (dir == 'left' && this.world.getTileAroundPlayer(this.pid, -1) != 0) {
            pX -= 1;
        }
        else if (dir == 'right' && this.world.getTileAroundPlayer(this.pid, 1) != 0) {
            pX += 1;
        }
        else if (dir == 'up' && this.world.getTileAroundPlayer(this.pid, 0, -1) != 0) {
            pY -= 1;
        }
        if (dir == 'down' && this.world.getTileAroundPlayer(this.pid, 0, 1) != 0) {
            pY += 1;
        }
        this.world.setPlayerWorldPos(this.pid, pX, pY);
        this.world.updateWorldDisplay();
    }
}
class World {
    constructor(maxtiles = 500, balls = 1500, sand = 1, ground = 6, hill = 40, mountain = 50, snowtop = 70, towndistance = 15) {
        this.mapX = 0;
        this.mapY = 0;
        this.camX = 0;
        this.camY = 0;
        this.playerCamPos = [];
        this.playerX = 0;
        this.playerY = 0;
        this.playerPos = [];
        this.spawncount = 0;
        this.windowX = 0;
        this.windowY = 0;
        this.heightmap = new Array();
        this.tiles = new Array();
        this.tileSize = 24;
        this.maxTiles = 500;
        this.maxBalls = 3000;
        this.tileswitch = true;
        this.zoomlevel = 1;
        this.maxzoomout = 5;
        this.zoompx = 4;
        this.tilecolors = [
            "#4997FF",
            "#E8C916", "#E8C916",
            "#5BB822", "#5BB822",
            "#4B981C", "#4B981C",
            "#AFE037", "#AFE037",
            "#CCCCCC", "#CCCCCC", "#CCCCCC", "#FFFFFF",
            "#FFEC8E", "#5BB822",
            "#808060", "#808060", "#808060", "#808060", "#808060", "#808060", "#808060", "#808060", "#808060", "#808060", "#808060",
            "#333333",
            "#5da53a", "#5da53a", "#5da53a",
            "#4B981C", "#4B981C"
        ];
        this.minimalimgdraw = 16;
        this.generictilename = [
            "water",
            "cactus", "desert",
            "plains", "plains",
            "tree", "tree",
            "hills", "hills",
            "mountain", "mountain", "mountain", "mountain snow",
            "town", "watch tower",
            "road", "road", "road", "road", "road", "road", "road", "road", "road", "road", "road",
            "npc",
            "bushes", "bushes", "bushes",
            "cutted tree", "cutted tree"
        ];
        this.typebyheight = [
            1,
            6,
            40,
            50,
            70
        ];
        this.spawnlocs = new Array();
        this.town = new Array();
        this.towndistance = 15;
        this.townid = 0;
        this.minimalWoodstock = 6;
        this.minimalStonestock = 10;
        this.townnames = new Array(["New ", "Old ", "Belle-", "Great ", "Fine ", "Fort ", "Grand ", "Jolly ", "Los ", "Sugary ", "Spooky ", "Lonely ", "Mystic ", "Golden ", "Port ", "San ", "Muddy ", "Snowy ", "Camp ", "Rio ", "Santa ", "Les ", "Cote des ", "El ", "Long ", "Baja ", "Grassy ", "Rosey ", "Lake ", "Hillside ", "Saint ", "Novi ", "Lone ", "Forgotten ", "Priest's ", "Shady ", "Abandoned ", "Hopeless ", "Fore ", "Virgin ", "Haunted ", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""], ["Babble", "Courtney", "Mansel", "Winston", "Apple", "Horse", "Otter", "Scorpion", "Banana", "Baby", "Merkel", "Dinkle", "Lumber", "Middle", "Moose", "Camel", "Glenn", "Or", "William", "Black", "White", "Yellow", "Wood", "Stone", "Iron", "Goat", "Gold", "Red", "Copper", "Mud", "John", "Dave", "Snow", "Turkey", "Red", "Cracker", "Miccous", "Mingle", "Fiddle", "Sparkle", "Cox", "Flint", "Triangle", "Gar", "Man", "Power", "Fish", "Bear", "Coffee", "Little", "Ever", "Bread", "Wheat", "Yarn", "Pannekoeken", "Cookie", "Mother", "Tallapoose", "Yazoo", "Skeleton", "Boo", "Fire", "Zulu", "Water", "Sky", "Christmas", "Thanksgiving", "Indiana", "Pine", "Oak", "Willow", "Maple", "Ogre", "Goblin", "Trollen", "Amster", "Bone", "Children", "Bingo", "Wanker", "Church", "God", "Locust", "Crab", "Fertile", "Ash", "Mattock", "Manter", "Yangle", "Work", "Butter", "Jelly", "Liberty", "Milk", "Glob", "Jack", "Duffel", "Corner", "Rabbit", "Christ", "School", "Tornado", "Tsunami", "Earthquake", "Odin", "Zeus", "Fork", "Spoon", "Machete", "Friend", "Kindle", "Chicken", "Goose", "Duck", "Croc", "Alligator", "Rock", "Scissor", "Candle", "Gust", "Chipper", "Spider", "Lady", "Gentle", "Clay", "Serb", "Windmill", "Citron", "Lemon", "Melon", "Paper", "Flash", "Frog", "Toad", "Snake", "Bell", "Ringing", "Pineapple", "Meat", "Super", "Wax", "Herb", "Almond", "Dagger", "Pilk", "Silk", "War"], [" Field", "heim", "ville", " City", " Valley", " Town", "shire", "-Pablos", " Village", "ton", "dale", "list", "ington", "-Ex", "lington", "lee", " Harbor", "ey", "moore", "loo", " Hole", " Well", " Plateau", " Swamp", "-Roche", " Rogue", " Crater", " Paradise", " Hill", " Hot", " Station", " Arm", " Coast", " Beach", "daal", " Stad", "er", "ard", "gaard", "garde", "mont", "burg", " Platte", "wood", " Park", " Springs", "green", "ery", " Sad", " Peak", " Edge", " High", "way", "craft", "fall", "clap", " Lodge", "Hope", " Chance", " Vault", "Cafe", "-Ginn", "dam", "'s Last Stand"]);
        this.image = new Images(false);
        this.maxTiles = maxtiles;
        this.maxBalls = balls;
        this.mapX = this.maxTiles;
        this.mapY = this.maxTiles;
        this.windowX = Math.floor(window.innerWidth / this.tileSize) + 1;
        this.windowY = Math.floor(window.innerHeight / this.tileSize);
        this.typebyheight[0] = sand;
        this.typebyheight[1] = ground;
        this.typebyheight[2] = hill;
        this.typebyheight[3] = mountain;
        this.typebyheight[4] = snowtop;
        this.runTimers();
    }
    setupWorld() {
        this.generateNewWorld(this.maxTiles, this.maxBalls);
    }
    updateWorldDisplay() {
        var canvasx = 0, canvasy = 0, tid, playerID = 0;
        this.canvas = document.getElementsByTagName("canvas")[0];
        this.context = this.canvas.getContext('2d');
        this.canvas.style.backgroundColor = this.tilecolors[0];
        for (let x = this.getPlayerCamX(playerID); x < this.getPlayerCamX(playerID) + this.windowX; x++) {
            for (let y = this.getPlayerCamY(playerID); y < this.getPlayerCamY(playerID) + this.windowY; y++) {
                this.context.beginPath();
                this.context.rect(canvasx, canvasy, this.tileSize, this.tileSize);
                if (this.tiles[x][y] >= 0) {
                    this.context.fillStyle = this.tilecolors[this.tiles[x][y]];
                }
                this.context.fill();
                this.context.lineWidth = 0;
                this.context.strokeStyle = 'grey';
                this.context.stroke();
                if (this.tileSize > this.minimalimgdraw) {
                    var img = new Image();
                    if (x == this.getPlayerWorldX(playerID) && y == this.getPlayerWorldY(playerID)) {
                        if (this.tiles[this.getPlayerWorldX(playerID)][this.getPlayerWorldY(playerID)] > 0) {
                            img.src = this.image.getTileImage(0);
                        }
                    }
                    else {
                        if (this.tiles[x][y] > 0) {
                            img.src = this.image.getTileImage(this.tiles[x][y]);
                        }
                    }
                    this.context.drawImage(img, canvasx + 2, canvasy + 2, this.tileSize - 4, this.tileSize - 4);
                }
                canvasy = canvasy + this.tileSize;
            }
            canvasx = canvasx + this.tileSize;
            canvasy = 0;
        }
        return true;
    }
    fillNewWorldTiles(max) {
        let scale = 100 / max;
        let spawncount = 0;
        for (let x = 0; x < this.mapX; x++) {
            for (let y = 0; y < this.mapY; y++) {
                let hh = Math.floor(this.heightmap[x][y] * scale);
                this.heightmap[x][y] = hh;
                if (hh >= this.typebyheight[4]) {
                    this.tiles[x][y] = 12;
                }
                else if (hh >= this.typebyheight[3]) {
                    if (this.tileSort(50, 0)) {
                        if (this.tileSort(50, 0)) {
                            this.tiles[x][y] = 10;
                        }
                        else {
                            this.tiles[x][y] = 11;
                        }
                    }
                    else {
                        this.tiles[x][y] = 9;
                    }
                }
                else if (hh >= this.typebyheight[2]) {
                    if (this.tileSort(50, 0)) {
                        this.tiles[x][y] = 8;
                    }
                    else {
                        this.tiles[x][y] = 7;
                    }
                    this.addSpawnLoc(x, y);
                }
                else if (hh >= this.typebyheight[1]) {
                    if (this.tileSort(98, 0)) {
                        if (this.tileSort(90, 0)) {
                            let tdist = Math.floor(Math.random() * (this.towndistance)) + this.towndistance;
                            if (this.tileOutRange(x, y, 13, tdist)) {
                                let townname = "";
                                for (let i = 0; i < this.townnames.length; i++) {
                                    townname += String(this.townnames[i][Math.floor(Math.random() * this.townnames[i].length)]);
                                }
                                this.tiles[x][y] = 13;
                                this.town[this.townid] = [];
                                this.town[this.townid][0] = townname;
                                this.town[this.townid][1] = x;
                                this.town[this.townid][2] = y;
                                this.town[this.townid][3] = this.minimalWoodstock;
                                this.town[this.townid][4] = this.minimalStonestock;
                                console.log(this.town[this.townid][1] + "," + "" + this.town[this.townid][2] + " - (tid: " + this.townid + ") name: " + this.town[this.townid][0]);
                                this.townid++;
                            }
                            else {
                                if (this.tileSort(50, 0)) {
                                    this.tiles[x][y] = 3;
                                }
                                else {
                                    this.tiles[x][y] = 4;
                                }
                                this.addSpawnLoc(x, y);
                            }
                        }
                        else {
                            if (this.tileSort(60, 0)) {
                                this.tiles[x][y] = 14;
                            }
                            else {
                                if (this.tileSort(50, 0)) {
                                    this.tiles[x][y] = 3;
                                }
                                else {
                                    this.tiles[x][y] = 4;
                                }
                                this.addSpawnLoc(x, y);
                            }
                        }
                    }
                    else {
                        if (this.tileSort(40, 50)) {
                            if (this.tileSort(50, 0)) {
                                if (this.tileSort(8, 0)) {
                                    this.tiles[x][y] = 5;
                                }
                                else {
                                    this.tiles[x][y] = 30;
                                }
                            }
                            else {
                                if (this.tileSort(8, 0)) {
                                    this.tiles[x][y] = 6;
                                }
                                else {
                                    this.tiles[x][y] = 31;
                                }
                            }
                        }
                        else {
                            if (this.tileSort(15, 0)) {
                                if (this.tileSort(50, 0)) {
                                    this.tiles[x][y] = 3;
                                }
                                else {
                                    this.tiles[x][y] = 4;
                                }
                                this.addSpawnLoc(x, y);
                            }
                            else {
                                if (this.tileSort(60, 0)) {
                                    this.tiles[x][y] = 27;
                                }
                                else {
                                    this.tiles[x][y] = 29;
                                }
                            }
                        }
                    }
                }
                else if (hh >= this.typebyheight[0]) {
                    if (this.tileSort(8, 0)) {
                        this.tiles[x][y] = 2;
                    }
                    else {
                        this.tiles[x][y] = 1;
                    }
                }
                else {
                    this.tiles[x][y] = 0;
                }
            }
        }
    }
    generateNewWorld(maxtiles, balls) {
        for (let i = 0; i < this.mapX; i++) {
            this.heightmap[i] = [];
            this.tiles[i] = [];
            for (let j = 0; j < this.mapY; j++) {
                this.heightmap[i][j] = 0;
            }
        }
        let max = 0;
        for (let i = 0; i < balls; i++) {
            let ballx = Math.floor(Math.random() * (this.mapX - 30) + 15);
            let bally = Math.floor(Math.random() * (this.mapY - 30) + 15);
            let ballr = Math.floor(Math.random() * (10) + 5);
            for (let x = -ballr; x <= ballr; x++) {
                for (let y = -ballr; y <= ballr; y++) {
                    let h = ballr * ballr - x * x - y * y;
                    if (h > 0) {
                        let cx = ballx + x;
                        let cy = bally + y;
                        if (cx > 0 && cx <= this.mapX && cy > 0 && cy <= this.mapY) {
                            let hh = this.heightmap[cx][cy] + h;
                            this.heightmap[cx][cy] = hh;
                            if (hh > max) {
                                max = hh;
                            }
                        }
                    }
                }
            }
        }
        this.fillNewWorldTiles(max);
    }
    TileSwitcher(type, playerID = 0) {
        if (type == 0) {
            this.canvas = document.getElementsByTagName("canvas")[0];
            this.context = this.canvas.getContext('2d');
            if (this.tileswitch) {
                this.tileswitch = false;
            }
            else {
                this.tileswitch = true;
            }
            var ux = (this.getPlayerWorldX(playerID) - this.getPlayerCamX(playerID)) * this.tileSize;
            var uy = (this.getPlayerWorldY(playerID) - this.getPlayerCamY(playerID)) * this.tileSize;
            var img = new Image();
            this.context.beginPath();
            this.context.rect(ux, uy, this.tileSize, this.tileSize);
            this.context.fillStyle = this.tilecolors[this.tiles[this.getPlayerWorldX(playerID)][this.getPlayerWorldY(playerID)]];
            this.context.fill();
            if (this.tileSize > this.minimalimgdraw) {
                if (this.tileswitch) {
                    img.src = this.image.getTileImage(this.tiles[this.getPlayerWorldX(playerID)][this.getPlayerWorldY(playerID)]);
                }
                else {
                    img.src = this.image.getTileImage(0);
                }
                this.context.drawImage(img, ux + 2, uy + 2, this.tileSize - 4, this.tileSize - 4);
            }
            else {
                img.src = this.image.getTileImage(0);
                this.context.drawImage(img, ux - 4, uy - 4, this.tileSize + 8, this.tileSize + 8);
            }
        }
        else {
        }
    }
    tileSort(range, extraoff) {
        if (Math.floor(Math.random() * (100) - extraoff) > range) {
            return true;
        }
        else {
            return false;
        }
    }
    tileOutRange(x, y, tiletype, distance) {
        for (let x2 = x + distance; x2 > (x - distance); x2--) {
            for (let y2 = y + distance; y2 > (y - distance); y2--) {
                if (x2 > distance && y2 > distance && x2 < this.mapX - distance && y2 < this.mapY - distance) {
                    if (this.tiles[x2][y2] == tiletype) {
                        return false;
                    }
                }
            }
        }
        return true;
    }
    addSpawnLoc(x, y) {
        this.spawnlocs[this.spawncount] = [];
        this.spawnlocs[this.spawncount][0] = x;
        this.spawnlocs[this.spawncount][1] = y;
        return this.spawncount++;
    }
    clickedTileInfo(x, y) {
        var string = "", playerID = 0;
        if (x == this.playerPos[playerID][0] && y == this.playerPos[playerID][1]) {
            string = "<p>clicked on player (" + x + "-" + y + "-" + this.tiles[x][y] + ")</p>";
        }
        else {
            string = "<p>clicked on " + this.generictilename[this.tiles[x][y]] + " (" + x + "-" + y + "-" + this.tiles[x][y] + ")</p>";
            if (this.tiles[x][y] == 13) {
                for (var i = 0; i < this.town.length; i++) {
                    if (this.town[i][1] == x && this.town[i][2] == y) {
                        string = "<p>clicked on '" + this.town[i][0] + "' (" + x + "-" + y + "-" + this.tiles[x][y] + " townid " + i + " | woodstock = " + this.town[i][3] + ", stonestock = " + this.town[i][4] + ")</p>";
                        return document.getElementById('clickinfo').innerHTML = string;
                    }
                }
            }
        }
        return document.getElementById('clickinfo').innerHTML = string;
    }
    getTileType(x, y) {
        return this.tiles[x][y];
    }
    setTileType(x, y, type) {
        return this.tiles[x][y] = type;
    }
    addPlayer(playerID) {
        this.playerPos[playerID] = [];
        this.playerPos[playerID][0] = 0;
        this.playerPos[playerID][1] = 0;
        this.playerCamPos[playerID] = [];
        this.playerCamPos[playerID][0] = 0;
        this.playerCamPos[playerID][1] = 0;
    }
    getTileAroundPlayer(playerID = 0, x = 0, y = 0) {
        return this.tiles[this.playerPos[playerID][0] + x][this.playerPos[playerID][1] + y];
    }
    getPlayerTile(playerID = 0) {
        return this.tiles[this.playerPos[playerID][0]][this.playerPos[playerID][1]];
    }
    setPlayerCamera(playerID = 0, x, y) {
        this.playerCamPos[playerID][0] = x;
        this.playerCamPos[playerID][1] = y;
    }
    setPlayerCameraX(playerID = 0, x) {
        return this.playerCamPos[playerID][0] = x;
    }
    setPlayerCameraY(playerID = 0, y) {
        return this.playerCamPos[playerID][1] = y;
    }
    getPlayerWorldX(playerID = 0) {
        return this.playerPos[playerID][0];
    }
    getPlayerWorldY(playerID = 0) {
        return this.playerPos[playerID][1];
    }
    setPlayerWorldPos(playerID = 0, x, y) {
        this.playerPos[playerID][0] = x;
        this.playerPos[playerID][1] = y;
    }
    getPlayerCamX(playerID = 0) {
        return this.playerCamPos[playerID][0];
    }
    getPlayerCamY(playerID = 0) {
        return this.playerCamPos[playerID][1];
    }
    getSpawnLocations() {
        return this.spawnlocs;
    }
    searchForTileType(x, y, tiletype) {
        let beginx = x - 1, beginy = y - 1, endx = x + 1, endy = y + 1;
        for (let i = beginx; i <= endx; i++) {
            for (let j = beginy; j <= endy; j++) {
                if (this.tiles[i][j] == tiletype) {
                    return true;
                }
            }
        }
        return false;
    }
    searchForTileTypeReturnClosest(x, y, tiletype) {
        let beginx = x - 1, beginy = y - 1, endx = x + 1, endy = y + 1, ret = new Object(), nr = 0;
        for (let i = beginx; i <= endx; i++) {
            for (let j = beginy; j <= endy; j++) {
                if (this.tiles[i][j] == tiletype) {
                    ret[nr] = [];
                    ret[nr][0] = i;
                    ret[nr][1] = j;
                    ret[nr][2] = this.tiles[i][j];
                    nr++;
                }
            }
        }
        return ret;
    }
    camMovement(playerID = 0, dir) {
        if (dir == 'left') {
            if (this.getPlayerCamX(playerID) > 0) {
                this.setPlayerCameraX(playerID, (this.getPlayerCamX(playerID) - 1));
            }
        }
        if (dir == 'right') {
            if (this.getPlayerCamX(playerID) < this.mapX - this.tileSize) {
                this.setPlayerCameraX(playerID, (this.getPlayerCamX(playerID) + 1));
            }
        }
        if (dir == 'up') {
            if (this.getPlayerCamY(playerID) > 0) {
                this.setPlayerCameraY(playerID, (this.getPlayerCamY(playerID) - 1));
            }
        }
        if (dir == 'down') {
            if (this.getPlayerCamY(playerID) < this.mapY - this.tileSize) {
                this.setPlayerCameraY(playerID, (this.getPlayerCamY(playerID) + 1));
            }
        }
        this.updateWorldDisplay();
    }
    mapZoom(zoom) {
        if (zoom == 'in' && this.zoomlevel > 0) {
            this.zoomlevel--;
            this.tileSize += this.zoompx;
            this.windowX = Math.floor(window.innerWidth / this.tileSize) + 1,
                this.windowY = Math.floor(window.innerHeight / this.tileSize);
            return this.updateWorldDisplay();
        }
        else if (zoom == 'out' && this.zoomlevel < this.maxzoomout) {
            this.zoomlevel++;
            this.tileSize -= this.zoompx;
            this.windowX = Math.floor(window.innerWidth / this.tileSize) + 1,
                this.windowY = Math.floor(window.innerHeight / this.tileSize);
            return this.updateWorldDisplay();
        }
    }
    getPlayerClickedTileInfo(playerID = 0, ex, ey) {
        let cx = 0, cy = 0;
        cx = Math.floor(ex / this.tileSize);
        cy = Math.floor(ey / this.tileSize);
        cx = cx + this.getPlayerCamX(playerID);
        cy = cy + this.getPlayerCamY(playerID);
        this.clickedTileInfo(cx, cy);
    }
    runTimers() {
        this.tileswitchtimer = setInterval(() => { this.TileSwitcher(0); }, 500);
    }
    destroyTimer() {
        clearInterval(this.tileswitchtimer);
    }
}
//# sourceMappingURL=game.js.map