MyMap.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. import CanmeraScrpts from "../Canmera/CanmeraScrpts";
  8. import LoadManager from "../LoadManager";
  9. import LocalData from "../LocalData";
  10. import RewardMnr from "../Template/RewardMnr";
  11. const { ccclass, property } = cc._decorator;
  12. type MapDataType = {
  13. bg: string,
  14. fs: {
  15. sp: number,
  16. ro: number,
  17. sc: number,
  18. x: number,
  19. y: number,
  20. }[]
  21. }
  22. type MapType = {
  23. map: MapDataType[],
  24. mapX: number,
  25. mapY: number,
  26. }
  27. type MapPosData = {
  28. index_x: number,
  29. index_y: number,
  30. Id: number,
  31. NodePos: cc.Vec2,
  32. SubMapData: MapDataType,
  33. }
  34. type NeedUpdataIndex = {
  35. x: number,
  36. y: number,
  37. }
  38. @ccclass
  39. export default class MyMap extends cc.Component {
  40. MapjsonData: MapType
  41. @property(cc.Node)
  42. CharacterNode: cc.Node = null;
  43. MapPosData: MapPosData[] = []
  44. private _CurrentMapPosData: MapPosData = null;
  45. public get CurrentMapPosData(): MapPosData {
  46. return this._CurrentMapPosData;
  47. }
  48. public set CurrentMapPosData(value: MapPosData) {
  49. if (this._CurrentMapPosData == value) {
  50. } else {
  51. this._CurrentMapPosData = value;
  52. this.UpdateMap()
  53. }
  54. }
  55. //更新自己位置数据的间隔s
  56. UpMyindexInterval: number = 0.1
  57. //更新自己位置数据的间隔s
  58. NeedUpdataIndexData: NeedUpdataIndex[] = []
  59. protected start(): void {
  60. this.Load('4')
  61. RewardMnr.getInstance().Reset()
  62. }
  63. public Load(level: string) {
  64. // cc.resources.load("json/map" + level, cc.JsonAsset, this.LoadDone.bind(this));
  65. let bundle = cc.assetManager.getBundle("sub");
  66. bundle.load("res/json/map" + level, cc.JsonAsset, this.LoadDone.bind(this));
  67. }
  68. private LoadDone(error: Error, resource: cc.JsonAsset) {
  69. if (error) {
  70. console.log(error.message);
  71. return;
  72. }
  73. if (window['map99']) {
  74. this.MapjsonData = window['map99']
  75. } else {
  76. this.MapjsonData = resource.json;
  77. }
  78. if (this.MapjsonData) {
  79. this.initMap()
  80. }
  81. }
  82. initMap() {
  83. this.NeedUpdataIndexData = this.GetMapUpdateIndex()
  84. //宽多少个
  85. this.MapjsonData.mapX
  86. //高多少个
  87. this.MapjsonData.mapY
  88. let total = this.MapjsonData.mapX * this.MapjsonData.mapY
  89. let index_x = 0
  90. let index_y = 0
  91. //生成全部地图
  92. for (let index = 0; index < total; index++) {
  93. // const SubMap = this.MapjsonData.map[index];
  94. // console.log(SubMap);
  95. //console.log('ID:', index);
  96. if (index % this.MapjsonData.mapX == 0) {
  97. index_y++
  98. index_x = 0
  99. }
  100. index_x++
  101. let SubMapPosData: MapPosData = {
  102. index_x: 1,
  103. index_y: 1,
  104. Id: 1,
  105. NodePos: null,
  106. SubMapData: this.MapjsonData.map[index]
  107. }
  108. SubMapPosData.Id = index
  109. SubMapPosData.index_x = (index_x - 1)
  110. SubMapPosData.index_y = (index_y - 1)
  111. SubMapPosData.NodePos = new cc.Vec2(750 * (index_x - 1), 1334 * (index_y - 1))
  112. this.MapPosData.push(SubMapPosData)
  113. let MapNode = cc.instantiate(new cc.Node())
  114. MapNode.addComponent(cc.Sprite)
  115. MapNode.setContentSize(750, 1334)
  116. MapNode.setPosition(SubMapPosData.NodePos)
  117. MapNode.name = 'bg' + index
  118. MapNode.parent = this.node
  119. }
  120. this.InitCharacterPrefab()
  121. return
  122. console.log(this.MapPosData);
  123. }
  124. //初始化人物的预制体
  125. InitCharacterPrefab() {
  126. (async () => {
  127. let RoleSkin = LocalData.getInstance().getCurrentCharacterSkin();
  128. let Prefab = await LoadManager.instance.LoadAssets<cc.Prefab>('res/Role/Role' + RoleSkin);
  129. if (cc.isValid(Prefab)) {
  130. let PrefabNode = cc.instantiate(Prefab)
  131. cc.Canvas.instance.node.addChild(PrefabNode)
  132. let zindex = cc.Canvas.instance.node.getChildByName("Parabola").getSiblingIndex()
  133. PrefabNode.setSiblingIndex(zindex + 1)
  134. cc.Camera.main.node.getComponent(CanmeraScrpts).Character = PrefabNode
  135. this.CharacterNode = PrefabNode
  136. let wordpos = this.node.getChildByName('bg' + this.MapjsonData['BronID']).convertToWorldSpaceAR(
  137. cc.v2(this.MapjsonData['BronX'], this.MapjsonData['BronY']))
  138. this.CharacterNode.setPosition(this.CharacterNode.parent.convertToNodeSpaceAR(wordpos))
  139. this.schedule(() => {
  140. let wordPos = this.CharacterNode.parent.convertToWorldSpaceAR(this.CharacterNode.position)
  141. let mapPos = this.node.convertToNodeSpaceAR(wordPos)
  142. this.Myindex(mapPos)
  143. }, this.UpMyindexInterval)
  144. }
  145. })();
  146. }
  147. Myindex(target: cc.Vec3) {
  148. let SceneSize = cc.v2(750, 1334)
  149. let halfX = SceneSize.x / 2
  150. let halfY = SceneSize.y / 2
  151. //是否越界
  152. let iscross = true
  153. for (let index = 0; index < this.MapPosData.length; index++) {
  154. const e = this.MapPosData[index];
  155. if (target.x > e.NodePos.x - halfX &&
  156. target.x <= e.NodePos.x + halfX &&
  157. target.y <= e.NodePos.y + halfY &&
  158. target.y > e.NodePos.y - halfY) {
  159. this.CurrentMapPosData = e
  160. // console.log('ID x y', e.Id, e.index_x, e.index_y);
  161. iscross = false
  162. break;
  163. }
  164. }
  165. if (iscross == true) {
  166. console.error('越界了 发送事件');
  167. }
  168. }
  169. 总共生成的地图数组 = []
  170. UpdateMap() {
  171. //当前我在的地图下标
  172. this.CurrentMapPosData.index_x
  173. this.CurrentMapPosData.index_y
  174. this.CurrentMapPosData.Id
  175. //需要生成的地图数组
  176. let bronMap = []
  177. let MyIndex = cc.v2(this.CurrentMapPosData.index_x, this.CurrentMapPosData.index_y);
  178. // bronMap.push({ x: MyIndex.x - 1, y: MyIndex.y + 1, Id: this.CurrentMapPosData.Id + this.MapjsonData.mapX - 1 })
  179. // bronMap.push({ x: MyIndex.x, y: MyIndex.y + 1, Id: this.CurrentMapPosData.Id + this.MapjsonData.mapX })
  180. // bronMap.push({ x: MyIndex.x + 1, y: MyIndex.y + 1, Id: this.CurrentMapPosData.Id + this.MapjsonData.mapX + 1 })
  181. // bronMap.push({ x: MyIndex.x - 1, y: MyIndex.y, Id: this.CurrentMapPosData.Id - 1 })
  182. // bronMap.push({ x: MyIndex.x, y: MyIndex.y, Id: this.CurrentMapPosData.Id })
  183. // bronMap.push({ x: MyIndex.x + 1, y: MyIndex.y, Id: this.CurrentMapPosData.Id + 1 })
  184. // bronMap.push({ x: MyIndex.x - 1, y: MyIndex.y - 1, Id: this.CurrentMapPosData.Id - this.MapjsonData.mapX - 1 })
  185. // bronMap.push({ x: MyIndex.x, y: MyIndex.y - 1, Id: this.CurrentMapPosData.Id - this.MapjsonData.mapX })
  186. // bronMap.push({ x: MyIndex.x + 1, y: MyIndex.y - 1, Id: this.CurrentMapPosData.Id - this.MapjsonData.mapX + 1 })
  187. bronMap.push({ x: MyIndex.x, y: MyIndex.y, Id: this.CurrentMapPosData.Id })
  188. bronMap.push({ x: MyIndex.x, y: MyIndex.y + 1, Id: this.CurrentMapPosData.Id + this.MapjsonData.mapX })
  189. bronMap.push({ x: MyIndex.x, y: MyIndex.y - 1, Id: this.CurrentMapPosData.Id - this.MapjsonData.mapX })
  190. bronMap.push({ x: MyIndex.x - 1, y: MyIndex.y, Id: this.CurrentMapPosData.Id - 1 })
  191. bronMap.push({ x: MyIndex.x + 1, y: MyIndex.y, Id: this.CurrentMapPosData.Id + 1 })
  192. bronMap.push({ x: MyIndex.x - 1, y: MyIndex.y + 1, Id: this.CurrentMapPosData.Id + this.MapjsonData.mapX - 1 })
  193. bronMap.push({ x: MyIndex.x + 1, y: MyIndex.y + 1, Id: this.CurrentMapPosData.Id + this.MapjsonData.mapX + 1 })
  194. bronMap.push({ x: MyIndex.x - 1, y: MyIndex.y - 1, Id: this.CurrentMapPosData.Id - this.MapjsonData.mapX - 1 })
  195. bronMap.push({ x: MyIndex.x + 1, y: MyIndex.y - 1, Id: this.CurrentMapPosData.Id - this.MapjsonData.mapX + 1 })
  196. for (let index = 0; index < bronMap.length; index++) {
  197. console.log(bronMap[index].Id);
  198. if (bronMap[index].x < 0 || bronMap[index].x > (this.MapjsonData.mapX - 1)) {
  199. continue
  200. }
  201. if (bronMap[index].y < 0 || bronMap[index].y > (this.MapjsonData.mapY - 1)) {
  202. continue
  203. }
  204. const SubMap = this.getSubMapDataByID(bronMap[index].Id)
  205. if (SubMap) {
  206. if (this.总共生成的地图数组.includes(SubMap.Id)) {
  207. } else {
  208. this.BronSubMap(SubMap)
  209. this.总共生成的地图数组.push(SubMap.Id)
  210. }
  211. }
  212. }
  213. for (let index = 0; index < this.总共生成的地图数组.length; index++) {
  214. const MapId = this.总共生成的地图数组[index];
  215. const SubMap = this.getSubMapDataByID(MapId)
  216. if (SubMap) {
  217. for (let j = 0; j < bronMap.length; j++) {
  218. const element = bronMap[j];
  219. if (element.Id == MapId) {
  220. break
  221. }
  222. if (j == bronMap.length - 1) {
  223. this.DeleteSubMap(SubMap)
  224. const array = this.总共生成的地图数组
  225. const elementToRemove = MapId; // 要删除的元素
  226. this.总共生成的地图数组 = array.filter(item => item !== elementToRemove);
  227. }
  228. }
  229. }
  230. }
  231. }
  232. getSubMapDataByID(ID: number) {
  233. for (let k = 0; k < this.MapPosData.length; k++) {
  234. const SubMap = this.MapPosData[k];
  235. if (SubMap.Id == ID) {
  236. return SubMap
  237. }
  238. }
  239. console.error('不存在');
  240. return null
  241. }
  242. BronSubMap(SubMap: MapPosData) {
  243. if (this.node.getChildByName('bg' + SubMap.Id).getComponent(cc.Sprite)?.spriteFrame?.name == 'bg' + SubMap.SubMapData.bg) {
  244. } else {
  245. // cc.resources.load('bg/bg' + SubMap.SubMapData.bg, cc.SpriteFrame, (err, SpriteFrame) => {
  246. // if (err) {
  247. // console.log("bg_error:" + err);
  248. // return
  249. // }
  250. // this.node.getChildByName('bg' + SubMap.Id).getComponent(cc.Sprite).spriteFrame = SpriteFrame
  251. // });
  252. let bundle = cc.assetManager.getBundle("sub");
  253. bundle.load('res/bg/bg' + SubMap.SubMapData.bg, cc.SpriteFrame, (err: Error, SpriteFrame) => {
  254. if (err) {
  255. console.log("bg_error:" + err);
  256. return
  257. }
  258. this.node.getChildByName('bg' + SubMap.Id).getComponent(cc.Sprite).spriteFrame = SpriteFrame
  259. });
  260. }
  261. for (let j = 0; j < SubMap.SubMapData.fs.length; j++) {
  262. const fs = SubMap.SubMapData.fs[j];
  263. // cc.resources.load('footstep/footstep' + fs.sp, cc.Prefab, (err, Prefab) => {
  264. // if (err) {
  265. // console.log("bg_error:" + err);
  266. // return
  267. // }
  268. // let SubPrefab = cc.instantiate(Prefab)
  269. // SubPrefab.setPosition(fs.x, fs.y)
  270. // SubPrefab.angle = -fs.ro
  271. // SubPrefab.setScale(fs.sc)
  272. // SubPrefab.active = true
  273. // this.node.getChildByName('bg' + SubMap.Id).addChild(SubPrefab)
  274. // });
  275. let bundle = cc.assetManager.getBundle("sub");
  276. bundle.load('res/footstep/footstep' + fs.sp, cc.Prefab, (err: Error, Prefab) => {
  277. if (err) {
  278. console.log("bg_error:" + err);
  279. return
  280. }
  281. let SubPrefab = cc.instantiate(Prefab)
  282. SubPrefab.setPosition(fs.x, fs.y)
  283. SubPrefab.angle = -fs.ro
  284. SubPrefab.setScale(fs.sc)
  285. SubPrefab.active = true
  286. this.node.getChildByName('bg' + SubMap.Id).addChild(SubPrefab)
  287. });
  288. }
  289. }
  290. DeleteSubMap(SubMap: MapPosData) {
  291. this.总共生成的地图数组 = this.总共生成的地图数组.filter(item => item !== SubMap.Id);
  292. this.node.getChildByName('bg' + SubMap.Id).getComponent(cc.Sprite).spriteFrame = null
  293. this.node.getChildByName('bg' + SubMap.Id).children.forEach(e => { e.destroy() })
  294. }
  295. //得到需要更新地图的下标
  296. GetMapUpdateIndex() {
  297. this.MapjsonData.mapX
  298. this.MapjsonData.mapY
  299. let NeedUpMapArray = []
  300. //只有长宽都大于3的
  301. let X_Has_Couont = 0
  302. let Y_Has_Couont = 0
  303. let Youhua = false
  304. if (this.MapjsonData.mapX >= 3 && this.MapjsonData.mapY >= 3) {
  305. X_Has_Couont = this.MapjsonData.mapX - 2
  306. Y_Has_Couont = this.MapjsonData.mapY - 2
  307. Youhua = true
  308. }
  309. if (Youhua) {
  310. //开启优化
  311. for (let i = 1; i < this.MapjsonData.mapX - 1; i++) {
  312. for (let j = 1; j < this.MapjsonData.mapY - 1; j++) {
  313. //输出所有在这个ID需要更新的地图
  314. // console.log(i + j * this.MapjsonData.mapX)
  315. NeedUpMapArray.push({ x: i, y: j })
  316. }
  317. }
  318. } else {
  319. //不开启优化
  320. }
  321. console.log(NeedUpMapArray);
  322. return NeedUpMapArray
  323. }
  324. }