123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573 |
- 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';
- import { genPlant } from './genPlant';
- import { timer } from './timer';
- import {
- paiHang,
- param, wechatAd
- } from './loadData';
- const { ccclass, property } = _decorator;
- let myPackBox: Node, mySaleBox: Node, storeBox: Node, paiHangBox: Node, msgBox: Node = null;
- let userAvata: Node, nickName: Node, jinbi: Node = null;
- let mainAd1, mainAd2, mainAd3 = null;
- //0 橘子香蕉西红柿幼苗,1 红富士苹果幼苗,2 紫金冠茄幼苗,3 红森胡萝卜幼苗
- @ccclass('main')
- export class main extends Component {
- public static addSalePackCount: number = 0;
- public static addPackCount: number = 0;
- public static saleBox: Node = null;
- // @property([Node])
- // fingerFlows
- start() {
- director.preloadScene("main");
- //login顶部banner广告
- if (sys.platform === sys.Platform.WECHAT_GAME && director.getScene().name === "login") {
- let { screenWidth } = wx.getSystemInfoSync()
- mainAd1 = wx.createBannerAd({
- adUnitId: wechatAd[0].adid,
- style: {
- left: 0,
- top: 0,
- width: screenWidth,
- },
- //自动刷新
- adIntervals: 30,
- });
- if (mainAd1 !== null) {
- mainAd1.show();
- mainAd1.onError(err => {
- console.log(err)
- })
- }
- }
- if (sys.platform === sys.Platform.WECHAT_GAME && director.getScene().name === "main") {
- let { screenWidth, screenHeight } = wx.getSystemInfoSync()
- //main场景插屏广告
- mainAd3 = wx.createInterstitialAd({
- adUnitId: wechatAd[2].adid,
- });
- if (mainAd3 !== null) {
- mainAd3.show();
- mainAd3.onError(err => {
- console.log(err)
- })
- }
- //main场景底部banner广告
- mainAd2 = wx.createBannerAd({
- adUnitId: wechatAd[1].adid,
- style: {
- left: 0,
- top: screenHeight - 60,
- width: screenWidth,
- },
- //自动刷新
- adIntervals: 30,
- });
- if (mainAd2 !== null) {
- mainAd2.show();
- mainAd2.onError(err => {
- console.log(err)
- })
- }
- }
- //手指跟随
- // const finger = find("MainCanvas/op/finger");
- // this.fingerFlows.forEach(item => {
- // item.on(Node.EventType.TOUCH_END, function (event: EventTouch) {
- // finger.parent.active = true;
- // const position = item.getPosition();
- // finger.setPosition(new Vec3(position.x + 30, position.y - 30, 0));
- // }, this);
- // });
- }
- clickLoginBtn() {
- const btn = find("Canvas/btnClick");
- btn.getComponent(AudioSource).play();
- }
- clickBtn() {
- const btn = find("MainCanvas/btnClick");
- btn.getComponent(AudioSource).play();
- }
- login() {
- director.loadScene("main", () => {
- //游戏开始倒计时
- timer.isStart = true;
- this.init();
- nickName.getComponent(Label).string = param.nickname;
- resources.load(param.avata, SpriteFrame, (err, spriteFrame) => {
- userAvata.getComponent(Sprite).sizeMode = Sprite.SizeMode.CUSTOM;
- userAvata.getComponent(Sprite).spriteFrame = spriteFrame;
- });
- jinbi.getComponent(Label).string = param.money.toString();
- timer.time = param.levelTime;
- if (sys.platform === sys.Platform.WECHAT_GAME) {
- if (mainAd1 !== null) {
- mainAd1.hide();
- }
- if (mainAd2 !== null) {
- mainAd2.hide();
- }
- }
- });
- }
- //类型,索引
- static addSalePack(type: number) {
- let sale_item = new Node();
- //成熟果实仅仅可以售卖 只入包一次
- if (type === 10) {
- sale_item = instantiate(mySaleBox.getChildByName("items").getChildByName("fanqie_base"));
- sale_item.name = "fanqie";
- }
- if (type === 11) {
- sale_item = instantiate(mySaleBox.getChildByName("items").getChildByName("pingguo_base"));
- sale_item.name = "pingguo";
- }
- if (type === 12) {
- sale_item = instantiate(mySaleBox.getChildByName("items").getChildByName("qiezi_base"));
- sale_item.name = "qiezi";
- }
- if (type === 13) {
- sale_item = instantiate(mySaleBox.getChildByName("items").getChildByName("huluobo_base"));
- sale_item.name = "huluobo";
- }
- main.addSalePackCount++;
- sale_item.active = true;
- sale_item.layer = Layers.Enum.UI_2D;
- sale_item.setParent(mySaleBox);
- //果实背包只放一条记录,后期改成scrollview组件即可,多条的话会出现位置不对的bug
- sale_item.setPosition(-60, 271 - 189 * main.addSalePackCount, 0);
- mySaleBox.getChildByName("items").addChild(sale_item);
- }
- static addPack(type: number) {
- //动态添加背包
- //种子 仅仅可以种植
- let clone_item = new Node();
- if (type === 0) {
- clone_item = instantiate(myPackBox.getChildByName("items").getChildByName("fanqiezz_base"));
- clone_item.name = "fanqiezz";
- }
- if (type === 1) {
- clone_item = instantiate(myPackBox.getChildByName("items").getChildByName("pingguozz_base"));
- clone_item.name = "pingguozz";
- }
- if (type === 2) {
- clone_item = instantiate(myPackBox.getChildByName("items").getChildByName("qiezizz_base"));
- clone_item.name = "qiezizz";
- }
- if (type === 3) {
- clone_item = instantiate(myPackBox.getChildByName("items").getChildByName("huluobozz_base"));
- clone_item.name = "huluobozz";
- }
- main.addPackCount++;
- clone_item.active = true;
- clone_item.layer = Layers.Enum.UI_2D;
- clone_item.setParent(myPackBox);
- clone_item.setPosition(-60, 271 - 189 * main.addPackCount, 0);
- myPackBox.getChildByName("items").addChild(clone_item);
- }
- addPaiHang() {
- //动态添加排行榜
- paiHang.forEach((item, index) => {
- const clone_item = instantiate(paiHangBox.getChildByName("items"));
- clone_item.layer = Layers.Enum.UI_2D;
- clone_item.setParent(paiHangBox);
- clone_item.setPosition(0, 0 - clone_item.position.y - 240 * index, 0);
- const name = clone_item.getChildByName("box")
- .getChildByName("txtBox")
- .getChildByName("name");
- name.getComponent(Label).string = item.nickName;
- resources.load(item.top === 1 ? "1/spriteFrame" : item.top === 2 ? "2/spriteFrame" : "3/spriteFrame", SpriteFrame, (err, spriteFrame) => {
- const top = clone_item.getChildByName("box")
- .getChildByName("txtBox")
- .getChildByName("top");
- top.getComponent(Sprite).sizeMode = Sprite.SizeMode.CUSTOM;
- top.getComponent(Sprite).spriteFrame = spriteFrame;
- });
- resources.load(item.userAvata, SpriteFrame, (err, spriteFrame) => {
- const sp = clone_item.getChildByName("box")
- .getChildByName("txtBox")
- .getChildByName("pro");
- sp.getComponent(Sprite).sizeMode = Sprite.SizeMode.CUSTOM;
- sp.getComponent(Sprite).spriteFrame = spriteFrame;
- });
- paiHangBox.addChild(clone_item);
- });
- }
- init() {
- //种子背包
- myPackBox = find("MainCanvas/popBox/mypack"),
- //果实背包
- mySaleBox = find("MainCanvas/popBox/mysalepack"),
- storeBox = find("MainCanvas/popBox/store"),
- paiHangBox = find("MainCanvas/popBox/paihang"),
- msgBox = find("MainCanvas/popBox/msgBox");
- userAvata = find("MainCanvas/touxiangkuang/userAvata"),
- nickName = find("MainCanvas/touxiangkuang/nickname"),
- jinbi = find("MainCanvas/jinbi/boxbg/txt");
- //默认添加胡萝卜到商店植物 胡萝卜最便宜
- genPlant._genPlant.addPlants({
- name: "huluobo",
- img: "huluobozz",
- chengshuImg: "huluobo"
- });
- main.saleBox = mySaleBox;
- main.addPackCount = 0;
- main.addSalePackCount = 0;
- }
- update(deltaTime: number) {
- }
- //弹窗
- showDialog(event: EventTouch) {
- switch (event.currentTarget.name) {
- case "package":
- myPackBox.active = true;
- if (sys.platform === sys.Platform.WECHAT_GAME) {
- //种子背包原生模板
- let mainAd8 = wx.createCustomAd({
- adUnitId: wechatAd[8].adid,
- style: {
- left: 50,
- top: 550,
- width: 650,
- },
- //自动刷新
- adIntervals: 30,
- })
- if (mainAd8 !== null) {
- // 用户触发广告后,显示激励视频广告
- mainAd8.show();
- mainAd8.onError(err => {
- console.log(err)
- })
- }
- }
- break;
- case "package_guoshi":
- mySaleBox.active = true;
- if (sys.platform === sys.Platform.WECHAT_GAME) {
- if (mainAd2 !== null) {
- mainAd2.show();
- mainAd2.onError(err => {
- console.log(err)
- })
- }
- //果实背包原生模板
- let mainAd9 = wx.createCustomAd({
- adUnitId: wechatAd[9].adid,
- style: {
- left: 50,
- top: 550,
- width: 650,
- },
- //自动刷新
- adIntervals: 30,
- })
- if (mainAd9 !== null) {
- // 用户触发广告后,显示激励视频广告
- mainAd9.show();
- mainAd9.onError(err => {
- console.log(err)
- })
- }
- }
- break;
- case "zhishipai":
- storeBox.active = true;
- break;
- case "rongyaobang":
- this.addPaiHang();
- paiHangBox.active = true;
- break;
- }
- }
- closeDialog(event: EventTouch) {
- let type = event.currentTarget.parent.name + "_" + event.currentTarget.name;
- switch (type) {
- case "store_close":
- storeBox.active = false;
- break;
- case "mypack_close":
- myPackBox.active = false;
- break;
- case "mysalepack_close":
- mySaleBox.active = false;
- case "paihang_close":
- paiHangBox.active = false;
- break;
- }
- }
- //弹消息
- showMsg(msg: string) {
- const mg = msgBox.getChildByName("msgTxt").getComponent(Label);
- mg.string = msg;
- msgBox.active = true;
- this.scheduleOnce(function callback() {
- msgBox.active = false;
- }, 1);
- }
- //购买种子 进背包
- buyPlant(event: EventTouch) {
- //必须把种子背包的种子用完才可以购买种子
- const mySaleBoxItems = find("MainCanvas/popBox/mypack/items");
- if (mySaleBoxItems.children.length > 4) {
- this.showMsg("种子尚未使用完毕!");
- return;
- }
- const currentJinbi = parseInt(jinbi.getComponent(Label).string);
- let price = 0;
- if (event.currentTarget.parent.parent.name === "fanqiezz") {
- price = 300;
- if (currentJinbi - price <= 0) {
- this.showMsg("金币不足!"); return;
- } else {
- if (main.addPackCount > 3) {
- this.showMsg("种子背包已满!");
- return;
- }
- let arrIndex = myPackBox.getChildByName("items").children.findIndex(item => {
- return item.name === "fanqiezz"
- })
- if (arrIndex > -1) {
- this.showMsg("已购买此种子!");
- return;
- }
- main.addPack(0);
- jinbi.getComponent(Label).string = (currentJinbi - price).toString();
- myPackBox.active = false;
- this.showMsg("购买成功!"); return;
- }
- } if (event.currentTarget.parent.parent.name === "pingguozz") {
- price = 180;
- if (currentJinbi - price <= 0) {
- this.showMsg("金币不足!"); return;
- } else {
- if (main.addPackCount > 3) {
- this.showMsg("种子背包已满!");
- return;
- }
- let arrIndex = myPackBox.getChildByName("items").children.findIndex(item => {
- return item.name === "pingguozz"
- })
- if (arrIndex > -1) {
- this.showMsg("已购买此种子!");
- return;
- }
- main.addPack(1);
- jinbi.getComponent(Label).string = (currentJinbi - price).toString();
- myPackBox.active = false;
- this.showMsg("购买成功!"); return;
- }
- } if (event.currentTarget.parent.parent.name === "qiezizz") {
- price = 120;
- if (currentJinbi - price <= 0) {
- this.showMsg("金币不足!"); return;
- } else {
- if (main.addPackCount > 3) {
- this.showMsg("种子背包已满!");
- return;
- }
- let arrIndex = myPackBox.getChildByName("items").children.findIndex(item => {
- return item.name === "qiezizz"
- })
- if (arrIndex > -1) {
- this.showMsg("已购买此种子!");
- return;
- }
- main.addPack(2);
- jinbi.getComponent(Label).string = (currentJinbi - price).toString();
- myPackBox.active = false;
- this.showMsg("购买成功!"); return;
- }
- } if (event.currentTarget.parent.parent.name === "huluobozz") {
- price = 60;
- if (currentJinbi - price <= 0) {
- this.showMsg("金币不足!"); return;
- } else {
- if (main.addPackCount > 3) {
- this.showMsg("种子背包已满!");
- return;
- }
- let arrIndex = myPackBox.getChildByName("items").children.findIndex(item => {
- return item.name === "huluobozz"
- })
- if (arrIndex > -1) {
- this.showMsg("已购买此种子!");
- return;
- }
- main.addPack(3);
- jinbi.getComponent(Label).string = (currentJinbi - price).toString();
- myPackBox.active = false;
- this.showMsg("购买成功!"); return;
- }
- }
- }
- removePack(type: number) {
- //种子播种 减去背包
- if (type === 0) {
- myPackBox.getChildByName("items").getChildByName("fanqiezz").destroy();
- myPackBox.getChildByName("items").getChildByName("fanqiezz_base").active = false;
- }
- if (type === 1) {
- myPackBox.getChildByName("items").getChildByName("pingguozz").destroy();
- myPackBox.getChildByName("items").getChildByName("pingguozz_base").active = false;
- }
- if (type === 2) {
- myPackBox.getChildByName("items").getChildByName("qiezizz").destroy();
- myPackBox.getChildByName("items").getChildByName("qiezizz_base").active = false;
- }
- if (type === 3) {
- myPackBox.getChildByName("items").getChildByName("huluobozz").destroy();
- myPackBox.getChildByName("items").getChildByName("huluobozz_base").active = false;
- }
- //果实成熟 减去背包
- //因为用的场景中的Node 克隆的节点所以使用active来控制克隆节点的显示和隐藏
- if (type === 10) {
- mySaleBox.getChildByName("items").getChildByName("fanqie").destroy();
- mySaleBox.getChildByName("items").getChildByName("fanqie_base").active = false;
- }
- if (type === 11) {
- mySaleBox.getChildByName("items").getChildByName("pingguo").destroy();
- mySaleBox.getChildByName("items").getChildByName("pingguo_base").active = false;
- }
- if (type === 12) {
- mySaleBox.getChildByName("items").getChildByName("qiezi").destroy();
- mySaleBox.getChildByName("items").getChildByName("qiezi_base").active = false;
- }
- if (type === 13) {
- mySaleBox.getChildByName("items").getChildByName("huluobo").destroy();
- mySaleBox.getChildByName("items").getChildByName("huluobo_base").active = false;
- }
- }
- //出售果实 减去背包
- salePlant(event: EventTouch) {
- let price = 0;
- const currentJinbi = parseInt(jinbi.getComponent(Label).string);
- if (event.currentTarget.parent.parent.name === "fanqie") {
- price = 30;
- this.removePack(10);
- jinbi.getComponent(Label).string = (currentJinbi + price).toString();
- this.showMsg("出售果实成功!");
- } if (event.currentTarget.parent.parent.name === "pingguo") {
- price = 18;
- this.removePack(11);
- jinbi.getComponent(Label).string = (currentJinbi + price).toString();
- this.showMsg("出售果实成功!");
- } if (event.currentTarget.parent.parent.name === "qiezi") {
- price = 12;
- this.removePack(12);
- jinbi.getComponent(Label).string = (currentJinbi + price).toString();
- this.showMsg("出售果实成功!");
- } if (event.currentTarget.parent.parent.name === "huluobo") {
- price = 6;
- this.removePack(13);
- jinbi.getComponent(Label).string = (currentJinbi + price).toString();
- this.showMsg("出售果实成功!");
- }
- main.addSalePackCount--;
- }
- //播种,一次播6个种子,并且修改genPlant种子类型,土地添加种子
- boZhong(event: EventTouch) {
- let plants = find("MainCanvas/plant");
- let arrIndex = plants.children.findIndex(item => {
- return item.name === "huluobo" || item.name === "qiezi" || item.name === "pingguo" || item.name === "fanqie"
- })
- if (event.currentTarget.parent.parent.name === "fanqiezz") {
- if (arrIndex > -1) {
- this.showMsg("请将成熟的果实收获!");
- return;
- }
- genPlant._genPlant.addPlants({
- name: "fanqie",
- img: "fanqiezz",
- chengshuImg: "fanqie"
- });
- //播种后种子库移除,种子背包items数量减1,否则不能继续购买种子了
- this.removePack(0);
- main.addPackCount -= 1;
- } if (event.currentTarget.parent.parent.name === "pingguozz") {
- if (arrIndex > -1) {
- this.showMsg("请将成熟的果实收获!");
- return;
- }
- genPlant._genPlant.addPlants({
- name: "pingguo",
- img: "pingguozz",
- chengshuImg: "pingguo"
- });
- this.removePack(1);
- main.addPackCount -= 1;
- } if (event.currentTarget.parent.parent.name === "qiezizz") {
- if (arrIndex > -1) {
- this.showMsg("请将成熟的果实收获!");
- return;
- }
- genPlant._genPlant.addPlants({
- name: "qiezi",
- img: "qiezizz",
- chengshuImg: "qiezi"
- });
- this.removePack(2);
- main.addPackCount -= 1;
- } if (event.currentTarget.parent.parent.name === "huluobozz") {
- if (arrIndex > -1) {
- this.showMsg("请将成熟的果实收获!");
- return;
- }
- genPlant._genPlant.addPlants({
- name: "huluobo",
- img: "huluobozz",
- chengshuImg: "huluobo"
- });
- this.removePack(3);
- main.addPackCount -= 1;
- }
- const Timer = find("MainCanvas/Timer");
- //播种完成需要重置生长时间
- Timer.active = true;
- timer.time = param.levelTime;
- timer.isStart = true;
- this.showMsg("播种完成!");
- }
- //看激励视频随机增加金币
- adCalcTime(event: EventTouch, customEventData: any) {
- let currentMoney = parseInt(jinbi.getComponent(Label).string);
- currentMoney = currentMoney + param.adGiveMoney;
- if (sys.platform === sys.Platform.WECHAT_GAME) { //种子背包点击激励视频广告
- let videoAd = wx.createRewardedVideoAd({
- adUnitId: wechatAd[3 + parseInt(customEventData)].adid
- })
- if (videoAd !== null) {
- // 用户触发广告后,显示激励视频广告
- videoAd.show();
- videoAd.onError(err => {
- console.log(err)
- })
- videoAd.onClose(res => {
- //看完广告后增加10金币
- if (res && res.isEnded || res === undefined) {
- jinbi.getComponent(Label).string = currentMoney.toString();
- this.showMsg("已奖励10金币!");
- }
- else {
- // 播放中途退出,不下发游戏奖励
- this.showMsg("广告没有播放完成!");
- }
- })
- }
- } else {
- this.showMsg("敬请期待!");
- }
- }
- //分享成功随机增加金币,暂时不做
- share() {
- this.showMsg("分享成功!");
- }
- }
|