Main.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. import i18n from "../i18n/i18n";
  2. import ViewManage, { AudioMgr, GameViewType } from "./ViewManage";
  3. import Player, { UserData } from "../game/data/udata/Player";
  4. import SManage, { RewardData } from "../game/data/sdata/SManage";
  5. import CHttp, { ReveData } from "../util/CHttp";
  6. import Profile from "../util/Profile";
  7. import ViewObject from "./ViewObject";
  8. import Tips from "../util/Tips";
  9. import RewardView from "../game/common/RewardView";
  10. import Home from "../game/home/Home";
  11. import GuideMask from "../plot/GuideMask";
  12. import AdminLogin from "../login/AdminLogin";
  13. /**
  14. * 主界面
  15. */
  16. const { ccclass, property } = cc._decorator;
  17. @ccclass
  18. export default class Main extends cc.Component {
  19. /**
  20. * view管理
  21. */
  22. public viewManage: ViewManage;
  23. //跟随角色摄像机
  24. @property(cc.Camera)
  25. mMapCamera: cc.Camera = null;
  26. @property(cc.Node)
  27. mLoading: cc.Node = null;
  28. @property(cc.Prefab)
  29. mTips: cc.Prefab = null;
  30. @property(GuideMask)
  31. mGuideMask: GuideMask = null;//最上层的引导
  32. @property(i18n)
  33. mI18n: i18n = null;
  34. @property(cc.SpriteFrame)
  35. RED_POINT: cc.SpriteFrame = null;
  36. @property(cc.Node)
  37. topNode: cc.Node = null;
  38. /**
  39. * 主页
  40. */
  41. public home: Home;
  42. /**
  43. * 系统数据管理
  44. */
  45. public sManage: SManage;
  46. /**
  47. * 用户数据
  48. */
  49. public userData: UserData;
  50. /**
  51. * 玩家数据
  52. */
  53. public player: Player;
  54. /**
  55. * 配置文件
  56. */
  57. public profile: Profile = null;
  58. /**
  59. * 网络连接
  60. */
  61. public loginHttp: CHttp;
  62. /**
  63. * 游戏网络连接
  64. */
  65. public gameHttp: CHttp;
  66. onLoad() {
  67. // cc.view.enableAntiAlias(false);
  68. cc.game.setFrameRate(35);
  69. // cc.director.getPhysicsManager().enabledAccumulator=true;
  70. // cc.PhysicsManager.FIXED_TIME_STEP = 1/35
  71. // cc.director.getCollisionManager().enabled = true;
  72. // cc.director.getCollisionManager().enabledDebugDraw = true;
  73. // cc.director.getPhysicsManager().enabled = true;
  74. // cc.director.getPhysicsManager().debugDrawFlags = 1;
  75. this.viewManage = this.node.getComponent(ViewManage);
  76. cc.view.resizeWithBrowserSize(true);
  77. this.resizeView();
  78. cc.view.setResizeCallback(() => {
  79. cc.log('view resize callback');
  80. this.resizeView();
  81. });
  82. this.profile = this.node.getComponent(Profile);
  83. this.loginHttp = new CHttp(this.profile.url);
  84. }
  85. start() {
  86. this.mI18n.__init(() => {
  87. // cc.log('语言测试 hello:', i18n.t('label_text.hello',{name: 'chelios'}));
  88. cc.log('语言测试 rabbit:', i18n.t('rabbit'));
  89. this.toHotUpdate();
  90. });
  91. }
  92. private resizeView() {
  93. let winsize = cc.winSize;
  94. let dt = winsize.width / winsize.height;
  95. if (dt > 1.78) {
  96. cc.Canvas.instance.fitWidth = false;
  97. cc.Canvas.instance.fitHeight = true;
  98. } else {
  99. cc.Canvas.instance.fitWidth = true;
  100. cc.Canvas.instance.fitHeight = false;
  101. }
  102. cc.log('屏幕尺寸:', winsize.width, winsize.height);
  103. cc.log('canvas尺寸:', cc.Canvas.instance.node.width, cc.Canvas.instance.node.height);
  104. cc.log('屏幕比例:', dt);
  105. }
  106. /**
  107. * 获取多国语言
  108. */
  109. public get i18n() {
  110. return i18n;
  111. }
  112. public startLoad() {
  113. this.mLoading.active = true;
  114. }
  115. public stopLoad() {
  116. this.mLoading.active = false;
  117. }
  118. public showTips(msg) {
  119. let node = cc.instantiate(this.mTips);
  120. let tips = node.getComponent(Tips);
  121. tips.setLabel(i18n.t(msg))
  122. let winsize = cc.winSize;
  123. node.y += winsize.height / 5;
  124. node.parent = this.node;
  125. }
  126. public showTips1(reve: ReveData) {
  127. let list: Array<RewardData> = this.sManage.getRewards(reve)
  128. if (list.length <= 0) {
  129. return
  130. }
  131. let str = '<color=#FFFFFF>' + i18n.t('获得道具') + ':</c><color=#0fffff>'
  132. for (let i = 0; i < list.length; i++) {
  133. const element = list[i];
  134. str += element.name + 'x' + element.count + ';'
  135. }
  136. str += '</c>'
  137. this.showTips(str)
  138. }
  139. /**
  140. * 显示从服务器上获取的奖励
  141. * @param msg
  142. */
  143. public showReward(reve: ReveData, callback?: () => void, title?: string, about?: string) {
  144. let list = this.sManage.getRewards(reve)
  145. this.showRewardList(list, callback, title, about)
  146. }
  147. public showRewardList(list, callback?: () => void, title?: string, about?: string) {
  148. if (list.length > 0) {
  149. this.viewManage.loadFunc(GameViewType.reward, (viewObject: ViewObject) => {
  150. let rewardView = viewObject as RewardView;
  151. rewardView.init(list)
  152. if (title) {
  153. rewardView.mTitle.string = i18n.t(title)
  154. }
  155. if (about) {
  156. rewardView.mAbout.string = i18n.t(about)
  157. } else {
  158. rewardView.mAbout.string = ''
  159. }
  160. if (callback) {
  161. rewardView.setCloseCallback(() => {
  162. callback()
  163. })
  164. }
  165. viewObject.show();
  166. this.playerEffectByPath(AudioMgr.award);
  167. });
  168. } else {
  169. if (callback) {
  170. callback()
  171. }
  172. }
  173. }
  174. /**
  175. * 进入热更界面
  176. */
  177. public toHotUpdate() {
  178. // if(AdminLogin.userData){
  179. // this.userData = AdminLogin.userData
  180. // this.viewManage.loadFunc(GameViewType.area,(viewObject:ViewObject)=>{
  181. // viewObject.show();
  182. // });
  183. // return
  184. // }
  185. // if (cc.sys.isNative) {
  186. // //FqLogin
  187. // this.viewManage.loadFunc(GameViewType.fqLogin, (viewObject: ViewObject) => {
  188. // viewObject.show();
  189. // });
  190. // } else {
  191. // this.viewManage.loadFunc(GameViewType.login, (viewObject: ViewObject) => {
  192. // viewObject.show();
  193. // });
  194. // }
  195. this.viewManage.loadFunc(GameViewType.login,(viewObject:ViewObject)=>{
  196. viewObject.show();
  197. });
  198. // if(CC_DEBUG){
  199. // //载入测试的登陆界面
  200. // this.viewManage.loadFunc(GameViewType.login,(viewObject:ViewObject)=>{
  201. // viewObject.show();
  202. // });
  203. // }else{
  204. // //只有isNative 版本才需要进行热更新
  205. // if (cc.sys.isNative) {
  206. // } else {
  207. // }
  208. // }
  209. }
  210. // public mp3Path = null;
  211. // public mp3Clip:cc.AudioClip = null;
  212. // public backSoundId = -1;
  213. // public playMusic() {
  214. // if(this.mp3Clip){
  215. // this.playSound1Clip(this.mp3Clip);
  216. // }else{
  217. // this.playSound1(this.mp3Path);
  218. // }
  219. // }
  220. // public pauseMusic() {
  221. // cc.audioEngine.stopAll();
  222. // }
  223. // /**
  224. // * 播放背景音乐
  225. // * @param path
  226. // */
  227. // public playSound1(path:string){
  228. // this.mp3Path = path;
  229. // this.mp3Clip.destroy();
  230. // this.mp3Clip = null;
  231. // if(path == null){
  232. // return;
  233. // }
  234. // let setting = this.player.setting;
  235. // cc.audioEngine.stopAll();
  236. // this.backSoundId = -1;
  237. // if(setting.music_ON_OFF_1){
  238. // return;
  239. // }else{
  240. // cc.resources.load(path,cc.AudioClip, (err, mp3:cc.AudioClip)=>{
  241. // if(err){
  242. // cc.log(err);
  243. // }else{
  244. // if(setting.music_ON_OFF_1){
  245. // this.backSoundId = cc.audioEngine.play(mp3, true, setting.music_Slider_1);
  246. // }
  247. // }
  248. // });
  249. // }
  250. // }
  251. // public playSound1Clip(mp3:cc.AudioClip){
  252. // this.mp3Path = null;
  253. // this.mp3Clip = mp3;
  254. // cc.audioEngine.stopAll();
  255. // let setting = this.player.setting;
  256. // if(setting.music_ON_OFF_1){
  257. // this.backSoundId = cc.audioEngine.play(mp3, true, setting.music_Slider_1);
  258. // }
  259. // }
  260. // /**
  261. // * 播放特效音乐
  262. // * @param path
  263. // */
  264. // public playSound2(path:string){
  265. // let setting = this.player.setting;
  266. // if(setting.music_ON_OFF_2){
  267. // cc.loader.loadRes(path,cc.AudioClip, (err, mp3)=>{
  268. // if(err){
  269. // cc.log(err);
  270. // }else{
  271. // if(setting.music_ON_OFF_2){
  272. // cc.audioEngine.play(mp3, false, setting.music_Slider_2);
  273. // }
  274. // }
  275. // });
  276. // }
  277. // }
  278. // public playSound2Clip(mp3:cc.AudioClip){
  279. // let setting = this.player.setting;
  280. // if(setting.music_ON_OFF_2){
  281. // cc.audioEngine.play(mp3, false, setting.music_Slider_2);
  282. // }
  283. // }
  284. public bgmId: number = -1;
  285. public effectId: number = -1;
  286. public effectList: number[] = [];
  287. // 播放背景音乐
  288. playMusicByClip(clip: cc.AudioClip) {
  289. let setting = this.player.setting;
  290. if (this.bgmId != -1) {
  291. cc.audioEngine.stopMusic();
  292. this.bgmId = -1;
  293. }
  294. if (setting.music_ON_OFF_1) {
  295. cc.audioEngine.setMusicVolume(setting.music_Slider_1);
  296. } else {
  297. cc.audioEngine.setMusicVolume(0);
  298. }
  299. this.bgmId = cc.audioEngine.playMusic(clip, true);
  300. }
  301. // 播放背景音乐
  302. playMusicByPath(path: string) {
  303. if (path == "") return
  304. cc.resources.load(path, cc.AudioClip, (err, mp3: cc.AudioClip) => {
  305. if (err) {
  306. console.log("===播放背景音乐 err===", err)
  307. } else {
  308. this.playMusicByClip(mp3);
  309. }
  310. });
  311. }
  312. // 播放音效
  313. playerEffectByClip(clip: cc.AudioClip) {
  314. let setting = this.player.setting;
  315. if (setting.music_ON_OFF_2) {
  316. if (this.effectId != -1) {
  317. cc.audioEngine.stopEffect(this.effectId);
  318. this.effectId = -1;
  319. }
  320. this.effectId = cc.audioEngine.playEffect(clip, false);
  321. cc.audioEngine.setEffectsVolume(setting.music_Slider_2);
  322. }
  323. }
  324. // 播放音效
  325. playerEffectByPath(path: string) {
  326. if (path == "") return
  327. let setting = this.player.setting;
  328. if (setting.music_ON_OFF_2) {
  329. cc.resources.load(path, cc.AudioClip, (err, mp3: cc.AudioClip) => {
  330. if (err) {
  331. console.log("===播放背景音效 err===", err)
  332. } else {
  333. this.playerEffectByClip(mp3);
  334. }
  335. });
  336. }
  337. }
  338. // 暂停音乐
  339. public pauseMusic() {
  340. let setting = this.player.setting;
  341. if (!setting.music_ON_OFF_1) {
  342. if (this.bgmId != -1) {
  343. cc.audioEngine.pauseMusic();
  344. }
  345. }
  346. }
  347. //继续播放音乐
  348. public resumeMusic() {
  349. let setting = this.player.setting;
  350. if (setting.music_ON_OFF_1) {
  351. if (this.bgmId != -1) {
  352. cc.audioEngine.resumeMusic();
  353. cc.audioEngine.setMusicVolume(setting.music_Slider_1);
  354. }
  355. }
  356. }
  357. public stopAll() {
  358. this.effectList = [];
  359. cc.audioEngine.stopAll();
  360. }
  361. }