common.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /**
  2. * 公共数据
  3. */
  4. Module = {
  5. gameObj:null,//gameScene对象
  6. goFishingObj: null, //鱼线对象
  7. famLayerObj: null, //传说鱼对象层
  8. fishingLine: null, //钓鱼线
  9. isPlaySound: true,
  10. state: false,
  11. gameState: false,
  12. score: 200, //分数
  13. depth: 60, //水深 M为单位,每10米,1136高度
  14. fishingNum: 0, //捕鱼数量
  15. fishingNumLimit: 5, //当次捕鱼上限
  16. gravitation: 1, //当前吸引力
  17. isCollision: false,
  18. money: 0, //当前金币
  19. isShouGou: false, //当前是否收钩
  20. nonceMoney: 0, //本局获得金币
  21. addMoneyLabel: null, //钩到鱼后显示的价格
  22. prefabArr: [], //鱼预制体
  23. nonceFishingArr: [], //存放当局捕到鱼的tag
  24. tujianFishing: [],//纪录鱼是否发现
  25. tujianObj: null,//图鉴对象
  26. offlineTime: "",
  27. getGiftNum: 0,
  28. }
  29. /**
  30. * window.Utils 公共方法
  31. */
  32. window.Utils = {
  33. /**
  34. * 播放动画
  35. * @param {*} url 资源路径
  36. * @param {*} action 动作名称
  37. * @param {*} pNode 父节点
  38. * @param {*} dl 播放时间
  39. * @param {*} pos 坐标
  40. */
  41. playEffectSpine: function(url , action , pNode , dl, pos) {
  42. cc.loader.loadRes(url, sp.SkeletonData, function(err, res) {
  43. var node =new cc.Node();
  44. var sk = node.addComponent(sp.Skeleton);
  45. sk.skeletonData = res;
  46. sk.animation = action;
  47. sk.premultipliedAlpha = false;
  48. pNode.addChild(node);
  49. if (pos != undefined) {
  50. node.x = pos.x;
  51. node.y = pos.y;
  52. }
  53. node.runAction(cc.sequence(cc.delayTime(dl) , cc.removeSelf()));
  54. });
  55. },
  56. /**
  57. * 播放音乐
  58. * @param {*} url 路径
  59. * @param {*} type 类型 1播放背景音乐 2播放音效
  60. * @param {*} isLoop 是否循环播放
  61. */
  62. playSound: function(url , type , isLoop) {
  63. if (!Module.isPlaySound) return;
  64. if (type == 1) {
  65. cc.loader.loadRes(url, cc.AudioClip, function (err, clip) {
  66. cc.audioEngine.playMusic(clip, isLoop);
  67. });
  68. } else {
  69. cc.loader.loadRes(url, cc.AudioClip, function (err, clip) {
  70. cc.audioEngine.playEffect(clip, isLoop);
  71. });
  72. }
  73. },
  74. /**
  75. * 金币转换,超过5位数,后3位用K表示,只显示
  76. * @param {*} moeny 传入进来的金币值
  77. */
  78. moneySwitch: function (money) {
  79. return money;
  80. console.log("---money---: " , money);
  81. if (money < 1000000){
  82. return money;
  83. }
  84. var tempStr = money+"";
  85. var s = tempStr.substr(0,4) + "k";
  86. return s;
  87. },
  88. /**
  89. * 生成n-m之间随机值
  90. * @param {*} n 第一个值
  91. * @param {*} m 第二个值
  92. */
  93. random : function (n, m) {
  94. var random = Math.floor(Math.random() * (m - n + 1) + n);
  95. return random;
  96. },
  97. /**
  98. * 传入所需的概率值,返回结果
  99. * @param {*} val 概率值,比如80等同80%概率
  100. */
  101. probability: function (val) {
  102. return window.Utils.random(1 , 100) <= val;
  103. },
  104. /**
  105. * 获取鱼价格
  106. * @param {*} tag 鱼的tag值 PS: tag值是个鱼的标签,tag值不同,鱼属性也不同
  107. */
  108. getFishingPrice : function (tag) {
  109. if(window.Config.fishing.price[tag] == undefined) return 100;
  110. return window.Config.fishing.price[tag];
  111. },
  112. /**
  113. * 获取鱼重量
  114. * @param {*} tag 鱼的tag值
  115. */
  116. getFishingWeight: function(tag) {
  117. var weights = window.Config.fishing.weight;
  118. if (weights[tag] == undefined) return weights[0];
  119. return weights[tag];
  120. },
  121. /**
  122. * 获取升级鱼竿的价格
  123. */
  124. getfishRodPrice: function() {
  125. var weight = window.Config.fishRod.weight;
  126. var price = window.Config.fishRod.price;
  127. for (var i = 0; i < weight.length; i++) {
  128. if (weight[i] == Module.fishingNumLimit) return price[i+1];
  129. }
  130. return 100000000;
  131. },
  132. /**
  133. * 获取升级鱼竿的重量
  134. */
  135. getfishRodWeight: function() {
  136. var weight = window.Config.fishRod.weight;
  137. for (var i = 0; i < weight.length; i++) {
  138. if (weight[i] == Module.fishingNumLimit) return weight[i+1];
  139. }
  140. return 100000000;
  141. },
  142. /**
  143. * 获取鱼饵的价格
  144. */
  145. getfishingBaitPrice: function() {
  146. var gravitation = window.Config.fishingBait.gravitation;
  147. var price = window.Config.fishingBait.price;
  148. for (var i = 0; i < gravitation.length; i++) {
  149. if (gravitation[i] == Module.gravitation) return price[i+1];
  150. }
  151. return 100000000;
  152. },
  153. /**
  154. * 获取鱼饵的吸引力
  155. */
  156. getfishingBaitGravitation: function() {
  157. var gravitation = window.Config.fishingBait.gravitation;
  158. for (var i = 0; i < gravitation.length; i++) {
  159. if (gravitation[i] == Module.gravitation) return gravitation[i+1];
  160. }
  161. return 100000000;
  162. },
  163. /**
  164. * 存储int类型数据
  165. * @param {*} index 存储名
  166. * @param {*} key 值
  167. */
  168. setIntegerForKey: function (index,key) {
  169. wb.Data.setItem(index,key.toString());
  170. },
  171. /**
  172. * 获取int类型数据
  173. * @param {*} index 存储名
  174. * @param {*} key 值
  175. */
  176. getIntegerForKey: function (index,defaultkey) {
  177. return parseInt(wb.Data.getItem(index,defaultkey));
  178. },
  179. /**
  180. * 存储string类型数据
  181. * @param {*} index 存储名
  182. * @param {*} key 值
  183. */
  184. setStringForKey: function (index,key) {
  185. key = key.toString();
  186. wb.Data.setItem(index,key);
  187. },
  188. /**
  189. * 获取string类型数据
  190. * @param {*} index 存储名
  191. * @param {*} key 值
  192. */
  193. getStringForKey: function (index,defaultkey) {
  194. defaultkey = defaultkey.toString();
  195. return wb.Data.getItem(index,defaultkey);
  196. },
  197. }
  198. /**
  199. * 按钮按下松手时状态
  200. */
  201. window.btnState = {
  202. touchBegin:function(touch,event){
  203. touch.target.scale = 0.9;
  204. },
  205. touchMoving:function(touch,event){
  206. },
  207. touchEnd:function(touch, event){
  208. touch.target.scale = 1;
  209. },
  210. touchCancel: function(touch, event){
  211. this.touchCount -= 1;
  212. touch.target.scale = 1;
  213. },
  214. }
  215. /**
  216. * 初始化鱼饵,鱼竿,场景数据表
  217. */
  218. window.Config = {
  219. //鱼饵
  220. fishingBait : {
  221. price : [100,300 , 600 ,900 ,1100 ,1500, 2000 ,3000, 4000],
  222. gravitation : [100,300,400,500,600,700,800,900,1200],
  223. },
  224. //鱼杆
  225. fishRod : {
  226. price : [120 , 300 ,500 ,800 ,1200, 1800 ,2500, 4000],
  227. weight : [200,300,500,600,700,800,900,1200],
  228. },
  229. //鱼的属性
  230. fishing : {
  231. price : [10,20,30,40,50,60,70,80,90,100,110,120,130,150,160,180,200,220,240,400,500,600,1000],
  232. weight : [10,20,30,40,50,60,70,80,90,100,110,120,120,120,120,120,120,120,120,120,120,120,120],
  233. name:["Grass carp", "Starfish", "Goldfish", "Lionhead goldfish", "Octopus", "Sicklefish", "Shrimp", "Seahorse", "Goby", "Catfish", "Crab", "Turtle", "Clownfish", "Devil fish", "Water wood", "Dolphin", "Angelfish", "Jellyfish", "Manatee", "Lantern fish", "Orca", "Golden dragon fish", "Golden dolphin", "Golden koi"],
  234. catch: [],//捕捉度
  235. des: [],
  236. }
  237. }
  238. /**
  239. * 读配置表
  240. */
  241. window.CSVParse ={
  242. CSVToArray:function ( strData, strDelimiter ,length) {
  243. strDelimiter = (strDelimiter || ",");
  244. var objPattern = new RegExp(
  245. (
  246. "(\\" + strDelimiter + "|\\r?\\n|\\r|^)" +
  247. "(?:\"([^\"]*(?:\"\"[^\"]*)*)\"|" +
  248. "([^\"\\" + strDelimiter + "\\r\\n]*))"
  249. ),
  250. "gi"
  251. );
  252. var arrData = [[]];
  253. var arrMatches = null;
  254. while (arrMatches = objPattern.exec( strData )){
  255. var strMatchedDelimiter = arrMatches[ 1 ];
  256. if (
  257. strMatchedDelimiter.length &&
  258. (strMatchedDelimiter != strDelimiter)
  259. ){
  260. arrData.push( [] );
  261. }
  262. if (arrMatches[ 2 ]){
  263. strMatchedValue = arrMatches[ 2 ].replace(
  264. new RegExp( "\"\"", "g" ),
  265. "\""
  266. );
  267. } else {
  268. strMatchedValue = arrMatches[ 3 ];
  269. }
  270. if(strMatchedValue.length >= 0)
  271. {
  272. arrData[ arrData.length - 1 ].push( strMatchedValue );
  273. }
  274. }
  275. //去掉空行
  276. for(var i =0 ;i < arrData.length;i++)
  277. {
  278. if(arrData[i].length == 0)
  279. {
  280. arrData.splice(i,1);
  281. i--;
  282. }
  283. }
  284. return arrData;
  285. },
  286. parseConfig:function(fileName,func)
  287. {
  288. var outDatas = [];
  289. try{
  290. var callFUnM = function(err,data){
  291. if(err){cc.log(err);return;}
  292. callFUnM.func(window.CSVParse.CSVToArray(data,","));
  293. };
  294. callFUnM.func = func;
  295. cc.loader.loadRes(fileName,callFUnM);
  296. }catch(err){
  297. cc.log("获取关卡配置表错误:"+err);
  298. return outDatas;
  299. }
  300. },
  301. };