Skip to content

Instantly share code, notes, and snippets.

@cioddi
Created October 29, 2016 09:07
Show Gist options
  • Save cioddi/5d5613df849274149a7c89f3b73e67b0 to your computer and use it in GitHub Desktop.
Save cioddi/5d5613df849274149a7c89f3b73e67b0 to your computer and use it in GitHub Desktop.
<html>
<body>
<canvas width="800" height="800" id="c" />
<script type="text/javascript">
function doit(){
F = 0;
E = 'blue';
if(Cobj[2] == E)E = 'red';
if(Cobj[5] > 8 && Cobj[4] == 1){
build = [4,1,4];
if(count(3,Cobj[2]) < 9)build = [3,2,3];
Objs.push([Cobj[0],Cobj[1],Cobj[2],build[2],build[0],3,build[1],0]);
Cobj[5] -= 8;
}
if(Cobj[4] == 3){
F = 1;
E = Cobj[2];
if(Cobj[7] == 0 && count(2,'green') > 0){
F = 2;
E = 'green';
}
}
if(Cobj[4] == 4){
if(count(3,E))F = 3;
if(count(4,E))F = 4;
}
if(F){
tobj = 0;
tobj = movetonext(Cobj[0],Cobj[1],F,E,Cobj[6]);
if(Cobj[0] == Objs[tobj][0] && Cobj[1] == Objs[tobj][1]){
if(Cobj[7] == 1)Objs[tobj][5] += 1;
Cobj[7] = 0;
if(F == 2 || Cobj[4] == 4){
Cobj[7] = 1;
Objs[tobj][5] -= 2;
}
}
Cobj[0] = newX;
Cobj[1] = newY;
}
ctx.fillStyle = Cobj[2];
ctx.fillRect(Cobj[0],Cobj[1],Cobj[3],Cobj[3]);
}
Objs = [];
function count(B,sp){
var cnt = 0;
for(var k in Objs)if(Objs[k][2] == sp && Objs[k][4] == B)cnt++;
return cnt;
}
function C(){
A = [];
ctx.clearRect(0, 0, b, b);
for(var i in Objs){
Cobj = Objs[i];
doit();
Objs[i] = Cobj;
if(Objs[i][5] < 1)A.push(i);
}
while(A.length){
Objs.splice(A.pop(),1);
}
setTimeout('C()', 1);
}
function movetonext(xPos,yPos,B,color,v){
obj = 0;
for(var j in Objs){
if(Objs[j][4] == B && Objs[j][2] == color){
if(!obj)obj = j;
else if(getD(xPos,yPos,Objs[j][0],Objs[j][1]) < getD(xPos,yPos,Objs[obj][0],Objs[obj][1]))obj = j;
}
}
newX = Objs[obj][0];
newY = Objs[obj][1];
D = getD(xPos,yPos,newX,newY);
if(D > v){
newX = Math.round(v/D * (newX - xPos) + xPos);
newY = Math.round(v/D * (newY - yPos) + yPos);
}
return obj;
}
function getD(xPos,yPos,destXPos,destYPos){
return Math.round(Math.sqrt(((destXPos -xPos)*(destXPos -xPos))+((destYPos-yPos)*(destYPos-yPos))));
}
function r1(max){
return Math.floor(Math.random() * max)-3;
}
c = document.getElementById("c");
ctx = c.getContext("2d");
b = 500;
Objs.push([r1(b),r1(b),'red',10,1,9,0,0]);
Objs.push([r1(b),r1(b),'blue',10,1,9,0,0]);
for(i=1;i<91;i++){
Objs.push([r1(b),r1(b),'green',8,2,9,0,0]);
}
C();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment