Skip to content

Commit

Permalink
added scatter, frightened, chase functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
satls committed Aug 3, 2013
1 parent c05d7ae commit 9bbbb5b
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 99 deletions.
99 changes: 53 additions & 46 deletions Game.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,64 +12,71 @@ function Game(w,h,drawPane){
var clyde;
var map = "wwwwwwwwwwwwwwwwwwwwwwwwwwww w************ww************w w*wwww*wwwww*ww*wwww*wwwww*w w*wwww*wwwww*ww*wwww*wwwww*w w*wwww*wwwww*ww*wwww*wwwww*w w**************************w w*wwww*ww*wwwwwwww*ww*wwww*w w*wwww*ww*wwwwwwww*ww*wwww*w w******ww****ww****ww******w wwwwww*wwwwwswwswwwww*wwwwww wwwwww*wwwwwswwswwwww*wwwwww wwwwww*wwssssssssssww*wwwwww wwwwww*wwswwwwwwwwsww*wwwwww wwwwww*wwswwwwwwwwsww*wwwwww sssssw*ssswwwwwwwwsss*wsssss wwwwww*wwswwwwwwwwsww*wwwwww wwwwww*wwswwwwwwwwsww*wwwwww wwwwww*wwssssssssssww*wwwwww wwwwww*wwswwwwwwwwsww*wwwwww wwwwww*wwswwwwwwwwsww*wwwwww w************ww************w w*wwwww*wwww*ww*wwwww*wwww*w w*wwwww*wwww*ww*wwwww*wwww*w w***ww*******ss*******ww***w www*ww*ww*wwwwwwww*ww*ww*www www*ww*ww*wwwwwwww*ww*ww*www w******ww****ww****ww******w w*wwwwwwwwww*ww*wwwwwwwwww*w w*wwwwwwwwww*ww*wwwwwwwwww*w w**************************w wwwwwwwwwwwwwwwwwwwwwwwwwwwwe";

var processing =true;
var i = 0;
var j = 0;
var ix = 0;
cells[0]=new Array();
while(processing){
if(map[i]=='w'){
cells[j][ix]=new Cell(false, false, true);
fillMap();

//fill out the map
function fillMap(){
var processing =true;
var i = 0;
var j = 0;
var ix = 0;
cells[0]=new Array();
while(processing){
if(map[i]=='w'){
cells[j][ix]=new Cell(false, false, true);
}
if(map[i]=='*'){
cells[j][ix]=new Cell(true, true, false); // a passable cell with a nibble
}
if(map[i]=='s'){
cells[j][ix]=new Cell(true, false, false); // a passable cell with a nibble
}
if(map[i]==' '){
cells[j+1]=new Array();
j++;
ix=-1;

}
if(map[i]=='e'){
processing=false;
}
i++;
ix++;
}
if(map[i]=='*'){
cells[j][ix]=new Cell(true, true, false); // a passable cell with a nibble
}

this.scatterGhosts = function(){
for(var i = 0; i < ghosts.length; i++){
ghosts[i].scatter();
}
if(map[i]=='s'){
cells[j][ix]=new Cell(true, false, false); // a passable cell with a nibble
}
this.chaseGhosts = function(){
for(var i = 0; i < ghosts.length; i++){
ghosts[i].chase();
}
if(map[i]==' '){
cells[j+1]=new Array();
j++;
ix=-1;

}
this.frightenGhosts = function(){
for(var i = 0; i < ghosts.length; i++){
ghosts[i].frighten();
}
if(map[i]=='e'){
processing=false;
}
this.unfrightenGhosts = function(){
for(var i = 0; i < ghosts.length; i++){
ghosts[i].unfrighten();
}
//alert(map[i]);
i++;
ix++;
}
//remember kids: create your 2d arrays for the order you want to address them!
// for(var y = 0; y < height; y++){
// cells[y]=new Array();
// for(var x = 0; x < width; x++){
// cells[y][x]=new Cell(true, true, false); // a passable cell with a nibble
// // if(y%2==1){
// // cells[y][x]=new Cell(true, true, false); // a passable cell with a nibble
// // }else if(x%2==1){
// // cells[y][x]=new Cell(true, true, false); // a passable cell with a nibble
// // }else{
// // cells[y][x]=new Cell(false, false, true); // a passable cell with a nibble
// // }
// if(x==0 || x == width-1){
// cells[y][x]=new Cell(false, false, true); // a wall
// }
// if(y==0 || y == height-1){
// cells[y][x]=new Cell(false, false, true); // a wall
// }
// }
// }
}

//ghost getter functions for targeting and logic
this.getBlinky = function(){//for inkies targeting
return blinky;
}
this.getPinky = function(){//for inkies targeting
this.getPinky = function(){
return pinky;
}
this.getInky = function(){//for inkies targeting
this.getInky = function(){
return inky;
}
this.getClyde = function(){//for inkies targeting
this.getClyde = function(){
return clyde;
}
this.getWidth = function(){
Expand Down
18 changes: 17 additions & 1 deletion PacMan.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
setInterval(function() {
board.update();
board.draw();
}, 500)
}, 100)
});
function addBlinky(){
board.getBlinky().setPos(14,11);
Expand All @@ -56,6 +56,18 @@
board.getClyde().setPos(14,11);
//board.getClyde().setPos(10,29);
}
function scatter(){
board.scatterGhosts();
}
function chase(){
board.chaseGhosts();
}
function frighten(){
board.frightenGhosts();
}
function unfrighten(){
board.unfrightenGhosts();
}
</script>

</head>
Expand All @@ -68,6 +80,10 @@
<input type=submit value='pinky' onclick='addPinky()'></input>
<input type=submit value='inky' onclick='addInky()'></input>
<input type=submit value='clyde' onclick='addClyde()'></input>
<input type=submit value='scatter' onclick='scatter()'></input>
<input type=submit value='chase' onclick='chase()'></input>
<input type=submit value='frighten' onclick='frighten()'></input>
<input type=submit value='unfrighten' onclick='unfrighten()'></input>
</div>
<div id='clydeLog'></div>
</body>
Expand Down
157 changes: 105 additions & 52 deletions ghost.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,30 @@ function Ghost(startX, startY, col, b){
var graveYardY = 0;

var chase=false;
var dead=false;
var frightened=false;

var targetX;
var targetY;


this.scatter = function(){
chase = false;
vX = -vX;
vY = -vY;
}
this.chase = function(){
chase = true;
vX = -vX;
vY = -vY;
}

this.frighten = function(){
frightened=true;
}
this.unfrighten = function(){
frightened=false;
}

this.setHome=function(x,y){
homeX = x;
homeY = y;
Expand Down Expand Up @@ -56,72 +75,105 @@ function Ghost(startX, startY, col, b){
this.update=function(){

//update target cell

//blinky: target pacman
//if(color == '#FF0000'){
//this.setTarget(pacMan.getX(),pacMan.getY());


this.getTargets(board);
//targetX = pacMan.getX();
//targetY = pacMan.getY();
//}

if(chase){
targetX = homeX;
targetY = homeY;
}
//set next direction
//check all four directions
//cannot move backwards ie next v != -v

var bestVX = 0;
var bestVY = 0;
var bestD = 99999;


for(var i = -1; i < 2; i++){//check left and right
if(i!= -vX && i!=0 && board.canWalk(posX+i, posY)){
var thisD = Math.sqrt( Math.pow(targetX - (posX+i),2) + Math.pow((targetY - posY),2));

if(thisD<bestD) {
bestD = thisD;
bestVX = i;
bestVY = 0;
if(frightened){
//randomly choose a square that is not backwards
var choices = new Array();
for(var i = -1; i<2; i++){
//check left right
if(board.canWalk(posX+i, posY) && vX!=-i && i!=0){
if(i==-1){
choices.push(function(){
vX = -1;
vY = 0;
});
}
if(i==1){
choices.push(function(){
vX = 1;
vY = 0;
});
}
}
}
}
for(var i = -1; i<2; i++){
//check up down
if(board.canWalk(posX, posY+i) && vY!=-i && i!=0){
if(i==-1){
choices.push(function(){
vX = 0;
vY = -1;
});
}
if(i==1){
choices.push(function(){
vX = 0;
vY = 1;
});
}
}
}
if(choices.length>0){
choices[Math.floor(Math.random()*choices.length)]();
}
}else{

for(var i = -1; i < 2; i++){//check up and down
if(i!= -vY && i!=0 &&board.canWalk(posX, posY+i)){
//alert(i);
var thisD = Math.sqrt( Math.pow((targetX - posX),2) + Math.pow(targetY - (posY +i),2));
if(thisD<bestD) {
bestD = thisD;
bestVX = 0;
bestVY = i;
if(!chase){
targetX = homeX;
targetY = homeY;
}else{
this.getTargets(board);
}
//set next direction
//check all four directions
//cannot move backwards ie next v != -v
var bestVX = 0;
var bestVY = 0;
var bestD = 99999;


for(var i = -1; i < 2; i++){//check left and right
if(i!= -vX && i!=0 && board.canWalk(posX+i, posY)){
var thisD = Math.sqrt( Math.pow(targetX - (posX+i),2) + Math.pow((targetY - posY),2));

if(thisD<bestD) {
bestD = thisD;
bestVX = i;
bestVY = 0;
}
}
}
}


if(bestVX!=0){
vX=bestVX;
vY=0;
}
if(bestVY!=0){
vX=0;
vY=bestVY;
for(var i = -1; i < 2; i++){//check up and down
if(i!= -vY && i!=0 &&board.canWalk(posX, posY+i)){
var thisD = Math.sqrt( Math.pow((targetX - posX),2) + Math.pow(targetY - (posY +i),2));
if(thisD<bestD) {
bestD = thisD;
bestVX = 0;
bestVY = i;
}
}
}


if(bestVX!=0){
vX=bestVX;
vY=0;
}
if(bestVY!=0){
vX=0;
vY=bestVY;
}

}
//alert(bestVX + " " +vX+ " " + bestVY + " " + vY + " best d" + bestD);
//vX=bestVX;
//vY=bestVY;

//update pos
if(board.canWalk(posX+vX, posY+vY)){
posX+=vX;
posY+=vY;
}

}

//ghosts can turn left or right but not reverse without a special call.
Expand Down Expand Up @@ -197,3 +249,4 @@ function Clyde(startX, startY, b){




0 comments on commit 9bbbb5b

Please sign in to comment.