PageRenderTime 4936ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/AS3/come2play_as3/dominoGame/DominoGameGraphic.as

http://multiplayer-api.googlecode.com/
ActionScript | 307 lines | 282 code | 25 blank | 0 comment | 28 complexity | dccfa538941153d60ed498dd85fbeaa8 MD5 | raw file
  1. package come2play_as3.dominoGame
  2. {
  3. import come2play_as3.api.auto_copied.AS3_vs_AS2;
  4. import come2play_as3.api.auto_copied.T;
  5. import come2play_as3.api.auto_generated.API_Message;
  6. import come2play_as3.dominoGame.events.AnimationEvent;
  7. import come2play_as3.dominoGame.events.DrawEvent;
  8. import come2play_as3.dominoGame.events.GrabEvent;
  9. import come2play_as3.dominoGame.graphicClasses.DominoBrickGraphic;
  10. import come2play_as3.dominoGame.graphicClasses.DominoButton;
  11. import come2play_as3.dominoGame.graphicClasses.HandGraphic;
  12. import come2play_as3.dominoGame.graphicClasses.MiddleBoardGraphic;
  13. import come2play_as3.dominoGame.graphicClasses.MyHelpClip;
  14. import come2play_as3.dominoGame.serverClasses.DominoCube;
  15. import come2play_as3.dominoGame.serverClasses.DominoDraw;
  16. import come2play_as3.dominoGame.serverClasses.DominoMove;
  17. import come2play_as3.dominoGame.serverClasses.DominoPass;
  18. import come2play_as3.dominoGame.usefullFunctions.Tools;
  19. import flash.display.DisplayObject;
  20. import flash.display.Sprite;
  21. import flash.events.Event;
  22. import flash.events.MouseEvent;
  23. import flash.filters.ColorMatrixFilter;
  24. import flash.geom.Point;
  25. public class DominoGameGraphic extends Sprite
  26. {
  27. private var canGrab:Boolean
  28. private var helpScreen:MyHelpClip
  29. private var middleGraphic:MiddleBoardGraphic
  30. private var myHand:HandGraphic
  31. private var rivalHand:HandGraphic
  32. private var pileGraphic:DominoBrickGraphic
  33. private var dominoBack:GameBackground;
  34. private var currentDominoAmount:int
  35. private var dominoDraw:DominoButton
  36. private var dominoPass:DominoButton
  37. private var waitingScreen:WaitingFLV = new WaitingFLV();
  38. private var isViewer:Boolean
  39. public function DominoGameGraphic()
  40. {
  41. var allWidth:int = T.custom(API_Message.CUSTOM_INFO_KEY_gameWidth,400) as int
  42. var allHeight:int = T.custom(API_Message.CUSTOM_INFO_KEY_gameHeight,400) as int
  43. waitingScreen.mouseEnabled = waitingScreen.mouseChildren = false;
  44. waitingScreen.x = allWidth - (waitingScreen.width + 70);
  45. waitingScreen.y = allWidth - (waitingScreen.height + 70)
  46. helpScreen = new MyHelpClip()
  47. dominoBack = new GameBackground()
  48. middleGraphic = new MiddleBoardGraphic()
  49. helpScreen.x = (allWidth - helpScreen.width)/2
  50. helpScreen.y = (allHeight - helpScreen.height)/2
  51. addChild(dominoBack);
  52. dominoDraw = new DominoButton(dominoBack.drawButton1,drawDomino)
  53. dominoPass = new DominoButton(dominoBack.passButton1,passTurn)
  54. pileGraphic = new DominoBrickGraphic(null,null)
  55. pileGraphic.x = dominoBack.pile1.x + 23
  56. pileGraphic.y = dominoBack.pile1.y + 25
  57. dominoBack.addChild(pileGraphic)
  58. myHand = new HandGraphic(330,pileGraphic.x,pileGraphic.y)
  59. rivalHand = new HandGraphic(5,pileGraphic.x,pileGraphic.y)
  60. AS3_vs_AS2.myAddEventListener("myHand",myHand,DrawEvent.DRAW_EVENT,takeDominoFromDeck)
  61. AS3_vs_AS2.myAddEventListener("rivalHand",rivalHand,DrawEvent.DRAW_EVENT,takeDominoFromDeck)
  62. AS3_vs_AS2.myAddEventListener("myHand",myHand,GrabEvent.GRAB_BRICK,grabBrick)
  63. AS3_vs_AS2.myAddEventListener("myHand",myHand,GrabEvent.LEAVE_BRICK,leaveBrick)
  64. dominoBack.table1.holder_mc.addChild(middleGraphic);
  65. AS3_vs_AS2.myAddEventListener("helpListener",dominoBack.helpBtn,MouseEvent.CLICK,showHelp)
  66. AS3_vs_AS2.myAddEventListener("tableListener",dominoBack.table1,MouseEvent.MOUSE_UP,stopTableDrag)
  67. AS3_vs_AS2.myAddEventListener("tableListener",dominoBack.table1,MouseEvent.MOUSE_DOWN,startTableDrag)
  68. AS3_vs_AS2.myAddEventListener("navButtonRight",dominoBack.navButtonRight,MouseEvent.CLICK,navRight)
  69. AS3_vs_AS2.myAddEventListener("navButtonLeft",dominoBack.navButtonLeft,MouseEvent.CLICK,navLeft)
  70. AS3_vs_AS2.myAddEventListener("navButtonDown",dominoBack.navButtonDown,MouseEvent.CLICK,navDown)
  71. AS3_vs_AS2.myAddEventListener("navButtonUp",dominoBack.navButtonUp,MouseEvent.CLICK,navUp)
  72. addChild(rivalHand)
  73. addChild(myHand)
  74. width = allWidth * DominoGameMain.myWidthMod;
  75. height = allHeight* DominoGameMain.myHeightMod;
  76. }
  77. private function navRight(ev:MouseEvent):void{
  78. middleGraphic.x+=10
  79. }
  80. private function navLeft(ev:MouseEvent):void{
  81. middleGraphic.x-=10
  82. }
  83. private function navDown(ev:MouseEvent):void{
  84. middleGraphic.y+=10
  85. }
  86. private function navUp(ev:MouseEvent):void{
  87. middleGraphic.y-=10
  88. }
  89. private function showHelp(ev:MouseEvent):void{
  90. addChild(helpScreen)
  91. }
  92. public function gameEnded():void{
  93. brickPosed = canGrab = false;
  94. leaveBrick()
  95. filters = []
  96. if(waitingScreen.parent!=null) waitingScreen.parent.removeChild(waitingScreen);
  97. dominoPass.enabled = false;
  98. dominoDraw.enabled = false;
  99. }
  100. private var isMyTurn:Boolean
  101. public function disableButtons(isMyTurn:Boolean,turn:int):void{
  102. dominoDraw.enabled = false;
  103. dominoPass.enabled = false
  104. this.isMyTurn = isMyTurn
  105. if(isMyTurn){
  106. dominoBack.turnIndicator1.gotoAndStop(1)
  107. filters = []
  108. if(waitingScreen.parent!=null) waitingScreen.parent.removeChild(waitingScreen);
  109. tryEnableDraw()
  110. tryEnablePass()
  111. }else{
  112. if(isViewer){
  113. dominoBack.turnIndicator1.gotoAndStop(3+turn)
  114. }else{
  115. dominoBack.turnIndicator1.gotoAndStop(2)
  116. filters = [createSaturationFilter(20)]
  117. addChild(waitingScreen)
  118. }
  119. }
  120. }
  121. private function tryEnableDraw():void{
  122. dominoDraw.enabled = ((currentDominoAmount>0) && (!didReachMaxHand()) && isMyTurn);
  123. }
  124. private function tryEnablePass():void{
  125. dominoPass.enabled = (!hasMoves() && (!dominoDraw.enabled) && isMyTurn)
  126. }
  127. private function hasMoves():Boolean{
  128. if(isViewer) return false;
  129. return myHand.hasMoves();
  130. }
  131. public function isNoMoreDomino():Boolean{
  132. return (currentDominoAmount==0)
  133. }
  134. public function drawDomino(ev:MouseEvent):void{
  135. dominoDraw.enabled = false;
  136. dispatchEvent(DominoDraw.create())
  137. }
  138. public function didReachMaxHand():Boolean{
  139. return ((myHand.dominoInHand + dominoesDelayed.length ) >= DominoGameMain.dominoMaxHand)
  140. }
  141. public function passTurn(ev:MouseEvent):void{
  142. canGrab = false;
  143. dominoPass.enabled = false
  144. dispatchEvent(DominoPass.create())
  145. }
  146. public function startGraphic(currentDominoAmount:int,isViewer:Boolean):void{
  147. this.isViewer = isViewer
  148. dominoesDelayed = []
  149. waitingPutBricks = []
  150. middleGraphic.x = -dominoBack.table1.x
  151. middleGraphic.y = -dominoBack.table1.y
  152. canGrab = false;
  153. middleGraphic.clear()
  154. myHand.clear()
  155. rivalHand.clear();
  156. this.currentDominoAmount = currentDominoAmount;
  157. }
  158. public function rivalShow(key:String,dominoCube:DominoCube):void{
  159. var dominoGraphic:DominoBrickGraphic = rivalHand.findBrick(key)
  160. dominoGraphic.show(dominoCube)
  161. }
  162. public function myShow(key:String,dominoCube:DominoCube):void{
  163. if(!isViewer) return
  164. var dominoGraphic:DominoBrickGraphic = rivalHand.findBrick(key)
  165. dominoGraphic.show(dominoCube)
  166. }
  167. public function rivalDraw(key:String,rivalNum:int):void{
  168. dispatchEvent(new AnimationEvent(true,"drawDomino"))
  169. if(rivalNum==0)
  170. rivalHand.draw(key)
  171. else
  172. myHand.draw(key);
  173. }
  174. public function draw(dominoCube:DominoCube,key:String):void{
  175. dispatchEvent(new AnimationEvent(true,"drawDomino"))
  176. for(var i:int =0;i<dominoesDelayed.length;i++){
  177. var waitingKey:String = dominoesDelayed[i]
  178. if(key == waitingKey){
  179. dominoesDelayed.splice(i,1);
  180. break;
  181. }
  182. }
  183. myHand.draw(key,dominoCube)
  184. }
  185. private var waitingPutBricks:Array
  186. public function tryPutBricks():void{
  187. if(waitingPutBricks.length == 0) return;
  188. var obj:Object = waitingPutBricks.shift()
  189. putBrick(obj.dominoMove,obj.dominoCube,obj.putWhere,obj.isLoad)
  190. }
  191. public function putBrick(dominoMove:DominoMove,dominoCube:DominoCube,putWhere:int,isLoad:Boolean):void{
  192. if(myHand.isWithQueue || rivalHand.isWithQueue){
  193. waitingPutBricks.push({dominoMove:dominoMove,putWhere:putWhere,dominoCube:dominoCube,isLoad:isLoad})
  194. return;
  195. }
  196. var cube:DominoBrickGraphic
  197. if(putWhere==-1){
  198. cube = myHand.getBrick(dominoMove.key)
  199. }else if(putWhere==0){
  200. cube = rivalHand.getBrick(dominoMove.key)
  201. }else if(putWhere==1){
  202. cube = myHand.getBrick(dominoMove.key)
  203. }
  204. cube.show(dominoCube)
  205. if(middleGraphic.canAddCenter()){
  206. middleGraphic.addCenter(cube)
  207. }else{
  208. middleGraphic.addBrick(cube,dominoMove,dominoCube)
  209. }
  210. tryPutBricks()
  211. }
  212. public var dominoesDelayed:Array
  213. public function takeDominoFromDeck(ev:DrawEvent):void{
  214. dispatchEvent(new AnimationEvent(false,"drawDomino"))
  215. currentDominoAmount--;
  216. dominoBack.stockText1.text = "x "+currentDominoAmount;
  217. tryEnableDraw()
  218. tryEnablePass()
  219. if(ev.isFinish) tryPutBricks()
  220. }
  221. public function startStageListening():void{
  222. AS3_vs_AS2.myAddEventListener("keyboard-stage",stage,MouseEvent.MOUSE_MOVE,mouseMoved)
  223. AS3_vs_AS2.myAddEventListener("keyboard-stage",stage,MouseEvent.CLICK,leaveBrick)
  224. }
  225. private var grabbedBrick:DominoBrickGraphic
  226. private var brickPosed:Boolean
  227. private function stopTableDrag(ev:MouseEvent):void{
  228. middleGraphic.stopDrag()
  229. }
  230. private function startTableDrag(ev:MouseEvent):void{
  231. middleGraphic.startDrag(false);
  232. }
  233. private function mouseMoved(ev:MouseEvent):void{
  234. if(grabbedBrick!=null){
  235. var point:Point = Tools.localToGlobal(ev.target as DisplayObject,ev.localX,ev.localY)
  236. brickPosed = middleGraphic.checkHit(grabbedBrick,point)
  237. if(brickPosed) return;
  238. grabbedBrick.hasShadow(false)
  239. grabbedBrick.brickDirection(DominoBrickGraphic.UP)
  240. grabbedBrick.xPos = point.x/Tools.scaleX
  241. grabbedBrick.yPos = point.y/Tools.scaleY
  242. }
  243. }
  244. public function allowMove():void{
  245. canGrab = true;
  246. }
  247. private function grabBrick(ev:GrabEvent):void{
  248. if(canGrab)
  249. grabbedBrick = ev.brick;
  250. }
  251. private function leaveBrick(ev:Event = null):void{
  252. if(grabbedBrick==null) return;
  253. if(brickPosed){
  254. brickPosed = canGrab = false;
  255. grabbedBrick.allowDrag(false);
  256. dispatchEvent(new GrabEvent(GrabEvent.PUT_BRICK,grabbedBrick));
  257. }else{
  258. grabbedBrick.brickDirection(DominoBrickGraphic.UP);
  259. grabbedBrick.returnBrick()
  260. }
  261. grabbedBrick = null;
  262. }
  263. private static const rConst:Number = 0.2225
  264. private static const gConst:Number = 0.7169
  265. private static const bConst:Number = 0.0606
  266. private static const rMod:Number = 1 - rConst
  267. private static const gMod:Number = 1 - gConst
  268. private static const bMod:Number = 1 - bConst
  269. private function createSaturationFilter(saturation:int):ColorMatrixFilter{
  270. var matrix:Array = new Array();
  271. var num:Number = saturation*0.01
  272. var rLum:Number =rConst+ num*(rMod);
  273. var gLum:Number =gConst+ num*(gMod);
  274. var bLum:Number =bConst+ num*(bMod);
  275. matrix = matrix.concat([rLum, gLum -(gLum*num), bLum-(bLum*num), 0, 0]); // red
  276. matrix = matrix.concat([rLum-(rLum*num), gLum, bLum-(bLum*num), 0, 0]); // green
  277. matrix = matrix.concat([rLum-(rLum*num), gLum-(gLum*num), bLum, 0, 0]); // blue
  278. matrix = matrix.concat([0, 0, 0, 1, 0]); // alpha
  279. return new ColorMatrixFilter(matrix);
  280. }
  281. }
  282. }