Skip to content

Instantly share code, notes, and snippets.

@MrSmith33
Created November 28, 2016 13:46
Show Gist options
  • Save MrSmith33/6380152b52b17b08ffcbebc632ccd522 to your computer and use it in GitHub Desktop.
Save MrSmith33/6380152b52b17b08ffcbebc632ccd522 to your computer and use it in GitHub Desktop.
bytecode_viz
module bytecodeplugin;
// lib
import std.datetime : MonoTime, Duration, msecs, dur;
import voxelman.log;
import voxelman.math;
import voxelman.geometry.box;
import pluginlib;
// plugins
import voxelman.eventdispatcher.plugin;
import voxelman.graphics.plugin;
import voxelman.world.clientworld;
import voxelman.movement.plugin;
// engine stuff
import voxelman.block.utils;
import voxelman.core.config;
import voxelman.core.events;
import voxelman.world.gen.utils;
import voxelman.world.storage;
shared static this()
{
// register our plugin
pluginRegistry.regClientPlugin(new ByteCodePlugin);
}
enum MAX_BORDERS = Box(ivec3(-int.max/2, -int.max/2, -int.max/2), ivec3(int.max, int.max, int.max));
class ByteCodePlugin : IPlugin
{
override string id() @property { return "bytecodeplugin"; }
override string semver() @property { return "1.0.0"; }
ClientWorld world;
GraphicsPlugin graphics;
MovementPlugin movement;
// cursor geometry will be written here
Batch batch;
enum ticksPerSecond = 5;
Duration tickTime = (1000 / ticksPerSecond).msecs;
MonoTime prevTime;
override void registerResourceManagers(void delegate(IResourceManager) registerHandler){}
override void registerResources(IResourceManagerRegistry resmanRegistry){}
override void preInit() {}
override void init(IPluginManager pluginman)
{
// gather plugins
world = pluginman.getPlugin!ClientWorld;
graphics = pluginman.getPlugin!GraphicsPlugin;
movement = pluginman.getPlugin!MovementPlugin;
auto evDispatcher = pluginman.getPlugin!EventDispatcherPlugin;
// event handlers
evDispatcher.subscribeToEvent(&drawCursor);
evDispatcher.subscribeToEvent(&onUpdateEvent);
}
override void postInit()
{
enum AREA_SIZE = 20;
Box borders = Box(ivec3(-AREA_SIZE/2, -AREA_SIZE/2, 0), ivec3(AREA_SIZE, AREA_SIZE, 1));
auto worldBox = WorldBox(borders, DimensionId(0));
// set borders for current dimension, so 1-chunk layer will be rendered
world.setDimensionBorders(DimensionId(0), borders);
// load chunks
world.chunkObserverManager.addServerObserverBox(worldBox, MAX_BORDERS);
foreach(ChunkWorldPos cwp; worldBox)
{
ChunkLayerItem[1] layers;
// chunk filled with air
layers[0] = ChunkLayerItem(StorageType.uniform, 0, BLOCKID_UNIFORM_FILL_BITS, 0, AIR, TRANSPARENT_CHUNK_METADATA);
world.onChunkLoaded(cwp, layers);
}
// set blocks
setBlock(0,0, GRASS);
setBlock(1,1, LAVA);
setBlock(3,3, SAND);
// notify world for chunk remesh (whole modified area in blocks)
auto modifiedBlockBox = WorldBox(ivec3(0,0,1), ivec3(3,3,1), DimensionId(0));
world.onBlockBoxChanged(modifiedBlockBox);
// prevent falling
movement.isFlying = true;
// setup camera
graphics.camera.position = vec3(0, 0, -10);
graphics.camera.setHeading(vec2(180, 0));
// tick times
prevTime = MonoTime.currTime;
}
void setBlock(int x, int y, BlockId blockId)
{
world.worldAccess.setBlock(BlockWorldPos(x, y, 1, 0), blockId);
}
void onUpdateEvent(ref UpdateEvent event)
{
// tick
MonoTime newTime = MonoTime.currTime;
Duration delta = (newTime - prevTime);
if (delta > tickTime)
{
tick();
prevTime = newTime;
}
// update cursor (filled cube)
batch.reset();
batch.putCube(vec3(cursorPos.x, cursorPos.y,1), vec3(1,1,1), Color4ub(255, 20, 120), true);
}
// simulation
enum CursorState {
up,
left,
down,
right
}
ivec2 cursorPos = ivec2(0, 0);
CursorState cursorState = CursorState.up;
void tick()
{
final switch(cursorState) with(CursorState)
{
case up:
++cursorPos.y;
if (cursorPos.y >= 5)
cursorState = left;
break;
case left:
--cursorPos.x;
if (cursorPos.x <= -5)
cursorState = down;
break;
case down:
--cursorPos.y;
if (cursorPos.y <= -5)
cursorState = right;
break;
case right:
++cursorPos.x;
if (cursorPos.x >= 5)
cursorState = up;
break;
}
}
void drawCursor(ref RenderSolid3dEvent event)
{
graphics.draw(batch);
}
}
module main;
import pluginlib;
import pluginlib.pluginmanager;
import voxelman.log;
import voxelman.cons;
void main(string[] args) {
import enginestarter;
EngineStarter engineStarter;
engineStarter.setupLogs(EngineStarter.AppType.client, "./");
scope(exit) closeBinLog();
auto pluginman = new PluginManager;
foreach(p; pluginRegistry.clientPlugins.byValue)
{
if (p.id != "voxelman.chat")
pluginman.registerPlugin(p);
}
pluginman.initPlugins();
pluginRegistry.clientMain(args);
engineStarter.waitForThreads();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment