MyMap.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. // Learn TypeScript:
  2. // - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
  3. // Learn Attribute:
  4. // - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
  5. // Learn life-cycle callbacks:
  6. // - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html
  7. const { ccclass, property } = cc._decorator;
  8. type MapDataType = {
  9. bg: string,
  10. fs: {
  11. sp: number,
  12. ro: number,
  13. sc: number,
  14. x: number,
  15. y: number,
  16. }[]
  17. }
  18. type MapType = {
  19. map: MapDataType[],
  20. mapX: number,
  21. mapY: number,
  22. }
  23. type MapPosData = {
  24. index_x: number,
  25. index_y: number,
  26. Id: number,
  27. NodePos: cc.Vec2,
  28. SubMapData: MapDataType,
  29. }
  30. type NeedUpdataIndex = {
  31. x: number,
  32. y: number,
  33. }
  34. @ccclass
  35. export default class MyMap extends cc.Component {
  36. MapjsonData: MapType
  37. @property(cc.Node)
  38. CharacterNode: cc.Node = null;
  39. MapPosData: MapPosData[] = []
  40. private _CurrentMapPosData: MapPosData = null;
  41. public get CurrentMapPosData(): MapPosData {
  42. return this._CurrentMapPosData;
  43. }
  44. public set CurrentMapPosData(value: MapPosData) {
  45. if (this._CurrentMapPosData == value) {
  46. } else {
  47. this._CurrentMapPosData = value;
  48. this.UpdateMap()
  49. }
  50. }
  51. //更新自己位置数据的间隔s
  52. UpMyindexInterval: number = 1
  53. //更新自己位置数据的间隔s
  54. NeedUpdataIndexData: NeedUpdataIndex[] = []
  55. protected start(): void {
  56. this.Load('2')
  57. }
  58. public Load(level: string) {
  59. cc.resources.load("json/map" + level, cc.JsonAsset, this.LoadDone.bind(this));
  60. this.schedule(() => {
  61. let wordPos = this.CharacterNode.parent.convertToWorldSpaceAR(this.CharacterNode.position)
  62. let mapPos = this.node.convertToNodeSpaceAR(wordPos)
  63. // console.log(mapPos);
  64. this.Myindex(mapPos)
  65. }, this.UpMyindexInterval)
  66. }
  67. private LoadDone(error: Error, resource: cc.JsonAsset) {
  68. if (error) {
  69. console.log(error.message);
  70. return;
  71. }
  72. this.MapjsonData = resource.json;
  73. if (this.MapjsonData) {
  74. this.initMap()
  75. }
  76. }
  77. initMap() {
  78. this.NeedUpdataIndexData = this.GetMapUpdateIndex()
  79. //宽多少个
  80. this.MapjsonData.mapX
  81. //高多少个
  82. this.MapjsonData.mapY
  83. let total = this.MapjsonData.mapX * this.MapjsonData.mapY
  84. let index_x = 0
  85. let index_y = 0
  86. //生成全部地图
  87. for (let index = 0; index < total; index++) {
  88. // const SubMap = this.MapjsonData.map[index];
  89. // console.log(SubMap);
  90. //console.log('ID:', index);
  91. if (index % this.MapjsonData.mapX == 0) {
  92. index_y++
  93. index_x = 0
  94. }
  95. index_x++
  96. let SubMapPosData: MapPosData = {
  97. index_x: 1,
  98. index_y: 1,
  99. Id: 1,
  100. NodePos: null,
  101. SubMapData: this.MapjsonData.map[index]
  102. }
  103. SubMapPosData.Id = index
  104. SubMapPosData.index_x = (index_x - 1)
  105. SubMapPosData.index_y = (index_y - 1)
  106. SubMapPosData.NodePos = new cc.Vec2(750 * (index_x - 1), 1334 * (index_y - 1))
  107. this.MapPosData.push(SubMapPosData)
  108. let MapNode = cc.instantiate(new cc.Node())
  109. MapNode.addComponent(cc.Sprite)
  110. MapNode.setContentSize(750, 1334)
  111. MapNode.setPosition(SubMapPosData.NodePos)
  112. MapNode.name = 'bg' + index
  113. MapNode.parent = this.node
  114. }
  115. return
  116. console.log(this.MapPosData);
  117. }
  118. Myindex(target: cc.Vec3) {
  119. let SceneSize = cc.v2(750, 1334)
  120. let halfX = SceneSize.x / 2
  121. let halfY = SceneSize.y / 2
  122. //是否越界
  123. let iscross = true
  124. for (let index = 0; index < this.MapPosData.length; index++) {
  125. const e = this.MapPosData[index];
  126. if (target.x > e.NodePos.x - halfX &&
  127. target.x <= e.NodePos.x + halfX &&
  128. target.y <= e.NodePos.y + halfY &&
  129. target.y > e.NodePos.y - halfY) {
  130. this.CurrentMapPosData = e
  131. console.log('ID x y', e.Id, e.index_x, e.index_y);
  132. iscross = false
  133. break;
  134. }
  135. }
  136. if (iscross == true) {
  137. console.error('越界了 发送事件');
  138. }
  139. }
  140. 总共生成的地图数组 = []
  141. UpdateMap() {
  142. // for (let index = 0; index < this.NeedUpdataIndexData.length; index++) {
  143. // const element = this.NeedUpdataIndexData[index];
  144. // if (element.x == this.CurrentMapPosData.index_x && element.y == this.CurrentMapPosData.index_y) {
  145. // }
  146. // }
  147. //当前我在的地图下标
  148. this.CurrentMapPosData.index_x
  149. this.CurrentMapPosData.index_y
  150. this.CurrentMapPosData.Id
  151. //需要生成的地图数组
  152. let bronMap = []
  153. let MyIndex = cc.v2(this.CurrentMapPosData.index_x, this.CurrentMapPosData.index_y);
  154. bronMap.push({ x: MyIndex.x - 1, y: MyIndex.y + 1, Id: this.CurrentMapPosData.Id + this.MapjsonData.mapX - 1 })
  155. bronMap.push({ x: MyIndex.x, y: MyIndex.y + 1, Id: this.CurrentMapPosData.Id + this.MapjsonData.mapX })
  156. bronMap.push({ x: MyIndex.x + 1, y: MyIndex.y + 1, Id: this.CurrentMapPosData.Id + this.MapjsonData.mapX + 1 })
  157. bronMap.push({ x: MyIndex.x - 1, y: MyIndex.y, Id: this.CurrentMapPosData.Id - 1 })
  158. bronMap.push({ x: MyIndex.x, y: MyIndex.y, Id: this.CurrentMapPosData.Id })
  159. bronMap.push({ x: MyIndex.x + 1, y: MyIndex.y, Id: this.CurrentMapPosData.Id + 1 })
  160. bronMap.push({ x: MyIndex.x - 1, y: MyIndex.y - 1, Id: this.CurrentMapPosData.Id - this.MapjsonData.mapX - 1 })
  161. bronMap.push({ x: MyIndex.x, y: MyIndex.y - 1, Id: this.CurrentMapPosData.Id - this.MapjsonData.mapX })
  162. bronMap.push({ x: MyIndex.x + 1, y: MyIndex.y - 1, Id: this.CurrentMapPosData.Id - this.MapjsonData.mapX + 1 })
  163. for (let index = 0; index < bronMap.length; index++) {
  164. const SubMap = this.getSubMapDataByID(bronMap[index].Id)
  165. if (SubMap) {
  166. if (this.总共生成的地图数组.includes(SubMap.Id)) {
  167. } else {
  168. console.log('生成一次' + SubMap.Id);
  169. this.BronSubMap(SubMap)
  170. this.总共生成的地图数组.push(SubMap.Id)
  171. }
  172. }
  173. }
  174. console.error('this.总共生成的地图数组');
  175. console.error(this.总共生成的地图数组);
  176. for (let index = 0; index < this.总共生成的地图数组.length; index++) {
  177. const MapId = this.总共生成的地图数组[index];
  178. const SubMap = this.getSubMapDataByID(MapId)
  179. if (SubMap) {
  180. for (let j = 0; j < bronMap.length; j++) {
  181. const element = bronMap[j];
  182. if (element.Id == MapId) {
  183. break
  184. }
  185. if (j == bronMap.length - 1) {
  186. this.DeleteSubMap(SubMap)
  187. }
  188. }
  189. }
  190. }
  191. }
  192. getSubMapDataByID(ID: number) {
  193. for (let k = 0; k < this.MapPosData.length; k++) {
  194. const SubMap = this.MapPosData[k];
  195. if (SubMap.Id == ID) {
  196. return SubMap
  197. }
  198. }
  199. console.error('不存在');
  200. return null
  201. }
  202. BronSubMap(SubMap: MapPosData) {
  203. if (this.node.getChildByName('bg' + SubMap.Id).getComponent(cc.Sprite)?.spriteFrame?.name == 'bg' + SubMap.SubMapData.bg) {
  204. } else {
  205. cc.resources.load('bg/bg' + SubMap.SubMapData.bg, cc.SpriteFrame, (err, SpriteFrame) => {
  206. if (err) {
  207. console.log("bg_error:" + err);
  208. return
  209. }
  210. this.node.getChildByName('bg' + SubMap.Id).getComponent(cc.Sprite).spriteFrame = SpriteFrame
  211. });
  212. }
  213. for (let j = 0; j < SubMap.SubMapData.fs.length; j++) {
  214. const fs = SubMap.SubMapData.fs[j];
  215. cc.resources.load('footstep/footstep' + fs.sp, cc.Prefab, (err, Prefab) => {
  216. if (err) {
  217. console.log("bg_error:" + err);
  218. return
  219. }
  220. let SubPrefab = cc.instantiate(Prefab)
  221. SubPrefab.setPosition(fs.x, fs.y)
  222. SubPrefab.angle = fs.ro
  223. SubPrefab.setScale(fs.sc)
  224. SubPrefab.active = true
  225. this.node.getChildByName('bg' + SubMap.Id).addChild(SubPrefab)
  226. });
  227. }
  228. }
  229. DeleteSubMap(SubMap: MapPosData) {
  230. this.总共生成的地图数组 = this.总共生成的地图数组.filter(item => item !== SubMap.Id);
  231. this.node.getChildByName('bg' + SubMap.Id).getComponent(cc.Sprite).spriteFrame = null
  232. this.node.getChildByName('bg' + SubMap.Id).children.forEach(e => { e.destroy() })
  233. }
  234. //得到需要更新地图的下标
  235. GetMapUpdateIndex() {
  236. this.MapjsonData.mapX
  237. this.MapjsonData.mapY
  238. let NeedUpMapArray = []
  239. //只有长宽都大于3的
  240. let X_Has_Couont = 0
  241. let Y_Has_Couont = 0
  242. let Youhua = false
  243. if (this.MapjsonData.mapX >= 3 && this.MapjsonData.mapY >= 3) {
  244. X_Has_Couont = this.MapjsonData.mapX - 2
  245. Y_Has_Couont = this.MapjsonData.mapY - 2
  246. Youhua = true
  247. }
  248. if (Youhua) {
  249. //开启优化
  250. for (let i = 1; i < this.MapjsonData.mapX - 1; i++) {
  251. for (let j = 1; j < this.MapjsonData.mapY - 1; j++) {
  252. //输出所有在这个ID需要更新的地图
  253. // console.log(i + j * this.MapjsonData.mapX)
  254. NeedUpMapArray.push({ x: i, y: j })
  255. }
  256. }
  257. } else {
  258. //不开启优化
  259. }
  260. console.log(NeedUpMapArray);
  261. return NeedUpMapArray
  262. }
  263. }