How does the RGG card extend when the player reaches the limit? Javascript HTML5

advertisements

Im struggling to find an answer for my question on google, so thought id post.

Im building a top-down tile-based rpg game using html5, javascript and jquery only.

I have set up images in an array. I have set up my map in an array and just looping through it to show the tiles on screen.

I want a 750px x 600px section of a large map to show on the canvas and when the player moves to one of the 4 boundaries the map will update and show the next part of the map or if no map is available stay as it is.

How would i implement this??

If you dont understand what im asking ill try to explain better :p but heres my code:

$(document).ready(function(){

var canvas = $("#TBG");
var context = canvas.get(0).getContext("2d");

var canvasWidth = canvas.width();
var canvasHeight = canvas.height();

//Player Controls -------------------------------------------------------------------------
$(window).keydown(function(e){
   var keyCode = e.keyCode;
    if (keyCode == arrowRight){
        Player.moveRight = true;
    }
    else if (keyCode == arrowLeft){
        Player.moveLeft = true;
    }
    else if (keyCode == arrowUp){
        Player.moveUp = true;
    }
    else if (keyCode == arrowDown) {
        Player.moveDown = true;
    }
});
$(window).keyup(function(e){
    var keyCode = e.keyCode;
    if (keyCode == arrowRight){
        Player.moveRight = false;
    }
    else if (keyCode == arrowLeft){
        Player.moveLeft = false;
    }
    else if (keyCode == arrowUp){
        Player.moveUp = false;
    }
    else if (keyCode == arrowDown) {
        Player.moveDown = false;
    }
});

function player() {

    this.x = 100;
    this.y = 100;
    this.vX = 0;
    this.vY = 0;

    var moveRight = false;
    var moveLeft = false;
    var moveUp = false;
    var moveDown = false;
}

var Player = new player();

var arrowLeft = 37;
var arrowUp = 38;
var arrowRight = 39;
var arrowDown = 40;

//End Player controls ------------------------------------------------------------------------------

//Map initialisation -------------------------------------------------------------------------------------

var images = new Array(3);

images[0] = new Image();
images[0].src = "images/player.png";

images[1] = new Image();
images[1].src = "images/wall.png";

images[2] = new Image();
images[2].src = "images/grass.png";

images[3] = new Image();
images[3].src = "images/floor.png";

var tileMap = [
    [3,1,3,3,3,3,3,3,3,3],
    [3,1,1,2,2,2,2,2,2,3],
    [3,1,1,1,2,2,2,2,2,3],
    [3,2,1,1,1,2,2,2,2,3],
    [3,2,2,1,1,1,2,2,2,3],
    [3,2,2,2,1,1,1,1,1,3],
    [3,2,2,2,2,1,1,1,1,3],
    [3,2,2,2,2,1,1,1,1,3],
    [3,2,2,2,2,1,1,1,1,3],
    [3,3,3,3,3,3,3,3,3,3]
];

//Animate Loop -------------------------------------------------------------------------------------------
function animate () {

    context.clearRect(0,0,canvasWidth,canvasHeight);

    Player.vX = 0;
    Player.vY = 0;

    //Player Animation
    if (Player.moveRight) {
        Player.vX =2;
    };
    if (Player.moveUp) {
        Player.vY = -2;
    };
    if (Player.moveDown) {
        Player.vY = 2;
    };
    if (Player.moveLeft) {
        Player.vX = -2;
    };

    Player.x += Player.vX;
    Player.y += Player.vY;

    if (Player.x < 0){
        Player.x = 0;
        Player.vX *= -2;
    }
    else if (Player.x + 64 > canvasWidth) {
        Player.x = canvasWidth - 64;
        Player.vX *= -2;
    };
    if (Player.y < 0){
        Player.y = 0;
        Player.vY *= -2;
    }
    else if (Player.y + 64 > canvasHeight) {
        Player.y = canvasWidth - 64;
        Player.vY *= -2;
    };

    for (var i = 0; i < 10; i++) {
        for (var j = 0; j < 10; j++){

            var tileId = tileMap[i][j];
            var tileWidth = 32;
            var tileHeight = 32;

            context.drawImage(images[tileId],i * tileWidth, j * tileHeight);
        }

    }
    context.drawImage(images[0], Player.x, Player.y);

    setTimeout(animate, 0);
}
// End Loop ------------------------------------------------------------------------------------------------

animate();

});

Im not great with Javascript but learning and know a comfortable amount about programming.

thanks in advance Tom


Well there isn't really a particular way to do it(like anything in activity development), but perhaps I could provide some assistance as I'm creating a floor centered activity too. There isn't really anything unique to floor layers; it's just a phrase given to charts that can be padded on top of each other. You could even have one part in your whole activity, but you can see how that wouldn't perform out. If a there was a 5X5 position of lawn and you desired to put a shrub on it, then in between the divisions you would just see whatever color the qualifications is because you changed the lawn floor that was there before. Some individuals deal with this by including another part to position the shrub on. That way the first part has lawn on it, and the shrub looks like it's on top. You might have to use several levels for several plants that are the actual to get the impact you want though. Individually, I'm only having probably one floor part in my activity. This will be for primary scenery things lawn, dust, etc. Then I will have an item part where I will have pre-made objects(which can be plants, blossoms, bad individuals, homes, etc.) and my motor will take their roles and figure out how to sketch them.

For beginners, you could get your personality to conflict with the sides of the display. For example, to conflict with the remaining part of the display, you would examine if the performer's x position was less than 0, and if so, set it to 0.

Next you'll want to be able to sketch flooring on the display. You could signify a floor map using an range of integers. Different figures would signify different floor designs that you would have to sketch. So a 3X3 range of '0's would be a map that symbolizes a 3X3 lines of lawn flooring while '1's might be water or something. You could these figures from a text/binary computer file or an XML computer file or something. Since you were thinking about XML, it's generally just a human-readable way to shop details. XML details don't really do anything(in a sense), it's up to your system to understand it.

One more recommendation is that since you are using SFML, you can use "views" to shift the photographic camera around. Look into the SFML web page for how to use them.

Now for my best recommendation. My publish is relatively unexplained, but everything I described is protected in Chip Gravelyn's floor motor guides. It's published in C# and he does a lot more things developing a floor manager, but you can still implement his things to your C++/SFML activity. Look up "NIckGravelyn" online. com.