Online Book Reader

Home Category

HTML5 Canvas [197]

By Root 6516 0

var enemyLocationFound = false;

while(!enemyLocationFound){

randRow = Math.floor(Math.random()*15);

randCol = Math.floor(Math.random()*15);

if (playField[randRow][randCol]==roadTile){

enemyLocationFound = true;

var tempEnemy = {};

tempEnemy.row = randRow;

tempEnemy.col = randCol;

tempEnemy.nextRow = 0;

tempEnemy.nextCol = 0;

tempEnemy.currentTile = 0;

tempEnemy.rotation = 0;

tempEnemy.x = tempEnemy.col*32;

tempEnemy.y = tempEnemy.row*32;

tempEnemy.speed = 2;

tempEnemy.destinationX = 0;

tempEnemy.destinationY = 0;

tempEnemy.dx = 0;

tempEnemy.dy = 0;

tempEnemy.hit = false;

tempEnemy.dead = false;

tempEnemy.moveComplete = false;

enemy.push(tempEnemy);

items[randRow][randCol] = 1;

}

}

}

//place player

var playerLocationFound = false;

while(!playerLocationFound){

randRow = Math.floor(Math.random()*15);

randCol = Math.floor(Math.random()*15);

if (playField[randRow][randCol]==roadTile &&

items[randRow][randCol]==0){

playerLocationFound = true;

player.col = randCol;

player.row = randRow;

player.x = player.col*32;

player.y = player.row*32;

items[randRow][randCol] = 1;

}

}

//place goal

var goalLocationFound = false;

while(!goalLocationFound){

randRow = Math.floor(Math.random()*15);

randCol = Math.floor(Math.random()*15);

if (playField[randRow][randCol]==roadTile &&

items[randRow][randCol]==0){

playField[randRow][randCol] = goalTile;

goalLocationFound = true;

}

}

//console.log("playField=" + playField);

}

function resetPlayer(){

player.row = 0;

player.col = 0;

player.nextRow = 0;

player.nextCol = 0;

player.currentTile = 0;

player.rotation = 0;

player.speed = 2;

player.destinationX = 0;

player.destinationY = 0;

player.x = 0;

player.y = 0;

player.dx = 0;

player.dy = 0;

player.hit = false;

player.dead = false;

player.win = false;

}

function gameStateWaitForPlayerMove() {

if (keyPressList[38]==true){

//up

if (checkBounds(-1,0, player)){

setPlayerDestination();

}

}else if (keyPressList[37]==true) {

//left

if (checkBounds(0,-1, player)){

setPlayerDestination();

}

}else if (keyPressList[39]==true) {

//right

if (checkBounds(0,1, player)){

setPlayerDestination();

}

}else if (keyPressList[40]==true){

//down

if (checkBounds(1,0, player)){

setPlayerDestination();

}

}

}

function setPlayerDestination(){

player.destinationX = player.nextCol*32;

player.destinationY = player.nextRow*32;

switchGameState(GAME_STATE_ANIMATE_PLAYER);

}

function checkBounds(rowInc, colInc, object){

object.nextRow = object.row+rowInc;

object.nextCol = object.col+colInc;

if (object.nextCol >=0 && object.nextCol<15 &&

object.nextRow>=0 && object.nextRow<15){

object.dx = colInc;

object.dy = rowInc;

if (colInc==1){

object.rotation = 90;

}else if (colInc==-1){

object.rotation = 270;

}else if (rowInc==-1){

object.rotation = 0;

}else if (rowInc==1){

object.rotation = 180;

}

return(true);

}else{

object.nextRow = object.row;

object.nextCol = object.col;

return(false);

}

}

function gameStateAnimatePlayer(){

player.x += player.dx*player.speed;

player.y += player.dy*player.speed;

player.currentTile++;

if (player.currentTile==playerTiles.length){

player.currentTile = 0;

}

renderPlayField();

if (player.x==player.destinationX && player.y==player.destinationY){

switchGameState(GAME_STATE_EVALUATE_PLAYER_MOVE);

}

}

function gameStateEvaluatePlayerMove(){

player.row = player.nextRow;

player.col = player.nextCol;

if (playField[player.row][player.col]==wallTile){

player.hit = true;

}else if (playField[player.row][player.col]==goalTile){

player.win = true;

}

for (var eCtr=enemy.length-1;eCtr>=0;eCtr--){

if (player.row==enemy[eCtr].row && player.col==enemy[eCtr].col){

enemy[eCtr].hit = true;

player.hit = true;

}

}

switchGameState(GAME_STATE_ENEMY_MOVE);

}

function gameStateEnemyMove(){

for (var eCtr=0;eCtrvar tempEnemy = enemy[eCtr];

if (!tempEnemy.hit){

var directionsToTest=[];

var hDiff = tempEnemy.col - player.col;

var vDiff = tempEnemy.row - player.row;

if (Math.abs(vDiff)

Return Main Page Previous Page Next Page

®Online Book Reader