main.ts 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. import { _decorator, Component, Node, tween, CCInteger, Vec3, CCFloat, find, EventTouch, instantiate, Layers, resources, Sprite, SpriteFrame, UITransform, Label, director, Scene, sys, assetManager, AudioClip, AudioSource } from 'cc';
  2. import { genPlant } from './genPlant';
  3. import { timer } from './timer';
  4. import {
  5. paiHang,
  6. param, wechatAd
  7. } from './loadData';
  8. const { ccclass, property } = _decorator;
  9. let myPackBox: Node, mySaleBox: Node, storeBox: Node, paiHangBox: Node, msgBox: Node = null;
  10. let userAvata: Node, nickName: Node, jinbi: Node = null;
  11. let mainAd1, mainAd2, mainAd3 = null;
  12. //0 橘子香蕉西红柿幼苗,1 红富士苹果幼苗,2 紫金冠茄幼苗,3 红森胡萝卜幼苗
  13. @ccclass('main')
  14. export class main extends Component {
  15. public static addSalePackCount: number = 0;
  16. public static addPackCount: number = 0;
  17. public static saleBox: Node = null;
  18. // @property([Node])
  19. // fingerFlows
  20. start() {
  21. director.preloadScene("main");
  22. //login顶部banner广告
  23. if (sys.platform === sys.Platform.WECHAT_GAME && director.getScene().name === "login") {
  24. let { screenWidth } = wx.getSystemInfoSync()
  25. mainAd1 = wx.createBannerAd({
  26. adUnitId: wechatAd[0].adid,
  27. style: {
  28. left: 0,
  29. top: 0,
  30. width: screenWidth,
  31. },
  32. //自动刷新
  33. adIntervals: 30,
  34. });
  35. if (mainAd1 !== null) {
  36. mainAd1.show();
  37. mainAd1.onError(err => {
  38. console.log(err)
  39. })
  40. }
  41. }
  42. if (sys.platform === sys.Platform.WECHAT_GAME && director.getScene().name === "main") {
  43. let { screenWidth, screenHeight } = wx.getSystemInfoSync()
  44. //main场景插屏广告
  45. mainAd3 = wx.createInterstitialAd({
  46. adUnitId: wechatAd[2].adid,
  47. });
  48. if (mainAd3 !== null) {
  49. mainAd3.show();
  50. mainAd3.onError(err => {
  51. console.log(err)
  52. })
  53. }
  54. //main场景底部banner广告
  55. mainAd2 = wx.createBannerAd({
  56. adUnitId: wechatAd[1].adid,
  57. style: {
  58. left: 0,
  59. top: screenHeight - 60,
  60. width: screenWidth,
  61. },
  62. //自动刷新
  63. adIntervals: 30,
  64. });
  65. if (mainAd2 !== null) {
  66. mainAd2.show();
  67. mainAd2.onError(err => {
  68. console.log(err)
  69. })
  70. }
  71. }
  72. //手指跟随
  73. // const finger = find("MainCanvas/op/finger");
  74. // this.fingerFlows.forEach(item => {
  75. // item.on(Node.EventType.TOUCH_END, function (event: EventTouch) {
  76. // finger.parent.active = true;
  77. // const position = item.getPosition();
  78. // finger.setPosition(new Vec3(position.x + 30, position.y - 30, 0));
  79. // }, this);
  80. // });
  81. }
  82. clickLoginBtn() {
  83. const btn = find("Canvas/btnClick");
  84. btn.getComponent(AudioSource).play();
  85. }
  86. clickBtn() {
  87. const btn = find("MainCanvas/btnClick");
  88. btn.getComponent(AudioSource).play();
  89. }
  90. login() {
  91. director.loadScene("main", () => {
  92. //游戏开始倒计时
  93. timer.isStart = true;
  94. this.init();
  95. nickName.getComponent(Label).string = param.nickname;
  96. resources.load(param.avata, SpriteFrame, (err, spriteFrame) => {
  97. userAvata.getComponent(Sprite).sizeMode = Sprite.SizeMode.CUSTOM;
  98. userAvata.getComponent(Sprite).spriteFrame = spriteFrame;
  99. });
  100. jinbi.getComponent(Label).string = param.money.toString();
  101. timer.time = param.levelTime;
  102. if (sys.platform === sys.Platform.WECHAT_GAME) {
  103. if (mainAd1 !== null) {
  104. mainAd1.hide();
  105. }
  106. if (mainAd2 !== null) {
  107. mainAd2.hide();
  108. }
  109. }
  110. });
  111. }
  112. //类型,索引
  113. static addSalePack(type: number) {
  114. let sale_item = new Node();
  115. //成熟果实仅仅可以售卖 只入包一次
  116. if (type === 10) {
  117. sale_item = instantiate(mySaleBox.getChildByName("items").getChildByName("fanqie_base"));
  118. sale_item.name = "fanqie";
  119. }
  120. if (type === 11) {
  121. sale_item = instantiate(mySaleBox.getChildByName("items").getChildByName("pingguo_base"));
  122. sale_item.name = "pingguo";
  123. }
  124. if (type === 12) {
  125. sale_item = instantiate(mySaleBox.getChildByName("items").getChildByName("qiezi_base"));
  126. sale_item.name = "qiezi";
  127. }
  128. if (type === 13) {
  129. sale_item = instantiate(mySaleBox.getChildByName("items").getChildByName("huluobo_base"));
  130. sale_item.name = "huluobo";
  131. }
  132. main.addSalePackCount++;
  133. sale_item.active = true;
  134. sale_item.layer = Layers.Enum.UI_2D;
  135. sale_item.setParent(mySaleBox);
  136. //果实背包只放一条记录,后期改成scrollview组件即可,多条的话会出现位置不对的bug
  137. sale_item.setPosition(-60, 271 - 189 * main.addSalePackCount, 0);
  138. mySaleBox.getChildByName("items").addChild(sale_item);
  139. }
  140. static addPack(type: number) {
  141. //动态添加背包
  142. //种子 仅仅可以种植
  143. let clone_item = new Node();
  144. if (type === 0) {
  145. clone_item = instantiate(myPackBox.getChildByName("items").getChildByName("fanqiezz_base"));
  146. clone_item.name = "fanqiezz";
  147. }
  148. if (type === 1) {
  149. clone_item = instantiate(myPackBox.getChildByName("items").getChildByName("pingguozz_base"));
  150. clone_item.name = "pingguozz";
  151. }
  152. if (type === 2) {
  153. clone_item = instantiate(myPackBox.getChildByName("items").getChildByName("qiezizz_base"));
  154. clone_item.name = "qiezizz";
  155. }
  156. if (type === 3) {
  157. clone_item = instantiate(myPackBox.getChildByName("items").getChildByName("huluobozz_base"));
  158. clone_item.name = "huluobozz";
  159. }
  160. main.addPackCount++;
  161. clone_item.active = true;
  162. clone_item.layer = Layers.Enum.UI_2D;
  163. clone_item.setParent(myPackBox);
  164. clone_item.setPosition(-60, 271 - 189 * main.addPackCount, 0);
  165. myPackBox.getChildByName("items").addChild(clone_item);
  166. }
  167. addPaiHang() {
  168. //动态添加排行榜
  169. paiHang.forEach((item, index) => {
  170. const clone_item = instantiate(paiHangBox.getChildByName("items"));
  171. clone_item.layer = Layers.Enum.UI_2D;
  172. clone_item.setParent(paiHangBox);
  173. clone_item.setPosition(0, 0 - clone_item.position.y - 240 * index, 0);
  174. const name = clone_item.getChildByName("box")
  175. .getChildByName("txtBox")
  176. .getChildByName("name");
  177. name.getComponent(Label).string = item.nickName;
  178. resources.load(item.top === 1 ? "1/spriteFrame" : item.top === 2 ? "2/spriteFrame" : "3/spriteFrame", SpriteFrame, (err, spriteFrame) => {
  179. const top = clone_item.getChildByName("box")
  180. .getChildByName("txtBox")
  181. .getChildByName("top");
  182. top.getComponent(Sprite).sizeMode = Sprite.SizeMode.CUSTOM;
  183. top.getComponent(Sprite).spriteFrame = spriteFrame;
  184. });
  185. resources.load(item.userAvata, SpriteFrame, (err, spriteFrame) => {
  186. const sp = clone_item.getChildByName("box")
  187. .getChildByName("txtBox")
  188. .getChildByName("pro");
  189. sp.getComponent(Sprite).sizeMode = Sprite.SizeMode.CUSTOM;
  190. sp.getComponent(Sprite).spriteFrame = spriteFrame;
  191. });
  192. paiHangBox.addChild(clone_item);
  193. });
  194. }
  195. init() {
  196. //种子背包
  197. myPackBox = find("MainCanvas/popBox/mypack"),
  198. //果实背包
  199. mySaleBox = find("MainCanvas/popBox/mysalepack"),
  200. storeBox = find("MainCanvas/popBox/store"),
  201. paiHangBox = find("MainCanvas/popBox/paihang"),
  202. msgBox = find("MainCanvas/popBox/msgBox");
  203. userAvata = find("MainCanvas/touxiangkuang/userAvata"),
  204. nickName = find("MainCanvas/touxiangkuang/nickname"),
  205. jinbi = find("MainCanvas/jinbi/boxbg/txt");
  206. //默认添加胡萝卜到商店植物 胡萝卜最便宜
  207. genPlant._genPlant.addPlants({
  208. name: "huluobo",
  209. img: "huluobozz",
  210. chengshuImg: "huluobo"
  211. });
  212. main.saleBox = mySaleBox;
  213. main.addPackCount = 0;
  214. main.addSalePackCount = 0;
  215. }
  216. update(deltaTime: number) {
  217. }
  218. //弹窗
  219. showDialog(event: EventTouch) {
  220. switch (event.currentTarget.name) {
  221. case "package":
  222. myPackBox.active = true;
  223. if (sys.platform === sys.Platform.WECHAT_GAME) {
  224. //种子背包原生模板
  225. let mainAd8 = wx.createCustomAd({
  226. adUnitId: wechatAd[8].adid,
  227. style: {
  228. left: 50,
  229. top: 550,
  230. width: 650,
  231. },
  232. //自动刷新
  233. adIntervals: 30,
  234. })
  235. if (mainAd8 !== null) {
  236. // 用户触发广告后,显示激励视频广告
  237. mainAd8.show();
  238. mainAd8.onError(err => {
  239. console.log(err)
  240. })
  241. }
  242. }
  243. break;
  244. case "package_guoshi":
  245. mySaleBox.active = true;
  246. if (sys.platform === sys.Platform.WECHAT_GAME) {
  247. if (mainAd2 !== null) {
  248. mainAd2.show();
  249. mainAd2.onError(err => {
  250. console.log(err)
  251. })
  252. }
  253. //果实背包原生模板
  254. let mainAd9 = wx.createCustomAd({
  255. adUnitId: wechatAd[9].adid,
  256. style: {
  257. left: 50,
  258. top: 550,
  259. width: 650,
  260. },
  261. //自动刷新
  262. adIntervals: 30,
  263. })
  264. if (mainAd9 !== null) {
  265. // 用户触发广告后,显示激励视频广告
  266. mainAd9.show();
  267. mainAd9.onError(err => {
  268. console.log(err)
  269. })
  270. }
  271. }
  272. break;
  273. case "zhishipai":
  274. storeBox.active = true;
  275. break;
  276. case "rongyaobang":
  277. this.addPaiHang();
  278. paiHangBox.active = true;
  279. break;
  280. }
  281. }
  282. closeDialog(event: EventTouch) {
  283. let type = event.currentTarget.parent.name + "_" + event.currentTarget.name;
  284. switch (type) {
  285. case "store_close":
  286. storeBox.active = false;
  287. break;
  288. case "mypack_close":
  289. myPackBox.active = false;
  290. break;
  291. case "mysalepack_close":
  292. mySaleBox.active = false;
  293. case "paihang_close":
  294. paiHangBox.active = false;
  295. break;
  296. }
  297. }
  298. //弹消息
  299. showMsg(msg: string) {
  300. const mg = msgBox.getChildByName("msgTxt").getComponent(Label);
  301. mg.string = msg;
  302. msgBox.active = true;
  303. this.scheduleOnce(function callback() {
  304. msgBox.active = false;
  305. }, 1);
  306. }
  307. //购买种子 进背包
  308. buyPlant(event: EventTouch) {
  309. //必须把种子背包的种子用完才可以购买种子
  310. const mySaleBoxItems = find("MainCanvas/popBox/mypack/items");
  311. if (mySaleBoxItems.children.length > 4) {
  312. this.showMsg("种子尚未使用完毕!");
  313. return;
  314. }
  315. const currentJinbi = parseInt(jinbi.getComponent(Label).string);
  316. let price = 0;
  317. if (event.currentTarget.parent.parent.name === "fanqiezz") {
  318. price = 300;
  319. if (currentJinbi - price <= 0) {
  320. this.showMsg("金币不足!"); return;
  321. } else {
  322. if (main.addPackCount > 3) {
  323. this.showMsg("种子背包已满!");
  324. return;
  325. }
  326. let arrIndex = myPackBox.getChildByName("items").children.findIndex(item => {
  327. return item.name === "fanqiezz"
  328. })
  329. if (arrIndex > -1) {
  330. this.showMsg("已购买此种子!");
  331. return;
  332. }
  333. main.addPack(0);
  334. jinbi.getComponent(Label).string = (currentJinbi - price).toString();
  335. myPackBox.active = false;
  336. this.showMsg("购买成功!"); return;
  337. }
  338. } if (event.currentTarget.parent.parent.name === "pingguozz") {
  339. price = 180;
  340. if (currentJinbi - price <= 0) {
  341. this.showMsg("金币不足!"); return;
  342. } else {
  343. if (main.addPackCount > 3) {
  344. this.showMsg("种子背包已满!");
  345. return;
  346. }
  347. let arrIndex = myPackBox.getChildByName("items").children.findIndex(item => {
  348. return item.name === "pingguozz"
  349. })
  350. if (arrIndex > -1) {
  351. this.showMsg("已购买此种子!");
  352. return;
  353. }
  354. main.addPack(1);
  355. jinbi.getComponent(Label).string = (currentJinbi - price).toString();
  356. myPackBox.active = false;
  357. this.showMsg("购买成功!"); return;
  358. }
  359. } if (event.currentTarget.parent.parent.name === "qiezizz") {
  360. price = 120;
  361. if (currentJinbi - price <= 0) {
  362. this.showMsg("金币不足!"); return;
  363. } else {
  364. if (main.addPackCount > 3) {
  365. this.showMsg("种子背包已满!");
  366. return;
  367. }
  368. let arrIndex = myPackBox.getChildByName("items").children.findIndex(item => {
  369. return item.name === "qiezizz"
  370. })
  371. if (arrIndex > -1) {
  372. this.showMsg("已购买此种子!");
  373. return;
  374. }
  375. main.addPack(2);
  376. jinbi.getComponent(Label).string = (currentJinbi - price).toString();
  377. myPackBox.active = false;
  378. this.showMsg("购买成功!"); return;
  379. }
  380. } if (event.currentTarget.parent.parent.name === "huluobozz") {
  381. price = 60;
  382. if (currentJinbi - price <= 0) {
  383. this.showMsg("金币不足!"); return;
  384. } else {
  385. if (main.addPackCount > 3) {
  386. this.showMsg("种子背包已满!");
  387. return;
  388. }
  389. let arrIndex = myPackBox.getChildByName("items").children.findIndex(item => {
  390. return item.name === "huluobozz"
  391. })
  392. if (arrIndex > -1) {
  393. this.showMsg("已购买此种子!");
  394. return;
  395. }
  396. main.addPack(3);
  397. jinbi.getComponent(Label).string = (currentJinbi - price).toString();
  398. myPackBox.active = false;
  399. this.showMsg("购买成功!"); return;
  400. }
  401. }
  402. }
  403. removePack(type: number) {
  404. //种子播种 减去背包
  405. if (type === 0) {
  406. myPackBox.getChildByName("items").getChildByName("fanqiezz").destroy();
  407. myPackBox.getChildByName("items").getChildByName("fanqiezz_base").active = false;
  408. }
  409. if (type === 1) {
  410. myPackBox.getChildByName("items").getChildByName("pingguozz").destroy();
  411. myPackBox.getChildByName("items").getChildByName("pingguozz_base").active = false;
  412. }
  413. if (type === 2) {
  414. myPackBox.getChildByName("items").getChildByName("qiezizz").destroy();
  415. myPackBox.getChildByName("items").getChildByName("qiezizz_base").active = false;
  416. }
  417. if (type === 3) {
  418. myPackBox.getChildByName("items").getChildByName("huluobozz").destroy();
  419. myPackBox.getChildByName("items").getChildByName("huluobozz_base").active = false;
  420. }
  421. //果实成熟 减去背包
  422. //因为用的场景中的Node 克隆的节点所以使用active来控制克隆节点的显示和隐藏
  423. if (type === 10) {
  424. mySaleBox.getChildByName("items").getChildByName("fanqie").destroy();
  425. mySaleBox.getChildByName("items").getChildByName("fanqie_base").active = false;
  426. }
  427. if (type === 11) {
  428. mySaleBox.getChildByName("items").getChildByName("pingguo").destroy();
  429. mySaleBox.getChildByName("items").getChildByName("pingguo_base").active = false;
  430. }
  431. if (type === 12) {
  432. mySaleBox.getChildByName("items").getChildByName("qiezi").destroy();
  433. mySaleBox.getChildByName("items").getChildByName("qiezi_base").active = false;
  434. }
  435. if (type === 13) {
  436. mySaleBox.getChildByName("items").getChildByName("huluobo").destroy();
  437. mySaleBox.getChildByName("items").getChildByName("huluobo_base").active = false;
  438. }
  439. }
  440. //出售果实 减去背包
  441. salePlant(event: EventTouch) {
  442. let price = 0;
  443. const currentJinbi = parseInt(jinbi.getComponent(Label).string);
  444. if (event.currentTarget.parent.parent.name === "fanqie") {
  445. price = 30;
  446. this.removePack(10);
  447. jinbi.getComponent(Label).string = (currentJinbi + price).toString();
  448. this.showMsg("出售果实成功!");
  449. } if (event.currentTarget.parent.parent.name === "pingguo") {
  450. price = 18;
  451. this.removePack(11);
  452. jinbi.getComponent(Label).string = (currentJinbi + price).toString();
  453. this.showMsg("出售果实成功!");
  454. } if (event.currentTarget.parent.parent.name === "qiezi") {
  455. price = 12;
  456. this.removePack(12);
  457. jinbi.getComponent(Label).string = (currentJinbi + price).toString();
  458. this.showMsg("出售果实成功!");
  459. } if (event.currentTarget.parent.parent.name === "huluobo") {
  460. price = 6;
  461. this.removePack(13);
  462. jinbi.getComponent(Label).string = (currentJinbi + price).toString();
  463. this.showMsg("出售果实成功!");
  464. }
  465. main.addSalePackCount--;
  466. }
  467. //播种,一次播6个种子,并且修改genPlant种子类型,土地添加种子
  468. boZhong(event: EventTouch) {
  469. let plants = find("MainCanvas/plant");
  470. let arrIndex = plants.children.findIndex(item => {
  471. return item.name === "huluobo" || item.name === "qiezi" || item.name === "pingguo" || item.name === "fanqie"
  472. })
  473. if (event.currentTarget.parent.parent.name === "fanqiezz") {
  474. if (arrIndex > -1) {
  475. this.showMsg("请将成熟的果实收获!");
  476. return;
  477. }
  478. genPlant._genPlant.addPlants({
  479. name: "fanqie",
  480. img: "fanqiezz",
  481. chengshuImg: "fanqie"
  482. });
  483. //播种后种子库移除,种子背包items数量减1,否则不能继续购买种子了
  484. this.removePack(0);
  485. main.addPackCount -= 1;
  486. } if (event.currentTarget.parent.parent.name === "pingguozz") {
  487. if (arrIndex > -1) {
  488. this.showMsg("请将成熟的果实收获!");
  489. return;
  490. }
  491. genPlant._genPlant.addPlants({
  492. name: "pingguo",
  493. img: "pingguozz",
  494. chengshuImg: "pingguo"
  495. });
  496. this.removePack(1);
  497. main.addPackCount -= 1;
  498. } if (event.currentTarget.parent.parent.name === "qiezizz") {
  499. if (arrIndex > -1) {
  500. this.showMsg("请将成熟的果实收获!");
  501. return;
  502. }
  503. genPlant._genPlant.addPlants({
  504. name: "qiezi",
  505. img: "qiezizz",
  506. chengshuImg: "qiezi"
  507. });
  508. this.removePack(2);
  509. main.addPackCount -= 1;
  510. } if (event.currentTarget.parent.parent.name === "huluobozz") {
  511. if (arrIndex > -1) {
  512. this.showMsg("请将成熟的果实收获!");
  513. return;
  514. }
  515. genPlant._genPlant.addPlants({
  516. name: "huluobo",
  517. img: "huluobozz",
  518. chengshuImg: "huluobo"
  519. });
  520. this.removePack(3);
  521. main.addPackCount -= 1;
  522. }
  523. const Timer = find("MainCanvas/Timer");
  524. //播种完成需要重置生长时间
  525. Timer.active = true;
  526. timer.time = param.levelTime;
  527. timer.isStart = true;
  528. this.showMsg("播种完成!");
  529. }
  530. //看激励视频随机增加金币
  531. adCalcTime(event: EventTouch, customEventData: any) {
  532. let currentMoney = parseInt(jinbi.getComponent(Label).string);
  533. currentMoney = currentMoney + param.adGiveMoney;
  534. if (sys.platform === sys.Platform.WECHAT_GAME) { //种子背包点击激励视频广告
  535. let videoAd = wx.createRewardedVideoAd({
  536. adUnitId: wechatAd[3 + parseInt(customEventData)].adid
  537. })
  538. if (videoAd !== null) {
  539. // 用户触发广告后,显示激励视频广告
  540. videoAd.show();
  541. videoAd.onError(err => {
  542. console.log(err)
  543. })
  544. videoAd.onClose(res => {
  545. //看完广告后增加10金币
  546. if (res && res.isEnded || res === undefined) {
  547. jinbi.getComponent(Label).string = currentMoney.toString();
  548. this.showMsg("已奖励10金币!");
  549. }
  550. else {
  551. // 播放中途退出,不下发游戏奖励
  552. this.showMsg("广告没有播放完成!");
  553. }
  554. })
  555. }
  556. } else {
  557. this.showMsg("敬请期待!");
  558. }
  559. }
  560. //分享成功随机增加金币,暂时不做
  561. share() {
  562. this.showMsg("分享成功!");
  563. }
  564. }