123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- import i18n from "../../../i18n/i18n";
- import ViewObject from "../../../main/ViewObject";
- import { HttpStateType, ReveData } from "../../../util/CHttp";
- import CUtil from "../../../util/CUtil";
- import GoodItem from "../../common/GoodItem";
- import Pack from "./Pack";
- /**
- * 食物查看并使用
- */
- const {ccclass, property} = cc._decorator;
- @ccclass
- export default class FoodView extends ViewObject {
- @property(cc.Label)
- mName: cc.Label = null;
- @property(cc.Label)
- mAbout: cc.Label = null;
- @property(GoodItem)
- mGoodItem: GoodItem = null;
- @property(cc.Label)
- mCount: cc.Label = null;
- @property(cc.Label)
- mUserLabel: cc.Label = null;
- public pack:Pack
- public goodItem:GoodItem
- public initGood(pack:Pack,goodItem:GoodItem){
- this.pack = pack
- this.goodItem = goodItem
- this.mGoodItem.initGood(this.main,this.goodItem.data)
- this.mName.string = i18n.t(goodItem.good.name)
- this.mAbout.string = i18n.t(goodItem.good.about)
- if(goodItem.good.type == 3){
- this.mUserLabel.string = i18n.t('使用')
- }else if(goodItem.good.type == 5){
- this.mUserLabel.string = i18n.t('食用')
- }
- this.flush()
- }
- private flush(){
- if(this.goodItem.good.type == 5){
- let foodAttr = this.main.player.foodAttr
- let nowDate = CUtil.getNowDateInt()
- if(nowDate > foodAttr.date){
- foodAttr.date = nowDate
- foodAttr.count = 0
- }
- this.mCount.string = i18n.t('今日食用食物上限')+":"+foodAttr.count+"/10"
- }
- }
- public onclickEat(){
- if(this.goodItem.good.type == 3){
- this.eatGift()
- }else if(this.goodItem.good.type == 5){
- this.eatFood()
- }
- }
- private eatFood(){
- let msg = {
- id:this.goodItem.good.id
- }
- this.main.startLoad()
- this.main.gameHttp.sendJson('eatFood/v1/eat',msg,(state,reve:ReveData)=>{
- this.main.stopLoad();
- if(state == HttpStateType.SUCCESS){
- if(reve.retCode == 0){
- this.main.player.foodAttr = reve.data._foodAttr
- this.pack.flushGood()
- this.flush()
- this.mGoodItem.data.count -= 1
- if(this.mGoodItem.data.count <= 0){
- this.exitDistroy()
- }else{
- this.mGoodItem.flushGood()
- }
- this.main.showTips('成功食用');
- }else{
- this.main.showTips(reve.message);
- }
- }else{
- this.main.showTips('网络异常');
- }
- });
- }
- private eatGift(){
- let msg = {
- id:this.goodItem.good.id
- }
- this.main.gameHttp.sendJson('eatFood/v1/gift',msg,(state,reve:ReveData)=>{
- this.main.stopLoad();
- if(state == HttpStateType.SUCCESS){
- if(reve.retCode == 0){
- this.pack.flushGood()
- this.flush()
- this.mGoodItem.data.count -= 1
- if(this.mGoodItem.data.count <= 0){
- this.exitDistroy()
- }else{
- this.mGoodItem.flushGood()
- }
- this.main.showReward(reve)
- }else{
- this.main.showTips(reve.message);
- }
- }else{
- this.main.showTips('网络异常');
- }
- });
- }
- }
|