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