overworld-scene.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  1. class OverworldScene {
  2. constructor() {
  3. this.camera = {};
  4. this.area = {};
  5. this.player = {};
  6. this.rails = [];
  7. this.events = [];
  8. this.script = [];
  9. this.fadeAmount = 0;
  10. }
  11. init() {
  12. loadGame();
  13. this.camera = { x: 0, y: 0, center: { x: 0, y: 0 }, movementSpeed: 3 };
  14. this.area = {
  15. width: 2560,
  16. height: 2560,
  17. };
  18. var railData = [
  19. {
  20. unlocks: {
  21. isVisible: 0,
  22. isActivated: 0,
  23. isDamaged: 9,
  24. },
  25. path: [
  26. // { x: 768, y: 1024, node: 10 },
  27. { x: 896, y: 1024, node: 10, enter: 0 },
  28. { x: 1024, y: 1024 },
  29. { x: 1152, y: 1152 },
  30. { x: 1280, y: 1152, node: 10, down: { width: 3, height: 3, successRail: 1, successNode: 0 }, enter: 1 },
  31. { x: 1408, y: 1024 },
  32. { x: 1536, y: 1024, node: 10 },
  33. ],
  34. pathUnlocks: [
  35. { node: 5, unlock: 3, up: { width: 4, height: 4, successRail: 2, successNode: 0 } },
  36. ],
  37. },
  38. {
  39. unlocks: {
  40. isVisible: 0,
  41. isActivated: 2,
  42. isDamaged: 9,
  43. },
  44. path: [
  45. { x: 1280, y: 1152 + 30, node: 10, up: { width: 3, height: 4, successRail: 0, successNode: 3 }, enter: 2 },
  46. { x: 1408, y: 1152 + 30 },
  47. { x: 1536, y: 1280 },
  48. { x: 1664, y: 1280, node: 10, enter: 3 },
  49. ],
  50. pathUnlocks: [],
  51. },
  52. {
  53. unlocks: {
  54. isVisible: 3,
  55. isActivated: 4,
  56. isDamaged: 9,
  57. },
  58. path: [
  59. { x: 1536, y: 1024 - 30, node: 10, down: { width: 5, height: 4, successRail: 0, successNode: 5 }, enter: 4 },
  60. { x: 1664, y: 896 },
  61. { x: 1664, y: 768 },
  62. { x: 1792, y: 640, node: 10, enter: 5, up: { width: 4, height: 5, successRail: 3, successNode: 0 } },
  63. ],
  64. pathUnlocks: [
  65. { node: 3, unlock: 6, down: { width: 4, height: 6, successRail: 4, successNode: 0 } },
  66. ],
  67. },
  68. {
  69. unlocks: {
  70. isVisible: 4,
  71. isActivated: 6,
  72. isDamaged: 9,
  73. },
  74. path: [
  75. { x: 1792, y: 640 - 30, node: 10, enter: 6, down: { width: 5, height: 4, successRail: 2, successNode: 3 } },
  76. { x: 1920, y: 768, },
  77. { x: 2048, y: 768 },
  78. { x: 2176, y: 896, node: 10, enter: 7 },
  79. ],
  80. pathUnlocks: [],
  81. },
  82. {
  83. unlocks: {
  84. isVisible: 6,
  85. isActivated: 8,
  86. isDamaged: 9,
  87. },
  88. path: [
  89. { x: 1792, y: 640 + 30, node: 10, up: { width: 5, height: 4, successRail: 2, successNode: 3 }, enter: 8 },
  90. { x: 1920 - 30, y: 768 + 30, },
  91. { x: 2048, y: 768 + 30 },
  92. { x: 2176, y: 896 + 30 },
  93. { x: 2304 - 30, y: 1024 },
  94. { x: 2432, y: 1024, node: 10, enter: 9 },
  95. ],
  96. pathUnlocks: [
  97. { node: 5, unlock: 9, down: { width: 5, height: 4, successRail: 2, successNode: 3 } },
  98. ],
  99. },
  100. {
  101. unlocks: {
  102. isVisible: 9,
  103. isActivated: 9,
  104. isDamaged: 10,
  105. },
  106. path: [
  107. { x: 2176, y: 1152, node: 10, enter: 10 },
  108. { x: 2304, y: 1024 + 30, },
  109. { x: 2432, y: 1024 + 30, node: 10 },
  110. ],
  111. pathUnlocks: [],
  112. },
  113. {
  114. unlocks: {
  115. isVisible: 9,
  116. isActivated: 9,
  117. isDamaged: 11,
  118. },
  119. path: [
  120. { x: 1792, y: 1280, node: 10, down: { width: 6, height: 4, successRail: 7, successNode: 4 } },
  121. { x: 1920, y: 1280 },
  122. { x: 2048, y: 1152 },
  123. { x: 2176, y: 1152, node: 10 },
  124. ],
  125. pathUnlocks: [],
  126. },
  127. {
  128. unlocks: {
  129. isVisible: 10,
  130. isActivated: 11,
  131. isDamaged: 13,
  132. },
  133. path: [
  134. { x: 1280, y: 1408, node: 10, enter: 12, up: { width: 0, height: 0, successRail: 8, successNode: 7 } },
  135. { x: 1408, y: 1408 },
  136. { x: 1536, y: 1280 + 30 },
  137. { x: 1664, y: 1280 + 30 },
  138. { x: 1792, y: 1280 + 30, node: 10, enter: 11 },
  139. ],
  140. pathUnlocks: [],
  141. },
  142. {
  143. unlocks: {
  144. isVisible: 11,
  145. isActivated: 13,
  146. },
  147. path: [
  148. { x: 384, y: 1536 },
  149. { x: 512, y: 1536 },
  150. { x: 640, y: 1536 },
  151. { x: 768, y: 1536 },
  152. { x: 896, y: 1536 },
  153. { x: 1024, y: 1536, enter: 14 },
  154. { x: 1152, y: 1408 - 50 },
  155. { x: 1280, y: 1408 - 50, node: 20, enter: 13 },
  156. ],
  157. pathUnlocks: [],
  158. },
  159. ];
  160. this.rails = [];
  161. for (let i = 0; i < railData.length; i++) {
  162. let rail = new Rail();
  163. rail.setPath(railData[i].path);
  164. rail.setPathUnlocks(railData[i].pathUnlocks);
  165. rail.unlocks = railData[i].unlocks;
  166. this.rails.push(rail);
  167. }
  168. let currentNode = this.getRailNode(saveData.player.rail, saveData.player.railnode);
  169. this.player = {
  170. x: currentNode.x,
  171. y: currentNode.y,
  172. width: 20,
  173. height: 20,
  174. center: { x: 10, y: 10 },
  175. angle: 45 * Math.PI / 180,
  176. rotationSpeed: 3 * Math.PI / 180,
  177. movementSpeed: 3,
  178. target: { x: currentNode.x, y: currentNode.y },
  179. rail: saveData.player.rail,
  180. railnode: saveData.player.railnode,
  181. };
  182. this.camera.x = saveData.camera.x;
  183. this.camera.y = saveData.camera.y;
  184. var eventData = [
  185. {
  186. lock: 0,
  187. dialog: [
  188. { c: 1, s: 1, t: "What are you doing here?" },
  189. { c: 0, s: 0, t: "Oh, uh... hello there :D" },
  190. { c: 1, s: 1, t: "What are you looking for?" },
  191. { c: 0, s: 0, t: "I'm here to... pick something up." },
  192. { c: 1, s: 1, t: "Ha! This place is so old you'll never find what you're looking for." },
  193. ],
  194. },
  195. {
  196. lock: 1,
  197. dialog: [
  198. { c: 1, s: 1, t: "That connection is broken." },
  199. { c: 0, s: 0, t: "So I can't go this way?" },
  200. { c: 1, s: 1, t: "Not unless you can fix the connection." },
  201. ],
  202. },
  203. {
  204. lock: 2,
  205. dialog: [
  206. { c: 1, s: 1, t: "How did you get here?" },
  207. { c: 0, s: 0, t: "I made the thingie light up?" },
  208. { c: 1, s: 1, t: "You can't be here! Stop it." },
  209. ],
  210. },
  211. {
  212. lock: 3,
  213. dialog: [
  214. { c: 0, s: 0, t: "Hey, are you there?" },
  215. { c: 1, s: 1, t: "You're still here? What do you want?" },
  216. { c: 0, s: 0, t: "I'm stuck. What do I do now?" },
  217. { c: 1, s: 1, t: "Go away. Go back where you came from. You're not allowed here." },
  218. ],
  219. },
  220. {
  221. lock: 4,
  222. dialog: [
  223. { c: 1, s: 1, t: "This is getting tiresome." },
  224. { c: 0, s: 0, t: "I want to see where this goes! ;)" },
  225. { c: 1, s: 1, t: "Nowhere fast." },
  226. ],
  227. },
  228. {
  229. lock: 5,
  230. dialog: [
  231. { c: 1, s: 1, t: "You are a persistent little square, aren't you?" },
  232. { c: 0, s: 0, t: "I have to get something. Do you know where I can get it from?" },
  233. { c: 1, s: 1, t: "I might have an idea..." },
  234. ],
  235. },
  236. {
  237. lock: 6,
  238. dialog: [],
  239. },
  240. {
  241. lock: 7,
  242. dialog: [
  243. { c: 1, s: 1, t: "What exactly are you looking for?" },
  244. { c: 0, s: 0, t: "It's a kind of request. I'm not really sure ;)" },
  245. { c: 1, s: 1, t: "Try that other branch. But I'm not making any promises!" },
  246. ],
  247. },
  248. {
  249. lock: 8,
  250. dialog: [],
  251. },
  252. {
  253. lock: 9,
  254. dialog: [
  255. { c: 1, s: 1, t: "Fine. Look. You can have this." },
  256. { c: 0, s: 0, t: "Woah! This is what I was looking for!" },
  257. { c: 1, s: 1, t: "It's not what you think. Plus, you still have to get it out of here." },
  258. ],
  259. teleport: { rail: 5, node: 3, x: 2432, y: 1054 },
  260. },
  261. {
  262. lock: 10,
  263. dialog: [
  264. { c: 1, s: 1, t: "Uh oh. Things are shutting down." },
  265. { c: 0, s: 0, t: "Oh no! What should I do?!" },
  266. { c: 1, s: 1, t: "Just get out of here. I might not see you again." },
  267. ],
  268. teleport: { rail: 6, node: 3, x: 2176, y: 1152 },
  269. },
  270. {
  271. lock: 11,
  272. dialog: [
  273. { c: 1, s: 1, t: "I'll miss you, you annoying little square." },
  274. { c: 0, s: 0, t: "..." }],
  275. },
  276. {
  277. lock: 12,
  278. dialog: [
  279. { c: 0, s: 0, t: "I'm at the end! But this connection is much bigger than most..." },
  280. { c: 0, s: 0, t: "" },
  281. { c: 0, s: 0, t: "... server?" },
  282. ],
  283. },
  284. {
  285. lock: 13,
  286. dialog: []
  287. },
  288. {
  289. lock: 14,
  290. dialog: [
  291. { c: 0, s: 0, t: "Goodbye, server!" },
  292. ]
  293. },
  294. ];
  295. this.events = [];
  296. for (let i = 0; i < eventData.length; i++) {
  297. let data = eventData[i];
  298. let railEvent = new RailEvent(this, data.lock);
  299. railEvent.teleport = data.teleport;
  300. for (let j = 0; j < data.dialog.length; j++) {
  301. railEvent.addDialog(data.dialog[j].c, data.dialog[j].s, data.dialog[j].t);
  302. }
  303. this.events.push(railEvent);
  304. }
  305. for (let i = 0; i < 256; i++) {
  306. keys[i] = false;
  307. }
  308. dragCompleted = false;
  309. isDragging = false;
  310. this.updateCamera(0);
  311. this.dialog = new Dialog();
  312. this.script = [];
  313. if (currentNode.enter != null) {
  314. this.events[currentNode.enter].run();
  315. }
  316. this.updateRailUnlocks();
  317. }
  318. updateRailUnlocks() {
  319. for (let i = 0; i < this.rails.length; i++) {
  320. let rail = this.rails[i];
  321. for (let index in rail.unlocks) {
  322. rail[index] = rail.unlocks[index] == 0 || saveData.unlocked.includes(rail.unlocks[index]);
  323. }
  324. for (let j = 0; j < rail.pathUnlocks.length; j++) {
  325. let pathUnlock = rail.pathUnlocks[j];
  326. if (saveData.unlocked.includes(pathUnlock.unlock)) {
  327. if (pathUnlock.hasOwnProperty("up")) {
  328. rail.vertexes[pathUnlock.node].up = pathUnlock.up;
  329. }
  330. if (pathUnlock.hasOwnProperty("down")) {
  331. rail.vertexes[pathUnlock.node].down = pathUnlock.down;
  332. }
  333. }
  334. }
  335. }
  336. }
  337. getRailNode(rail, node) {
  338. return this.rails[rail].getPath()[node];
  339. }
  340. update(delta) {
  341. if (keys[KeyCode.Esc]) {
  342. this.saveScene();
  343. changeState(0);
  344. }
  345. this.updateBackground(delta);
  346. if (this.script.length > 0) {
  347. this.dialog.display(this.script[0].character, this.script[0].side, this.script[0].text);
  348. this.dialog.update(delta);
  349. if (keys[KeyCode.Space] || keys[KeyCode.Enter]) {
  350. this.advanceScript();
  351. keys[KeyCode.Space] = false;
  352. keys[KeyCode.Enter] = false;
  353. }
  354. return;
  355. } else {
  356. this.dialog.hide();
  357. if (saveData.unlocked.includes(14)) {
  358. this.fadeAmount += 0.01;
  359. if (this.fadeAmount >= 1.5) {
  360. changeState(3);
  361. }
  362. }
  363. }
  364. this.updatePlayer(delta);
  365. this.updateRails(delta);
  366. }
  367. draw(context) {
  368. context.save();
  369. context.translate(-this.camera.x + dragDelta.x, -this.camera.y + dragDelta.y);
  370. this.drawBackground(context);
  371. this.drawRail(context);
  372. this.drawPlayer(context);
  373. context.restore();
  374. context.fillStyle = `rgba(0,0,0,${this.fadeAmount})`;
  375. context.beginPath();
  376. context.rect(0, 0, canvas.width, canvas.height);
  377. context.fill();
  378. this.dialog.draw(context);
  379. }
  380. saveScene() {
  381. saveData.player.rail = this.player.rail;
  382. saveData.player.railnode = this.player.railnode;
  383. saveData.camera.x = this.camera.x;
  384. saveData.camera.y = this.camera.y;
  385. saveGame();
  386. this.updateRailUnlocks();
  387. }
  388. updateCamera(delta) {
  389. let playerRatioX = (this.player.x - this.camera.x) / canvas.width;
  390. if (playerRatioX < 0.3) {
  391. this.camera.x = this.player.x - (0.3 * canvas.width);
  392. }
  393. if (playerRatioX > 0.7) {
  394. this.camera.x = this.player.x - (0.7 * canvas.width);
  395. }
  396. let playerRatioY = (this.player.y - this.camera.y) / canvas.height;
  397. if (playerRatioY < 0.3) {
  398. this.camera.y = this.player.y - (0.3 * canvas.height);
  399. }
  400. if (playerRatioY > 0.7) {
  401. this.camera.y = this.player.y - (0.7 * canvas.height);
  402. }
  403. }
  404. updateBackground(delta) {
  405. }
  406. drawBackground(context) {
  407. context.fillStyle = Color.VeryDarkBlue;
  408. for (let y = 0; y < this.area.height / 128; y++) {
  409. for (let x = 0; x < this.area.width / 128; x++) {
  410. let wobble = (Math.sin((x * 45) + new Date().getTime() / 1000));
  411. if (saveData.unlocked.includes(9)) {
  412. wobble = (Math.sin((x * 45) + new Date().getTime() / 200));
  413. context.fillStyle = `rgb(${140 + (20 * wobble)}, 20, 60)`;
  414. }
  415. context.save();
  416. context.translate(x * 128 + (5 * wobble), y * 128 + (10 * wobble));
  417. context.rotate(45 * (Math.PI / 180));
  418. context.beginPath();
  419. context.rect(-(wobble) - 4, -(wobble) - 4, 2 * wobble + 8, 2 * wobble + 8);
  420. context.fill();
  421. context.restore();
  422. }
  423. }
  424. }
  425. updateRails(delta) {
  426. for (let i = 0; i < this.rails.length; i++) {
  427. let rail = this.rails[i];
  428. if (!rail.isVisible) {
  429. continue;
  430. }
  431. rail.update(delta);
  432. }
  433. }
  434. drawRail(context) {
  435. context.lineWidth = 4;
  436. for (let i = 0; i < this.rails.length; i++) {
  437. let rail = this.rails[i];
  438. if (!rail.isVisible) {
  439. continue;
  440. }
  441. rail.draw(context);
  442. }
  443. }
  444. updatePlayer(delta) {
  445. let playerInputReceived = false;
  446. let previousNode = this.getRailNode(this.player.rail, this.player.railnode - 1);
  447. let nextNode = this.getRailNode(this.player.rail, this.player.railnode + 1);
  448. if (keys[KeyCode.W] || keys[KeyCode.Up]) {
  449. this.jumpUpRail();
  450. playerInputReceived = true;
  451. }
  452. if (keys[KeyCode.S] || keys[KeyCode.Down]) {
  453. this.jumpDownRail();
  454. playerInputReceived = true;
  455. }
  456. if (keys[KeyCode.Enter]) {
  457. let currentRail = this.getRailNode(this.player.rail, this.player.railnode);
  458. if (null != currentRail.up) {
  459. this.jumpUpRail();
  460. } else {
  461. if (null != currentRail.down) {
  462. this.jumpDownRail();
  463. }
  464. }
  465. }
  466. if (this.player.x == this.player.target.x && this.player.y == this.player.target.y) {
  467. if (keys[KeyCode.A] || keys[KeyCode.Left]) {
  468. this.moveToPreviousNode();
  469. playerInputReceived = true;
  470. }
  471. if (keys[KeyCode.D] || keys[KeyCode.Right] && nextNode) {
  472. this.moveToNextNode();
  473. playerInputReceived = true;
  474. }
  475. }
  476. if (Math.abs(this.player.x - this.player.target.x) <= this.player.movementSpeed) {
  477. this.player.x = this.player.target.x;
  478. }
  479. if (Math.abs(this.player.y - this.player.target.y) <= this.player.movementSpeed) {
  480. this.player.y = this.player.target.y;
  481. }
  482. if (previousNode && this.player.x == previousNode.x && this.player.y == previousNode.y) {
  483. this.player.railnode--;
  484. this.saveScene();
  485. let currentNode = this.getRailNode(this.player.rail, this.player.railnode);
  486. if (!currentNode.node) {
  487. this.moveToPreviousNode();
  488. }
  489. if (currentNode.enter != null) {
  490. this.events[currentNode.enter].run();
  491. }
  492. }
  493. if (nextNode && this.player.x == nextNode.x && this.player.y == nextNode.y) {
  494. this.player.railnode++;
  495. this.saveScene();
  496. let currentNode = this.getRailNode(this.player.rail, this.player.railnode);
  497. if (!currentNode.node) {
  498. this.moveToNextNode();
  499. }
  500. if (currentNode.enter != null) {
  501. this.events[currentNode.enter].run();
  502. }
  503. }
  504. if (this.player.target.x != this.player.x || this.player.target.y != this.player.y) {
  505. let angle = (Math.atan2(this.player.target.y - this.player.y, this.player.target.x - this.player.x));
  506. this.player.x += this.player.movementSpeed * Math.cos(angle);
  507. this.player.y += this.player.movementSpeed * Math.sin(angle);
  508. playerInputReceived = true;
  509. }
  510. //area boundaries
  511. if (this.player.x > this.area.width - this.player.width / 2) {
  512. this.player.x = this.area.width - this.player.width / 2;
  513. this.player.target.x = this.player.x;
  514. }
  515. if (this.player.x < 0 + this.player.width / 2) {
  516. this.player.x = this.player.width / 2;
  517. this.player.target.x = this.player.x;
  518. }
  519. if (this.player.y > this.area.height - this.player.height / 2) {
  520. this.player.y = this.area.height - this.player.height / 2;
  521. this.player.target.y = this.player.y;
  522. }
  523. if (this.player.y < 0 + this.player.height / 2) {
  524. this.player.y = this.player.height / 2;
  525. this.player.target.y = this.player.y;
  526. }
  527. if (playerInputReceived) {
  528. this.updateCamera(delta);
  529. }
  530. }
  531. moveToPreviousNode() {
  532. let previousNode = this.getRailNode(this.player.rail, this.player.railnode - 1);
  533. if (previousNode) {
  534. this.player.target.x = previousNode.x;
  535. this.player.target.y = previousNode.y;
  536. }
  537. }
  538. moveToNextNode() {
  539. let nextNode = this.getRailNode(this.player.rail, this.player.railnode + 1);
  540. if (nextNode) {
  541. this.player.target.x = nextNode.x;
  542. this.player.target.y = nextNode.y;
  543. }
  544. }
  545. jumpUpRail() {
  546. let currentRailNode = this.getRailNode(this.player.rail, this.player.railnode);
  547. if (null != currentRailNode.up) {
  548. let quest = currentRailNode.up;
  549. puzzleRules = quest;
  550. this.saveScene();
  551. changeState(2);
  552. }
  553. }
  554. jumpDownRail() {
  555. let currentRail = this.getRailNode(this.player.rail, this.player.railnode);
  556. if (null != currentRail.down) {
  557. let quest = currentRail.down;
  558. puzzleRules = quest;
  559. this.saveScene();
  560. changeState(2);
  561. }
  562. }
  563. drawPlayer(context) {
  564. context.save();
  565. context.fillStyle = Color.LightBlue;
  566. context.translate(this.player.x, this.player.y);
  567. context.rotate(this.player.angle);
  568. context.beginPath();
  569. context.rect(-this.player.center.x, -this.player.center.y, this.player.width, this.player.height);
  570. context.fill();
  571. context.restore();
  572. }
  573. onResize() {
  574. this.camera.center.x = canvas.width / 2;
  575. this.camera.center.y = canvas.height / 2;
  576. this.dialog.onResize();
  577. }
  578. onMouseUp(event) {
  579. if (isDragging) {
  580. isDragging = false;
  581. dragCompleted = true;
  582. this.camera.x -= dragDelta.x;
  583. this.camera.y -= dragDelta.y;
  584. }
  585. if (Math.abs(dragDelta.x) > 0 || Math.abs(dragDelta.y) > 0) {
  586. dragDelta.x = 0;
  587. dragDelta.y = 0;
  588. return;
  589. }
  590. if (this.script.length > 0) {
  591. this.advanceScript();
  592. return;
  593. }
  594. if (!(this.player.x == this.player.target.x && this.player.y == this.player.target.y)) {
  595. return;
  596. }
  597. if (Math.abs((this.camera.x + event.clientX) - this.player.x) > Math.abs((this.camera.y + event.clientY) - this.player.y)) {
  598. if (this.camera.x + event.clientX > this.player.x) {
  599. this.moveToNextNode();
  600. } else if (this.camera.x + event.clientX < this.player.x) {
  601. this.moveToPreviousNode();
  602. }
  603. } else {
  604. if (this.camera.y + event.clientY > this.player.y) {
  605. this.jumpDownRail();
  606. } else if (this.camera.y + event.clientY < this.player.y) {
  607. this.jumpUpRail();
  608. }
  609. }
  610. }
  611. advanceScript() {
  612. this.script.shift();
  613. if (this.script.length > 0) {
  614. this.dialog.characters[this.script[0].character].speak();
  615. } else {
  616. this.updateRailUnlocks();
  617. }
  618. }
  619. onMouseMove(event) {
  620. if (isDragging) {
  621. dragDelta.x = event.clientX - startDrag.x;
  622. dragDelta.y = event.clientY - startDrag.y;
  623. }
  624. }
  625. onMouseDown(event) {
  626. if (event.button == 0) {
  627. isDragging = true;
  628. startDrag.x = event.clientX;
  629. startDrag.y = event.clientY;
  630. dragDelta.x = 0;
  631. dragDelta.y = 0;
  632. startDrag.time = new Date().getUTCMilliseconds();
  633. return;
  634. }
  635. dragCompleted = false;
  636. }
  637. onRightClick(event) {
  638. event.preventDefault();
  639. }
  640. onTouchStart(event) {
  641. startDrag.x = parseInt(event.targetTouches[0].clientX);
  642. startDrag.y = parseInt(event.targetTouches[0].clientY);
  643. dragDelta.x = 0;
  644. dragDelta.y = 0;
  645. startDrag.time = new Date().getUTCMilliseconds();
  646. dragCompleted = false;
  647. }
  648. onTouchMove(event) {
  649. isDragging = true;
  650. dragDelta.x = parseInt(event.targetTouches[0].clientX) - startDrag.x;
  651. dragDelta.y = parseInt(event.targetTouches[0].clientY) - startDrag.y;
  652. }
  653. onTouchEnd(event) {
  654. if (event.touches.length != 0) { //I've let go of all points
  655. return;
  656. }
  657. if (isDragging) {
  658. isDragging = false;
  659. dragCompleted = true;
  660. this.camera.x -= dragDelta.x;
  661. this.camera.y -= dragDelta.y;
  662. dragDelta.x = 0;
  663. dragDelta.y = 0;
  664. return;
  665. }
  666. if (!isDragging && !dragCompleted) {
  667. if (this.script.length > 0) {
  668. return;
  669. }
  670. if (!(this.player.x == this.player.target.x && this.player.y == this.player.target.y)) {
  671. return;
  672. }
  673. //is this touch more vertical or horizontal?
  674. let touchX = parseInt(event.changedTouches[0].clientX);
  675. let touchY = parseInt(event.changedTouches[0].clientY);
  676. if (Math.abs((this.camera.x + touchX) - this.player.x) > Math.abs((this.camera.y + touchY) - this.player.y)) {
  677. if (this.camera.x + touchX > this.player.x) {
  678. this.moveToNextNode();
  679. } else if (this.camera.x + touchX < this.player.x) {
  680. this.moveToPreviousNode();
  681. }
  682. } else {
  683. if (this.camera.y + touchY > this.player.y) {
  684. this.jumpDownRail();
  685. } else if (this.camera.y + touchY < this.player.y) {
  686. this.jumpUpRail();
  687. }
  688. }
  689. }
  690. }
  691. }
  692. class Rail {
  693. constructor() {
  694. this.vertexes = [];
  695. this.isVisible = false;
  696. this.isActivated = false;
  697. this.isDamaged = false;
  698. this.activationUnlock = 0;
  699. this.visibleUnlock = 0;
  700. this.unlocks = {
  701. isVisible: 0,
  702. isActivated: 0,
  703. };
  704. this.pathUnlocks = [];
  705. this.damagedFade = { r: 200, g: 20, b: 60 };
  706. }
  707. setPath(vertexes) {
  708. this.vertexes = vertexes;
  709. }
  710. getPath() {
  711. return this.vertexes;
  712. }
  713. setPathUnlocks(unlocks) {
  714. this.pathUnlocks = unlocks;
  715. }
  716. update(delta) {
  717. this.damagedFade.r = 200 + (40 * Math.sin(new Date().getTime() / 100));
  718. }
  719. draw(context) {
  720. context.strokeStyle = Color.DarkBlue;
  721. if (this.isActivated && !this.isDamaged) {
  722. context.strokeStyle = Color.LightBlue;
  723. } else if (this.isActivated && this.isDamaged) {
  724. context.strokeStyle = `rgb(${this.damagedFade.r}, ${this.damagedFade.g}, ${this.damagedFade.b})`;
  725. }
  726. // let activeNodeWobble = 2 + (1 * Math.sin(new Date().getTime() / 100));
  727. let activeNodeWobble = 0;
  728. for (let i = 0; i < this.vertexes.length - 1; i++) {
  729. let vertex = this.vertexes[i];
  730. let nextVertex = this.vertexes[i + 1];
  731. let radius = vertex.node;
  732. let nextRadius = nextVertex.node;
  733. if (vertex.node) {
  734. context.beginPath();
  735. if (this.isActivated) {
  736. radius = vertex.node - activeNodeWobble;
  737. }
  738. context.arc(vertex.x, vertex.y, radius, 0, 2 * Math.PI);
  739. context.stroke();
  740. }
  741. context.beginPath();
  742. let angle = -Math.atan2(vertex.x - nextVertex.x, vertex.y - nextVertex.y) - (90 * Math.PI / 180);
  743. if (vertex.node) {
  744. context.moveTo(vertex.x + radius * Math.cos(angle), vertex.y + radius * Math.sin(angle));
  745. } else {
  746. context.moveTo(vertex.x, vertex.y);
  747. }
  748. if (nextVertex.node) {
  749. if (this.isActivated) {
  750. nextRadius = nextVertex.node - activeNodeWobble;
  751. }
  752. context.lineTo(nextVertex.x - nextRadius * Math.cos(angle), nextVertex.y - nextRadius * Math.sin(angle));
  753. } else {
  754. context.lineTo(nextVertex.x, nextVertex.y);
  755. }
  756. context.stroke();
  757. }
  758. let lastVertex = this.vertexes[this.vertexes.length - 1];
  759. if (lastVertex.node) {
  760. let lastRadius = lastVertex.node;
  761. if (this.isActivated) {
  762. lastRadius = lastVertex.node - activeNodeWobble;
  763. }
  764. context.beginPath();
  765. context.arc(lastVertex.x, lastVertex.y, lastRadius, 0, 2 * Math.PI);
  766. context.stroke();
  767. }
  768. }
  769. }
  770. class RailEvent {
  771. constructor(container, lock) {
  772. this.container = container;
  773. this.lock = lock;
  774. this.script = [];
  775. this.teleport = null;
  776. }
  777. addDialog(character, side, text) {
  778. this.script.push({ character: character, side: side, text: text });
  779. }
  780. run() {
  781. if (this.teleport) {
  782. this.container.player.rail = this.teleport.rail;
  783. this.container.player.railnode = this.teleport.node;
  784. this.container.player.target.x = this.teleport.x;
  785. this.container.player.target.y = this.teleport.y;
  786. }
  787. if (saveData.unlocked.includes(this.lock)) {
  788. return;
  789. }
  790. this.container.script = this.container.script.concat(this.script);
  791. if (this.container.script.length > 0) {
  792. this.container.dialog.characters[this.script[0].character].speak();
  793. }
  794. saveData.unlocked.push(this.lock);
  795. }
  796. }