@moderrkowo/jsgl
Version:
Client-side JavaScript library for creating web 2D games. Focusing at objective game.
244 lines (243 loc) • 7.99 kB
TypeScript
import { Vector2 } from './structs/Vector2';
import { GameEvent } from './events/GameEvent';
import { Resource, ResourceType } from './structs/Resource';
import { Renderer } from './drawing/Renderer';
import { GameObject } from './gameobjects/GameObject';
import { ClickableGameObject } from './gameobjects/ClickableGameObject';
import { GameStartEvent } from './events/GameStartEvent';
import { GameSettings } from './structs/GameSettings';
import { Input } from './Input';
/**
* @group Important Classes
*/
export declare class Game {
readonly canvas: HTMLCanvasElement;
readonly renderer: Renderer;
readonly grid: Vector2;
readonly gameSettings: GameSettings;
canvasViewOffset: Vector2;
/**
* Constructs new Game instance with given settings.
* @param gameSettings The settings
*/
constructor(gameSettings: GameSettings);
/**
* Scaling canvas size on page to percentage of parent element.
* @param percentage The decimal midpoint
* @returns The final canvas size
*/
RescaleCanvasToParentElement(percentage: number): Vector2;
private _registerCanvasEvents;
private _isPlaying;
private _unscaledTime;
private _deltaTime;
timeScale: number;
isNeedToUpdate: boolean;
private _gameLoopUpdate;
private _gameLoopDraw;
private readonly _gameLoop;
/**
* Tells the game it needs to be redrawn.
* Call it when some visible objects has been changed.
*/
Update(): void;
/**
* Starts new game loop
*/
StartGameLoop(): void;
/**
* WARNING! Stops the game loop
*/
StopGameLoop(): void;
/**
* Starts the game loop and emit `start` ({@link GameEvent}) event.
*/
Start(): void;
/**
* Starts loading game resources and returns promise.
* @method
* @returns The Promise
* @example
* game.LoadGameAndStart().then((e) => {
* console.log('Game sucessfully loaded!');
* }).catch((error) => {
* console.error('Error');
* }).finally(() => {
* console.log("Finally");
* });
*/
LoadGameAndStart(): Promise<GameStartEvent>;
private readonly _signals;
emit: (channel: string, event: GameEvent) => void;
/**
* Appends listener to event channel.
* @method
* @param channel - The listener event channel.
* @param callback - Executed function after receiving a signal on given channel.
* @example
* game.on('channel', () => { console.log('received!') });
*/
on: (channel: string, callback: (event: GameEvent) => void) => void;
resources: Map<string, Resource>;
private _isLoadedAllResources;
/**
* Loads resource with resource manager.
* @method
* @param type - The resource type
* @param uid - The resource unique key
* @param path - The resource path
* @example
* game.LoadResource('image', 'player', './resources/img/player.png');
*/
LoadResource(type: ResourceType, uid: string, path: string): void;
/**
* Creates {@link HTMLImageElement} from path.
* @method
* @param path - The image path
* @returns The image element
* @example
* game.CreateImage('./resources/img/player.png');
*/
CreateImage(path: string): HTMLImageElement;
/**
* Gets the resource by unique resource key.
* @method
* @param uid - The unique resource key.
* @returns The resource
* @example
* const resource = game.GetResource('player');
*/
GetResource(uid: string): Resource;
/**
* Gets the image resource by unique resource key.
* @method
* @param uid - The unique resource key.
* @returns The image
* @example
* const image = game.GetImage('player');
*/
GetImage(uid: string): object | undefined;
/**
* Starts loading all resources which is not loaded. Emits {@link GameEvent} at `loadAllResources` channel.
*/
LoadAllResources(): void;
/**
* Sorted game object array
*/
readonly gameObjects: Array<GameObject>;
/**
* Sorts all game objects by sorting order property.
*/
SortGameObjects(): void;
/**
* Adds unique game object to game.
* @method
* @param gameObject - The unique game object
* @returns The added game object
* @example
* game.AddGameObject(new JSGL.GameObject());
*/
AddGameObject(gameObject: GameObject): GameObject;
/**
* Destroys existed game object by reference
* @method
* @param gameObject - The game object reference
* @example
* const gameObject = ...
* game.DestroyGameObjectByRef(gameObject);
*/
DestroyGameObjectByRef(gameObject: GameObject): void;
/**
* Destroys existed game object by unique id
* @method
* @param id - The unique id
* @example
* game.DestroyGameObjectById('51870300-4187221613-3012590175-3461657014');
*/
DestroyGameObjectById(id: string): void;
/**
* Destroys existed game object by index in game objects array
* @method
* @param index - The index
* @example
* game.DestroyGameObjectByIndex(0);
*/
DestroyGameObjectByIndex(index: number): void;
/**
* Gets all existed game objects with type equal to param.
* @param type - The type
* @returns Array of selected game objects
* @example
* game.GetGameObjectsByType(JSGL.Shape);
*/
GetGameObjectsByType(type: object): Array<GameObject>;
/**
* Gets all existed game objects by given name.
* @method
* @param name - The name.
* @returns Array of game objects with given name.
* @example
* game.GetGameObjectsByName('exampleName');
*/
GetGameObjectsByName(name: string): Array<GameObject>;
/**
* Gets all existed game objects by given tag.
* @method
* @param tag The - tag.
* @returns The array of game objects with given tag.
* @example
* game.GetGameObjectsByTag('exampleTag');
*/
GetGameObjectsByTag(tag: string): Array<GameObject>;
/**
* Gets game object by unique id.
* @method
* @param id - The unique id
* @returns The game object
* @example
* game.GetGameObjectById('51870300-4187221613-3012590175-3461657014');
*/
GetGameObjectById(id: string): GameObject | undefined;
/**
* Plays sound at the page.
* @method
* @param path - The path to sound file
* @param loop - is looped?
* @param volume - The volume decimal midpoint
* @example
* game.PlaySound('./resources/sounds/death.mp3', false, 0.8);
*/
PlaySound(path: string, loop?: boolean, volume?: number): void;
input: Input;
/**
* Use `input.mouseWorldPosition` instead.
* @deprecated since version 1.0.8
*/
get mousePos(): Vector2;
/**
* Use `input.mouseClientPosition` instead.
* @deprecated since version 1.0.8
*/
get mouseClientPos(): Vector2;
/**
* Use `input.mousePreciseWorldPosition` instead.
* @deprecated since version 1.0.8
*/
get mousePrecisePos(): Vector2;
/**
* Use `input.isMousePrimaryButtonDown` instead.
* @deprecated since version 1.0.8
*/
get isMousePrimaryButtonDown(): boolean;
private lastMouseHoveredGameObject;
get mouseHoveredGameObject(): ClickableGameObject | undefined;
private constructMouseEvent;
private mouseMoveHandler;
private mouseDownHandler;
private mouseUpHandler;
private mouseClickHandler;
private _tempMouseWheelDeltaY;
private mouseWheelHandler;
GetRandomPosition(): Vector2;
GetRandomPositionIn(min: Vector2, max: Vector2): Vector2;
}