Core game loop and gameplay logic for hexaworld games. Constructs a game world using the provided schema, and runs the game, including movement, collision detection, and gameplay events. This module is designed to be passed to a variety of external renderers or other forms of progammatic access. It only requires a schema
and controller
, each of which conform to a simple API. Works well with crtrdg.js components.
Example renderers:
- 3d with stack.gl:
hexaworld-renderer-3d
First define a level schema
var config = {
energy: 2400
}
var map = {
tiles: [
{coordinates: [0, 0], paths: [0, 2, 4]},
{coordinates: [-1, 0], paths: [0, 2, 4]},
{coordinates: [0, 1], paths: [0, 2, 4]}
]
}
var schema = {
config: config,
map: map
}
then create by providing the schema and a controller
var controller = require('crtrdg-tty')()
var core = require('hexaworld-core')
var game = core({schema: schema, controller: controller})
then you can start the game
game.start()
with the crtrdg-tty
controller you can play with your keys and see game events in the terminal! But you'll usually want to combine with a renderer, and use controllers designed for the browser.
The required inputs are
-
schema
Schema for a level, should follow the schema for levels defined inhexaworld-schema
.Examples:
hexaworld-levels
-
controller
Should have akeysDown
property with the current state of keys and/or buttons.Examples:
crtrdg-keyboard
,crtrdg-touch
,crtrdg-tty
Methods are provided to control the underlying game loop
game.start()
start the gamegame.end()
end the gamegame.pause()
pause the gamegame.resume()
resume the game
The following properties are returned
game.objects
A list of all game objects (including the player, consumables, and parts of the game world) to be used as required by external renderers. Each object has the following schema:
{
id: 'string',
type: 'string',
points: [[x,y], [x,y], ...],
transform: {translation: [x,y], scale: s, rotation: r}
}
The following events are provided:
game.on('consume')
collide with any consumable objectgame.on('move')
player movesgame.on('enter')
player enters a tilegame.on('exit')
player exits a tile
Each provides as argument to the callback the object affected by the event.