Added initial kinds of valid path

This commit is contained in:
Rob Pearce 2016-08-18 16:18:35 +10:00
parent 08b65688ca
commit a73ff6ce6d
2 changed files with 149 additions and 43 deletions

189
cat.html
View File

@ -17,6 +17,7 @@ var things = [];
var myScore;
var curpath = [];
var pathvalid = false;
var MAXDIRS = 4;
var DIRXMOD = [ 0, 1, 0, -1 ];
@ -52,6 +53,26 @@ function getthingxy(x, y) {
return null;
}
function clearpath() {
curpath = [];
pathvalid = false;
}
function addtopath(what) {
console.log("addpath() " + what.name);
dumppath("addpath pre: ", curpath);
curpath.push(what);
pathvalid = true;
if (curpath.length == 1) {
console.log("Starting path with " + what.name);
} else {
dumppath("Cur path is: ",curpath);
}
}
function pathcontains(what) {
var i;
if (curpath == undefined) return false;
@ -64,6 +85,54 @@ function pathcontains(what) {
return false;
}
function isvalidpath(mypath) {
var valid = true;
for (i = 0; i < mypath.length - 1; i++) {
var thisone,nextone;
thisone = mypath[i];
/*
if (i == mypath.length - 1) { // last one
nextone = null;
} else {
nextone = mypath[i+1];
}
*/
nextone = mypath[i+1];
if ((thisone.type == "cat") && nextone.type == "cat") {
// lines of cats are ok
} else if ((i == 0) && (thisone.type == "cat") && (nextone.type == "food")) {
// first cat -> food is ok
} else {
// not ok
valid = false;
break;
}
}
//var pr = "isvalidpath()? " + valid + " ";
//dumppath(pr, mypath);
return valid;
}
// would adding 'overthing' to our existing path result in a valid path?
function canextendpath(overthing) {
if (!overthing) return false;
if (overthing &&
isadjacent(overthing, curpath[curpath.length-1]) && // adjacent to last thing in path?
!pathcontains(overthing) ) { // path doesn't already contain this?
// create a fake new path containing this.
var fakepath = curpath.slice();
fakepath.push(overthing);
if (isvalidpath(fakepath)) {
return true;
}
}
return false;
}
function isadjacent(thing1, thing2) {
// is thing1 adjacent to thing2?
var newgridx,newgridy;
@ -82,28 +151,22 @@ function isadjacent(thing1, thing2) {
function startGame() {
var x,y;
/*
myGamePiece = new thing(30, 30, "red", 10, 120);
myGamePiece.gravity = 0.05;
myScore = new thing("30px", "Consolas", "black", 280, 40, "text");
*/
for (y = 0; y < GRIDH; y++) {
for (x = 0; x < GRIDW; x++) {
things.push(new thing(x, y, getrandomname()));
things.push(new thing(x, y, "random"));
}
}
myGameArea.start();
}
function dumppath() {
function dumppath(prefix,arr) {
var str;
str = "";
for (i = 0; i < curpath.length; i++) {
str = str + " " + curpath[i].name;
for (i = 0; i < arr.length; i++) {
str = str + " " + arr[i].name;
}
console.log("Cur path is: " + str);
console.log(prefix + str);
}
function thingsmoving() {
@ -164,14 +227,14 @@ var myGameArea = {
// make sure nothing is moving
if (thingsmoving()) return;
// clear current path
curpath = [];
// clear existing path
clearpath();
// did you click on an object?
var onthing = getthingxy(event.pageX,event.pageY);
if (onthing) {
curpath.push(onthing);
console.log("Starting path: " + onthing.name);
console.log("Initial click on " + onthing.name);
addtopath(onthing);
}
},
@ -182,27 +245,49 @@ var myGameArea = {
// existing path?
if (curpath != undefined && curpath.length > 0) {
var overthing = getthingxy(event.pageX,event.pageY);
var lastinpath;
if (curpath == undefined) {
lastinpath = null;
} else {
lastinpath = curpath[curpath.length-1];
}
// is overthing adjacent to the last thing in the path?
if (overthing && isadjacent(overthing, curpath[curpath.length-1])) {
if (canextendpath(overthing)) {
// add it to the path
curpath.push(overthing);
console.log("Adding to path: " + overthing.name);
dumppath();
addtopath(overthing);
} else if (pathcontains(overthing)) {
pathvalid = true;
} else {
pathvalid = false;
}
}
},
handlemouseup : function(event) {
// make sure nothing is moving
if (thingsmoving()) return;
var ok = true;
var overthing = getthingxy(event.pageX,event.pageY);
if (thingsmoving()) {
console.log("mouseup() - things are moving");
ok = false;
} else if (!isvalidpath(curpath)) {
console.log("mouseup() - path isn't valid");
ok = false;
} else if ((overthing == undefined) || !pathcontains(overthing)) {
console.log("mouseup() - not over something in path");
ok = false;
}
if (!ok) {
clearpath();
return;
}
// kill all in path
while (curpath != undefined && curpath.length > 0) {
curpath[0].kill();
curpath.splice(0, 1);
}
curpath = null;
clearpath();
},
}
@ -218,19 +303,42 @@ function getrandomcolour() {
function getrandomname() {
var letters = '0123456789';
var name = "cat-";
var name = "";
for (var i = 0; i < 3; i++ ) {
name += letters[Math.floor(Math.random() * 10)];
}
return name;
}
function thing(gridx, gridy, name) {
this.name = name;
function getrandomtype() {
var pct,type;
pct = Math.random() * 100;
if ( pct < 50) {
type = "cat";
} else {
type = "food";
}
return type;
}
function thing(gridx, gridy, type) {
this.width = THINGSIZE;
this.height = THINGSIZE;
this.color = getrandomcolour();
if (type == "random") {
type = getrandomtype();
}
this.type = type;
if (this.type == "cat") {
this.color = "#b5dea8";
} else if (type == "food") {
this.color = "#d8db03";
} else {
this.color = getrandomcolour();
}
this.name = type + "-" + getrandomname();
this.gridx = gridx;
if (gridy == "top") {
@ -275,13 +383,23 @@ function thing(gridx, gridy, name) {
if (inpath) {
ctx.beginPath();
ctx.fillText("PATH", this.x + 10, this.y + (THINGSIZE/2) + 20);
ctx.strokeStyle = "red";
if (pathvalid) {
ctx.strokeStyle = "green";
} else {
ctx.strokeStyle = "red";
}
ctx.rect(this.x, this.y, this.width, this.height);
ctx.stroke();
}
}
this.issametype = function(otherthing) {
if (otherthing == undefined) return false;
if (otherthing.type == this.type) return true;
return false;
}
this.snaptogrid = function() {
if (this.y % GRIDSIZE != 0) {
this.y = this.gridy * GRIDSIZE;
@ -300,7 +418,7 @@ function thing(gridx, gridy, name) {
this.kill = function() {
// add a new cat above us
things.push(new thing(this.gridx, "top", getrandomname()));
things.push(new thing(this.gridx, "top", "random"));
// kill ourselves
var idx = things.indexOf(this);
@ -361,19 +479,6 @@ function updateGameArea() {
myGameArea.clear();
myGameArea.draw();
myGameArea.frameNo += 1;
/*
if (myGameArea.frameNo == 1 || everyinterval(150)) {
x = myGameArea.canvas.width;
minHeight = 20;
maxHeight = 200;
height = Math.floor(Math.random()*(maxHeight-minHeight+1)+minHeight);
minGap = 50;
maxGap = 200;
gap = Math.floor(Math.random()*(maxGap-minGap+1)+minGap);
things.push(new thing(10, height, "green", x, 0));
things.push(new thing(10, x - height - gap, "green", x, height + gap));
}
*/
for (i = 0; i < things.length; i += 1) {
things[i].draw();
things[i].move();

3
todo
View File

@ -78,6 +78,7 @@ start:
*drag across multi, all disappear, rest fall, new are generated
*don't allow clicks until all grid locations are full
then change types of things
*allow cat->food or cat->...->cat
cat eating food should take its place