KilledByAPixel / LittleJS
- четверг, 30 сентября 2021 г. в 00:29:59
The Tiny JavaScript Game Engine That Can! 🚂
LittleJS is a lightweight 2D JavaScript game engine with a super fast WebGL rendering system. The goal of this project is to be small, simple, and easy to use for a variety of applications from size coding game jams to commercial releases. This engine has everything necessary for most games including super fast rendering, physics, particles, sound effects, music, keyboard/mouse/gamepad input handling, update/render loop, and debug tools.
It is recommended that you start by copying the LittleJS Starter Project It is mostly empty with just a few things you can use to get started or remove. You can also download and include engine.all.js or engine.all.min.js.
To startup LittleJS, you must create 5 functions and call engineInit. A canvas will automatically be created and added to the document. You can use this template to get started.
function gameInit()
{
// called once after the engine starts up
// setup the game
}
function gameUpdate()
{
// called every frame at 60 frames per second
// handle input and update the game state
}
function gameUpdatePost()
{
// called after physics and objects are updated
// setup camera and prepare for render
}
function gameRender()
{
// called before objects are rendered
// draw any background effects that appear behind objects
}
function gameRenderPost()
{
// called after objects are rendered
// draw effects or hud that appear above all objects
}
// startup LittleJS with your game functions after the tile image is loaded
engineInit(gameInit, gameUpdate, gameUpdatePost, gameRender, gameRenderPost, 'tiles.png');
For most games you will want to extend EngineObject with your own objects. This will create an object class called GameObject and the constructor automatically adds it to the list of objects. Engine objects are automatically updated and rendered until they are destroyed.
You can override these functions to make objects behave however you want. See the examples for a complete demonstration.
class MyObject extends EngineObject
{
constructor(pos, size, tileIndex, tileSize, angle, color)
{
super(pos, size, tileIndex, tileSize, angle, color);
// your object init code here
}
update()
{
super.update(); // update object physics and position
// your object update code here
}
render()
{
super.render(); // draw object as a sprite
// your object render code here
}
}
All engine settings are listed in engineConfig.js. Here are the most important settings...