not really known
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.

508 lines
12 KiB

  1. function Game(stage,xocolor,doc,datastore,activity){
  2. this.margin = 0;
  3. //These must be even
  4. this.gridwidth = 10;
  5. this.gridheight = 6;
  6. this.circleswidth = 0;
  7. this.circlesheight = 0;
  8. this.radius = 0;
  9. console.log(xocolor);
  10. this.verticalline = null;
  11. this.horizontalline = null;
  12. this.mode = null;
  13. this.dotsarr = [];
  14. this.buddy = ["#FFFFFF","#000000",xocolor.fill,xocolor.stroke];
  15. this.rainbow = ['#FFFFFF','#000000','#FF0000','#FF8000','#FFFF00','#00FF00','#00FFFF','#0000FF','#FF00FF'];
  16. this.colours = this.buddy;
  17. this.robot = false;
  18. this.gameOver = false;
  19. //0 = horizontal, 1 = vertical, 2 = bilateral
  20. //Helper functions
  21. this.radiusFromX = function(){
  22. this.margin = 1/50*stage.canvas.width;
  23. var diameter = (stage.canvas.width-(this.margin*(this.gridwidth+1)))/this.gridwidth;
  24. var radius = diameter/2;
  25. return radius;
  26. }
  27. this.radiusFromY = function(){
  28. this.margin = 1/50*stage.canvas.height;
  29. var diameter = (stage.canvas.height-(this.margin*(this.gridheight+1)))/this.gridheight;
  30. var radius = diameter/2;
  31. return radius;
  32. }
  33. this.canDoFromX = function(){
  34. var rad = this.radiusFromX();
  35. this.margin = 1/50*stage.canvas.width;
  36. if ((((rad*2)*this.gridheight)+(this.margin*(this.gridheight+1)))<=stage.canvas.height){
  37. return rad;
  38. } else {
  39. return false;
  40. }
  41. }
  42. this.removeLines = function(){
  43. if (this.verticalline!=null){
  44. stage.removeChild(this.verticalline);
  45. this.verticalline = null;
  46. }
  47. if (this.horizontalline!=null){
  48. stage.removeChild(this.horizontalline);
  49. this.horizontalline = null;
  50. }
  51. }
  52. this.addVerticalLine = function(){
  53. this.verticalline = new createjs.Shape();
  54. this.verticalline.graphics.beginFill("#000000").drawRect(0,0,this.margin/3,this.circlesheight-this.margin);
  55. this.verticalline.x = stage.canvas.width/2-this.margin/6;
  56. this.verticalline.y = this.margin/2;
  57. stage.addChild(this.verticalline);
  58. }
  59. this.addHorizontalLine = function(){
  60. this.horizontalline = new createjs.Shape();
  61. this.horizontalline.graphics.beginFill("#000000").drawRect(0,0,this.circleswidth-this.margin,this.margin/3);
  62. this.horizontalline.x = (stage.canvas.width-this.circleswidth)/2+this.margin/2;
  63. this.horizontalline.y = this.circlesheight/2-this.margin/6;
  64. stage.addChild(this.horizontalline);
  65. }
  66. //Game Inits
  67. this.initHorizontalGame = function(colours){
  68. if (colours === undefined) colours=false;
  69. this.gameOver = false;
  70. this.removeLines();
  71. this.addVerticalLine();
  72. this.mode = 0;
  73. if (colours==false){
  74. this.initDots();
  75. } else {
  76. this.initDotsFromSave(colours);
  77. }
  78. }
  79. this.initVerticalGame = function(colours){
  80. if (colours === undefined) colours=false;
  81. this.gameOver = false;
  82. this.removeLines();
  83. this.addHorizontalLine();
  84. this.mode = 1;
  85. if (colours==false){
  86. this.initDots();
  87. } else {
  88. this.initDotsFromSave(colours);
  89. }
  90. }
  91. this.initBilateralGame = function(colours){
  92. if (colours === undefined) colours=false;
  93. this.gameOver = false;
  94. this.removeLines();
  95. this.addHorizontalLine();
  96. this.addVerticalLine();
  97. this.mode = 2;
  98. if (colours==false){
  99. this.initDots();
  100. } else {
  101. this.initDotsFromSave(colours);
  102. }
  103. }
  104. this.setBuddyStyle = function(){
  105. var buddyicon = doc.getElementById("buddy-button");
  106. buddyicon.style.backgroundColor = "#808080";
  107. var rainbowicon = doc.getElementById("rainbow-button");
  108. rainbowicon.removeAttribute("style");
  109. }
  110. this.initBuddy = function(){
  111. this.setBuddyStyle();
  112. this.colours = this.buddy;
  113. switch(this.mode) {
  114. case 0:
  115. this.initHorizontalGame();
  116. break;
  117. case 1:
  118. this.initVerticalGame();
  119. break;
  120. case 2:
  121. this.initBilateralGame();
  122. break;
  123. }
  124. }
  125. this.setRainbowStyle = function(){
  126. var buddyicon = doc.getElementById("buddy-button");
  127. buddyicon.removeAttribute("style");
  128. var rainbowicon = doc.getElementById("rainbow-button");
  129. rainbowicon.style.backgroundColor = "#808080";
  130. }
  131. this.initRainbow = function(){
  132. this.setRainbowStyle();
  133. this.colours = this.rainbow;
  134. switch(this.mode) {
  135. case 0:
  136. this.initHorizontalGame();
  137. break;
  138. case 1:
  139. this.initVerticalGame();
  140. break;
  141. case 2:
  142. this.initBilateralGame();
  143. break;
  144. }
  145. }
  146. this.initDots = function(){
  147. this.dotsarr = [];
  148. var temparr = [];
  149. var incr = (this.radius*2+this.margin);
  150. var xp = (stage.canvas.width-this.circleswidth)/2+this.margin;
  151. var yp = this.margin;
  152. for (var x = 0; x<this.gridwidth; x++){
  153. temparr = [];
  154. yp = this.margin;
  155. for (var y = 0; y<this.gridheight; y++){
  156. //console.log(x);
  157. //console.log(y);
  158. var s = new SymmetryDot(stage,true,xp+this.radius,yp+this.radius,this.radius,this.colours,Math.floor(Math.random()*this.colours.length),this,x,y);
  159. s.init();
  160. temparr.push(s);
  161. //console.log(s);
  162. yp+=incr;
  163. }
  164. this.dotsarr.push(temparr);
  165. xp+=incr;
  166. }
  167. //console.log(this.dotsarr);
  168. }
  169. this.initDotsFromSave = function(cols){
  170. this.dotsarr = [];
  171. var temparr = [];
  172. var incr = (this.radius*2+this.margin);
  173. var xp = (stage.canvas.width-this.circleswidth)/2+this.margin;
  174. var yp = this.margin;
  175. //console.log("colour array");
  176. //console.log(cols);
  177. for (var x = 0; x<this.gridwidth; x++){
  178. temparr = [];
  179. yp = this.margin;
  180. for (var y = 0; y<this.gridheight; y++){
  181. //console.log(x);
  182. //console.log(y);
  183. var s = new SymmetryDot(stage,true,xp+this.radius,yp+this.radius,this.radius,this.colours,cols[x][y],this,x,y);
  184. s.init();
  185. temparr.push(s);
  186. //console.log(s);
  187. yp+=incr;
  188. }
  189. this.dotsarr.push(temparr);
  190. xp+=incr;
  191. }
  192. //console.log(this.dotsarr);
  193. }
  194. //Game Logic
  195. this.checkHorizontalGame = function(){
  196. var correct = true;
  197. for (var x = 0; x<this.gridwidth/2; x++){
  198. for (var y = 0; y<this.gridheight; y++){
  199. if (this.dotsarr[x][y].colour!=this.dotsarr[(this.gridwidth-1)-x][y].colour){
  200. correct = false;
  201. }
  202. }
  203. }
  204. if (correct==true){
  205. for (var x = 0; x<this.gridwidth; x++){
  206. for (var y = 0; y<this.gridheight; y++){
  207. this.gameOver = true;
  208. this.dotsarr[x][y].clickable = false;
  209. this.dotsarr[x][y].showSmile();
  210. }
  211. }
  212. }
  213. }
  214. this.checkVerticalGame = function(){
  215. var correct = true;
  216. for (var x = 0; x<this.gridwidth; x++){
  217. for (var y = 0; y<this.gridheight/2; y++){
  218. if (this.dotsarr[x][y].colour!=this.dotsarr[x][(this.gridheight-1)-y].colour){
  219. correct = false;
  220. }
  221. }
  222. }
  223. if (correct==true){
  224. for (var x = 0; x<this.gridwidth; x++){
  225. for (var y = 0; y<this.gridheight; y++){
  226. this.gameOver = true;
  227. this.dotsarr[x][y].clickable = false;
  228. this.dotsarr[x][y].showSmile();
  229. }
  230. }
  231. }
  232. }
  233. this.checkBilateralGame = function(){
  234. var correct = true;
  235. for (var x = 0; x<this.gridwidth/2; x++){
  236. for (var y = 0; y<this.gridheight/2; y++){
  237. if (this.dotsarr[x][y].colour!=this.dotsarr[(this.gridwidth-1)-x][y].colour ||
  238. this.dotsarr[x][y].colour!=this.dotsarr[x][(this.gridheight-1)-y].colour ||
  239. this.dotsarr[x][y].colour!=this.dotsarr[(this.gridwidth-1)-x][(this.gridheight-1)-y].colour){
  240. correct = false;
  241. }
  242. }
  243. }
  244. if (correct==true){
  245. for (var x = 0; x<this.gridwidth; x++){
  246. for (var y = 0; y<this.gridheight; y++){
  247. this.gameOver = true;
  248. this.dotsarr[x][y].clickable = false;
  249. this.dotsarr[x][y].showSmile();
  250. }
  251. }
  252. }
  253. }
  254. this.checkColours = function(){
  255. switch(this.mode) {
  256. case 0:
  257. this.checkHorizontalGame();
  258. break;
  259. case 1:
  260. this.checkVerticalGame();
  261. break;
  262. case 2:
  263. this.checkBilateralGame();
  264. break;
  265. }
  266. }
  267. //Robot Functions
  268. this.robotOff = function(){
  269. var robo = doc.getElementById("robot-button");
  270. robo.title="Turn on the Robot";
  271. robo.style.backgroundImage = "url('./icons/robot-off.svg')";
  272. this.robot = false;
  273. }
  274. this.robotOn = function(){
  275. var robo = doc.getElementById("robot-button");
  276. robo.title="Turn off the Robot";
  277. robo.style.backgroundImage = "url('./icons/robot-on.svg')";
  278. this.robot = true;
  279. }
  280. this.toggleRobot = function(){
  281. if (this.robot){
  282. this.robotOff();
  283. } else {
  284. this.robotOn();
  285. }
  286. }
  287. this.robotColours = function(x,y,index){
  288. switch(this.mode) {
  289. case 0:
  290. this.horizontalColour(x,y,index);
  291. break;
  292. case 1:
  293. this.verticalColour(x,y,index);
  294. break;
  295. case 2:
  296. this.bilateralColour(x,y,index);
  297. break;
  298. }
  299. }
  300. this.horizontalColour = function(x,y,index){
  301. this.dotsarr[(this.gridwidth-1)-x][y].setColour(index);
  302. }
  303. this.verticalColour = function(x,y,index){
  304. this.dotsarr[x][(this.gridheight-1)-y].setColour(index);
  305. }
  306. this.bilateralColour = function(x,y,index){
  307. this.dotsarr[(this.gridwidth-1)-x][y].setColour(index);
  308. this.dotsarr[x][(this.gridheight-1)-y].setColour(index);
  309. this.dotsarr[(this.gridwidth-1)-x][(this.gridheight-1)-y].setColour(index);
  310. }
  311. //Save-related things
  312. this.stop = function(restart){
  313. if (restart === undefined) restart=false;
  314. //store mode, dotsarr (as colour index), robot on/off, game over, buddy
  315. var arr = {};
  316. arr.mode = this.mode;
  317. var dots = [];
  318. var temparr = [];
  319. for (var x = 0; x<this.gridwidth; x++){
  320. temparr = [];
  321. for (var y = 0; y<this.gridheight; y++){
  322. temparr.push(this.dotsarr[x][y].colour);
  323. }
  324. dots.push(temparr);
  325. }
  326. arr.dots = dots;
  327. arr.robot = this.robot;
  328. arr.gameOver = this.gameOver;
  329. if (this.colours.length == this.buddy.length){
  330. arr.buddy = true;
  331. } else {
  332. arr.buddy = false;
  333. }
  334. console.log(arr);
  335. var js = JSON.stringify(arr);
  336. activity.getDatastoreObject().setDataAsText(js);
  337. if (restart == true){
  338. activity.getDatastoreObject().save(function(){
  339. location.reload();
  340. });
  341. } else {
  342. activity.getDatastoreObject().save(function(){
  343. activity.close();
  344. });
  345. }
  346. }
  347. this.resize = function(){
  348. this.circleswidth = 0;
  349. this.circlesheight = 0;
  350. this.radius = 0;
  351. this.margin = 0;
  352. var go = this.gameOver;
  353. var r = this.canDoFromX();
  354. if (r==false){
  355. //console.log("position based on y");
  356. r = this.radiusFromY();
  357. }
  358. this.radius = r;
  359. //console.log(r);
  360. this.circleswidth = this.radius*2*this.gridwidth+this.margin*(this.gridwidth+1);
  361. this.circlesheight = this.radius*2*this.gridheight+this.margin*(this.gridheight+1);
  362. var dots = [];
  363. var temparr = [];
  364. for (var x = 0; x<this.gridwidth; x++){
  365. temparr = [];
  366. for (var y = 0; y<this.gridheight; y++){
  367. temparr.push(this.dotsarr[x][y].colour);
  368. }
  369. dots.push(temparr);
  370. }
  371. switch(this.mode) {
  372. case 0:
  373. this.initHorizontalGame(dots);
  374. break;
  375. case 1:
  376. this.initVerticalGame(dots);
  377. break;
  378. case 2:
  379. this.initBilateralGame(dots);
  380. break;
  381. }
  382. if (go == true){
  383. this.gameOver = true;
  384. for (var x = 0; x<this.gridwidth; x++){
  385. for (var y = 0; y<this.gridheight; y++){
  386. this.dotsarr[x][y].clickable = false;
  387. this.dotsarr[x][y].showSmile();
  388. }
  389. }
  390. }
  391. }
  392. //Load-related things
  393. this.init = function(){
  394. console.log("init");
  395. console.log(activity.getDatastoreObject());
  396. activity.getDatastoreObject().getMetadata(this.init_canaccessdatastore.bind(this));
  397. }
  398. this.init_canaccessdatastore = function(error,mdata){
  399. console.log("datastore check");
  400. var d = new Date().getTime();
  401. if (Math.abs(d-mdata.creation_time)<2000){
  402. console.log("Time too short");
  403. this.initActivity(false,[]);
  404. } else {
  405. activity.getDatastoreObject().loadAsText(this.init_getdatastore.bind(this));
  406. }
  407. }
  408. this.init_getdatastore = function(error,metadata,data){
  409. if (error==null&&data!=null){
  410. data = JSON.parse(data);
  411. this.initActivity(true,data);
  412. } else {
  413. this.initActivity(false,[]);
  414. }
  415. }
  416. this.initActivity = function(isdata,data){
  417. console.log(isdata);
  418. console.log(data);
  419. var r = this.canDoFromX();
  420. if (r==false){
  421. console.log("position based on y");
  422. r = this.radiusFromY();
  423. }
  424. this.radius = r;
  425. console.log(r);
  426. this.circleswidth = this.radius*2*this.gridwidth+this.margin*(this.gridwidth+1);
  427. this.circlesheight = this.radius*2*this.gridheight+this.margin*(this.gridheight+1);
  428. console.log(data);
  429. if (isdata==false||data==null){
  430. this.initHorizontalGame();
  431. } else {
  432. //mode, dotsarr (as colour index), robot on/off, game over, buddy
  433. if (data.buddy == true){
  434. this.colours = this.buddy;
  435. } else {
  436. this.colours = this.rainbow;
  437. }
  438. switch(data.mode) {
  439. case 0:
  440. this.initHorizontalGame(data.dots);
  441. break;
  442. case 1:
  443. this.initVerticalGame(data.dots);
  444. break;
  445. case 2:
  446. this.initBilateralGame(data.dots);
  447. break;
  448. }
  449. if (data.gameOver == true){
  450. this.gameOver = true;
  451. for (var x = 0; x<this.gridwidth; x++){
  452. for (var y = 0; y<this.gridheight; y++){
  453. this.dotsarr[x][y].clickable = false;
  454. this.dotsarr[x][y].showSmile();
  455. }
  456. }
  457. }
  458. if (data.robot == true){
  459. this.robotOn();
  460. } else {
  461. this.robotOff();
  462. }
  463. }
  464. if (this.colours==this.buddy){
  465. this.setBuddyStyle();
  466. } else {
  467. this.setRainbowStyle();
  468. }
  469. }
  470. }