Module: pm

Presentation Manager module abstracts functions common to all games.
Author:
  • Robert Laing
Source:

Members

(static) imagesDict :Object

A flyweight pattern to hold blobs of images to be used any number of times by renderObjects
Type:
  • Object
Source:

(static) renderDict :Object

Accessed by clients via upsert_renderDict(name, renderObject)
Type:
  • Object
Source:

Methods

(static) draw(ctx, renderObject)

Moved sx, sy, sWidth, sHeight to renderObject, so no longer reference via imagesDict[renderObject.name] to allow sprites.
Parameters:
Name Type Description
ctx CanvasRenderingContext2D Either for an OffscreenCanvas or browser canvas.
renderObject renderObject Note the renderObject looks up the source image via the name string.
Source:

(static) imagesDict(key, value)

Exported function to add or replace an imageObject in imagesDict
Parameters:
Name Type Description
key string not necessarily the same as renderObject.name, but can be.
value imageObject An object with the relevant keys for the given item to be rendered.
Source:

(static) render(renderArr)

Parameters:
Name Type Description
renderArr Array.<string>
Source:

(static) renderDict(key, value)

Exported function to add or replace a renderObject in renderDict
Parameters:
Name Type Description
key string not necessarily the same as renderObject.name, but can be.
value renderObject An object with the relevant keys for the given item to be rendered.
Source:

(static) resize()

Source:

Type Definitions

imageObject

The source portion and binary (ie "nontransferable") data of an item to be drawn in a canvas.
Type:
  • Object
Properties:
Name Type Description
image Image loaded from a file or offscreen canvas drawing
loaded boolean a flag to avoid ctx.drawImage crashes
sx pixels left hand side of source image, usually 0 unless from sprite sheet
sy pixels top of source image, usually 0 unless from sprite sheet
sWidth pixels source width
sHeight pixels source height
Source:

renderObject

The destination portion with only a string key referencing the binary (ie "nontransferable") data stored separately in imagesDict.
Type:
  • Object
Properties:
Name Type Attributes Description
name string key to imagesDict, creating a "flyweight" pattern to avoid duplicating image blobs, also allows structuredClone
base Array <optional>
base in state corresponding to counter
x1 pixels sprite's current center X, used by ctx.translate
y1 pixels sprite's current center Y, used by ctx.translate
sx pixels Best draw() looks for the source corner here in case it's a sprite
sy pixels Best draw() looks for the source corner here in case it's a sprite
sWidth pixels Best draw() looks for the source corner here in case it's a sprite
sHeight pixels Best draw() looks for the source corner here in case it's a sprite
angle1 radians 0 for stationary drawn images or Math.PI/2 for sprites, Math.atan2(y2 - y1, x2 - x1) for moving images
x2 pixels <optional>
sprite's destination center X, same as x1 if not moving
y2 pixels <optional>
sprite's destination center Y
angle2 radians <optional>
sprite's destination direction, same as angle1 if it's not rotating
velocity pixels <optional>
0.0 if the sprite is stationary
deltaX pixels <optional>
added to x1 each tick, Math.cos(angle1 + Math.PI/2) * velocity (the 90 degree addition is to remove the sprite facing up)
deltaY pixels <optional>
added to y1 each tick, Math.sin(angle1 + Math.PI/2) * velocity
deltaAngle degrees <optional>
- added to angle1 each tick, controls how fast sprites rotate
width pixels needed by ctx.drawImage
height pixels needed by ctx.drawImage
live boolean if false, gets removed from sprites array
status string <optional>
per game lables such as "idle", "selectable", "selected", "moving", "attacking", "defending", "killed" used to access relevant update function.
Source: