LittleJS 3D Level Plugin
- A 3D level is a list of objects, each a type name, a position, and a rotation, scale and properties where they are not the default, kept as plain JSON that can be written by hand
- level3DAddType names the types a game makes them from, as objectLayersAddType does in 2D
- level3DLoad makes every object of a level
- Box, Sphere, Cylinder and Light are built in, to block out and light a level with no code
- The 3D level editor, in debug builds, edits the level a game loaded
- Source
Methods
(static) level3DAddMesh(name, mesh, tileInfoopt, coloropt)
Add a type that is a mesh and nothing more, a static prop with no class to write, for a built mesh or a model
- Each object has a color and a solid property, solid collides as the box around the mesh
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
name | string | The type the objects have in the level | |
mesh | Mesh | Shared by every object of the type | |
tileInfo | TileInfo | <optional> | Its texture |
color | Color | <optional> | Its color, an object's own color property goes over it |
- Source
Example
level3DAddMesh('Tree', treeMesh, tile(4));(static) level3DAddType(name, make, defaultsopt, tileInfoopt)
Add a type of object, so level3DLoad makes one wherever a level has an object of that type
- The name is a string because minified builds rename classes
- A class, or any function with a prototype, is made with new make(pos3D, properties); an arrow function is called as make(pos3D, properties), for what is not an object, like a player start
- properties is the defaults with the object's own values over them, and each of the type's own is also set on what was made; a property the type has no default for is in properties and is not set
- Give a class a constructor of its own that takes the position: one that hands every argument on to EngineObject3D would hand it the properties as its mesh
- An EngineObject3D then gets the object's rotation, and its scale times the scale it was made with
- Adding a name again replaces it, Box, Sphere, Cylinder and Light too
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
name | string | The type the objects have in the level | |
make | function | A class made at each object's position, or a function called with it | |
defaults | Object | <optional> | Properties of each one made, the level editor shows inputs for them |
tileInfo | TileInfo | <optional> | An icon for the level editor |
- Source
Example
level3DAddType('Crate', Crate, {health: 3});
level3DAddType('PlayerStart', (pos)=> playerStart = pos);(static) level3DLoad(level) → {Array.<any>}
Make the objects of a level, each from the type added for its name
- The level is an object: {littlejs3D: 1, objects: [{id, type, pos: [x, y, z]}, ...]}, and {} is a new one
- rotation is pitch, yaw and roll in degrees, scale is a number for each axis, both left out when they are the default, and properties holds what differs from the type's defaults
- A Color property is a #rrggbb or #rrggbbaa string, a Vector2 or Vector3 an array, as the default says
- An object whose type was not added is skipped, with a warning in debug builds
- What a file written by hand gets wrong uses the default: a value that is not of its default's type
- An object its type can not make is skipped with an error in debug builds, where asserts throw, and the rest of the level is made
Parameters:
| Name | Type | Description |
|---|---|---|
level | Object | The level, the level editor edits this same object |
- Source
Returns:
- What each object's type made, a function that made nothing is left out
- Type:
- Array.<any>