Back to home page

EIC code displayed by LXR

 
 

    


Warning, /firebird/firebird-ng/src/app/services/game-controller.service.ts is written in an unsupported language. File is not indexed.

0001 import { Injectable } from '@angular/core';
0002 import * as THREE from "three";
0003 import {BehaviorSubject, Observable, Subject} from "rxjs";
0004 
0005 
0006 export enum GamepadButtonIndexes {
0007   ButtonA = 0,
0008   ButtonB = 1,
0009   ButtonX = 2,
0010   ButtonY = 3,
0011   ButtonLB = 4,
0012   ButtonRB = 5,
0013   ButtonLT = 6,
0014   ButtonRT = 7,
0015   Select = 8,
0016   Start = 9,
0017 }
0018 
0019 export class GamepadObservableButton {
0020   private onPressSubject = new Subject<boolean>();
0021   public onPress = this.onPressSubject.asObservable();
0022   public state: GamepadButton = {pressed: false, touched: false, value: 0};
0023 
0024   constructor(public index: GamepadButtonIndexes) {
0025   }
0026 
0027   updateState(newState: GamepadButton) {
0028     if(this.onPressSubject.observed) {
0029       if(newState.pressed != this.state.pressed) {
0030         this.onPressSubject.next(newState.pressed);
0031       }
0032     }
0033     this.state = newState;
0034   }
0035 
0036   updateFromGamepadState(gamepad: Gamepad) {
0037     this.updateState(gamepad.buttons[this.index]);
0038   }
0039 }
0040 
0041 @Injectable({
0042   providedIn: 'root'
0043 })
0044 export class GameControllerService {
0045 
0046   private xAxisSubject = new BehaviorSubject<number>(0);
0047   public xAxisChanged = this.xAxisSubject.asObservable();
0048   public xAxis = 0;
0049 
0050   private yAxisSubject = new BehaviorSubject<number>(0);
0051   private yAxisChanged = this.yAxisSubject.asObservable();
0052   public yAxis: number = 0;
0053   public buttons: GamepadButton[] = [];
0054   public prevButtons: GamepadButton[] = [];
0055 
0056 
0057   public buttonA: GamepadObservableButton = new GamepadObservableButton(GamepadButtonIndexes.ButtonA);
0058   public buttonB: GamepadObservableButton = new GamepadObservableButton(GamepadButtonIndexes.ButtonB);
0059   public buttonX: GamepadObservableButton = new GamepadObservableButton(GamepadButtonIndexes.ButtonX);
0060   public buttonY: GamepadObservableButton = new GamepadObservableButton(GamepadButtonIndexes.ButtonY);
0061   public buttonLB: GamepadObservableButton = new GamepadObservableButton(GamepadButtonIndexes.ButtonLB);
0062   public buttonRB: GamepadObservableButton = new GamepadObservableButton(GamepadButtonIndexes.ButtonRB);
0063   public buttonLT: GamepadObservableButton = new GamepadObservableButton(GamepadButtonIndexes.ButtonLT);
0064   public buttonRT: GamepadObservableButton = new GamepadObservableButton(GamepadButtonIndexes.ButtonRT);
0065   public buttonStart: GamepadObservableButton = new GamepadObservableButton(GamepadButtonIndexes.Start);
0066   public buttonSelect: GamepadObservableButton = new GamepadObservableButton(GamepadButtonIndexes.Select);
0067 
0068   public activeGamepad: Gamepad|null = null;
0069 
0070   animationLoopHandler () {
0071 
0072     const epsilon = 0.01;
0073     const gamepads = navigator.getGamepads();
0074     for (const gamepad of gamepads) {
0075       if (gamepad) {
0076 
0077         this.activeGamepad = gamepad;
0078         // Example: Using left joystick to control OrbitControls
0079         // Axis 0: Left joystick horizontal (left/right)
0080         // Axis 1: Left joystick vertical (up/down)
0081         this.xAxis = gamepad.axes[0];
0082         this.yAxis = gamepad.axes[1];
0083 
0084         if(Math.abs(this.xAxis - this.xAxisSubject.value) > epsilon) {
0085           this.xAxisSubject.next(this.xAxis);
0086         }
0087 
0088         if(Math.abs(this.yAxis - this.yAxisSubject.value) > epsilon) {
0089           this.yAxisSubject.next(this.yAxis);
0090         }
0091 
0092         this.buttonSelect.updateFromGamepadState(gamepad);
0093         this.buttonStart.updateFromGamepadState(gamepad);
0094 
0095         this.buttonA.updateFromGamepadState(gamepad);
0096         this.buttonB.updateFromGamepadState(gamepad);
0097         this.buttonY.updateFromGamepadState(gamepad);
0098         this.buttonX.updateFromGamepadState(gamepad);
0099 
0100         this.buttonLB.updateFromGamepadState(gamepad);
0101         this.buttonRB.updateFromGamepadState(gamepad);
0102         this.buttonLT.updateFromGamepadState(gamepad);
0103         this.buttonRT.updateFromGamepadState(gamepad);
0104 
0105         break; // Only use the first connected gamepad
0106       }
0107     }
0108   };
0109 
0110   constructor() {
0111     // Run it on contruction so if we have an active controller we set up values
0112     this.animationLoopHandler();
0113   }
0114 }