parsing tiled json map into html canvas

225 views Asked by At

I am trying to make a 2d top view javascript game and i want to make my map using program "tiled" i have made my map and have the exported json but dont know how to parse it into a canvas html tag and i dont know were to start here is my crazy game code

<html>

<body>
</body>

</html>
<script>
// Create the canvas
var canvas = document.createElement("canvas");
var context = canvas.getContext("2d");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
document.body.appendChild(canvas);

// Background image
var bgReady = false;
var bgImage = new Image();
bgImage.onload = function() {
    bgReady = true;
};
bgImage.src = "images/newbackground.png";
//hp box
var hpBoxReady = false;
var hpBoxImage = new Image();
hpBoxImage.onload = function() {
    hpBoxReady = true;
};
hpBoxImage.src = "images/hpbox.png";
// player image
var playerReady = false;
var playerImage = new Image();
playerImage.onload = function() {
    playerReady = true;
};
playerImage.src = "images/char.png";

// enemy image
var enemyReady = false;
var enemyImage = new Image();
enemyImage.onload = function() {
    enemyReady = true;
};
enemyImage.src = "images/enemy_idle01.png";

// Game objects
var hpBox = {
    restoreHealth: 34,
    x: 300,
    y: 300
}
var player = {
    stamina: 7,
    health: 100,
    sprintSpeed: 400,
    weakSpeed: 150,
    speed: 300 // movement in pixels per second
};
var enemy = {
    speed: 250,
    viewDistance: 40
};
var enemysCaught = 0;

// Handle keyboard controls
var keysDown = {};
addEventListener("keydown", function(e) {
    keysDown[e.keyCode] = true;
}, false);

addEventListener("keyup", function(e) {
    delete keysDown[e.keyCode];
}, false);

// Reset the game when the player catches a enemy
var reset = function() {
    player.x = canvas.width / 2;
    player.y = canvas.height / 2;

    // Throw the enemy somewhere on the screen randomly
    enemy.x = 32 + (Math.random() * (canvas.width - 64));
    enemy.y = 32 + (Math.random() * (canvas.height - 64));
};



//w is 87
//a is 65
//s is 83
//d is 68
// Update game objects
var update = function(modifier) {
    if (87 in keysDown) { // Player holding up
        player.y -= player.speed * modifier;
    }
    if (83 in keysDown) { // Player holding down
        player.y += player.speed * modifier;
    }
    if (65 in keysDown) { // Player holding left
        player.x -= player.speed * modifier;
    }
    if (68 in keysDown) { // Player holding right
        player.x += player.speed * modifier;
    }

    if (
        player.x <= (0)) {
        player.health -= 1;
        console.log('health decreasing');
    }
}
if (
    player.y <= (0)) {

    player.health -= 1;
    console.log('health decreasing');
};

// Are they touching?
if (
    player.x <= (enemy.x + 32) &&
    enemy.x <= (player.x + 32) &&
    player.y <= (enemy.y + 32) &&
    enemy.y <= (player.y + 32)
) {
    ++enemysCaught;
    reset();
}

// Draw everything
var render = function() {
    if (bgReady) {
        context.drawImage(bgImage, 0, 0);
    }
    if (hpBoxReady) {
        context.drawImage(hpBoxImage, hpBox.x, hpBox.y);
    }
    if (playerReady) {
        context.drawImage(playerImage, player.x, player.y);
    }

    if (enemyReady) {
        context.drawImage(enemyImage, enemy.x, enemy.y);
    }

    // Score
    context.fillStyle = "rgb(250, 250, 250)";
    context.font = "24px Helvetica";
    context.textAlign = "left";
    context.textBaseline = "top";
    context.fillText("enemys killed: " + enemysCaught, canvas.width / 2, 32);
};

function dieEvent() {
    player.health = 100;
}

function updateHealth() {
    context.fillStyle = "rgb(255 ,0 ,0)";
    context.textAlign = "right";
    context.fillText("Health: " + player.health, 1000, 32)

}

function isNearHPBox() {

    if (
        player.y <= (hpBox.y + enemy.viewDistance + 64) &&
        player.y >= (hpBox.y - enemy.viewDistance - 64) &&
        player.x <= (hpBox.x + enemy.viewDistance + 64) &&
        player.x >= (hpBox.x - enemy.viewDistance - 64)) {
        console.log("healing!");
        if (player.health <= 100) {
            hpBox.restoreHealth = player.health - 100;
            player.health += hpBox.restoreHealth;
        }

    }

}

function moveEnemy() {
    if (
        player.y <= (enemy.y + enemy.viewDistance + 64) &&
        player.y >= (enemy.y - enemy.viewDistance - 64) &&
        player.x <= (enemy.x + enemy.viewDistance + 64) &&
        player.x >= (enemy.x - enemy.viewDistance - 64)) {
        console.log("seen on enemys Y");
        var audio = new Audio('sounds/theWanderer_Scream.m4a');
        audio.play();
        if (player.x >= (enemy.x)) {
            enemy.x -= enemy.speed;
        }
        if (player.x >= (enemy.x)) {
            enemy.x -= enemy.speed;
        }
    }
}

function checkWallCollision() {
    if (player.y <= 0) {
        console.log("y")
        player.y += 64;
    }
    if (player.x <= 0) {
        console.log("x")
        player.x += 64;
    }
    if (enemy.y <= 0) {
        console.log("y")
        enemy.y += 64;
    }
    if (enemy.x <= 0) {
        console.log("x")
        enemy.x += 64;
    }
}

function reducedSpeed() {
    player.speed = player.weakSpeed;
}
// The main game loop
var main = function() {
    var now = Date.now();
    var delta = now - then;
    update(delta / 1000);
    context.clearRect(0, 0, canvas.width, canvas.height);
    render();
    updateHealth();
    moveEnemy();
    if (player.health <= 20) {
        reducedSpeed();
    } else {
        player.speed = 300;
    }
    if (player.health <= 0) {
        dieEvent();
    }
    checkWallCollision();
    isNearHPBox();
    then = now;

    // Request to do this again ASAP
    requestAnimationFrame(main);
};

// Cross-browser support for requestAnimationFrame
var w = window;
requestAnimationFrame = w.requestAnimationFrame || w.webkitRequestAnimationFrame || w.msRequestAnimationFrame || w.mozRequestAnimationFrame;

// Let's play this game!
var then = Date.now();
reset();
main();
</script>
<style>
body {
    margin: 0;
    padding: 0;
}
</style>

i have tried all the tutorials but cant seem to get it to work so for now i just have a small little background image could sombody please help?

1

There are 1 answers

2
mece1390 On

Your question is a bit general. I recommand to enroll Udacity's HTML5 Game Development course I think many part of your question should be answered after watching that course.