|
| 1 | +import { Point, type Ticker } from "pixi.js"; |
| 2 | +import { WIDTH } from "../constants"; |
| 3 | +import GameNode from "./GameNode"; |
| 4 | +import Player from "./Player"; |
| 5 | +import { campaign } from "../levels"; |
| 6 | +import Background from "./Background"; |
| 7 | +import { inputs } from "../inputs"; |
| 8 | +import Countdown from "./Countdown"; |
| 9 | +import { type IMediaInstance } from "@pixi/sound"; |
| 10 | +import { playSound } from "../audio"; |
| 11 | +import { slideIn } from "../animations"; |
| 12 | +import { UP } from "../points"; |
| 13 | +import { choice } from "../random"; |
| 14 | + |
| 15 | +type Mode = "game" | "score"; |
| 16 | + |
| 17 | +export default class IdleMode extends GameNode { |
| 18 | + player: Player; |
| 19 | + onFinish?: () => void; |
| 20 | + background: Background; |
| 21 | + mode: Mode = "game"; |
| 22 | + music?: IMediaInstance; |
| 23 | + |
| 24 | + constructor(background: Background, startLevel: number) { |
| 25 | + super(); |
| 26 | + this.background = background; |
| 27 | + this.player = new Player({ |
| 28 | + inputMap: inputs.player1, |
| 29 | + levels: campaign, |
| 30 | + startLevel, |
| 31 | + }); |
| 32 | + this.player.onLevelUp = (level) => { |
| 33 | + this.background.setGenerator(level.randomQubit); |
| 34 | + }; |
| 35 | + this.player.onTopOut = () => { |
| 36 | + this.music?.stop(); |
| 37 | + }; |
| 38 | + this.player.onGameOver = () => { |
| 39 | + this.view.removeChild(this.player.view); |
| 40 | + this.player.destroy(); |
| 41 | + this.endIdle(); |
| 42 | + }; |
| 43 | + } |
| 44 | + |
| 45 | + async start() { |
| 46 | + // this.view.addChild( |
| 47 | + // new HTMLText({ |
| 48 | + // text: "Press any button", |
| 49 | + // }) |
| 50 | + // ); |
| 51 | + this.view.addChild(this.player.view); |
| 52 | + document.addEventListener("keydown", this.handleKeyDown); |
| 53 | + slideIn( |
| 54 | + this.player.view, |
| 55 | + new Point(WIDTH / 2 - this.player.view.width / 2, 0), |
| 56 | + UP |
| 57 | + ); |
| 58 | + |
| 59 | + const countdown = new Countdown(); |
| 60 | + this.view.addChild(countdown.view); |
| 61 | + await countdown.start(); |
| 62 | + this.player.start(); |
| 63 | + this.view.removeChild(countdown.view); |
| 64 | + this.music = await playSound("bgMusic", { loop: true }); |
| 65 | + } |
| 66 | + |
| 67 | + tick = (time: Ticker) => { |
| 68 | + if (this.mode === "game") { |
| 69 | + this.player.tick(time); |
| 70 | + // Very simple, just press a random button |
| 71 | + if (Math.random() < 0.02) |
| 72 | + this.player.onPress(choice(Object.values(inputs.player1))); |
| 73 | + } |
| 74 | + }; |
| 75 | + |
| 76 | + endIdle() { |
| 77 | + this.music?.stop(); |
| 78 | + document.removeEventListener("keydown", this.handleKeyDown); |
| 79 | + this.onFinish?.(); |
| 80 | + } |
| 81 | + |
| 82 | + handleKeyDown = () => { |
| 83 | + this.endIdle(); |
| 84 | + }; |
| 85 | +} |
0 commit comments