var w,h,ratio,i,s,g,g2,el,el2,div,game,my={};function germMain(){var version='0.61' w=440;h=440;my.opts={type:'r8',appName:document.currentScript.innerHTML.replace('Main();','')} my.drag={onQ:false,x:0,y:0,i:0} my.id=0;my.time=0;my.imgHome=(document.domain=='localhost')?'/mathsisfun/games/images/':'/games/images/' my.shooterTypes={1:{type:1,name:'Soap',x:0,y:0,rad:125,cost:10,n:0,ammoSpeed:30,shootTime:0.5,bombTime:0,wd:40,ht:40,action:'kill'},2:{type:2,name:'Iodine',x:0,y:0,rad:135,cost:10,n:0,ammoSpeed:10,shootTime:3,bombTime:1,action:'kill'},3:{type:3,name:'Freeze',x:0,y:0,rad:65,cost:10,n:0,ammoSpeed:50,shootTime:0.3,bombTime:0,action:'freeze'},4:{type:4,name:'Antibody',x:0,y:0,rad:30,cost:10,n:0,ammoSpeed:20,shootTime:0.4,bombTime:0,action:'kill'},5:{type:5,name:'Apple',x:0,y:0,rad:35,cost:10,n:0,ammoSpeed:30,shootTime:1,bombTime:0,action:'kill'},6:{type:6,name:'Nanobot',x:0,y:0,rad:50,cost:10,n:0,ammoSpeed:15,moveSpeed:10,shootTime:0.6,bombTime:0,action:'kill'},7:{type:7,name:'Nanobot2',x:0,y:0,rad:105,cost:10,n:0,ammoSpeed:20,moveSpeed:15,shootTime:0.4,bombTime:0,action:'kill'},} var s='';s+=`` s+='';s+='
';s+='';s+='';s+='
';s+=getPlayHTML(36);s+='
';s+=`
0
` s+='
';var cans=['can1','can2','can3'];for(var i=0;i';} s+='
germs
' s+='
';s+=optPopHTML();s+='
© 2019 MathsIsFun.com v'+version+'
' s+='
';document.write(s);var els=[];var gs=[];for(var i=0;ihighestIndex){my.drag.x=mouseX-shooter.x;my.drag.y=mouseY-shooter.y;highestIndex=i;my.drag.i=i;}}} if(my.drag.onQ){if(ev.touchQ){window.addEventListener('touchmove',touchMove,false);}else{window.addEventListener("mousemove",mouseMove,false);}} if(ev.touchQ){el2.removeEventListener("touchstart",touchStart,false);window.addEventListener("touchend",touchEnd,false);}else{el2.removeEventListener("mousedown",mouseDown,false);window.addEventListener("mouseup",mouseUp,false);} if(ev.preventDefault){ev.preventDefault();} else if(ev.returnValue){ev.returnValue=false;} return false;} function mouseUp(ev){g2.clearRect(0,0,g2.canvas.width,g2.canvas.height);el2.addEventListener("mousedown",mouseDown,false);window.removeEventListener("mouseup",mouseUp,false);if(my.drag.onQ){my.drag.onQ=false;window.removeEventListener("mousemove",mouseMove,false);}} function mouseMove(ev){var posX;var posY;var shapeRad=12;var minX=shapeRad;var maxX=el2.width-shapeRad;var minY=shapeRad;var maxY=el2.height-shapeRad;var bRect=el2.getBoundingClientRect();var mouseX=(ev.clientX-bRect.left)*(el2.width/ratio/bRect.width);var mouseY=(ev.clientY-bRect.top)*(el2.height/ratio/bRect.height);posX=mouseX-my.drag.x;posX=(posXmaxX)?maxX:posX);posY=mouseY-my.drag.y;posY=(posYmaxY)?maxY:posY);var sh=my.shooters[my.drag.i] sh.x=posX;sh.y=posY;my.imgs.move(sh.id,posX,posY,45);g2.clearRect(0,0,g2.canvas.width,g2.canvas.height);g2.strokeStyle='black';g2.fillStyle='rgba(100,100,100,0.1)';g2.beginPath();g2.arc(posX,posY,sh.rad,0,2*Math.PI);g2.fill();g2.stroke();} function hitTest(shape,mx,my){var dx;var dy;dx=mx-shape.x;dy=my-shape.y;return(dx*dx+dy*dyw){x=60 y+=60} console.log('shooterSetup',j,x,y) var id='s'+my.id++;var shooter=new Shooter(id,sh.type,x,y,sh.rad);my.shooters.push(shooter);console.log("shooter",shooter);my.imgs.add(id,`${my.imgHome}gb-shooter${sh.type}.gif`,x,y,10);}}} function animate(){var now=performance.now();if(my.playQ){if(my.prevTime>0)my.time+=now-my.prevTime;doSpawn();var i=my.currgerms.length;while(i--){var germ=my.currgerms[i];germ.move();if(!germ.onQ){my.imgs.remove(germ.id);my.currgerms.splice(i,1);gameCheckOver();break;}} for(i=0;i=my.germs.length){if(my.currgerms.length==0){console.log("gameOver!");}}} function getTimer(){return my.time;} function scoreAdd(x){console.log('scoreAdd',x);my.score+=x my.score=Math.round(my.score*100)/100 document.getElementById('score').innerHTML=my.score} function doSpawn(){var timeNow=(getTimer()-timeStt)/1000;while(my.currGermNopt2.x)pt2.x=pt.x;if(pt.y>pt2.y)pt2.y=pt.y;} return{a:pt1,b:pt2};} function getPlayHTML(w){var s='';s+='';s+='';return s;} function togglePlay(){if(my.resetQ){reset();my.resetQ=false;} if(this.frame>=this.frameMax){this.frame=0;} var btn='playBtn';if(my.playQ){my.playQ=false;document.getElementById(btn).classList.add("play");document.getElementById(btn).classList.remove("pause");}else{my.playQ=true;document.getElementById(btn).classList.add("pause");document.getElementById(btn).classList.remove("play");} if(my.colNo';for(var i=0;i'+opts[i]+'';} s+='';return s;} function optPopHTML(){var s='';s+=`
` s+='
' s+='
' s+='
Total Cash:
' s+='
Total Cost:
' s+='
Cash Left:
' s+='
' s+='
';s+='';s+='';s+='
';s+='
';return s;} function optShooterUpdate(){var sum=0 for(var id in my.shooterTypes){var sh=my.shooterTypes[id] sum+=sh.n*sh.cost} console.log('numChg',sum) var cash=my.userCash-sum if(cash>=0){document.getElementById('opt-cash').innerHTML='$'+my.userCash document.getElementById('opt-cost-tot').innerHTML='$'+sum document.getElementById('opt-cash-left').innerHTML='$'+cash}else{} return my.userCash-sum} function numChg(id,chg){console.log('numChg',id,chg) var nWas=my.shooterTypes[id].n var n=nWas+chg n=Math.max(0,Math.min(n,100)) my.shooterTypes[id].n=n var cash=optShooterUpdate() if(cash>=0){document.getElementById('opt-shoot'+id).innerHTML=n document.getElementById('opt-cost'+id).innerHTML=`$${n*my.shooterTypes[id].cost}`}else{my.shooterTypes[id].n=nWas}} function optPop(){var pop=document.getElementById('optpop');pop.style.transitionDuration="0.3s";pop.style.opacity=1;pop.style.zIndex=12;pop.style.left=(w-400)/2+'px';document.getElementById('shooter-types').innerHTML=optShooterHTML() optShooterUpdate()} function optShooterHTML(){var s='' for(var id in my.shooterTypes){var sh=my.shooterTypes[id] s+=`
${sh.name}
${sh.n}
$${sh.n*sh.cost}
`} return s} function optYes(){var pop=document.getElementById('optpop');pop.style.opacity=0;pop.style.zIndex=1;pop.style.left='-999px';gameNew();} function optNo(){var pop=document.getElementById('optpop');pop.style.opacity=0;pop.style.zIndex=1;pop.style.left='-999px';} function Germ(type,spawnSecs,newpath){this.id='g'+my.id++;this.pathno=0;this.pathpos=0;this.x=-100;this.y=-100;this.driftx=0;this.drifty=0;this.driftxv=0;this.driftyv=0;this.wd=0;this.ht=0;var germtype;this.speed=2;var prevSpeed=1;this.rot=0;this.rotd=0;var frozenQ=false;var spawnTime;this.onQ=true;var spawnSecs=6;this.rand=my.rand this.spawnSecs=spawnSecs;germtype=type;my.imgs.add(this.id,`${my.imgHome}gb-germ${type}.gif`,100*Math.random(),200) path=newpath;driftxv=(random()-0.5)*0.2;driftyv=(random()-0.5)*0.2;rot=1;speed=3+random()*1;spawnTime=getTimer()+(1+random()*0.5)*spawnSecs*1000;} Germ.prototype.getType=function(){return(germtype);} Germ.prototype.move=function(){this.pathpos+=this.speed;while(this.pathpos>my.path[this.pathno].a){this.pathpos-=my.path[this.pathno].a;this.pathno++;if(this.pathno>=my.path.length-1){this.onQ=false;scoreAdd(-5) return}} var ratio=this.pathpos/(my.path[this.pathno].a);if(random()>0.5){this.driftxv+=(random()-0.5)*0.1;this.driftyv+=(random()-0.5)*0.1;if(this.driftx>20)this.driftxv*=-0.1;if(this.driftx<-20)this.driftxv*=-0.1;if(this.drifty>20)this.driftyv*=-0.1;if(this.drifty<-20)this.driftyv*=-0.1;this.rotd=1;} this.driftx+=this.driftxv;this.drifty+=this.driftyv;this.rot+=this.rotd;if(this.pathno+1>',img) dad.removeChild(img);});} this.imgs=[];this.update();} add(id,src,x,y,rot){this.imgs.push({id:id,src:src,x:x,y:y,rot:rot,wd:40,ht:40});this.update();} remove(id){for(var i=0;i `} document.getElementById('imgs').innerHTML=s;}} function Shooter(id,typ,x,y,rad){this.id=id;this.typ=typ;this.x=x;this.y=y;this.rad=rad;this.timeNext=0;this.wd=40;this.ht=40;this.moveSpeed=0;var d=my.shooterTypes[typ];this.rad=d.rad;this.ammoSpeed=d.ammoSpeed;this.moveSpeed=d.moveSpeed;this.shootTime=d.shootTime;this.bombTime=d.bombTime;this.clr=d.clr;this.stroke=d.stroke;this.action=d.action;} Shooter.prototype.doFrame=function(){if(getTimer()>=this.timeNext){for(var i=0;i20)this.onQ=false;} function Ammo(typ,sttx,stty,angle,rad,speed,bombTime,clr,action){this.typ=typ;this.sttx=sttx;this.stty=stty;this.angle=angle;this.rad=rad;this.speed=speed;this.bombTime=bombTime;this.clr=clr;this.action=action;this.x=sttx;this.y=stty;this.sttTime=getTimer();this.onQ=true;var ammoLine;var radius=300;var germSprite;var bombTime;var bombSttTime;var action;} Ammo.prototype.drawMe=function(){var g=my.ammog;g.fillStyle=this.clr;switch(this.typ){case 1:g.strokeStyle='grey';g.beginPath();g.arc(this.x,this.y,5,0,2*Math.PI);g.fill();g.stroke();break;case 2:g.strokeStyle='black';g.beginPath();g.arc(this.x,this.y,5,0,2*Math.PI);g.fill();g.stroke();break;default:g.strokeStyle='white';g.beginPath();g.arc(this.x,this.y,5,0,2*Math.PI);g.fill();g.stroke();}} Ammo.prototype.doFrame=function(){var shootDist=(getTimer()-this.sttTime)/100*this.speed;if(shootDistbombTime){if(parent!=null) parent.removeChild(this);}else{var zoom=1+bombElapsed*2;ammoImg.scaleX=zoom;ammoImg.scaleY=zoom;for(var i=germSprite.numChildren-1;i>=0;i--){var germ=germSprite.getChildAt(i);if(this.hitTestObject(germ)){switch(action){case "kill":if(parent.parent!=null)parent.parent.germKilled(germ);break;case "freeze":if(germ.isFrozen())continue;if(parent.parent!=null)parent.parent.germDelayed(germ);break;default:} break;}}}} function dist(dx,dy){return(Math.sqrt(dx*dx+dy*dy));} var seed=1;function random(){var x=Math.sin(seed++)*10000;return x-Math.floor(x);} function optGet(name){var val=localStorage.getItem(`${my.opts.appName}.${name}`) if(val==null)val=my.opts[name] return val} function optSet(name,val){localStorage.setItem(`${my.opts.appName}.${name}`,val) my.opts[name]=val}