You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cardgame.js 30 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038
  1. function initHexi(preloadImages) {
  2. //1. Setting up and starting Hexi
  3. //Initialize and start Hexi
  4. g = hexi(stageWidth, stageHeight, setup, preloadImages, load);
  5. g.fps = 60;
  6. g.border = "2px red dashed";
  7. g.backgroundColor = 0x000000;
  8. g.scaleToWindow();
  9. g.start();
  10. }
  11. //2. The `load` function that will run while your files are loading
  12. function load(){
  13. //Display the file currently being loaded
  14. console.log(`loading: ${g.loadingFile}`);
  15. //Display the percentage of files currently loaded
  16. console.log(`progress: ${g.loadingProgress}`);
  17. //Add an optional loading bar
  18. g.loadingBar();
  19. }
  20. //3. The `setup` function, which initializes your game objects, variables and sprites
  21. function setup() {
  22. //Create your game objects here
  23. let bg = g.sprite("assets/game/extra/battlemap6_middleground.png");
  24. bg.interactive = true;
  25. bg.on('click', bgClicked);
  26. bg.setPosition(0, 0);
  27. bg.width = stageWidth;
  28. bg.height = stageHeight;
  29. g.stage.addChild(bg);
  30. // Set up Methods for triggering actions based on pointer clicks
  31. //g.pointer.press = () => {console.log("The pointer was pressed "+g.pointer.x+" "+g.pointer.y);}
  32. //Add a custom `release` method
  33. //g.pointer.release = () => {console.log("The pointer was released");}
  34. //Add a custom `tap` method
  35. //g.pointer.tap = () => {console.log("The pointer was tapped");}
  36. //Set the game state to `play` to start the game loop
  37. g.state = play;
  38. }
  39. function bgClicked(eventData) {
  40. ws.send(JSON.stringify({
  41. messagetype: "otherclicked"
  42. }));
  43. }
  44. function drawTile(message) {
  45. var tileid = message.tile.tilex+"-"+message.tile.tiley;
  46. if (boardTiles.has(tileid)) {
  47. var tile = boardTiles.get(tileid);
  48. tile.show(message.mode);
  49. } else {
  50. var tile = g.sprite(message.tile.tileTextures);
  51. tile.setPosition(message.tile.xpos, message.tile.ypos);
  52. tile.width = message.tile.width;
  53. tile.height = message.tile.height;
  54. tile.tilex = message.tile.tilex;
  55. tile.tiley = message.tile.tiley;
  56. tile.show(message.mode);
  57. tile.interactive = true;
  58. tile.on('click', tileClicked);
  59. g.stage.addChild(tile);
  60. boardTiles.set(tileid, tile);
  61. }
  62. //console.log(message);
  63. }
  64. function tileClicked(eventData) {
  65. ws.send(JSON.stringify({
  66. messagetype: "tileclicked",
  67. tilex: eventData.target.tilex,
  68. tiley: eventData.target.tiley,
  69. }));
  70. }
  71. function drawCard(message) {
  72. var handIndex = message.position-1; // correct for indices starting from 0
  73. if (handContainers[handIndex]!=null) {
  74. // delete the container before drawing the new one
  75. g.stage.removeChild(handContainers[handIndex]);
  76. handContainers[handIndex] = null;
  77. }
  78. var cardContainer = new PIXI.Container();
  79. cardJSON[handIndex] = message.card;
  80. var backgroundCardImage = g.sprite(message.card.miniCard.cardTextures);
  81. backgroundCardImage.show(message.mode);
  82. backgroundCardImage.setPosition(0, 0);
  83. backgroundCardImage.width = 200;
  84. backgroundCardImage.height = 200;
  85. backgroundCardImage.cardindex = handIndex+1;
  86. backgroundCardImage.interactive = true;
  87. backgroundCardImage.on('click', cardClicked);
  88. cardContainer.addChild(backgroundCardImage);
  89. var cardSprite = g.sprite(message.card.miniCard.animationFrames);
  90. if (message.mode==0) {
  91. cardSprite.show(message.card.miniCard.index);
  92. } else {
  93. cardSprite.playAnimation();
  94. }
  95. cardSprite.setPosition(50, 50);
  96. cardSprite.width = 100;
  97. cardSprite.height = 100;
  98. cardContainer.addChild(cardSprite);
  99. handSprites[handIndex] = cardSprite;
  100. var cardNameBackground = g.sprite("assets/game/extra/ui/button_end_turn_enemy.png");
  101. cardNameBackground.setPosition(0, 140);
  102. cardNameBackground.width = 220;
  103. cardNameBackground.height = 50;
  104. cardContainer.addChild(cardNameBackground);
  105. var cardText = new PIXI.Text(message.card.cardname, { font: '18px Roboto', fill: 'white', align: 'center' });
  106. cardText.position.x = 30;
  107. cardText.position.y = 155;
  108. cardContainer.addChild(cardText);
  109. var manacircle = g.sprite("assets/game/extra/ManaCircle.png");
  110. manacircle.setPosition(120, 15);
  111. manacircle.width = 50;
  112. manacircle.height = 50;
  113. cardContainer.addChild(manacircle);
  114. var manaText = new PIXI.Text(message.card.manacost, { font: '25px Roboto', fill: 'white', align: 'center' });
  115. manaText.position.x = 138;
  116. manaText.position.y = 25;
  117. cardContainer.addChild(manaText);
  118. handContainers[handIndex] = cardContainer;
  119. cardContainer.position.x = 200+(handIndex*220);
  120. cardContainer.position.y = 880;
  121. g.stage.addChild(cardContainer);
  122. }
  123. function renderCardPreview(position) {
  124. if (cardPreview!=null) {
  125. g.stage.removeChild(cardPreview);
  126. cardPreview = null;
  127. }
  128. var card = cardJSON[position-1];
  129. var previewContainer = new PIXI.Container();
  130. var backgroundCardImage = g.sprite(card.bigCard.cardTextures);
  131. backgroundCardImage.show(0);
  132. backgroundCardImage.setPosition(0, 0);
  133. backgroundCardImage.width = 250;
  134. backgroundCardImage.height = 350;
  135. previewContainer.addChild(backgroundCardImage);
  136. var cardText = new PIXI.Text(card.cardname, { font: '20px Roboto', fill: 'white', align: 'center' });
  137. cardText.position.x = 30;
  138. cardText.position.y = 30;
  139. previewContainer.addChild(cardText);
  140. var cardSprite = g.sprite(card.miniCard.animationFrames);
  141. cardSprite.playAnimation();
  142. cardSprite.setPosition(75, 65);
  143. cardSprite.width = 100;
  144. cardSprite.height = 100;
  145. previewContainer.addChild(cardSprite);
  146. var manacircle = g.sprite("assets/game/extra/ManaCircle.png");
  147. manacircle.setPosition(190, 15);
  148. manacircle.width = 50;
  149. manacircle.height = 50;
  150. previewContainer.addChild(manacircle);
  151. var manaText = new PIXI.Text(card.manacost, { font: '25px Roboto', fill: 'white', align: 'center' });
  152. manaText.position.x = 210;
  153. manaText.position.y = 25;
  154. previewContainer.addChild(manaText);
  155. for (i = 0; i < card.bigCard.rulesTextRows.length; i++) {
  156. var line = card.bigCard.rulesTextRows[i];
  157. var rulesLine = new PIXI.Text(line, { font: '15px Roboto', fill: 'white', align: 'center' });
  158. rulesLine.position.x = 30;
  159. rulesLine.position.y = 250+(i*20);
  160. previewContainer.addChild(rulesLine);
  161. }
  162. if (card.bigCard.attack>-1) {
  163. var cardAttack = new PIXI.Text(card.bigCard.attack, { font: '20px Roboto', fill: 'white', align: 'center' });
  164. cardAttack.position.x = 60;
  165. cardAttack.position.y = 200;
  166. previewContainer.addChild(cardAttack);
  167. var cardHealth = new PIXI.Text(card.bigCard.health, { font: '20px Roboto', fill: 'white', align: 'center' });
  168. cardHealth.position.x = 180;
  169. cardHealth.position.y = 200;
  170. previewContainer.addChild(cardHealth);
  171. }
  172. previewContainer.position.x = 1600;
  173. previewContainer.position.y = 400;
  174. prevewCountdown = 300;
  175. g.stage.addChild(previewContainer);
  176. cardPreview = previewContainer;
  177. }
  178. function cardClicked(eventData) {
  179. renderCardPreview(eventData.target.cardindex);
  180. ws.send(JSON.stringify({
  181. messagetype: "cardclicked",
  182. position: eventData.target.cardindex
  183. }));
  184. }
  185. function drawUnit(message) {
  186. //console.log(message.unit);
  187. var unitContainer = new PIXI.Container();
  188. // Draw unit in idle stance
  189. var unit = g.sprite(message.unit.animations.allFrames);
  190. unit.playAnimation(message.unit.animations.idle.frameStartEndIndices);
  191. unit.loop = message.unit.animations.idle.loop;
  192. unit.fps = message.unit.animations.idle.fps;
  193. var spriteX = message.unit.position.xpos - message.unit.correction.spriteTopLeftX;
  194. var spriteY = message.unit.position.ypos - message.unit.correction.spriteTopLeftY-20;
  195. unit.setPosition(message.unit.correction.offsetX, message.unit.correction.offsetY);
  196. unit.width = message.unit.correction.imgWidth*(1+(message.unit.correction.spriteTopLeftX/message.unit.correction.imgWidth))*message.unit.correction.scale;
  197. unit.height = message.unit.correction.imgHeight*(1+(message.unit.correction.spriteTopLeftY/message.unit.correction.imgHeight))*message.unit.correction.scale;
  198. unit.gameID = message.unit.id;
  199. // if reflect, flip the unit sprite
  200. if (message.unit.correction.reflected) {
  201. unit.scale.x = -1*message.unit.correction.scale;
  202. unit.setPosition((message.unit.correction.imgWidth*message.unit.correction.scale)+message.unit.correction.offsetX, message.unit.correction.offsetY);
  203. }
  204. //unit.interactive = true;
  205. //unit.on('click', unitClicked);
  206. unitContainer.addChild(unit);
  207. // Draw attack value
  208. var attackcircle = g.sprite("assets/game/extra/AttackCircle.png");
  209. attackcircle.setPosition(message.unit.correction.spriteTopLeftX+5, message.unit.correction.spriteTopLeftY+message.tile.height-25);
  210. attackcircle.width = 40;
  211. attackcircle.height = 40;
  212. unitContainer.addChild(attackcircle);
  213. var attackText = new PIXI.Text('0', { font: '20px Roboto', fill: 'white', align: 'center' });
  214. attackText.position.x = message.unit.correction.spriteTopLeftX+20;
  215. attackText.position.y = message.unit.correction.spriteTopLeftY+message.tile.height-15;
  216. unitContainer.addChild(attackText);
  217. // Draw health value
  218. var healthcircle = g.sprite("assets/game/extra/HealthCircle.png");
  219. healthcircle.setPosition(message.unit.correction.spriteTopLeftX+message.tile.height-45, message.unit.correction.spriteTopLeftY+message.tile.height-25);
  220. healthcircle.width = 40;
  221. healthcircle.height = 40;
  222. unitContainer.addChild(healthcircle);
  223. var healthText = new PIXI.Text('0', { font: '20px Roboto', fill: 'white', align: 'center' });
  224. healthText.position.x = message.unit.correction.spriteTopLeftX+message.tile.height-30;
  225. healthText.position.y = message.unit.correction.spriteTopLeftY+message.tile.height-15;
  226. unitContainer.addChild(healthText);
  227. unitContainer.position.x = spriteX;
  228. unitContainer.position.y = spriteY;
  229. g.stage.addChild(unitContainer);
  230. spriteContainers.set(message.unit.id, unitContainer);
  231. sprites.set(message.unit.id, unit);
  232. healthLabels.set(message.unit.id, healthText);
  233. attackLabels.set(message.unit.id, attackText);
  234. }
  235. function getFrameSet(unit) {
  236. var frameSet = [];
  237. var anim = unit.animation;
  238. if (anim === "idle") {
  239. frameSet = unit.animations.idle;
  240. }
  241. if (anim === "death") {
  242. frameSet = unit.animations.death;
  243. }
  244. if (anim === "attack") {
  245. frameSet = unit.animations.attack;
  246. }
  247. if (anim === "move") {
  248. frameSet = unit.animations.move;
  249. }
  250. if (anim === "channel") {
  251. frameSet = unit.animations.channel;
  252. }
  253. if (anim === "hit") {
  254. frameSet = unit.animations.hit;
  255. }
  256. console.log(frameSet);
  257. return frameSet;
  258. }
  259. // Starts a move action for a Unit
  260. function moveUnit(unitID, xTile, yTile) {
  261. ws.send(JSON.stringify({
  262. messagetype: "getTileForMove",
  263. unitID: unitID,
  264. xTile: xTile,
  265. yTile: yTile
  266. }));
  267. }
  268. function moveUnitToTile(message) {
  269. activeMoves.set(message.unitID, message);
  270. }
  271. // Performs a single frame move towards the target destination for a sprite
  272. // Returns whether the destination has been reached
  273. function executeMoveStep(message) {
  274. var targetUnit = sprites.get(message.unit.id);
  275. var targetContainer = spriteContainers.get(message.unit.id);
  276. if (message.unit.animation != "move") {
  277. targetUnit.stopAnimation();
  278. ws.send(JSON.stringify({
  279. messagetype: "unitMoving",
  280. id: message.unit.id
  281. }));
  282. message.unit.animation = "move";
  283. targetUnit.fps = message.unit.animations.move.fps;
  284. targetUnit.loop = message.unit.animations.move.loop;
  285. targetUnit.playAnimation(message.unit.animations.move.frameStartEndIndices);
  286. //sprite.interactive = false;
  287. }
  288. //console.log(moveMessage)
  289. var spriteX = message.tile.xpos - message.unit.correction.spriteTopLeftX;
  290. var spriteY = message.tile.ypos - message.unit.correction.spriteTopLeftY-20;
  291. var dx = Math.abs(targetContainer.position.x - spriteX);
  292. var dy = Math.abs(targetContainer.position.y - spriteY);
  293. //console.log("d:"+dx+" "+dy);
  294. if ((dx+dy) > 0) {
  295. if (message.yfirst == true) {
  296. if (dy>0) {
  297. if (dy>0) {
  298. if (targetContainer.position.y - spriteY < 0) {
  299. targetContainer.position.vy = Math.min(moveVelocity,dy);
  300. targetContainer.position.vx = 0;
  301. } else {
  302. targetContainer.position.vy = Math.max(-moveVelocity,-dy);
  303. targetContainer.position.vx = 0;
  304. }
  305. }
  306. } else {
  307. if (targetContainer.position.x - spriteX < 0) {
  308. targetContainer.position.vx = Math.min(moveVelocity, dx);
  309. targetContainer.position.vy = 0;
  310. } else {
  311. targetContainer.position.vx = Math.max(-moveVelocity, -dx);
  312. targetContainer.position.vy = 0;
  313. }
  314. }
  315. } else {
  316. if (dx>0) {
  317. if (targetContainer.position.x - spriteX < 0) {
  318. targetContainer.position.vx = Math.min(moveVelocity, dx);
  319. targetContainer.position.vy = 0;
  320. } else {
  321. targetContainer.position.vx = Math.max(-moveVelocity, -dx);
  322. targetContainer.position.vy = 0;
  323. }
  324. } else {
  325. if (dy>0) {
  326. if (targetContainer.position.y - spriteY < 0) {
  327. targetContainer.position.vy = Math.min(moveVelocity,dy);
  328. targetContainer.position.vx = 0;
  329. } else {
  330. targetContainer.position.vy = Math.max(-moveVelocity,-dy);
  331. targetContainer.position.vx = 0;
  332. }
  333. }
  334. }
  335. }
  336. //console.log(targetContainer.position.x+" "+targetContainer.position.y);
  337. g.move(targetContainer.position);
  338. dx = Math.abs(targetContainer.position.x - spriteX);
  339. dy = Math.abs(targetContainer.position.y - spriteY);
  340. return false
  341. } else {
  342. var sprite = sprites.get(message.unit.id);
  343. sprite.stopAnimation();
  344. ws.send(JSON.stringify({
  345. messagetype: "unitstopped",
  346. id: message.unit.id,
  347. tilex: message.tile.tilex,
  348. tiley: message.tile.tiley
  349. }));
  350. message.unit.animation = "idle";
  351. targetUnit.fps = message.unit.animations.idle.fps;
  352. targetUnit.loop = message.unit.animations.idle.loop;
  353. targetUnit.playAnimation(message.unit.animations.idle.frameStartEndIndices);
  354. return true;
  355. }
  356. }
  357. function drawProjectile(message) {
  358. var projectile = g.sprite(message.effect.animationTextures);
  359. var effectX = message.tile.xpos - message.effect.correction.spriteTopLeftX;
  360. var effectY = message.tile.ypos - message.effect.correction.spriteTopLeftY;
  361. projectile.setPosition(effectX+message.effect.correction.offsetX, effectY+message.effect.correction.offsetY);
  362. projectile.width = message.effect.correction.imgWidth*(1+(message.effect.correction.spriteTopLeftX/message.effect.correction.imgWidth))*message.effect.correction.scale;
  363. projectile.height = message.effect.correction.imgHeight*(1+(message.effect.correction.spriteTopLeftY/message.effect.correction.imgHeight))*message.effect.correction.scale;
  364. projectile.show(message.mode);
  365. // if reflect, flip the unit sprite
  366. if (message.effect.correction.reflected) {
  367. projectile.scale.x = -1*message.effect.correction.scale;
  368. projectile.setPosition((message.effect.correction.imgWidth*message.effect.correction.scale)+message.effect.correction.offsetX, message.effect.correction.offsetY);
  369. }
  370. projectile.tile = message.tile;
  371. projectile.targetTile = message.targetTile;
  372. projectile.effect = message.effect;
  373. g.stage.addChild(projectile);
  374. activeProjectiles.push(projectile)
  375. }
  376. function executeProjectileMoveStep(projectile) {
  377. var tile = projectile.tile;
  378. var targetTile = projectile.targetTile;
  379. var effect = projectile.effect;
  380. var xvelocity = Math.abs(tile.tilex-targetTile.tilex)*2;
  381. var yvelocity = Math.abs(tile.tiley-targetTile.tiley)*2;
  382. var spriteX = targetTile.xpos - effect.correction.spriteTopLeftX+effect.correction.offsetX;
  383. var spriteY = targetTile.ypos - effect.correction.spriteTopLeftY+effect.correction.offsetY;
  384. var dx = Math.abs(projectile.position.x - spriteX);
  385. var dy = Math.abs(projectile.position.y - spriteY);
  386. //console.log("d:"+dx+" "+dy);
  387. if ((dx+dy) > 0) {
  388. //console.log(targetContainer.position.x+" "+targetContainer.position.y);
  389. if (dx>0) {
  390. if (projectile.position.x - spriteX < 0) {
  391. projectile.position.vx = Math.min(xvelocity, dx);
  392. } else {
  393. projectile.position.vx = Math.max(-xvelocity, -dx);
  394. }
  395. } else {
  396. projectile.position.vx = 0;
  397. }
  398. if (dy>0) {
  399. if (projectile.position.y - spriteY < 0) {
  400. projectile.position.vy = Math.min(yvelocity,dy);
  401. }else {
  402. projectile.position.vy = Math.max(-yvelocity,-dy);
  403. }
  404. } else {
  405. projectile.position.vy = 0;
  406. }
  407. g.move(projectile.position);
  408. dx = Math.abs(projectile.position.x - spriteX);
  409. dy = Math.abs(projectile.position.y - spriteY);
  410. return false
  411. } else {
  412. g.stage.removeChild(projectile);
  413. return true;
  414. }
  415. }
  416. function setUnitHealth(message) {
  417. var unitID = message.unit.id;
  418. var health = message.health;
  419. var oldHealth = parseInt(healthLabels.get(unitID).text);
  420. healthLabels.get(unitID).text = health;
  421. if (health>9 && oldHealth<10) {
  422. healthLabels.get(unitID).position.x = healthLabels.get(unitID).position.x-5;
  423. } else if (health<10 && oldHealth>9) {
  424. healthLabels.get(unitID).position.x = healthLabels.get(unitID).position.x+5;
  425. }
  426. }
  427. function setUnitAttack(message) {
  428. var unitID = message.unit.id;
  429. var attack = message.attack;
  430. var oldAttack = parseInt(attackLabels.get(unitID).text);
  431. attackLabels.get(unitID).text = attack;
  432. if (attack>9 && oldAttack<10) {
  433. attackLabels.get(unitID).position.x = attackLabels.get(unitID).position.x-5;
  434. } else if (attack<10 && oldAttack>9) {
  435. attackLabels.get(unitID).position.x = attackLabels.get(unitID).position.x+5;
  436. }
  437. }
  438. function renderPlayer1Card() {
  439. var icons = ["assets/game/extra/ui/icon_mana_inactive.png","assets/game/extra/ui/icon_mana.png"];
  440. var health = g.sprite("assets/game/extra/ui/notification_quest_small.png");
  441. health.setPosition(30, 250);
  442. health.width = 350;
  443. health.height = 110;
  444. g.stage.addChild(health);
  445. var healthTitle = new PIXI.Text('Life', { font: '28px Roboto', fill: 'white', align: 'center' });
  446. healthTitle.position.x = 120;
  447. healthTitle.position.y = 285;
  448. g.stage.addChild(healthTitle);
  449. player1Health = new PIXI.Text('0', { font: '28px Roboto', fill: 'white', align: 'center' });
  450. player1Health.position.x = 280;
  451. player1Health.position.y = 285;
  452. g.stage.addChild(player1Health);
  453. var mana1 = g.sprite(icons);
  454. mana1.setPosition(60, 400);
  455. mana1.width = 80;
  456. mana1.height = 80;
  457. mana1.show(0);
  458. g.stage.addChild(mana1);
  459. player1ManaIcons.set(1, mana1);
  460. var mana2 = g.sprite(icons);
  461. mana2.setPosition(60+50, 450);
  462. mana2.width = 80;
  463. mana2.height = 80;
  464. mana2.show(0);
  465. g.stage.addChild(mana2);
  466. player1ManaIcons.set(2, mana2);
  467. var mana3 = g.sprite(icons);
  468. mana3.setPosition(60, 500);
  469. mana3.width = 80;
  470. mana3.height = 80;
  471. mana3.show(0);
  472. g.stage.addChild(mana3);
  473. player1ManaIcons.set(3, mana3);
  474. var mana4 = g.sprite(icons);
  475. mana4.setPosition(60+50, 550);
  476. mana4.width = 80;
  477. mana4.height = 80;
  478. mana4.show(0);
  479. g.stage.addChild(mana4);
  480. player1ManaIcons.set(4, mana4);
  481. var mana5 = g.sprite(icons);
  482. mana5.setPosition(60, 600);
  483. mana5.width = 80;
  484. mana5.height = 80;
  485. mana5.show(0);
  486. g.stage.addChild(mana5);
  487. player1ManaIcons.set(5, mana5);
  488. var mana6 = g.sprite(icons);
  489. mana6.setPosition(60+50, 650);
  490. mana6.width = 80;
  491. mana6.height = 80;
  492. mana6.show(0);
  493. g.stage.addChild(mana6);
  494. player1ManaIcons.set(6, mana6);
  495. var mana7 = g.sprite(icons);
  496. mana7.setPosition(60, 700);
  497. mana7.width = 80;
  498. mana7.height = 80;
  499. mana7.show(0);
  500. g.stage.addChild(mana7);
  501. player1ManaIcons.set(7, mana7);
  502. var mana8 = g.sprite(icons);
  503. mana8.setPosition(60+50, 750);
  504. mana8.width = 80;
  505. mana8.height = 80;
  506. mana8.show(0);
  507. g.stage.addChild(mana8);
  508. player1ManaIcons.set(8, mana8);
  509. var mana9 = g.sprite(icons);
  510. mana9.setPosition(60, 800);
  511. mana9.width = 80;
  512. mana9.height = 80;
  513. mana9.show(0);
  514. g.stage.addChild(mana9);
  515. player1ManaIcons.set(9, mana9);
  516. var player1Portrait = g.sprite("assets/game/extra/ui/general_portrait_image_hex_f4-third@2x.png");
  517. player1Portrait.setPosition(65, 20);
  518. player1Portrait.width = 300;
  519. player1Portrait.height = 300;
  520. g.stage.addChild(player1Portrait);
  521. }
  522. function renderEndTurnButton() {
  523. var endTurnButton = g.sprite("assets/game/extra/ui/button_primary.png");
  524. endTurnButton.setPosition(1600, 950);
  525. endTurnButton.width = 300;
  526. endTurnButton.height = 100;
  527. endTurnButton.on('click', endturnClicked);
  528. endTurnButton.interactive = true;
  529. g.stage.addChild(endTurnButton);
  530. var endTurnText = new PIXI.Text('End Turn', { font: '28px Roboto', fill: 'white', align: 'center' });
  531. endTurnText.position.x = 1700;
  532. endTurnText.position.y = 980;
  533. g.stage.addChild(endTurnText);
  534. }
  535. function endturnClicked(eventData) {
  536. ws.send(JSON.stringify({
  537. messagetype: "endturnclicked"
  538. }));
  539. }
  540. function renderPlayer2Card() {
  541. var moveRight = 1500;
  542. var icons = ["assets/game/extra/ui/icon_mana_inactive.png","assets/game/extra/ui/icon_mana.png"];
  543. var health = g.sprite("assets/game/extra/ui/notification_quest_small.png");
  544. health.setPosition(moveRight+30, 250);
  545. health.width = 350;
  546. health.height = 110;
  547. g.stage.addChild(health);
  548. var healthTitle = new PIXI.Text('Life', { font: '28px Roboto', fill: 'white', align: 'center' });
  549. healthTitle.position.x = moveRight+120;
  550. healthTitle.position.y = 285;
  551. g.stage.addChild(healthTitle);
  552. player2Health = new PIXI.Text('0', { font: '28px Roboto', fill: 'white', align: 'center' });
  553. player2Health.position.x = moveRight+280;
  554. player2Health.position.y = 285;
  555. g.stage.addChild(player2Health);
  556. var mana1 = g.sprite(icons);
  557. mana1.setPosition(moveRight+200, 400);
  558. mana1.width = 80;
  559. mana1.height = 80;
  560. mana1.show(0);
  561. g.stage.addChild(mana1);
  562. player2ManaIcons.set(1, mana1);
  563. var mana2 = g.sprite(icons);
  564. mana2.setPosition(moveRight+200+50, 450);
  565. mana2.width = 80;
  566. mana2.height = 80;
  567. mana2.show(0);
  568. g.stage.addChild(mana2);
  569. player2ManaIcons.set(2, mana2);
  570. var mana3 = g.sprite(icons);
  571. mana3.setPosition(moveRight+200, 500);
  572. mana3.width = 80;
  573. mana3.height = 80;
  574. mana3.show(0);
  575. g.stage.addChild(mana3);
  576. player2ManaIcons.set(3, mana3);
  577. var mana4 = g.sprite(icons);
  578. mana4.setPosition(moveRight+200+50, 550);
  579. mana4.width = 80;
  580. mana4.height = 80;
  581. mana4.show(0);
  582. g.stage.addChild(mana4);
  583. player2ManaIcons.set(4, mana4);
  584. var mana5 = g.sprite(icons);
  585. mana5.setPosition(moveRight+200, 600);
  586. mana5.width = 80;
  587. mana5.height = 80;
  588. mana5.show(0);
  589. g.stage.addChild(mana5);
  590. player2ManaIcons.set(5, mana5);
  591. var mana6 = g.sprite(icons);
  592. mana6.setPosition(moveRight+200+50, 650);
  593. mana6.width = 80;
  594. mana6.height = 80;
  595. mana6.show(0);
  596. g.stage.addChild(mana6);
  597. player2ManaIcons.set(6, mana6);
  598. var mana7 = g.sprite(icons);
  599. mana7.setPosition(moveRight+200, 700);
  600. mana7.width = 80;
  601. mana7.height = 80;
  602. mana7.show(0);
  603. g.stage.addChild(mana7);
  604. player2ManaIcons.set(7, mana7);
  605. var mana8 = g.sprite(icons);
  606. mana8.setPosition(moveRight+200+50, 750);
  607. mana8.width = 80;
  608. mana8.height = 80;
  609. mana8.show(0);
  610. g.stage.addChild(mana8);
  611. player2ManaIcons.set(8, mana8);
  612. var mana9 = g.sprite(icons);
  613. mana9.setPosition(moveRight+200, 800);
  614. mana9.width = 80;
  615. mana9.height = 80;
  616. mana9.show(0);
  617. g.stage.addChild(mana9);
  618. player2ManaIcons.set(9, mana9);
  619. var player2Portrait = g.sprite("assets/game/extra/ui/general_portrait_image_hex_f1-third@2x.png");
  620. player2Portrait.setPosition(moveRight+65, 20);
  621. player2Portrait.width = 300;
  622. player2Portrait.height = 300;
  623. g.stage.addChild(player2Portrait);
  624. }
  625. function setPlayer1Health(message) {
  626. player1Health.text = message.player.health;
  627. }
  628. function setPlayer2Health(message) {
  629. player2Health.text = message.player.health;
  630. }
  631. function setPlayer1Mana(message) {
  632. var mana = message.player.mana;
  633. for (i = 1; i < 10; i++) {
  634. if (mana>=i) player1ManaIcons.get(i).show(1);
  635. else player1ManaIcons.get(i).show(0);
  636. }
  637. }
  638. function setPlayer2Mana(message) {
  639. var mana = message.player.mana;
  640. for (i = 1; i < 10; i++) {
  641. if (mana>=i) player2ManaIcons.get(i).show(1);
  642. else player2ManaIcons.get(i).show(0);
  643. }
  644. }
  645. function addPlayer1Notification(message) {
  646. if (player1Notification==null) {
  647. // we need to create a new notification
  648. player1Notification = g.sprite("assets/game/extra/ui/tooltip_left@2x.png");
  649. player1Notification.setPosition(320, 100);
  650. player1Notification.width = 800;
  651. player1Notification.height = 150;
  652. player1Notification.countdown = message.seconds*60;
  653. g.stage.addChild(player1Notification);
  654. player1NotificationText = new PIXI.Text(message.text, { font: '35px Roboto', fill: 'white', align: 'center' });
  655. player1NotificationText.position.x = 460;
  656. player1NotificationText.position.y = 150;
  657. g.stage.addChild(player1NotificationText);
  658. } else {
  659. player1Notification.countdown = message.seconds*60;
  660. player1NotificationText.text = message.text;
  661. }
  662. }
  663. function addPlayer2Notification(message) {
  664. if (player2Notification==null) {
  665. // we need to create a new notification
  666. player2Notification = g.sprite("assets/game/extra/ui/tooltip_right@2x.png");
  667. player2Notification.setPosition(320, 100);
  668. player2Notification.width = 800;
  669. player2Notification.height = 150;
  670. player2Notification.countdown = message.seconds*60;
  671. g.stage.addChild(player2Notification);
  672. player2NotificationText = new PIXI.Text(message.text, { font: '35px Roboto', fill: 'white', align: 'center' });
  673. player2NotificationText.position.x = 460;
  674. player2NotificationText.position.y = 150;
  675. g.stage.addChild(player2NotificationText);
  676. } else {
  677. player2Notification.countdown = message.seconds*60;
  678. player2NotificationText.text = message.text;
  679. }
  680. }
  681. function playUnitAnimation(message) {
  682. var targetUnit = sprites.get(message.unit.id);
  683. var animationData = getFrameSet(message.unit);
  684. targetUnit.loop = animationData.loop;
  685. targetUnit.fps = animationData.fps;
  686. targetUnit.playAnimation(animationData.frameStartEndIndices);
  687. }
  688. function deleteCard(message) {
  689. var card = handContainers[message.position-1];
  690. g.stage.removeChild(card);
  691. handContainers[message.position-1]=null;
  692. handSprites[message.position-1]=null;
  693. cardJSON[message.position-1]=null;
  694. prevewCountdown = 0;
  695. }
  696. function deleteUnit(message) {
  697. var unitContainer = spriteContainers.get(message.unit.id);
  698. g.stage.removeChild(unitContainer);
  699. spriteContainers.delete(message.unit.id);
  700. sprites.delete(message.unit.id);
  701. attackLabels.delete(message.unit.id);
  702. healthLabels.delete(message.unit.id);
  703. }
  704. function playEffectAnimation(message) {
  705. var effect = g.sprite(message.effect.animationTextures);
  706. var effectX = message.tile.xpos - message.effect.correction.spriteTopLeftX;
  707. var effectY = message.tile.ypos - message.effect.correction.spriteTopLeftY;
  708. effect.setPosition(effectX+message.effect.correction.offsetX, effectY+message.effect.correction.offsetY);
  709. effect.width = message.effect.correction.imgWidth*(1+(message.effect.correction.spriteTopLeftX/message.effect.correction.imgWidth))*message.effect.correction.scale;
  710. effect.height = message.effect.correction.imgHeight*(1+(message.effect.correction.spriteTopLeftY/message.effect.correction.imgHeight))*message.effect.correction.scale;
  711. effect.fps = message.effect.fps;
  712. effect.loop = false;
  713. effect.playAnimation();
  714. g.stage.addChild(effect);
  715. var frameDiff = (60/effect.fps)+1;
  716. effect.killCountdown = frameDiff*message.effect.animationTextures.length;
  717. console.log(effect);
  718. playingEffects.push(effect);
  719. }
  720. //4. The `play` function, which is your game or application logic that runs in a loop
  721. function play(){
  722. //This is your game loop, where you can move sprites and add your
  723. //game logic
  724. if (gameActorInitalized) {
  725. if (!gameStart) {
  726. ws.send(JSON.stringify({
  727. messagetype: "initalize"
  728. }));
  729. gameStart = true;
  730. renderPlayer1Card();
  731. renderPlayer2Card();
  732. renderEndTurnButton();
  733. }
  734. // Draw Tile Actions
  735. while (drawTileQueue.length>0) {
  736. drawTile(drawTileQueue.pop());
  737. }
  738. // Draw Tile Actions
  739. while (drawUnitQueue.length>0) {
  740. drawUnit(drawUnitQueue.pop());
  741. }
  742. var continuingProjectiles = [];
  743. for (i = 0; i < activeProjectiles.length; i++) {
  744. if(!executeProjectileMoveStep(activeProjectiles[i])) {
  745. continuingProjectiles.push(activeProjectiles[i]);
  746. }
  747. }
  748. activeProjectiles = continuingProjectiles;
  749. // Operationalize Sprite Movement
  750. var completedMoves = [];
  751. for (let [key, value] of activeMoves) {
  752. if (executeMoveStep(value)) {
  753. completedMoves.push(key);
  754. }
  755. }
  756. for (i = 0; i < completedMoves.length; i++) {
  757. activeMoves.delete(completedMoves[i]);
  758. }
  759. if (player1Notification!==null) {
  760. player1Notification.countdown = player1Notification.countdown-1;
  761. if (player1Notification.countdown<=0) {
  762. g.stage.removeChild(player1Notification);
  763. g.stage.removeChild(player1NotificationText);
  764. player1Notification = null;
  765. player1NotificationText = null;
  766. }
  767. }
  768. if (player2Notification!==null) {
  769. player2Notification.countdown = player2Notification.countdown-1;
  770. if (player2Notification.countdown<=0) {
  771. g.stage.removeChild(player2Notification);
  772. g.stage.removeChild(player2NotificationText);
  773. player2Notification = null;
  774. player2NotificationText = null;
  775. }
  776. }
  777. // Remove the card preview after the pre-determined time
  778. if (cardPreview!=null) {
  779. if (prevewCountdown<=0) {
  780. g.stage.removeChild(cardPreview);
  781. cardPreview = null;
  782. }
  783. prevewCountdown = prevewCountdown -1;
  784. }
  785. // tidy up animations that have finished playing
  786. for (i = 0; i < playingEffects.length; i++) {
  787. if (playingEffects[i].killCountdown<=0) {
  788. g.stage.removeChild(playingEffects[i]);
  789. playingEffects.splice(i, 1);
  790. break;
  791. }
  792. playingEffects[i].killCountdown = playingEffects[i].killCountdown-1;
  793. }
  794. sinceLastHeartbeat = sinceLastHeartbeat+1;
  795. if (sinceLastHeartbeat==120) {
  796. ws.send(JSON.stringify({
  797. messagetype: "heartbeat"
  798. }));
  799. sinceLastHeartbeat = 1;
  800. }
  801. }
  802. }