myMouseUp: function () {
var mouseX = me.game.viewport.pos.x + me.input.mouse.pos.x;
var mouseY = me.game.viewport.pos.y + me.input.mouse.pos.y;
var background = me.game.currentLevel.getLayerByName("background"); //Background always have a tile everywhere on the map
var gameCollisionLayer = me.game.currentLevel.getLayerByName("collision");
var gameCollisionMap = new Array();
var player = me.game.getEntityByName("mainPlayer");
var playerX = player[0].collisionBox.pos.x;
var playerY = player[0].collisionBox.pos.y;
var playerTile = background.getTile(playerX, playerY);
var clickedTile = background.getTile(mouseX, mouseY);
//Create an 2d array with all the tiles as 0 or 1 for collision
for (var h = 0; h < gameCollisionLayer.rows; h++) {
gameCollisionMap[h] = new Array();
for (var i = 0; i < gameCollisionLayer.cols; i++) {
if (jQuery.type(gameCollisionLayer.layerData[i][h]) != "null") {
gameCollisionMap[h].push(1);
}
else {
gameCollisionMap[h].push(0);
}
}
}
// start and end of path
var pathStart = [playerTile.row, playerTile.col];
var pathEnd = [clickedTile.row, clickedTile.col];
// use pathfinding to get an array with the fasted non blocked route
var currentPath = findPath(gameCollisionMap, pathStart, pathEnd);
//Should move player to next tile by iterating over the tiles in the path array
for (var im = 0; im < currentPath.length; im++) {
var prevTile;
if (im == 0) {
prevTile = currentPath[im];
}
var currentTile = currentPath[im];
var moveY = currentTile[1] - prevTile[1];
var moveX = currentTile[0] - prevTile[0];
if (moveX > 0) { //left
player[0].renderable.setCurrentAnimation('left');
player[0].direction = 'left';
player[0].vel.x -= 32;
player[0].updateMovement();
} else
if (moveX < 0) {
player[0].vel.x += 32; //right
player[0].renderable.setCurrentAnimation('right');
player[0].direction = 'right';
player[0].updateMovement();
} else
if (moveY < 0) { //down
player[0].vel.y -= 32;
player[0].renderable.setCurrentAnimation('down');
player[0].direction = 'down';
player[0].updateMovement();
} else
if (moveY > 0) { //up
player[0].vel.y += 32;
player[0].renderable.setCurrentAnimation('up');
player[0].direction = 'up';
player[0].updateMovement();
}
prevTile = currentPath[im];
}
player[0].vel.y = 0;
player[0].vel.x = 0;
player[0].renderable.setCurrentAnimation('down');
player[0].direction = 'down';
player[0].updateMovement();
}
// Start state machine ... do this on click this.step = 1; this.index = 1;
// Initialize ... do this in the object.init() method this.step = 0; this.index = 0;
// State machine logic ... do this in the object.update() method switch (this.step) { case 1: // Get destination from A* // Expects an object with `x` and `y` properties expressed in pixels this.dest = this.astar[this.index++]; // Determine direction and set velocity if (this.dest.x != this.pos.x) { // Direction is left or right this.velocity = { x : (this.dest.x - this.pos.x).clamp(-1, 1) * this.accel.x, y : 0 }; this.dir = this.velocity.x < 0 ? "left" : "right"; } else { // Direction is up or down this.velocity = { x : 0, y : (this.dest.y - this.pos.y).clamp(-1, 1) * this.accel.y }; this.dir = this.velocity.y < 0 ? "up" : "down"; } // Set animation this.renderable.setCurrentAnimation("walk_" + this.dir); // Advance to next state this.step++; // FALL THROUGH! case 2: // Set actual velocity this.vel.copy(this.velocity); this.vel.scale(new me.Vector2d(me.timer.tick, me.timer.tick)); this.updateMovement(); // Advance to next step in state machine function nextStep() { // To state 0 if there is more A*, else state 0 (do nothing) this.step = (this.index < this.astar.length) ? 1 : 0; // Force position to destination this.pos.copy(this.dest); } // Determine if object has reached its destination switch (this.dir) { case "up": if (this.pos.y <= this.dest.y) nextStep(); break; case "down": if (this.pos.y >= this.dest.y) nextStep(); break; case "left": if (this.pos.x <= this.dest.x) nextStep(); break; case "right": if (this.pos.x >= this.dest.x) nextStep(); break; } break; }
That's a feature of melonjs, as If no object are returning true, the engine wil, consider that there is nothing to update and won't redraw the screen (that helps sometimes in saving a few fps here and there),
--
You received this message because you are subscribed to a topic in the Google Groups "melonJS - A lightweight HTML5 game engine" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/melonjs/5BbT7QhVtKI/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to melonjs+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
// keep track of the world dimensions
// Note that this A-star implementation expects the world array to be square:
// it must have equal height and width. If your game world is rectangular,
// just fill the array with dummy values to pad the empty space.
var worldWidth = world[0].length;
var worldHeight = world.length;
var worldSize = worldWidth * worldHeight;
// keep track of the world dimensions
// Note that this A-star implementation expects the world array to be square:
// it must have equal height and width. If your game world is rectangular,
var worldWidth = world[0].length;
var worldHeight = world.length;
// just fill the array with dummy values to pad the empty space.
if (worldHeight != worldWidth) {
if(worldWidth < worldHeight) {
//Create blocked cols foreach row that is missing to create same width as height
for (var wh = 0; wh <= worldHeight; wh++)
{
for (var whc = 0; whc < worldHeight - worldWidth; whc++) {
world[wh].push(1);
}
}
} else {
//Create blocked rows that are missing to make the array equal square in h and w
for (var ww = worldHeight++; ww > worldWidth; ww++) {
world.push(ww);
for(var wwc = 0; wwc < worldWidth;wwc++) {
world[ww].push(1);
}
}
}
}
for (var ww = worldHeight; ww < worldWidth; ww++) {world.push(new Array());for (var wwc = 0; wwc < worldWidth; wwc++) {world[ww].push(1);}}