Skip to content

Instantly share code, notes, and snippets.

@timetocode
timetocode / GameComponent.js
Last active November 11, 2015 03:02
nengi.GameObject and nengi.GameComponent - prototype of component based design
function GameComponent(gameObject, config) {
this.gameObject = gameObject
this.position = gameObject.position
this.initialize()
if (config) {
this.configure(config)
}
function compareNumbers(a, b) {
return a - b
}
function arrayDiffs(a, b) {
a.sort(compareNumbers)
b.sort(compareNumbers)
//console.log('A', a)
//console.log('B', b)
// specify which properties should be networked, and their value types
Hero.prototype.netSchema = nengi.BinarySerializer.createSchema({
'id': nengi.UInt16,
'type': nengi.UInt8,
'x': nengi.Int16,
'y': nengi.Int16,
'hp': nengi.UInt8,
}, {
'x': { delta: true, binaryType: nengi.Int8 },
'y': { delta: true, binaryType: nengi.Int8 }
var BitBuffer = function(sourceOrLength) {
this.bitLength = null // length in bits (can be less than the bytes/8)
this.byteLength = null // length in bytes (atleast enough to hold the bits)
this.byteArray = null // Uint8Array holding the underlying bits and bytes
//console.log('BitBuffer ctor', sourceOrLength, typeof sourceOrLength)
if (typeof sourceOrLength === 'number') {
// create a bitBuffer with *length* bits
this.bitLength = sourceOrLength
@timetocode
timetocode / PhysicsComponent.js
Last active February 9, 2017 04:48
An entity with some of its logic in a physics and collider component, and the rest (weapon, hitpoints) just written directly in SumoShip entity
var nengi = require('../../nengi/nengi')
var Vector2 = nengi.Vector2
function PhysicsComponent(gameObject, config) {
nengi.Component.call(this, gameObject, config)
// force applied from external sources
this.externalForce = new Vector2()
// Keying
var BinaryType = {
/* basic binary types */
Int8: 0, // signed integer
UInt8: 1, // unsigned integer
/* skipped a dozen other basic binary types */
/* fancy or custom binary types */
@timetocode
timetocode / InputManager.js
Created March 10, 2016 21:55
Example of an input manager to go w/ nengi
var EventEmitter = require('../../nengi/external/EventEmitter')
function InputManager() {
EventEmitter.call(this)
// tracks the real time state of W A S D
this.W = false
this.A = false
this.S = false
this.D = false
@timetocode
timetocode / gameClient.js
Created March 10, 2016 21:59
An example nengi client
// nengi client side!
var nengi = require('../../nengi/nengi')
var common = require('../common')
var app = new nengi.Application(common)
app.shouldEmitGameState = true
// actions
var Move = require('./input/Move')
var Attack = require('./input/Attack')
@timetocode
timetocode / Existence-spec.js
Last active April 6, 2016 01:20
Example tests via jasmine.js
var Existence = require('../Existence')
describe('Existence', function() {
var existence = null
beforeEach(function() {
existence = new Existence()
})
it('can add/getById entities', function() {
@timetocode
timetocode / AABB.js
Last active February 9, 2017 04:47
point quadtree with pooling (the AABBs are pooled)
var BinarySerializer = require('../binary/BinarySerializer')
var BinaryType = require('../binary/BinaryType')
// axis-aligned bounding box
/* PARAMS: (Point)center, (Point)half */
function AABB(x, y, halfWidth, halfHeight) {
this.initialize(x, y, halfWidth, halfHeight)
}
AABB.pool = []