diff --git a/images/boss.png b/images/boss.png new file mode 100644 index 0000000..15954d5 Binary files /dev/null and b/images/boss.png differ diff --git a/index.html b/index.html index c1764a8..6c00e19 100644 --- a/index.html +++ b/index.html @@ -1,10 +1,43 @@ - - - - Simple Canvas Game - - - - + + + + + + + + Simple Canvas Game + + + + + + + + diff --git a/js/Hero.js b/js/Hero.js new file mode 100644 index 0000000..1339808 --- /dev/null +++ b/js/Hero.js @@ -0,0 +1,38 @@ +//==================================================================================== +// HERO CLASS +//==================================================================================== +Game.Hero = (function(win, doc) { + + /** + @constructor + */ + function Hero() { + this.speed = 256; + this.x = 0; + this.y = 0; + } + + //----------------------------------------------------------------------------------------------------- + // PRIVATE METHODS + //----------------------------------------------------------------------------------------------------- + //----------------------------------------------------------------------------------------------------- + + //----------------------------------------------------------------------------------------------------- + // EVENT HANDLERS + //----------------------------------------------------------------------------------------------------- + //----------------------------------------------------------------------------------------------------- + + //----------------------------------------------------------------------------------------------------- + // PUBLIC METHODS + //----------------------------------------------------------------------------------------------------- + Hero.prototype = { + 'init': function() { + + } + }; + //----------------------------------------------------------------------------------------------------- + + return Hero; + +}(window, document)); +//==================================================================================== \ No newline at end of file diff --git a/js/Level.js b/js/Level.js new file mode 100644 index 0000000..605fd7e --- /dev/null +++ b/js/Level.js @@ -0,0 +1,47 @@ +//==================================================================================== +// LEVEL CLASS +//==================================================================================== +Game.Level = (function(win, doc) { + + /** + @constructor + */ + function Level() { + this.stages = [{ + "numEnemies": 3, + "numBosses": 0, + "enemyStrength": 1 }, { + "numEnemies": 6, + "numBosses": 0, + "enemyStrength": 1 }]; + } + + //----------------------------------------------------------------------------------------------------- + // PRIVATE METHODS + //----------------------------------------------------------------------------------------------------- + //----------------------------------------------------------------------------------------------------- + + //----------------------------------------------------------------------------------------------------- + // EVENT HANDLERS + //----------------------------------------------------------------------------------------------------- + //----------------------------------------------------------------------------------------------------- + + //----------------------------------------------------------------------------------------------------- + // PUBLIC METHODS + //----------------------------------------------------------------------------------------------------- + Level.prototype = { + 'get': function(numLevel) { + //make sure numLevel is within the amount of stages + if((numLevel < this.stages.length - 1) && (numLevel < 0)) { + return false; + } + + return this.stages[numLevel]; + } + }; + //----------------------------------------------------------------------------------------------------- + + return new Level(); + +}(window, document)); +//==================================================================================== \ No newline at end of file diff --git a/js/Monster.js b/js/Monster.js new file mode 100644 index 0000000..dadb490 --- /dev/null +++ b/js/Monster.js @@ -0,0 +1,37 @@ +//==================================================================================== +// MONSTER CLASS +//==================================================================================== +Game.Monster = (function(win, doc) { + + /** + @constructor + */ + function Monster() { + this.x = 0; + this.y = 0; + } + + //----------------------------------------------------------------------------------------------------- + // PRIVATE METHODS + //----------------------------------------------------------------------------------------------------- + //----------------------------------------------------------------------------------------------------- + + //----------------------------------------------------------------------------------------------------- + // EVENT HANDLERS + //----------------------------------------------------------------------------------------------------- + //----------------------------------------------------------------------------------------------------- + + //----------------------------------------------------------------------------------------------------- + // PUBLIC METHODS + //----------------------------------------------------------------------------------------------------- + Monster.prototype = { + 'init': function() { + + } + }; + //----------------------------------------------------------------------------------------------------- + + return Monster; + +}(window, document)); +//==================================================================================== \ No newline at end of file diff --git a/js/curl.js b/js/curl.js new file mode 100644 index 0000000..71042b2 --- /dev/null +++ b/js/curl.js @@ -0,0 +1,23 @@ +/* + MIT License (c) copyright B Cavalier & J Hann */ +var r=!0,z=!1; +(function(l){function i(){}function t(a,b){return 0==O.call(a).indexOf("[object "+b)}function m(a){return a&&"/"==a.charAt(a.length-1)?a.substr(0,a.length-1):a}function j(a,b){var c,f,e;f=1;a=a.replace(P,function(a,b,c,k){c&&f++;e=r;return k||""});return e?(c=b.split("/"),c.splice(c.length-f,f),c.concat(a||[]).join("/")):a}function B(a){var b=a.indexOf("!");return{K:a.substr(b+1),j:0<=b&&a.substr(0,b)}}function x(){}function p(a){x.prototype=a;a=new x;x.prototype=I;return a}function A(){function a(a,b, +c){f.push([a,b,c])}function b(a,b){for(var c,e=0;c=f[e++];)(c=c[a])&&c(b)}var c,f,e;c=this;f=[];e=function(c,s){a=c?function(a){a&&a(s)}:function(a,b){b&&b(s)};e=i;b(c?0:1,s);b=i;f=u};this.$=function(b,c,f){a(b,c,f)};this.h=function(a){c.r=a;e(r,a)};this.d=function(a){c.da=a;e(z,a)};this.o=function(a){b(2,a)}}function o(a,b,c,f){a instanceof A?a.$(b,c,f):b(a)}function d(a,b,c){var f;return function(){0<=--a&&b&&(f=b.apply(u,arguments));0==a&&c&&c(f);return f}}function q(){function a(b,f,e){var g; +g=k.f(h,u,[].concat(b));this.then=b=function(a,b){o(g,function(b){a&&a.apply(u,b)},function(a){if(b)b(a);else throw a;});return this};this.next=function(b,c){return new a(b,c,g)};f&&b(f);o(e,function(){k.i(g)})}var b=[].slice.call(arguments);t(b[0],"Object")&&(h=k.b(b.shift()));return new a(b[0],b[1])}function D(a){var b=a.id;if(b==u)if(E!==u)E={z:"Multiple anonymous defines in url"};else if(!(b=k.V()))E=a;if(b!=u){var c=v[b];b in v||(c=k.k(b,h).b,c=v[b]=k.u(c,b));if(!(c instanceof A))throw Error("duplicate define: "+ +b);c.ca=z;k.v(c,a)}}var h=l.curl,w,C,y=l.document,n=y&&(y.head||y.getElementsByTagName("head")[0]),J={},K={},L={},F={},I={},O=I.toString,u,M={loaded:1,interactive:L,complete:1},v={},G=z,E,Q=/\?/,N=/^\/|^[^:]+:\/\//,P=/(\.)(\.?)(?:$|\/([^\.\/]+.*)?)/g,R=/\/\*[\s\S]*?\*\/|(?:[^\\])\/\/.*?[\n\r]/g,S=/require\s*\(\s*["']([^"']+)["']\s*\)|(?:[^\\]?)(["'])/g,H,k;k={f:function(a,b,c,f){function e(a){return j(a,s.e)}function g(b,c){var g,d,h,i;g=c&&function(a){c.apply(u,a)};if(t(b,"String")){d=e(b);h=v[d]; +i=h instanceof A&&h.a;if(!(d in v))throw Error("Module not resolved: "+d);if(g)throw Error("require(id, callback) not allowed");return i||h}o(k.i(k.f(a,s.e,b,f)),g)}var s;s=new A;s.e=s.id=b||"";s.W=f;s.w=c;s.p=g;g.toUrl=function(b){return k.k(e(b),a).url};s.ba=e;return s},u:function(a,b,c,f){var e,g,s;e=k.f(a,b,u,c);e.e=f==u?b:f;g=e.h;s=d(1,function(a){e.m=a;try{return k.O(e)}catch(b){e.d(b)}});e.h=function(a){o(c||G,function(){g(v[e.id]=s(a))})};e.A=function(a){o(c||G,function(){e.a&&(s(a),e.o(K))})}; +return e},N:function(a,b,c,f){a=k.f(a,b,u,c);a.e=f;return a},U:function(a){return a.p},B:function(a){return a.a||(a.a={})},T:function(a){var b=a.n;b||(b=a.n={id:a.id,uri:k.C(a),exports:k.B(a)},b.a=b.exports);return b},C:function(a){return a.url||(a.url=k.t(a.p.toUrl(a.id)))},b:function(a){var b,c,f;(b=a)||(a={});c=a.apiName;if((f=a.apiContext)||c?f[c]:w&&b)throw Error((c||"curl")+" already exists");(f||l)[c||"curl"]=q;w&&b&&(l.curl=w);c=a.defineName;if((f=a.defineContext)||c?f[c]:C&&b)throw Error((c|| +"define")+" already exists");(f||l)[c||"define"]=c=function(){var a=k.S(arguments);D(a)};C&&b&&(l.define=C);c.amd={plugins:r,jQuery:r,curl:"0.6.4"};b&&(k.b=k.H);return k.H(a)},H:function(a){function b(b,c){var e,g,d,h,i;for(i in b){d=b[i];d.name=d.id||d.name||i;h=a;g=B(m(d.name||i));e=g.K;if(g=g.j)h=f[g],h||(h=f[g]=p(a),h.g=p(a.g),h.c=[]),delete b[i];if(c){g=d;var n=void 0;g.path=m(g.path||g.location||"");n=m(g.main)||"main";"."!=n.charAt(0)&&(n="./"+n);g.F=j(n,g.name+"/");g.X=j(n,g.path+"/");g.b= +g.config}else g={path:m(d)};g.L=e.split("/").length;e?(h.g[e]=g,h.c.push(e)):h.s=k.J(d,a)}}function c(a){var b=a.g;a.Z=RegExp("^("+a.c.sort(function(a,c){return b[a].La.indexOf("/")&&!(a in f))&&(g=a=m(b.I)+"/"+a);c=N.test(a)?a:a.replace(b.Z,function(b){e=f[b]||{};d=e.b;return e.F&&b==a?(g=e.F,e.X):e.path||""});return{e:g||a,b:d||h,url:k.J(c,b)}},J:function(a,b){var c=b.s;return c&&!N.test(a)?m(c)+"/"+a:a},t:function(a){return a+(Q.test(a)?"":".js")},D:function(a,b,c){var f=y.createElement("script");f.onload=f.onreadystatechange=function(c){c=c||l.event;if("load"== +c.type||M[f.readyState])delete F[a.id],f.onload=f.onreadystatechange=f.onerror="",b()};f.onerror=function(){c(Error("Syntax or http error: "+a.url))};f.type=a.G||"text/javascript";f.charset="utf-8";f.async=!a.Y;f.src=a.url;F[a.id]=f;n.insertBefore(f,n.firstChild);return f},P:function(a){var b=[],c;("string"==typeof a?a:a.toSource?a.toSource():a.toString()).replace(R,"").replace(S,function(a,e,g){g?c=c==g?u:c:c||b.push(e);return a});return b},S:function(a){var b,c,f,e,g,d;g=a.length;f=a[g-1];e=t(f, +"Function")?f.length:-1;2==g?t(a[0],"Array")?c=a[0]:b=a[0]:3==g&&(b=a[0],c=a[1]);!c&&0= this.canvas.width - 32) { + this.hero.x = this.canvas.width - 32; + } + + //if hero hit the top + if(this.hero.y <= 0) { + this.hero.y = 0; + } + + //if hero hit the bottom + if(this.hero.y >= this.canvas.height - 32) { + this.hero.y = this.canvas.height - 32; + } + + //Are the hero and monsters touching? + for(var i = 0; i < this.enemies.length; i++) { + if (this.hero.x < (this.enemies[i].x + 32) && this.enemies[i].x < (this.hero.x + 32) && this.hero.y <= (this.enemies[i].y + 32) && this.enemies[i].y <= (this.hero.y + 32)) { + this.enemies.splice(i, 1); + + ++this.monstersCaught; + + if(this.enemies.length === 0) { + this.start(); + } + } + } + + return this; + }, + + /** + @function + @description + @returns {Game} + */ + 'run': function() { + var now = Date.now(), delta = now - this.then; + + this.update(delta / 1000); + this.render(); + + this.then = now; + + return this; + } + }; + //----------------------------------------------------------------------------------------------------- + + return new Game(); - // Score - ctx.fillStyle = "rgb(250, 250, 250)"; - ctx.font = "24px Helvetica"; - ctx.textAlign = "left"; - ctx.textBaseline = "top"; - ctx.fillText("Goblins caught: " + monstersCaught, 32, 32); -}; - -// The main game loop -var main = function () { - var now = Date.now(); - var delta = now - then; - - update(delta / 1000); - render(); - - then = now; -}; - -// Let's play this game! -reset(); -var then = Date.now(); -setInterval(main, 1); // Execute as fast as possible +}(window, document)); +//==================================================================================== \ No newline at end of file