edLayout.ts 10 KB

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