源战役客户端
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.

255 lines
6.2 KiB

  1. DailyRechargeJipsawView = DailyRechargeJipsawView or BaseClass(BaseView)
  2. local math_random = math.random
  3. local table_insert = table.insert
  4. local table_remove = table.remove
  5. function DailyRechargeJipsawView:__init()
  6. self.base_file = "dailyRecharge"
  7. self.layout_file = "DailyRechargeJipsawView"
  8. self.layer_name = "Top"
  9. self.use_background = true
  10. self.is_set_zdepth = true
  11. self.select_idx = false
  12. self.item_list = {}
  13. self.model = RechargeActivityModel:getInstance()
  14. self.load_callback = function ()
  15. self:LoadSuccess()
  16. self:InitEvent()
  17. end
  18. self.destroy_callback = function ()
  19. self:Clear()
  20. end
  21. end
  22. function DailyRechargeJipsawView:Clear()
  23. for i,v in ipairs(self.item_list) do
  24. v:DeleteMe()
  25. end
  26. self.item_list = {}
  27. end
  28. function DailyRechargeJipsawView:LoadSuccess()
  29. local nodes = {
  30. "bg:raw", "help_btn:obj", "close_btn:obj", "contain", "title_text:txt", "reference_pic:raw",
  31. "restart_btn:obj",
  32. }
  33. self:GetChildren(nodes)
  34. lua_resM:setOutsideRawImage(self,self.bg_raw,GameResPath.GetViewBigBg("dailyRecharge_bg"),true)
  35. if self.need_refreshData then
  36. self:SetData(self.vo)
  37. end
  38. end
  39. function DailyRechargeJipsawView:InitEvent()
  40. local function onClickHandler(target)
  41. if self.close_btn_obj == target then
  42. self:Close()
  43. elseif self.help_btn_obj == target then
  44. if not self.is_win then
  45. self:GameHelp()
  46. else
  47. Message.show("游戏已结束")
  48. end
  49. elseif self.restart_btn_obj == target then
  50. Message.show("游戏开始")
  51. self:ReStartGame()
  52. end
  53. end
  54. AddClickEvent(self.close_btn_obj,onClickHandler)
  55. AddClickEvent(self.restart_btn_obj,onClickHandler)
  56. AddClickEvent(self.help_btn_obj,onClickHandler)
  57. end
  58. function DailyRechargeJipsawView:SetData()
  59. if self.is_loaded then
  60. self:ReStartGame()
  61. else
  62. self.need_refreshData = true
  63. end
  64. end
  65. function DailyRechargeJipsawView:UpdateView()
  66. for i,v in ipairs(self.item_list) do
  67. v:SetVisible(false)
  68. end
  69. local size_x = 935 * 0.5
  70. local size_y = 512 * 0.5
  71. local row = 3
  72. local col = 3
  73. local path = GameResPath.GetViewBigBg("arena_ragewar_bg")
  74. local pic_list, cell_x, cell_y = GetImageSplitUVRectInfo(size_x, size_y, row, col)
  75. local start_x = - (cell_x / 2 ) - (row /2 -1) * cell_x
  76. local start_y = - (cell_y / 2 ) - (col /2 -1) * cell_y
  77. local random_data = self:RandomData(row * col)
  78. self.pic_data = {}
  79. for i,v in ipairs(random_data) do
  80. local info = pic_list[v]
  81. local d =
  82. {
  83. pic_pos = v,
  84. cell_x = info.cell_x,
  85. cell_y = info.cell_y,
  86. uv_cell_x = info.uv_cell_x,
  87. uv_cell_y = info.uv_cell_y,
  88. uv_cell_w = info.uv_cell_w,
  89. uv_cell_h = info.uv_cell_h,
  90. }
  91. self.pic_data[i] = d
  92. end
  93. self.cfg =
  94. {
  95. start_x = start_x,
  96. start_y = start_y,
  97. row = row,
  98. col = col,
  99. size_x = size_x,
  100. size_y = size_y,
  101. max_num = row * col,
  102. }
  103. print('Cat:DailyRechargeJipsawView.lua[83] data', start_x, start_y, size_x, cell_x)
  104. PrintTable(self.pic_data)
  105. local idx = 1
  106. for i,v in ipairs(self.pic_data) do
  107. v.res_path = path
  108. local item = self.item_list[idx]
  109. local uv_rect = {x = v.uv_cell_x, y = v.uv_cell_y, w = v.uv_cell_w, h = v.uv_cell_h,}
  110. if not item then
  111. item = DailyRechargeJipsawItem.New(self.contain,nil,self.layer_name)
  112. self.item_list[idx] = item
  113. local function call_back(vo, pos)
  114. self:OnHandleClickItem(vo, pos)
  115. end
  116. item:SetClickCallBack(call_back)
  117. end
  118. item:SetVisible(true)
  119. item:SetData(v, idx)
  120. item:SetSize({x = v.cell_x, y = v.cell_y,})
  121. item:SetUIRect(uv_rect)
  122. local cur_row = math.floor((i - 1) / col)
  123. local cur_col = (i - 1) % col + 1
  124. item:SetAnchoredPosition(start_x + cur_row * v.cell_x, start_y + (cur_col - 1) * v.cell_y)
  125. item:SetSelect(false)
  126. idx = idx + 1
  127. end
  128. self.is_win = false
  129. lua_resM:setOutsideRawImage(self, self.reference_pic_raw, path)
  130. SetSizeDelta(self.reference_pic, size_x * 0.3 , size_y * 0.3)
  131. end
  132. function DailyRechargeJipsawView:OnHandleClickItem(vo, idx)
  133. if not self.select_idx then
  134. self:StopGameHelp()
  135. self.select_idx = idx
  136. for i,v in ipairs(self.item_list) do
  137. v:SetSelect(self.select_idx == v:GetIndex())
  138. end
  139. else
  140. if self.select_idx ~= idx then
  141. self:SwitchItem(self.select_idx, idx)
  142. end
  143. self.select_idx = false
  144. for i,v in ipairs(self.item_list) do
  145. v:SetSelect(false)
  146. end
  147. end
  148. end
  149. function DailyRechargeJipsawView:SwitchItem(old_idx, new_idx)
  150. local item_old = self.item_list[old_idx]
  151. local item_new = self.item_list[new_idx]
  152. local old_pos = item_old:GetAnchoredPosition()
  153. local new_pos = item_new:GetAnchoredPosition()
  154. self.item_list[old_idx] = item_new
  155. self.item_list[new_idx] = item_old
  156. item_old:SetAnchoredPosition(new_pos.x, new_pos.y)
  157. item_new:SetAnchoredPosition(old_pos.x, old_pos.y)
  158. item_old:SetIdx(new_idx)
  159. item_new:SetIdx(old_idx)
  160. self:CheckResult()
  161. end
  162. function DailyRechargeJipsawView:CheckResult()
  163. local max_num = self.cfg.max_num
  164. local is_win = true
  165. for i,v in ipairs(self.item_list) do
  166. if max_num >= i then
  167. if v:GetPicPos() ~= i then
  168. is_win = false
  169. break
  170. end
  171. end
  172. end
  173. if is_win then
  174. Message.show("游戏胜利")
  175. self.is_win = true
  176. end
  177. end
  178. function DailyRechargeJipsawView:RandomData(max_num)
  179. local list = {}
  180. local t = {}
  181. for i=1,max_num do
  182. t[i] = i
  183. end
  184. --先从2到倒数第二项,随机抽,保证一定能够错开
  185. local idx = math_random(2, #t - 1)
  186. local item = table_remove(t, idx)
  187. table_insert(list, item)
  188. while (#t > 0) do
  189. local idx = math_random(1, #t)
  190. local item = table_remove(t, idx)
  191. table_insert(list, item)
  192. end
  193. return list
  194. end
  195. function DailyRechargeJipsawView:ReStartGame()
  196. self:UpdateView()
  197. end
  198. function DailyRechargeJipsawView:GameHelp()
  199. local max_num = self.cfg.max_num
  200. local old_idx = -1
  201. local new_idx = -1
  202. for i,v in ipairs(self.item_list) do
  203. if max_num >= i then
  204. if old_idx == -1 and v:GetPicPos() ~= i then
  205. old_idx = i
  206. end
  207. if old_idx ~= -1 and v:GetPicPos() == old_idx then
  208. new_idx = i
  209. break
  210. end
  211. end
  212. end
  213. self.select_idx = false
  214. for i,v in ipairs(self.item_list) do
  215. if i == old_idx or i == new_idx then
  216. v:SetSelect(true)
  217. v:ShowTipsAni(true)
  218. else
  219. v:StopShowTipsAni()
  220. v:SetSelect(false)
  221. end
  222. end
  223. end
  224. function DailyRechargeJipsawView:StopGameHelp()
  225. for i,v in ipairs(self.item_list) do
  226. v:StopShowTipsAni()
  227. v:SetSelect(false)
  228. end
  229. end