main.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. import {mazegen} from './mazegen.js';
  2. const _CONFIG_WIDTH = 1920;
  3. const _CONFIG_HEIGHT = 1080;
  4. const _TILES_X = 64;
  5. const _TILES_Y = _TILES_X / 2;
  6. const _TIME_PER_STEP = 1.0 / 30.0;
  7. const _LAYER_NODES = 100;
  8. const _LAYER_EDGES = 50;
  9. const _LAYER_WALL = 10;
  10. const _LAYER_BG = 0;
  11. class SquareSprite {
  12. constructor(scene, node, nodes) {
  13. this._node = node;
  14. this._nodes = nodes;
  15. this._scene = scene;
  16. this._gfx = null;
  17. this._text = null;
  18. this._params = {
  19. tint: 0xFFFFFF,
  20. start: false,
  21. end: false,
  22. fScore: '',
  23. gScore: 0,
  24. };
  25. this._Redraw({});
  26. }
  27. destroy() {
  28. if (this._gfx != null) {
  29. this._gfx.destroy();
  30. }
  31. if (this._text != null) {
  32. this._text.destroy();
  33. }
  34. }
  35. Redraw(params) {
  36. let changed = false;
  37. for (const k in this._params) {
  38. if (k in params && this._params[k] != params[k]) {
  39. changed = true;
  40. this._params[k] = params[k];
  41. }
  42. }
  43. if (changed||1) {
  44. this._Redraw();
  45. }
  46. }
  47. _Redraw() {
  48. const x = this._node.metadata.position[0];
  49. const y = this._node.metadata.position[1];
  50. const w = _CONFIG_WIDTH / _TILES_X;
  51. const h = _CONFIG_HEIGHT / _TILES_Y;
  52. if (this._gfx != null) {
  53. this._gfx.destroy();
  54. }
  55. this._gfx = this._scene.add.graphics(0, 0);
  56. this._gfx.lineStyle(w / 60.0, 0xC0C0C0, 1.0);
  57. this._gfx.fillStyle(this._params.tint, 1.0);
  58. this._gfx.fillRect(x * w, y * h, w, h);
  59. this._gfx.displayOriginX = 0;
  60. this._gfx.displayOriginY = 0;
  61. this._gfx.setDepth(_LAYER_BG);
  62. }
  63. }
  64. class WallRenderer {
  65. constructor(scene, nodes) {
  66. this._nodes = nodes;
  67. this._scene = scene;
  68. this._gfx = null;
  69. }
  70. destroy() {
  71. if (this._gfx != null) {
  72. this._gfx.destroy();
  73. }
  74. }
  75. get visible() {
  76. return this._visible;
  77. }
  78. set visible(v) {
  79. this._visible = v;
  80. }
  81. Redraw() {
  82. if (this._gfx != null) {
  83. this._gfx.destroy();
  84. }
  85. this._gfx = this._scene.add.graphics(0, 0);
  86. const edges = {};
  87. for (const k in this._nodes) {
  88. const curNode = this._nodes[k];
  89. const x = curNode.metadata.position[0];
  90. const y = curNode.metadata.position[1];
  91. const w = _CONFIG_WIDTH / _TILES_X;
  92. const h = _CONFIG_HEIGHT / _TILES_Y;
  93. this._gfx.lineStyle(w / 60.0, 0xC0C0C0, 1.0);
  94. if (curNode.metadata.render.active) {
  95. this._gfx.fillStyle(0x8080FF, 1.0);
  96. } else if (curNode.metadata.render.visited) {
  97. this._gfx.fillStyle(0xFFFFFF, 1.0);
  98. } else {
  99. this._gfx.fillStyle(0x808080, 1.0);
  100. }
  101. this._gfx.fillRect(x * w, y * h, w, h);
  102. const neighbours = [[0, 1], [1, 0], [0, -1], [-1, 0]];
  103. this._gfx.lineStyle(w * 0.05, 0x000000, 1.0);
  104. this._gfx.beginPath();
  105. for (let ni = 0; ni < neighbours.length; ni++) {
  106. const n = neighbours[ni];
  107. const ki = _Key(x + n[0], y + n[1]);
  108. if (curNode.edges.indexOf(_Key(x, y + 1)) < 0) {
  109. this._gfx.moveTo(w * (x + 0.0), h * (y + 1.0));
  110. this._gfx.lineTo(w * (x + 1.0), h * (y + 1.0));
  111. }
  112. if (curNode.edges.indexOf(_Key(x + 1, y + 0)) < 0) {
  113. this._gfx.moveTo(w * (x + 1.0), h * (y + 0.0));
  114. this._gfx.lineTo(w * (x + 1.0), h * (y + 1.0));
  115. }
  116. if (curNode.edges.indexOf(_Key(x, y - 1)) < 0) {
  117. this._gfx.moveTo(w * (x + 0.0), h * (y + 0.0));
  118. this._gfx.lineTo(w * (x + 1.0), h * (y + 0.0));
  119. }
  120. if (curNode.edges.indexOf(_Key(x - 1, y)) < 0) {
  121. this._gfx.moveTo(w * (x + 0.0), h * (y + 0.0));
  122. this._gfx.lineTo(w * (x + 0.0), h * (y + 1.0));
  123. }
  124. }
  125. this._gfx.closePath();
  126. this._gfx.strokePath();
  127. }
  128. this._gfx.displayOriginX = 0;
  129. this._gfx.displayOriginY = 0;
  130. this._gfx.setDepth(_LAYER_WALL);
  131. this._gfx.setVisible(this._visible);
  132. }
  133. }
  134. class PotentialEdgeRenderer {
  135. constructor(scene, nodes) {
  136. this._nodes = nodes;
  137. this._scene = scene;
  138. this._gfx = null;
  139. }
  140. destroy() {
  141. if (this._gfx != null) {
  142. this._gfx.destroy();
  143. }
  144. }
  145. get visible() {
  146. return this._visible;
  147. }
  148. set visible(v) {
  149. this._visible = v;
  150. }
  151. Redraw() {
  152. if (this._gfx != null) {
  153. this._gfx.destroy();
  154. }
  155. this._gfx = this._scene.add.graphics(0, 0);
  156. const edges = {};
  157. for (const k in this._nodes) {
  158. const curNode = this._nodes[k];
  159. const x = curNode.metadata.position[0];
  160. const y = curNode.metadata.position[1];
  161. const w = _CONFIG_WIDTH / _TILES_X;
  162. const h = _CONFIG_HEIGHT / _TILES_Y;
  163. for (let nk of curNode.potentialEdges) {
  164. if ((k + '.' + nk) in edges ||
  165. (nk + '.' + k) in edges) {
  166. continue;
  167. }
  168. const neighbourNode = this._nodes[nk];
  169. const x1 = neighbourNode.metadata.position[0];
  170. const y1 = neighbourNode.metadata.position[1];
  171. if (curNode.metadata.render.active) {
  172. if (neighbourNode.metadata.render.visited) {
  173. this._gfx.lineStyle(w * 0.025, 0xFF8080, 1.0);
  174. } else {
  175. this._gfx.lineStyle(w * 0.025, 0x80FF80, 1.0);
  176. }
  177. } else if (neighbourNode.metadata.render.active) {
  178. if (curNode.metadata.render.visited) {
  179. this._gfx.lineStyle(w * 0.025, 0xFF8080, 1.0);
  180. } else {
  181. this._gfx.lineStyle(w * 0.025, 0x80FF80, 1.0);
  182. }
  183. } else {
  184. if (curNode.edges.indexOf(nk) >= 0) {
  185. this._gfx.lineStyle(w * 0.025, 0x0000FF, 1.0);
  186. } else {
  187. this._gfx.lineStyle(w * 0.001, 0x000000, 1.0);
  188. }
  189. }
  190. this._gfx.beginPath();
  191. this._gfx.moveTo(w * (x + 0.5), h * (y + 0.5));
  192. this._gfx.lineTo(w * (x1 + 0.5), h * (y1 + 0.5));
  193. this._gfx.closePath();
  194. this._gfx.strokePath();
  195. edges[k + '.' + nk] = true;
  196. edges[nk + '.' + k] = true;
  197. }
  198. }
  199. this._gfx.displayOriginX = 0;
  200. this._gfx.displayOriginY = 0;
  201. this._gfx.setDepth(_LAYER_EDGES);
  202. this._gfx.setVisible(this._visible);
  203. }
  204. }
  205. class EdgeRenderer {
  206. constructor(scene, nodes) {
  207. this._nodes = nodes;
  208. this._scene = scene;
  209. this._gfx = null;
  210. this._visible = false;
  211. }
  212. destroy() {
  213. if (this._gfx != null) {
  214. this._gfx.destroy();
  215. }
  216. }
  217. get visible() {
  218. return this._visible;
  219. }
  220. set visible(v) {
  221. this._visible = v;
  222. }
  223. Redraw() {
  224. if (this._gfx != null) {
  225. this._gfx.destroy();
  226. }
  227. this._gfx = this._scene.add.graphics(0, 0);
  228. const edges = {};
  229. for (const k in this._nodes) {
  230. const curNode = this._nodes[k];
  231. const x = curNode.metadata.position[0];
  232. const y = curNode.metadata.position[1];
  233. const w = _CONFIG_WIDTH / _TILES_X;
  234. const h = _CONFIG_HEIGHT / _TILES_Y;
  235. for (let nk of curNode.edges) {
  236. if ((k + '.' + nk) in edges ||
  237. (nk + '.' + k) in edges) {
  238. continue;
  239. }
  240. const neighbourNode = this._nodes[nk];
  241. const x1 = neighbourNode.metadata.position[0];
  242. const y1 = neighbourNode.metadata.position[1];
  243. this._gfx.lineStyle(w * 0.025, 0x000000, 1.0);
  244. this._gfx.beginPath();
  245. this._gfx.moveTo(w * (x + 0.5), h * (y + 0.5));
  246. this._gfx.lineTo(w * (x1 + 0.5), h * (y1 + 0.5));
  247. this._gfx.closePath();
  248. this._gfx.strokePath();
  249. edges[k + '.' + nk] = true;
  250. edges[nk + '.' + k] = true;
  251. }
  252. }
  253. this._gfx.displayOriginX = 0;
  254. this._gfx.displayOriginY = 0;
  255. this._gfx.setDepth(_LAYER_EDGES);
  256. this._gfx.setVisible(this._visible);
  257. }
  258. }
  259. class NodeRenderer {
  260. constructor(scene, nodes) {
  261. this._nodes = nodes;
  262. this._scene = scene;
  263. this._gfx = null;
  264. this._visible = false;
  265. }
  266. destroy() {
  267. if (this._gfx != null) {
  268. this._gfx.destroy();
  269. }
  270. }
  271. get visible() {
  272. return this._visible;
  273. }
  274. set visible(v) {
  275. this._visible = v;
  276. }
  277. Redraw() {
  278. if (this._gfx != null) {
  279. this._gfx.destroy();
  280. }
  281. this._gfx = this._scene.add.graphics(0, 0);
  282. for (const k in this._nodes) {
  283. const node = this._nodes[k];
  284. const x = node.metadata.position[0];
  285. const y = node.metadata.position[1];
  286. const w = _CONFIG_WIDTH / _TILES_X;
  287. const h = _CONFIG_HEIGHT / _TILES_Y;
  288. if (node.metadata.render.visited) {
  289. this._gfx.fillStyle(0xFF8080, 1.0);
  290. } else {
  291. this._gfx.fillStyle(0x80FF80, 1.0);
  292. }
  293. this._gfx.fillCircle(w * (x + 0.5), h * (y + 0.5), w * 0.1);
  294. }
  295. this._gfx.displayOriginX = 0;
  296. this._gfx.displayOriginY = 0;
  297. this._gfx.setDepth(_LAYER_NODES);
  298. this._gfx.setVisible(this._visible);
  299. }
  300. }
  301. function _Key(x, y) {
  302. return x + '.' + y;
  303. }
  304. function _Distance(p1, p2) {
  305. return ((p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2) ** 0.5;
  306. }
  307. class AStarRenderer {
  308. constructor(nodes, scene) {
  309. this._scene = scene;
  310. this._nodes = nodes;
  311. this._edgeRenderer = new EdgeRenderer(scene, nodes);
  312. this._potentialEdgeRenderer = new PotentialEdgeRenderer(scene, nodes);
  313. this._nodeRenderer = new NodeRenderer(scene, nodes);
  314. this._wallRenderer = new WallRenderer(scene, nodes);
  315. this._wallRenderer._visible = true;
  316. this._sprites = {};
  317. }
  318. destroy() {
  319. for (const k in this._sprites) {
  320. this._sprites[k].destroy();
  321. }
  322. this._nodeRenderer.destroy();
  323. this._edgeRenderer.destroy();
  324. this._potentialEdgeRenderer.destroy();
  325. this._wallRenderer.destroy();
  326. }
  327. Update(touched) {
  328. if (touched == null) {
  329. touched = Object.keys(this._nodes);
  330. }
  331. for (const k of touched) {
  332. const node = this._nodes[k];
  333. const k_bg = k + '.bg';
  334. if (!(k_bg in this._sprites)) {
  335. this._sprites[k_bg] = new SquareSprite(this._scene, node, this._nodes);
  336. }
  337. const params = {};
  338. if (node.metadata.render.visited) {
  339. params.tint = 0xFFFFFF;
  340. } else {
  341. params.tint = 0x808080;
  342. }
  343. if (node.metadata.render.active) {
  344. params.tint = 0x8080FF;
  345. }
  346. this._sprites[k_bg].Redraw(params);
  347. }
  348. this._nodeRenderer.Redraw();
  349. this._edgeRenderer.Redraw();
  350. this._potentialEdgeRenderer.Redraw();
  351. this._wallRenderer.Redraw();
  352. }
  353. }
  354. class Graph {
  355. constructor() {
  356. this._nodes = {};
  357. }
  358. get Nodes() {
  359. return this._nodes;
  360. }
  361. AddNode(k, e, m) {
  362. this._nodes[k] = {
  363. edges: [...e],
  364. potentialEdges: [...e],
  365. metadata: m
  366. };
  367. }
  368. }
  369. class MazeGenDemo {
  370. constructor() {
  371. this._game = this._CreateGame();
  372. }
  373. _CreateGame() {
  374. const self = this;
  375. const config = {
  376. type: Phaser.AUTO,
  377. scene: {
  378. preload: function() { self._OnPreload(this); },
  379. create: function() { self._OnCreate(this); },
  380. update: function() { self._OnUpdate(this); },
  381. },
  382. scale: {
  383. mode: Phaser.Scale.FIT,
  384. autoCenter: Phaser.Scale.CENTER_BOTH,
  385. width: _CONFIG_WIDTH,
  386. height: _CONFIG_HEIGHT
  387. }
  388. };
  389. return new Phaser.Game(config);
  390. }
  391. _Reset() {
  392. this._astarRenderer.destroy();
  393. this._Init();
  394. }
  395. _Init() {
  396. this._stepTime = 0.0;
  397. this._previousFrame = null;
  398. this._graph = new Graph();
  399. for (let x = 0; x < _TILES_X; x++) {
  400. for (let y = 0; y < _TILES_Y; y++) {
  401. const k = _Key(x, y);
  402. this._graph.AddNode(
  403. k, [],
  404. {
  405. position: [x, y],
  406. weight: 0,
  407. render: {
  408. visited: false,
  409. }
  410. });
  411. }
  412. }
  413. // for (let x = 0; x < _TILES_X; x++) {
  414. // for (let y = 0; y < _TILES_Y; y++) {
  415. // const roll = Math.random();
  416. // if (roll < 0.2) {
  417. // const k = _Key(x, y);
  418. // this._graph.Nodes[k].metadata.weight = -1;
  419. // }
  420. // }
  421. // }
  422. //
  423. for (let x = 0; x < _TILES_X; x++) {
  424. for (let y = 0; y < _TILES_Y; y++) {
  425. const k = _Key(x, y);
  426. for (let xi = -1; xi <= 1; xi++) {
  427. for (let yi = -1; yi <= 1; yi++) {
  428. if (xi == 0 && yi == 0 || (Math.abs(xi) + Math.abs(yi) != 1)) {
  429. continue;
  430. }
  431. const ki = _Key(x + xi, y + yi);
  432. if (ki in this._graph.Nodes) {
  433. this._graph.Nodes[k].potentialEdges.push(ki);
  434. }
  435. }
  436. }
  437. }
  438. }
  439. const start = _Key(0, 0);
  440. const end = _Key(4, 0);
  441. this._mazeGenerator = new mazegen.MazeGenerator(this._graph.Nodes);
  442. this._mazeIterator = this._mazeGenerator.GenerateIteratively(start);
  443. this._astarRenderer = new AStarRenderer(this._graph.Nodes, this._scene);
  444. }
  445. _OnPreload(scene) {
  446. this._scene = scene;
  447. // /this._scene.load.image('sky', 'assets/sky.png');
  448. }
  449. _OnCreate(scene) {
  450. this._keys = {
  451. f: this._scene.input.keyboard.addKey('F'),
  452. r: this._scene.input.keyboard.addKey('R'),
  453. n: this._scene.input.keyboard.addKey('N'),
  454. e: this._scene.input.keyboard.addKey('E'),
  455. p: this._scene.input.keyboard.addKey('P'),
  456. w: this._scene.input.keyboard.addKey('W'),
  457. };
  458. this._keys.w.on('down', function() {
  459. this._astarRenderer._wallRenderer.visible = !this._astarRenderer._wallRenderer.visible;
  460. }, this);
  461. this._keys.n.on('down', function() {
  462. this._astarRenderer._nodeRenderer.visible = !this._astarRenderer._nodeRenderer.visible;
  463. }, this);
  464. this._keys.e.on('down', function() {
  465. this._astarRenderer._edgeRenderer.visible = !this._astarRenderer._edgeRenderer.visible;
  466. }, this);
  467. this._keys.p.on('down', function() {
  468. this._astarRenderer._potentialEdgeRenderer.visible = !this._astarRenderer._potentialEdgeRenderer.visible;
  469. }, this);
  470. this._keys.r.on('down', function() {
  471. this._Reset();
  472. }, this);
  473. this._keys.f.on('down', function() {
  474. this._mazeIterator.next();
  475. }, this);
  476. this._Init();
  477. }
  478. _OnUpdate(scene) {
  479. const currentFrame = scene.time.now;
  480. if (this._previousFrame == null) {
  481. this._previousFrame = currentFrame;
  482. this._astarRenderer.Update(null);
  483. }
  484. const timeElapsedInS = Math.min(
  485. (currentFrame - this._previousFrame) / 1000.0, 1.0 / 30.0);
  486. let touched = [];
  487. this._stepTime += timeElapsedInS;
  488. while (this._stepTime >= _TIME_PER_STEP) {
  489. this._stepTime -= _TIME_PER_STEP;
  490. const r = this._mazeIterator.next();
  491. if (r.done) {
  492. setTimeout(() => {
  493. this._Reset();
  494. }, 2000);
  495. }
  496. }
  497. this._astarRenderer.Update(null);
  498. this._previousFrame = currentFrame;
  499. }
  500. }
  501. const _GAME = new MazeGenDemo();