buildingsInfo.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. var reGameStates = require('GameStates');
  2. //建筑物信息
  3. var BuildInfo = cc.Class({
  4. name: "BuildInfo",
  5. properties: {
  6. //建筑类型
  7. buildType: {
  8. default: reGameStates.BuildType.Housing,
  9. type: cc.Enum(reGameStates.BuildType),
  10. },
  11. //建筑id
  12. id: -1,
  13. //建筑所占区域
  14. occupyArea: new cc.Vec2(),
  15. //起始位置,代码动态设置
  16. startTilePos: {
  17. default: new cc.Vec2(),
  18. visible: false,
  19. },
  20. //是否激活建筑物
  21. //建筑在地图上,但是没激活,不能给游客们进行相应的操作
  22. isItActive: {
  23. default: false,
  24. tooltip: '是否激活建筑物',
  25. },
  26. //是否有人占用
  27. isItOccupied: {
  28. default: false,
  29. visible: false,
  30. },
  31. //占用对象用的信息
  32. //AI_worker_Player
  33. occupantPlayerInfo: {
  34. default: null,
  35. visible: false,
  36. },
  37. //有东西吗?//是否存在物品
  38. //
  39. isThereAnItem: {
  40. default: false,
  41. visible: false,
  42. },
  43. //是否可以销售产品
  44. isItSaleable: {
  45. default: false,
  46. visible: false,
  47. tooltip: '动态设定,是否允许销售',
  48. },
  49. //是否停运
  50. isItStopOperation: {
  51. default: false,
  52. visible: false,
  53. tooltip: '动态设定,是否停止运营',
  54. },
  55. //建筑名称
  56. /**
  57. * 重要:建筑时候 要用名字匹配来做减法 ManageGame.MuinusBuilding
  58. */
  59. buildingName: {
  60. default: '',
  61. },
  62. BuildingPrefabName: {
  63. default: '',
  64. },
  65. EnglishName: {
  66. default: '',
  67. },
  68. //建筑简介
  69. BuildingSynopsis: {
  70. default: '',
  71. },
  72. //建筑图片
  73. BuildingSprite: {
  74. default: null,
  75. type: cc.Sprite
  76. },
  77. //此建筑维护费用
  78. RunningCost: {
  79. default: 200,
  80. type: cc.Integer,
  81. visible: true,
  82. tooltip: '消耗:维护费用/每月',
  83. },
  84. //此建筑每分钟消耗的体力值
  85. consumeStrength: {
  86. default: 0,
  87. type: cc.Integer,
  88. visible: true,
  89. // displayName: '体力',
  90. tooltip: '消耗:体力/每分钟',
  91. },
  92. //此建筑总共消耗的体力值
  93. //工厂的生产力,
  94. totalConsumption: {
  95. default: 50,
  96. type: cc.Integer,
  97. visible: true,
  98. // displayName: '体力',
  99. tooltip: '这个建筑总共消耗的体力值,工厂代表生成力,商店代表销售力',
  100. },
  101. //商品的库存总量
  102. totalInventory: {
  103. default: 0,
  104. type: cc.Integer,
  105. tooltip: '商品的库存总量:销售商店需要,工厂',
  106. },
  107. //工厂:代表生产的商品
  108. //商店:代表销售的商品
  109. //定义的商品类型,也可动态添加
  110. //todo...
  111. goodsArray: {
  112. default: [],
  113. type: reGameStates.goods,
  114. },
  115. //当前销售的商品
  116. _goods: {
  117. default: null,
  118. visible: false,
  119. },
  120. //商品的库存
  121. _inventory: {
  122. default: 0,
  123. type: cc.Integer,
  124. visible: false,
  125. },
  126. //目标建筑信息
  127. //工厂只对应商店
  128. _targetBuildingsInfo: {
  129. default: null,
  130. visible: false,
  131. },
  132. //可以寻找的目标点
  133. //记录图块的二位数组
  134. _gotoPosistion: {
  135. default: [],
  136. visible: false,
  137. },
  138. //特殊建筑用到的影响值,在预制设置
  139. specialSetValue: {
  140. default: 3,
  141. type: cc.Integer,
  142. visible: true,
  143. tooltip: '特殊建筑需要填写的值',
  144. },
  145. consumeStrengthAddValue: {
  146. default: 0,
  147. type: cc.Integer,
  148. visible: false,
  149. tooltip: '消耗加成值:体力/每分钟',
  150. },
  151. goodsPriceAddValue: {
  152. default: 0,
  153. type: cc.Integer,
  154. visible: false,
  155. tooltip: '商品销售加成值',
  156. },
  157. AssetValue: {
  158. default: 0,
  159. type: cc.Integer,
  160. visible: false,
  161. tooltip: '资产值',
  162. },
  163. },
  164. //设置当前存量
  165. onSetCurrentInventory(value) {
  166. // cc.log("onSetCurrentInventory", value);
  167. //购买的个数
  168. let callBackNum = Math.abs(value);
  169. this._inventory += Number(value);
  170. if (this._inventory <= 0) {
  171. // cc.warn("onSetCurrentInventory", this._inventory);
  172. //如果商品库存不够,返回只购买剩余的个数
  173. callBackNum += this._inventory;
  174. this._inventory = 0;
  175. }
  176. if (this._inventory > this.totalInventory)
  177. this._inventory = this.totalInventory;
  178. // this.node.getComponent("buildingTips").setShowStockData(null,"x"+this._inventory);
  179. if (this.updatainventory != null) {
  180. this.updatainventory();
  181. }
  182. // console.log("现在调用这个了",value);
  183. return callBackNum;
  184. },
  185. updatainventory: function () {
  186. },
  187. setCallBackinventory: function (updatainventory) {
  188. this.updatainventory = updatainventory;
  189. }
  190. });
  191. cc.Class({
  192. extends: cc.Component,
  193. properties: {
  194. title: cc.Label,
  195. //建筑物信息
  196. buildInfo: {
  197. type: BuildInfo,
  198. default: null
  199. },
  200. //绘制提示区域
  201. buildZone: cc.Node,
  202. _canBuild: false,
  203. //是否是从数据初始化
  204. InitPosFromStore: {
  205. default: false,
  206. visible: false,
  207. },
  208. //初始化位置
  209. InitPos: false,
  210. //建筑初始化的开始位置
  211. InitStartPos: new cc.Vec2(),
  212. //初始化商店的商品信息
  213. InitBuildingInfo: {
  214. default: false,
  215. tooltip: '初始化商店的商品信息',
  216. },
  217. ShowTip: {
  218. default: null,
  219. type: cc.Label,
  220. tooltip: '显示状态提示UI节点',
  221. },
  222. ShowTipString: '',
  223. //初始工作地点的状态
  224. InitWorkBuildingInfo: {
  225. default: false,
  226. tooltip: '初始工作地点的状态',
  227. },
  228. InitWorkBuildingIndex: {
  229. default: 0,
  230. type: cc.Integer,
  231. tooltip: '下标:初始工作地点的状态',
  232. },
  233. // //是否添加到原料地
  234. // isAddMatrialArray: {
  235. // default: false,
  236. // tooltip: '是否添加到原料地',
  237. // },
  238. },
  239. onLoad() {
  240. this._buildZone = this.buildZone.getComponent('buildZone');
  241. //设置空字符
  242. // this.title.string = '';
  243. // this.buildInfo.setCallBackinventory(function () {
  244. // this.node.getComponent("buildingTips").setShowStockData(null,"x"+this.buildInfo._inventory);
  245. // }.bind(this));
  246. this.AssetValues = [];
  247. this.AssetValues.push(5000);
  248. this.AssetValues.push(10000);
  249. this.AssetValues.push(15000);
  250. this.AssetValues.push(400);
  251. this.AssetValues.push(500);
  252. this.AssetValues.push(600);
  253. this.AssetValues.push(700);
  254. this.AssetValues.push(800);
  255. this.AssetValues.push(900);
  256. // console.log("我经过吗",this.buildInfo.id);
  257. //设置财产值
  258. //银行
  259. // 406001
  260. //加工厂
  261. // 610002
  262. //农田
  263. // 610003
  264. //绿化带
  265. // 401001
  266. //花坛
  267. // 402001
  268. //游乐场
  269. // 403001
  270. //医院
  271. // 404001
  272. //住宅
  273. // 101001
  274. //单元楼
  275. // 102001
  276. //别墅
  277. // 103001
  278. //矿坑
  279. // 610005
  280. //伐木场
  281. // 610004
  282. // 神龙相关id
  283. //农田
  284. // 610006
  285. //警察局
  286. // 405001
  287. //甜品店
  288. // 610010
  289. //甜品店
  290. // 610011
  291. //甜品店
  292. // 610012
  293. //甜品店
  294. // 610013
  295. //甜品店
  296. // 610014
  297. //甜品店
  298. // 610015
  299. //甜品店
  300. // 610016
  301. switch (this.buildInfo.id) {
  302. case 101001:
  303. this.buildInfo.AssetValue = this.AssetValues[0];
  304. break;
  305. case 102001:
  306. this.buildInfo.AssetValue = this.AssetValues[1];
  307. break;
  308. case 103001:
  309. this.buildInfo.AssetValue = this.AssetValues[1];
  310. break;
  311. case 610003:
  312. this.buildInfo.AssetValue = this.AssetValues[0];
  313. break;
  314. case 610004:
  315. this.buildInfo.AssetValue = this.AssetValues[1];
  316. break;
  317. case 610005:
  318. this.buildInfo.AssetValue = this.AssetValues[1];
  319. break;
  320. case 610002:
  321. this.buildInfo.AssetValue = this.AssetValues[0];
  322. break;
  323. case 610010:
  324. case 610013:
  325. case 610016:
  326. this.buildInfo.AssetValue = this.AssetValues[0];
  327. break;
  328. case 610011:
  329. case 610014:
  330. this.buildInfo.AssetValue = this.AssetValues[1];
  331. break;
  332. case 610012:
  333. case 610015:
  334. this.buildInfo.AssetValue = this.AssetValues[2];
  335. break;
  336. case 401001:
  337. this.buildInfo.AssetValue = this.AssetValues[0];
  338. break;
  339. case 402001:
  340. this.buildInfo.AssetValue = this.AssetValues[1];
  341. break;
  342. case 403001:
  343. this.buildInfo.AssetValue = this.AssetValues[2];
  344. break;
  345. }
  346. // if (this.buildInfo.buildType == reGameStates.BuildType.Housing) {
  347. //
  348. // this.buildInfo.AssetValue = this.AssetValues[0];
  349. // } else if (this.buildInfo.buildType == reGameStates.BuildType.Farmland) {
  350. // this.buildInfo.AssetValue = this.AssetValues[1];
  351. // } else if (this.buildInfo.buildType == reGameStates.BuildType.TimberYard) {
  352. // this.buildInfo.AssetValue = this.AssetValues[2];
  353. // } else if (this.buildInfo.buildType == reGameStates.BuildType.MiningPit) {
  354. // this.buildInfo.AssetValue = this.AssetValues[3];
  355. // } else if (this.buildInfo.buildType == reGameStates.BuildType.Factory) {
  356. // this.buildInfo.AssetValue = this.AssetValues[4];
  357. // } else if (this.buildInfo.buildType == reGameStates.BuildType.Shop) {
  358. // this.buildInfo.AssetValue = this.AssetValues[5];
  359. // } else if (this.buildInfo.buildType == reGameStates.BuildType.Special) {
  360. // this.buildInfo.AssetValue = this.AssetValues[6];
  361. // }
  362. },
  363. start() {
  364. this._tileMap = GlobalD.TiledMap;
  365. //开始初始化位置,添加到数组里面
  366. if (this.InitPos) {
  367. //场景的预制初始化可销售状态
  368. if (this.buildInfo.buildType == reGameStates.BuildType.Farmland) {
  369. //设置销售状态,
  370. this.buildInfo.isItSaleable = true;
  371. }
  372. //把物体定位到对应的坐标上去
  373. this._currentTiledValue = this.InitStartPos;
  374. if (this._currentTiledValue) {
  375. //往前移一格
  376. var endTiledPos = cc.v2(this._currentTiledValue.x, this._currentTiledValue.y);
  377. var endPos = this._tileMap._getTheMiddleLocationFromtilePos(endTiledPos);
  378. this.node.setPosition(endPos);
  379. let isHas = GlobalD.game.doesItExistArray(this.buildInfo.id);
  380. if (!isHas) {
  381. //起始坐标,占位范围,是否占位
  382. GlobalD.game.addBuildTiled(this.buildInfo.id, this._currentTiledValue, this.buildInfo.occupyArea);
  383. this.buildInfo.startTilePos = this._currentTiledValue;
  384. GlobalD.game.addBuilding(this);
  385. console.log(this.buildInfo.buildingName);
  386. } else {//如果已经存在,更新新位置
  387. //可以移动
  388. let isMove = true;
  389. GlobalD.game.updateBuildOccupy(isMove, this.buildInfo.id, this._currentTiledValue, this.buildInfo.occupyArea);
  390. this.buildInfo.startTilePos = this._currentTiledValue;
  391. GlobalD.game.updateBuilding(this);
  392. }
  393. }
  394. }
  395. //初始化设置房屋信息
  396. if (this.InitBuildingInfo) {
  397. //设置销售物品
  398. this.onSettingBuildingState();
  399. }
  400. if (this.InitWorkBuildingInfo) {
  401. this.onInitBuildingState();
  402. }
  403. if (this.buildInfo.consumeStrength == 0 && this.buildInfo.buildType !== reGameStates.BuildType.Shop && this.buildInfo.buildType !== reGameStates.BuildType.Special) {
  404. cc.warn('这个建筑可能需要消耗或者增加体力值:', this.buildInfo.startTilePos);
  405. }
  406. },
  407. //代码设置位置
  408. setInitStartPos: function (x, y) {
  409. this.InitStartPos = cc.v2(x, y);
  410. //把物体定位到对应的坐标上去
  411. this._currentTiledValue = this.InitStartPos;
  412. if (this._currentTiledValue) {
  413. //往前移一格
  414. var endTiledPos = cc.v2(this._currentTiledValue.x, this._currentTiledValue.y);
  415. var endPos = this._tileMap._getTheMiddleLocationFromtilePos(endTiledPos);
  416. this.node.setPosition(endPos);
  417. let isHas = GlobalD.game.doesItExistArray(this.buildInfo.id);
  418. if (!isHas) {
  419. //起始坐标,占位范围,是否占位
  420. GlobalD.game.addBuildTiled(this.buildInfo.id, this._currentTiledValue, this.buildInfo.occupyArea);
  421. this.buildInfo.startTilePos = this._currentTiledValue;
  422. GlobalD.game.addBuilding(this);
  423. } else {//如果已经存在,更新新位置
  424. //可以移动
  425. let isMove = true;
  426. GlobalD.game.updateBuildOccupy(isMove, this.buildInfo.id, this._currentTiledValue, this.buildInfo.occupyArea);
  427. this.buildInfo.startTilePos = this._currentTiledValue;
  428. GlobalD.game.updateBuilding(this);
  429. }
  430. }
  431. },
  432. //初始化时候,生成房屋的数据状态
  433. onInitBuildingState() {
  434. // if (this.buildInfo.buildType == reGameStates.BuildType.Farmland)
  435. {
  436. //设置库存
  437. this.buildInfo._inventory = this.buildInfo.totalInventory;
  438. //设置满库存状态
  439. this.node.getComponent('WorkingBuilding').onSetWorkStateFormReadingData(this.InitWorkBuildingIndex);
  440. }
  441. },
  442. //设置房屋当前状态
  443. //比如,商店:没有商品,
  444. onSettingBuildingState() {
  445. //如果是商店的话
  446. if (this.buildInfo.buildType == reGameStates.BuildType.Shop) {
  447. let num = this.buildInfo.onSetCurrentInventory(-1000);
  448. // cc.log(num);
  449. //初始化销售的商品
  450. for (let i = 0; i < this.buildInfo.goodsArray.length; i++) {
  451. if (this.buildInfo.goodsArray[i].isItSale) {
  452. this.buildInfo._goods = this.buildInfo.goodsArray[i];
  453. break;
  454. }
  455. }
  456. }
  457. },
  458. //在预制初始化时候调用
  459. onInitFromPrefabs() {
  460. },
  461. onShowTip(tipString) {
  462. if (!this.ShowTip) return;
  463. this.ShowTip.string = tipString;
  464. this.ShowTip.node.parent.active = true;
  465. setTimeout(() => {
  466. this.ShowTip.node.parent.active = false;
  467. }, 1000)
  468. },
  469. //商品售罄
  470. onSetTipSellOut() {
  471. // cc.log('商店售罄');
  472. // this.onShowTip(this.ShowTipString);
  473. //通知工厂生产销售商品
  474. //{buildingInfo,goodsItem}
  475. GlobalD.game.onNotificationFactory(this);
  476. },
  477. //工人送完商品设置
  478. //商店
  479. onResetFromWorkersFinished() {
  480. //暂时设置最高的库存量
  481. this.buildInfo.onSetCurrentInventory(5);
  482. //清空对应的工厂信息
  483. this.buildInfo._targetBuildingsInfo = null;
  484. },
  485. //工厂到商店流程中断时候设置
  486. onResetFromWorkersSuspend() {
  487. //暂时设置最高的库存量
  488. // this.buildInfo.onSetCurrentInventory(0);
  489. //清空对应的工厂信息
  490. this.buildInfo._targetBuildingsInfo = null;
  491. },
  492. //商店重置对应工厂信息
  493. onResetProductionRequest() {
  494. //通知工厂对应的工人
  495. if (this.buildInfo.occupantPlayerInfo != null) {
  496. //工厂设置值
  497. this.buildInfo.occupantPlayerInfo.transTarget = null;
  498. }
  499. this.buildInfo._goods = null;
  500. this.buildInfo._targetBuildingsInfo = null;
  501. this.buildInfo.isItOccupied = false;
  502. },
  503. //清除建筑后,调用此函数
  504. onClearSelfResetFromType() {
  505. // cc.log('清除类型:', this.buildInfo.buildType)
  506. if (this.buildInfo.buildType == reGameStates.BuildType.Shop) {
  507. //如果清除的建筑是商店
  508. //通知工厂工作清除
  509. if (this.buildInfo._targetBuildingsInfo) {
  510. this.buildInfo._targetBuildingsInfo.onResetProductionRequest();
  511. }
  512. } else if (this.buildInfo.buildType == reGameStates.BuildType.Factory) {
  513. //如果是工厂拆除了
  514. //通知工厂对应的工人
  515. if (this.buildInfo.occupantPlayerInfo != null) {
  516. // this.occupantPlayerInfo.AIAttribute.isWorking = false;
  517. // this.occupantPlayerInfo._isColletion = false;
  518. //工厂设置值
  519. this.buildInfo.occupantPlayerInfo.transTarget = null;
  520. }
  521. //通知商店,通知清除对应的工厂目标
  522. if (this.buildInfo._targetBuildingsInfo)
  523. this.buildInfo._targetBuildingsInfo.onResetFromWorkersSuspend();
  524. } else if (this.buildInfo.buildType == reGameStates.BuildType.Housing) {
  525. //如果是房子拆除
  526. if (this.buildInfo.occupantPlayerInfo) {
  527. this.buildInfo.occupantPlayerInfo.onDismantleBuilding();
  528. }
  529. }
  530. // else {
  531. // //其他工作地点,比如农田,矿厂,伐木场
  532. // //特殊建筑可以忽略
  533. // // cc.log('清除类型:', this.buildInfo.buildType)
  534. // }
  535. },
  536. //如果商店停运调用这个函数
  537. onStopOperation() {
  538. if (this.buildInfo.buildType == reGameStates.BuildType.Shop) {
  539. if (this.buildInfo._targetBuildingsInfo)
  540. this.buildInfo._targetBuildingsInfo.onResetProductionRequest();
  541. //如果商店停运
  542. this.onResetFromWorkersSuspend();
  543. // cc.log('停运后,',this.buildInfo._targetBuildingsInfo);
  544. } else if (this.buildInfo.buildType == reGameStates.BuildType.Factory) {
  545. if (this.buildInfo._targetBuildingsInfo) {
  546. //如果工厂停运,就把工厂对应的商店目标,通知商店,清空工厂自己
  547. this.buildInfo._targetBuildingsInfo.buildInfo._targetBuildingsInfo = null;
  548. }
  549. this.onResetProductionRequest();
  550. }
  551. },
  552. /**
  553. * 植物生长
  554. */
  555. onInitHolyFarmlandSeedFromGrow(growStage,spriteFrame){
  556. this.node.getComponent('WorkingBuilding').onSetGrow(growStage,spriteFrame);
  557. // this.node.getComponent('WorkingBuilding').onHolyFarmlandSeedFromGrow();
  558. }
  559. });