|
- -- 房间GPS界面
- local RoomGpsView = class("RoomGpsView", cc.UIView)
-
- --[[
-
- maxPlayerNum = 4, -- 房间内的玩家最大数量
- userInfoList = --房间内所有玩家的信息
- {
- [nUserId] = {seatId = 0, userInfo = {}},
- [nUserId] = {seatId = 0, userInfo = {}},
- [nUserId] = {seatId = 0, userInfo = {}},
- }
- isGameStarted : 游戏是否已经开始
- exitCallback : 退出房间的回调
- dismissCallback : 解散房间的回调
- --]]
-
- function RoomGpsView:ctor(maxPlayerNum, safeDistance, userInfoList, isGameStart, exitCallback, dismissCallback, closeCallback)
- RoomGpsView.super.ctor(self);
-
- self.maxPlayerNum = maxPlayerNum or 4
- self.safeDistance = safeDistance
- self.userInfoList = userInfoList;
- self.isGameStart = isGameStart or false
- self.exitCallback = exitCallback;
- self.dismissCallback = dismissCallback;
- self.closeCallback = closeCallback;
-
- self:loadUI()
-
- self.playerView = nil
- end
-
- ---
- -- 加载UI
- -- @return
- --
- function RoomGpsView:loadUI ()
- local ui = loadUI("res/ui/ui_fangjian/ui_fangjian_dingwei.ui");
- self.ui = ui;
- self:addChild(ui);
- end
-
- function RoomGpsView:updateUserInfo(userInfoList)
- self.userInfoList = userInfoList;
-
- self:updateView();
- end
-
- function RoomGpsView:onEnter()
- RoomGpsView.super.onEnter(self)
-
- -- 打开GPS界面的时候主动获取GPS位置
- app.plugin:updateLocation()
-
- -- 继续游戏
- self.ui.Items.Button_Ok:registerClick(handler(self , self.onClickOk))
- self.ui.Items.Button_Close:registerClick(handler(self , self.onClickOk))
-
- -- 退出房间
- self.ui.Items.Button_Exit:registerClick(handler(self , self.onClickExit))
- self.ui.Items.Button_Exit:setVisible(not self.isGameStart)
-
- -- 解散游戏
- self.ui.Items.Button_Dismiss:registerClick(handler(self , self.onClickDismiss))
- self.ui.Items.Button_Dismiss:setVisible(self.isGameStart)
-
- -- 开启GPS
- self.ui.Items.Button_OpenGps:registerClick(handler(self, self.onClickOpenGps))
- self.ui.Items.Button_OpenGps:setVisible(false)
-
- self:updateView();
- self:checkCanDismiss();
- end
-
- -- 关闭
- function RoomGpsView:onClickOk()
- playBtnEffect()
- if self.closeCallback then
- self.closeCallback();
- end
- if self and not tolua.isnull(self) then
- self:removeFromParent()
- end
- end
-
- -- 退出房间
- function RoomGpsView:onClickExit()
- playBtnEffect()
- if self.exitCallback then
- self.exitCallback()
- end
- self:onClickOk()
- end
-
- -- 解散房间
- function RoomGpsView:onClickDismiss()
- playBtnEffect()
- if self.dismissCallback then
- self.dismissCallback()
- end
- self:onClickOk()
- end
-
- -- 开启GPS
- function RoomGpsView:onClickOpenGps()
- if app.plugin then
- app.plugin:openGPS()
- end
- self:onClickOk()
- end
-
- ---
- -- 加载玩家界面
- -- @return
- --
- function RoomGpsView:loadPlayerViewUI (maxPlayerNum)
- local path = string.format("res/ui/ui_fangjian/ui_fangjian_dingwei_%d.ui", self.maxPlayerNum)
- local ui = loadUI(path)
- return ui
- end
-
- ---
- -- 加载其他界面
- -- @return
- --
- function RoomGpsView:loadOtherViewUI (maxPlayerNum, index)
- local ui
- if maxPlayerNum == 5 then
- if index == 1 or index == 2 then
- ui = loadUI("res/ui/ui_fangjian/ui_fangjian_dingwei_player_di.ui")
- elseif index == 3 then
- ui = loadUI("res/ui/ui_fangjian/ui_fangjian_dingwei_player_ding.ui")
- else
- ui = loadUI("res/ui/ui_fangjian/ui_fangjian_dingwei_player.ui")
- end
- elseif maxPlayerNum == 6 then
- if index == 6 then
- ui = loadUI("res/ui/ui_fangjian/ui_fangjian_dingwei_player_ding.ui")
- elseif index == 1 then
- ui = loadUI("res/ui/ui_fangjian/ui_fangjian_dingwei_player_di.ui")
- elseif index == 2 or index == 4 then
- ui = loadUI("res/ui/ui_fangjian/ui_fangjian_dingwei_player_left.ui")
- elseif index == 3 or index == 5 then
- ui = loadUI("res/ui/ui_fangjian/ui_fangjian_dingwei_player_right.ui")
- end
- else
- ui = loadUI("res/ui/ui_fangjian/ui_fangjian_dingwei_player.ui")
- end
- return ui
- end
-
- -- 更新界面
- function RoomGpsView:updateView()
- if self.playerView then
- self.playerView:removeFromParent()
- self.playerView = nil
- end
-
- if self.maxPlayerNum < 3 or self.maxPlayerNum > 6 then
- showTooltip("暂不支持3个以下,6个以上玩家的房间")
- return
- end
- self.playerView = self:loadPlayerViewUI(self.maxPlayerNum)
- self.ui.Items.Layout_2:addChild(self.playerView);
-
- -- 计算各个玩家的位置,我自己始终是在第0位
- -- 假如我是旁观,列表里面没有我,则按照服务器下发的座位对号入座
- -- 服务器的椅子号是从0开始的,客户端的椅子号是从1开始的
-
- local nMyUserId = app.user.loginInfo.uid;
- local nMySeatId = nil
- if self.userInfoList and self.userInfoList[nMyUserId] then
- nMySeatId = self.userInfoList[nMyUserId].nSeatId;
-
- -- 检测我的GPS是否已开启
- local userInfo = self.userInfoList[nMyUserId].userInfo
- logD("RoomGpsView:updateView() myUserInfo = ", table.tostring(userInfo))
- if userInfo then
- local gpsInfo = userInfo.gpsInfo
- if gpsInfo and gpsInfo.gpsStatus and tonumber(gpsInfo.gpsStatus) ~= 2 then
- self.ui.Items.Button_OpenGps:setVisible(true)
- end
- end
- end
-
- local idxs = {}
- for nUserId, v in pairs(self.userInfoList) do
- local nSeatId
- if nMySeatId then
- nSeatId = (v.nSeatId - nMySeatId + self.maxPlayerNum) % self.maxPlayerNum + 1
- else
- nSeatId = v.nSeatId + 1
- end
- idxs[nSeatId] = nUserId
- end
-
- -- 初始化节点
- for i = 1,self.maxPlayerNum do
- -- 玩家头像父节点
- local layoutName= string.format("Layout_Player_%d", i)
- local layoutNode = self.playerView.Items[layoutName]
- if layoutNode then
- local item = self:createPlayerInfo(idxs[i],self.maxPlayerNum,i)
- if item then
- layoutNode:addChild(item);
- end
- end
- end
-
- -- 计算玩家与玩家之间的距离
- for i = 1, self.maxPlayerNum do
- for j = i, self.maxPlayerNum do
- local idx = tostring(i)..tostring(j)
- local uiTextName = string.format("Text_line_%s",idx)
- local uiImageName = string.format("ImageView_%s",idx)
- local uiTextNode = self.playerView.Items[uiTextName]
- local uiImageNode = self.playerView.Items[uiImageName]
- if uiTextNode and uiImageNode then
-
- -- 两端都有人才显示距离,否则不显示
- uiTextNode:setVisible(false);
- uiImageNode:setVisible(false);
-
- local nUserIdA = idxs[i]
- local nUserIdB = idxs[j]
- if nUserIdA and nUserIdB then
- -- 两端都有人,默认显示灰色未知距离
- uiTextNode:setColor(self:getUnknowGPSColor())
- uiTextNode:setText("未知距离")
- uiTextNode:setVisible(true);
- uiImageNode:setVisible(true);
- uiImageNode:loadTexture("res/ui/zy_fangjian/dingwei/dingwei_huideng.png")
-
- local userInfoA = self.userInfoList[nUserIdA]
- local userInfoB = self.userInfoList[nUserIdB]
- if userInfoA and userInfoB then
- local AA = userInfoA.userInfo.gpsInfo
- local BB = userInfoB.userInfo.gpsInfo
-
- logD("RoomGpsView:updateView() AA = ", table.toString(AA))
- logD("RoomGpsView:updateView() BB = ", table.toString(BB))
-
- if AA and BB then
- if AA.x and AA.y and AA.x > 0 and AA.y > 0 then
- if BB.x and BB.y and BB.x > 0 and BB.y > 0 then
- local distance = getDistance(AA.x, AA.y, BB.x, BB.y)
- local distanceText = getDistanceDesc(distance);
- uiTextNode:setText(string.format("%s", distanceText))
-
- if distance < self.safeDistance then
- uiImageNode:loadTexture("res/ui/zy_fangjian/dingwei/dingwei_hongdeng.png")
- else
- uiImageNode:loadTexture("res/ui/zy_fangjian/dingwei/dingwei_lvdeng.png")
- end
- end
- end
- end
- end
- end
- end
- end
- end
- end
-
- function RoomGpsView:createPlayerInfo(nUserId,maxPlayerNum,index)--userid 最大人数,Layout_Player_
-
- local ui = self:loadOtherViewUI(maxPlayerNum, index)
-
- -- 默认状态
- ui.Items.ImageView_Head:setVisible(false)
- ui.Items.ImageView_HeadFrame:loadTexture("res/ui/zy_fangjian/dingwei/dingwei_wuren.png")
- ui.Items.Text_Name:setVisible(false)
- ui.Items.Text_Status:setVisible(false)
-
- local info = self.userInfoList[nUserId]
- if not info then
- return ui
- end
-
- local userInfo = info.userInfo
- if not userInfo then
- return ui
- end
-
-
- -- 昵称
- ui.Items.Text_Name:setVisible(true)
- ui.Items.Text_Name:setText(userInfo.nickname or "unknow")
-
- -- 头像
- ui.Items.ImageView_Head:setVisible(true)
- setPlayerHeadImage(nUserId, userInfo.headimgurl, ui.Items.ImageView_Head)
-
- -- 头像框
- ui.Items.ImageView_HeadFrame:loadTexture("res/ui/zy_fangjian/dingwei/dingwei_youren.png")
-
- -- GPS状态
- local gpsInfo = userInfo.gpsInfo;
- local str, col = self:getGpsStatusString(gpsInfo)
- ui.Items.Text_Status:setVisible(true)
- ui.Items.Text_Status:setText(str)
- ui.Items.Text_Status:setColor(col);
-
- return ui;
- end
-
- function RoomGpsView:getGpsStatusString(gpsInfo)
- logD("RoomGpsView:getGpsStatusString() gpsInfo = ", table.tostring(gpsInfo))
-
- local str = "获取失败...";
- local col = cc.c3b(192,091,032); -- 棕
-
- if gpsInfo and gpsInfo.gpsStatus then
- if GpsStatusString[gpsInfo.gpsStatus] then
- str = GpsStatusString[gpsInfo.gpsStatus]
- end
- if GpsStatusColor[gpsInfo.gpsStatus] then
- col = GpsStatusColor[gpsInfo.gpsStatus]
- end
- end
-
- return str, col
- end
-
- function RoomGpsView:getUnknowGPSColor()
- return cc.c3b(44,44,44)
- end
- ---
- -- 检测是否可以解散房间
- -- @param
- -- @return
- --
- function RoomGpsView:checkCanDismiss( )
- local onCanDismissCallback = function (info)
- local canDismiss = (tonumber(info.canDismiss or 1) == 1)
- self:onCanDismissCallback(canDismiss)
- end
-
- if dd.IClub.getCanDismiss then
- local canDismiss = dd.IClub.getCanDismiss(onCanDismissCallback)
- if canDismiss then
- onCanDismissCallback({canDismiss = canDismiss})
- end
- end
- end
-
- function RoomGpsView:onCanDismissCallback(canDismiss)
- if canDismiss == false then
- self.ui.Items.Button_Exit:setVisible(false)
- self.ui.Items.Button_Dismiss:setVisible(false)
- local pos = self.ui.Items.Button_Ok:getPositionPercent();
- pos.x = 0.5;
- self.ui.Items.Button_Ok:setPositionPercent(pos)
- end
- end
-
- return RoomGpsView;
|