edLayout.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. // "map": [
  2. // {
  3. // "bg": "5",
  4. // "fs": []
  5. // },
  6. // {
  7. // "bg": "1",
  8. // "fs": []
  9. // },
  10. // {
  11. // "bg": "1",
  12. // "fs": []
  13. // },
  14. // {
  15. // "bg": "3",
  16. // "fs": []
  17. // }
  18. // ],
  19. // "mapX": 2,
  20. // "mapY": 2,
  21. // "BronID": 0,
  22. // "BronX": 55,
  23. // "BronY": 55
  24. import footstepmove from "./footstepmove";
  25. const { ccclass, property } = cc._decorator;
  26. type ddddtype = {
  27. map: maptype[]
  28. mapX: number
  29. mapY: number
  30. BronID: number
  31. BronX: number
  32. BronY: number
  33. }
  34. type maptype = {
  35. bg: number
  36. fs: [{}]
  37. }
  38. @ccclass
  39. export default class edLayout extends cc.Component {
  40. Click: cc.Node = null;
  41. // LIFE-CYCLE CALLBACKS:
  42. // onLoad () {}
  43. start() {
  44. this.schedule(() => {
  45. let temp = this.node.children
  46. for (let index = 0; index < temp.length; index++) {
  47. const element = temp[index];
  48. if (element.childrenCount <= 0) {
  49. let tempNOde = new cc.Node
  50. tempNOde.parent = element
  51. let mylabel = tempNOde.addComponent(cc.Label)
  52. mylabel.fontSize = 80
  53. mylabel.lineHeight = 80
  54. mylabel.string = index + ''
  55. }
  56. }
  57. }, 1, 9999999999999)
  58. }
  59. onLoad() {
  60. // 监听键盘按下事件
  61. cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
  62. // 监听键盘松开事件
  63. cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
  64. //绑定事件
  65. //因为摇杆很小,如果给摇杆绑定事件玩家将很难控制,摇杆的背景比较大,所以把事件都绑定在背景上是不错的选择
  66. this.node.on(cc.Node.EventType.TOUCH_START, this.starttt, this);//当手指在背景上移动时触发move事件
  67. this.node.on(cc.Node.EventType.TOUCH_MOVE, this.move, this);//当手指在背景上移动时触发move事件
  68. this.node.on(cc.Node.EventType.TOUCH_END, this.finish, this);//当手指在目标节点区域内离开屏幕时触发finish事件
  69. this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.finish, this);//当手指在目标节点区域外离开屏幕时触发finish事件
  70. }
  71. onKeyDown(event) {
  72. switch (event.keyCode) {
  73. case cc.macro.KEY.a:
  74. this.zuo()
  75. this.zuo()
  76. break;
  77. case cc.macro.KEY.d:
  78. this.you()
  79. this.you()
  80. break;
  81. case cc.macro.KEY.w:
  82. this.shang()
  83. this.shang()
  84. break;
  85. case cc.macro.KEY.s:
  86. this.xia()
  87. this.xia()
  88. break;
  89. // 添加更多按键处理逻辑
  90. }
  91. }
  92. onKeyUp(event) {
  93. switch (event.keyCode) {
  94. case cc.macro.KEY.a:
  95. console.log('A 键被松开');
  96. break;
  97. case cc.macro.KEY.b:
  98. console.log('B 键被松开');
  99. break;
  100. // 添加更多按键处理逻辑
  101. }
  102. }
  103. onDestroy() {
  104. // 在组件销毁时,取消键盘事件监听
  105. cc.systemEvent.off(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
  106. cc.systemEvent.off(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
  107. }
  108. startpos: cc.Vec2
  109. starttt(event: cc.Event.EventTouch) {
  110. this.startpos = event.getLocation()
  111. }
  112. move(event: cc.Event.EventTouch) {//负责移动摇杆 手指移动时调用
  113. console.log(event.getLocationX(), event.getLocationY());
  114. this.node.x += (event.getLocationX() - this.startpos.x) * 0.1
  115. this.node.y += (event.getLocationY() - this.startpos.y) * 0.1
  116. this.node.x += event.getDeltaX()
  117. this.node.y += event.getDeltaY()
  118. }
  119. finish() {//摇杆弹回原位置
  120. }
  121. initx(x) {
  122. console.log(x);
  123. this.heng = x
  124. this.init()
  125. }
  126. inity(y) {
  127. console.log(y);
  128. this.shu = y
  129. this.init()
  130. }
  131. heng: number = 0
  132. shu: number = 0
  133. init() {
  134. if (!this.heng || !this.shu) {
  135. return
  136. }
  137. this.node.setContentSize(750 * this.heng, 1334 * this.shu)
  138. let total = this.heng * this.shu
  139. let background = this.node.getChildByName("background")
  140. this.node.children.forEach((e, i) => {
  141. if (i != 0) {
  142. e.destroy()
  143. }
  144. })
  145. for (let index = 0; index < total; index++) {
  146. if (index == 0) {
  147. background.on('touchstart', (event) => {
  148. console.log(index);
  149. if (window['selectNode']) {
  150. (window['selectNode'].opacity = 255)
  151. window['selectNode'].children.forEach(e => {
  152. e.color = cc.Color.WHITE
  153. })
  154. }
  155. background.opacity = 180
  156. window['selectNode'] = background
  157. })
  158. let Label = background.getChildByName("Label")
  159. Label.getComponent(cc.Label).string = index.toString()
  160. continue
  161. } else {
  162. let temp = cc.instantiate(background)
  163. temp.parent = this.node
  164. let Label = temp.getChildByName("Label")
  165. Label.getComponent(cc.Label).string = index.toString()
  166. temp.on('touchstart', (event) => {
  167. console.log(index);
  168. if (window['selectNode']) {
  169. (window['selectNode'].opacity = 255)
  170. window['selectNode'].children.forEach(e => {
  171. e.color = cc.Color.WHITE
  172. })
  173. }
  174. window['selectNode'] = temp
  175. temp.opacity = 180
  176. temp.children.forEach(e => {
  177. e.color = cc.Color.RED
  178. })
  179. })
  180. }
  181. }
  182. }
  183. hengjiayi() {
  184. let my_laye = this.node.getComponent(cc.Layout)
  185. my_laye.startAxis = cc.Layout.AxisDirection.HORIZONTAL
  186. var a = cc.instantiate(this.node.children[this.node.childrenCount - 1]);
  187. a.parent = this.node
  188. }
  189. hengjianyi() {
  190. let my_laye = this.node.getComponent(cc.Layout)
  191. my_laye.startAxis = cc.Layout.AxisDirection.HORIZONTAL
  192. this.node.children[this.node.childrenCount - 1].destroy()
  193. }
  194. shujiayi() {
  195. let my_laye = this.node.getComponent(cc.Layout)
  196. my_laye.startAxis = cc.Layout.AxisDirection.VERTICAL
  197. var a = cc.instantiate(this.node.children[this.node.childrenCount - 1]);
  198. a.parent = this.node
  199. }
  200. shujianyi() {
  201. let my_laye = this.node.getComponent(cc.Layout)
  202. my_laye.startAxis = cc.Layout.AxisDirection.VERTICAL
  203. this.node.children[this.node.childrenCount - 1].destroy()
  204. }
  205. /////
  206. shang() {
  207. this.node.y += 20
  208. }
  209. xia() {
  210. this.node.y -= 20
  211. }
  212. zuo() {
  213. this.node.x -= 20
  214. }
  215. you() {
  216. this.node.x += 20
  217. }
  218. shanchu() {
  219. if (window['selectfootstep']) {
  220. window['selectfootstep'].destroy()
  221. }
  222. }
  223. daoru() {
  224. this.heng = (window['dd'] as ddddtype).mapX
  225. this.shu = (window['dd'] as ddddtype).mapY;
  226. this.init()
  227. let mmmp = (window['dd'] as ddddtype).map
  228. mmmp.forEach(e => {
  229. e.bg
  230. e.fs
  231. })
  232. for (let i = 0; i < this.node.children.length; i++) {
  233. const node = this.node.children[i];
  234. const data = mmmp[i];
  235. const url = "res/bg/bg" + data.bg
  236. // cc.resources.load(url, cc.SpriteFrame, (err, spriteframe) => {
  237. // if (err) {
  238. // return
  239. // }
  240. // let sprite = node.getComponent(cc.Sprite);
  241. // sprite.spriteFrame = spriteframe;
  242. // });
  243. let bundle = cc.assetManager.getBundle("sub");
  244. bundle.load(url, cc.SpriteFrame, (err: Error, spriteframe) => {
  245. if (err) {
  246. return
  247. }
  248. let sprite = node.getComponent(cc.Sprite);
  249. sprite.spriteFrame = spriteframe;
  250. });
  251. for (let j = 0; j < data.fs.length; j++) {
  252. const element = data.fs[j];
  253. const url = "res/footstep/footstep" + element['sp']
  254. // cc.resources.load(url, cc.Prefab, (err, Prefab) => {
  255. // if (err) {
  256. // return
  257. // }
  258. // let a = cc.instantiate(Prefab);
  259. // a.parent = node
  260. // a.active = true
  261. // a.setPosition(element['x'], element['y'])
  262. // a.setRotation(element['ro'])
  263. // a.setScale(element['sc'])
  264. // a.addComponent(footstepmove)
  265. // });
  266. let bundle = cc.assetManager.getBundle("sub");
  267. bundle.load(url, cc.Prefab, (err: Error, Prefab) => {
  268. if (err) {
  269. return
  270. }
  271. let a = cc.instantiate(Prefab);
  272. a.parent = node
  273. a.active = true
  274. a.setPosition(element['x'], element['y'])
  275. a.setRotation(element['ro'])
  276. a.setScale(element['sc'])
  277. a.addComponent(footstepmove)
  278. });
  279. }
  280. }
  281. }
  282. out() {
  283. // 将JavaScript对象转换为JSON字符串
  284. let tempmap = this.node.children
  285. let subMapData = {
  286. map: [],
  287. mapX: 0,
  288. mapY: 0,
  289. BronID: 0,
  290. BronX: 0,
  291. BronY: 0,
  292. }
  293. for (let index = 0; index < tempmap.length; index++) {
  294. const element = tempmap[index];
  295. let Mapdata = {
  296. bg: '',
  297. fs: []
  298. }
  299. Mapdata.bg = (element.getComponent(cc.Sprite).spriteFrame.name).replace(/[^0-9]/ig, "")
  300. for (let index1 = 1; index1 < element.children.length; index1++) {
  301. const prop = element.children[index1];
  302. Mapdata.fs.push(
  303. {
  304. sp: prop.name.replace(/[^0-9]/ig, ""),
  305. // sp: prop.getComponent(cc.Sprite).spriteFrame.name.replace(/[^0-9]/ig, ""),
  306. // sp: prop.getComponent(cc.Sprite).spriteFrame.name,
  307. ro: prop.rotation,
  308. sc: prop.scale,
  309. x: prop.position.x.toFixed(2),
  310. y: prop.position.y.toFixed(2)
  311. }
  312. )
  313. }
  314. subMapData.map.push(Mapdata)
  315. }
  316. subMapData.mapX = this.heng
  317. subMapData.mapY = this.shu
  318. subMapData.BronID = 0
  319. subMapData.BronX = 55
  320. subMapData.BronY = 55
  321. console.log(subMapData);
  322. const jsonData = JSON.stringify(subMapData);
  323. console.log(jsonData);
  324. this.saveForBrowser(jsonData, 'map1')
  325. }
  326. /**
  327. * 存字符串内容到文件。
  328. * @param textToWrite - 要保存的文件内容
  329. * @param fileNameToSaveAs - 要保存的文件名
  330. */
  331. private saveForBrowser(textToWrite, fileNameToSaveAs) {
  332. let textFileAsBlob = new Blob([textToWrite], { type: 'application/json' });
  333. let downloadLink = document.createElement("a");
  334. downloadLink.download = fileNameToSaveAs;
  335. downloadLink.innerHTML = "Download File";
  336. if (window.webkitURL != null) {
  337. downloadLink.href = window.webkitURL.createObjectURL(textFileAsBlob);
  338. }
  339. downloadLink.click();
  340. }
  341. }