adventuregame.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. function AdventureGame() {
  2. var container = null;
  3. var canvas = null;
  4. var contextType = '2d';
  5. var contextAttributes = {};
  6. var context = null;
  7. var game = null;
  8. var heroes = [];
  9. var destinations = [];
  10. var minHeroes = 6;
  11. var minShops = 10;
  12. var minMonsters = 10;
  13. var minCoins = 10;
  14. var leaderboardElement = null;
  15. this.nameGenerator = new NameGenerator();
  16. this.create = function(elementName, leaderboard) {
  17. game = this;
  18. container = document.getElementById(elementName);
  19. canvas = container.getElementsByTagName('canvas')[0];
  20. window.addEventListener('resize', game.handleResize);
  21. canvas.addEventListener('mousemove', game.handleMouseMove);
  22. canvas.addEventListener('mousedown', game.handleMouseClick);
  23. canvas.addEventListener('contextmenu', game.handleMouseClick);
  24. canvas.addEventListener('touchstart', game.handleMouseClick);
  25. window.requestAnimationFrame(game.handleRequestAnimationFrame);
  26. this.handleResize(null);
  27. game.init(canvas);
  28. leaderboardElement = document.getElementById(leaderboard);
  29. };
  30. this.handleResize = function(event) {
  31. canvas.width = container.offsetWidth;
  32. canvas.height = container.offsetHeight;
  33. context = canvas.getContext(contextType, contextAttributes);
  34. context.translate(0.5, 0.5);
  35. };
  36. this.handleRequestAnimationFrame = function(event) {
  37. game.update(canvas);
  38. context.fillStyle = "#D3D3D3";
  39. context.fillRect(0 ,0, canvas.width, canvas.height);
  40. game.draw(context);
  41. window.requestAnimationFrame(game.handleRequestAnimationFrame);
  42. }
  43. this.handleMouseMove = function(event) {
  44. game.mouseMove(canvas, event.offsetX, event.offsetY);
  45. }
  46. this.handleMouseClick = function(event) {
  47. game.mouseClick(canvas, event.button, event.offsetX, event.offsetY);
  48. event.preventDefault();
  49. }
  50. this.init = function(canvas) {
  51. destinations = [];
  52. heroes = [];
  53. for(var i = 0; i < minHeroes; i++) {
  54. var newbie = new Hero();
  55. destinations.push(newbie);
  56. heroes.push(newbie);
  57. }
  58. for(var i = 0; i < 25; i++) {
  59. var x = canvas.width * Math.random() + 1;
  60. var y = canvas.height * Math.random() + 1;
  61. destinations.push(new CoinDestination(x, y, 0));
  62. }
  63. /*for(var i = 0; i < 25; i++) {
  64. var x = canvas.width * Math.random() + 1;
  65. var y = canvas.height * Math.random() + 1;
  66. destinations.push(new HeartDestination(x,y));
  67. }*/
  68. for(var i = 0; i < minMonsters; i++) {
  69. var x = canvas.width * Math.random() + 1;
  70. var y = canvas.height * Math.random() + 1;
  71. destinations.push(new MonsterDestination(x,y));
  72. }
  73. for(var i = 0; i < minShops; i++) {
  74. var x = canvas.width * Math.random() + 1;
  75. var y = canvas.height * Math.random() + 1;
  76. destinations.push(new ShopDestination(x,y));
  77. }
  78. for(var i = 0; i < destinations.length; i++) {
  79. destinations[i].init(canvas, this, i);
  80. }
  81. };
  82. this.update = function(canvas) {
  83. for(var i = 0, destination = destinations[0]; destination = destinations[i]; i++) {
  84. destination.update(canvas);
  85. }
  86. for(var i = destinations.length - 1; i >= 0; i--) {
  87. var destination = destinations[i];
  88. if(destination.isConsumed) {
  89. destinations.splice(i, 1);
  90. }
  91. }
  92. var heroCount = 0;
  93. var coinCount = 0;
  94. var monsterCount = 0;
  95. //heroes = [];
  96. for(var i = 0, destination = destinations[0]; destination = destinations[i]; i++) {
  97. switch(destination.getType()) {
  98. case "hero":
  99. heroCount++;
  100. //heroes.push(destination);
  101. break;
  102. case "coin":
  103. coinCount++;
  104. break;
  105. case "monster":
  106. monsterCount++;
  107. break;
  108. }
  109. }
  110. if(heroCount < minHeroes) {
  111. var newbie = new Hero();
  112. destinations.push(newbie);
  113. newbie.init(canvas, this, destinations.length);
  114. heroes.push(newbie);
  115. }
  116. if(monsterCount < minMonsters) {
  117. var x = canvas.width * Math.random() + 1;
  118. var y = canvas.height * Math.random() + 1;
  119. var mob = new MonsterDestination(x, y);
  120. destinations.push(mob);
  121. mob.init(canvas, this, destinations.length);
  122. }
  123. if(coinCount < minCoins) {
  124. var x = canvas.width * Math.random() + 1;
  125. var y = canvas.height * Math.random() + 1;
  126. var coin = new CoinDestination(x, y, 0);
  127. destinations.push(coin);
  128. coin.init(canvas, this, destinations.length);
  129. }
  130. leaderboardElement.innerHTML = "";
  131. leaderboard = [];
  132. for(var i = 0, hero = heroes[0]; hero = heroes[i]; i++) {
  133. //hero.addToLeaderboard(leaderboardElement);
  134. leaderboard.push(hero.getInfo());
  135. }
  136. leaderboard = leaderboard.sort(compare);
  137. for(var i = 0; i < leaderboard.length; i++) {
  138. displayBoard(leaderboard[i]);
  139. }
  140. };
  141. var displayBoard = function(row) {
  142. var topInfo = "level " + row.level + " " + row.name + " " + row.inventory.hearts + "/" + row.inventory.maxHearts;
  143. var info = "coins:" + row.inventory.coins + " xp:" + row.inventory.xp + " dmg:" + row.inventory.weaponStrength;
  144. var topNode = document.createTextNode(topInfo);
  145. var infoNode = document.createTextNode(info);
  146. var topLine = document.createElement('p');
  147. topLine.style.color = row.heroColor;
  148. var infoLine = document.createElement('p');
  149. infoLine.style.color = row.heroColor;
  150. topLine.appendChild(topNode);
  151. infoLine.appendChild(infoNode);
  152. leaderboardElement.appendChild(topLine);
  153. leaderboardElement.appendChild(infoLine);
  154. };
  155. var compare = function(a,b) {
  156. if (a.inventory.xp < b.inventory.xp) {
  157. return 1;
  158. }
  159. if (a.inventory.xp > b.inventory.xp) {
  160. return -1;
  161. }
  162. return 0;
  163. }
  164. this.draw = function(context) {
  165. for(var i = 0, destination = destinations[0]; destination = destinations[i]; i++) {
  166. destination.draw(context);
  167. }
  168. };
  169. this.mouseMove = function(canvas, x, y) {
  170. for(var i = 0, destination = destinations[0]; hero = destinations[i]; i++) {
  171. destination.mouseMove(canvas, x, y);
  172. }
  173. };
  174. this.mouseClick = function(canvas, button, x, y) {
  175. var newDest = null;
  176. if(button == 0) {
  177. game.spawnCoins({x: x, y: y}, Math.floor(25 * Math.random() + 1));
  178. } else if (button == 1) {
  179. newDest = new MonsterDestination(x, y);
  180. destinations.push(newDest);
  181. newDest.init(canvas, this);
  182. } else {
  183. newDest = new HeartDestination(x, y);
  184. destinations.push(newDest);
  185. newDest.init(canvas, this);
  186. }
  187. };
  188. this.getNewDestination = function() {
  189. var destinationLength = destinations.length;
  190. if(destinationLength == 0) {
  191. return;
  192. }
  193. var index = Math.floor(destinations.length * Math.random());
  194. var destination = destinations[index];
  195. return destination;
  196. }
  197. this.getVisibleDestinations = function(x, y, viewRange) {
  198. var visibleDestinations = [];
  199. for(var i = 0, destination = destinations[0]; destination = destinations[i]; i++) {
  200. var destPos = destination.getPosition();
  201. if(destPos.x >= x - viewRange && destPos.x <= x + viewRange &&
  202. destPos.y >= y - viewRange && destPos.y <= y + viewRange) {
  203. visibleDestinations.push(destination);
  204. }
  205. }
  206. return shuffle(visibleDestinations);
  207. }
  208. var shuffle = function(collection) {
  209. for (var i = collection.length - 1; i > 0; i--) {
  210. var temp = Math.floor(Math.random() * (i + 1));
  211. [collection[i], collection[temp]] = [collection[temp], collection[i]];
  212. }
  213. return collection;
  214. }
  215. this.getAllDestinations = function() {
  216. return shuffle(destinations);
  217. }
  218. this.heroDies = function(deadHero) {
  219. for(var i = destinations.length - 1; i >= 0; i--) {
  220. var hero = destinations[i];
  221. if(hero == deadHero) {
  222. destinations.splice(i, 1);
  223. }
  224. }
  225. for(var i = heroes.length - 1; i >= 0; i--) {
  226. var hero = heroes[i];
  227. if(hero == deadHero) {
  228. heroes.splice(i, 1);
  229. }
  230. }
  231. }
  232. this.spawnCoins = function(position, amount) {
  233. var coinsRemaining = amount;
  234. var magnitude = 1;
  235. var bounds = 50;
  236. while(coinsRemaining > 0) {
  237. var spawn = coinsRemaining % Math.pow(10, magnitude) / Math.pow(10, magnitude - 1);
  238. for(var i = 0; i < spawn; i++) {
  239. var coinX = Math.floor(2 * bounds * Math.random() + position.x - bounds);
  240. var coinY = Math.floor(2 * bounds * Math.random() + position.y - bounds);
  241. var coin = new CoinDestination(coinX, coinY, magnitude - 1);
  242. destinations.push(coin);
  243. coin.init(canvas, this);
  244. }
  245. coinsRemaining -= spawn * Math.pow(10, magnitude - 1);
  246. magnitude++;
  247. }
  248. }
  249. /*this.spawnCoins = function(position, amount) {
  250. var level2Coins = Math.floor(amount / 100);
  251. var level0Coins = amount - (level2Coins * 100);
  252. var bounds = 50;
  253. for(var i = 0; i < level2Coins; i++) {
  254. var coinX = Math.floor(2 * bounds * Math.random() + position.x - bounds);
  255. var coinY = Math.floor(2 * bounds * Math.random() + position.y - bounds);
  256. var coin = new CoinDestination(coinX, coinY, 2);
  257. destinations.push(coin);
  258. coin.init(canvas, this);
  259. }
  260. for(var i = 0; i < level0Coins; i++) {
  261. var coinX = Math.floor(2 * bounds * Math.random() + position.x - bounds);
  262. var coinY = Math.floor(2 * bounds * Math.random() + position.y - bounds);
  263. var coin = new CoinDestination(coinX, coinY, 0);
  264. destinations.push(coin);
  265. coin.init(canvas, this);
  266. }
  267. }*/
  268. this.spawnHeart = function(x, y) {
  269. var bounds = 50;
  270. var heartX = Math.floor(2 * bounds * Math.random() + x - bounds);
  271. var heartY = Math.floor(2 * bounds * Math.random() + y - bounds);
  272. var heart = new HeartDestination(heartX, heartY);
  273. destinations.push(heart);
  274. heart.init(canvas, this);
  275. }
  276. this.getAveragePlayerLevel = function() {
  277. var average = 0;
  278. for(var i = 0, hero = heroes[0]; hero = heroes[i]; i++) {
  279. average += hero.getLevel();
  280. }
  281. return Math.round(average / heroes.length);
  282. }
  283. this.getMaxPlayerLevel = function() {
  284. var max = 0;
  285. for(var i = 0, hero = heroes[0]; hero = heroes[i]; i++) {
  286. if(hero.getLevel() > max) {
  287. max = hero.getLevel();
  288. }
  289. }
  290. return max;
  291. }
  292. this.getMinPlayerLevel = function() {
  293. var min = 99;
  294. for(var i = 0, hero = heroes[0]; hero = heroes[i]; i++) {
  295. if(hero.getLevel() < min) {
  296. min = hero.getLevel();
  297. }
  298. }
  299. return min;
  300. }
  301. }