DailyRechargeJipsawView = DailyRechargeJipsawView or BaseClass(BaseView)
|
|
local math_random = math.random
|
|
local table_insert = table.insert
|
|
local table_remove = table.remove
|
|
|
|
function DailyRechargeJipsawView:__init()
|
|
self.base_file = "dailyRecharge"
|
|
self.layout_file = "DailyRechargeJipsawView"
|
|
self.layer_name = "Top"
|
|
self.use_background = true
|
|
self.is_set_zdepth = true
|
|
self.select_idx = false
|
|
self.item_list = {}
|
|
|
|
self.model = RechargeActivityModel:getInstance()
|
|
|
|
|
|
self.load_callback = function ()
|
|
self:LoadSuccess()
|
|
self:InitEvent()
|
|
end
|
|
self.destroy_callback = function ()
|
|
self:Clear()
|
|
end
|
|
end
|
|
|
|
function DailyRechargeJipsawView:Clear()
|
|
for i,v in ipairs(self.item_list) do
|
|
v:DeleteMe()
|
|
end
|
|
self.item_list = {}
|
|
end
|
|
|
|
function DailyRechargeJipsawView:LoadSuccess()
|
|
local nodes = {
|
|
"bg:raw", "help_btn:obj", "close_btn:obj", "contain", "title_text:txt", "reference_pic:raw",
|
|
"restart_btn:obj",
|
|
}
|
|
self:GetChildren(nodes)
|
|
lua_resM:setOutsideRawImage(self,self.bg_raw,GameResPath.GetViewBigBg("dailyRecharge_bg"),true)
|
|
|
|
if self.need_refreshData then
|
|
self:SetData(self.vo)
|
|
end
|
|
end
|
|
|
|
function DailyRechargeJipsawView:InitEvent()
|
|
local function onClickHandler(target)
|
|
if self.close_btn_obj == target then
|
|
self:Close()
|
|
elseif self.help_btn_obj == target then
|
|
if not self.is_win then
|
|
self:GameHelp()
|
|
else
|
|
Message.show("游戏已结束")
|
|
end
|
|
elseif self.restart_btn_obj == target then
|
|
Message.show("游戏开始")
|
|
self:ReStartGame()
|
|
end
|
|
end
|
|
|
|
AddClickEvent(self.close_btn_obj,onClickHandler)
|
|
AddClickEvent(self.restart_btn_obj,onClickHandler)
|
|
AddClickEvent(self.help_btn_obj,onClickHandler)
|
|
end
|
|
|
|
|
|
function DailyRechargeJipsawView:SetData()
|
|
if self.is_loaded then
|
|
self:ReStartGame()
|
|
else
|
|
self.need_refreshData = true
|
|
end
|
|
end
|
|
|
|
|
|
function DailyRechargeJipsawView:UpdateView()
|
|
|
|
for i,v in ipairs(self.item_list) do
|
|
v:SetVisible(false)
|
|
end
|
|
|
|
local size_x = 935 * 0.5
|
|
local size_y = 512 * 0.5
|
|
|
|
local row = 3
|
|
local col = 3
|
|
local path = GameResPath.GetViewBigBg("arena_ragewar_bg")
|
|
local pic_list, cell_x, cell_y = GetImageSplitUVRectInfo(size_x, size_y, row, col)
|
|
local start_x = - (cell_x / 2 ) - (row /2 -1) * cell_x
|
|
local start_y = - (cell_y / 2 ) - (col /2 -1) * cell_y
|
|
local random_data = self:RandomData(row * col)
|
|
self.pic_data = {}
|
|
for i,v in ipairs(random_data) do
|
|
local info = pic_list[v]
|
|
local d =
|
|
{
|
|
pic_pos = v,
|
|
cell_x = info.cell_x,
|
|
cell_y = info.cell_y,
|
|
uv_cell_x = info.uv_cell_x,
|
|
uv_cell_y = info.uv_cell_y,
|
|
uv_cell_w = info.uv_cell_w,
|
|
uv_cell_h = info.uv_cell_h,
|
|
}
|
|
self.pic_data[i] = d
|
|
end
|
|
|
|
self.cfg =
|
|
{
|
|
start_x = start_x,
|
|
start_y = start_y,
|
|
row = row,
|
|
col = col,
|
|
size_x = size_x,
|
|
size_y = size_y,
|
|
max_num = row * col,
|
|
}
|
|
print('Cat:DailyRechargeJipsawView.lua[83] data', start_x, start_y, size_x, cell_x)
|
|
PrintTable(self.pic_data)
|
|
local idx = 1
|
|
for i,v in ipairs(self.pic_data) do
|
|
v.res_path = path
|
|
local item = self.item_list[idx]
|
|
local uv_rect = {x = v.uv_cell_x, y = v.uv_cell_y, w = v.uv_cell_w, h = v.uv_cell_h,}
|
|
if not item then
|
|
item = DailyRechargeJipsawItem.New(self.contain,nil,self.layer_name)
|
|
self.item_list[idx] = item
|
|
local function call_back(vo, pos)
|
|
self:OnHandleClickItem(vo, pos)
|
|
end
|
|
item:SetClickCallBack(call_back)
|
|
end
|
|
item:SetVisible(true)
|
|
item:SetData(v, idx)
|
|
item:SetSize({x = v.cell_x, y = v.cell_y,})
|
|
item:SetUIRect(uv_rect)
|
|
local cur_row = math.floor((i - 1) / col)
|
|
local cur_col = (i - 1) % col + 1
|
|
item:SetAnchoredPosition(start_x + cur_row * v.cell_x, start_y + (cur_col - 1) * v.cell_y)
|
|
item:SetSelect(false)
|
|
idx = idx + 1
|
|
end
|
|
self.is_win = false
|
|
lua_resM:setOutsideRawImage(self, self.reference_pic_raw, path)
|
|
SetSizeDelta(self.reference_pic, size_x * 0.3 , size_y * 0.3)
|
|
end
|
|
|
|
function DailyRechargeJipsawView:OnHandleClickItem(vo, idx)
|
|
if not self.select_idx then
|
|
self:StopGameHelp()
|
|
self.select_idx = idx
|
|
for i,v in ipairs(self.item_list) do
|
|
v:SetSelect(self.select_idx == v:GetIndex())
|
|
end
|
|
else
|
|
if self.select_idx ~= idx then
|
|
self:SwitchItem(self.select_idx, idx)
|
|
end
|
|
self.select_idx = false
|
|
for i,v in ipairs(self.item_list) do
|
|
v:SetSelect(false)
|
|
end
|
|
end
|
|
end
|
|
|
|
function DailyRechargeJipsawView:SwitchItem(old_idx, new_idx)
|
|
local item_old = self.item_list[old_idx]
|
|
local item_new = self.item_list[new_idx]
|
|
local old_pos = item_old:GetAnchoredPosition()
|
|
local new_pos = item_new:GetAnchoredPosition()
|
|
self.item_list[old_idx] = item_new
|
|
self.item_list[new_idx] = item_old
|
|
item_old:SetAnchoredPosition(new_pos.x, new_pos.y)
|
|
item_new:SetAnchoredPosition(old_pos.x, old_pos.y)
|
|
item_old:SetIdx(new_idx)
|
|
item_new:SetIdx(old_idx)
|
|
self:CheckResult()
|
|
end
|
|
|
|
function DailyRechargeJipsawView:CheckResult()
|
|
local max_num = self.cfg.max_num
|
|
local is_win = true
|
|
for i,v in ipairs(self.item_list) do
|
|
if max_num >= i then
|
|
if v:GetPicPos() ~= i then
|
|
is_win = false
|
|
break
|
|
end
|
|
end
|
|
end
|
|
if is_win then
|
|
Message.show("游戏胜利")
|
|
self.is_win = true
|
|
end
|
|
end
|
|
|
|
function DailyRechargeJipsawView:RandomData(max_num)
|
|
local list = {}
|
|
local t = {}
|
|
for i=1,max_num do
|
|
t[i] = i
|
|
end
|
|
--先从2到倒数第二项,随机抽,保证一定能够错开
|
|
local idx = math_random(2, #t - 1)
|
|
local item = table_remove(t, idx)
|
|
table_insert(list, item)
|
|
|
|
while (#t > 0) do
|
|
local idx = math_random(1, #t)
|
|
local item = table_remove(t, idx)
|
|
table_insert(list, item)
|
|
end
|
|
return list
|
|
end
|
|
|
|
function DailyRechargeJipsawView:ReStartGame()
|
|
self:UpdateView()
|
|
end
|
|
|
|
function DailyRechargeJipsawView:GameHelp()
|
|
local max_num = self.cfg.max_num
|
|
local old_idx = -1
|
|
local new_idx = -1
|
|
for i,v in ipairs(self.item_list) do
|
|
if max_num >= i then
|
|
if old_idx == -1 and v:GetPicPos() ~= i then
|
|
old_idx = i
|
|
end
|
|
|
|
if old_idx ~= -1 and v:GetPicPos() == old_idx then
|
|
new_idx = i
|
|
break
|
|
end
|
|
end
|
|
end
|
|
|
|
self.select_idx = false
|
|
for i,v in ipairs(self.item_list) do
|
|
if i == old_idx or i == new_idx then
|
|
v:SetSelect(true)
|
|
v:ShowTipsAni(true)
|
|
else
|
|
v:StopShowTipsAni()
|
|
v:SetSelect(false)
|
|
end
|
|
end
|
|
end
|
|
|
|
function DailyRechargeJipsawView:StopGameHelp()
|
|
for i,v in ipairs(self.item_list) do
|
|
v:StopShowTipsAni()
|
|
v:SetSelect(false)
|
|
end
|
|
end
|