1 /** 2 Copyright: Copyright (c) 2015-2016 Andrey Penechko. 3 License: $(WEB boost.org/LICENSE_1_0.txt, Boost License 1.0). 4 Authors: Andrey Penechko. 5 */ 6 7 module voxelman.movement.plugin; 8 9 import std.experimental.logger; 10 11 import dlib.math.vector; 12 13 import pluginlib; 14 import voxelman.core.events; 15 16 import voxelman.client.plugin; 17 import voxelman.eventdispatcher.plugin; 18 import voxelman.graphics.plugin; 19 import voxelman.gui.plugin; 20 import voxelman.input.plugin; 21 import voxelman.input.keybindingmanager; 22 23 shared static this() 24 { 25 pluginRegistry.regClientPlugin(new MovementPlugin); 26 } 27 28 class MovementPlugin : IPlugin 29 { 30 ClientPlugin clientPlugin; 31 EventDispatcherPlugin evDispatcher; 32 GraphicsPlugin graphics; 33 GuiPlugin guiPlugin; 34 InputPlugin input; 35 36 bool autoMove; 37 38 mixin IdAndSemverFrom!(voxelman.movement.plugininfo); 39 40 override void registerResources(IResourceManagerRegistry resmanRegistry) 41 { 42 auto keyBindingMan = resmanRegistry.getResourceManager!KeyBindingManager; 43 keyBindingMan.registerKeyBinding(new KeyBinding(KeyCode.KEY_W, "key.forward")); 44 keyBindingMan.registerKeyBinding(new KeyBinding(KeyCode.KEY_A, "key.left")); 45 keyBindingMan.registerKeyBinding(new KeyBinding(KeyCode.KEY_S, "key.backward")); 46 keyBindingMan.registerKeyBinding(new KeyBinding(KeyCode.KEY_D, "key.right")); 47 keyBindingMan.registerKeyBinding(new KeyBinding(KeyCode.KEY_SPACE, "key.up")); 48 keyBindingMan.registerKeyBinding(new KeyBinding(KeyCode.KEY_LEFT_CONTROL, "key.down")); 49 keyBindingMan.registerKeyBinding(new KeyBinding(KeyCode.KEY_LEFT_SHIFT, "key.fast")); 50 } 51 52 override void init(IPluginManager pluginman) 53 { 54 clientPlugin = pluginman.getPlugin!ClientPlugin; 55 evDispatcher = pluginman.getPlugin!EventDispatcherPlugin; 56 graphics = pluginman.getPlugin!GraphicsPlugin; 57 guiPlugin = pluginman.getPlugin!GuiPlugin; 58 input = pluginman.getPlugin!InputPlugin; 59 60 evDispatcher.subscribeToEvent(&onPreUpdateEvent); 61 } 62 63 void onPreUpdateEvent(ref PreUpdateEvent event) 64 { 65 if(clientPlugin.mouseLocked) 66 { 67 ivec2 mousePos = input.mousePosition; 68 mousePos -= cast(ivec2)(guiPlugin.window.size) / 2; 69 70 // scale, so up and left is positive, as rotation is anti-clockwise 71 // and coordinate system is right-hand and -z if forward 72 mousePos *= -1; 73 74 if(mousePos.x !=0 || mousePos.y !=0) 75 { 76 graphics.camera.rotate(vec2(mousePos)); 77 } 78 input.mousePosition = cast(ivec2)(guiPlugin.window.size) / 2; 79 80 uint cameraSpeed = 10; 81 vec3 posDelta = vec3(0,0,0); 82 if(input.isKeyPressed("key.fast")) cameraSpeed = 60; 83 84 if(input.isKeyPressed("key.right")) posDelta.x = 1; 85 else if(input.isKeyPressed("key.left")) posDelta.x = -1; 86 87 if(input.isKeyPressed("key.forward")) posDelta.z = 1; 88 else if(input.isKeyPressed("key.backward")) posDelta.z = -1; 89 90 if(input.isKeyPressed("key.up")) posDelta.y = 1; 91 else if(input.isKeyPressed("key.down")) posDelta.y = -1; 92 93 if (posDelta != vec3(0)) 94 { 95 posDelta.normalize(); 96 posDelta *= cameraSpeed * event.deltaTime; 97 graphics.camera.moveAxis(posDelta); 98 } 99 } 100 // TODO: remove after bug is found 101 else if (autoMove) 102 { 103 // Automoving 104 graphics.camera.moveAxis(vec3(0,0,20)*event.deltaTime); 105 } 106 } 107 }