FoodView.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import i18n from "../../../i18n/i18n";
  2. import ViewObject from "../../../main/ViewObject";
  3. import { HttpStateType, ReveData } from "../../../util/CHttp";
  4. import CUtil from "../../../util/CUtil";
  5. import GoodItem from "../../common/GoodItem";
  6. import Pack from "./Pack";
  7. /**
  8. * 食物查看并使用
  9. */
  10. const {ccclass, property} = cc._decorator;
  11. @ccclass
  12. export default class FoodView extends ViewObject {
  13. @property(cc.Label)
  14. mName: cc.Label = null;
  15. @property(cc.Label)
  16. mAbout: cc.Label = null;
  17. @property(GoodItem)
  18. mGoodItem: GoodItem = null;
  19. @property(cc.Label)
  20. mCount: cc.Label = null;
  21. @property(cc.Label)
  22. mUserLabel: cc.Label = null;
  23. public pack:Pack
  24. public goodItem:GoodItem
  25. public initGood(pack:Pack,goodItem:GoodItem){
  26. this.pack = pack
  27. this.goodItem = goodItem
  28. this.mGoodItem.initGood(this.main,this.goodItem.data)
  29. this.mName.string = i18n.t(goodItem.good.name)
  30. this.mAbout.string = i18n.t(goodItem.good.about)
  31. if(goodItem.good.type == 3){
  32. this.mUserLabel.string = i18n.t('使用')
  33. }else if(goodItem.good.type == 5){
  34. this.mUserLabel.string = i18n.t('食用')
  35. }
  36. this.flush()
  37. }
  38. private flush(){
  39. if(this.goodItem.good.type == 5){
  40. let foodAttr = this.main.player.foodAttr
  41. let nowDate = CUtil.getNowDateInt()
  42. if(nowDate > foodAttr.date){
  43. foodAttr.date = nowDate
  44. foodAttr.count = 0
  45. }
  46. this.mCount.string = i18n.t('今日食用食物上限')+":"+foodAttr.count+"/10"
  47. }
  48. }
  49. public onclickEat(){
  50. if(this.goodItem.good.type == 3){
  51. this.eatGift()
  52. }else if(this.goodItem.good.type == 5){
  53. this.eatFood()
  54. }
  55. }
  56. private eatFood(){
  57. let msg = {
  58. id:this.goodItem.good.id
  59. }
  60. this.main.startLoad()
  61. this.main.gameHttp.sendJson('eatFood/v1/eat',msg,(state,reve:ReveData)=>{
  62. this.main.stopLoad();
  63. if(state == HttpStateType.SUCCESS){
  64. if(reve.retCode == 0){
  65. this.main.player.foodAttr = reve.data._foodAttr
  66. this.pack.flushGood()
  67. this.flush()
  68. this.mGoodItem.data.count -= 1
  69. if(this.mGoodItem.data.count <= 0){
  70. this.exitDistroy()
  71. }else{
  72. this.mGoodItem.flushGood()
  73. }
  74. this.main.showTips('成功食用');
  75. }else{
  76. this.main.showTips(reve.message);
  77. }
  78. }else{
  79. this.main.showTips('网络异常');
  80. }
  81. });
  82. }
  83. private eatGift(){
  84. let msg = {
  85. id:this.goodItem.good.id
  86. }
  87. this.main.gameHttp.sendJson('eatFood/v1/gift',msg,(state,reve:ReveData)=>{
  88. this.main.stopLoad();
  89. if(state == HttpStateType.SUCCESS){
  90. if(reve.retCode == 0){
  91. this.pack.flushGood()
  92. this.flush()
  93. this.mGoodItem.data.count -= 1
  94. if(this.mGoodItem.data.count <= 0){
  95. this.exitDistroy()
  96. }else{
  97. this.mGoodItem.flushGood()
  98. }
  99. this.main.showReward(reve)
  100. }else{
  101. this.main.showTips(reve.message);
  102. }
  103. }else{
  104. this.main.showTips('网络异常');
  105. }
  106. });
  107. }
  108. }