-- <* -- @Author: Saber -- @Description: 藏宝图 -- *> TreasureMapShopItem = TreasureMapShopItem or BaseClass(BaseItem) local TreasureMapShopItem = TreasureMapShopItem function TreasureMapShopItem:__init(parent_wnd,prefab_asset,layer_name) self.base_file = "treasureMap" self.layout_file = "TreasureMapShopItem" self.parent_wnd = parent_wnd self.layer_name = layer_name self.has_receive_free_goods = false self.need_show_leftime = true self.model = TreasureMapModel:getInstance() self:Load() end function TreasureMapShopItem:Load_callback() local nodes = { "bg:obj:img", "left_time_icon:obj", "item_con", "reddot:obj", "name:tmp", "price_con/price:tmp", "price_con/old_price:tmp", "price_con/delete_icon:obj", "day_limit:tmp", "left_time:tmp", } self:GetChildren(nodes) self.delete_icon_obj:SetActive(false) self.awardItem = UIObjPool:getInstance():PopItem(UIObjPool.UIType.AwardItem, self.item_con) self.awardItem:SetItemSize(78, 78) self.awardItem:SetAnchoredPosition(0, 0) self:AddEvents() if self.need_refreshData then self:UpdateView() end end function TreasureMapShopItem:AddEvents( ) local function click_event(target, x, y) if target == self.bg_obj then -- 点击节点为购买表现 if self.data then if self.data.is_free then -- 免费道具走不同的协议 TreasureMapModel:getInstance():Fire(TreasureMapConst.REQUEST_CCMD_EVENT, 42412) else -- 限购个数 local limit_num = self.data.reward[2][4] -- 获取剩余个数 local rest_num = self.model:GetTreasureMapShopRestNumData(self.data.reward[1]) or limit_num if rest_num > 0 then local reward = self.data.reward[2][1] local goods_id = reward[2] local data = { shop_data = { price = self.data.reward[2][3], money_type = self.data.reward[2][2] == "gold" and 1 or 2, goods_id = goods_id, quota_num = limit_num, sold_out = limit_num - rest_num, quota_type = 1, is_tm_shop = true, discount = 100, reward_id = self.reward_id, reward_index = self.data.reward[1], }, } UIToolTipMgr:getInstance():AppendGoodsTips(goods_id, x, y, nil, nil, data, true) else Message.show("该物品已售罄", "fault") end end end end end AddClickEvent(self.bg_obj, click_event) local function updateFreeStatus(status) self.has_receive_free_goods = status self:UpdateLeftNum() self:StartLeftTimeFunc() end self:BindEvent(self.model, TreasureMapConst.UPDATE_SHOP_FREE_RECEIVE_STATUS, updateFreeStatus) local function updateGoodsLeftNum() self:UpdateLeftNum() self:StartLeftTimeFunc() end self:BindEvent(self.model, TreasureMapConst.UPDATE_TREASUREMAP_SHOP_VIEW, updateGoodsLeftNum) end function TreasureMapShopItem:SetData(data, expire_time, reward_id) self.data = data self.expire_time = expire_time self.reward_id = reward_id if self.is_loaded then self.need_refreshData = false self:UpdateView() else self.need_refreshData = true end end function TreasureMapShopItem:UpdateView( ) self:UpdateBasicData() self:StartLeftTimeFunc() self:UpdateLeftNum() end function TreasureMapShopItem:UpdateBasicData( ) if self.data then local reward_data = self.data.is_free and self.data.reward or self.data.reward[2][1] local goods_Id, lock = GoodsModel:getInstance():GetMappingTypeId(reward_data[1] , reward_data[2]) self.awardItem:SetData(goods_Id, reward_data[3], nil, nil, lock) self.name_tmp.text = GoodsModel:getInstance():getGoodsName(goods_Id) end end -- 更新剩余可购次数 function TreasureMapShopItem:UpdateLeftNum( ) if self.data.is_free then -- 免费道具 else end if self.data then if self.data.is_free then -- 免费道具 self.reddot_obj:SetActive(not self.has_receive_free_goods) self.day_limit_tmp.text = string.format("限购%s次", self.has_receive_free_goods and ColorUtil.RED_DARK or ColorUtil.GREEN_DARK, self.has_receive_free_goods and 0 or 1) self.price_tmp.text = self.has_receive_free_goods and string.format("已售完", ColorUtil.GRAY_DARK) or "免费" SetImageGray(self.bg_img, self.has_receive_free_goods) self.need_show_leftime = not self.has_receive_free_goods else -- 购买道具 self.reddot_obj:SetActive(false) -- 限购个数 local limit_num = self.data.reward[2][4] -- 获取剩余个数 local rest_num = self.model:GetTreasureMapShopRestNumData(self.data.reward[1]) or limit_num self.day_limit_tmp.text = string.format("限购%s/%s", rest_num > 0 and ColorUtil.GREEN_DARK or ColorUtil.RED_DARK, rest_num, limit_num) if rest_num > 0 then self.need_show_leftime = true local price_type = self.data.reward[2][2] == "gold" and 1 or 2 self.price_tmp.text = WordManager:GetMoneyFaceStr(price_type) .. self.data.reward[2][3] else self.need_show_leftime = false self.price_tmp.text = string.format("已售完", ColorUtil.GRAY_DARK) end SetImageGray(self.bg_img, rest_num <= 0) end end end function TreasureMapShopItem:StartLeftTimeFunc( ) self:ClearLeftTimeFunc() if self.need_show_leftime then local function left_time_func() local left_time = self.expire_time - TimeUtil:getServerTime() if left_time > 0 then self.left_time_tmp.text = string.format("%s", ColorUtil.GREEN_DARK, TimeUtil:convertTimeWithoutHour(left_time)) else self.left_time_tmp.text = string.format("已超时", ColorUtil.RED_DARK) self:ClearLeftTimeFunc() end end self.left_time_icon_obj:SetActive(true) left_time_func() self.left_time_func_id = GlobalTimerQuest:AddPeriodQuest(left_time_func, 0.5, -1) else self.left_time_icon_obj:SetActive(false) self.left_time_tmp.text = "" end end function TreasureMapShopItem:ClearLeftTimeFunc( ) if self.left_time_func_id then GlobalTimerQuest:CancelQuest(self.left_time_func_id) self.left_time_func_id = nil end end function TreasureMapShopItem:__delete( ) if self.awardItem then UIObjPool:getInstance():PushItem(UIObjPool.UIType.AwardItem, self.awardItem) self.awardItem = nil end self:ClearLeftTimeFunc() end