|
| 1 | +import * as ex from 'excalibur'; |
| 2 | +import Engine from './engine.js'; |
| 3 | + |
| 4 | +export class Scene extends ex.Scene { |
| 5 | + onActivate(ctx) { |
| 6 | + const engine = ctx.engine; |
| 7 | + this.engine = ctx.data.engine; |
| 8 | + |
| 9 | + this.clear(); |
| 10 | + |
| 11 | + // Particle creation |
| 12 | + const particles = new Array(this.engine.count); |
| 13 | + const rnd = [1, -1]; |
| 14 | + const spriteImage = new ex.ImageSource('sprite.png'); |
| 15 | + spriteImage.load(); |
| 16 | + for (let i = 0; i < this.engine.count; i++) { |
| 17 | + const size = 10 + Math.random() * 80; |
| 18 | + const x = Math.random() * this.engine.width; |
| 19 | + const y = Math.random() * (this.engine.height - size); |
| 20 | + const [dx, dy] = [ |
| 21 | + 3 * Math.random() * rnd[Math.floor(Math.random() * 2)], |
| 22 | + 3 * Math.random() * rnd[Math.floor(Math.random() * 2)], |
| 23 | + ]; |
| 24 | + |
| 25 | + const particle = new ex.Actor({ |
| 26 | + x: x, |
| 27 | + y: y, |
| 28 | + radius: size, |
| 29 | + }); |
| 30 | + |
| 31 | + if (this.engine.type === 'sprite') { |
| 32 | + const sprite = ex.Sprite.from(spriteImage); |
| 33 | + particle.graphics.use(sprite); |
| 34 | + } else { |
| 35 | + const circle = new ex.Circle({ |
| 36 | + x: x, |
| 37 | + y: y, |
| 38 | + radius: size, |
| 39 | + color: |
| 40 | + this.engine.type === 'stroke' |
| 41 | + ? ex.Color.Transparent |
| 42 | + : ex.Color.fromRGB(255, 255, 255), |
| 43 | + strokeColor: |
| 44 | + this.engine.type === 'stroke' |
| 45 | + ? ex.Color.fromRGB(255, 255, 255) |
| 46 | + : ex.Color.fromRGB(0, 0, 0), |
| 47 | + strokeWidth: this.engine.type === 'stroke' ? 1 : undefined, |
| 48 | + }); |
| 49 | + particle.graphics.use(circle); |
| 50 | + } |
| 51 | + this.add(particle); |
| 52 | + |
| 53 | + particles[i] = { x, y, size: size, dx, dy, el: particle }; |
| 54 | + |
| 55 | + particle.on("postupdate", () => { |
| 56 | + const r = particles[i]; |
| 57 | + r.x -= r.dx; |
| 58 | + r.y -= r.dy; |
| 59 | + if (r.x + r.size < 0) r.dx *= -1; |
| 60 | + else if (r.y + r.size < 0) r.dy *= -1; |
| 61 | + if (r.x > engine.screen.drawWidth) r.dx *= -1; |
| 62 | + else if (r.y > engine.screen.drawHeight) r.dy *= -1; |
| 63 | + r.el.pos.x = r.x; |
| 64 | + r.el.pos.y = r.y; |
| 65 | + }); |
| 66 | + } |
| 67 | + } |
| 68 | + onPostUpdate() { |
| 69 | + this.engine.fpsmeter.tick(); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +class ExcaliburEngine extends Engine { |
| 74 | + init() { |
| 75 | + super.init(); |
| 76 | + |
| 77 | + window.cancelAnimationFrame(this.request); |
| 78 | + if (this.game) { |
| 79 | + this.game.dispose(); |
| 80 | + this.canvas = document.createElement("canvas"); |
| 81 | + this.canvas.id = "canvas"; |
| 82 | + this.canvas.className = "canvas"; |
| 83 | + this.canvas.width = this.width; |
| 84 | + this.canvas.height = this.height; |
| 85 | + document.querySelector("main").appendChild(this.canvas); |
| 86 | + } |
| 87 | + |
| 88 | + const game = new ex.Engine({ |
| 89 | + width: this.width, |
| 90 | + height: this.height, |
| 91 | + canvasElement: this.canvas, |
| 92 | + backgroundColor: ex.Color.fromRGB(26, 26, 26), |
| 93 | + scenes: { scene: Scene } |
| 94 | + }); |
| 95 | + this.game = game; |
| 96 | + } |
| 97 | + render() { |
| 98 | + this.game.start().then(() => { |
| 99 | + this.game.goToScene('scene', { sceneActivationData: { engine: this } }); |
| 100 | + }); |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +document.addEventListener('DOMContentLoaded', () => { |
| 105 | + const engine = new ExcaliburEngine(); |
| 106 | + engine.render(); |
| 107 | +}); |
0 commit comments