Constructor
new EngineObject(posopt, sizeopt, tileInfoopt, angleopt, coloropt, renderOrderopt)
Create an engine object and adds it to the list of objects
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
pos | Vector2 | <optional> | vec2() | World space position of the object |
size | Vector2 | <optional> | vec2(1) | World space size of the object |
tileInfo | TileInfo | <optional> | Tile info to render object (undefined is untextured) | |
angle | number | <optional> | 0 | Angle the object is rotated by |
color | Color | <optional> | WHITE | Color to apply to tile when rendered |
renderOrder | number | <optional> | 0 | Objects sorted by renderOrder before being rendered |
- Source
// create an engine object, normally you would first extend the class with your own
const pos = vec2(2,3);
const object = new EngineObject(pos);Members
additiveColor :Color|undefined
- Color |
undefined
| Type | Description |
|---|---|
| Color | | Additive color to apply when rendered |
- Source
angle
Properties| Type | Description |
|---|---|
| number | Angle to rotate the object |
- Source
angleDamping
Properties| Type | Description |
|---|---|
| number | Fraction of angular velocity kept each frame, 1 keeps all of it, 0 stops at once |
- Source
angleVelocity
Properties| Type | Description |
|---|---|
| number | Angular velocity of the object, in radians per frame |
- Source
castShadow
Properties| Type | Description |
|---|---|
| boolean | Does this object draw into the light system's shadow map; false for a floor layer, a background, a pickup |
- Source
children :Array.<EngineObject>
- Array.<EngineObject>
| Type | Description |
|---|---|
| Array.<EngineObject> | List of children of this object |
- Source
clampSpeed
Properties| Type | Description |
|---|---|
| boolean | Limit object speed along x and y axis |
- Source
collideLevel :boolean
- boolean
| Type | Description |
|---|---|
| boolean | Object collides with the level, its tile collision layers |
- Source
collideRaycast
Properties| Type | Description |
|---|---|
| boolean | Object collides with raycasts |
- Source
collideSolidObjects
Properties| Type | Description |
|---|---|
| boolean | Object collides with solid objects |
- Source
collideTiles :boolean
- boolean
- Deprecated
- since 1.20, use collideLevel
- Source
color
Properties| Type | Description |
|---|---|
| Color | Color to apply when rendered |
- Source
damping
Properties| Type | Description |
|---|---|
| number | Fraction of velocity kept each frame, 1 keeps all of it, 0 stops at once |
- Source
destroyed
Properties| Type | Description |
|---|---|
| boolean | Has object been destroyed? |
- Source
drawSize :Vector2|undefined
- Vector2 |
undefined
| Type | Description |
|---|---|
| Vector2 | | Size of object used for drawing, uses size if not set |
- Source
emissive
Properties| Type | Description |
|---|---|
| number | With the light system, how much it lights itself: 0 lit only by the lights, 1 full brightness in its own colors whatever the lights do, between partly; drawn into the lightmap through renderEmissive, as 3D's emissive. Exact for solid pixels; a partly transparent one is self lit by its alpha too, so a half alpha pixel shows at a quarter and a fading emissive sprite fades a little faster |
- Source
friction
Properties| Type | Description |
|---|---|
| number | Fraction of sliding speed kept each frame on the ground, 1 is no friction, 0 stops at once, the more slippery of the object and its ground is used |
- Source
gravityScale
Properties| Type | Description |
|---|---|
| number | How much to scale gravity by for this object |
- Source
groundObject :EngineObject|undefined
- EngineObject |
undefined
| Type | Description |
|---|---|
| EngineObject | | Object we are standing on, if any |
- Source
isSolid
Properties| Type | Description |
|---|---|
| boolean | Object collides with and blocks other objects |
- Source
localAngle
Properties| Type | Description |
|---|---|
| number | Local angle if child |
- Source
localPos :Vector2|undefined
- Vector2 |
undefined
| Type | Description |
|---|---|
| Vector2 | | Position relative to the parent, only while attached to one |
- Source
mass
Properties| Type | Description |
|---|---|
| number | How heavy the object is, static if 0: a static object moves by its velocity but does not collide on its own, the moving ones collide with it |
- Source
mirror
Properties| Type | Description |
|---|---|
| boolean | Should the rendered tile flip along the y axis. Affects rendering and the local→world transform of attached children (a mirrored parent flips its children's localPos.x and localAngle). Does not affect this object's own physics, collision, or localToWorld/worldToLocal. |
- Source
parent :EngineObject|undefined
- EngineObject |
undefined
| Type | Description |
|---|---|
| EngineObject | | Parent of object if in local space |
- Source
persistent
Properties| Type | Description |
|---|---|
| boolean | Object is skipped by engineObjectsDestroy, for things that outlive a level like a camera
|
- Source
pos
Properties| Type | Description |
|---|---|
| Vector2 | World space position of the object |
- Source
renderOrder
Properties| Type | Description |
|---|---|
| number | Objects are sorted by render order |
- Source
restitution
Properties| Type | Description |
|---|---|
| number | How bouncy the object is when colliding (0-1) |
- Source
shader :Shader|undefined
- Shader |
undefined
| Type | Description |
|---|---|
| Shader | | Custom shader to render with, undefined for the engine's own |
- Source
size
Properties| Type | Description |
|---|---|
| Vector2 | World space width and height of the object |
- Source
spawnTime
Properties| Type | Description |
|---|---|
| number | Track when object was created |
- Source
tileInfo :TileInfo|undefined
- TileInfo |
undefined
| Type | Description |
|---|---|
| TileInfo | | Tile info to render object (undefined is untextured) |
- Source
velocity
Properties| Type | Description |
|---|---|
| Vector2 | Velocity of the object, in world units per frame |
- Source
Methods
addChild(child, localPosopt, localAngleopt) → {EngineObject}
Attaches a child to this with a local transform, returns child for chaining
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
child | EngineObject | |||
localPos | Vector2 | <optional> | vec2() | |
localAngle | number | <optional> | 0 |
- Source
The child object added
- Type:
- EngineObject
applyAcceleration(acceleration)
Apply acceleration to this object (adjust velocity, not affected by mass)
- Does nothing on a static object (mass 0), set its velocity instead
| Name | Type | Description |
|---|---|---|
acceleration | Vector2 |
- Source
applyAngularAcceleration(acceleration)
Apply angular acceleration to this object
- Does nothing on a static object (mass 0), set its angleVelocity instead
| Name | Type | Description |
|---|---|---|
acceleration | number |
- Source
applyForce(force)
Apply force to this object (adjust velocity, affected by mass)
- Does nothing on a static object (mass 0), set its velocity instead
| Name | Type | Description |
|---|---|---|
force | Vector2 |
- Source
attach(child) → {EngineObject}
Attaches a child to this without moving it: the local transform is worked out from where the child is now, where addChild takes one; a child of something else is moved over, returns child for chaining
| Name | Type | Description |
|---|---|---|
child | EngineObject |
- Source
The child object attached
- Type:
- EngineObject
collideWithObject(object, pushopt) → {boolean}
Called by the engine to check if an object collision should be resolved. Return true for physics to resolve the collision or false to ignore and resolve it manually.
- Both objects of a touching pair are asked once a frame, whichever order they update in; an object that destroys itself here is gone at the end of the frame and is still asked about the pairs left this frame, so a bullet that should hit one thing checks its own destroyed flag first
| Name | Type | Attributes | Description |
|---|---|---|---|
object | EngineObject | the object to test against | |
push | Vector3 | <optional> | what it would take to move this object clear, a Vector3 from the 3D plugin, undefined in 2D |
- Source
- true if the collision should be resolved by modifying it's position and velocity
- Type:
- boolean
collideWithTile(tileData, pos) → {boolean}
Called to check if a tile collision should be resolved. Return true for physics to resolve the collision or false to ignore and resolve it manually.
- Called for each solid tile the physics tests, which can be several times a frame for the same tile, and for positions it only tries, so keep it free of side effects or guard them to once a frame
- this.pos has already moved, so a check on where it came from, like a one way platform, needs the position saved in update, as the platformer example does
| Name | Type | Description |
|---|---|---|
tileData | number | the value of the tile at the position |
pos | Vector2 | the tile's bottom left corner in world space |
- Source
- true if the collision should be resolved by modifying it's position and velocity
- Type:
- boolean
destroy(immediateopt)
Destroy this object, destroy its children, detach its parent, and mark it for removal
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
immediate | boolean | <optional> | false | true removes attached effects like particle emitters at once, false lets them finish first |
- Source
getAliveTime() → {number}
How long since the object was created
- Source
- Type:
- number
getMirrorSign() → {number}
Get the direction of the mirror
- Source
-1 if this.mirror is true, or 1 if not mirrored
- Type:
- number
getRight(scaleopt) → {Vector2}
Get this object's right vector
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
scale | number | <optional> | 1 | length of the vector |
- Source
- Type:
- Vector2
getSpeed() → {number}
Get the speed of this object
- Source
- Type:
- number
getUp(scaleopt) → {Vector2}
Get this object's up vector
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
scale | number | <optional> | 1 | length of the vector |
- Source
- Type:
- Vector2
isOverlapping(pos, sizeopt) → {boolean}
Check if overlapping a point or aligned bounding box
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
pos | Vector2 | Center of box | ||
size | Vector2 | <optional> | vec2() | Size of box, uses a point if undefined |
- Source
- Type:
- boolean
isOverlappingObject(object) → {boolean}
Check if this object's box overlaps another object's box
| Name | Type | Description |
|---|---|---|
object | EngineObject |
- Source
- Type:
- boolean
localToWorld(pos)
Convert from local space to world space
| Name | Type | Description |
|---|---|---|
pos | Vector2 | local space point |
- Source
localToWorldVector(vec)
Convert from local space to world space for a vector (rotation only)
| Name | Type | Description |
|---|---|---|
vec | Vector2 | local space vector |
- Source
removeChild(child)
Removes a child from this one, it stays where it is in the world
| Name | Type | Description |
|---|---|---|
child | EngineObject |
- Source
render()
Render the object, draws a tile by default, automatically called each frame, sorted by renderOrder
- Source
renderDebugInfo()
Render debug info for this object
- Source
renderEmissive()
Draw this object's shape into the light system's lightmap, called during its light pass when emissive is above 0; what it draws shows in its own colors that much brighter. Calls render() by default so the whole object glows; override to glow a part, like a robot's eyes
- Source
renderLight()
Optional hook called during the light system plugin's lightmap pass to draw this object's lightmap contribution. Does nothing by default.
- Source
renderShadow()
Draw this object into the light system's shadow map, called during its shadow pass when castShadow is set. Calls render() by default so the object casts its own shape; override to cast a different one, like a blob at a character's feet so its body stays lit; screen space WebGL draws in render() are skipped during the pass
- Source
setCollision(collideSolidObjectsopt, isSolidopt, collideLevelopt, collideRaycastopt)
Set how this object collides
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
collideSolidObjects | boolean | <optional> | true | Does it collide with solid objects? |
isSolid | boolean | <optional> | true | Does it collide with and block other objects? (expensive in large numbers) |
collideLevel | boolean | <optional> | true | Does it collide with the level, its tile collision layers? |
collideRaycast | boolean | <optional> | true | Does it collide with raycasts? |
- Source
toString() → {string}
Returns string containing info about this object for debugging
- Source
- Type:
- string
update()
Update the object, called automatically by engine once each frame. Does nothing by default.
- Source
updatePhysics()
Update the object physics, called automatically by engine once each frame. Can be overridden to stop or change how physics works for an object.
- Source
updateTransforms(updateChildrenopt)
Update the object transform, called automatically by engine even when paused
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
updateChildren | boolean | <optional> | true | Also update the children's transforms |
- Source
worldToLocal(pos)
Convert from world space to local space
| Name | Type | Description |
|---|---|---|
pos | Vector2 | world space point |
- Source
worldToLocalVector(vec)
Convert from world space to local space for a vector (rotation only)
| Name | Type | Description |
|---|---|---|
vec | Vector2 | world space vector |
- Source