28d485c4ed9e609f2425e7aaabc41eab0037d541
[sixth-3d.git] / src / main / java / eu / svjatoslav / sixth / e3d / gui / humaninput / WorldNavigationTracker.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 WorldNavigationTracker 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 true;
41     }
42
43     @Override
44     public boolean keyReleased(final KeyEvent event, final ViewPanel viewContext) {
45         return true;
46     }
47
48     /**
49      * interpret currently pressed keys
50      */
51     public void trackKeys(final long millisecondsSinceLastFrame,
52                           final ViewPanel viewContext) {
53
54         final UserInputTracker inputTracker = viewContext.getUserInputTracker();
55
56         final Avatar avatar = viewContext.getAvatar();
57
58         final double actualAcceleration = millisecondsSinceLastFrame
59                 * avatar.avatarAcceleration
60                 * (1 + (avatar.getMovementSpeed() / 10));
61
62         if (inputTracker.isKeyPressed(KeyboardHelper.UP))
63             avatar.getMovementVector().z += actualAcceleration;
64
65         if (inputTracker.isKeyPressed(KeyboardHelper.DOWN))
66             avatar.getMovementVector().z -= actualAcceleration;
67
68         if (inputTracker.isKeyPressed(KeyboardHelper.RIGHT))
69             avatar.getMovementVector().x += actualAcceleration;
70
71         if (inputTracker.isKeyPressed(KeyboardHelper.LEFT))
72             avatar.getMovementVector().x -= actualAcceleration;
73
74         avatar.enforceSpeedLimit();
75     }
76 }