| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- import { _decorator, Component, Node, find, Sprite, SpriteFrame, Animation, director, game } from 'cc';
- import { Http } from '../core/net/Http';
- import { ResourcesUtils } from '../core/resourceManager/ResourcesUtils';
- import { Sound } from '../core/sound/Sound';
- import { OpenWindow } from '../core/ui/window/OpenWindow';
- import { WindowManager } from '../core/ui/window/WindowManager';
- import { WindowOpenMode } from '../core/ui/window/WindowOpenMode';
- import { ConfigData } from '../Data/ConfigData';
- import { g } from '../Data/g';
- import { platform } from '../Data/platform';
- import { MoveToCenter } from '../Map/MoveToCenter';
- import { PlantWindow } from '../UI/Farm/PlantWindow';
- import { FarmCountDown } from './FarmCountDown';
- import { FarmGrowProcess } from './FarmGrowProcess';
- import { FarmManager } from './FarmManager';
- import { FarmSystem } from './FarmSystem';
- import { IDHelper } from './IDHelper';
- const { ccclass, property } = _decorator;
- @ccclass('FarmIcon')
- export class FarmIcon extends Component {
- @property({ type: Node, tooltip: "未解锁时显示节点" }) lockNode: Node;
- @property({ type: Node, tooltip: "选中闪烁节点" }) selectNode: Node;
- @property({ type: Sprite, tooltip: "种植动画图标" }) plantIcon: Sprite;
- @property({ type: FarmCountDown, tooltip: "倒计时组件" }) countDown: FarmCountDown;
- @property({ type: FarmGrowProcess, tooltip: "成长控制组件" }) process: FarmGrowProcess;
- @property({ tooltip: "网络请求对象", type: Http }) http: Http;
- public configID: number = 0;
- public sortID: number = 0;
- private farmSystem: FarmSystem;
- private plantID: number = 0;
- private _state: FarmState = FarmState.Lock;
- private hasActive = false;
- public get state(): FarmState { return this._state; }
- onLoad() {
- this.configID = this.getComponent(IDHelper).buildID;
- this.sortID = this.getComponent(IDHelper).sortID;
- this.checkFarmSystem();
- this.lockNode.active = !(this.selectNode.active = false);
- }
- start() {
- if (g.gameData.getProductingList(this.configID)[0]) {
- this.countDown.setData(this.configID, false);
- this.process.setData(this.configID);
- }
- }
- update() {
- if (this.state == FarmState.Lock && g.gameData.hasBuildData(this.configID)) {
- this.activeFram();
- }
- let producting = g.gameData.getProductingList(this.configID);
- if (producting.length > 0) {
- if (producting[0].ripeDate <= Date.now()) {
- this.setState(FarmState.Ripe);
- } else {
- this.setState(FarmState.Growing);
- }
- this.countDown.setData(this.configID, false);
- this.process.setData(this.configID);
- } else {
- this.setState(FarmState.Empty);
- this.countDown.cleanRiped();
- this.process.cleanProcess();
- }
- }
- private setState(value: FarmState): void {
- if (this.hasActive) {
- if (this.state != value) {
- this._state = value;
- }
- }
- }
- private checkFarmSystem() {
- var frameSystemNode = find("Canvas/FarmSystem");
- var farmSystem: FarmSystem;
- if (!frameSystemNode) {//如果没有窗口系统节点则创建一个
- var frameSystemNode = new Node("FarmSystem");
- farmSystem = frameSystemNode.addComponent(FarmSystem);
- }
- else {
- farmSystem = frameSystemNode.getComponent(FarmSystem);
- }
- this.farmSystem = farmSystem || frameSystemNode.addComponent(FarmSystem);//如果没有窗口系统脚本则创建一个
- this.farmSystem.addFram(this);
- if (!frameSystemNode.parent) {
- frameSystemNode.parent = find("Canvas");
- }
- }
- public activeFram(): void {
- this.lockNode.active = false;
- this.hasActive = true;
- }
- public selectFarm(isSelect = false): void {
- this.selectNode.active = isSelect;
- if (isSelect) {
- this.getComponent(MoveToCenter).move();
- }
- }
- public async onTouch() {
- if (this.state != FarmState.Lock) {
- if (this.state == FarmState.Empty) {
- var sound = this.getComponent(Sound);
- sound && sound.play();
- this.getComponent(OpenWindow).open(this.configID);
- } else if (this.state == FarmState.Growing) {
- this.countDown.setCountActive(true);
- } else if (this.state == FarmState.Ripe) {
- WindowManager.open("Prefabs/HarvestWindow", WindowOpenMode.CloseAndCover, this.configID, this.onHarvest.bind(this));
- }
- } else {
- let config = ConfigData.configMap.get("build")[this.configID];
- if (config["unlockVideo"] != 1) {
- let lvl = config["lvl"];
- WindowManager.showTips(`${lvl}级解锁`);
- } else {
- if (g.gameData.hasBuildData(this.configID - 1)) {
- WindowManager.open("Prefabs/ADUnlockWindow", WindowOpenMode.CloseAndCover, this.configID);
- platform.umUp("videoExpansion");//视频扩充埋点
- }
- }
- }
- }
- public onHarvest(): void {
- this.countDown.cleanRiped();
- this.process.cleanProcess();
- this.farmSystem.currSelectFarm = null;
- g.gameData.getProductingList(this.configID).shift();
- }
- public async plant(id: number) {
- if (this.state == FarmState.Empty && !FarmManager.plantIsRequesting) {
- FarmManager.plantIsRequesting = true;
- let result = await this.http.send("/api/product/createOrder", { buildID: this.configID, productID: id });
- if (result.code == 0) {
- this.plantID = id;
- FarmManager.plantCountMap.set(this.plantID, FarmManager.plantCountMap.get(this.plantID) - 1);
- if (FarmManager.plantCountMap.get(this.plantID) == 0) {
- PlantWindow.needReLoad = true;
- }
- this.plantIcon.node.active = true;
- this.plantIcon.spriteFrame = await ResourcesUtils.load<SpriteFrame>("plant_icons/" + "plantIcon_" + this.plantID + "/spriteFrame", SpriteFrame, this.node);
- this.plantIcon.getComponent(Animation).play();
- this.plantIcon.getComponent(Sound).play();
- this.plantIcon.getComponent(Animation).on(Animation.EventType.FINISHED, this.onPlantAnimationFinish, this);
- let config = ConfigData.configMap.get("product")[this.plantID];
- g.gameData.getProductingList(this.configID).push({ productID: this.plantID, ripeDate: Date.now() + config["time"] * 1000 });
- this.farmSystem.selectNextFarm();
- if (result.data.productPrize) {
- g.gameData.productPrize = result.data.productPrize;
- }
- }
- // else {
- // WindowManager.showTips("请求失败");
- // }
- FarmManager.plantIsRequesting = false;
- }
- }
- public onRiped(): void {
- this._state = FarmState.Ripe;
- g.gameData.getProductingList(this.configID)[0].ripeDate = Date.now();
- }
- private async onPlantAnimationFinish() {
- this.plantIcon.getComponent(Animation).off(Animation.EventType.FINISHED, this.onPlantAnimationFinish, this);
- this.plantIcon.node.active = false;
- this.farmSystem.hideAllSelectProgressGroup();
- this.countDown.setData(this.configID);
- this.process.setData(this.configID);
- }
- public novice_plant(): void {
- this.process.setData(this.configID);
- }
- public novice_clean(): void {
- this.process.cleanProcess();
- }
- }
- export enum FarmState {
- Lock,
- Empty,
- Growing,
- Ripe
- }
|