Skip to content

Instantly share code, notes, and snippets.

@geggleto
Created December 8, 2019 06:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geggleto/a53c393f0d0d24eec0fab23aa4b1ee8b to your computer and use it in GitHub Desktop.
Save geggleto/a53c393f0d0d24eec0fab23aa4b1ee8b to your computer and use it in GitHub Desktop.
Crypto Voxel Scripting

CryptoVoxel Scripting Engine Library

Superduper proof of concept messaging mechanism for CryptoVoxels

Goals

  1. Teleporters
  2. Stretch goal .... Battle Bot Pit with Players controlling their bots for all to watch

Facets

  1. Game Engine Integration
  2. Scripting Engine Integration

Communication Pathways

The game engine runs inside the browser on the cryptovoxel.com domain. Parcel Scripts are loaded on a subdomain inside a sandboxed iframe. Messages are exchanged between the game engine and the sandbox using the postMessage browser API.

With the 2.18 client the following RPC calls are implemented:

  • create
  • update
  • snapshot
  • destroy
  • chat
  • play
  • animate

Event Contexts

Currently the Parcel Scripts are executed in the context of a single user. There is no communication between people inside the parcel

Websockets & Security

Currently the game engine only supports sending move updates via websockets. While there is an active WebSocket connection on the cryptovoxel.com domain, there isn't one inside the parcelScript. Additionally CSP headers block externsal scripts from loading inside parcel script.

Proposed Changes

The ParcelScript class is where we are going to target some changes. ParcelScript renders the iframe in the browser and sets up event listeners and acts as a proxy to the websocket for certain events. For those who want to follow along search the compiled client file for class ParcelScript it should be found on line 107835

Change 1 - Add Teleport RPC type

This change would go inside the game engine client code.

// ...
else if("teleport"===e.type){
  this.updatePacket={type:"move",rotation:Array(3),position:Array(3)};
  this.updatePacket.rotation = e.targetRotation;
  this.updatePacket.position = e.targetPosition;
  this.send(this.updatePacket);
}

This function allows the ParcelScript to send the player to a different predefined position.

This could would go into a ParcelScript

//Find the target location by defining another feature inside the game world.

let teleportReceiver = parcel.getFeatureById("teleport_receiver");
//feature is locally bound; it is the object in the game world you clicked on
feature.on('click', (e) => {
  //parcel is in the global scope and refers to the ParcelScript object that you are contained in
  parcel.broadcast({
    type : "teleport",
    targetRotation : teleportReceiver.rotation,
    targetPosition : teleportReceiver.position
  })
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment