Table.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. import Beef, { BeefState, FoodType } from "./Beef";
  2. import gameScene from "./gameScene";
  3. import guidePanel from "./guide/guidePanel";
  4. import { App } from "./Manager/App";
  5. import Person, { PersonState } from "./Person";
  6. import TipPanel from "./view/tipPanel/TipPanel";
  7. import TipPanelMediator from "./view/tipPanel/TipPanelMediator";
  8. import AESUtil from "./AESUtil"
  9. import ATSDK from "./AnyThinkAds/ATJSSDK";
  10. import ATRewardedVideoSDK from "./AnyThinkAds/ATRewardedVideoJSSDK";
  11. import AAJS2 from "./ATAndroidJS2";
  12. import GlobalManager from './GlobalManager';
  13. import ATInterstitialSDK from "./AnyThinkAds/ATInterstitialJSSDK";
  14. import ATBannerSDK from "./AnyThinkAds/ATBannerJSSDK";
  15. import { AdType } from './ATAndroidJS2';
  16. const { ccclass, property } = cc._decorator;
  17. @ccclass
  18. export default class Table extends cc.Component {
  19. private static _instance: Table = null;
  20. // @property([cc.SpriteFrame])
  21. // private tableSpriteFrame: cc.SpriteFrame[] = [];
  22. @property(cc.Button)
  23. btnShengNiuPai: cc.Button = null;
  24. rawSteakList: cc.Node[] = [null, null, null, null];
  25. cookedSteakList: cc.Node[] = [null, null, null, null];
  26. @property(cc.Prefab)
  27. prefabBeef: cc.Prefab = null;
  28. @property(cc.Button)
  29. btnYellowSouce: cc.Button = null;
  30. @property(cc.Button)
  31. btnCocumber: cc.Button = null;
  32. @property(cc.Button)
  33. btnTomato: cc.Button = null;
  34. @property(cc.Button)
  35. btnRedSouce: cc.Button = null;
  36. @property(cc.Node)
  37. liuShui: cc.Node = null;
  38. @property(cc.Node)
  39. drinkCup: cc.Node = null;
  40. @property([cc.Node])
  41. kaoPanGaiZi: cc.Node[] = [];
  42. @property([cc.Node])
  43. plateGaiZi: cc.Node[] = [];
  44. @property([cc.Node])
  45. cupGaiZi: cc.Node[] = [];
  46. @property(cc.ProgressBar)
  47. pDrinkBar: cc.ProgressBar = null;
  48. m_shuiLiuTime = 3;
  49. @property(cc.Prefab)
  50. fullCup: cc.Prefab = null;
  51. drinkCupList: cc.Node[] = [];
  52. kaoPanNum = 2;
  53. plateNum = 2;
  54. cupNum = 3;
  55. beefBornPos: cc.Vec3[] = [cc.v3(-290, -185, 0), cc.v3(-197, -195, 0), cc.v3(-243, -131, 0), cc.v3(-160, -133, 0)];
  56. platePos: cc.Vec3[] = [cc.v3(-58, -200, 0), cc.v3(80, -201, 0), cc.v3(-52, -126, 0), cc.v3(68, -131, 0)];
  57. burntPos: cc.Vec3 = cc.v3(-360, -640, 0);
  58. cupPos: cc.Vec3[] = [cc.v3(258, -209, 0), cc.v3(232, -161, 0), cc.v3(205, -115, 0)];
  59. // ------------------------person---------------------------
  60. @property([cc.Prefab])
  61. renwuPerfabList: cc.Prefab[] = [];
  62. personList: cc.Node[] = [];
  63. // 点餐位置
  64. personPos: cc.Vec3[] = [cc.v3(30, 38, 0), cc.v3(260, 38, 0), cc.v3(-200, 38, 0)];
  65. // 人物出生位置
  66. bornPos: cc.Vec3[] = [cc.v3(-500, 38, 0), cc.v3(550, 38, 0)];
  67. fixTime = 1;
  68. passTime = 0;
  69. personWalkTime = 2;
  70. createpPersonNum = 0; //已经来了到少人
  71. fullPersonNum = 0; //多少吃饱了
  72. maxSitPersonNum = 3; //总共只能坐下人数
  73. kaoJiaoCount = 0;//合计烤焦了多少个?
  74. // LIFE-CYCLE CALLBACKS:
  75. public static getInstance(): any {
  76. if (this._instance == null) {
  77. this._instance = new Table();
  78. }
  79. return this._instance;
  80. }
  81. onLoad() {
  82. Table._instance = this;
  83. this.btnShengNiuPai.node.on(cc.Node.EventType.TOUCH_END, this.onClickShengNiuPai.bind(this), this);
  84. this.btnYellowSouce.node.on(cc.Node.EventType.TOUCH_END, this.onClickYellowSouce.bind(this), this);
  85. this.btnCocumber.node.on(cc.Node.EventType.TOUCH_END, this.onClickCocumber.bind(this), this);
  86. this.btnTomato.node.on(cc.Node.EventType.TOUCH_END, this.onClickTomato.bind(this), this);
  87. this.btnRedSouce.node.on(cc.Node.EventType.TOUCH_END, this.onClickRedSouce.bind(this), this);
  88. // this.drinksMachine.node.on(cc.Node.EventType.TOUCH_END, this.onClickDrinksMachine.bind(this), this);
  89. for (let i = 0; i < this.maxSitPersonNum; i++) {
  90. this.personList[i] = null;
  91. }
  92. this.plateNum = App.DataManager.kitchenNum[0][App.DataManager.kinchenLevel[0]];
  93. this.kaoPanNum = App.DataManager.kitchenNum[1][App.DataManager.kinchenLevel[1]];
  94. this.cupNum = App.DataManager.kitchenNum[2][App.DataManager.kinchenLevel[2]];
  95. this.initAdForPage();
  96. }
  97. initAdForPage() {
  98. if (cc.sys.os === cc.sys.OS_ANDROID) {
  99. let deviceId = AAJS2.getDeviceUserId();
  100. console.log("zh:checkstatus:", ATRewardedVideoSDK.checkAdStatus(AAJS2.getPlacementId()));
  101. var setting = {};
  102. setting[ATRewardedVideoSDK.userIdKey] = deviceId;
  103. ATRewardedVideoSDK.loadRewardedVideo(AAJS2.getPlacementId(), setting);
  104. }
  105. }
  106. start() {
  107. for (let i = 0; i < this.kaoPanNum - 2; i++) {
  108. this.kaoPanGaiZi[i].active = false
  109. }
  110. for (let i = 0; i < this.plateNum - 2; i++) {
  111. this.plateGaiZi[i].active = true
  112. }
  113. for (let i = 0; i < this.cupNum - 1; i++) {
  114. this.cupGaiZi[i].active = true
  115. }
  116. if (App.DataManager.CurrentIntoLevel >= 3) {
  117. this.btnTomato.node.active = true;
  118. }
  119. if (App.DataManager.CurrentIntoLevel >= 5) {
  120. this.btnYellowSouce.node.active = true;
  121. }
  122. if (App.DataManager.CurrentIntoLevel >= 7) {
  123. this.btnRedSouce.node.active = true;
  124. }
  125. this.onClickDrinksMachine();
  126. if (App.DataManager.guideStep == 0) {
  127. guidePanel.getInstance().showCurGuide();
  128. }
  129. }
  130. onClickShengNiuPai() {
  131. if (App.DataManager.guideStep == 0) {
  132. App.DataManager.guideStep++;
  133. App.LocalStorageUtil.setNumber(App.LocalStorageUtil.str_guideStep, App.DataManager.guideStep);
  134. guidePanel.getInstance().hideGuideStep();
  135. }
  136. for (let i = 0; i < this.kaoPanNum; i++) {
  137. if (this.rawSteakList[i] == null) {
  138. let beef = cc.instantiate(this.prefabBeef);
  139. beef.on(cc.Node.EventType.TOUCH_END, this.onClickRawBeef.bind(this, i), beef);
  140. beef.setPosition(this.beefBornPos[i]);
  141. this.node.addChild(beef);
  142. this.rawSteakList[i] = beef;
  143. break;
  144. }
  145. }
  146. }
  147. onClickRawBeef(index: number) {
  148. console.log('zh: 已经烤焦=' + this.kaoJiaoCount);
  149. if (this.rawSteakList[index].getComponent(Beef).m_state == BeefState.Burnt) {
  150. console.log('zh: 已经烤焦了');
  151. var c1 = this.kaoJiaoCount + 1;
  152. this.kaoJiaoCount = c1;
  153. // 每烤焦2次执行惩罚
  154. if (c1 % 2 === 0) { // 当烤焦次数为2的倍数时执行惩罚
  155. this.kaoJiaoCount = 0;// 重置计数器
  156. console.log('zh: 需要惩罚');
  157. if (cc.sys.os == cc.sys.OS_ANDROID) {
  158. if (ATRewardedVideoSDK.hasAdReady(AAJS2.getPlacementId())) {
  159. console.log('zh:AD ok!');
  160. cc.director.pause();
  161. console.log('zh: 游戏已暂停');
  162. cc.sys.localStorage.setItem('yxAdMark', 'callBackFor_kaoJiao');//看广告用于 xx 标记
  163. cc.sys.localStorage.setItem('yxAdMark_index', index);//参数
  164. GlobalManager.instance.registerMethod('callBackFor_kaoJiao', this.callBackFor_kaoJiao.bind(this));
  165. ATRewardedVideoSDK.showAd(AAJS2.getPlacementId());
  166. } else {
  167. console.log('zh:AD NO OK');
  168. this.callBackFor_kaoJiao(index);
  169. this.initAdForPage();
  170. return;
  171. }
  172. } else {
  173. console.log('zh: H5 直接奖励');
  174. this.callBackFor_kaoJiao(index);
  175. return;
  176. }
  177. }else{
  178. this.callBackFor_kaoJiao(index);
  179. return;
  180. }
  181. // App.Facade.popView(TipPanelMediator, TipPanel, "The steak is burnt!", false);
  182. // cc.tween(this.rawSteakList[index]).to(0.5, {position: this.burntPos}).removeSelf().start()
  183. // this.rawSteakList[index] = null;
  184. return;
  185. }
  186. else if (this.rawSteakList[index].getComponent(Beef).m_state == BeefState.Cooked) {
  187. for (let i = 0; i < this.plateNum; i++) {
  188. if (!this.cookedSteakList[i]) {
  189. this.rawSteakList[index].targetOff(this.rawSteakList[index]);
  190. // this.rawSteakList[index].off(cc.Node.EventType.TOUCH_END, this.onClickRawBeef.bind(this, index), this);
  191. this.cookedSteakList[i] = this.rawSteakList[index];
  192. this.rawSteakList[index] = null;
  193. // 从烤架移动到盘子里面
  194. this.cookedSteakList[i].getComponent(Beef).setState(BeefState.Plate);
  195. this.cookedSteakList[i].on(cc.Node.EventType.TOUCH_END, this.onClickCookedBeef.bind(this, i), this);
  196. cc.tween(this.cookedSteakList[i]).to(0.5, { position: this.platePos[i] }).start()
  197. break;
  198. }
  199. }
  200. if (App.DataManager.guideStep == 1) {
  201. App.DataManager.guideStep++;
  202. App.LocalStorageUtil.setNumber(App.LocalStorageUtil.str_guideStep, App.DataManager.guideStep);
  203. guidePanel.getInstance().showCurGuide();
  204. }
  205. }
  206. }
  207. //烤焦了惩罚
  208. callBackFor_kaoJiao(index: number) {
  209. cc.director.resume();
  210. console.log('zh: 游戏已恢复');
  211. console.log('zh:callBackFor_kaoJiao 被触发 index = ' + index);
  212. App.Facade.popView(TipPanelMediator, TipPanel, "The steak is burnt!", false);
  213. cc.tween(this.rawSteakList[index]).to(0.5, { position: this.burntPos }).removeSelf().start()
  214. this.rawSteakList[index] = null;
  215. return;
  216. }
  217. // 点击熟牛排
  218. onClickCookedBeef(index: number) {
  219. if (App.DataManager.guideStep == 3) {
  220. App.DataManager.guideStep++;
  221. App.LocalStorageUtil.setNumber(App.LocalStorageUtil.str_guideStep, App.DataManager.guideStep);
  222. guidePanel.getInstance().showCurGuide();
  223. }
  224. // todo 移动到人
  225. // 移动到人之后,从数组里面删除
  226. let toPos = this.findFoodPos(this.cookedSteakList[index].getComponent(Beef).m_foodType);
  227. if (toPos) {
  228. let pos = this.node.convertToNodeSpaceAR(toPos);
  229. cc.tween(this.cookedSteakList[index]).to(0.5, { position: pos, scale: 0.5 }).removeSelf().start();
  230. this.cookedSteakList[index] = null;
  231. }
  232. }
  233. onClickYellowSouce() {
  234. for (let i = 0; i < this.cookedSteakList.length; i++) {
  235. if (this.cookedSteakList[i] && this.cookedSteakList[i].getComponent(Beef).m_state == BeefState.Cocumber) {
  236. this.cookedSteakList[i].getComponent(Beef).setState(BeefState.YellowSouce);
  237. break;
  238. }
  239. }
  240. }
  241. onClickCocumber() {
  242. if (App.DataManager.guideStep == 2) {
  243. App.DataManager.guideStep++;
  244. App.LocalStorageUtil.setNumber(App.LocalStorageUtil.str_guideStep, App.DataManager.guideStep);
  245. guidePanel.getInstance().showCurGuide();
  246. }
  247. for (let i = 0; i < this.cookedSteakList.length; i++) {
  248. if (this.cookedSteakList[i] && this.cookedSteakList[i].getComponent(Beef).m_state == BeefState.Plate) {
  249. this.cookedSteakList[i].getComponent(Beef).setState(BeefState.Cocumber);
  250. break;
  251. }
  252. }
  253. }
  254. onClickTomato() {
  255. for (let i = 0; i < this.cookedSteakList.length; i++) {
  256. if (this.cookedSteakList[i] && this.cookedSteakList[i].getComponent(Beef).m_state == BeefState.Plate) {
  257. this.cookedSteakList[i].getComponent(Beef).setState(BeefState.Tomato);
  258. break;
  259. }
  260. }
  261. }
  262. onClickRedSouce() {
  263. for (let i = 0; i < this.cookedSteakList.length; i++) {
  264. if (this.cookedSteakList[i] && this.cookedSteakList[i].getComponent(Beef).m_state == BeefState.Tomato) {
  265. this.cookedSteakList[i].getComponent(Beef).setState(BeefState.RedSouce);
  266. break;
  267. }
  268. }
  269. }
  270. onClickDrinksMachine() {
  271. this.pDrinkBar.node.active = true;
  272. this.liuShui.active = true;
  273. App.SpinManager.PlaySpinAnimation(this.liuShui, "newAnimation", true, null);
  274. App.SpinManager.PlaySpinAnimation(this.drinkCup, "newAnimation", false, null);
  275. this.pDrinkBar.progress = 0;
  276. cc.tween(this.pDrinkBar).to(this.m_shuiLiuTime, { progress: 1 }).call(() => {
  277. this.pDrinkBar.node.active = false;
  278. this.liuShui.active = false;
  279. this.drinkCup.getComponent(sp.Skeleton).animation = null;
  280. for (let i = 0; i < this.cupNum; i++) {
  281. if (!this.drinkCupList[i]) {
  282. let cup = cc.instantiate(this.fullCup);
  283. cup.setPosition(this.drinkCup.getPosition());
  284. this.node.addChild(cup);
  285. cup.zIndex = this.cupNum - i;
  286. this.drinkCupList[i] = cup;
  287. cup.on(cc.Node.EventType.TOUCH_END, this.onClickFullCup.bind(this, i), this);
  288. if (i == 0) {
  289. cc.tween(cup).to(0.5, { position: this.cupPos[0] }).start();
  290. }
  291. else {
  292. cc.tween(cup).to(0.5, { position: this.cupPos[0] }).to(0.2, { position: this.cupPos[i] }).start();
  293. }
  294. }
  295. }
  296. }).start();
  297. }
  298. // 点击饮料
  299. onClickFullCup(index: number) {
  300. // todo 移动到人
  301. if (App.DataManager.guideStep == 4) {
  302. App.DataManager.guideStep++;
  303. App.LocalStorageUtil.setNumber(App.LocalStorageUtil.str_guideStep, App.DataManager.guideStep);
  304. guidePanel.getInstance().hideGuideStep();
  305. }
  306. // 移动到人之后,从数组里面删除
  307. let toPos = this.findFoodPos(FoodType.Drink);
  308. if (toPos) {
  309. let pos = this.node.convertToNodeSpaceAR(toPos);
  310. cc.tween(this.drinkCupList[index]).to(0.3, { position: pos, scale: 0.5 }).removeSelf().start();
  311. this.drinkCupList[index] = null;
  312. // this.m_machineState = DrinksMachineState.None;
  313. this.onClickDrinksMachine();
  314. }
  315. }
  316. onDisable() {
  317. this.btnShengNiuPai.node.off(cc.Node.EventType.TOUCH_END, this.onClickShengNiuPai.bind(this), this);
  318. this.btnYellowSouce.node.off(cc.Node.EventType.TOUCH_END, this.onClickYellowSouce.bind(this), this);
  319. this.btnCocumber.node.off(cc.Node.EventType.TOUCH_END, this.onClickCocumber.bind(this), this);
  320. this.btnTomato.node.off(cc.Node.EventType.TOUCH_END, this.onClickTomato.bind(this), this);
  321. this.btnRedSouce.node.off(cc.Node.EventType.TOUCH_END, this.onClickRedSouce.bind(this), this);
  322. }
  323. // 找到点这个食物剩余时间最少的人
  324. findFoodPos(foodt: FoodType): cc.Vec3 {
  325. let curPerson = null;
  326. let personIndex = -1;
  327. let lessLeftTime = 100;
  328. for (let i = 0; i < this.maxSitPersonNum; i++) {
  329. if (this.personList[i] && this.personList[i].getComponent(Person).m_state == PersonState.Waite) {
  330. // 点餐种类
  331. let fTypelist = this.personList[i].getComponent(Person).foodTypeList;
  332. let time = this.personList[i].getComponent(Person).leftTime;
  333. for (let j = 0; j < fTypelist.length; j++) {
  334. if (foodt == fTypelist[j] && lessLeftTime > time) {
  335. lessLeftTime = time;
  336. personIndex = i;
  337. curPerson = this.personList[i];
  338. }
  339. }
  340. }
  341. }
  342. if (curPerson) {
  343. let worldPos = curPerson.getComponent(Person).getFoodWorldPos(foodt);
  344. if (curPerson.getComponent(Person).judgeFoodFull()) {
  345. this.fullPersonNum++;
  346. let smallLevel = App.DataManager.PassProgress[App.DataManager.CurrentIntoLevel];
  347. if (this.fullPersonNum == App.DataManager.personCount[App.DataManager.CurrentIntoLevel - 1][smallLevel - 1]) {
  348. // GameSuccess
  349. this.personList[personIndex] = null;
  350. let rand = Math.round(Math.random());
  351. cc.tween(curPerson).delay(0.6).call(() => {
  352. curPerson.getComponent(Person).setState(PersonState.FullOut);
  353. if (rand == 1) {
  354. curPerson.getChildByName("renwu").scaleX = -0.5;
  355. }
  356. }).to(this.personWalkTime, { position: this.bornPos[rand] }).call(() => {
  357. curPerson.removeFromParent();
  358. }).start();
  359. gameScene.instance.GameSuccess();
  360. }
  361. else {
  362. let rand = Math.round(Math.random());
  363. this.personList[personIndex] = null
  364. cc.tween(curPerson).delay(0.6).call(() => {
  365. curPerson.getComponent(Person).setState(PersonState.FullOut);
  366. if (rand == 1) {
  367. curPerson.getChildByName("renwu").scaleX = -0.5;
  368. }
  369. }).to(this.personWalkTime, { position: this.bornPos[rand] }).call(() => {
  370. curPerson.removeFromParent();
  371. }).start();
  372. }
  373. }
  374. return worldPos;
  375. }
  376. return null;
  377. }
  378. update(dt) {
  379. if (!gameScene.instance.GameStutas) return;
  380. this.passTime += dt;
  381. let smallLevel = App.DataManager.PassProgress[App.DataManager.CurrentIntoLevel];
  382. if (this.passTime >= this.fixTime && this.createpPersonNum < App.DataManager.personCount[App.DataManager.CurrentIntoLevel - 1][smallLevel - 1]) {
  383. this.passTime = 0;
  384. this.fixTime = 1;
  385. console.log("aaa-------randomIndex---------", smallLevel);
  386. for (let i = 0; i < this.maxSitPersonNum; i++) {
  387. if (!this.personList[i]) {
  388. let randomIndex = Math.floor(Math.random() * this.renwuPerfabList.length);
  389. let person = cc.instantiate(this.renwuPerfabList[randomIndex])
  390. person.getComponent(Person).setState(PersonState.Comming);
  391. person.zIndex = -1;
  392. this.node.addChild(person);
  393. this.personList[i] = person;
  394. let rand = Math.round(Math.random());
  395. person.setPosition(this.bornPos[rand]);
  396. if (rand == 0) {
  397. person.getChildByName("renwu").scaleX = -0.5;
  398. }
  399. App.SpinManager.PlaySpinAnimation(person.getChildByName("renwu"), "newAnimation", true);
  400. cc.tween(person).to(this.personWalkTime, { position: this.personPos[i] }).call(() => {
  401. person.getChildByName("renwu").scaleX = 0.5;
  402. person.getComponent(Person).setState(PersonState.Waite);
  403. }).start();
  404. this.createpPersonNum++;
  405. let smallLevel = App.DataManager.PassProgress[App.DataManager.CurrentIntoLevel];
  406. gameScene.instance.updateLeftPeople(App.DataManager.personCount[App.DataManager.CurrentIntoLevel - 1][smallLevel - 1] - this.createpPersonNum);
  407. break;
  408. }
  409. }
  410. }
  411. }
  412. }