| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- import { _decorator, Component, Node, Animation, Sprite, SpriteFrame, JsonAsset, ProgressBar, Prefab, instantiate, UITransform, v3, Label } from 'cc';
- import { Http } from '../core/net/Http';
- import { ResourcesUtils } from '../core/resourceManager/ResourcesUtils';
- import { Sound } from '../core/sound/Sound';
- import { BitmapFont } from '../core/ui/BitmapFont';
- import { WindowManager } from '../core/ui/window/WindowManager';
- import { WindowOpenMode } from '../core/ui/window/WindowOpenMode';
- import { Utils } from '../core/utils/Utils';
- import { ConfigData } from '../Data/ConfigData';
- import { g } from '../Data/g';
- import { platform } from '../Data/platform';
- import { TakeAWalk } from '../public/TakeAWalk';
- import { FactoryCountDown } from '../UI/Factory/FactoryCountDown';
- import { ProductShow } from '../UI/Factory/ProductShow';
- import { Animal, AnimalState } from './Pasture/Animal';
- const { ccclass, property } = _decorator;
- @ccclass('PastureIcon')
- export class PastureIcon extends Component {
- @property({ tooltip: "点击是否缩放动画" }) touchAmition: boolean = true;
- @property({ type: Animation, tooltip: "点击动画" }) animation: Animation;
- @property({ type: Node, tooltip: "建筑状态组" }) stateGroup: Node;
- @property({ type: Node, tooltip: "完成品展示节点" }) showGroup: Node;
- @property({ tooltip: "配置ID" }) configID: number = 0;
- @property({ type: FactoryCountDown, tooltip: "计时装置" }) countDown: FactoryCountDown;
- @property({ type: Node, tooltip: "喂养组" }) feedGroup: Node;
- @property({ type: Sprite, tooltip: "饲料图标" }) feedIcon: Sprite;
- @property({ type: Animation, tooltip: "喂养动画" }) feedAnimation: Animation;
- @property({ type: Label, tooltip: "喂食剩余次数" }) feedLabel: Label;
- @property({ type: Node, tooltip: "进度组" }) countDownGroup: Node;
- @property({ type: ProgressBar, tooltip: "倒计时条" }) countDownProgress: ProgressBar;
- @property({ type: BitmapFont, tooltip: "倒计时数字" }) countDownLabel: BitmapFont;
- @property({ type: Sprite, tooltip: "倒计时图标" }) countDownIcon: Sprite;
- @property({ type: Node, tooltip: "动物图标" }) animalParent: Node;
- @property({ type: Prefab, tooltip: "动物预制体" }) animalPrefabs: Prefab;
- @property({ type: [Node], tooltip: "活动点" }) walkPoints: Array<Node> = [];
- @property({ tooltip: "网络请求对象", type: Http }) http: Http;
- private _state: PastureState = PastureState.Lock;
- public get state(): PastureState {
- return this._state;
- }
- private hasActive = false;
- private havrestList: { id: number, node: Node }[] = [];
- public canShowFeedGroup = false;
- public canShowCountDownGroup = false;
- private feedCount = 0;
- private animals: Array<Animal> = [];
- async start() {
- if (g.gameData.hasBuildData(this.configID)) {
- this.activeFactroy();
- }
- this.feedIcon.spriteFrame = await ResourcesUtils.load<SpriteFrame>("pasture_icons/pasture_feed_" + this.configID + "/spriteFrame", SpriteFrame, this.node);
- this.countDownIcon.spriteFrame = await ResourcesUtils.load<SpriteFrame>("pasture_icons/pasture_speed_" + this.configID + "/spriteFrame", SpriteFrame, this.node);
- for (let i = 0; i < 5; i++) {
- let prefab = await ResourcesUtils.load<Prefab>("Prefabs/pastureAnimals/paster_" + this.configID, Prefab, this.node);
- let animal = instantiate(prefab);
- animal.getComponent(TakeAWalk).points = this.walkPoints;
- animal.setPosition(this.walkPoints[Utils.random_both(i * 5, i * 5 + 4)].position);
- animal.setParent(this.animalParent);
- this.animals.push(animal.getComponent(Animal));
- }
- }
- update() {
- let producting = g.gameData.getProductingList(this.configID);
- if (producting.length > 0) {
- this.setState(PastureState.Producting);
- } else {
- this.setState(PastureState.Free);
- }
- if (this.state == PastureState.Lock) {
- if (g.gameData.hasBuildData(this.configID)) {
- this.activeFactroy();
- }
- }
- this.upDataAnimals();
- this.feedGroup.active = this.canShowFeedGroup && producting.length < g.gameData.getUnlock(this.configID);
- this.countDownGroup.active = this.canShowCountDownGroup && producting.length > 0;
- this.countDownProgress.progress = 1 - this.countDown.progress;
- this.countDownLabel.string = this.countDown.timerString;
- }
- public activeFactroy(): void {
- this.hasActive = true;
- }
- public async inPolygon() {
- if (this.touchAmition) {
- this.animation.play();
- }
- this.getComponent(Sound).play();
- if (this.state != PastureState.Lock) {
- if (this.havrestList.length > 0) {
- WindowManager.open("Prefabs/HarvestWindow", WindowOpenMode.CloseAndCover, this.configID, this.onHarvest.bind(this));
- this.canShowFeedGroup = this.canShowCountDownGroup = false;
- } else {
- let productID = ConfigData.configMap.get("build")[this.configID]["product"][0];
- let result = await this.http.send("/api/product/getProductTimes", { productIDList: productID + "" });
- if (result.code == 0) {
- let countData = result.data;
- this.feedCount = ConfigData.configMap.get("product")[productID]["reap"] - (countData[productID] ? countData[productID] : 0);
- this.feedLabel.string = this.feedCount + "";
- this.canShowFeedGroup = this.canShowCountDownGroup = true;
- }
- }
- } else {
- let lvl = ConfigData.configMap.get("build")[this.configID]["lvl"];
- WindowManager.showTips(`${lvl}级解锁`);
- }
- }
- private getThankingKey(): string {
- let key = "";
- switch (this.configID) {
- case 30001: key = "chick"; break;
- case 30002: key = "cow"; break;
- case 30003: key = "pig"; break;
- case 30004: key = "corn"; break;
- case 30005: key = "milk"; break;
- case 30006: key = "sugar"; break;
- case 30007: key = "cake"; break;
- case 30008: key = "fastfood"; break;
- case 30009: key = "noodle"; break;
- }
- return key;
- }
- public async onFeedButton() {
- if (this.feedCount > 0) {
- let producting = g.gameData.getProductingList(this.configID);
- let productID = ConfigData.configMap.get("build")[this.configID]["product"][0];
- if (producting.length < g.gameData.getUnlock(this.configID)) {
- platform.umUp("factoryClick");//牧场下单
- let result = await this.http.send("/api/product/createOrder", { buildID: this.configID, productID: productID });
- if (result.code == 0) {
- platform.reportThinking("feed", JSON.stringify({ farm_name: this.getThankingKey(), feed_name: productID, id: g.userData.id, level: g.userData.getLevel(), role_name: g.userData.nickName, feed_time: Utils.formatDate(new Date()) }));
- this.feedAnimation.play();
- this.feedCount--;
- this.feedLabel.string = this.feedCount + "";
- let time = ConfigData.configMap.get("product")[productID]["time"];
- producting.push({ productID: productID, ripeDate: Date.now() + time * 1000 });
- if (result.data.productPrize) {
- g.gameData.productPrize = result.data.productPrize;
- }
- }
- }
- }
- }
- public async onSpeedButton() {
- if (g.userData.diamond >= 25) {
- let result = await this.http.send("/api/product/quickProduct", { buildID: this.configID });
- if (result.code == 0) {
- platform.reportThinking("diamond_decrease", JSON.stringify({ previous_number: g.userData.diamond, decrease_number: result.data, current_number: g.userData.diamond - result.data, reasons: "speed" }));
- g.userData.diamond -= result.data;
- this.countDown.ripe();
- this.canShowFeedGroup = false;
- this.canShowCountDownGroup = false;
- return;
- }
- }
- WindowManager.showTips("钻石不足");
- }
- public onHarvest(mult: number): void {
- this.havrestList.shift().node.destroy();
- }
- /**生产完成回调 */
- public async onProductComplete(id: number) {
- if (this.showGroup) {
- let prefab = await ResourcesUtils.load<Prefab>("Prefabs/ProductShow", null, this.node);
- let item = instantiate(prefab);
- item.getComponent(ProductShow).id = id;
- item.setParent(this.showGroup);
- let x = Utils.random_both(-this.showGroup.getComponent(UITransform).width / 2, this.showGroup.getComponent(UITransform).width / 2);
- let y = Utils.random_both(-this.showGroup.getComponent(UITransform).height / 2, this.showGroup.getComponent(UITransform).height / 2);
- item.setPosition(v3(x, y, 0));
- this.havrestList.push({ id: id, node: item });
- }
- }
- public clean(): void {
- this.showGroup.removeAllChildren();
- this.havrestList = [];
- }
- private setState(value: PastureState): void {
- if (this.hasActive) {
- if (this._state != value) {
- this._state = value;
- if (this._state == PastureState.Producting) {
- this.stateGroup && (this.stateGroup.active = true);
- } else {
- this.stateGroup && (this.stateGroup.active = false);
- }
- }
- }
- }
- private upDataAnimals(): void {
- let producting = g.gameData.getProductingList(this.configID);
- if (this.animals.length == 5) {
- for (let i = 0; i < producting.length; i++) {
- this.animals[i].state = AnimalState.Wait;
- }
- for (let j = 0; j < this.havrestList.length; j++) {
- this.animals[producting.length + j].state = AnimalState.Out;
- }
- for (let k = 0; k < this.animals.length - this.havrestList.length - producting.length; k++) {
- this.animals[producting.length + this.havrestList.length + k].state = AnimalState.Hungry;
- }
- }
- }
- }
- export enum PastureState {
- Lock,
- Producting,
- Free
- }
|