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.

307 lines
10 KiB

  1. -- 供房间使用的GPS组件
  2. local RoomGpsComponent = class("RoomGpsComponent")
  3. function RoomGpsComponent:ctor(gpsButton)
  4. self.maxPlayerNum = 0;
  5. self.userInfoList = {}
  6. self.safeDistance = 500; -- 安全距离,单位米
  7. self.gpsButton = gpsButton;
  8. --加载纹理
  9. -- loadSpriteFrameFile("res/ui/zy_fangjian/dingwei/dingwei.plist");
  10. -- app.plugin:updateLocation()
  11. end
  12. -- 更新房间内所有玩家的信息
  13. --[[
  14. maxPlayerNum --房间可容纳的最大人数
  15. userInfoList = --房间内所有玩家的信息
  16. {
  17. [nUserId] = {nSeatId = 0, userInfo = ""},
  18. [nUserId] = {nSeatId = 0, userInfo = ""},
  19. [nUserId] = {nSeatId = 0, userInfo = ""},
  20. }
  21. --]]
  22. function RoomGpsComponent:updateUserInfos(userInfoList)
  23. self:_updateUserInfos(userInfoList)
  24. end
  25. -- 检测房间内所有玩家之间的距离
  26. -- nUserId : 如果有值,则检测该玩家和其他玩家之间的距离
  27. -- 如果没值,则检测房间内所有玩家两两之间的距离
  28. function RoomGpsComponent:checkGpsDistance(nUserId)
  29. return self:_checkGpsDistance(nUserId)
  30. end
  31. -- 打开GPS界面
  32. -- isGameStart : 游戏是否已开始,决定了界面是显示退出房间还是解散房间
  33. -- exitRoomCallback : 玩家选择退出房间的回调
  34. -- dismissCallback : 玩家选择解散房间的回调
  35. function RoomGpsComponent:showGpsView(maxPlayerNum, isGameStart, exitRoomCallback, dismissCallback)
  36. self:_showGpsView(maxPlayerNum, isGameStart, exitRoomCallback, dismissCallback)
  37. end
  38. --[[
  39. ----------------------------------------- 以下接口不对外使用 -----------------------------------------
  40. ----------------------------------------- 以下接口不对外使用 -----------------------------------------
  41. ----------------------------------------- 以下接口不对外使用 -----------------------------------------
  42. --]]
  43. -- 更新房间内所有玩家的信息
  44. function RoomGpsComponent:_updateUserInfos(userInfoList)
  45. self.userInfoList = {}
  46. for nUserId, tt in pairs(userInfoList) do
  47. local userInfo = json.decode(tt.userInfo)
  48. self.userInfoList[nUserId] = {nSeatId = tt.nSeatId, userInfo = userInfo}
  49. end
  50. self:_updateGpsView();
  51. end
  52. -- 检测房间内玩家之间的距离
  53. function RoomGpsComponent:_checkGpsDistance(curUserId)
  54. logD("RoomGpsComponent:_checkUserDistance()", tostring(curUserId))
  55. local isDanger = false;
  56. if curUserId then
  57. logD("RoomGpsComponent:_checkUserDistance() 和其他玩家进行比较",tostring(curUserId))
  58. local nickname1 = string.format("[%s]", self:_getUserName(curUserId))
  59. local gpsStatus, strResult = self:_checkGpsValid(curUserId)
  60. -- 提示这个人的GPS状态
  61. local str = string.format("[%s] %s", nickname1, errText1)
  62. logD("RoomGpsComponent:_checkUserDistance() ", str)
  63. local colorStatus = GpsDefaultColor[gpsStatus or GpsStatus.unkown]
  64. local tt =
  65. {
  66. {color = GpsDefaultColor, text = nickname1},
  67. {color = colorStatus, text = strResult},
  68. }
  69. showDropTip(tt);
  70. -- 如果没开,后面就不用和其他玩家比较了
  71. if gpsStatus ~= GpsStatus.enable then
  72. isDanger = true
  73. -- return
  74. end
  75. -- 将这个人和其他人进行比较
  76. for nUserId, v in pairs(self.userInfoList) do
  77. if curUserId ~= nUserId then
  78. local gpsStatus, errText2 = self:_checkGpsValid(nUserId)
  79. if gpsStatus == GpsStatus.enable then
  80. local ret, distance = self:_checkDistance(curUserId, nUserId)
  81. if ret and tonumber(distance) <= self.safeDistance then
  82. local nickname2 = self:_getUserName(nUserId)
  83. local nameString = string.format("[%s]和[%s]之间的距离", nickname1, nickname2)
  84. local distanceString = getDistanceDesc(tonumber(distance) or 0)
  85. local tt =
  86. {
  87. {color = GpsDefaultColor, text = nameString},
  88. {color = GpsDistanceColor.danger, text = distanceString},
  89. }
  90. showDropTip(tt)
  91. isDanger = true;
  92. end
  93. else
  94. isDanger = true;
  95. end
  96. end
  97. end
  98. else
  99. local ttUserIdCompared = {} -- 已经比较过的玩家ID
  100. logD("RoomGpsComponent:_checkUserDistance() 玩家之间两两进行比较")
  101. for curUserId, _ in pairs(self.userInfoList) do
  102. ttUserIdCompared[curUserId] = true
  103. while true do
  104. local nickname1 = self:_getUserName(curUserId)
  105. local gpsStatus, errText1 = self:_checkGpsValid(curUserId)
  106. if gpsStatus ~= GpsStatus.enable then
  107. -- 如果这个人的GPS数据不是有效的,则弹出提示
  108. local nameString = string.format("[%s]", nickname1)
  109. local statusString = tostring(errText1)
  110. local tt =
  111. {
  112. {color = GpsDefaultColor, text = nameString},
  113. {color = GpsStatusColor.close, text = statusString},
  114. }
  115. showDropTip(tt);
  116. isDanger = true
  117. break;
  118. end
  119. -- 和其他人进行比较
  120. for nUserId, _ in pairs(self.userInfoList) do
  121. if not ttUserIdCompared[nUserId] then -- 不和已经已经比较过的玩家比较
  122. local nickname2 = self:_getUserName(nUserId)
  123. local gpsStatus, errText2 = self:_checkGpsValid(nUserId)
  124. if gpsStatus == GpsStatus.enable then
  125. local ret, distance = self:_checkDistance(curUserId, nUserId)
  126. if ret and tonumber(distance) <= self.safeDistance then
  127. local nickname2 = self:_getUserName(nUserId)
  128. local nameString = string.format("[%s]和[%s]之间的距离", nickname1, nickname2)
  129. local distanceString = getDistanceDesc(tonumber(distance) or 0)
  130. local tt =
  131. {
  132. {color = GpsDefaultColor, text = nameString},
  133. {color = GpsDistanceColor.danger, text = distanceString},
  134. }
  135. showDropTip(tt)
  136. isDanger = true
  137. end
  138. else
  139. isDanger = true
  140. end
  141. end
  142. end
  143. break
  144. end
  145. end
  146. end
  147. logD("RoomGpsComponent:_checkUserDistance()", isDanger)
  148. self:_updateGpsButtonState(isDanger);
  149. self:_updateGpsView();
  150. return isDanger
  151. end
  152. -- 计算两个玩家之间的距离
  153. function RoomGpsComponent:_checkDistance(nUserId1, nUserId2)
  154. local ret1 = self:_checkGpsValid(nUserId1);
  155. local ret2 = self:_checkGpsValid(nUserId2);
  156. if ret1 and ret2 then
  157. local nickname1 = self:_getUserName(nUserId1)
  158. local nickname2 = self:_getUserName(nUserId2)
  159. local x1, y1 = self:_getUserPositon(nUserId1)
  160. local x2, y2 = self:_getUserPositon(nUserId2)
  161. local distance = getDistance(x1, y1, x2, y2)
  162. logD("RoomGpsComponent:"..nUserId1.." "..nUserId2.." distance:"..distance)
  163. return true, distance
  164. end
  165. return false, 0
  166. end
  167. -- 判断玩家的GPS数据是否有效
  168. function RoomGpsComponent:_checkGpsValid(nUserId)
  169. local userInfo = self.userInfoList[nUserId].userInfo
  170. logD("RoomGpsComponent:_checkGpsValid()", tostring(nUserId), table.tostring(userInfo))
  171. if not userInfo then
  172. logD("RoomGpsComponent:_checkGpsValid() 未找到用户信息")
  173. return GpsStatus.unkown, "未找到用户信息"
  174. end
  175. local gpsInfo = userInfo.gpsInfo
  176. if not gpsInfo then
  177. logD("RoomGpsComponent:_checkGpsValid() 未找到定位信息")
  178. return GpsStatus.unkown, "未找到定位信息"
  179. end
  180. if gpsInfo.gpsStatus ~= GpsStatus.enable then
  181. return gpsInfo.gpsStatus, GpsStatusString[gpsInfo.gpsStatus or GpsStatus.close]
  182. end
  183. if not gpsInfo.x or not gpsInfo.y or tonumber(gpsInfo.x) <= 0 or tonumber(gpsInfo.y) <= 0 then
  184. logD("RoomGpsComponent:_checkGpsValid() 定位数据错误")
  185. return GpsStatus.unkown,"定位数据错误"
  186. end
  187. return GpsStatus.enable, GpsStatusString[GpsStatus.enable]
  188. end
  189. -- 获取玩家的位置
  190. function RoomGpsComponent:_getUserPositon(nUserId)
  191. if self.userInfoList and self.userInfoList[nUserId] and self.userInfoList[nUserId].userInfo then
  192. local gpsInfo = self.userInfoList[nUserId].userInfo.gpsInfo
  193. if gpsInfo then
  194. return gpsInfo.x or 0, gpsInfo.y or 0
  195. end
  196. end
  197. return 0, 0
  198. end
  199. -- 获取玩家的名字
  200. function RoomGpsComponent:_getUserName(nUserId)
  201. if self.userInfoList and self.userInfoList[nUserId] and self.userInfoList[nUserId].userInfo then
  202. return self.userInfoList[nUserId].userInfo.nickname or ""
  203. end
  204. return ""
  205. end
  206. -- 打开GPS界面
  207. function RoomGpsComponent:_showGpsView(maxPlayerNum, isGameStart, exitRoomCallback, dismissCallback)
  208. local function gpsViewCloseCallback()
  209. self.gpsView = nil
  210. end
  211. if self.gpsView then
  212. self.gpsView:removeFromParent()
  213. self.gpsView=nil
  214. end
  215. self.gpsView = import("luaScript.Views.Room.RoomGpsView"):new(maxPlayerNum, self.safeDistance, self.userInfoList, isGameStart, exitRoomCallback, dismissCallback, gpsViewCloseCallback)
  216. self.gpsView:setAnchorPoint(cc.p(0.5, 0.5))
  217. app:showWaitDialog(self.gpsView)
  218. end
  219. function RoomGpsComponent:_updateGpsView()
  220. if self.gpsView then
  221. self.gpsView:updateUserInfo(self.userInfoList)
  222. end
  223. end
  224. function RoomGpsComponent:_updateGpsButtonState(isDanger)
  225. if self.gpsButton then
  226. if isDanger then
  227. self.gpsButton:loadTextureNormal("res/ui/zy_fangjian/dingwei/dingwei_new_red.png");
  228. --加载plist可能需要时间,这里延迟一秒启动扫光动画吧
  229. --[[self.gpsButton:runAction(cc.Sequence:create(cc.DelayTime:create(1.0),cc.CallFunc:create(function ()
  230. --self:_playAni();
  231. end)));--]]
  232. else
  233. self.gpsButton:stopAllActions();
  234. self.gpsButton:removeAllChildren();
  235. self.gpsButton:loadTextureNormal("res/ui/zy_fangjian/dingwei/dingwei_new_green.png");
  236. end
  237. end
  238. end
  239. function RoomGpsComponent:_playAni()
  240. -- 创建图片
  241. local nodeImage = cc.ImageView:createNode()
  242. nodeImage:loadTextureFromPlist("dingweiAni/dingweiAni_1.png");
  243. if self.gpsButton then
  244. self.gpsButton:addChild(nodeImage)
  245. end
  246. local size = self.gpsButton:getContentSize();
  247. nodeImage:setPosition(cc.p(size.width/2,size.height/2));
  248. --判断纹理是否存在
  249. local cache = cc.SpriteFrameCache:getInstance()
  250. local spriteFrame = cache:getSpriteFrameByName("dingweiAni/dingweiAni_1.png");
  251. if tolua.isnull(spriteFrame) then
  252. print("spriteFrame is not in cache")
  253. nodeImage:removeFromParent()
  254. return
  255. end
  256. nodeImage:runAction(cc.Sequence:create(cc.CallFunc:create(function ()
  257. local indexFace = 0;
  258. local max = 8
  259. --每隔多少秒切换一张图片
  260. local everyFrame = 0.1;
  261. local seq = cc.Sequence:create(cc.DelayTime:create(everyFrame),cc.CallFunc:create(function ()
  262. indexFace = indexFace + 1
  263. if 0 < indexFace and indexFace <= max then
  264. local name = string.format("dingweiAni/dingweiAni_%d.png",indexFace)
  265. if not tolua.isnull(nodeImage) then
  266. nodeImage:loadTexture(name, cc.TextureResType.plistType)
  267. end
  268. else
  269. indexFace = 0
  270. end
  271. end))
  272. local act = cc.RepeatForever:create(seq);
  273. nodeImage:runAction(act)
  274. end)))
  275. end
  276. return RoomGpsComponent;