-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.min.js
1 lines (1 loc) · 12.9 KB
/
game.min.js
1
(function(){var t,n,e,i,o,r,s,h,a,u,c,p,f,d,l,g,m,y,_,x,v,w,C,b,M,A,I={}.hasOwnProperty,E=function(t,n){function e(){this.constructor=t}for(var i in n)I.call(n,i)&&(t[i]=n[i]);return e.prototype=n.prototype,t.prototype=new e,t.__super__=n.prototype,t},B=[].slice,T=[].indexOf||function(t){for(var n=0,e=this.length;e>n;n++)if(n in this&&this[n]===t)return n;return-1};c=window._GAME_CONSTATS_={author:"Kamil Misiowiec"},h=function(){function t(t,n){this.x=null!=t?t:0,this.y=null!=n?n:0,this.width=0,this.height=0}return t.prototype.draw=function(){throw new Error("Draw method should be overriden")},t}(),l=function(t){function n(){n.__super__.constructor.apply(this,arguments),this.childs=[]}return E(n,t),n.prototype.draw=function(t){var n,e,i,o;for(t.save(),t.translate(this.x,this.y),o=this.childs,e=0,i=o.length;i>e;e++)n=o[e],n.draw(t);return t.restore()},n.prototype.addChild=function(t){return this.childs.push(t)},n.prototype.removeChild=function(t){return this.childs.splice(this.childs.indexOf(t),1)},n.prototype.onMouseDown=function(t,e){var i,o,r,s;for(s=this.childs,o=0,r=s.length;r>o;o++)i=s[o],i instanceof n?i.onMouseDown(t,e):i.x+this.x<t&&t<i.x+this.x+i.width&&i.y+this.y<e&&e<i.y+this.y+i.height&&"function"==typeof i.onMouseDown&&i.onMouseDown()},n}(h),b=function(t){function n(){var t,e;t=arguments[0],e=2<=arguments.length?B.call(arguments,1):[],this.canvas=t,n.__super__.constructor.apply(this,e),this.ctx=this.canvas.getContext("2d"),this.ctx.webkitImageSmoothingEnabled=!1,this.ctx.mozImageSmoothingEnabled=!1,this.ctx.scale(2,2),this.width=this.canvas.width/2,this.height=this.canvas.height/2,this.canvas.addEventListener("mousedown",this._onMouseDown.bind(this))}return E(n,t),n.prototype.render=function(){return this.draw(this.ctx)},n.prototype._onMouseDown=function(t){var n,e;return n=t.pageX-this.canvas.offsetLeft,e=t.pageY-this.canvas.offsetTop,this.onMouseDown(n/2,e/2)},n}(l),M=function(t){function n(t,e,i){this.text=t,this.font=null!=e?e:"20px Arial",this.color=null!=i?i:"black",n.__super__.constructor.call(this)}return E(n,t),n.prototype.draw=function(t){return t.font=this.font,t.fillStyle=this.color,t.fillText(this.text,this.x,this.y)},n}(h),g=function(){function t(){}return t.images={},t.load=function(t){var n,e,i;for(e=0,i=t.length;i>e;e++)n=t[e],null==this.images[n]&&(this.images[n]=new Image,this.images[n].src=n);return this._checkIfReady()},t.onready=null,t._checkIfReady=function(){var t,n,e;e=this.images;for(n in e)if(t=e[n],!t.complete)return setTimeout(this._checkIfReady.bind(this),50);return this.onready()},t}(),u=function(){function t(t,n){this.x=null!=t?t:0,this.y=null!=n?n:0,this.set({hp:0,level:0,id:32})}return t.prototype.set=function(t){var n,e;for(n in t)e=t[n],this[n]=e},t}(),m=function(){function t(){var t,n,e,i,o,r,s,h,u,c,p,f,d,l;for(this.data=[],this.enemies=[],h=u=0;100>=u;h=++u)for(o=[],this.data.push(o),s=c=0;9>=c;s=++c)o.push([[(0|4*Math.random())+1],[]]);for(i=["@@@@@@@@@@","@@@@@@@@@@","@@@@@@@@@@","@@@@@@@@@@","@@@@@@@@@@","@@@@@@@@@@","@@@@@@@@@@","@@@@@@@@@@","@@@@@@@@@@","@@@@@@@@@@","@@@@@@@@@@","@@...r..@@","@@......@@","@@@@@|@@@@","....e|e...",".....|....","...>-]....","..q|.q....","...[---<..","..@@@@@|..","w.@@@@@|.e","..@@@@@|..",".w..>--]e.","...>].....","@..|@q....",".@@|@@....","@@@|@@@@@@","..e|......","...|..w...","@q@[<.....","@@@@[<..e.","@@@@@[<...","@@@@@@|.q.","@@.@@@|...","@@@@@>]...",".@@@>]..w.","...@|.w...",".q..|....@","....|.q.@@","...>]....@","..q|..q...","@.@[<...w.","@@@@[<....","@@@@@[<..q","@@@@@@|...","@@.@@@|.q.","@@@@@>]...",".@@@>]..q.","...@|....@","....|....@","....|...@@","....|...@@"],r={"@":0,"|":16,">":21,"]":20,"[":18,"<":17,"-":19},e={q:48,w:49,e:50,r:51},h=p=0,d=i.length;d>p;h=++p)for(o=i[h],s=f=0,l=o.length;l>f;s=++f)t=o[s],void 0!==r[t]?this.data[h][s][0].push(r[t]):e[t]&&(n=new a(e[t]),this.enemies.push(n),this.addEntity(n,s,h))}return t.Obstacle=[0],t.prototype.canIMoveTo=function(n,e){return n>0&&10>n&&e>0&&e<this.data.length&&!(this.data[e][n][0].some(function(n){return T.call(t.Obstacle,n)>=0})||this.data[e][n][1].length>0)},t.prototype.isSomeoneOn=function(t,n){return this.data[n][t][1].some(function(t,n){return n>0&&null!=t})},t.prototype.getEntitiesAt=function(t,n){return this.data[n][t][1]},t.prototype.getEntitesAround=function(t,n){return this.enemies.filter(function(e){var i,o;return t-1<=(i=e.x)&&t+1>=i&&n-1<=(o=e.y)&&n+1>=o})},t.prototype.removeEntity=function(t){var n,e,i,o,r,s;for((r=this.data[t.y][t.x][1]).splice(r.indexOf(t),1),s=this.enemies,e=i=0,o=s.length;o>i;e=++i)if(n=s[e],n.pesel===t.pesel)return this.enemies.splice(e,1),void 0},t.prototype.addEntity=function(t,n,e){var i;return t.x=n,t.y=e,(null!=(i=this.data[e][n])[1]?(i=this.data[e][n])[1]:i[1]=[]).push(t)},t.prototype.moveTo=function(t,n,e){var i,o;this.canIMoveTo(n,e)&&(o=this.getEntitiesAt(t.x,t.y),i=this.getEntitiesAt(n,e),o.splice(o.indexOf(t),1),t.set({x:n,y:e}),i.push(t))},t}(),y=function(t){function n(){var t,e;t=arguments[0],e=2<=arguments.length?B.call(arguments,1):[],this.map=t,n.__super__.constructor.apply(this,e),this.srcImg=g.images["img/tiles.png"]}return E(n,t),n.prototype.draw=function(t){var n,e,i,o,r,s,h,a,u,c,p,f,d,l,g,m,y,_,x;for(t.save(),t.translate(this.x,this.y),y=this.map.data,a=u=0,d=y.length;d>u;a=++u)for(i=y[a],h=c=0,l=i.length;l>c;h=++c){for(e=i[h],_=e[0],p=0,g=_.length;g>p;p++)s=_[p],o=s%16,r=0|s/16,t.drawImage(this.srcImg,16*o,16*r,16,16,16*h,16*a,16,16);for(x=e[1],f=0,m=x.length;m>f;f++)n=x[f],o=n.id%16,r=0|n.id/16,t.drawImage(this.srcImg,16*o,16*r,16,16,16*h,16*a,16,16)}return t.restore()},n}(h),d=function(t){function n(){n.__super__.constructor.apply(this,arguments),this.id=32,this.hp=3}return E(n,t),n}(u),a=function(t){function n(t){null==t&&(t=48),n.__super__.constructor.apply(this,arguments),this.id=t,this.pesel=n.lastEnemyPesel++,this.hp=n.hitpointsById[t]}return E(n,t),n.lastEnemyPesel=0,n.hitpointsById={48:1,49:3,50:4,51:8},n.damageById={48:1,49:2,50:2,51:3},n.prototype.hit=function(){return--this.hp<1?(p.Ctx.map.removeEntity(this),p.Ctx.map.data[this.y][this.x][0].push(80),51===this.id?setTimeout(p.Ctx.onGameWin.bind(p.Ctx,0)):void 0):void 0},n.prototype.update=function(t,e){var i,o,r;i={x:t.x-this.x,y:t.y-this.y},Math.abs(i.x)<3&&Math.abs(i.y)<3?e.moveTo(this,this.x+i.x/Math.abs(i.x),this.y+i.y/Math.abs(i.y)):e.moveTo(this,this.x+Math.round(3*Math.random()-1.5),this.y),this.x-1<=(o=t.x)&&o<=this.x+1&&this.y-1<=(r=t.y)&&r<=this.y+1&&(t.hp-=n.damageById[this.id],t.hp<1&&setTimeout(p.Ctx.onGameEnd.bind(p.Ctx,0)))},n.prototype._distnaceToHero=function(t){return{x:t.x-this.x,y:t.y-this.y}},n}(u),p=function(){function o(){o.Ctx=this,this.map=new m,this.hero=new d,this.actionManager=new i,this.map.addEntity(this.hero,4,48),this.mapRenderer=new y(this.map),this.actionBar=new n(this.actionManager),this.actionBar.update(),this.healthBar=new f(this.hero.hp),this.actionCounterBar=new e(t.movesLeft),c.stage.addChild(this.mapRenderer),c.stage.addChild(this.actionBar),c.stage.addChild(this.healthBar),c.stage.addChild(this.actionCounterBar)}return o.Ctx=null,o.prototype.makeStep=function(){var n,e,i,o;for(o=this.map.enemies,e=0,i=o.length;i>e;e++)n=o[e],n.update(this.hero,this.map);return this.actionBar.update(),this.actionCounterBar.update(t.movesLeft),this.healthBar.update(this.hero.hp),this.render()},o.prototype.render=function(){return this.mapRenderer.y=16*-(this.hero.y-9),c.stage.render()},o.prototype.onGameEnd=function(){return c.stage.ctx.drawImage(g.images["img/gameover.png"],0,0),c.stage.childs=[]},o.prototype.onGameWin=function(){return c.stage.ctx.drawImage(g.images["img/gamewin.png"],0,0),c.stage.childs=[]},o}(),t=function(){function t(n){this.icon=null!=n?n:0,this.id=t.lastID++}return t.lastID=0,t.movesLeft=2,t.prototype.execute=function(t){return this._execute(),t()},t.prototype._execute=function(){throw new Error("This method should be overriden")},t.prototype["do"]=function(){var t,n,e=this;return t=[this].concat(this._getComboActions()),(n=function(i){return i===t.length?e._done():t[i].execute(function(){return p.Ctx.actionManager.replaceWithNew(t[i]),n(i+1)})})(0)},t.prototype._done=function(){return t.movesLeft-=1,p.Ctx.actionCounterBar.update(t.movesLeft),t.movesLeft<1?(t.movesLeft=2,p.Ctx.makeStep()):(p.Ctx.actionBar.update(),p.Ctx.render())},t.prototype._getComboActions=function(){var t,n=this;return t=[],p.Ctx.actionManager.currentActions.forEach(function(e,i){return e.id===n.id?t=n._countLeft(i-1).concat(n._countRight(i+1)):void 0}),t},t.prototype._countLeft=function(t){var n;return t>=0?(n=p.Ctx.actionManager.currentActions[t],n instanceof this.constructor?[n].concat(this._countLeft(t-1)):[]):[]},t.prototype._countRight=function(t){var n;return t<p.Ctx.actionManager.currentActions.length?(n=p.Ctx.actionManager.currentActions[t],n instanceof this.constructor?[n].concat(this._countRight(t+1)):[]):[]},t}(),r=function(t){function n(){n.__super__.constructor.call(this,0)}return E(n,t),n.prototype._execute=function(){var t,n,e;return e=p.Ctx.hero,t=e.x,n=e.y,p.Ctx.map.getEntitesAround(t,n).forEach(function(t){return t.hit()})},n}(t),C=function(t){function n(){n.__super__.constructor.call(this,6)}return E(n,t),n.prototype._execute=function(){var t;return p.Ctx.hero.hp<10?(t=p.Ctx.hero.hp+=1,p.Ctx.healthBar.update(t)):void 0},n}(t),o=function(n){function e(){e.__super__.constructor.call(this,7)}return E(e,n),e.prototype._execute=function(){var n;if(t.movesLeft<4)return n=t.movesLeft+=2,p.Ctx.actionCounterBar.update(n),c.stage.render()},e}(t),_=function(t){function n(){n.__super__.constructor.call(this,5)}return E(n,t),n.prototype.execute=function(t){return p.Ctx.actionBar.askForDirection(function(n){return n._execute(),p.Ctx.render(),t()})},n}(t),x=function(t){function n(){n.__super__.constructor.call(this,1)}return E(n,t),n.prototype._execute=function(){var t,n,e;return e=p.Ctx.hero,t=e.x,n=e.y,p.Ctx.map.moveTo(p.Ctx.hero,t,n-1)},n}(t),v=function(t){function n(){n.__super__.constructor.call(this,3)}return E(n,t),n.prototype._execute=function(){var t,n,e;return e=p.Ctx.hero,t=e.x,n=e.y,p.Ctx.map.moveTo(p.Ctx.hero,t-1,n)},n}(t),w=function(t){function n(){n.__super__.constructor.call(this,2)}return E(n,t),n.prototype._execute=function(){var t,n,e;return e=p.Ctx.hero,t=e.x,n=e.y,p.Ctx.map.moveTo(p.Ctx.hero,t+1,n)},n}(t),i=function(){function t(){this._ActionClasses=[_,r,o,C],this.currentActions=[],this.reset()}return t.prototype.reset=function(){var t,n,e;for(this.currentActions=[],e=[],t=n=0;5>n;t=++n)e.push(this.currentActions.push(this._getNewRandomAction()));return e},t.prototype.replaceWithNew=function(t){var n;return n=this.currentActions.indexOf(t),this.currentActions[n]=this._getNewRandomAction()},t.prototype._getNewRandomAction=function(){var t,n,e,i,o,r,s,h,a;for(e=this._ActionClasses.length,i=function(){var t,n;for(n=[],s=t=1;e>=t;s=t+=1)n.push(Math.pow(2.5,s));return n}(),i.reverse(),i[0]=i[0]/2+1,o=Math.random()*i.reduce(function(t,n){return t+n}),r=0,n=h=0,a=i.length;a>h;n=++h)if(t=i[n],r+=t,r>o)return new this._ActionClasses[n]},t}(),s=function(t){function n(){var t,e;e=arguments[0],t=2<=arguments.length?B.call(arguments,1):[],this.id=e,n.__super__.constructor.apply(this,t),this.img=g.images["img/buttons.png"],this.width=32,this.height=32}return E(n,t),n.prototype.draw=function(t){return t.drawImage(this.img,32*this.id,0,32,32,this.x,this.y,32,32)},n}(h),n=function(t){function n(t){var e,i,o;for(this.actionMgr=t,n.__super__.constructor.call(this,0,c.stage.height-32),this.buttons=[],i=o=0;5>o;i=++o)e=new s(0,32*i),this.buttons.push(e),this.addChild(e)}return E(n,t),n.prototype.update=function(){var t,n,e,i,o,r;if(this.actionMgr.currentActions)for(t=this.actionMgr.currentActions,r=this.buttons,e=i=0,o=r.length;o>i;e=++i)n=r[e],n.id=t[e].icon,n.onMouseDown=t[e]["do"].bind(t[e])},n.prototype.askForDirection=function(t){return this.buttons[0].id=4,this.buttons[1].id=3,this.buttons[1].onMouseDown=t.bind(null,new v),this.buttons[2].id=1,this.buttons[2].onMouseDown=t.bind(null,new x),this.buttons[3].id=2,this.buttons[3].onMouseDown=t.bind(null,new w),this.buttons[4].id=4,c.stage.render()},n}(l),f=function(t){function n(t){this.health=null!=t?t:0,n.__super__.constructor.call(this,0,0),this.img=g.images["img/tiles.png"]}return E(n,t),n.prototype.update=function(t){this.health=t},n.prototype.draw=function(t){var n,e,i;for(n=e=0,i=this.health;i>=0?i>e:e>i;n=i>=0?++e:--e)t.drawImage(this.img,16,32,16,16,this.x+16*n,this.y,16,16)},n}(h),e=function(t){function n(t){this.actions=null!=t?t:0,n.__super__.constructor.call(this,0,c.stage.height-32-16),this.img=g.images["img/tiles.png"]}return E(n,t),n.prototype.update=function(t){this.actions=t},n.prototype.draw=function(t){var n,e,i,o;for(n=e=0;2>=e;n=++e)t.drawImage(this.img,32,32,16,16,this.x+16*n,this.y,16,16);for(n=i=0,o=this.actions-1;o>=0?o>i:i>o;n=o>=0?++i:--i)t.drawImage(this.img,48,32,16,16,this.x+16*n,this.y,16,16)},n}(h),c.canvas=document.getElementById("canvas"),c.stage=A=new b(c.canvas),g.onready=function(){var t;return t=new p,t.makeStep(),c.stage.render()},g.load(["img/buttons.png","img/tiles.png","img/gameover.png","img/gamewin.png"])}).call(this);