Skip to content
/ loop Public

The missing game loop for your Pure function.

Notifications You must be signed in to change notification settings

AdamGonda/loop

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

53 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

The missing game loop for your Pure function.

You just have to define your initial state and an update function, then the loop takes care of the rest, it will call update in each iteration with the currentState and input, or it will terminate if the isGameOver property in your state is true or the user presses q.

Example usage

Install:

npm install adamgonda/loop
// in your index.js
const { run } = require('loop')

run({
  initialState: {
    player: { x: 10, y: 5, isHappy: true },
    view: { width: 20, height: 10 },
    isGameOver: false,
  },
  update: (state, input) => state,
  toCommon: (state) => [{ ...state.player, tag: 'player' }],
  renderMap: { player: 'X' },
  dimensions: { width: 20, height: 10 }
})

when you run node ./index.js it will produce a running game that looks like this 👇

{ player: { x: 10, y: 5 } }
input null
-----------------------
|                     |
|                     |
|                     |
|                     |
|                     |
|          X          |
|                     |
|                     |
|                     |
|                     |
|                     |
-----------------------

API

Loop exports one function run.

run({
  // required: starting point for a game
  // it has to contain properties:
  //   loop uses this to know when to terminate the game
  //   isGameOver: boolean
  //   
  //   loop uses this to know how big the view has to be
  //   view: { width: number, height: number }
  // example 👇
  initialState: {
    player: { x: 10, y: 5, isHappy: true },
    view: { width: 20, height: 10 },
    isGameOver: false,
  },

  // required: implements the main game logic
  // example 👇
  update: (state, input) => state,

  // required: transforms every unique game state to a common form
  // which is a list of objects with x, y and a tag property
  // example 👇
  toCommon: (state) => [{ ...state.player, tag: 'player' }],

  // required: maps tags to characters
  // example 👇
  renderMap: { player: 'X' },

  // optional, maps a numeric character from the console to a value relevant to you
  // example 👇
  // inputMap: { '119': 'UP', '115': 'DOWN' }

  // optional, function (state) => void, called before and after render
  // example 👇
  // header: (state) => console.log('This is the header'),
  // footer: (state) => console.log('This is the footer'),

  // optional, default is 100, determines time between renders
  // example 👇
  // timeBetweenRender: 50,

  // optional, default true, logs state and input
  // example 👇
  // isDebug: false
})

About

The missing game loop for your Pure function.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published