Skip to content

Instantly share code, notes, and snippets.

@timetocode
timetocode / master.js
Created October 17, 2018 00:47
Example of using tls-json + express to create a master server listing service.
const fs = require('fs')
const express = require('express')
const config = require('config')
const TLSServer = require('tls-json').Server
const Router = require('express-promise-router')
const servers = require('../servers')
const app = express()
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
@timetocode
timetocode / Grid.js
Created September 16, 2017 00:57
spatial rid w/ raycast as a search option (can fill the grid with objects or arrays instead of a specific value)
function length(x,y) {
return Math.sqrt((x * x) + (y * y))
}
function norm(x ,y) {
var len = length(x, y)
if (len > 0) {
return { x: x/len, y: y/len }
} else {
return { x: 0, y: 0 }
var Floor = require('../map/Floor')
var Wall = require('../map/Wall')
function createKey(x, y) {
return x + y * 10000000
}
function keyInBounds(key, gx, gy, halfWidth, halfHeight) {
var keyX = key % 10000000
var keyY = Math.floor(key / 10000000)
@timetocode
timetocode / nengi+TileMap-misc-examples.js
Last active June 15, 2019 19:32
Demonstration of representing 2D tile maps as 1D arrays and sending their data via nengi. Includes a tilemap which is a "2d array"(not really) of integers, and examples for a "2d array" of objects.
var TileEnum = {
EMPTY: 0,
GRASS: 1,
DIRT: 2,
STONEWALL: 3
}
class TileMap {
constructor(width, height) {
@timetocode
timetocode / BoidSys.js
Created February 28, 2017 07:13
A rendition of https://github.com/hughsk/boids used in http://sharkz.io/ (~v0.1.3 of sharkz)
function hypot(a, b) {
a = Math.abs(a)
b = Math.abs(b)
var lo = Math.min(a, b)
var hi = Math.max(a, b)
return hi + 3 * lo / 32 + Math.max(0, 2 * lo - hi) / 8 + Math.max(0, 4 * lo - hi) / 16
}
function BoidSys(opts) {
@timetocode
timetocode / proxy.js
Created December 20, 2016 07:48
A node-based proxy server example. Here 8 game servers are listening for websocket connections (ports 8000-8007), a file server that serves the game's assests is listening on 7999, and a small app that creates a list of the servers is running on 8080. Externally all of these services are exposed on port 80 and different urls. /0 through /7 are t…
var http = require('http')
var httpProxy = require('http-proxy')
var servers = {
'/0': 'ws://127.0.0.1:8000',
'/1': 'ws://127.0.0.1:8001',
'/2': 'ws://127.0.0.1:8002',
'/3': 'ws://127.0.0.1:8003',
'/4': 'ws://127.0.0.1:8004',
'/5': 'ws://127.0.0.1:8005',
@timetocode
timetocode / Client.js
Last active February 9, 2017 04:44
Example of using nengi.js. NOTE: the clientside rendering code is is skipped, and this example primarily highlights where your game code could use nengi.. the serverside game logic is more revealing (and messy)
var nengi = require('../../nengi')
var EDictionary = require('../../nengi/external/EDictionary')
var protocols = require('../protocols')
// Graphics code
var Squid = require('./entity/Squid')
var Fish = require('./entity/Fish')
var Shark = require('./entity/Shark')
var Ink = require('./effect/Ink')
var Blood = require('./effect/Blood')
@timetocode
timetocode / GameScene.js
Created November 15, 2016 20:34
Scene management, loading assets, beginning a game loop
var Client = require('../Client')
function GameScene() {
this.client = new Client()
}
GameScene.prototype.enterScene = function() {
console.log('GameScene entered')
this.client.connect()
}
var nengi = require('../../../nengi/nengi')
var common = require('../../common')
var Renderer = require('../renderer/Renderer')
var InputSystem = require('../input/InputSystem')
function GameScene(ip, port) {
this.ip = ip
this.port = port
MapVisual.prototype.recenter = function(me) {
var gridCoord = this.map.worldCoordsToGridCoords(me.x, me.y)
if (this.previousX !== gridCoord.x || this.previousY !== gridCoord.y) {
var dx = gridCoord.x - this.previousX
var dy = gridCoord.y - this.previousY
// start