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 voxelman.math;
12 
13 import pluginlib;
14 import voxelman.core.events;
15 
16 import voxelman.client.plugin;
17 import voxelman.config.configmanager : ConfigOption, ConfigManager;
18 import voxelman.eventdispatcher.plugin;
19 import voxelman.graphics.plugin;
20 import voxelman.gui.plugin;
21 import voxelman.input.plugin;
22 import voxelman.input.keybindingmanager;
23 
24 shared static this()
25 {
26 	pluginRegistry.regClientPlugin(new MovementPlugin);
27 }
28 
29 class MovementPlugin : IPlugin
30 {
31 	ClientPlugin clientPlugin;
32 	EventDispatcherPlugin evDispatcher;
33 	GraphicsPlugin graphics;
34 	GuiPlugin guiPlugin;
35 	InputPlugin input;
36 
37 	bool autoMove;
38 	ConfigOption cameraSpeedOpt;
39 	ConfigOption cameraBoostOpt;
40 
41 	mixin IdAndSemverFrom!(voxelman.movement.plugininfo);
42 
43 	override void registerResources(IResourceManagerRegistry resmanRegistry)
44 	{
45 		auto keyBindingMan = resmanRegistry.getResourceManager!KeyBindingManager;
46 		keyBindingMan.registerKeyBinding(new KeyBinding(KeyCode.KEY_W, "key.forward"));
47 		keyBindingMan.registerKeyBinding(new KeyBinding(KeyCode.KEY_A, "key.left"));
48 		keyBindingMan.registerKeyBinding(new KeyBinding(KeyCode.KEY_S, "key.backward"));
49 		keyBindingMan.registerKeyBinding(new KeyBinding(KeyCode.KEY_D, "key.right"));
50 		keyBindingMan.registerKeyBinding(new KeyBinding(KeyCode.KEY_SPACE, "key.up"));
51 		keyBindingMan.registerKeyBinding(new KeyBinding(KeyCode.KEY_LEFT_CONTROL, "key.down"));
52 		keyBindingMan.registerKeyBinding(new KeyBinding(KeyCode.KEY_LEFT_SHIFT, "key.fast"));
53 		keyBindingMan.registerKeyBinding(new KeyBinding(KeyCode.KEY_KP_ADD, "key.speed_up", null, &speedUp));
54 		keyBindingMan.registerKeyBinding(new KeyBinding(KeyCode.KEY_KP_SUBTRACT, "key.speed_down", null, &speedDown));
55 
56 		ConfigManager config = resmanRegistry.getResourceManager!ConfigManager;
57 		cameraSpeedOpt = config.registerOption!int("camera_speed", 20);
58 		cameraBoostOpt = config.registerOption!int("camera_boost", 5);
59 	}
60 
61 	override void init(IPluginManager pluginman)
62 	{
63 		clientPlugin = pluginman.getPlugin!ClientPlugin;
64 		evDispatcher = pluginman.getPlugin!EventDispatcherPlugin;
65 		graphics = pluginman.getPlugin!GraphicsPlugin;
66 		guiPlugin = pluginman.getPlugin!GuiPlugin;
67 		input = pluginman.getPlugin!InputPlugin;
68 
69 		evDispatcher.subscribeToEvent(&onPreUpdateEvent);
70 	}
71 
72 	void speedUp(string) {
73 		cameraSpeedOpt.set(clamp(cameraSpeedOpt.get!uint + 1, 1, 20));
74 	}
75 	void speedDown(string)	 {
76 		cameraSpeedOpt.set(clamp(cameraSpeedOpt.get!uint - 1, 1, 20));
77 	}
78 
79 	void onPreUpdateEvent(ref PreUpdateEvent event)
80 	{
81 		if(clientPlugin.mouseLocked)
82 		{
83 			ivec2 mousePos = input.mousePosition;
84 			mousePos -= cast(ivec2)(guiPlugin.window.size) / 2;
85 
86 			// scale, so up and left is positive, as rotation is anti-clockwise
87 			// and coordinate system is right-hand and -z if forward
88 			mousePos *= -1;
89 
90 			if(mousePos.x !=0 || mousePos.y !=0)
91 			{
92 				graphics.camera.rotate(vec2(mousePos));
93 			}
94 			input.mousePosition = cast(ivec2)(guiPlugin.window.size) / 2;
95 
96 			uint cameraSpeed = cameraSpeedOpt.get!uint;
97 			vec3 posDelta = vec3(0,0,0);
98 			if(input.isKeyPressed("key.fast")) cameraSpeed *= cameraBoostOpt.get!uint;
99 
100 			if(input.isKeyPressed("key.right")) posDelta.x = 1;
101 			else if(input.isKeyPressed("key.left")) posDelta.x = -1;
102 
103 			if(input.isKeyPressed("key.forward")) posDelta.z = 1;
104 			else if(input.isKeyPressed("key.backward")) posDelta.z = -1;
105 
106 			if(input.isKeyPressed("key.up")) posDelta.y = 1;
107 			else if(input.isKeyPressed("key.down")) posDelta.y = -1;
108 
109 			if (posDelta != vec3(0))
110 			{
111 				posDelta.normalize();
112 				posDelta *= cameraSpeed * event.deltaTime;
113 				graphics.camera.moveAxis(posDelta);
114 			}
115 		}
116 		// TODO: remove after bug is found
117 		else if (autoMove)
118 		{
119 			// Automoving
120 			graphics.camera.moveAxis(vec3(0,0,20)*event.deltaTime);
121 		}
122 	}
123 }