GoodMapDecoder.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. var GoodsData = require("GoodsData");
  2. var PriceData = require("PriceData");
  3. var GoodMapDecoder = cc.Class({
  4. extends: cc.Component,
  5. properties: {
  6. jsonName:"goods",
  7. goodsList:{
  8. default:[],
  9. type:[GoodsData],
  10. },
  11. },
  12. //解析数据
  13. DecodeJson:function (event) {
  14. //cc.log("===解析物品数据===");
  15. var self = this;
  16. self.reCb = event;
  17. cc.loader.loadRes("json/"+self.jsonName, function (error, obj) {
  18. if(error)
  19. {
  20. //cc.log("+++解析出错,查下json+++" + error);
  21. self.reCb(false);
  22. return;
  23. }
  24. var jsonRoot = obj.json.goods;
  25. //cc.log("===数据长度===" + jsonRoot.length);
  26. for (var i = 0; i < jsonRoot.length; i++) {
  27. var goodsData = new GoodsData();
  28. goodsData.sex = jsonRoot[i].sex;
  29. goodsData.Id = jsonRoot[i].itemId;
  30. goodsData.icon = jsonRoot[i].icon;
  31. goodsData.name = jsonRoot[i].name;
  32. for (var j = 0; j < jsonRoot[i].priceList.length; j++) {
  33. var price = new PriceData();
  34. var data = jsonRoot[i].priceList[j];
  35. price.lowprice = data[0];
  36. price.highprice = data[1];
  37. price.probability = data[2];
  38. goodsData.priceList[j] = price;
  39. }
  40. self.goodsList[i] = goodsData;
  41. }
  42. self.reCb(true);
  43. });
  44. },
  45. //通过名字拿到当前的数据 不建议用,你要用我也没办法
  46. getDataByName:function (name) {
  47. var data = null;
  48. for (var i = this.goodsList.length - 1; i >= 0; i--) {
  49. if(name == this.goodsList[i].name)
  50. {
  51. data = this.goodsList[i];
  52. break;
  53. }
  54. }
  55. return data;
  56. },
  57. //通过itemid获取数据
  58. getDataByItemId:function(itemId){
  59. var data = null;
  60. for (var i = this.goodsList.length - 1; i >= 0; i--) {
  61. if(itemId == this.goodsList[i].Id)
  62. {
  63. data = this.goodsList[i];
  64. break;
  65. }
  66. }
  67. return data;
  68. },
  69. getJsonLength:function(){
  70. return this.goodsList.length
  71. },
  72. getDataList:function(){
  73. return this.goodsList;
  74. },
  75. getDataListBySex:function(Sex){
  76. var dataList = [];
  77. //var index = 0;
  78. for (var i = 0; i < this.goodsList.length; i++) {
  79. if(this.goodsList[i].sex == Sex || this.goodsList[i].sex == 0)
  80. {
  81. //dataList[index] = this.goodsList[i];
  82. //index += 1;
  83. dataList.push(this.goodsList[i])
  84. }
  85. }
  86. //cc.log("对应性别的玩家展示物品种类 ====== " + dataList.length);
  87. return dataList;
  88. },
  89. });
  90. module.exports = GoodMapDecoder;