d80924cf3c521946d706c5fe30a5894b73a578df
[sixth-3d.git] / src / main / java / eu / svjatoslav / sixth / e3d / gui / humaninput / WorldNavigationUserInputTracker.java
1 /*
2  * Sixth 3D engine. Copyright ©2012-2018, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of version 3 of the GNU Lesser General Public License
6  * or later as published by the Free Software Foundation.
7  *
8  */
9
10 package eu.svjatoslav.sixth.e3d.gui.humaninput;
11
12 import eu.svjatoslav.sixth.e3d.gui.Avatar;
13 import eu.svjatoslav.sixth.e3d.gui.ViewPanel;
14 import eu.svjatoslav.sixth.e3d.gui.textEditorComponent.KeyboardHelper;
15
16 import java.awt.event.KeyEvent;
17
18 public class WorldNavigationUserInputTracker implements UserInputHandler {
19
20     @Override
21     public boolean beforeRender(final ViewPanel viewPanel,
22                                 final int millisecondsSinceLastFrame) {
23
24         trackKeys(millisecondsSinceLastFrame, viewPanel);
25         return false;
26     }
27
28     @Override
29     public boolean focusLost(final ViewPanel viewPanel) {
30         return false;
31     }
32
33     @Override
34     public boolean focusReceived(final ViewPanel viewContext) {
35         return false;
36     }
37
38     @Override
39     public boolean keyPressed(final KeyEvent event, final ViewPanel viewContext) {
40         return false;
41     }
42
43     @Override
44     public boolean keyReleased(final KeyEvent event, final ViewPanel viewContext) {
45         return false;
46     }
47
48     /**
49      * interpret currently pressed keys
50      */
51     private void trackKeys(final long millisecondsSinceLastFrame,
52                            final ViewPanel viewPanel) {
53
54         System.out.println("Track keys!");
55
56         final HIDInputTracker inputTracker = viewPanel.getHIDInputTracker();
57
58         final Avatar avatar = viewPanel.getAvatar();
59
60         final double actualAcceleration = millisecondsSinceLastFrame
61                 * avatar.avatarAcceleration
62                 * (1 + (avatar.getMovementSpeed() / 10));
63
64         if (inputTracker.isKeyPressed(KeyboardHelper.UP))
65             avatar.getMovementVector().z += actualAcceleration;
66
67         if (inputTracker.isKeyPressed(KeyboardHelper.DOWN))
68             avatar.getMovementVector().z -= actualAcceleration;
69
70         if (inputTracker.isKeyPressed(KeyboardHelper.RIGHT))
71             avatar.getMovementVector().x += actualAcceleration;
72
73         if (inputTracker.isKeyPressed(KeyboardHelper.LEFT))
74             avatar.getMovementVector().x -= actualAcceleration;
75
76         avatar.enforceSpeedLimit();
77     }
78 }