Removed ViewListener interface. Renamed View to ViewPanel.
[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.ViewContext;
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 ViewContext viewContext,
22                                 final int millisecondsSinceLastFrame) {
23
24         trackKeys(millisecondsSinceLastFrame, viewContext);
25         return false;
26     }
27
28     @Override
29     public void focusLost(final ViewContext viewContext) {
30     }
31
32     @Override
33     public void focusReceived(final ViewContext viewContext) {
34     }
35
36     @Override
37     public void keyPressed(final KeyEvent event, final ViewContext viewContext) {
38     }
39
40     @Override
41     public void keyReleased(final KeyEvent event, final ViewContext viewContext) {
42     }
43
44     /**
45      * interpret currently pressed keys
46      */
47     public void trackKeys(final long millisecondsSinceLastFrame,
48                           final ViewContext viewContext) {
49
50         final UserInputTracker inputTracker = viewContext.getUserInputTracker();
51
52         final Avatar avatar = viewContext.getAvatar();
53
54         final double actualAcceleration = millisecondsSinceLastFrame
55                 * avatar.avatarAcceleration
56                 * (1 + (avatar.getMovementSpeed() / 10));
57
58         if (inputTracker.isKeyPressed(KeyboardHelper.UP))
59             avatar.getMovementVector().z += actualAcceleration;
60
61         if (inputTracker.isKeyPressed(KeyboardHelper.DOWN))
62             avatar.getMovementVector().z -= actualAcceleration;
63
64         if (inputTracker.isKeyPressed(KeyboardHelper.RIGHT))
65             avatar.getMovementVector().x += actualAcceleration;
66
67         if (inputTracker.isKeyPressed(KeyboardHelper.LEFT))
68             avatar.getMovementVector().x -= actualAcceleration;
69
70         avatar.enforceSpeedLimit();
71     }
72 }