HR-CMGT / PRG04-ExcaliburJS-Arcade-voorbeeld

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

๐Ÿ’€ OLD - Arcade Stick demo 2023

NOOT: oud demo project, niet meer relevant omdat excalibur gamepad support heeft.

๐Ÿ•น ๐ŸŽฎ Joystick and Gamepad

The arcade class will detect if you use a gamepad ๐ŸŽฎ or the arcade cabinet joystick ๐Ÿ•น

Add library to package.json

Run next line in your project terminal:

npm install git@github.com:HR-CMGT/arcade-game.git

Usage in game class

import { Arcade } from "arcade-game"

export class Game {

   #arcade;
   #joystickListener;

   constructor() {
       //TODO: add reference to startGame()
   }

   startGame() {
       this.#arcade = new Arcade(this, false, true);

       this.#joystickListener = (e) => this.#joyStickFound(e)
       document.addEventListener("joystickcreated",  this.#joystickListener)
   }

   #joyStickFound(e) {
       let joystick = this.#arcade.Joysticks[e.detail]
       
       // debug, this shows you the names of the buttons when they are pressed
       for (const buttonEvent of joystick.ButtonEvents) {
           document.addEventListener(buttonEvent, () => console.log(buttonEvent))
       }

       this.update();
   }

   // excalibur update function
   onPreUpdate() {
       for (let joystick of this.#arcade.Joysticks) {
           joystick.update()
       }
   }

   // voorbeeld zonder excalibur
   /*
   update() {
       for (let joystick of this.#arcade.Joysticks) {
           joystick.update()
       }
       requestAnimationFrame(() => this.update());
   }
   */

   disconnect() {
       document.removeEventListener("joystickcreated", this.#joystickListener)
   }
}

new Game()

About


Languages

Language:JavaScript 90.6%Language:HTML 7.5%Language:CSS 1.9%