UNPKG

@vrspace/babylonjs

Version:

vrspace.org babylonjs client

140 lines (139 loc) 6.05 kB
/** * Base class for a simple multiuser game. * Contains utility methods to create, join and quit the game, that open appropriate forms and dialogues. * Whoever created the game owns it: only their browser executes all of game logic, * and is allowed to change game state by sending events to the game object. * All players receive game all events, and execute the same presentation logic. */ export class BasicGame extends BasicScript { constructor(world: any, vrObject: any); /** Object containing all URLs of all sounds */ sounds: {}; /** GameStatusForm, created and destroyed by show/close game status methods */ gameStatus: GameStatusForm; /** Dialogue, created and destroyed by invitePlayers/joinGame methods */ joinDlg: Dialogue; /** Status flag, set in joinGame method */ playing: boolean; /** Game status flag, supposed to be set on remote event */ gameStarted: boolean; /** Callback executed with true/false when game starts/ends */ callback: any; /** Number of players at the moment of creation, copied from the shared object */ totalPlayers: any; /** Shared delay, set initially and updated once the game starts */ delay: number; /** Minumum delay, limits the slider */ minDelay: number; /** Maximum delay, limits the slider */ maxDelay: number; /** Start time in milliseconds, set once the game starts - owner only */ startTime: number; /** Player list, contains User objects */ players: any[]; indicators: any[]; /** * Creates a new HUD row, and opens a new GameStatusForm to start or join the game. */ showGameStatus(): void; /** * Sets number of players in game status form, if currently open. */ updateStatus(): void; /** * If the game is owned by current user, sends start event. */ startGame(): void; /** * Close GameStatusForm if currently open, and restore the HUD. */ closeGameStatus(): void; /** * Join the game - shows game status and sends game join command to the server. * @param {boolean} yes */ joinGame(yes: boolean): void; /** * Quit the game - sends the command to the server, optionally calls this.callback with false. */ quitGame(): void; /** * Typically the first thing to do. Game owner joins the game at once, everyone else gets the dialogue, * that triggers this.joinGame() as callback. */ invitePlayers(): void; /** * Calls either showGameStatus, or invitePlayers, as appropriate. * Supposed to be executed on button click, if game instance already exists. */ startRequested(): void; /** * Helper method, returns current position the avatar, in either 1st or 3rd person view. */ avatarPosition(): any; /** * Helper method to attach an icon above the avatar. * @param {Mesh} baseMesh avatar parent mesh * @param {String} icon URL of the icon to attach * @param {*} [color=new BABYLON.Color4(1,1,1,1)] optional color */ addIndicator(baseMesh: Mesh, icon: string, color?: any): void; /** * Removes and disposes indicator from the base mesh and from list of indicators. */ removeIndicator(baseMesh: any): void; /** * Stops currently playing sound, and plays a sound attached to the avatar, if available. Sets SoundPlaying member field of the avatar mesh. */ playSound(avatarBase: any, soundName: any): void; /** * Adds and indicator to an avatar, and optionally plays a sound. * @param {Object} playerEvent object containing className and id of the player (can be User object) * @param {String} soundName name of the sound, one of this.sound, passed to playSound * @param {String} icon URL of the texture, passed to addIndicator * @param {*} color passed to addIndicator */ changePlayerStatus(playerEvent: any, soundName: string, icon: string, color: any): any; /** * Attach sounds to avatar mesh, called when player joins; this implementation does nothing. */ attachSounds(avatarMesh: any): void; /** * If the mesh contains field specified by soundName, detaches it, disposes, and deletes. */ removeSound(baseMesh: any, soundName: any): void; /** * Remove all sounds from avatar mesh, called when player quits. * Iterates over all keys of this.sounds object, and calls removeSound for each. */ detachSounds(avatarMesh: any): void; /** * Helper method to be executed when a player joins the game, including the one that starts the game. * Adds the player to the player list, and calls attachSounds witht he player avatar. * When local player joins the game, the callback is called with true (if set). * Requires player avatar to be already loaded - may not be safe for async usage. * @param {Object} player object containing className and id of the player (User object will do) */ playerJoins(player: any): void; /** * Enables or disables movement for any kind of camera. */ enableMovement(enable: any): void; movementMode: any; /** * Helper method to be executed when a player quits the game, including the game owner. * Finds the player avatar and detaches attached sounds and indicator, then removes it from the player list. * If this player has quit, also closes the the game status. * @param {Object} player object containing className and id of the player (User object will do) */ playerQuits(player: any): void; /** * Main method of the game. Receives all events that happen in the game, and is supposed to implement scene changes. * @param {Game} vrObject game object * @param {Object} changes custom game event */ remoteChange(vrObject: Game, changes: any): void; } import { BasicScript } from "../scripts/basic-script.js"; import { GameStatusForm } from "./game-status-form.js"; import { Dialogue } from "../ui/widget/dialogue.js";