PageRenderTime 26ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/player/swf/board.as

http://eidogo.googlecode.com/
ActionScript | 192 lines | 174 code | 14 blank | 4 comment | 52 complexity | 0f94886b3183fc061a8076930dcdd2ce MD5 | raw file
  1. import flash.external.ExternalInterface;
  2. class Board {
  3. static var app : Board;
  4. var playerId,
  5. size,
  6. width,
  7. height,
  8. margin,
  9. boardWidth,
  10. boardHeight,
  11. boardMargin,
  12. stones,
  13. depth;
  14. function Board() {
  15. this.width = 421;
  16. this.height = 421;
  17. this.margin = 25;
  18. this.boardWidth = this.width - this.margin * 2;
  19. this.boardHeight = this.height - this.margin * 2;
  20. this.boardMargin = 5;
  21. this.depth = 1;
  22. ExternalInterface.addCallback("init", this, this.init);
  23. ExternalInterface.addCallback("clear", this, this.clear);
  24. ExternalInterface.addCallback("renderStone", this, this.renderStone);
  25. ExternalInterface.addCallback("renderMarker", this, this.renderMarker);
  26. }
  27. public function init(playerId, size) {
  28. this.playerId = playerId;
  29. this.size = size;
  30. // Board background
  31. _root.createEmptyMovieClip("bg_mc", _root.getNextHighestDepth());
  32. var mcBg = _root.bg_mc;
  33. mcBg.beginFill(0xE8C473);
  34. mcBg.moveTo(this.margin, this.margin);
  35. mcBg.lineTo(this.width-this.margin, this.margin);
  36. mcBg.lineTo(this.width-this.margin, this.height-this.margin);
  37. mcBg.lineTo(this.margin, this.height-this.margin);
  38. mcBg.lineTo(this.margin, this.margin);
  39. mcBg.endFill();
  40. // Grid lines, star points, labels
  41. _root.createEmptyMovieClip("grid_mc", _root.getNextHighestDepth());
  42. var mcGrid = _root.grid_mc;
  43. _root.createEmptyMovieClip("stars_mc", _root.getNextHighestDepth());
  44. var mcStars = _root.stars_mc;
  45. _root.createEmptyMovieClip("labels_mc", _root.getNextHighestDepth());
  46. var mcLabels = _root.labels_mc;
  47. var bW = this.boardWidth - (this.boardMargin * 2);
  48. var bH = this.boardHeight - (this.boardMargin * 2);
  49. var ptW = bW / this.size;
  50. var ptH = bH / this.size;
  51. var m = this.margin + this.boardMargin + (ptW / 2);
  52. var pts = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M',
  53. 'N', 'O', 'P', 'Q', 'R', 'S', 'T'];
  54. var fmt : TextFormat = new TextFormat();
  55. fmt.color = 0xAA9A76;
  56. fmt.font = "Arial";
  57. for (var x = 0; x < this.size; x++) {
  58. mcGrid.lineStyle(0, 0xAE9454);
  59. mcGrid.moveTo((x * ptW) + m, m);
  60. mcGrid.lineTo((x * ptW) + m, this.boardHeight + this.margin - this.boardMargin - (ptH / 2));
  61. mcLabels.createTextField("xlabel1_" + x, this.depth++, (x * ptW) + m - (ptW / 2) + 4, this.margin - 20, 20, 20);
  62. mcLabels['xlabel1_' + x].text = pts[x];
  63. mcLabels['xlabel1_' + x].setTextFormat(fmt);
  64. mcLabels.createTextField("xlabel2_" + x, this.depth++, (x * ptW) + m - (ptW / 2) + 4, this.height - this.margin + (this.margin - 22), 20, 20);
  65. mcLabels['xlabel2_' + x].text = pts[x];
  66. mcLabels['xlabel2_' + x].setTextFormat(fmt);
  67. for (var y = 0; y < this.size; y++) {
  68. if (x == 0) {
  69. mcGrid.lineStyle(0, 0xAE9454);
  70. mcGrid.moveTo(m, (y * ptH) + m);
  71. mcGrid.lineTo(this.boardWidth + this.margin - this.boardMargin - (ptW / 2), (y * ptH) + m);
  72. mcLabels.createTextField("ylabel1_" + y, this.depth++, this.margin - 22, (y * ptH) + m - (ptH / 2) + 2, 20, 20);
  73. mcLabels['ylabel1_' + y].text = (19 - y < 10 ? " " : "") + (19 - y);
  74. mcLabels['ylabel1_' + y].setTextFormat(fmt);
  75. mcLabels.createTextField("ylabel2_" + y, this.depth++, this.width - this.margin + (this.margin - 22), (y * ptH) + m - (ptH / 2) + 2, 20, 20);
  76. mcLabels['ylabel2_' + y].text = (19 - y < 10 ? " " : "") + (19 - y);
  77. mcLabels['ylabel2_' + y].setTextFormat(fmt);
  78. }
  79. if (size != 19) continue;
  80. if ((x == 3 && y == 3) || (x == 15 && y == 3) || (x == 3 && y == 15) || (x == 15 && y == 15) ||
  81. (x == 3 && y == 9) || (x == 9 && y == 3) || (x == 15 && y == 9) || (x == 9 && y == 15) ||
  82. (x == 9 && y == 9)) {
  83. mcStars.beginFill(0x705F36);
  84. mcStars.moveTo((x * ptW) + m - 1.5, (y * ptH) + m - 1.5);
  85. mcStars.lineTo((x * ptW) + m + 1.5, (y * ptH) + m - 1.5);
  86. mcStars.lineTo((x * ptW) + m + 1.5, (y * ptH) + m + 1.5);
  87. mcStars.lineTo((x * ptW) + m - 1.5, (y * ptH) + m + 1.5);
  88. mcStars.moveTo((x * ptW) + m - 1.5, (y * ptH) + m - 1.5);
  89. mcStars.endFill();
  90. }
  91. }
  92. }
  93. // Stones
  94. this.stones = {};
  95. // From: http://www.actionscript.org/forums/showthread.php3?s=&threadid=30328
  96. MovieClip.prototype.drawCircle = function(x, y, r) {
  97. var c1=r*(Math.SQRT2-1);
  98. var c2=r*Math.SQRT2/2;
  99. this.moveTo(x+r,y);
  100. this.curveTo(x+r,y+c1,x+c2,y+c2);
  101. this.curveTo(x+c1,y+r,x,y+r);
  102. this.curveTo(x-c1,y+r,x-c2,y+c2);
  103. this.curveTo(x-r,y+c1,x-r,y);
  104. this.curveTo(x-r,y-c1,x-c2,y-c2);
  105. this.curveTo(x-c1,y-r,x,y-r);
  106. this.curveTo(x+c1,y-r,x+c2,y-c2);
  107. this.curveTo(x+r,y-c1,x+r,y);
  108. }
  109. var board = this;
  110. var margin = this.margin;
  111. var boardMargin = this.boardMargin;
  112. _root.createEmptyMovieClip("mouse_mc", _root.getNextHighestDepth());
  113. var mcMouse : MovieClip = _root.mouse_mc;
  114. mcMouse.onMouseUp = function() {
  115. var mx = this._xmouse - margin;
  116. var my = this._ymouse - margin;
  117. var bx = Math.round((mx - boardMargin - (ptW / 2)) / ptW);
  118. var by = Math.round((my - boardMargin - (ptH / 2)) / ptH);
  119. if (bx < 0 || by < 0 || bx > size-1 || by > size-1) return;
  120. ExternalInterface.call("eidogo.delegate", board.playerId, "handleBoardMouseUp", bx, by);
  121. };
  122. }
  123. public function trace(arg) {
  124. ExternalInterface.call("console.log", arg);
  125. }
  126. public function clear() {
  127. for (var id in stones) {
  128. if (stones[id]) {
  129. stones[id].removeMovieClip();
  130. stones[id] = null;
  131. }
  132. }
  133. }
  134. public function renderStone(pt, color) {
  135. var bW = this.boardWidth - (this.boardMargin * 2);
  136. var bH = this.boardHeight - (this.boardMargin * 2);
  137. var ptW = bW / this.size;
  138. var ptH = bH / this.size;
  139. var m = this.margin + this.boardMargin;
  140. var x = (pt.x + 1) * ptW + m - (ptW / 2);
  141. var y = (pt.y + 1) * ptH + m - (ptH / 2);
  142. var id = "stone-" + pt.x + "-" + pt.y;
  143. if (stones[id]) {
  144. stones[id].removeMovieClip();
  145. stones[id] = null;
  146. }
  147. if (color == "white") {
  148. _root.createEmptyMovieClip(id, _root.getNextHighestDepth());
  149. var mcW = _root[id];
  150. mcW.beginFill(0xFFFFFF);
  151. mcW.lineStyle(1.75, 0x888888);
  152. mcW.drawCircle(x, y, ptW / 2 - 0.5);
  153. mcW.endFill();
  154. this.stones[id] = mcW;
  155. } else if (color == "black") {
  156. _root.createEmptyMovieClip(id, _root.getNextHighestDepth());
  157. var mcB = _root[id];
  158. mcB.beginFill(0x000000);
  159. mcB.lineStyle(1.5, 0x000000);
  160. mcB.drawCircle(x, y, ptW / 2 - 0.5);
  161. mcB.endFill();
  162. this.stones[id] = mcB;
  163. }
  164. }
  165. public function renderMarker(pt, type) {
  166. var bW = this.boardWidth - (this.boardMargin * 2);
  167. var bH = this.boardHeight - (this.boardMargin * 2);
  168. var ptW = bW / this.size;
  169. var ptH = bH / this.size;
  170. var m = this.margin + this.boardMargin;
  171. var x = (pt.x + 1) * ptW + m - (ptW / 2);
  172. var y = (pt.y + 1) * ptH + m - (ptH / 2);
  173. var id = "marker-" + pt.x + "-" + pt.y;
  174. }
  175. static function main(mc) {
  176. app = new Board();
  177. }
  178. }