| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322 |
- /** 养殖类 */
- import { AnimalState, DataEventId, ExpAddType, GameProp, PastureState, ProductType } from "../../game/data/GameData";
- import { MoveToCenter } from "../map/MoveToCenter";
- import Animals from "../view/Animals";
- import ProductShow from "../view/ProductShow";
- import { FarmCountDown } from "./FarmCountDown";
- import { IDHelper } from "./IDHelper";
- const { ccclass, property } = cc._decorator;
- @ccclass
- export default class PastureIcon extends cc.Component {
- @property({ type: cc.Node, tooltip: "动物显示节点" }) points: cc.Node = null;
- @property({ type: cc.Node, tooltip: "动物喂食节点" }) eatNode: cc.Node = null;
- @property({ type: FarmCountDown, tooltip: "倒计时组件" }) countDown: FarmCountDown = null;
- @property({ type: cc.Node, tooltip: "完成品展示节点" }) showGroup: cc.Node = null;
- @property({ type: cc.Node, tooltip: '闲置动画' }) idleAni: cc.Node = null;
- @property({ type: cc.Node, tooltip: '文字动画' }) node_ani: cc.Node = null;
- private animalsArr: Array<Animals> = new Array<Animals>();
- public configID: number = 0;
- public sortID: number = 0;
- private _state: PastureState = -1;
- public get state(): PastureState { return this._state; }
- data = null;
- //空地点击两次喂食
- private emptyClick = false;
- private pastureData = null;
- async onLoad() {
- let helper = this.getComponent(IDHelper);
- this.configID = helper.buildID;
- this.sortID = helper.sortID;
- this.data = gData.gameData.getPastureDataMap(this.configID);
- gData.pastureSystem.addPastureIcon(this);
- this.points.parent.active = false;
- let index = 0;
- let point = null;
- this.pastureData = gData.pastureSystem.setPastureData(this.configID)
- this.points.children.forEach(element => {
- point = element.getPosition();
- this.pastureData.setPointMap(index, point);
- index++;
- });
- for (var i = 0; i < 5; i++) {
- let prefab = await mk.loader.load("game/prefab/paster_" + this.configID, cc.Prefab);
- let animal = cc.instantiate(prefab);
- let animais = animal.getComponent(Animals);
- this.animalsArr.push(animais);
- animais.pointIndex = this.pastureData.getCanMoveIndex();
- animal.setPosition(this.pastureData.getCanMoveIndexPoint(animais.pointIndex));
- animal.parent = this.points.parent;
- }
- this.freshState();
- }
- update() {
- let index = gData.gameData.needFreshArr.indexOf(this.configID);
- if (index != -1) {
- this.freshState();
- gData.gameData.needFreshArr.splice(index, 1);
- }
- }
- freshState() {
- this.data = gData.gameData.getPastureDataMap(this.configID);
- this.setState(this.data.state);
- }
- private setState(value: PastureState): void {
- if (this.state != value) {
- this._state = value;
- this.points.parent.active = false;
- this.eatNode.active = false;
- this.countDown.node.active = false;
- switch (this.state) {
- case PastureState.Lock:
- for (var i = 0; i < this.animalsArr.length; i++) {
- this.animalsArr[i].setState(AnimalState.Wait, this.configID);
- }
- break;
- case PastureState.Empty:
- for (var i = 0; i < this.animalsArr.length; i++) {
- this.animalsArr[i].setState(AnimalState.Hanger, this.configID);
- }
- if (this.idleAni && this.idleAni.active == false) {
- this.idleAni.active = true;
- }
- break;
- case PastureState.Growing:
- this.points.parent.active = true;
- this.eatNode.active = false;
- this.countDown.node.active = true;
- this.countDown.setData(this.data);
- for (var i = 0; i < this.animalsArr.length; i++) {
- this.animalsArr[i].setState(AnimalState.Eat, this.configID);
- }
- if (this.idleAni && this.idleAni.active == true) {
- this.idleAni.active = false;
- }
- break;
- case PastureState.Ripe:
- this.countDown.node.active = true;
- this.countDown.setState(1);
- for (var i = 0; i < this.animalsArr.length; i++) {
- this.animalsArr[i].setState(AnimalState.Wait, this.configID);
- }
- this.onProductComplete();
- break;
- case PastureState.Sick:
- this.points.parent.active = true;
- this.countDown.node.active = true;
- this.countDown.setState(0);
- for (var i = 0; i < this.animalsArr.length; i++) {
- this.animalsArr[i].setState(AnimalState.Wait, this.configID);
- }
- break;
- }
- }
- }
- /**生产完成 */
- private async onProductComplete() {
- if (this.showGroup) {
- let prefab = await mk.loader.load("game/prefab/ProductShow", cc.Prefab);
- let item = cc.instantiate(prefab);
- let id = this.data.productID;
- item.getComponent(ProductShow).id = id;
- item.parent = this.showGroup;
- }
- }
- private clean(): void {
- this.showGroup.removeAllChildren();
- }
- isCanTouch = true;
- public async onTouch() {
- if(!this.isCanTouch){
- return;
- }
- switch (this.data.state) {
- case PastureState.Lock:
- let config = gData.gameData.getProductMap(this.data.productID);
- if (config.unlock == 2) {
- if(gData.gameData.playerProp.orderData){
- let times =config.value - gData.gameData.playerProp.orderData.overTimes;
- mk.tip.pop(`订单提现${times}次后解锁`);
- }else{
- mk.tip.pop(`提现订单后解锁`);
- }
- }
- else if (config.unlock == 1) {
- mk.tip.pop(`喂食${config.name}${config.value}次解锁`)
- }
- break;
- case PastureState.Empty:
- if (!this.emptyClick) {
- this.emptyClick = true;
- this.eatNode.active = true;
- }
- else {
- //调用喂食
- // this.onEat();
- this.eatNode.active = false;
- this.emptyClick = false;
- }
- break;
- case PastureState.Growing:
- mk.ui.openPanel('module/speedUpUI/speedUp');
- break;
- case PastureState.Ripe:
- gData.harvestData.openPanel(this.configID, this.data.productID, this.onHarvest.bind(this), this.node, 1);
- this.isCanTouch = false;
- setTimeout(()=>{
- this.isCanTouch = true;
- }, 1000);
- break;
- case PastureState.Sick:
- // mk.tip.pop(`生虫了!`);
- mk.data.sendDataEvent(DataEventId.button_click, "生病icon");
- gData.adClearSickData.openPanel(ProductType.dw, this.configID);
- break;
- }
- }
- async clickEatBtn()
- {
- let flyRed = await this.onEat();
- if (flyRed) {
- //mk.audio.playEffect('redmoney');
- if (gData.gameData.playerProp.userFarmTaskInfo) {
- let com = gData.gameData.playerProp.userFarmTaskInfo.completeCount;
- let count = gData.gameData.playerProp.userFarmTaskInfo.taskCount;
- let addAni = cc.instantiate(this.node_ani);
- addAni.parent = this.node_ani.parent;
- addAni.getComponent(cc.Label).string = `百元红包:${com}/${count}`;
- cc.tween(addAni).delay(0.6).call(()=>{
- addAni.opacity = 255;
- }).by(0.8, {y : 100}).to(0.4, {opacity : 0}).call(()=>{
- addAni.destroy();
- }).start();
- }
- let pos = this.eatNode.parent.convertToWorldSpaceAR(this.eatNode.getPosition());
- gData.gameData.gameStyle.dpFlyRedAni(pos);
- }
- }
- async onEat() {
- if (gData.gameData.leftTimes <= 0) {
- mk.ui.openPanel('module/speedUpUI/productReward');
- return false;
- }
- if (this.state == PastureState.Empty) {
- gData.gameData.isProducting = true;
- let config = gData.gameData.getProductMap(this.data.productID);
- let growSpan = Date.now() + config.time * 1000;
- this.data.state = PastureState.Growing;
- this.data.productID = this.data.productID;
- this.data.growSpan = growSpan;
- this.eatNode.active = false;
- this.getComponent(MoveToCenter).move();
- try {
- gData.gameData.setPastureDataMap(this.configID, this.data);
- gData.gameData.changeLeftTimes(-1);
- await gData.gameData.gameStyle.doCropFlyLogic(this.data.productID, this.node);
- } catch (error) {
- console.log("=======pastureIcon");
- }
- //gData.gameData.updateOrderTaskCopyData(this.data.productID);
- gData.gameData.nextMake = null;
- gData.gameData.setNextProduct(false);
-
-
- gData.gameData.isProducting = false;
- let flyRed = await gData.gameData.updateNewTaskProgress();
- return flyRed;
- // if (flyRed) {
- // //mk.audio.playEffect('redmoney');
- // if (gData.gameData.playerProp.userFarmTaskInfo) {
- // let com = gData.gameData.playerProp.userFarmTaskInfo.completeCount;
- // let count = gData.gameData.playerProp.userFarmTaskInfo.taskCount;
-
- // let addAni = cc.instantiate(this.node_ani);
- // addAni.parent = this.node_ani.parent;
- // addAni.getComponent(cc.Label).string = `百元红包:${com}/${count}`;
- // cc.tween(addAni).delay(0.6).call(()=>{
- // addAni.opacity = 255;
- // }).by(0.8, {y : 100}).to(0.4, {opacity : 0}).call(()=>{
- // addAni.destroy();
- // }).start();
- // }
- // let pos = this.eatNode.parent.convertToWorldSpaceAR(this.eatNode.getPosition());
- // gData.gameData.gameStyle.dpFlyRedAni(pos);
- // }
- }else{
- return false;
- }
- }
- clickSpeedUp() {
- mk.ui.openPanel('module/speedUpUI/speedUp');
- }
- onRipe(sendToServer) {
- this.data.state = PastureState.Ripe;
- gData.gameData.setPastureDataMap(this.configID, this.data, sendToServer);
- gData.gameData.addProductMakeTimesById(this.data.productID);
- }
- onSick(sendToServer) {
- this.data.state = PastureState.Sick;
- gData.gameData.setPastureDataMap(this.configID, this.data, sendToServer);
- }
- public async onHarvest(){
- this.countDown.cleanRiped();
- this.clean();
- this.data.state = PastureState.Empty;
- this.data.growSpan = 0;
- gData.gameData.setPastureDataMap(this.configID, this.data);
- //await gData.gameData.gameStyle.doCropFlyLogic(this.data.productID, this.node);
- //gData.gameData.nextMake = null;
- gData.gameData.setNextProduct(false);
- if (gData.harvestData.getType == 0) {
- gData.farmGradeData.addGradeExp(ExpAddType.EAT_harvest, this.node);
- }
- else {
- gData.farmGradeData.addGradeExp(ExpAddType.EAT_others, this.node);
- }
- }
- public canHarvest() {
- this.getComponent(MoveToCenter).move();
- gData.harvestData.openPanel(this.configID, this.data.productID, this.onHarvest.bind(this), this.node, 1);
- }
- public canClearSick() {
- this.getComponent(MoveToCenter).move();
- mk.data.sendDataEvent(DataEventId.button_click, "生病icon");
- gData.adClearSickData.openPanel(ProductType.dw, this.configID);
- }
- }
|