IN THIS ARTICLE
I recently stumbled across the awesome Voxeljs library which allows anybody to create Minecraft style games (Voxel) that run in the browser with NodeJS. In this tutorial, we’ll walk you through how to develop multiplayer Voxeljs to build a DIY Minecraft!
I decided I was going to take an afternoon to add the power of PubNub to Voxeljs, creating the basis for an MMO version of Minecraft. Want to see it in action and go for a stroll around? Here’s the multiplayer Voxeljs demo.
Getting Started with Multiplayer Voxeljs
I started with the Voxel Hello World example. It didn’t include any textures, so I got them from the Occulus Rift demo.
I modified the “hello world” example to include our standard PubNub initialization.
First I added the script into index.html.
And then I initialize PubNub. In this special case the PUBNUB
object needs to be referenced by window.PUBNUB
.
Then I created a User
object and list of Users
so I could keep track of who was online and what their position in game was.
I then pubnub.subscribe
, and add a player when I get a join event. I subscribe to notifications on our channel, and update the player position when I receive a message.
All that’s left is to send out player positions! I use setInterval()
to publish the player position every second.
That’s it! It’s very crude but was easy enough to set up in an afternoon. We can open two windows and see the player position update in real time. It wouldn’t take much to add smoother animations and the ability to place and delete blocks.
Want to see it in action? Check out the live multiplayer Voxeljs demo here, or watch the quick overview video below:
Get Started
Sign up for free and use PubNub to power multiplayer games