PageRenderTime 65ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/com/lele/Controller/NetWorkController.as

https://gitlab.com/Treeky-NULL/LezaiNiubi
ActionScript | 261 lines | 237 code | 11 blank | 13 comment | 43 complexity | 060c507b0449ac04ed7a93b4f528e2c9 MD5 | raw file
  1. package com.lele.Controller
  2. {
  3. import com.lele.Controller.Avatar.Events.Avatar_PlayerController_Event;
  4. import com.lele.Controller.Avatar.Events.NetWorkController_NetPlayerUnit_Event;
  5. import com.lele.Controller.Avatar.Interface.IAvatar;
  6. import com.lele.Data.GloableData;
  7. import com.lele.Map.BitMapUtil;
  8. import com.lele.MathTool.LeleMath;
  9. import com.lele.Controller.Avatar.CAvatar;
  10. import com.lele.Controller.Avatar.Enum.AvatarState;
  11. import com.lele.Controller.ControllerBase;
  12. import com.lele.Data.IAnimationData;
  13. import com.lele.Manager.Interface.IReport;
  14. import com.lele.Plugin.RoadFind.Interface.IRoadFinder;
  15. import com.lele.Plugin.RoadFind.Interface.IRoadMap;
  16. import com.lele.Plugin.RoadFind.RoadFinder;
  17. import flash.display.Sprite;
  18. import flash.events.Event;
  19. import flash.geom.Point;
  20. /**
  21. * ...
  22. * @author Lele
  23. */
  24. //这里不要方向数据等等一切,都由对方主机发送.
  25. /*
  26. * 基本思路是其他玩家的数据处理过程如寻路,等的过程由对等主机计算,本地不计算,本地只接收结果和动作,尽量减少多人带了的资源消耗
  27. * 与此同时增加的网络流量我想不是什么问题。
  28. * */
  29. public class NetWorkController extends Sprite implements IReport
  30. {
  31. //测试才改成Public
  32. private var _roadFinder:IRoadFinder;
  33. private var _roadMap:IRoadMap;
  34. public var _netAvatar:CAvatar;
  35. private var _netAvatarState:AvatarState;
  36. private var _netRoadNodes:Array;
  37. private var _netCurrentTarget:Point;
  38. private var _netCurrentChange:Array;
  39. private var _preTarget:Point;
  40. private var _needBrake:Boolean;
  41. private const _brakeDistance:int = 50;
  42. private const _enbleBrakeDistance:int = 200;
  43. private var _repoter:IReport;
  44. private var _netWalkingSpeed:Number;
  45. private const _netRuningSpeed:Number = 3.5;
  46. private const _netSwimmingSpeed:Number = 2;
  47. public function NetWorkController(moleAct:Object,resourcePack:IAnimationData,color:String,spawnPoint:Point,name:String)
  48. {
  49. _netAvatarState = AvatarState.IDLE;//初始站立
  50. _netCurrentTarget = spawnPoint;
  51. _netWalkingSpeed = _netRuningSpeed;
  52. _netAvatar = new CAvatar(moleAct, resourcePack, false,color,name);
  53. _netAvatar.Avatar.x = spawnPoint.x;
  54. _netAvatar.Avatar.y = spawnPoint.y;
  55. _netAvatar.Repoter = this;
  56. _netAvatar.DoAction("stand", "dd");
  57. this.addEventListener(Event.ENTER_FRAME, EveryFrame);
  58. }
  59. public function set Repoter(repoter:IReport)
  60. {
  61. _repoter = repoter;
  62. }
  63. public function get AState():AvatarState
  64. {
  65. return _netAvatarState;
  66. }
  67. public function CreatController(roadMap:IRoadMap)
  68. {
  69. _roadMap = roadMap;
  70. _preTarget = new Point(_netAvatar.A_X, _netAvatar.A_Y);
  71. var colorObj:Object = { canGo:4289555498, notGo:4294967295, special:4278229503 };
  72. var bitMapUtil:BitMapUtil = new BitMapUtil(roadMap.Type_mc, GloableData.MaxRoadMapSize.x, GloableData.MaxRoadMapSize.y, 2, colorObj);
  73. _roadFinder = new RoadFinder();
  74. bitMapUtil.DrawPixelMap();
  75. _roadFinder.CreatRoadDataFromMapData(bitMapUtil);
  76. _netRoadNodes = new Array();
  77. _netRoadNodes.push(new Point(_preTarget.x, _preTarget.y));
  78. }
  79. public function AvatarStateCheck()
  80. {
  81. var tempState:int = _roadFinder.GetState(new Point(_netAvatar.A_X, _netAvatar.A_Y));
  82. if (tempState == 2&&_netAvatarState!=AvatarState.SWIM) //该游泳不游泳
  83. {
  84. if (_netAvatarState == AvatarState.WALK)
  85. {
  86. _netAvatarState = AvatarState.SWIM;
  87. directionHandle();
  88. }
  89. else
  90. {
  91. _netAvatarState = AvatarState.SWIM;
  92. DoAction("swim", "rd");
  93. }
  94. _netWalkingSpeed = _netSwimmingSpeed;
  95. }
  96. else if(tempState!=2&&_netAvatarState==AvatarState.SWIM)//不该游泳时游泳
  97. {
  98. _netAvatarState = AvatarState.IDLE;//设为静止
  99. _netWalkingSpeed = _netRuningSpeed;
  100. DoAction("stand", "dd");
  101. _needBrake = false;
  102. }
  103. }
  104. public function Move(targetPoint:Point)
  105. {
  106. var source:Point = new Point(_netAvatar.Avatar.x, _netAvatar.Avatar.y);
  107. if (_preTarget.x == Math.round(targetPoint.x) && _preTarget.y == Math.round(targetPoint.y)) { return; }//如果起点和终点一样驳回请求
  108. _preTarget = new Point(Math.round(targetPoint.x), Math.round(targetPoint.y));
  109. _roadFinder.FindRoad(source, CalculateTarget(source, targetPoint, 4));
  110. _netRoadNodes = _roadFinder.RoadNode;
  111. _netCurrentChange = _roadFinder.ChangeNode;
  112. _netCurrentTarget = _netRoadNodes[0];
  113. if(_netAvatarState!=AvatarState.WALK&&_netAvatarState!=AvatarState.SWIM)
  114. _netAvatarState = AvatarState.WALK;
  115. if (LeleMath.GetDistance(_netRoadNodes[_netRoadNodes.length - 1], _netRoadNodes[_netRoadNodes.length - 2]) > _enbleBrakeDistance) { _needBrake = true; }
  116. }
  117. public function DoAction(actName:String, actDir:String,callback:Function=null)
  118. {
  119. if (_netAvatarState == AvatarState.SWIM&&actName!="swim") { return; }
  120. _netAvatar.DoAction(actName, actDir,callback);
  121. }
  122. public function EveryFrame(evt:Event)
  123. {
  124. if (_netAvatarState==AvatarState.WALK||_netAvatarState==AvatarState.PLAY||_netAvatarState==AvatarState.SWIM)//动画都在这
  125. {
  126. if (_netCurrentTarget == null) { return; }
  127. if (Math.abs(_netAvatar.Avatar.x -_netCurrentTarget.x)<_netWalkingSpeed && Math.abs(_netAvatar.Avatar.y -_netCurrentTarget.y)<_netWalkingSpeed)
  128. {
  129. _netAvatar.Avatar.x = _netCurrentTarget.x;
  130. _netAvatar.Avatar.y = _netCurrentTarget.y;
  131. if (_netCurrentChange != null&&_netCurrentChange.length>0&& _netCurrentTarget.equals(_netCurrentChange[0]))
  132. {
  133. if (_netAvatarState == AvatarState.WALK)
  134. {
  135. _netAvatarState = AvatarState.SWIM;
  136. _netWalkingSpeed = _netSwimmingSpeed;
  137. }
  138. else if (_netAvatarState == AvatarState.SWIM)
  139. {
  140. _netAvatarState = AvatarState.WALK;
  141. _netWalkingSpeed = _netRuningSpeed;
  142. }
  143. _netCurrentChange.splice(0, 1);
  144. }
  145. if (_netCurrentTarget.equals(_netRoadNodes[_netRoadNodes.length - 1]))//到了
  146. {
  147. if (_netAvatarState == AvatarState.SWIM)
  148. {
  149. return;
  150. }
  151. _netAvatarState = AvatarState.IDLE; //设置站立状态
  152. _netAvatar.DoAction("stand", LeleMath.GetDirection((LeleMath.DealRDifference(LeleMath.GetDigree(_netRoadNodes[_netRoadNodes.length - 2], _netCurrentTarget)))));
  153. }
  154. _netCurrentTarget = _netRoadNodes[_netRoadNodes.indexOf(_netCurrentTarget) + 1];
  155. directionHandle();
  156. return;
  157. //这里有一个return??????????????????????????????????????????????????????????//
  158. }
  159. //移动
  160. var tinyStep = LeleMath.GetDistance(new Point(_netAvatar.Avatar.x, _netAvatar.Avatar.y), _netCurrentTarget)/_netWalkingSpeed;
  161. _netAvatar.Avatar.x =_netAvatar.Avatar.x + (_netCurrentTarget.x - _netAvatar.Avatar.x) / tinyStep;
  162. _netAvatar.Avatar.y = _netAvatar.Avatar.y + (_netCurrentTarget.y - _netAvatar.Avatar.y) / tinyStep;
  163. //模拟刹车
  164. if (_needBrake&&_netCurrentTarget.equals(_netRoadNodes[_netRoadNodes.length - 1])&&_netAvatarState!=AvatarState.SWIM)//较远距离
  165. {
  166. if ((int)(LeleMath.GetDistance(new Point(_netAvatar.A_X, _netAvatar.A_Y), _netCurrentTarget)) <=_brakeDistance)
  167. {
  168. _netAvatar.DoAction("brake", directionHandle(false));
  169. _needBrake = false;
  170. }
  171. }
  172. }
  173. }
  174. private function CalculateTarget(source:Point, meansTarget:Point,offset:int):Point//得到一个差一点距离的点
  175. {
  176. if (LeleMath.GetDistance(source, meansTarget) < offset) { return meansTarget; }
  177. if (meansTarget.x > source.x)
  178. {
  179. meansTarget.x -= offset;
  180. }
  181. else
  182. {
  183. meansTarget.x += offset;
  184. }
  185. if (meansTarget.y > source.y)
  186. {
  187. meansTarget.y -= offset;
  188. }
  189. else
  190. {
  191. meansTarget.y += offset;
  192. }
  193. return meansTarget;
  194. }
  195. private function directionHandle(doAction:Boolean = true):String//右下优先原则//这个用于转化Avatar的内容
  196. {
  197. if (_netCurrentTarget == null) { return "dd"; }//默认执行转向命令,false就否
  198. var dir:String;
  199. if (_netAvatar.Avatar.x - _netCurrentTarget.x > 0)//先判断左右,,在左边
  200. {
  201. if (_netAvatar.Avatar.y - _netCurrentTarget.y <= 0)//在下边
  202. {
  203. dir = "dl";
  204. }
  205. else//在上边
  206. {
  207. dir = "lu";
  208. }
  209. }
  210. else//在右边
  211. {
  212. if (_netAvatar.Avatar.y - _netCurrentTarget.y <= 0)//在下边
  213. {
  214. dir = "rd";
  215. }
  216. else//在上边
  217. {
  218. dir = "ur";
  219. }
  220. }
  221. if (doAction)
  222. {
  223. if(_netAvatarState==AvatarState.WALK)
  224. _netAvatar.DoAction("run", dir);
  225. if (_netAvatarState == AvatarState.SWIM)
  226. _netAvatar.DoAction("swim", dir);
  227. }
  228. return dir;
  229. }
  230. public function Clean()
  231. {
  232. this.removeEventListener(Event.ENTER_FRAME, EveryFrame);
  233. _netAvatar = null;
  234. _netAvatarState = null;
  235. _netCurrentTarget = null;
  236. _netRoadNodes = null;
  237. }
  238. public function OnReport(evt:Event):void
  239. {
  240. var avt:Avatar_PlayerController_Event = evt as Avatar_PlayerController_Event;
  241. switch(avt.EvtType)
  242. {
  243. case Avatar_PlayerController_Event.BECLICKED:
  244. var callShowInfo:NetWorkController_NetPlayerUnit_Event = new NetWorkController_NetPlayerUnit_Event(NetWorkController_NetPlayerUnit_Event.SHOWOTHERPLAYERINFO);
  245. _repoter.OnReport(callShowInfo);
  246. break;
  247. }
  248. }
  249. }
  250. }