Skip to content
This repository has been archived by the owner on Jan 24, 2021. It is now read-only.

gamestdio/timeline

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@gamestdio/timeline Build Status

Greenkeeper badge

Timelines implementation for lag compensation techniques in networked games. Use it both on server and/or client-side according to your needs.

  • Take snapshots of game state
  • Interpolates and extrapolates values based on snapshots taken.

API

  • createTimeline([ maxSnapshots=10 ]) - Create a Timeline instance
  • Timeline#takeSnapshot( state[, elapsedMs ] ) - Record state on timeline history
  • Timeline#at( elapsedMs[, interpolate=true ] ) - Get Proxy object containing interpolated values between recorded states.
  • Timeline#offset( elapsedMs[, interpolate=true ] ) - Same as #at, but from the last snapshot taken.

Example

import { createTimeline } from '@gamestdio/timeline'

var timeline = createTimeline()

// first game state snapshot
timeline.takeSnapshot({
  player: { x: 100 },
  enemy: { x: 0 }
})

setTimeout(() => {
  // take second game state snapshot after 1000ms

  timeline.takeSnapshot({
    player: { x: 0 },
    enemy: { x: 100 }
  })

  //
  // Retrieving a previous state
  //
  console.log( timeline.at( 0 ).player.x )
  // => 100
  console.log( timeline.at( 0 ).enemy.x )
  // => 0

  //
  // Interpolating data from known states
  //
  console.log( timeline.at( 500 ).player.x )
  // => 49.75124378109453
  console.log( timeline.at( 500 ).enemy.x )
  // => 50.24875621890547

  //
  // Extrapolating data between an unknown state
  //
  console.log( timeline.at( 1500 ).player.x )
  // => -49.25373134328358
  console.log( timeline.at( 1500 ).enemy.x )
  // => 149.2537313432836

}, 1000)

To execute this example run the following command:

node --harmony --harmony-proxies example/usage.js

References

License

MIT