urchinator.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*
  2. otter floating animation
  3. rocks, fish, seaweed, etc
  4. extra leaves on the kelp bases for decoration/deterioration
  5. menu
  6. assets:
  7. fish
  8. urchins
  9. decorations
  10. menus and gfx
  11. */
  12. var images = {};
  13. var img_to_load = {
  14. otter_swimming: 'art/otter-swimming.png',
  15. otter_floating: 'art/otter-floating.png',
  16. otter_eating: 'art/otter-eating.png',
  17. kelp_icon: 'art/kelp-icon.png',
  18. bubble_icon: 'art/bubble-breath.png',
  19. sea_urchin_icon: 'art/sea-urchin-icon.png',
  20. heart: 'art/heart.png',
  21. bubble: 'art/bubble-effect.png',
  22. holdfast1_1: 'art/holdfast1-1.png',
  23. holdfast1_2: 'art/holdfast1-2.png',
  24. holdfast1_3: 'art/holdfast1-3.png',
  25. holdfast1_4: 'art/holdfast1-4.png',
  26. holdfast1_5: 'art/holdfast1-5.png',
  27. holdfast2_1: 'art/holdfast2-1.png',
  28. holdfast2_2: 'art/holdfast2-2.png',
  29. holdfast2_3: 'art/holdfast2-3.png',
  30. holdfast2_4: 'art/holdfast2-4.png',
  31. holdfast2_5: 'art/holdfast2-5.png',
  32. holdfast3_1: 'art/holdfast3-1.png',
  33. holdfast3_2: 'art/holdfast3-2.png',
  34. holdfast3_3: 'art/holdfast3-3.png',
  35. holdfast3_4: 'art/holdfast3-4.png',
  36. holdfast3_5: 'art/holdfast3-5.png',
  37. }
  38. var _inputdata = {
  39. pressed: {},
  40. down: {},
  41. };
  42. $(document).ready(function() {
  43. var imgs = Object.keys(img_to_load).length;
  44. var maxImgs = imgs;
  45. function loadcb() {
  46. imgs--;
  47. $('.loading-progress').css({width: ((maxImgs-imgs)*100)/maxImgs + '%'});
  48. if(imgs == 0) {
  49. // $('.loading').hide();
  50. $('.loading, .loadbar').hide();
  51. $('.play').show();
  52. waitingOnUser = true;
  53. }
  54. }
  55. for(var k in img_to_load) {
  56. images[k] = new Image();
  57. images[k].onload = loadcb;
  58. images[k].src = img_to_load[k];
  59. }
  60. // init input stuff
  61. $(document).keydown(function(e) {
  62. _inputdata.down[e.which] = 1;
  63. _inputdata.down[e.which] |= 0;
  64. e.preventDefault();
  65. });
  66. $(document).keyup(function(e) {
  67. _inputdata.down[e.which] = 0;
  68. _inputdata.pressed[e.which] = (_inputdata.pressed[e.which]>>>0) + 1;
  69. e.preventDefault();
  70. });
  71. });
  72. var extStop = false;
  73. var extCallback = function() {};
  74. function newgame(gm) {
  75. extCallback = function() {
  76. game_lvl = gm;
  77. extStop = false;
  78. extCallback = function() {};
  79. game(game_lvl);
  80. };
  81. extStop = true;
  82. }
  83. function game(hardness) {
  84. hardness = hardness || 5;
  85. var runGame = true;
  86. var w = $(window);
  87. var canvas = $('.scribble-canvas');
  88. var ctx = canvas[0].getContext("2d");
  89. ctx.drawBox = function(x,y, style, width) {
  90. this.beginPath();
  91. this.rect(x - 3, y - 3, 6, 6);
  92. if(!(width > 1)) width = 1;
  93. this.lineWidth = width;
  94. this.strokeStyle = style;
  95. this.stroke();
  96. }
  97. function checkinputs() {
  98. var curinput = {
  99. down: _inputdata.down,
  100. pressed: _inputdata.pressed,
  101. };
  102. _inputdata.pressed = {};
  103. // pressed: {},
  104. // down: {},
  105. // };
  106. return curinput;
  107. }
  108. // w.resize(function(){
  109. // canvas.attr({width: w.width(), height: w.height()});
  110. // });
  111. // w.trigger('resize');
  112. //
  113. window.requestAnimFrame = (function(){
  114. return window.requestAnimationFrame ||
  115. window.webkitRequestAnimationFrame ||
  116. window.mozRequestAnimationFrame ||
  117. window.oRequestAnimationFrame ||
  118. window.msRequestAnimationFrame ||
  119. function(callback){
  120. window.setTimeout(callback, 1000 / 30);
  121. };
  122. })();
  123. function frame(info){
  124. checkVictory();
  125. checkDefeat();
  126. checkInterrupt();
  127. // info = info || {};
  128. //info.ii
  129. var lol = checkinputs();
  130. // info.ii = lol;
  131. // console.log(lol);
  132. // // console.log(info.ii);
  133. draw(info, lol);
  134. if(runGame) window.requestAnimFrame(frame);
  135. }
  136. var gameinfo = {
  137. numUrchins: 1 + hardness,
  138. numKelps: (3 + hardness * 1.5)|0,
  139. };
  140. var hud = new HUD();
  141. var collisions = new Collisions();
  142. var splashText = new SplashText();
  143. var waves = new Waves();
  144. var mud = new Mud();
  145. var otters = [];
  146. var urchins = [];
  147. var kelps = [];
  148. var seaweeds = [];
  149. seaweeds.push( new Seaweed({}) );
  150. otters.push( new Otter({}) );
  151. for(var i = 0; i < gameinfo.numUrchins; i++) {
  152. urchins.push( new Urchin({position: pt(Math.random() * 900 + 50, 200)}) );
  153. }
  154. for(var i = 0; i < gameinfo.numKelps; i++) {
  155. kelps.push( new Kelp({position: pt(Math.random() * 900 + 50, 428)}) );
  156. }
  157. // boxen.push(new RoundedBox({}));
  158. collisions.subtractStaticRect(rect(0,450,0,1000));
  159. collisions.addActor(urchins);
  160. collisions.addActor(seaweeds);
  161. collisions.addActor(otters);
  162. collisions.addActor(kelps);
  163. hud.addWidget(new HUDHearts(otters[0]), 990 - (6*25), 20);
  164. hud.addWidget(new HUDBubbles(otters[0]), 10, 20);
  165. hud.addWidget(new HUDList({
  166. list: kelps,
  167. icon: images.kelp_icon,
  168. textOffset: pt(2, 35),
  169. imgScale: .7,
  170. }), 750, 10);
  171. hud.addWidget(new HUDList({
  172. list: urchins,
  173. icon: images.sea_urchin_icon,
  174. textOffset: pt(5, 30),
  175. imgScale: 1,
  176. }), 650, 15);
  177. var d = new Date();
  178. var lastframe = d.getTime();
  179. frame();
  180. function draw(info, ii) {
  181. var d = new Date();
  182. var now = d.getTime();
  183. // time elapsed (since last frame)
  184. var te = (now - lastframe) / 1000.0;
  185. lastframe = now;
  186. var w = ctx.canvas.width;
  187. var h = ctx.canvas.height;
  188. ctx.clearRect(0, 0, w, h);
  189. // console.log(ii.pressed);
  190. otters[0].uiDirection.x = 0;
  191. otters[0].uiDirection.y = 0;
  192. otters[0].uiAcc = false;
  193. if(ii.down[37]) {
  194. //console.log(ii);
  195. otters[0].uiDirection.x = -1;
  196. otters[0].uiAcc = true;
  197. }
  198. if(ii.down[39]) {
  199. //console.log(ii);
  200. otters[0].uiDirection.x = 1;
  201. otters[0].uiAcc = true;
  202. }
  203. if(ii.down[38]) {
  204. //console.log(ii);
  205. otters[0].uiDirection.y = -1;
  206. otters[0].uiAcc = true;
  207. }
  208. if(ii.down[40]) {
  209. //console.log(ii);
  210. otters[0].uiDirection.y = 1;
  211. otters[0].uiAcc = true;
  212. }
  213. if(ii.pressed[32]) {
  214. //console.log(ii);
  215. otters[0].takeBite();
  216. // otters[0].dropItem();
  217. }
  218. if(ii.down[32]) {
  219. //console.log(ii);
  220. otters[0].mouthOpen = true;
  221. // otters[0].dropItem();
  222. }
  223. else {
  224. otters[0].mouthOpen = false;
  225. }
  226. splashText.frameMove(te);
  227. waves.frameMove(te);
  228. mud.frameMove(te);
  229. // other shit here
  230. kelps.map(function(q) { q.frameMove(te) });
  231. urchins.map(function(q) { q.frameMove(te) });
  232. otters.map(function(q) { q.frameMove(te) });
  233. seaweeds.map(function(q) { q.frameMove(te) });
  234. collisions.checkActorCollisions();
  235. collisions.checkActorBounds();
  236. collisions.frameMove(te);
  237. hud.frameMove(te);
  238. splashText.render(ctx);
  239. waves.render(ctx);
  240. mud.render(ctx);
  241. // other shit here
  242. seaweeds.map(function(q) { q.render(ctx) });
  243. kelps.map(function(q) { q.render(ctx) });
  244. urchins.map(function(q) { q.render(ctx) });
  245. otters.map(function(q) { q.render(ctx) });
  246. hud.render(ctx);
  247. // console.log('lol');
  248. }
  249. function distance(from, to) {
  250. var x = to.x - from.x;
  251. var y = to.y - from.y;
  252. return Math.sqrt(x*x + y*y);
  253. }
  254. function checkVictory() {
  255. var urchinsAlive = 0;
  256. for(var i = 0; i < urchins.length; i++) {
  257. if(urchins[i].hp > 0) urchinsAlive++;
  258. }
  259. if(urchinsAlive == 0) win();
  260. }
  261. function checkDefeat() {
  262. var kelpsAlive = 0;
  263. for(var i = 0; i < kelps.length; i++) {
  264. if(kelps[i].hp > 0) kelpsAlive++;
  265. }
  266. var otterDead = false;
  267. if(otters[0].hp == 0) otterDead = true;
  268. if(otterDead || kelpsAlive == 0) lose();
  269. }
  270. function checkInterrupt() {
  271. if(extStop) {
  272. runGame = false;
  273. setTimeout(extCallback, 100);
  274. }
  275. }
  276. function win() {
  277. runGame = false;
  278. game_lvl++;
  279. waitingOnUser = true;
  280. $('.victory').show();
  281. }
  282. function lose() {
  283. runGame = false;
  284. waitingOnUser = true;
  285. $('.defeat').show();
  286. }
  287. };