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.

358 lines
9.8 KiB

  1. -- 房间GPS界面
  2. local RoomGpsView = class("RoomGpsView", cc.UIView)
  3. --[[
  4. maxPlayerNum = 4, -- 房间内的玩家最大数量
  5. userInfoList = --房间内所有玩家的信息
  6. {
  7. [nUserId] = {seatId = 0, userInfo = {}},
  8. [nUserId] = {seatId = 0, userInfo = {}},
  9. [nUserId] = {seatId = 0, userInfo = {}},
  10. }
  11. isGameStarted : 游戏是否已经开始
  12. exitCallback : 退出房间的回调
  13. dismissCallback : 解散房间的回调
  14. --]]
  15. function RoomGpsView:ctor(maxPlayerNum, safeDistance, userInfoList, isGameStart, exitCallback, dismissCallback, closeCallback)
  16. RoomGpsView.super.ctor(self);
  17. self.maxPlayerNum = maxPlayerNum or 4
  18. self.safeDistance = safeDistance
  19. self.userInfoList = userInfoList;
  20. self.isGameStart = isGameStart or false
  21. self.exitCallback = exitCallback;
  22. self.dismissCallback = dismissCallback;
  23. self.closeCallback = closeCallback;
  24. self:loadUI()
  25. self.playerView = nil
  26. end
  27. ---
  28. -- 加载UI
  29. -- @return
  30. --
  31. function RoomGpsView:loadUI ()
  32. local ui = loadUI("res/ui/ui_fangjian/ui_fangjian_dingwei.ui");
  33. self.ui = ui;
  34. self:addChild(ui);
  35. end
  36. function RoomGpsView:updateUserInfo(userInfoList)
  37. self.userInfoList = userInfoList;
  38. self:updateView();
  39. end
  40. function RoomGpsView:onEnter()
  41. RoomGpsView.super.onEnter(self)
  42. -- 打开GPS界面的时候主动获取GPS位置
  43. app.plugin:updateLocation()
  44. -- 继续游戏
  45. self.ui.Items.Button_Ok:registerClick(handler(self , self.onClickOk))
  46. self.ui.Items.Button_Close:registerClick(handler(self , self.onClickOk))
  47. -- 退出房间
  48. self.ui.Items.Button_Exit:registerClick(handler(self , self.onClickExit))
  49. self.ui.Items.Button_Exit:setVisible(not self.isGameStart)
  50. -- 解散游戏
  51. self.ui.Items.Button_Dismiss:registerClick(handler(self , self.onClickDismiss))
  52. self.ui.Items.Button_Dismiss:setVisible(self.isGameStart)
  53. -- 开启GPS
  54. self.ui.Items.Button_OpenGps:registerClick(handler(self, self.onClickOpenGps))
  55. self.ui.Items.Button_OpenGps:setVisible(false)
  56. self:updateView();
  57. self:checkCanDismiss();
  58. end
  59. -- 关闭
  60. function RoomGpsView:onClickOk()
  61. playBtnEffect()
  62. if self.closeCallback then
  63. self.closeCallback();
  64. end
  65. if self and not tolua.isnull(self) then
  66. self:removeFromParent()
  67. end
  68. end
  69. -- 退出房间
  70. function RoomGpsView:onClickExit()
  71. playBtnEffect()
  72. if self.exitCallback then
  73. self.exitCallback()
  74. end
  75. self:onClickOk()
  76. end
  77. -- 解散房间
  78. function RoomGpsView:onClickDismiss()
  79. playBtnEffect()
  80. if self.dismissCallback then
  81. self.dismissCallback()
  82. end
  83. self:onClickOk()
  84. end
  85. -- 开启GPS
  86. function RoomGpsView:onClickOpenGps()
  87. if app.plugin then
  88. app.plugin:openGPS()
  89. end
  90. self:onClickOk()
  91. end
  92. ---
  93. -- 加载玩家界面
  94. -- @return
  95. --
  96. function RoomGpsView:loadPlayerViewUI (maxPlayerNum)
  97. local path = string.format("res/ui/ui_fangjian/ui_fangjian_dingwei_%d.ui", self.maxPlayerNum)
  98. local ui = loadUI(path)
  99. return ui
  100. end
  101. ---
  102. -- 加载其他界面
  103. -- @return
  104. --
  105. function RoomGpsView:loadOtherViewUI (maxPlayerNum, index)
  106. local ui
  107. if maxPlayerNum == 5 then
  108. if index == 1 or index == 2 then
  109. ui = loadUI("res/ui/ui_fangjian/ui_fangjian_dingwei_player_di.ui")
  110. elseif index == 3 then
  111. ui = loadUI("res/ui/ui_fangjian/ui_fangjian_dingwei_player_ding.ui")
  112. else
  113. ui = loadUI("res/ui/ui_fangjian/ui_fangjian_dingwei_player.ui")
  114. end
  115. elseif maxPlayerNum == 6 then
  116. if index == 6 then
  117. ui = loadUI("res/ui/ui_fangjian/ui_fangjian_dingwei_player_ding.ui")
  118. elseif index == 1 then
  119. ui = loadUI("res/ui/ui_fangjian/ui_fangjian_dingwei_player_di.ui")
  120. elseif index == 2 or index == 4 then
  121. ui = loadUI("res/ui/ui_fangjian/ui_fangjian_dingwei_player_left.ui")
  122. elseif index == 3 or index == 5 then
  123. ui = loadUI("res/ui/ui_fangjian/ui_fangjian_dingwei_player_right.ui")
  124. end
  125. else
  126. ui = loadUI("res/ui/ui_fangjian/ui_fangjian_dingwei_player.ui")
  127. end
  128. return ui
  129. end
  130. -- 更新界面
  131. function RoomGpsView:updateView()
  132. if self.playerView then
  133. self.playerView:removeFromParent()
  134. self.playerView = nil
  135. end
  136. if self.maxPlayerNum < 3 or self.maxPlayerNum > 6 then
  137. showTooltip("暂不支持3个以下,6个以上玩家的房间")
  138. return
  139. end
  140. self.playerView = self:loadPlayerViewUI(self.maxPlayerNum)
  141. self.ui.Items.Layout_2:addChild(self.playerView);
  142. -- 计算各个玩家的位置,我自己始终是在第0位
  143. -- 假如我是旁观,列表里面没有我,则按照服务器下发的座位对号入座
  144. -- 服务器的椅子号是从0开始的,客户端的椅子号是从1开始的
  145. local nMyUserId = app.user.loginInfo.uid;
  146. local nMySeatId = nil
  147. if self.userInfoList and self.userInfoList[nMyUserId] then
  148. nMySeatId = self.userInfoList[nMyUserId].nSeatId;
  149. -- 检测我的GPS是否已开启
  150. local userInfo = self.userInfoList[nMyUserId].userInfo
  151. logD("RoomGpsView:updateView() myUserInfo = ", table.tostring(userInfo))
  152. if userInfo then
  153. local gpsInfo = userInfo.gpsInfo
  154. if gpsInfo and gpsInfo.gpsStatus and tonumber(gpsInfo.gpsStatus) ~= 2 then
  155. self.ui.Items.Button_OpenGps:setVisible(true)
  156. end
  157. end
  158. end
  159. local idxs = {}
  160. for nUserId, v in pairs(self.userInfoList) do
  161. local nSeatId
  162. if nMySeatId then
  163. nSeatId = (v.nSeatId - nMySeatId + self.maxPlayerNum) % self.maxPlayerNum + 1
  164. else
  165. nSeatId = v.nSeatId + 1
  166. end
  167. idxs[nSeatId] = nUserId
  168. end
  169. -- 初始化节点
  170. for i = 1,self.maxPlayerNum do
  171. -- 玩家头像父节点
  172. local layoutName= string.format("Layout_Player_%d", i)
  173. local layoutNode = self.playerView.Items[layoutName]
  174. if layoutNode then
  175. local item = self:createPlayerInfo(idxs[i],self.maxPlayerNum,i)
  176. if item then
  177. layoutNode:addChild(item);
  178. end
  179. end
  180. end
  181. -- 计算玩家与玩家之间的距离
  182. for i = 1, self.maxPlayerNum do
  183. for j = i, self.maxPlayerNum do
  184. local idx = tostring(i)..tostring(j)
  185. local uiTextName = string.format("Text_line_%s",idx)
  186. local uiImageName = string.format("ImageView_%s",idx)
  187. local uiTextNode = self.playerView.Items[uiTextName]
  188. local uiImageNode = self.playerView.Items[uiImageName]
  189. if uiTextNode and uiImageNode then
  190. -- 两端都有人才显示距离,否则不显示
  191. uiTextNode:setVisible(false);
  192. uiImageNode:setVisible(false);
  193. local nUserIdA = idxs[i]
  194. local nUserIdB = idxs[j]
  195. if nUserIdA and nUserIdB then
  196. -- 两端都有人,默认显示灰色未知距离
  197. uiTextNode:setColor(self:getUnknowGPSColor())
  198. uiTextNode:setText("未知距离")
  199. uiTextNode:setVisible(true);
  200. uiImageNode:setVisible(true);
  201. uiImageNode:loadTexture("res/ui/zy_fangjian/dingwei/dingwei_huideng.png")
  202. local userInfoA = self.userInfoList[nUserIdA]
  203. local userInfoB = self.userInfoList[nUserIdB]
  204. if userInfoA and userInfoB then
  205. local AA = userInfoA.userInfo.gpsInfo
  206. local BB = userInfoB.userInfo.gpsInfo
  207. logD("RoomGpsView:updateView() AA = ", table.toString(AA))
  208. logD("RoomGpsView:updateView() BB = ", table.toString(BB))
  209. if AA and BB then
  210. if AA.x and AA.y and AA.x > 0 and AA.y > 0 then
  211. if BB.x and BB.y and BB.x > 0 and BB.y > 0 then
  212. local distance = getDistance(AA.x, AA.y, BB.x, BB.y)
  213. local distanceText = getDistanceDesc(distance);
  214. uiTextNode:setText(string.format("%s", distanceText))
  215. if distance < self.safeDistance then
  216. uiImageNode:loadTexture("res/ui/zy_fangjian/dingwei/dingwei_hongdeng.png")
  217. else
  218. uiImageNode:loadTexture("res/ui/zy_fangjian/dingwei/dingwei_lvdeng.png")
  219. end
  220. end
  221. end
  222. end
  223. end
  224. end
  225. end
  226. end
  227. end
  228. end
  229. function RoomGpsView:createPlayerInfo(nUserId,maxPlayerNum,index)--userid 最大人数,Layout_Player_
  230. local ui = self:loadOtherViewUI(maxPlayerNum, index)
  231. -- 默认状态
  232. ui.Items.ImageView_Head:setVisible(false)
  233. ui.Items.ImageView_HeadFrame:loadTexture("res/ui/zy_fangjian/dingwei/dingwei_wuren.png")
  234. ui.Items.Text_Name:setVisible(false)
  235. ui.Items.Text_Status:setVisible(false)
  236. local info = self.userInfoList[nUserId]
  237. if not info then
  238. return ui
  239. end
  240. local userInfo = info.userInfo
  241. if not userInfo then
  242. return ui
  243. end
  244. -- 昵称
  245. ui.Items.Text_Name:setVisible(true)
  246. ui.Items.Text_Name:setText(userInfo.nickname or "unknow")
  247. -- 头像
  248. ui.Items.ImageView_Head:setVisible(true)
  249. setPlayerHeadImage(nUserId, userInfo.headimgurl, ui.Items.ImageView_Head)
  250. -- 头像框
  251. ui.Items.ImageView_HeadFrame:loadTexture("res/ui/zy_fangjian/dingwei/dingwei_youren.png")
  252. -- GPS状态
  253. local gpsInfo = userInfo.gpsInfo;
  254. local str, col = self:getGpsStatusString(gpsInfo)
  255. ui.Items.Text_Status:setVisible(true)
  256. ui.Items.Text_Status:setText(str)
  257. ui.Items.Text_Status:setColor(col);
  258. return ui;
  259. end
  260. function RoomGpsView:getGpsStatusString(gpsInfo)
  261. logD("RoomGpsView:getGpsStatusString() gpsInfo = ", table.tostring(gpsInfo))
  262. local str = "获取失败...";
  263. local col = cc.c3b(192,091,032); -- 棕
  264. if gpsInfo and gpsInfo.gpsStatus then
  265. if GpsStatusString[gpsInfo.gpsStatus] then
  266. str = GpsStatusString[gpsInfo.gpsStatus]
  267. end
  268. if GpsStatusColor[gpsInfo.gpsStatus] then
  269. col = GpsStatusColor[gpsInfo.gpsStatus]
  270. end
  271. end
  272. return str, col
  273. end
  274. function RoomGpsView:getUnknowGPSColor()
  275. return cc.c3b(44,44,44)
  276. end
  277. ---
  278. -- 检测是否可以解散房间
  279. -- @param
  280. -- @return
  281. --
  282. function RoomGpsView:checkCanDismiss( )
  283. local onCanDismissCallback = function (info)
  284. local canDismiss = (tonumber(info.canDismiss or 1) == 1)
  285. self:onCanDismissCallback(canDismiss)
  286. end
  287. if dd.IClub.getCanDismiss then
  288. local canDismiss = dd.IClub.getCanDismiss(onCanDismissCallback)
  289. if canDismiss then
  290. onCanDismissCallback({canDismiss = canDismiss})
  291. end
  292. end
  293. end
  294. function RoomGpsView:onCanDismissCallback(canDismiss)
  295. if canDismiss == false then
  296. self.ui.Items.Button_Exit:setVisible(false)
  297. self.ui.Items.Button_Dismiss:setVisible(false)
  298. local pos = self.ui.Items.Button_Ok:getPositionPercent();
  299. pos.x = 0.5;
  300. self.ui.Items.Button_Ok:setPositionPercent(pos)
  301. end
  302. end
  303. return RoomGpsView;