gameStateActive.lua 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. local gameStateActive = class('gameStateActive')
  2. function gameStateActive:ctor()
  3. end
  4. function gameStateActive:getStateType()
  5. return global.MMO.GAME_STATE_ACTIVE
  6. end
  7. function gameStateActive:onEnter()
  8. package.loaded["LuaExtend"] = nil
  9. self:InitController()
  10. self:InitUserData()
  11. self:InitPlatform()
  12. self:Init3rdparty()
  13. self:InitConfig()
  14. self:InitDebug()
  15. -- goto init
  16. global.Facade:sendNotification( global.NoticeTable.ChangeGameState, global.MMO.GAME_STATE_INIT )
  17. return 1
  18. end
  19. function gameStateActive:onEnd()--重启
  20. return 1
  21. end
  22. function gameStateActive:onExit()
  23. return 1
  24. end
  25. function gameStateActive:InitPlatform()
  26. global.OperatingMode = (global.CURRENT_OPERMODE == global.OPERMODE_WINDOWS and global.MMO.OPERATING_MODE_WINDOWS or global.MMO.OPERATING_MODE_MOBILE)
  27. global.MouseEventOpen = global.isWinPlayMode
  28. if SL._DEBUG then
  29. local userData = UserData:new("phone_mode_device")
  30. local cjson = require("cjson")
  31. local key = global.isWinPlayMode and "pc" or "phone"
  32. local jsonStr = userData:getStringForKey(key, "")
  33. if jsonStr and string.len(jsonStr) > 0 then
  34. local size = cjson.decode(jsonStr)
  35. size = {width = ConvertToEven(size.width), height = ConvertToEven(size.height)}
  36. global.DeviceSize_Win = size
  37. global.DesignSize_Win = size
  38. end
  39. local glview = global.Director:getOpenGLView()
  40. glview:setFrameSize(global.DeviceSize_Win.width, global.DeviceSize_Win.height)
  41. glview:setFrameZoomFactor(global.DeviceZoom_Win)
  42. elseif cc.PLATFORM_OS_WINDOWS == global.Platform then
  43. global.DeviceSize_Win = {width = ConvertToEven(global.DeviceSize_Win.width), height = ConvertToEven(global.DeviceSize_Win.height)}
  44. local glview = global.Director:getOpenGLView()
  45. glview:setFrameSize(global.DeviceSize_Win.width, global.DeviceSize_Win.height)
  46. glview:setFrameZoomFactor(global.DeviceZoom_Win)
  47. end
  48. local frameSize = global.Director:getOpenGLView():getFrameSize()
  49. local frameFactor = frameSize.width / frameSize.height
  50. if frameFactor > 1.8 then -- 17:9 = 1.889
  51. global.DesignPolicy = cc.ResolutionPolicy.FIXED_HEIGHT
  52. else -- 16:9 = 1.778
  53. global.DesignPolicy = cc.ResolutionPolicy.FIXED_WIDTH
  54. end
  55. self:LoadGameSizeExtra()
  56. local rData = {}
  57. rData.rType = global.DesignPolicy
  58. if global.isWindows then
  59. rData.size = global.DesignSize_Win
  60. else
  61. rData.size = global.DesignSize_Oth
  62. end
  63. global.Facade:sendNotification( global.NoticeTable.ChangeResolution, rData )
  64. -- 输入法禁用
  65. if global.isWindows and disableIME then
  66. disableIME()
  67. end
  68. -- 下划线偏移
  69. if global.Director.setUnderlineOffsetY then
  70. global.Director:setUnderlineOffsetY(0)
  71. end
  72. -- 富文本对齐方式
  73. if global.Director.setRichTextVerticalAlignment then
  74. global.Director:setRichTextVerticalAlignment(1)
  75. end
  76. -- 文件不存在提醒
  77. global.FileUtilCtl:setPopupNotify(false)
  78. -- 窗口名
  79. if global.isWindows then
  80. local currentModule = global.L_ModuleManager:GetCurrentModule()
  81. local moduleGameEnv = currentModule:GetGameEnv()
  82. local server = moduleGameEnv:GetSelectedServer()
  83. local glview = global.Director:getOpenGLView()
  84. local viewName = global.L_GameEnvManager:GetEnvDataByKey("viewName")
  85. if viewName then
  86. local nameStr = string.format( "%s-%s", viewName, server.serverName)
  87. glview:setViewName(nameStr)
  88. else
  89. glview:setViewName(server.serverName)
  90. end
  91. end
  92. if not global.CUIKeyTable then
  93. global.CUIKeyTable = requireConfig("CUIKeyTable")
  94. end
  95. ----------ttfScale-----
  96. --c++版本大于等于1
  97. if LuaBridgeCtl:Inst():GetModulesSwitch(global.MMO.Modules_Index_Cpp_Version) >= global.MMO.CPP_VERSION_LABEL_TTF then
  98. local glview = global.Director:getOpenGLView()
  99. local frameSize = glview:getFrameSize()
  100. local DesignSize = glview:getDesignResolutionSize()
  101. local ttfscale = 1
  102. if global.DesignPolicy == cc.ResolutionPolicy.FIXED_HEIGHT then
  103. ttfscale = frameSize.height/DesignSize.height
  104. elseif global.DesignPolicy == cc.ResolutionPolicy.FIXED_WIDTH then
  105. ttfscale = frameSize.width/DesignSize.width
  106. end
  107. cc.Label:setTTFAliasTexParameters(true)
  108. cc.Label:setTTFScaleFactor(ttfscale)
  109. end
  110. end
  111. function gameStateActive:Init3rdparty()
  112. local luaBridge = LuaBridgeCtl:Inst()
  113. local inited = luaBridge:IsInit3rdparty()
  114. if not inited then
  115. print( "init 3rdparty")
  116. local platform = cc.Application:getInstance():getTargetPlatform()
  117. local config = require( "config/ThirdPartyConfig" )
  118. if cc.PLATFORM_OS_ANDROID == global.Platform then
  119. -- init bugly
  120. initCrashReport( config.BuglyID, config.BuglyDebug )
  121. end
  122. luaBridge:Set3rdpartyInited( true )
  123. else
  124. print( "3rdparty inited")
  125. end
  126. end
  127. function gameStateActive:InitConfig()
  128. cc.Texture2D.setDefaultAlphaPixelFormat( cc.TEXTURE2_D_PIXEL_FORMAT_AUTO )
  129. global.Director:setProjection( cc.DIRECTOR_PROJECTION2_D )
  130. -- game env
  131. local gameEnvironment = requireProxy( "remote/GameEnvironmentProxy" )
  132. local envInst = gameEnvironment.new( global.ProxyTable.GameEnvironment )
  133. global.Facade:registerProxy( envInst )
  134. end
  135. function gameStateActive:LoadGameSizeExtra()
  136. local fileUtil = cc.FileUtils:getInstance()
  137. local filename = "dev/data_config/size_extra_config.json"
  138. local isExist = fileUtil:isFileExist(filename)
  139. if not isExist then
  140. filename = "data_config/size_extra_config.json"
  141. end
  142. local jsonStr = nil
  143. if fileUtil:isFileExist(filename) then
  144. jsonStr = fileUtil:getDataFromFileEx(filename)
  145. end
  146. if not jsonStr or jsonStr == "" then
  147. return nil
  148. end
  149. local cjson = require("cjson")
  150. local jsonData = cjson.decode(jsonStr)
  151. if not jsonData then
  152. return nil
  153. end
  154. if jsonData["designSize_mobile"] then
  155. local width = jsonData["designSize_mobile"].width or 1136
  156. local height = jsonData["designSize_mobile"].height or 640
  157. global.DesignSize_Oth = {width = width, height = height}
  158. if cc.Application:getInstance():getTargetPlatform() == cc.PLATFORM_OS_WINDOWS then
  159. if not global.isWinPlayMode and (global.isDebugMode or global.isGMMode) then
  160. global.DesignSize_Win = {width = width, height = height}
  161. global.DeviceSize_Win = {width = width, height = height}
  162. local glview = global.Director:getOpenGLView()
  163. glview:setFrameSize(global.DeviceSize_Win.width, global.DeviceSize_Win.height)
  164. end
  165. end
  166. end
  167. end
  168. function gameStateActive:InitController()
  169. -- NativeBridgeCtl
  170. if not global.NativeBridgeCtl then
  171. global.NativeBridgeCtl = NativeBridgeCtl:Inst()
  172. end
  173. -- 重启CMD
  174. local RestartCmd = requireCommand( "gamestates/RestartGameCommand" )
  175. global.Facade:registerCommand( global.NoticeTable.RestartGame, RestartCmd )
  176. -- 设计分辨率
  177. local ChangeResolutionCmd = requireCommand( "ChangeResolutionCommand" )
  178. global.Facade:registerCommand( global.NoticeTable.ChangeResolution, ChangeResolutionCmd )
  179. end
  180. function gameStateActive:InitDebug()
  181. -- DEBUG. reload some lua, for fast edit ui
  182. if global.isDebugMode or global.isGMMode then
  183. local keycodeFunc = {
  184. {
  185. -- reload lua files
  186. keycode = {cc.KeyCode.KEY_CTRL, cc.KeyCode.KEY_F5},
  187. func = function()
  188. package.loaded[ "init/initReload" ] = nil
  189. require("init/initReload")
  190. reload_script_files()
  191. UpateGUIValueCfg()
  192. end
  193. },
  194. {
  195. -- preview anim
  196. keycode = {cc.KeyCode.KEY_CTRL, cc.KeyCode.KEY_F4},
  197. func = function()
  198. package.loaded["game/view/layersui/test_layer/PreviewAnimMediator"] = nil
  199. global.Facade:sendNotification( global.NoticeTable.PreviewAnimOpen )
  200. end
  201. },
  202. {
  203. keycode = {cc.KeyCode.KEY_CTRL, cc.KeyCode.KEY_F9},
  204. func = function()
  205. releaseLayerGUI("GUIEditor")
  206. global.Facade:sendNotification(global.NoticeTable.Layer_GUIEditor_Open)
  207. end
  208. },
  209. {
  210. -- test layer
  211. keycode = {cc.KeyCode.KEY_CTRL,cc.KeyCode.KEY_F10},
  212. func = function()
  213. global.Facade:sendNotification( global.NoticeTable.Layer_RichTextTest_Open )
  214. end
  215. },
  216. {
  217. keycode = {cc.KeyCode.KEY_CTRL, cc.KeyCode.KEY_F12},
  218. func = function()
  219. releaseLayerGUI("GUITXTEditor")
  220. global.Facade:sendNotification(global.NoticeTable.Layer_GUITXTEditor_Open)
  221. end
  222. },
  223. {
  224. -- debug map
  225. keycode = {cc.KeyCode.KEY_0},
  226. func = function()
  227. global.Facade:sendNotification( global.NoticeTable.DebugMapSwitch )
  228. end
  229. },
  230. {
  231. -- restart game
  232. keycode = {cc.KeyCode.KEY_SHIFT, cc.KeyCode.KEY_ESCAPE},
  233. func = function()
  234. global.Facade:sendNotification(global.NoticeTable.RestartGame)
  235. end
  236. },
  237. {
  238. keycode = {cc.KeyCode.KEY_CTRL, cc.KeyCode.KEY_Z},
  239. func = function()
  240. local AutoProxy = global.Facade:retrieveProxy(global.ProxyTable.Auto)
  241. if AutoProxy:IsAFKState() then
  242. LuaSendMsg(global.MsgType.MSG_CS_AUTOPLAYGAME_REQUEST,2)
  243. global.Facade:sendNotification(global.NoticeTable.AFKEnd)
  244. else
  245. LuaSendMsg(global.MsgType.MSG_CS_AUTOPLAYGAME_REQUEST,1)
  246. global.Facade:sendNotification(global.NoticeTable.AFKBegin)
  247. end
  248. end
  249. },
  250. {
  251. keycode = {cc.KeyCode.KEY_CTRL, cc.KeyCode.KEY_F11},
  252. func = function()
  253. package.loaded["game/view/layersui/test_layer/SUIEditor"] = nil
  254. global.Facade:sendNotification(global.NoticeTable.SUIEditorOpen)
  255. end
  256. },
  257. {
  258. keycode = {cc.KeyCode.KEY_TAB, cc.KeyCode.KEY_F5},
  259. func = function()
  260. if ssrGlobal_F5FUNC then
  261. ssrGlobal_F5FUNC()
  262. end
  263. end
  264. },
  265. {
  266. -- CTRL+F6 打开配置表设置界面
  267. keycode = {cc.KeyCode.KEY_CTRL, cc.KeyCode.KEY_F6},
  268. func = function()
  269. global.Facade:sendNotification(global.NoticeTable.Layer_ConfigSetting_Open)
  270. end
  271. },
  272. {
  273. keycode = {cc.KeyCode.KEY_CTRL, cc.KeyCode.KEY_F3},
  274. func = function()
  275. global.Facade:sendNotification(global.NoticeTable.Layer_PreviewNode_Open)
  276. end
  277. },
  278. {
  279. -- CTRL+F7 打开新增颜色编辑器
  280. keycode = {cc.KeyCode.KEY_CTRL, cc.KeyCode.KEY_F7},
  281. func = function()
  282. global.Facade:sendNotification(global.NoticeTable.Layer_ExtraColorEditor_Open)
  283. end
  284. },
  285. }
  286. local inputKeyCode = {}
  287. local function compareKeyCode(k1, k2)
  288. return (table.concat(k1, "+") == table.concat(k2, "+"))
  289. end
  290. local function calcEvent()
  291. for _, v in ipairs(keycodeFunc) do
  292. if compareKeyCode(inputKeyCode, v.keycode) then
  293. v.func()
  294. break
  295. end
  296. end
  297. end
  298. local function pressed_callback(keycode, evt)
  299. table.insert(inputKeyCode, keycode)
  300. calcEvent()
  301. end
  302. local function released_callback(keycode, evt)
  303. for k, v in pairs(inputKeyCode) do
  304. if v == keycode then
  305. table.remove(inputKeyCode, k)
  306. end
  307. end
  308. end
  309. local listener = cc.EventListenerKeyboard:create()
  310. local eventDispatcher = cc.Director:getInstance():getEventDispatcher()
  311. listener:registerScriptHandler(pressed_callback, cc.Handler.EVENT_KEYBOARD_PRESSED)
  312. listener:registerScriptHandler(released_callback, cc.Handler.EVENT_KEYBOARD_RELEASED)
  313. eventDispatcher:addEventListenerWithFixedPriority(listener, 1)
  314. end
  315. end
  316. --统一文件夹//localUserData
  317. function gameStateActive:InitUserData()
  318. local module = global.L_ModuleManager:GetCurrentModule()
  319. local modulePath = module.GetSubModPath and module:GetSubModPath() or module:GetStabPath()
  320. local moduleGameEnv = module:GetGameEnv()
  321. local storagePath = string.format("%s%s", modulePath,global.MMO.LOCAL_USERDATA)
  322. local WritablePath = cc.FileUtils:getInstance():getWritablePath()
  323. -- 文件夹创建
  324. local modulePathDir = WritablePath..modulePath
  325. if not global.FileUtilCtl:isDirectoryExist(modulePathDir) then
  326. global.FileUtilCtl:createDirectory(modulePathDir)
  327. end
  328. if not global.FileUtilCtl:isDirectoryExist(WritablePath..storagePath) then
  329. global.FileUtilCtl:createDirectory(WritablePath..storagePath)
  330. end
  331. UserData:Cleanup()
  332. UserData:setVersionPath(storagePath)
  333. end
  334. return gameStateActive