Table.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. const {ccclass, property} = cc._decorator;
  7. @ccclass
  8. export default class Table extends cc.Component {
  9. private static _instance: Table = null;
  10. // @property([cc.SpriteFrame])
  11. // private tableSpriteFrame: cc.SpriteFrame[] = [];
  12. @property(cc.Button)
  13. btnShengNiuPai: cc.Button = null;
  14. rawSteakList: cc.Node[] = [null, null, null, null];
  15. cookedSteakList: cc.Node[] = [null, null, null, null];
  16. @property(cc.Prefab)
  17. prefabBeef: cc.Prefab = null;
  18. @property(cc.Button)
  19. btnYellowSouce: cc.Button = null;
  20. @property(cc.Button)
  21. btnCocumber: cc.Button = null;
  22. @property(cc.Button)
  23. btnTomato: cc.Button = null;
  24. @property(cc.Button)
  25. btnRedSouce: cc.Button = null;
  26. @property(cc.Node)
  27. liuShui: cc.Node = null;
  28. @property(cc.Node)
  29. drinkCup: cc.Node = null;
  30. @property([cc.Node])
  31. kaoPanGaiZi: cc.Node[] = [];
  32. @property([cc.Node])
  33. plateGaiZi: cc.Node[] = [];
  34. @property([cc.Node])
  35. cupGaiZi: cc.Node[] = [];
  36. @property(cc.ProgressBar)
  37. pDrinkBar: cc.ProgressBar = null;
  38. m_shuiLiuTime = 3;
  39. @property(cc.Prefab)
  40. fullCup: cc.Prefab = null;
  41. drinkCupList: cc.Node[] = [];
  42. kaoPanNum = 2;
  43. plateNum = 2;
  44. cupNum = 3;
  45. beefBornPos: cc.Vec3[] = [cc.v3(-290, -185, 0), cc.v3(-197, -195, 0), cc.v3(-243, -131, 0), cc.v3(-160, -133, 0)];
  46. platePos: cc.Vec3[] = [cc.v3(-58, -200, 0), cc.v3(80, -201, 0), cc.v3(-52, -126, 0), cc.v3(68, -131, 0)];
  47. burntPos: cc.Vec3 = cc.v3(-360, -640, 0);
  48. cupPos: cc.Vec3[] = [cc.v3(258, -209, 0), cc.v3(232, -161, 0), cc.v3(205, -115, 0)];
  49. // ------------------------person---------------------------
  50. @property([cc.Prefab])
  51. renwuPerfabList:cc.Prefab[] = [];
  52. personList: cc.Node[] = [];
  53. // 点餐位置
  54. personPos:cc.Vec3[] = [cc.v3(30, 38, 0), cc.v3(260, 38, 0), cc.v3(-200, 38, 0)];
  55. // 人物出生位置
  56. bornPos: cc.Vec3[] = [cc.v3(-500, 38, 0), cc.v3(550, 38, 0)];
  57. fixTime = 1;
  58. passTime = 0;
  59. personWalkTime = 2;
  60. createpPersonNum = 0; //已经来了到少人
  61. fullPersonNum = 0; //多少吃饱了
  62. maxSitPersonNum = 3; //总共只能坐下人数
  63. // LIFE-CYCLE CALLBACKS:
  64. public static getInstance(): any {
  65. if (this._instance == null) {
  66. this._instance = new Table();
  67. }
  68. return this._instance;
  69. }
  70. onLoad () {
  71. Table._instance = this;
  72. this.btnShengNiuPai.node.on(cc.Node.EventType.TOUCH_END, this.onClickShengNiuPai.bind(this), this);
  73. this.btnYellowSouce.node.on(cc.Node.EventType.TOUCH_END, this.onClickYellowSouce.bind(this), this);
  74. this.btnCocumber.node.on(cc.Node.EventType.TOUCH_END, this.onClickCocumber.bind(this), this);
  75. this.btnTomato.node.on(cc.Node.EventType.TOUCH_END, this.onClickTomato.bind(this), this);
  76. this.btnRedSouce.node.on(cc.Node.EventType.TOUCH_END, this.onClickRedSouce.bind(this), this);
  77. // this.drinksMachine.node.on(cc.Node.EventType.TOUCH_END, this.onClickDrinksMachine.bind(this), this);
  78. for (let i = 0; i < this.maxSitPersonNum; i++) {
  79. this.personList[i] = null;
  80. }
  81. this.plateNum = App.DataManager.kitchenNum[0][App.DataManager.kinchenLevel[0]];
  82. this.kaoPanNum = App.DataManager.kitchenNum[1][App.DataManager.kinchenLevel[1]];
  83. this.cupNum = App.DataManager.kitchenNum[2][App.DataManager.kinchenLevel[2]];
  84. }
  85. start () {
  86. for (let i = 0; i < this.kaoPanNum - 2; i++) {
  87. this.kaoPanGaiZi[i].active = false
  88. }
  89. for (let i = 0; i < this.plateNum - 2; i++) {
  90. this.plateGaiZi[i].active = true
  91. }
  92. for (let i = 0; i < this.cupNum - 1; i++) {
  93. this.cupGaiZi[i].active = true
  94. }
  95. if(App.DataManager.CurrentIntoLevel >= 3){
  96. this.btnTomato.node.active = true;
  97. }
  98. if(App.DataManager.CurrentIntoLevel >= 5){
  99. this.btnYellowSouce.node.active = true;
  100. }
  101. if(App.DataManager.CurrentIntoLevel >= 7){
  102. this.btnRedSouce.node.active = true;
  103. }
  104. this.onClickDrinksMachine();
  105. if(App.DataManager.guideStep == 0){
  106. guidePanel.getInstance().showCurGuide();
  107. }
  108. }
  109. onClickShengNiuPai(){
  110. if(App.DataManager.guideStep == 0){
  111. App.DataManager.guideStep++;
  112. App.LocalStorageUtil.setNumber(App.LocalStorageUtil.str_guideStep, App.DataManager.guideStep);
  113. guidePanel.getInstance().hideGuideStep();
  114. }
  115. for (let i = 0; i < this.kaoPanNum; i++) {
  116. if(this.rawSteakList[i] == null){
  117. let beef = cc.instantiate(this.prefabBeef);
  118. beef.on(cc.Node.EventType.TOUCH_END, this.onClickRawBeef.bind(this, i), beef);
  119. beef.setPosition(this.beefBornPos[i]);
  120. this.node.addChild(beef);
  121. this.rawSteakList[i] = beef;
  122. break;
  123. }
  124. }
  125. }
  126. onClickRawBeef(index: number){
  127. if(this.rawSteakList[index].getComponent(Beef).m_state == BeefState.Burnt){
  128. cc.tween(this.rawSteakList[index]).to(0.5, {position: this.burntPos}).removeSelf().start()
  129. this.rawSteakList[index] = null;
  130. return;
  131. }
  132. else if(this.rawSteakList[index].getComponent(Beef).m_state == BeefState.Cooked){
  133. for (let i = 0; i < this.plateNum; i++) {
  134. if(!this.cookedSteakList[i])
  135. {
  136. this.rawSteakList[index].targetOff(this.rawSteakList[index]);
  137. // this.rawSteakList[index].off(cc.Node.EventType.TOUCH_END, this.onClickRawBeef.bind(this, index), this);
  138. this.cookedSteakList[i] = this.rawSteakList[index];
  139. this.rawSteakList[index] = null;
  140. // 从烤架移动到盘子里面
  141. this.cookedSteakList[i].getComponent(Beef).setState(BeefState.Plate);
  142. this.cookedSteakList[i].on(cc.Node.EventType.TOUCH_END, this.onClickCookedBeef.bind(this, i), this);
  143. cc.tween(this.cookedSteakList[i]).to(0.5, {position: this.platePos[i]}).start()
  144. break;
  145. }
  146. }
  147. if(App.DataManager.guideStep == 1){
  148. App.DataManager.guideStep++;
  149. App.LocalStorageUtil.setNumber(App.LocalStorageUtil.str_guideStep, App.DataManager.guideStep);
  150. guidePanel.getInstance().showCurGuide();
  151. }
  152. }
  153. }
  154. // 点击熟牛排
  155. onClickCookedBeef(index: number){
  156. if(App.DataManager.guideStep == 3){
  157. App.DataManager.guideStep++;
  158. App.LocalStorageUtil.setNumber(App.LocalStorageUtil.str_guideStep, App.DataManager.guideStep);
  159. guidePanel.getInstance().showCurGuide();
  160. }
  161. // todo 移动到人
  162. // 移动到人之后,从数组里面删除
  163. let toPos = this.findFoodPos(this.cookedSteakList[index].getComponent(Beef).m_foodType);
  164. if(toPos){
  165. let pos = this.node.convertToNodeSpaceAR(toPos);
  166. cc.tween(this.cookedSteakList[index]).to(0.5, {position: pos, scale: 0.5}).removeSelf().start();
  167. this.cookedSteakList[index] = null;
  168. }
  169. }
  170. onClickYellowSouce(){
  171. for (let i = 0; i < this.cookedSteakList.length; i++) {
  172. if(this.cookedSteakList[i] && this.cookedSteakList[i].getComponent(Beef).m_state == BeefState.Cocumber){
  173. this.cookedSteakList[i].getComponent(Beef).setState(BeefState.YellowSouce);
  174. break;
  175. }
  176. }
  177. }
  178. onClickCocumber(){
  179. if(App.DataManager.guideStep == 2){
  180. App.DataManager.guideStep++;
  181. App.LocalStorageUtil.setNumber(App.LocalStorageUtil.str_guideStep, App.DataManager.guideStep);
  182. guidePanel.getInstance().showCurGuide();
  183. }
  184. for (let i = 0; i < this.cookedSteakList.length; i++) {
  185. if(this.cookedSteakList[i] && this.cookedSteakList[i].getComponent(Beef).m_state == BeefState.Plate){
  186. this.cookedSteakList[i].getComponent(Beef).setState(BeefState.Cocumber);
  187. break;
  188. }
  189. }
  190. }
  191. onClickTomato(){
  192. for (let i = 0; i < this.cookedSteakList.length; i++) {
  193. if(this.cookedSteakList[i] && this.cookedSteakList[i].getComponent(Beef).m_state == BeefState.Plate){
  194. this.cookedSteakList[i].getComponent(Beef).setState(BeefState.Tomato);
  195. break;
  196. }
  197. }
  198. }
  199. onClickRedSouce(){
  200. for (let i = 0; i < this.cookedSteakList.length; i++) {
  201. if(this.cookedSteakList[i] && this.cookedSteakList[i].getComponent(Beef).m_state == BeefState.Tomato){
  202. this.cookedSteakList[i].getComponent(Beef).setState(BeefState.RedSouce);
  203. break;
  204. }
  205. }
  206. }
  207. onClickDrinksMachine(){
  208. this.pDrinkBar.node.active = true;
  209. this.liuShui.active = true;
  210. App.SpinManager.PlaySpinAnimation(this.liuShui, "newAnimation", true, null);
  211. App.SpinManager.PlaySpinAnimation(this.drinkCup, "newAnimation", false, null);
  212. this.pDrinkBar.progress = 0;
  213. cc.tween(this.pDrinkBar).to(this.m_shuiLiuTime, {progress: 1}).call(()=>{
  214. this.pDrinkBar.node.active = false;
  215. this.liuShui.active = false;
  216. this.drinkCup.getComponent(sp.Skeleton).animation = null;
  217. for (let i = 0; i < this.cupNum; i++) {
  218. if(!this.drinkCupList[i])
  219. {
  220. let cup = cc.instantiate(this.fullCup);
  221. cup.setPosition(this.drinkCup.getPosition());
  222. this.node.addChild(cup);
  223. cup.zIndex = this.cupNum - i;
  224. this.drinkCupList[i] = cup;
  225. cup.on(cc.Node.EventType.TOUCH_END, this.onClickFullCup.bind(this, i), this);
  226. if(i == 0){
  227. cc.tween(cup).to(0.5, {position: this.cupPos[0]}).start();
  228. }
  229. else{
  230. cc.tween(cup).to(0.5, {position: this.cupPos[0]}).to(0.2, {position: this.cupPos[i]}).start();
  231. }
  232. }
  233. }
  234. }).start();
  235. }
  236. // 点击饮料
  237. onClickFullCup(index: number){
  238. // todo 移动到人
  239. if(App.DataManager.guideStep == 4){
  240. App.DataManager.guideStep++;
  241. App.LocalStorageUtil.setNumber(App.LocalStorageUtil.str_guideStep, App.DataManager.guideStep);
  242. guidePanel.getInstance().hideGuideStep();
  243. }
  244. // 移动到人之后,从数组里面删除
  245. let toPos = this.findFoodPos(FoodType.Drink);
  246. if(toPos){
  247. let pos = this.node.convertToNodeSpaceAR(toPos);
  248. cc.tween(this.drinkCupList[index]).to(0.3, {position: pos, scale: 0.5}).removeSelf().start();
  249. this.drinkCupList[index] = null;
  250. // this.m_machineState = DrinksMachineState.None;
  251. this.onClickDrinksMachine();
  252. }
  253. }
  254. onDisable() {
  255. this.btnShengNiuPai.node.off(cc.Node.EventType.TOUCH_END, this.onClickShengNiuPai.bind(this), this);
  256. this.btnYellowSouce.node.off(cc.Node.EventType.TOUCH_END, this.onClickYellowSouce.bind(this), this);
  257. this.btnCocumber.node.off(cc.Node.EventType.TOUCH_END, this.onClickCocumber.bind(this), this);
  258. this.btnTomato.node.off(cc.Node.EventType.TOUCH_END, this.onClickTomato.bind(this), this);
  259. this.btnRedSouce.node.off(cc.Node.EventType.TOUCH_END, this.onClickRedSouce.bind(this), this);
  260. }
  261. // 找到点这个食物剩余时间最少的人
  262. findFoodPos(foodt: FoodType): cc.Vec3 {
  263. let curPerson = null;
  264. let personIndex = -1;
  265. let lessLeftTime = 100;
  266. for (let i = 0; i < this.maxSitPersonNum; i++) {
  267. if(this.personList[i] && this.personList[i].getComponent(Person).m_state == PersonState.Waite){
  268. // 点餐种类
  269. let fTypelist = this.personList[i].getComponent(Person).foodTypeList;
  270. let time = this.personList[i].getComponent(Person).leftTime;
  271. for (let j = 0; j < fTypelist.length; j++) {
  272. if(foodt == fTypelist[j] && lessLeftTime > time){
  273. lessLeftTime = time;
  274. personIndex = i;
  275. curPerson = this.personList[i];
  276. }
  277. }
  278. }
  279. }
  280. if(curPerson){
  281. let worldPos = curPerson.getComponent(Person).getFoodWorldPos(foodt);
  282. if(curPerson.getComponent(Person).judgeFoodFull()){
  283. this.fullPersonNum++;
  284. let smallLevel = App.DataManager.PassProgress[App.DataManager.CurrentIntoLevel];
  285. if(this.fullPersonNum == App.DataManager.personCount[App.DataManager.CurrentIntoLevel - 1][smallLevel - 1]){
  286. // GameSuccess
  287. this.personList[personIndex] = null;
  288. let rand = Math.round(Math.random());
  289. cc.tween(curPerson).delay(0.6).call(()=>{
  290. curPerson.getComponent(Person).setState(PersonState.FullOut);
  291. if(rand == 1){
  292. curPerson.getChildByName("renwu").scaleX = -0.5;
  293. }
  294. }).to( this.personWalkTime, {position: this.bornPos[rand]}).call(()=>{
  295. curPerson.removeFromParent();
  296. }).start();
  297. gameScene.instance.GameSuccess();
  298. }
  299. else{
  300. let rand = Math.round(Math.random());
  301. this.personList[personIndex] = null
  302. cc.tween(curPerson).delay(0.6).call(()=>{
  303. curPerson.getComponent(Person).setState(PersonState.FullOut);
  304. if(rand == 1){
  305. curPerson.getChildByName("renwu").scaleX = -0.5;
  306. }
  307. }).to( this.personWalkTime, {position: this.bornPos[rand]}).call(()=>{
  308. curPerson.removeFromParent();
  309. }).start();
  310. }
  311. }
  312. return worldPos;
  313. }
  314. return null;
  315. }
  316. update (dt) {
  317. if (!gameScene.instance.GameStutas) return;
  318. this.passTime+=dt;
  319. let smallLevel = App.DataManager.PassProgress[App.DataManager.CurrentIntoLevel];
  320. if(this.passTime >= this.fixTime && this.createpPersonNum < App.DataManager.personCount[App.DataManager.CurrentIntoLevel - 1][smallLevel - 1]){
  321. this.passTime = 0;
  322. this.fixTime = 1;
  323. console.log("aaa-------randomIndex---------", smallLevel);
  324. for (let i = 0; i < this.maxSitPersonNum; i++) {
  325. if(!this.personList[i]){
  326. let randomIndex = Math.floor(Math.random() * this.renwuPerfabList.length);
  327. let person = cc.instantiate(this.renwuPerfabList[randomIndex])
  328. person.getComponent(Person).setState(PersonState.Comming);
  329. person.zIndex = -1;
  330. this.node.addChild(person);
  331. this.personList[i] = person;
  332. let rand = Math.round(Math.random());
  333. person.setPosition(this.bornPos[rand]);
  334. if(rand == 0){
  335. person.getChildByName("renwu").scaleX = -0.5;
  336. }
  337. App.SpinManager.PlaySpinAnimation(person.getChildByName("renwu"), "newAnimation", true);
  338. cc.tween(person).to(this.personWalkTime, {position: this.personPos[i]}).call(()=>{
  339. person.getChildByName("renwu").scaleX = 0.5;
  340. person.getComponent(Person).setState(PersonState.Waite);
  341. }).start();
  342. this.createpPersonNum++;
  343. let smallLevel = App.DataManager.PassProgress[App.DataManager.CurrentIntoLevel];
  344. gameScene.instance.updateLeftPeople(App.DataManager.personCount[App.DataManager.CurrentIntoLevel - 1][smallLevel - 1] - this.createpPersonNum);
  345. break;
  346. }
  347. }
  348. }
  349. }
  350. }