|
| 1 | +import Engine from './engine.js'; |
| 2 | +import * as little from 'littlejsengine'; |
| 3 | + |
| 4 | +class LittleJSEngine extends Engine { |
| 5 | + init() { |
| 6 | + super.init(); |
| 7 | + |
| 8 | + // Clear the canvas |
| 9 | + this.rootElement = document.getElementById('root'); |
| 10 | + |
| 11 | + // Start the LittleJS engine |
| 12 | + if (!this.initialized) { |
| 13 | + little.engineInit( |
| 14 | + this.gameInit.bind(this), |
| 15 | + this.gameUpdate.bind(this), |
| 16 | + this.gameUpdatePost.bind(this), |
| 17 | + this.gameRender.bind(this), |
| 18 | + this.gameRenderPost.bind(this), |
| 19 | + this.type === 'sprite' ? ['sprite.png'] : [], |
| 20 | + this.rootElement |
| 21 | + ); |
| 22 | + this.initialized = true; |
| 23 | + this.rootElement.style.width = this.width + 'px'; |
| 24 | + this.rootElement.style.height = this.height + 'px'; |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + gameInit() { |
| 29 | + little.setShowWatermark(false); |
| 30 | + little.setCanvasFixedSize(little.vec2(this.width, this.height)); |
| 31 | + little.setCameraScale(1); |
| 32 | + little.setCameraPos(little.vec2(this.width / 2, this.height / 2)); |
| 33 | + little.mainCanvas.style.background = '#1a1a1a'; |
| 34 | + |
| 35 | + document |
| 36 | + .querySelectorAll('canvas:not(:first-of-type)') |
| 37 | + .forEach((canvas) => { |
| 38 | + canvas.style.background = 'transparent'; |
| 39 | + }); |
| 40 | + |
| 41 | + // Particle creation |
| 42 | + const particles = new Array(this.count); |
| 43 | + const rnd = [1, -1]; |
| 44 | + for (let i = 0; i < this.count; i++) { |
| 45 | + const size = 10 + Math.random() * 80; |
| 46 | + const x = Math.random() * this.width; |
| 47 | + const y = Math.random() * (this.height - size); |
| 48 | + const pos = little.vec2(x, y); |
| 49 | + const [dx, dy] = [ |
| 50 | + 3 * Math.random() * rnd[Math.floor(Math.random() * 2)], |
| 51 | + 3 * Math.random() * rnd[Math.floor(Math.random() * 2)], |
| 52 | + ]; |
| 53 | + particles[i] = { |
| 54 | + pos, |
| 55 | + size: size, |
| 56 | + dx, |
| 57 | + dy, |
| 58 | + tile: this.type === 'sprite' ? little.tile(0, 64, 0) : null, |
| 59 | + }; |
| 60 | + } |
| 61 | + this.particles = particles; |
| 62 | + } |
| 63 | + gameUpdate() { |
| 64 | + // Particle animation |
| 65 | + const particles = this.particles; |
| 66 | + for (let i = 0; i < this.count; i++) { |
| 67 | + const r = particles[i]; |
| 68 | + r.pos.x -= r.dx; |
| 69 | + r.pos.y -= r.dy; |
| 70 | + if (r.pos.x + r.size < 0) r.dx *= -1; |
| 71 | + else if (r.pos.y + r.size < 0) r.dy *= -1; |
| 72 | + if (r.pos.x > this.width) r.dx *= -1; |
| 73 | + else if (r.pos.y > this.height) r.dy *= -1; |
| 74 | + } |
| 75 | + } |
| 76 | + gameUpdatePost() {} |
| 77 | + gameRender() { |
| 78 | + this.fpsmeter.tick(); |
| 79 | + for (let i = 0; i < this.particles.length; i++) { |
| 80 | + const particle = this.particles[i]; |
| 81 | + if (this.type === 'sprite') { |
| 82 | + little.drawTile(particle.pos, particle.tile.size, particle.tile); |
| 83 | + } else { |
| 84 | + if (this.type === 'fill') { |
| 85 | + little.drawCircle( |
| 86 | + particle.pos, |
| 87 | + particle.size, |
| 88 | + little.WHITE, |
| 89 | + 1, |
| 90 | + little.BLACK |
| 91 | + ); |
| 92 | + } |
| 93 | + if (this.type != 'sprite') |
| 94 | + little.drawCircle( |
| 95 | + particle.pos, |
| 96 | + particle.size, |
| 97 | + little.rgb(0, 0, 0, 0), |
| 98 | + 1, |
| 99 | + little.WHITE |
| 100 | + ); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + gameRenderPost() {} |
| 105 | + |
| 106 | + render() { |
| 107 | + super.render(); |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +document.addEventListener('DOMContentLoaded', () => { |
| 112 | + const engine = new LittleJSEngine(); |
| 113 | + engine.render(); |
| 114 | +}); |
0 commit comments