DigState.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. import { Camera } from "../../libraries/Camera.js"
  2. import { Point } from "../../libraries/spatial/Point.js"
  3. import { Theme } from "../../libraries/components/Theme.js"
  4. import { Easing, TweenManager, Tween } from "../../libraries/Tween.js"
  5. import { Picture } from "../../libraries/CanvasArtist.js"
  6. import { Save } from "../../libraries/Save.js"
  7. import "../../libraries/RoundRectPolyfill.js"
  8. export class DigState {
  9. constructor(view) {
  10. this.stateMachine = view.stateMachine
  11. this.camera = new Camera()
  12. this.camera.scale = new Point(1, 1)
  13. this.time = 0
  14. this.skyColor = [{ r: 14, g: 85, b: 119 }, { r: 6, g: 12, b: 39 }, { r: 6, g: 12, b: 39 }, { r: 6, g: 12, b: 39 }, { r: 36, g: 41, b: 67 }, { r: 228, g: 166, b: 70 }]
  15. this.lightOffset = 0
  16. this.playerPosition = new Point(0, 0)
  17. this.camera.speed = 8
  18. this.touchStart = new Point(0, 0)
  19. this.touchEnd = new Point(0, 0)
  20. this.touchDuration = 0
  21. this.level = []
  22. this.levelSize = { width: 20, height: 10, bones: 12, skulls: 3, time: 24000 }
  23. this.actions = []
  24. this.hasMoved = false
  25. this.tweenManager = new TweenManager()
  26. this.save = new Save()
  27. }
  28. init(scaledCanvas) {
  29. this.canvasBounds = scaledCanvas.bounds
  30. }
  31. enter() {
  32. this.registeredEvents = {}
  33. this.registeredEvents["resize"] = this.onResize.bind(this)
  34. this.registeredEvents["keydown"] = this.onKeyDown.bind(this)
  35. this.registeredEvents["keyup"] = this.onKeyUp.bind(this)
  36. this.registeredEvents["mousedown"] = this.onMouseDown.bind(this)
  37. this.registeredEvents["mousemove"] = this.onMouseMove.bind(this)
  38. this.registeredEvents["mouseup"] = this.onMouseUp.bind(this)
  39. for (let index in this.registeredEvents) {
  40. window.addEventListener(index, this.registeredEvents[index])
  41. }
  42. this.time = 0
  43. this.lightOffset = 0
  44. this.level = this.generateLevel()
  45. this.playerPosition.y = 1
  46. this.playerPosition.x = 0
  47. this.camera.targetPosition.x = 0
  48. this.camera.targetPosition.y = 0
  49. this.camera.position.x = 0
  50. this.camera.position.y = 0
  51. this.level.find(tile => tile[0] == this.playerPosition.x && tile[1] == this.playerPosition.y)[3] = 0
  52. this.completePosition = new Point(0, -1000)
  53. this.completeText = "Daybreak!"
  54. this.isComplete = false
  55. this.digStrength = 1
  56. if (this.save.getKey("shovel-1") == true) {
  57. this.digStrength = 1.5
  58. }
  59. if (this.save.getKey("shovel-2")) {
  60. this.digStrength = 2
  61. }
  62. if (this.save.getKey("shovel-3")) {
  63. this.digStrength = 3
  64. }
  65. this.stones = []
  66. for(let i = 0; i < this.levelSize.width / 5; i++) {
  67. this.stones.push(Math.floor(this.levelSize.width * Math.random()))
  68. }
  69. this.actions = []
  70. }
  71. leave() {
  72. for (let index in this.registeredEvents) {
  73. window.removeEventListener(index, this.registeredEvents[index])
  74. }
  75. }
  76. draw(ctx, scaledCanvas) {
  77. this.canvasBounds = scaledCanvas.bounds
  78. ctx.fillStyle = Theme.Colors.dirtbrown
  79. ctx.fillRect(0, 0, this.canvasBounds.width, this.canvasBounds.height)
  80. this.camera.draw(ctx, scaledCanvas, () => {
  81. ctx.fillStyle = this.computeSkyColor()
  82. ctx.fillRect(
  83. -this.levelSize.width * (Theme.Game.TileSize.width + Theme.Game.TileSize.spacing) * 2,
  84. (-this.canvasBounds.height / 2) - ((Theme.Game.TileSize.height + Theme.Game.TileSize.spacing) / 2),
  85. this.levelSize.width * (Theme.Game.TileSize.width + Theme.Game.TileSize.spacing) * 6,
  86. this.canvasBounds.height / 2)
  87. ctx.fillStyle = Theme.Colors.dirtbrown
  88. ctx.fillRect(-this.canvasBounds.width, 0, this.levelSize.width * (Theme.Game.TileSize.width + Theme.Game.TileSize.spacing) * 4, this.canvasBounds.height / 2)
  89. this.stones.forEach((stone) => {
  90. ctx.save()
  91. ctx.translate(stone * (Theme.Game.TileSize.width + Theme.Game.TileSize.spacing) - (Theme.Game.TileSize.width +Theme.Game.TileSize.spacing) / 2, -1.35 * (Theme.Game.TileSize.height + Theme.Game.TileSize.spacing))
  92. Picture.Tombstone(ctx,{width: Theme.Game.TileSize.width, height: Theme.Game.TileSize.height})
  93. ctx.restore()
  94. })
  95. // ctx.save()
  96. // ctx.translate(this.stone * (Theme.Game.TileSize.width + Theme.Game.TileSize.spacing) + (Theme.Game.TileSize.width +Theme.Game.TileSize.spacing) / 2, -1.35 * (Theme.Game.TileSize.height + Theme.Game.TileSize.spacing))
  97. // Picture.Tombstone(ctx,{width: Theme.Game.TileSize.width, height: Theme.Game.TileSize.height})
  98. // ctx.restore()
  99. ctx.fillStyle = Theme.Colors.grassgreen
  100. ctx.roundRect(-this.levelSize.width * (Theme.Game.TileSize.width + Theme.Game.TileSize.spacing) * 2,
  101. - ((Theme.Game.TileSize.height + Theme.Game.TileSize.spacing) / 2),
  102. this.levelSize.width * (Theme.Game.TileSize.width + Theme.Game.TileSize.spacing) * 6,
  103. 10, 8)
  104. ctx.fill()
  105. for (let i = 0; i < this.level.length; i++) {
  106. let data = this.level[i]
  107. this.drawTile(ctx, data)
  108. }
  109. this.drawPlayer(ctx)
  110. })
  111. ctx.strokeStyle = Theme.Colors.ivory
  112. ctx.lineWidth = 2;
  113. ctx.beginPath()
  114. ctx.rect(2, 2, this.canvasBounds.width - 4, 8)
  115. ctx.stroke()
  116. ctx.fillStyle = Theme.Colors.ivory
  117. ctx.beginPath()
  118. ctx.rect(0, 2, (this.canvasBounds.width - 4) * (this.time / (this.levelSize.time - (this.levelSize.time / this.skyColor.length))), 8)
  119. ctx.fill()
  120. let fontScale = this.canvasBounds.width / 500
  121. let inventorySpacing = 0
  122. for (let i = 0; i < this.getNumberOfGatheredSkulls(); i++) {
  123. ctx.save()
  124. ctx.scale(0.4, 0.4)
  125. ctx.translate(16 + (i * (Theme.Game.TileSize.width + Theme.Game.TileSize.spacing) + (Theme.Game.TileSize.width / 2)), 32 + (Theme.Game.TileSize.height / 2))
  126. Picture.Skull(ctx, { width: Theme.Game.TileSize.width, height: Theme.Game.TileSize.height })
  127. ctx.restore()
  128. inventorySpacing++
  129. }
  130. for (let i = 0; i < this.getNumberOfGatheredBones(); i++) {
  131. ctx.save()
  132. ctx.scale(0.4, 0.4)
  133. ctx.translate(16 + ((i + inventorySpacing) * (Theme.Game.TileSize.width + Theme.Game.TileSize.spacing)), 32)
  134. Picture.Bone(ctx, { width: Theme.Game.TileSize.width, height: Theme.Game.TileSize.height })
  135. ctx.restore()
  136. }
  137. ctx.font = `${Math.min(Math.floor(48 * fontScale), 48)}px ${Theme.Fonts.Header}`
  138. ctx.textAlign = "center"
  139. let metrics = ctx.measureText(this.completeText)
  140. ctx.fillStyle = Theme.Colors.umber
  141. ctx.save()
  142. ctx.translate(this.canvasBounds.width / 2, this.canvasBounds.height / 2)
  143. ctx.beginPath()
  144. ctx.roundRect(this.completePosition.x - (metrics.width + 64) / 2, this.completePosition.y - Math.min(Math.floor(48 * fontScale), 48) - 16, metrics.width + 64, Math.min(Math.floor(64 * fontScale), 64) + 32, 8)
  145. ctx.fill()
  146. ctx.fillStyle = Theme.Colors.ivory
  147. ctx.fillText(this.completeText, this.completePosition.x, this.completePosition.y)
  148. ctx.restore()
  149. }
  150. drawPlayer(ctx) {
  151. ctx.fillStyle = Theme.Colors.ivory
  152. ctx.save()
  153. ctx.translate(this.playerPosition.x * (Theme.Game.TileSize.width + Theme.Game.TileSize.spacing), this.playerPosition.y * (Theme.Game.TileSize.height + Theme.Game.TileSize.spacing))
  154. Picture.DigPlayer(ctx, {})
  155. ctx.restore()
  156. }
  157. computeSkyColor() {
  158. let ratio = (this.time / (this.levelSize.time / this.skyColor.length)) - this.lightOffset
  159. let r = ratio * (this.skyColor[this.lightOffset + 1].r - this.skyColor[this.lightOffset + 0].r) + this.skyColor[this.lightOffset + 0].r
  160. let g = ratio * (this.skyColor[this.lightOffset + 1].g - this.skyColor[this.lightOffset + 0].g) + this.skyColor[this.lightOffset + 0].g
  161. let b = ratio * (this.skyColor[this.lightOffset + 1].b - this.skyColor[this.lightOffset + 0].b) + this.skyColor[this.lightOffset + 0].b
  162. return `rgb(${r}, ${g}, ${b})`
  163. }
  164. drawTile(ctx, tileData) {
  165. let width = Theme.Game.TileSize.width;
  166. let height = Theme.Game.TileSize.height;
  167. let spacing = Theme.Game.TileSize.spacing
  168. let x = tileData[0]
  169. let y = tileData[1]
  170. let type = tileData[2]
  171. let health = tileData[3]
  172. let maxHealth = tileData[4]
  173. ctx.save()
  174. ctx.translate(x * width - (width / 2) + (x * spacing), y * height - (height / 2) + (y * spacing))
  175. if (health == 0) {
  176. ctx.fillStyle = "black"
  177. ctx.roundRect(-2, -2, width + 2, height + 2, 10)
  178. ctx.fill()
  179. ctx.restore()
  180. return
  181. }
  182. switch (type) {
  183. case "dirt":
  184. Picture.DirtBlock(ctx, { width, height })
  185. break;
  186. case "grass":
  187. Picture.GrassBlock(ctx, { width, height })
  188. break
  189. case "dirt-bones":
  190. Picture.DirtBoneBlock(ctx, { width, height })
  191. break
  192. case "dirt-skull":
  193. Picture.DirtSkullBlock(ctx, { width, height })
  194. break
  195. case "dirt-diamond":
  196. Picture.DirtDiamondBlock(ctx, { width, height })
  197. break
  198. }
  199. if (health < maxHealth) {
  200. ctx.fillStyle = Theme.Colors.ivory
  201. ctx.strokeStyle = Theme.Colors.ivory
  202. ctx.lineWidth = 2
  203. ctx.beginPath()
  204. ctx.rect(4, height / 8, width - 8, 6)
  205. ctx.stroke()
  206. ctx.beginPath()
  207. ctx.rect(4, height / 8, width * (health / maxHealth) - 8, 6)
  208. ctx.fill()
  209. }
  210. ctx.restore()
  211. }
  212. update(delta) {
  213. if (!this.isComplete) {
  214. if (this.time >= this.levelSize.time - (this.levelSize.time / this.skyColor.length)) {
  215. this.isComplete = true
  216. this.completeText = "Daybreak!"
  217. this.tweenManager.add(new Tween(this.completePosition, { y: 0 }, 2000, Easing.Elastic.EaseOut, () => {
  218. this.sceneComplete()
  219. }))
  220. } else {
  221. this.time += delta
  222. this.lightOffset = Math.min(this.skyColor.length - 2, Math.floor(this.time / (this.levelSize.time / this.skyColor.length)))
  223. }
  224. if (this.actions.length > 0) {
  225. let nextAction = this.actions.shift()
  226. switch (nextAction) {
  227. case "up":
  228. this.digOrMoveY(-1)
  229. break;
  230. case "down":
  231. this.digOrMoveY(1)
  232. break
  233. case "left":
  234. this.digOrMoveX(-1)
  235. break
  236. case "right":
  237. this.digOrMoveX(1)
  238. break;
  239. }
  240. }
  241. if (this.levelSize.diamonds > 0 && this.getNumberOfGatheredDiamonds() == this.levelSize.diamonds) {
  242. this.isComplete = true
  243. this.completeText = "Success!"
  244. this.tweenManager.add(new Tween(this.completePosition, { y: 0 }, 2000, Easing.Elastic.EaseOut, () => {
  245. this.sceneComplete()
  246. }))
  247. }
  248. if (this.getNumberOfGatheredBones() == this.levelSize.bones && this.getNumberOfGatheredSkulls() == this.levelSize.skulls) {
  249. this.isComplete = true
  250. this.completeText = "Looted!"
  251. this.tweenManager.add(new Tween(this.completePosition, { y: 0 }, 2000, Easing.Elastic.EaseOut, () => {
  252. this.sceneComplete()
  253. }))
  254. }
  255. }
  256. this.camera.update(delta)
  257. this.tweenManager.update()
  258. }
  259. sceneComplete() {
  260. this.save.incrementKey("bones", this.getNumberOfGatheredBones())
  261. this.save.incrementKey("skulls", this.getNumberOfGatheredSkulls())
  262. this.save.incrementKey("diamonds", this.getNumberOfGatheredDiamonds())
  263. if (this.save.getKey("diamonds") > 0) {
  264. this.stateMachine.transitionTo("credits")
  265. } else {
  266. this.stateMachine.transitionTo("shop")
  267. }
  268. }
  269. getNumberOfGatheredBones() {
  270. return this.level.filter(tile => tile[2] == "dirt-bones" && tile[3] == 0).length
  271. }
  272. getNumberOfGatheredSkulls() {
  273. return this.level.filter(tile => tile[2] == "dirt-skull" && tile[3] == 0).length
  274. }
  275. getNumberOfGatheredDiamonds() {
  276. return this.level.filter(tile => tile[2] == "dirt-diamond" && tile[3] == 0).length
  277. }
  278. moveX(direction) {
  279. let playerSpeed = Theme.Game.TileSize.width + Theme.Game.TileSize.spacing
  280. this.playerPosition.x += direction
  281. if (this.playerPosition.x < 0) {
  282. this.playerPosition.x = 0
  283. }
  284. if (this.playerPosition.x >= (this.levelSize.width - 1)) {
  285. this.playerPosition.x = (this.levelSize.width - 1)
  286. }
  287. if (this.playerPosition.x * playerSpeed > this.camera.targetPosition.x + this.canvasBounds.width / 4) {
  288. this.camera.targetPosition.x += playerSpeed * direction
  289. }
  290. if (this.playerPosition.x * playerSpeed < this.camera.targetPosition.x + -this.canvasBounds.width / 4) {
  291. this.camera.targetPosition.x += playerSpeed * direction
  292. }
  293. }
  294. digOrMoveX(direction) {
  295. let nextTilePosition = { x: this.playerPosition.x + direction, y: this.playerPosition.y }
  296. let nextTile = this.level.find((tile) => tile[0] == nextTilePosition.x && tile[1] == nextTilePosition.y)
  297. if (!nextTile) {
  298. return
  299. }
  300. if (nextTile[3] > 0) {
  301. nextTile[3] = Math.max(0, nextTile[3] - this.digStrength)
  302. if(nextTile[3] == 0) {
  303. this.playSound(this.digSound2)
  304. if(!["dirt", "grass"].includes(nextTile[2])) {
  305. this.playSound(this.gatherSound)
  306. }
  307. }
  308. }
  309. if (nextTile[3] <= 0) {
  310. this.moveX(direction)
  311. }
  312. }
  313. moveY(direction) {
  314. let playerSpeed = Theme.Game.TileSize.height + Theme.Game.TileSize.spacing
  315. this.playerPosition.y += direction
  316. if (this.playerPosition.y < 0) {
  317. this.playerPosition.y = 0
  318. }
  319. if (this.playerPosition.y >= (this.levelSize.height - 1)) {
  320. this.playerPosition.y = (this.levelSize.height - 1)
  321. }
  322. if (this.playerPosition.y * playerSpeed > this.camera.targetPosition.y + this.canvasBounds.height / 4) {
  323. this.camera.targetPosition.y += playerSpeed * direction
  324. }
  325. if (this.playerPosition.y * playerSpeed < this.camera.targetPosition.y + -this.canvasBounds.height / 4) {
  326. this.camera.targetPosition.y += playerSpeed * direction
  327. }
  328. }
  329. digOrMoveY(direction) {
  330. let nextTilePosition = { x: this.playerPosition.x, y: this.playerPosition.y + direction }
  331. let nextTile = this.level.find((tile) => tile[0] == nextTilePosition.x && tile[1] == nextTilePosition.y)
  332. if (!nextTile) {
  333. return
  334. }
  335. if (nextTile[3] > 0) {
  336. nextTile[3] = Math.max(0, nextTile[3] - this.digStrength)
  337. if(nextTile[3] == 0) {
  338. this.playSound(this.digSound2)
  339. if(!["dirt", "grass"].includes(nextTile[2])) {
  340. this.playSound(this.gatherSound)
  341. }
  342. }
  343. }
  344. if (nextTile[3] <= 0) {
  345. this.moveY(direction)
  346. }
  347. }
  348. generateLevel() {
  349. this.levelSize = { width: 10, height: 6, bones: 8, skulls: 1, diamonds: 0, time: 24000 }
  350. if (this.save.getKey("graveyard-1")) {
  351. this.levelSize = { width: 20, height: 10, bones: 12, skulls: 3, diamonds: 0, time: 24000 }
  352. }
  353. if (this.save.getKey("graveyard-2")) {
  354. this.levelSize = { width: 40, height: 15, bones: 24, skulls: 8, diamonds: 0, time: 24000 }
  355. }
  356. if (this.save.getKey("graveyard-3")) {
  357. this.levelSize = { width: 20, height: 50, bones: 36, skulls: 16, diamonds: 1, time: 24000 }
  358. }
  359. let levelArray = []
  360. for (let y = 0; y < this.levelSize.height; y++) {
  361. for (let x = 0; x < this.levelSize.width; x++) {
  362. if (y == 0) {
  363. levelArray.push([x, y, "grass", 4, 4])
  364. } else {
  365. levelArray.push([x, y, "dirt", 3, 3])
  366. }
  367. }
  368. }
  369. if(this.save.getKey("bone-3")) {
  370. this.levelSize.bones = Math.ceil(2 * this.levelSize.bones)
  371. this.levelSize.skulls = Math.ceil(2 * this.levelSize.skulls)
  372. } else if(this.save.getKey("bone-2")) {
  373. this.levelSize.bones = Math.ceil(1.5 * this.levelSize.bones)
  374. this.levelSize.skulls = Math.ceil(1.5 * this.levelSize.skulls)
  375. } else if(this.save.getKey("bone-1")) {
  376. this.levelSize.bones = Math.ceil(1.2 * this.levelSize.bones)
  377. this.levelSize.skulls = Math.ceil(1.2 * this.levelSize.skulls)
  378. }
  379. for (let i = 0; i < this.levelSize.bones; i++) {
  380. let randomTile = this.getRandomIndex(levelArray, (tile) => tile[1] != 0 && tile[2] != "dirt-bones")
  381. randomTile[2] = "dirt-bones"
  382. randomTile[3] = 4
  383. randomTile[4] = 4
  384. }
  385. for (let i = 0; i < this.levelSize.skulls; i++) {
  386. let randomTile = this.getRandomIndex(levelArray, (tile) => tile[1] != 0 && tile[2] != "dirt-bones" && tile[2] != "dirt-skull")
  387. randomTile[2] = "dirt-skull"
  388. randomTile[3] = 8
  389. randomTile[4] = 8
  390. }
  391. for (let i = 0; i < this.levelSize.diamonds; i++) {
  392. let randomTile = this.getRandomIndex(levelArray, (tile) => tile[1] != 0 && tile[2] != "dirt-bones" && tile[2] != "dirt-skull" && tile[2] != "dirt-diamond")
  393. randomTile[2] = "dirt-diamond"
  394. randomTile[3] = 16
  395. randomTile[4] = 16
  396. }
  397. return levelArray
  398. }
  399. getRandomIndex(array, condition) {
  400. let item = array[Math.floor(array.length * Math.random())]
  401. let timeout = 10
  402. while (!condition(item)) {
  403. timeout--
  404. if (timeout <= 0) {
  405. throw new Error("unable to get item that meets conditions")
  406. }
  407. item = array[Math.floor(array.length * Math.random())]
  408. }
  409. return item
  410. }
  411. onResize() {
  412. this.camera.targetPosition.x = this.playerPosition.x * (Theme.Game.TileSize.width + Theme.Game.TileSize.spacing)
  413. this.camera.targetPosition.y = this.playerPosition.y * (Theme.Game.TileSize.height + Theme.Game.TileSize.spacing)
  414. }
  415. onKeyDown(event) {
  416. if (this.hasMoved) {
  417. return
  418. }
  419. let keys = []
  420. keys[event.code] = true
  421. if ((keys["KeyW"] || keys["ArrowUp"] || keys["KeyZ"])) {
  422. this.actions.push("up")
  423. this.hasMoved = true
  424. }
  425. if (keys["KeyS"] || keys["ArrowDown"]) {
  426. this.actions.push("down")
  427. this.hasMoved = true
  428. }
  429. if (keys["KeyA"] || keys["ArrowLeft"] || keys["KeyQ"]) {
  430. this.actions.push("left")
  431. this.hasMoved = true
  432. }
  433. if (keys["KeyD"] || keys["ArrowRight"]) {
  434. this.actions.push("right")
  435. this.hasMoved = true
  436. }
  437. // if(keys["Digit1"]) {
  438. // this.stateMachine.transitionTo("shop")
  439. // }
  440. // if(keys["Digit2"]) {
  441. // this.stateMachine.transitionTo("upgrade")
  442. // }
  443. // if(keys["Digit3"]) {
  444. // this.stateMachine.transitionTo("credits")
  445. // }
  446. }
  447. onKeyUp(event) {
  448. this.hasMoved = false
  449. }
  450. onMouseDown(event) {
  451. if (this.hasMoved) {
  452. return
  453. }
  454. this.hasMoved = true
  455. let delta = this.camera.screenToWorld(new Point(event.clientX, event.clientY)).difference(
  456. new Point()
  457. .clone(this.playerPosition)
  458. .scale(Theme.Game.TileSize.width + Theme.Game.TileSize.spacing, Theme.Game.TileSize.height + Theme.Game.TileSize.spacing)
  459. )
  460. if (Math.abs(delta.x) > Math.abs(delta.y)) {
  461. let direction = Math.sign(delta.x)
  462. if (direction > 0) {
  463. this.actions.push("right")
  464. } else {
  465. this.actions.push("left")
  466. }
  467. } else {
  468. let direction = Math.sign(delta.y)
  469. if (direction > 0) {
  470. this.actions.push("down")
  471. } else {
  472. this.actions.push("up")
  473. }
  474. }
  475. }
  476. onMouseMove(event) {
  477. }
  478. onMouseUp(event) {
  479. this.hasMoved = false
  480. }
  481. digSound1(i, t) {
  482. var n = 5e3;
  483. if (i > n) return null;
  484. return ((Math.pow(i + Math.sin(i * 0.01) * 1000, 0.25) & 200) ? 1 : -1) * Math.pow(t(i, n), 1);
  485. }
  486. digSound2(i, t) {
  487. var n = 5e3;
  488. if (i > n) return null;
  489. return (Math.random() * 2 - 1) * t(i, n);
  490. }
  491. gatherSound(i, t) {
  492. var n=1.6e4;
  493. var c=n/7;
  494. if (i > n) return null;
  495. var q=Math.pow(t(i,n),2.1);
  496. return (i<c ? ((i+Math.sin(-i/900)*10)&16) : i&13) ?q:-q;
  497. }
  498. playSound(soundFunction) {
  499. // Sound player
  500. let t = (i, n) => (n - i) / n,
  501. A = new AudioContext(),
  502. m = A.createBuffer(1, 96e3, 48e3),
  503. b = m.getChannelData(0)
  504. for (let i = 96e3; i--;)b[i] = soundFunction(i, t)
  505. let s = A.createBufferSource()
  506. s.buffer = m
  507. s.connect(A.destination)
  508. s.start()
  509. }
  510. }