playState.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. extend(PlayState, TrinState);
  2. function PlayState(e, t) {
  3. PlayState.superclass.constructor.apply(this);
  4. this.levelPack = e;
  5. this.levelNum = t;
  6. this.level = e.levels[t];
  7. ProductItem.prototype.products = [];
  8. PlayState.prototype.CURRENT = this;
  9. this.productsLayer = null;
  10. this.starsLayer = null;
  11. this.hiddenStarsLayer = null;
  12. this.completeLayer = null;
  13. this.guiLayer = null;
  14. this.pauseLayer = null;
  15. this.tutorialPoints = null;
  16. this.currentTutorialPoint = 0;
  17. if (this.level.tutorial !== undefined) {
  18. this.tutorialPoints = this.level.tutorial.path
  19. }
  20. this.hand = null;
  21. this.handTimer = 10;
  22. PlayState.prototype.state = this.STATE_GAME;
  23. this.completeBack = null;
  24. this.completeCat = null;
  25. this.completeStars = [];
  26. this.completeBNext = null;
  27. this.completeBRestart = null;
  28. this.completeBMainmenu = null;
  29. this.catPaw = null;
  30. this.blood = null;
  31. this.pawLayer = null;
  32. this.hignLightLayer = null;
  33. this.hignLightRects = [];
  34. this.bPause = null;
  35. this.bSound = null;
  36. this.blank = null;
  37. PlayState.prototype.starsInstances = [];
  38. PlayState.prototype.stars = 0;
  39. this.a10Logo = null;
  40. this.a10LogoP = null;
  41. this.moreGames = null
  42. }
  43. PlayState.prototype.update = function() {
  44. ProductItem.prototype.pickedProduct = null;
  45. PlayState.superclass.update.apply(this);
  46. if (this.hand !== null) {
  47. if (this.handTimer-- < 0) {
  48. var e = this.tutorialPoints[this.currentTutorialPoint];
  49. if (Math.abs(this.hand.x - e.x) <= 4 && Math.abs(this.hand.y - e.y) <= 4) {
  50. this.currentTutorialPoint++;
  51. if (this.currentTutorialPoint >= this.tutorialPoints.length) {
  52. this.currentTutorialPoint = 0;
  53. this.handTimer = 10;
  54. this.hand.reset(this.tutorialPoints[0].x, this.tutorialPoints[0].y)
  55. }
  56. } else {
  57. this.hand.x += (e.x - this.hand.x) / (16 / this.level.tutorial.speed);
  58. this.hand.y += (e.y - this.hand.y) / (16 / this.level.tutorial.speed)
  59. }
  60. }
  61. var t = this.level.tutorial.offTriger;
  62. if (t !== undefined) {
  63. var n = _TrinGame.mouse;
  64. switch (t.type) {
  65. case "click":
  66. if (n.isPressed()) {
  67. var r = ProductItem.prototype.fieldOffset.x + t.cell.x * ProductItem.prototype.cellSize.x;
  68. var i = ProductItem.prototype.fieldOffset.y + t.cell.y * ProductItem.prototype.cellSize.y;
  69. if (n.x >= r && n.x <= r + ProductItem.prototype.cellSize.x && n.y >= i && n.y <= i + ProductItem.prototype.cellSize.y) {
  70. this.remove(this.hand);
  71. this.hand = null
  72. }
  73. }
  74. break
  75. }
  76. }
  77. }
  78. switch (this.state) {
  79. case this.STATE_GAME:
  80. break;
  81. case this.STATE_PAUSE:
  82. break;
  83. case this.STATE_PAW_START:
  84. if (_TrinGame.SPIL_API.isReady) {
  85. _TrinGame.SPIL_API.GameBreak.request(_TrinGame.globalPause, _TrinGame.globalResume)
  86. }
  87. this.catPaw = new TrinSprite;
  88. this.catPaw.addAnimationFromCache("CatPaw", true);
  89. this.catPaw.looped = false;
  90. this.catPaw.reset(644, ProductItem.prototype.fieldOffset.y + ProductItem.prototype.cellSize.y * 2 - 20);
  91. this.catPaw.orign.x = this.catPaw.width;
  92. this.pawLayer.add(this.catPaw);
  93. this.productsLayer.active = false;
  94. this.guiLayer.active = false;
  95. this.state = this.STATE_PAW;
  96. break;
  97. case this.STATE_PAW:
  98. this.catPaw.orign.x = this.catPaw.width;
  99. if (this.catPaw.currentFrame === 11) {
  100. this.blood = new TrinSprite;
  101. this.blood.addAnimationFromCache("Blood");
  102. this.blood.looped = false;
  103. this.blood.orign.x = this.blood.width;
  104. this.blood.reset(640, this.catPaw.y - 50);
  105. this.pawLayer.remove(this.catPaw);
  106. this.pawLayer.add(this.blood);
  107. this.pawLayer.add(this.catPaw)
  108. }
  109. if (this.catPaw.isFinished()) {
  110. this.state = this.STATE_WIN_START;
  111. this.catPaw.visible = false
  112. }
  113. break;
  114. case this.STATE_WIN_START:
  115. this.remove(this.a10Logo);
  116. Global.prototype.levels[this.levelPack.name][this.levelNum] = Math.max(Global.prototype.levels[this.levelPack.name][this.levelNum], this.stars);
  117. if (this.levelNum + 1 < Global.prototype.levels[this.levelPack.name].length) {
  118. if (Global.prototype.levels[this.levelPack.name][this.levelNum + 1] === -1) {
  119. Global.prototype.levels[this.levelPack.name][this.levelNum + 1] = 0
  120. }
  121. }
  122. Global.prototype.save();
  123. this.completeLayer = new TrinLayer;
  124. this.completeBack = new TrinSprite;
  125. this.completeBack.addAnimationFromCache("CompleteBack");
  126. this.completeBack.alpha = 0;
  127. this.completeCat = new TrinSprite;
  128. this.completeCat.addAnimationFromCache("CompleteCat");
  129. this.completeCat.orign.x = this.completeCat.width / 2;
  130. this.completeCat.orign.y = this.completeCat.height / 2;
  131. this.completeCat.reset(320, 960 + this.completeCat.height / 2);
  132. // updateShare(this.levelNum + 1);
  133. // Play68.setRankingScoreDesc(this.levelNum + 1);
  134. for (var s = 0; s < this.stars; s++) {
  135. var o = new TrinSprite;
  136. o.addAnimationFromCache("CompleteStar");
  137. o.orign.x = o.width / 2;
  138. o.orign.y = o.height / 2;
  139. o.scale.x = o.scale.y = 0;
  140. o.reset(170 + s * 150, 260 - s % 2 * 50);
  141. this.completeStars[s] = o
  142. }
  143. var u = function() {
  144. var e = arguments.callee.state;
  145. _TrinGame.switchState(new PlayState(e.levelPack, e.levelNum + 1))
  146. };
  147. if (this.levelNum + 1 >= Global.prototype.levels[this.levelPack.name].length) {
  148. u = function() {
  149. var e = arguments.callee.state;
  150. e.state = PlayState.prototype.STATE_CONGRATS;
  151. e.blank = new TrinRectObject(0, 0, 640, 960, "#FFFFFF");
  152. e.blank.alpha = 0;
  153. e.completeLayer.add(e.blank)
  154. }
  155. }
  156. u.state = this;
  157. this.completeBNext = new TrinButton(u, "bPlay", false);
  158. this.completeBNext.orign.x = this.completeBNext.width / 2;
  159. this.completeBNext.orign.y = this.completeBNext.height / 2;
  160. this.completeBNext.reset(320, 1e4);
  161. var a = function() {
  162. var e = arguments.callee.state;
  163. _TrinGame.switchState(new PlayState(e.levelPack, e.levelNum))
  164. };
  165. a.state = this;
  166. this.completeBRestart = new TrinButton(a, "bRestart", false);
  167. this.completeBRestart.orign.x = this.completeBRestart.width / 2;
  168. this.completeBRestart.orign.y = this.completeBRestart.height / 2;
  169. this.completeBRestart.reset(120, 1e4);
  170. var f = function() {
  171. _TrinGame.switchState(new LevelSelectState(arguments.callee.levelPack))
  172. };
  173. f.levelPack = this.levelPack;
  174. this.completeBMainMenu = new TrinButton(f, "bMainMenu", false);
  175. this.completeBMainMenu.orign.x = this.completeBMainMenu.width / 2;
  176. this.completeBMainMenu.orign.y = this.completeBMainMenu.height / 2;
  177. this.completeBMainMenu.reset(520, 1e4);
  178. var l = new TrinButton(function() {
  179. _TrinGame.SPIL_MOREGAMES.action()
  180. }, "bMoreGames", true);
  181. l.orign.x = l.width;
  182. l.reset(632, _TrinGame.visibleArea.top + 8);
  183. this.moreGames = l;
  184. this.completeLayer.add(this.completeBack);
  185. this.completeLayer.add(this.completeCat);
  186. for (s = 0; s < this.completeStars.length; s++) {
  187. this.completeLayer.add(this.completeStars[s])
  188. }
  189. this.completeLayer.add(this.completeBNext);
  190. this.completeLayer.add(this.completeBRestart);
  191. this.completeLayer.add(this.completeBMainMenu);
  192. this.completeLayer.add(this.moreGames);
  193. this.add(this.completeLayer);
  194. this.add(this.a10Logo);
  195. this.state = this.STATE_WIN_SHOWING_1;
  196. break;
  197. case this.STATE_WIN_SHOWING_1:
  198. this.completeBack.alpha = Math.min(this.completeBack.alpha + .05, 1);
  199. this.completeCat.y = 400 + (1 - this.completeBack.alpha) * (560 + this.completeCat.height / 2);
  200. this.completeBNext.y = this.completeCat.y + this.completeCat.height - 60;
  201. this.completeBRestart.y = this.completeBMainMenu.y = this.completeBNext.y;
  202. if (this.completeBack.alpha === 1) {
  203. this.remove(this.productsLayer);
  204. if (this.stars > 0) {
  205. this.state = this.STATE_WIN_SHOWING_2
  206. } else {
  207. this.state = this.STATE_WIN
  208. }
  209. }
  210. break;
  211. case this.STATE_WIN_SHOWING_2:
  212. for (var s = 0; s < this.completeStars.length; s++) {
  213. var o = this.completeStars[s];
  214. if (o.scale.x < 1) {
  215. o.scale.x = o.scale.y = Math.min(o.scale.x + .1, 1);
  216. break
  217. }
  218. }
  219. if (this.completeStars[this.completeStars.length - 1].scale.x === 1) {
  220. this.state = this.STATE_WIN
  221. }
  222. break;
  223. case this.STATE_WIN:
  224. break;
  225. case this.STATE_CONGRATS:
  226. this.blank.alpha = Math.min(this.blank.alpha + .1, 1);
  227. if (this.blank.alpha === 1) {
  228. _TrinGame.switchState(new CongratsState)
  229. }
  230. break
  231. }
  232. };
  233. PlayState.prototype.create = function() {
  234. PlayState.superclass.create.apply(this);
  235. this.productsLayer = new TrinLayer;
  236. this.starsLayer = new TrinLayer;
  237. this.hiddenStarsLayer = new TrinLayer;
  238. this.guiLayer = new TrinLayer;
  239. this.pauseLayer = new TrinLayer;
  240. this.hignLightLayer = new TrinLayer;
  241. this.pauseLayer.visible = this.pauseLayer.active = false;
  242. var e = new TrinSprite;
  243. e.addAnimationFromCache("GameBack");
  244. var t = 72;
  245. var n;
  246. if (this.level.name === "" || true) {
  247. n = "Level " + (this.levelNum + 1)
  248. } else {
  249. n = this.level.name
  250. } if (n.length > 8) {
  251. t = 72 - (n.length - 8) * 4
  252. }
  253. var r = new TrinText(n);
  254. r.setStyle("font", t, true, "#FFFFFF", "left", "bottom");
  255. r.x = 32;
  256. r.y = 240;
  257. var i = function() {
  258. var e = arguments.callee.state;
  259. e.pauseLayer.visible = e.pauseLayer.active = true;
  260. e.productsLayer.active = false;
  261. e.a10LogoP.active = true;
  262. e.a10Logo.active = false
  263. };
  264. i.state = this;
  265. var s = new TrinButton(i, "bPause", false);
  266. s.orign.x = s.width;
  267. s.reset(608, _TrinGame.visibleArea.top);
  268. this.bPause = s;
  269. var o = new SoundButton;
  270. o.orign.x = o.width;
  271. o.reset(508, _TrinGame.visibleArea.top);
  272. this.bSound = o;
  273. this.guiLayer.add(r);
  274. this.guiLayer.add(s);
  275. this.guiLayer.add(o);
  276. var u = function() {
  277. var e = arguments.callee.state;
  278. e.pauseLayer.visible = e.pauseLayer.active = false;
  279. e.productsLayer.active = true;
  280. e.a10LogoP.active = false;
  281. e.a10Logo.active = true
  282. };
  283. u.state = this;
  284. var a = new TrinSprite;
  285. a.addAnimationFromCache("PauseBack");
  286. var f = new TrinButton(u, "bPlay", false);
  287. f.orign.x = f.width / 2;
  288. f.orign.y = f.height / 2;
  289. f.reset(320, 530);
  290. var l = function() {
  291. var e = arguments.callee.state;
  292. _TrinGame.switchState(new PlayState(e.levelPack, e.levelNum))
  293. };
  294. l.state = this;
  295. var c = new TrinButton(l, "bRestart", false);
  296. c.orign.x = c.width / 2;
  297. c.orign.y = c.height / 2;
  298. c.reset(120, f.y);
  299. var h = function() {
  300. _TrinGame.switchState(new LevelSelectState(arguments.callee.levelPack))
  301. };
  302. h.levelPack = this.levelPack;
  303. var p = new TrinButton(h, "bMainMenu", false);
  304. p.orign.x = p.width / 2;
  305. p.orign.y = p.height / 2;
  306. p.reset(520, f.y);
  307. var d = new TrinButton(function() {}, "A10Logo", true);
  308. d.orign.y = d.height;
  309. d.reset(8, _TrinGame.visibleArea.bottom - 8);
  310. this.a10LogoP = d;
  311. this.a10LogoP.active = false;
  312. this.pauseLayer.add(a);
  313. this.pauseLayer.add(f);
  314. this.pauseLayer.add(c);
  315. this.pauseLayer.add(p);
  316. this.pauseLayer.add(d);
  317. var v = this.level.products;
  318. for (var m = 0; m < v.length; m++) {
  319. var g = v[m];
  320. var y = new ProductItem(g.name, g.x, g.y);
  321. this.productsLayer.add(y)
  322. }
  323. var b = this.level.stars;
  324. if (b !== undefined) {
  325. for (m = 0; m < b.length; m++) {
  326. var w = new GameStar(b[m].x, b[m].y);
  327. this.starsLayer.add(w);
  328. this.starsInstances[m] = w
  329. }
  330. }
  331. this.hignLightRects = [new TrinRectObject(0, 0, 0, 0, "87f043"), new TrinRectObject(0, 0, 0, 0, "87f043")];
  332. for (m = 0; m < 2; m++) {
  333. this.hignLightRects[m].alpha = .5;
  334. this.hignLightLayer.add(this.hignLightRects[m])
  335. }
  336. if (this.tutorialPoints !== undefined && this.tutorialPoints !== null && this.tutorialPoints.length > 0) {
  337. this.hand = new TrinSprite;
  338. this.hand.addAnimationFromCache("Hand");
  339. this.hand.orign.x = 44;
  340. this.hand.orign.x = 22;
  341. this.hand.reset(this.tutorialPoints[0].x, this.tutorialPoints[0].y)
  342. }
  343. this.pawLayer = new TrinLayer;
  344. var E = new TrinButton(function() {}, "A10Logo", true);
  345. E.orign.x = E.width;
  346. E.orign.y = E.height;
  347. E.reset(640, _TrinGame.visibleArea.bottom);
  348. this.a10Logo = E;
  349. this.add(e);
  350. this.add(this.guiLayer);
  351. this.add(this.hignLightLayer);
  352. this.add(this.pawLayer);
  353. this.add(this.hiddenStarsLayer);
  354. this.add(this.productsLayer);
  355. this.add(this.starsLayer);
  356. if (this.hand !== null) {
  357. this.add(this.hand)
  358. }
  359. this.add(this.a10Logo);
  360. this.add(this.pauseLayer)
  361. };
  362. PlayState.prototype.draw = function(e) {
  363. PlayState.superclass.draw.apply(this, [e])
  364. };
  365. PlayState.prototype.hignLight = function(e, t) {
  366. var n = this.hignLightRects[0];
  367. n.x = e.x;
  368. n.y = e.y;
  369. n.width = e.width;
  370. n.height = e.height;
  371. n.visible = true;
  372. var r = this.hignLightRects[1];
  373. r.x = t.x;
  374. r.y = t.y;
  375. r.width = t.width;
  376. r.height = t.height;
  377. r.visible = true
  378. };
  379. PlayState.prototype.unHignLight = function() {
  380. this.hignLightRects[0].visible = this.hignLightRects[1].visible = false
  381. };
  382. PlayState.prototype.resized = function() {
  383. this.bPause.y = this.bSound.y = _TrinGame.visibleArea.top;
  384. this.a10Logo.y = _TrinGame.visibleArea.bottom;
  385. this.a10LogoP.y = _TrinGame.visibleArea.bottom - 8;
  386. if (this.moreGames !== null) {
  387. this.moreGames.y = _TrinGame.visibleArea.top + 8
  388. }
  389. };
  390. PlayState.prototype.STATE_GAME = 0;
  391. PlayState.prototype.STATE_PAUSE = 1;
  392. PlayState.prototype.STATE_PAW_START = 7;
  393. PlayState.prototype.STATE_WIN_START = 2;
  394. PlayState.prototype.STATE_PAW = 3;
  395. PlayState.prototype.STATE_WIN_SHOWING_1 = 4;
  396. PlayState.prototype.STATE_WIN_SHOWING_2 = 5;
  397. PlayState.prototype.STATE_WIN = 6;
  398. PlayState.prototype.STATE_CONGRATS = 8;
  399. PlayState.prototype.state = PlayState.prototype.STATE_GAME;
  400. PlayState.prototype.starsInstances = [];
  401. PlayState.prototype.stars = 0;
  402. PlayState.prototype.CURRENT = null;