MyMap.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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. // 新增的上左、上、上右
  188. bronMap.push({ x: MyIndex.x - 2, y: MyIndex.y + 2, Id: this.CurrentMapPosData.Id + this.MapjsonData.mapX * 2 - 2 });
  189. bronMap.push({ x: MyIndex.x - 1, y: MyIndex.y + 2, Id: this.CurrentMapPosData.Id + this.MapjsonData.mapX * 2 - 1 });
  190. bronMap.push({ x: MyIndex.x, y: MyIndex.y + 2, Id: this.CurrentMapPosData.Id + this.MapjsonData.mapX * 2 });
  191. bronMap.push({ x: MyIndex.x + 1, y: MyIndex.y + 2, Id: this.CurrentMapPosData.Id + this.MapjsonData.mapX * 2 + 1 });
  192. bronMap.push({ x: MyIndex.x + 2, y: MyIndex.y + 2, Id: this.CurrentMapPosData.Id + this.MapjsonData.mapX * 2 + 2 });
  193. // 新增的左、中、右
  194. bronMap.push({ x: MyIndex.x - 2, y: MyIndex.y + 1, Id: this.CurrentMapPosData.Id + this.MapjsonData.mapX - 2 });
  195. bronMap.push({ x: MyIndex.x - 1, y: MyIndex.y + 1, Id: this.CurrentMapPosData.Id + this.MapjsonData.mapX - 1 });
  196. bronMap.push({ x: MyIndex.x, y: MyIndex.y + 1, Id: this.CurrentMapPosData.Id + this.MapjsonData.mapX });
  197. bronMap.push({ x: MyIndex.x + 1, y: MyIndex.y + 1, Id: this.CurrentMapPosData.Id + this.MapjsonData.mapX + 1 });
  198. bronMap.push({ x: MyIndex.x + 2, y: MyIndex.y + 1, Id: this.CurrentMapPosData.Id + this.MapjsonData.mapX + 2 });
  199. // 新增的下左、下、下右
  200. bronMap.push({ x: MyIndex.x - 2, y: MyIndex.y, Id: this.CurrentMapPosData.Id - 2 });
  201. bronMap.push({ x: MyIndex.x - 1, y: MyIndex.y, Id: this.CurrentMapPosData.Id - 1 });
  202. bronMap.push({ x: MyIndex.x, y: MyIndex.y, Id: this.CurrentMapPosData.Id });
  203. bronMap.push({ x: MyIndex.x + 1, y: MyIndex.y, Id: this.CurrentMapPosData.Id + 1 });
  204. bronMap.push({ x: MyIndex.x + 2, y: MyIndex.y, Id: this.CurrentMapPosData.Id + 2 });
  205. // 新增的下左、下、下右
  206. bronMap.push({ x: MyIndex.x - 2, y: MyIndex.y - 1, Id: this.CurrentMapPosData.Id - this.MapjsonData.mapX - 2 });
  207. bronMap.push({ x: MyIndex.x - 1, y: MyIndex.y - 1, Id: this.CurrentMapPosData.Id - this.MapjsonData.mapX - 1 });
  208. bronMap.push({ x: MyIndex.x, y: MyIndex.y - 1, Id: this.CurrentMapPosData.Id - this.MapjsonData.mapX });
  209. bronMap.push({ x: MyIndex.x + 1, y: MyIndex.y - 1, Id: this.CurrentMapPosData.Id - this.MapjsonData.mapX + 1 });
  210. bronMap.push({ x: MyIndex.x + 2, y: MyIndex.y - 1, Id: this.CurrentMapPosData.Id - this.MapjsonData.mapX + 2 });
  211. // 新增的下左、下、下右
  212. bronMap.push({ x: MyIndex.x - 2, y: MyIndex.y - 2, Id: this.CurrentMapPosData.Id - this.MapjsonData.mapX * 2 - 2 });
  213. bronMap.push({ x: MyIndex.x - 1, y: MyIndex.y - 2, Id: this.CurrentMapPosData.Id - this.MapjsonData.mapX * 2 - 1 });
  214. bronMap.push({ x: MyIndex.x, y: MyIndex.y - 2, Id: this.CurrentMapPosData.Id - this.MapjsonData.mapX * 2 });
  215. bronMap.push({ x: MyIndex.x + 1, y: MyIndex.y - 2, Id: this.CurrentMapPosData.Id - this.MapjsonData.mapX * 2 + 1 });
  216. bronMap.push({ x: MyIndex.x + 2, y: MyIndex.y - 2, Id: this.CurrentMapPosData.Id - this.MapjsonData.mapX * 2 + 2 });
  217. // bronMap.push({ x: MyIndex.x, y: MyIndex.y, Id: this.CurrentMapPosData.Id })
  218. // bronMap.push({ x: MyIndex.x, y: MyIndex.y + 1, Id: this.CurrentMapPosData.Id + this.MapjsonData.mapX })
  219. // bronMap.push({ x: MyIndex.x, y: MyIndex.y - 1, Id: this.CurrentMapPosData.Id - this.MapjsonData.mapX })
  220. // bronMap.push({ x: MyIndex.x - 1, y: MyIndex.y, Id: this.CurrentMapPosData.Id - 1 })
  221. // bronMap.push({ x: MyIndex.x + 1, y: MyIndex.y, Id: this.CurrentMapPosData.Id + 1 })
  222. // bronMap.push({ x: MyIndex.x - 1, y: MyIndex.y + 1, Id: this.CurrentMapPosData.Id + this.MapjsonData.mapX - 1 })
  223. // bronMap.push({ x: MyIndex.x + 1, y: MyIndex.y + 1, Id: this.CurrentMapPosData.Id + this.MapjsonData.mapX + 1 })
  224. // bronMap.push({ x: MyIndex.x - 1, y: MyIndex.y - 1, Id: this.CurrentMapPosData.Id - this.MapjsonData.mapX - 1 })
  225. // bronMap.push({ x: MyIndex.x + 1, y: MyIndex.y - 1, Id: this.CurrentMapPosData.Id - this.MapjsonData.mapX + 1 })
  226. for (let index = 0; index < bronMap.length; index++) {
  227. console.log(bronMap[index].Id);
  228. if (bronMap[index].x < 0 || bronMap[index].x > (this.MapjsonData.mapX - 1)) {
  229. continue
  230. }
  231. if (bronMap[index].y < 0 || bronMap[index].y > (this.MapjsonData.mapY - 1)) {
  232. continue
  233. }
  234. const SubMap = this.getSubMapDataByID(bronMap[index].Id)
  235. if (SubMap) {
  236. if (this.总共生成的地图数组.includes(SubMap.Id)) {
  237. } else {
  238. this.BronSubMap(SubMap)
  239. this.总共生成的地图数组.push(SubMap.Id)
  240. }
  241. }
  242. }
  243. for (let index = 0; index < this.总共生成的地图数组.length; index++) {
  244. const MapId = this.总共生成的地图数组[index];
  245. const SubMap = this.getSubMapDataByID(MapId)
  246. if (SubMap) {
  247. for (let j = 0; j < bronMap.length; j++) {
  248. const element = bronMap[j];
  249. if (element.Id == MapId) {
  250. break
  251. }
  252. if (j == bronMap.length - 1) {
  253. this.DeleteSubMap(SubMap)
  254. const array = this.总共生成的地图数组
  255. const elementToRemove = MapId; // 要删除的元素
  256. this.总共生成的地图数组 = array.filter(item => item !== elementToRemove);
  257. }
  258. }
  259. }
  260. }
  261. }
  262. getSubMapDataByID(ID: number) {
  263. for (let k = 0; k < this.MapPosData.length; k++) {
  264. const SubMap = this.MapPosData[k];
  265. if (SubMap.Id == ID) {
  266. return SubMap
  267. }
  268. }
  269. console.error('不存在');
  270. return null
  271. }
  272. BronSubMap(SubMap: MapPosData) {
  273. if (this.node.getChildByName('bg' + SubMap.Id).getComponent(cc.Sprite)?.spriteFrame?.name == 'bg' + SubMap.SubMapData.bg) {
  274. } else {
  275. // cc.resources.load('bg/bg' + SubMap.SubMapData.bg, cc.SpriteFrame, (err, SpriteFrame) => {
  276. // if (err) {
  277. // console.log("bg_error:" + err);
  278. // return
  279. // }
  280. // this.node.getChildByName('bg' + SubMap.Id).getComponent(cc.Sprite).spriteFrame = SpriteFrame
  281. // });
  282. let bundle = cc.assetManager.getBundle("sub");
  283. bundle.load('res/bg/bg' + SubMap.SubMapData.bg, cc.SpriteFrame, (err: Error, SpriteFrame) => {
  284. if (err) {
  285. console.log("bg_error:" + err);
  286. return
  287. }
  288. this.node.getChildByName('bg' + SubMap.Id).getComponent(cc.Sprite).spriteFrame = SpriteFrame
  289. });
  290. }
  291. for (let j = 0; j < SubMap.SubMapData.fs.length; j++) {
  292. const fs = SubMap.SubMapData.fs[j];
  293. // cc.resources.load('footstep/footstep' + fs.sp, cc.Prefab, (err, Prefab) => {
  294. // if (err) {
  295. // console.log("bg_error:" + err);
  296. // return
  297. // }
  298. // let SubPrefab = cc.instantiate(Prefab)
  299. // SubPrefab.setPosition(fs.x, fs.y)
  300. // SubPrefab.angle = -fs.ro
  301. // SubPrefab.setScale(fs.sc)
  302. // SubPrefab.active = true
  303. // this.node.getChildByName('bg' + SubMap.Id).addChild(SubPrefab)
  304. // });
  305. let bundle = cc.assetManager.getBundle("sub");
  306. bundle.load('res/footstep/footstep' + fs.sp, cc.Prefab, (err: Error, Prefab) => {
  307. if (err) {
  308. console.log("bg_error:" + err);
  309. return
  310. }
  311. let SubPrefab = cc.instantiate(Prefab)
  312. SubPrefab.setPosition(fs.x, fs.y)
  313. SubPrefab.angle = -fs.ro
  314. SubPrefab.setScale(fs.sc)
  315. SubPrefab.active = true
  316. this.node.getChildByName('bg' + SubMap.Id).addChild(SubPrefab)
  317. });
  318. }
  319. }
  320. DeleteSubMap(SubMap: MapPosData) {
  321. this.总共生成的地图数组 = this.总共生成的地图数组.filter(item => item !== SubMap.Id);
  322. this.node.getChildByName('bg' + SubMap.Id).getComponent(cc.Sprite).spriteFrame = null
  323. this.node.getChildByName('bg' + SubMap.Id).children.forEach(e => { e.destroy() })
  324. }
  325. //得到需要更新地图的下标
  326. GetMapUpdateIndex() {
  327. this.MapjsonData.mapX
  328. this.MapjsonData.mapY
  329. let NeedUpMapArray = []
  330. //只有长宽都大于3的
  331. let X_Has_Couont = 0
  332. let Y_Has_Couont = 0
  333. let Youhua = false
  334. if (this.MapjsonData.mapX >= 3 && this.MapjsonData.mapY >= 3) {
  335. X_Has_Couont = this.MapjsonData.mapX - 2
  336. Y_Has_Couont = this.MapjsonData.mapY - 2
  337. Youhua = true
  338. }
  339. if (Youhua) {
  340. //开启优化
  341. for (let i = 1; i < this.MapjsonData.mapX - 1; i++) {
  342. for (let j = 1; j < this.MapjsonData.mapY - 1; j++) {
  343. //输出所有在这个ID需要更新的地图
  344. // console.log(i + j * this.MapjsonData.mapX)
  345. NeedUpMapArray.push({ x: i, y: j })
  346. }
  347. }
  348. } else {
  349. //不开启优化
  350. }
  351. console.log(NeedUpMapArray);
  352. return NeedUpMapArray
  353. }
  354. }