initial jsdoc

This commit is contained in:
Theis Pieter Hollebeek 2025-10-15 16:05:19 +02:00
parent 99703e03d7
commit cb6a64d8c3

View File

@ -150,6 +150,7 @@ export class Gamelib {
drawSprite(x, y, sprite) {
const cx = this.cx;
console.log(sprite);
cx.drawImage(sprite, x, y);
}
@ -276,7 +277,58 @@ export class Gamelib {
}
}
/**
* A function that is called whenever the Key it is registered to is pressed or released.
* @callback OnKeyEventHandler
*/
/**
* A function that is called whenever the MouseButton it is registered to is pressed or released.
* @callback OnClickEventHandler
*/
/**
* A function that is called whenever the mouse is moved.
* @callback OnMouseMoveEventHandler
* @param {number} positionX The mouse x position
* @param {number} positionY The mouse y position
* @param {number} deltaX The difference in x position between this and the last onMouseMove event
* @param {number} deltaY The difference in y position between this and the last onMouseMove event
*/
/**
* A loaded sprite
* @typedef {object} Sprite
*/
/**
* A html color
* @typedef {string} Color
*/
/**
* A component of `[x, y]` of a path.
* @typedef {[number, number]} PathComponent
*/
/**
* Text style
* @typedef TextStyle
* @type {object}
* @property {number} fontWeight Font weight
* @property {number} fontStyle !!!!!!!!!!!!!!!!!!!!! no idea what this does, needs better docs
* @property {number} fontSize Font size in pixels
* @property {string} fontFamily Font family
* @property {"left"|"center"|"right"} align Text alignment
* @property {string} baseline !!!!!!!!!!!!!!!!!!!!! no idea what this does, needs better docs
* @property {string} direction !!!!!!!!!!!!!!!!!!!!! no idea what this does, needs better docs
* @property {Color} color
*/
export class GamelibAdapter {
/* Enum for MouseButton values
* @enum {number}
*/
MouseButton = {
Left: 0,
Right: 1,
@ -311,74 +363,192 @@ export class GamelibAdapter {
this.gamelib.startGameLoop(loopFunction);
}
/**
* Returns whether or not `key` is currently pressed.
* `Key` can any one of [KeyBoardEvent.key](https://developer.mozilla.org/en-US/docs/Web/API/UI_Events/Keyboard_event_key_values)'s values
* @param {Key} key
* @return {boolean} Whether `key` is pressed.
*/
isPressed(key) {
return this.gamelib.keysPressed.has(key);
}
/**
* Registers a `handlerFunction` that is called when `key` is pressed.
* Key can be any of [KeyBoardEvent.key](https://developer.mozilla.org/en-US/docs/Web/API/UI_Events/Keyboard_event_key_values)'s values
* @param {Key} key
* @param {OnKeyEventHandler} handlerFunction
* @return {void}
*/
onPress(key, handlerFunction) {
this.gamelib.keyPressHandlers.set(key, handlerFunction);
}
/**
* Registers a `handlerFunction` that is called when `key` is pressed.
* Key can be any of [KeyBoardEvent.key](https://developer.mozilla.org/en-US/docs/Web/API/UI_Events/Keyboard_event_key_values)'s values
* @param {Key} key
* @param {OnKeyEventHandler} handlerFunction
*/
onRelease(key, handlerFunction) {
this.gamelib.keyReleaseHandlers.set(key, handlerFunction);
}
/**
* Registers a `handlerFunction` that is called when mouse is moved.
* @param {OnMouseMoveEventHandler} handlerFunction
*/
onMouseMove(handlerFunction) {
this.gamelib.mouseMoveHandler = handlerFunction;
}
/**
* Returns whether or not `button` is currently clicked.
* MouseButton is accessed from `lib.MouseButton`
* @param {GamelibAdapter.MouseButton} button defaults to {GamelibAdapter.MouseButton.Left}
* @return {boolean} Whether `key` is pressed.
*/
isClicking(button = this.MouseButton.Left) {
return this.gamelib.mouseButtonsPressed.has(button);
}
/**
* Registers a `handlerFunction` that is called when `button` is pressed.
* MouseButton is accessed from `lib.MouseButton`
* @param {OnClickEventHandler} handlerFunction
* @param {GamelibAdapter.MouseButton} button defaults to {GamelibAdapter.MouseButton.Left}
*/
onClick(handlerFunction, button = this.MouseButton.Left) {
this.gamelib.mouseDownHandlers.set(button, handlerFunction);
}
/**
* Registers a `handlerFunction` that is called when `button` is released.
* MouseButton is accessed from `lib.MouseButton`
* @param {OnClickEventHandler} handlerFunction
* @param {GamelibAdapter.MouseButton} button defaults to {GamelibAdapter.MouseButton.Left}
*/
onClickRelease(handlerFunction, button = this.MouseButton.Left) {
this.gamelib.mouseUpHandlers.set(button, handlerFunction);
}
loadSprite(name, width, height) {
return this.gamelib.loadSprite(name, width, height);
/**
* Loads a sprite with `name`, rendered with specified `width` and `height`
*
* @param {string} name The sprite name
* @param {number} width
* @param {number} height
* @return {Promise<Sprite>} The loaded sprite
*/
async loadSprite(name, width, height) {
return await this.gamelib.loadSprite(name, width, height);
}
/**
* Draws a sprite loaded by {GamelibAdapter.loadSprite}
*
* @param {number} x
* @param {number} y
* @param {Sprite} A loaded sprite
*/
drawSprite(x, y, sprite) {
this.gamelib.drawSprite(x, y, sprite);
}
/**
* Draws a rotated sprite loaded by {GamelibAdapter.loadSprite}
*
* @param {number} x
* @param {number} y
* @param {Sprite} sprite A loaded sprite
* @param {number} angle An angle in radians
*/
drawSpriteRotated(x, y, sprite, angle) {
this.gamelib.drawSpriteRotated(x, y, sprite, angle);
}
/**
* Generates an rgb Color.
*
* @param {number} red
* @param {number} green
* @param {number} blue
* @return {Color} Formatted {Color} string
*/
rgb(red, green, blue) {
return `rgb(${red}, ${green}, ${blue})`;
}
/**
* Clears the screen with `color`
* @param {Color} color
*/
clear(color) {
this.gamelib.clear(color);
}
/**
* Draws a rect at `(x,y)` with a size of `(width,height)` in `color`
* @param {number} x
* @param {number} y
* @param {number} width
* @param {number} height
* @param {Color} color
*/
drawRect(x, y, width, height, color) {
this.gamelib.drawRect(x, y, width, height, color);
}
/**
* Draws a circle at `(x,y)` with a radius of `r` in `color`
* @param {number} x
* @param {number} y
* @param {number} r Radius of circle
* @param {Color} color
*/
drawCircle(x, y, r, color) {
this.gamelib.drawCircle(x, y, r, color);
}
/**
* Draws a line from `(x0,y0)` to `(x1,y1)` with a thickness of `thickness` in `color`
* @param {number} x0
* @param {number} y0
* @param {number} x1
* @param {number} y1
* @param {number} thickness
* @param {Color} color
*/
drawLine(x0, y0, x1, y1, thickness, color) {
this.gamelib.drawLine(x0, y0, x1, y1, thickness, color);
}
/**
* Draws a polygon in `color` based off of `path` components
* @param {PathComponent[]} path
* @param {Color} color
*/
drawPath(path, color) {
this.gamelib.drawPath(path, color);
}
/**
* Draws an outline in `color` based off of `path` components, with a thickness of `thickness`
* @param {PathComponent[]} path
* @param {number} thickness
* @param {Color} color
*/
drawPathLine(path, thickness, color) {
this.gamelib.drawPathLine(path, thickness, color);
}
/**
* Draw text
* @param {number} x
* @param {number} y
* @param {string} text
* @param {TextStyle} style
*/
drawText(x, y, text, style = {}) {
this.gamelib.drawText(x, y, text, style);
}