world_classic_selection.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /* allows the user to select:
  2. * between 2-6 players
  3. * who is player and who is AI
  4. * the size of the stage
  5. */
  6. class WorldClassicSelection extends World {
  7. /* this worlds assets */
  8. assets() {
  9. return [
  10. "images/stage_empty.png",
  11. "images/button_up.png",
  12. "images/button_down.png",
  13. ];
  14. }
  15. /* get mode from parent world */
  16. constructor(data) {
  17. super(data);
  18. /* either "classic" or "time" */
  19. this.mode = data.mode;
  20. } /* constructor */
  21. /* initialize */
  22. start() {
  23. /* data */
  24. this.max_players = 2;
  25. this.ai = [];
  26. this.returner = {};
  27. this.returner.ai = this.ai;
  28. this.returner.stage_x = 3;
  29. this.returner.stage_y = 3;
  30. /* everyone is initialized as player */
  31. for (let i = 0; i < this.max_players; i++) {
  32. this.ai.push(false);
  33. }
  34. /* text style */
  35. let style = new PIXI.TextStyle({
  36. fill: "#ff0000",
  37. fontSize: 30,
  38. });
  39. /* player buttons */
  40. this.button_players = [];
  41. this.text_players = [];
  42. /* add buttons in row, then calculate their position */
  43. for (let i = 0; i < this.max_players; i++) {
  44. this.add_player_button();
  45. }
  46. this.relocate_buttons();
  47. /* update player text */
  48. this.update_text();
  49. /* add player button */
  50. this.button_add = new PIXI.Sprite(
  51. loader.resources["images/button_up.png"].texture
  52. );
  53. this.button_add.anchor = {x:0.5, y:0.5};
  54. this.button_add.x = width*2/3;
  55. this.button_add.y = height/3;
  56. app.stage.addChild(this.button_add);
  57. /* remove player button */
  58. this.button_remove = new PIXI.Sprite(
  59. loader.resources["images/button_down.png"].texture
  60. );
  61. this.button_remove.anchor = {x:0.5, y:0.5};
  62. this.button_remove.x = width/3;
  63. this.button_remove.y = height/3;
  64. app.stage.addChild(this.button_remove);
  65. /* add bigger stage buttons */
  66. this.button_bigX = new PIXI.Sprite(
  67. loader.resources["images/button_up.png"].texture
  68. );
  69. this.button_bigX.anchor = {x:0.5, y:0.5};
  70. this.button_bigX.x = width*2/3;
  71. this.button_bigX.y = height*2/3;
  72. app.stage.addChild(this.button_bigX);
  73. this.button_bigY = new PIXI.Sprite(
  74. loader.resources["images/button_up.png"].texture
  75. );
  76. this.button_bigY.anchor = {x:0.5, y:0.5};
  77. this.button_bigY.x = width*4/5;
  78. this.button_bigY.y = height*2/3;
  79. app.stage.addChild(this.button_bigY);
  80. /* add smaller stage buttons */
  81. this.button_smallX = new PIXI.Sprite(
  82. loader.resources["images/button_down.png"].texture
  83. );
  84. this.button_smallX.anchor = {x:0.5, y:0.5};
  85. this.button_smallX.x = width/3;
  86. this.button_smallX.y = height*2/3;
  87. app.stage.addChild(this.button_smallX);
  88. this.button_smallY = new PIXI.Sprite(
  89. loader.resources["images/button_down.png"].texture
  90. );
  91. this.button_smallY.anchor = {x:0.5, y:0.5};
  92. this.button_smallY.x = width/5;
  93. this.button_smallY.y = height*2/3;
  94. app.stage.addChild(this.button_smallY);
  95. /* add stage size text */
  96. this.text_size = new PIXI.Text(
  97. "hello world", {
  98. fill: "#ff0000",
  99. });
  100. this.text_size.anchor = {x:0.5, y:0.5};
  101. this.text_size.x = width/2;
  102. this.text_size.y = this.button_bigX.y;
  103. app.stage.addChild(this.text_size);
  104. this.update_size();
  105. /* start game button */
  106. this.button_start = new PIXI.Sprite(
  107. loader.resources["images/stage_empty.png"].texture
  108. );
  109. this.button_start.anchor = {x:0.5, y:0.5};
  110. this.button_start.x = width/2;
  111. this.button_start.y = height*4/5;
  112. app.stage.addChild(this.button_start);
  113. } /* start */
  114. /* handle button touch */
  115. update() {
  116. /* handle input */
  117. for (let e = 0; e < input.length; e++) {
  118. /* left click */
  119. if (input[e].which == 1) {
  120. /* click point */
  121. let p = new PIXI.Point(
  122. input[e].offsetX, input[e].offsetY
  123. );
  124. /* clicked one of player buttons
  125. * change between player and AI
  126. */
  127. for (let i = 0; i < this.button_players.length; i++) {
  128. if (this.button_players[i].containsPoint(p)) {
  129. this.ai[i] = !this.ai[i];
  130. this.update_text();
  131. }
  132. }
  133. /* add new player - max 6 */
  134. if (this.button_add.containsPoint(p)) {
  135. if (this.max_players < 6) {
  136. this.max_players++;
  137. this.add_player_button();
  138. this.relocate_buttons();
  139. this.ai.push(false);
  140. this.update_text();
  141. }
  142. }
  143. /* remove player - min 2 */
  144. if (this.button_remove.containsPoint(p)) {
  145. if (this.max_players > 2) {
  146. this.max_players--;
  147. this.remove_player_button();
  148. this.relocate_buttons();
  149. this.ai.pop();
  150. }
  151. }
  152. /* bigger stage x */
  153. if (this.button_bigX.containsPoint(p)) {
  154. if (this.returner.stage_x < 12) {
  155. this.returner.stage_x++;
  156. this.update_size();
  157. }
  158. }
  159. /* bigger stage y */
  160. if (this.button_bigY.containsPoint(p)) {
  161. if (this.returner.stage_y < 12) {
  162. this.returner.stage_y++;
  163. this.update_size();
  164. }
  165. }
  166. /* smaller stage x */
  167. if (this.button_smallX.containsPoint(p)) {
  168. if (this.returner.stage_x > 3) {
  169. this.returner.stage_x--;
  170. this.update_size();
  171. }
  172. }
  173. /* smaller stage y */
  174. if (this.button_smallY.containsPoint(p)) {
  175. if (this.returner.stage_y > 3) {
  176. this.returner.stage_y--;
  177. this.update_size();
  178. }
  179. }
  180. /* start game */
  181. if (this.button_start.containsPoint(p)) {
  182. this.returner.max_players = this.max_players;
  183. this.returner.mode = this.mode;
  184. return world_list.indexOf(WorldTicTacToe);
  185. }
  186. } /* left click */
  187. } /* handle input */
  188. } /* update */
  189. /* for each player update its text */
  190. update_text() {
  191. for (let i = 0; i < this.ai.length; i++) {
  192. if (!this.ai[i]) {
  193. this.text_players[i].text = "player";
  194. }
  195. else {
  196. this.text_players[i].text = "computer";
  197. }
  198. }
  199. } /* update text */
  200. /* update stage size */
  201. update_size() {
  202. this.text_size.text =
  203. this.returner.stage_x +"x"
  204. +this.returner.stage_y;
  205. } /* update stage size */
  206. /* add player button in array
  207. * must call "relocate" afterwards to
  208. * place them all in correct position
  209. */
  210. add_player_button() {
  211. /* text style */
  212. let style = new PIXI.TextStyle({
  213. fill: "#ff0000",
  214. fontSize: 30,
  215. });
  216. /* button */
  217. let button = new PIXI.Sprite(
  218. loader.resources["images/stage_empty.png"].texture
  219. );
  220. button.anchor = {x:0.5, y:0.5};
  221. app.stage.addChild(button);
  222. this.button_players.push(button);
  223. /* text */
  224. let text = new PIXI.Text(
  225. "playerxxxx", style);
  226. text.anchor = {x:0.5, y:0.5};
  227. app.stage.addChild(text);
  228. this.text_players.push(text);
  229. } /* add player button */
  230. /* remove player button,
  231. * must call relocate afterwards
  232. */
  233. remove_player_button() {
  234. app.stage.removeChild(
  235. this.button_players[this.button_players.length-1]
  236. );
  237. app.stage.removeChild(
  238. this.text_players[this.text_players.length-1]
  239. );
  240. this.button_players.pop();
  241. this.text_players.pop();
  242. } /* remove player button */
  243. /* places all player buttons in the correct position */
  244. relocate_buttons() {
  245. for (let i = 0; i < this.max_players; i++) {
  246. /* button */
  247. let button = this.button_players[i];
  248. button.x = width*(i+1)/(this.max_players+1);
  249. button.y = 50;
  250. /* text */
  251. let text = this.text_players[i];
  252. text.x = button.x;
  253. text.y = button.y;
  254. }
  255. } /* relocate buttons */
  256. } /* world classic selection */