Skip to content

Instantly share code, notes, and snippets.

@timetocode
timetocode / index.js
Created December 6, 2019 23:19
node & rust 500,000 entity iteration
const createEntity = (nid, x, y, name) => {
return {
nid,
x,
y,
hp: 100,
name
}
}
use std::time::Instant;
#[derive(Debug)]
struct Entity {
nid: u32,
x: f64,
y: f64,
hp: u8,
name: String
}
@timetocode
timetocode / childBot.js
Created August 4, 2019 22:18
nengi bot example using multiple cpus
const glue = require('./glue')
const nengi = glue.nengi.default
const nengiConfig = glue.nengiConfig.default
const PlayerInput = glue.PlayerInput.default
// bots need to all share the same protocols
const protocolMap = new nengi.ProtocolMap(nengiConfig, nengi.metaConfig)
//var address = 'ws://localhost:8001'
const address = null // put some url here
body {
background-color:#423b41;
}
.wrap {
display: flex;
}
.solo {
border: solid 4px #ebfff9;
@timetocode
timetocode / default.conf
Last active June 26, 2019 22:17
Nginx config and ubuntu systemd services for hosting multiple game instances on ports 8001 through 8005, proxied from urls like wss://subdomain.domain.io/1 through wss://subdomain.domain.io/5; can also remove the ssl and listen on port 80 instead. The service file is an example, but creating 5 called instance1.service through instance5.service a…
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
listen 443 ssl;
ssl_certificate /srv/certs/fullchain.pem;
ssl_certificate_key /srv/certs/privkey.pem;
@timetocode
timetocode / transfer-example.js
Created June 1, 2019 19:37
Example of transferring a player between two nengi instances while maintaining an authoritative server. One instance registers some information with the other instance, and provides the client with a token which it uses to be reconnected with that information after connecting to the other instance.
// pseudocode for transfers if game has a practically infinite number of instances.
// From one of the game servers
transfer(client, toInstance) {
toInstance.someWebApi.request(
{ transfer: { token: 'abc123', data }},
(res) => {
if (res.transferAccepted) {
instance.message(new TransferMessage(address, token)), client)
client.disconnect()
}
@timetocode
timetocode / PlayerCharacter.js
Created June 1, 2019 06:14
babylon.js + nengi.js entity example. Note: one can write the same thing in more of an ECS style (instead of regular oop) if desired by moving all of the methods off of PlayerCharacter and into systems
import nengi from 'nengi'
import * as BABYLON from 'babylonjs'
class PlayerCharacter {
constructor(scene) {
this.name = 'anon'
this.mesh = new BABYLON.Mesh('dummy', scene)
this.mesh.entity = this // for collisions within babylon
this.hitpoints = 100
// velocity from own impulses
@timetocode
timetocode / SoundAtlas.js
Last active May 6, 2019 09:41
Pooled sounds for Babylon.js
let basePath = 'http://localhost:8086'
if (typeof window === 'undefined') {
basePath = 'http://localhost:8086/'
}
if (process && process.env && process.env.NODE_ENV === 'production') {
basePath = `https://somedomain.io/`
}
const sounds = new Map()
@timetocode
timetocode / Score.message.js
Created April 18, 2019 00:02
nengi score system example,
import nengi from '../../nengi';
class Score {
constructor(score) {
this.entityId = score.entityId
this.kills = score.kills
this.assists = score.assists
this.damage = score.damage
this.deaths = score.deaths
}
@timetocode
timetocode / instance.js
Created October 17, 2018 01:00
Example of using tls-json + express to create a master server listing service. master.js uses tls-json to listen over tls and accept connections + listing data from other services that connect, meanwhile instance.js shows a snippet of a game server that is listing itself on the master server. server.json is an example of what the http call to th…
// from the game instance
const master = new TLSClient({
options: {
ca: fs.readFileSync(config.API_CERT),
},
reconnectInterval: 2000,
host: config.MASTER_API_HOST,
port: config.MASTER_API_PORT,
password: config.API_PASSWORD