SHOW:
|
|
- or go back to the newest paste.
| 1 | /* | |
| 2 | * To change this template, choose Tools | Templates | |
| 3 | * and open the template in the editor. | |
| 4 | */ | |
| 5 | package cz.ascaria.borekboritel.controllers; | |
| 6 | ||
| 7 | import com.jme3.math.Quaternion; | |
| 8 | import com.jme3.math.Vector3f; | |
| 9 | import com.jme3.scene.Geometry; | |
| 10 | import com.jme3.scene.Node; | |
| 11 | import cz.ascaria.borekboritel.Main; | |
| 12 | import de.lessvoid.nifty.Nifty; | |
| 13 | import de.lessvoid.nifty.elements.Element; | |
| 14 | import de.lessvoid.nifty.screen.Screen; | |
| 15 | import de.lessvoid.nifty.tools.SizeValue; | |
| 16 | ||
| 17 | /** | |
| 18 | * Hud controller. | |
| 19 | * @author Ascaria | |
| 20 | */ | |
| 21 | public class HudController extends BaseScreenController | |
| 22 | {
| |
| 23 | protected Node camNode; | |
| 24 | protected Node targetNode; | |
| 25 | ||
| 26 | protected int gunSightHalfWidth = 0; | |
| 27 | protected int gunSightHalfHeight = 0; | |
| 28 | protected int screenHeight = 0; | |
| 29 | protected Vector3f enemyScreenCoords = new Vector3f(); | |
| 30 | ||
| 31 | protected Element gunSight; | |
| 32 | protected Geometry target; | |
| 33 | ||
| 34 | @Override | |
| 35 | public void bind(Nifty nifty, Screen screen) | |
| 36 | {
| |
| 37 | super.bind(nifty, screen); | |
| 38 | ||
| 39 | camNode = new Node(); | |
| 40 | camNode.attachChild(targetNode = new Node()); | |
| 41 | ||
| 42 | gunSight = screen.findElementByName("hud-gun-sight");
| |
| 43 | gunSightHalfWidth = gunSight.getWidth() / 2; | |
| 44 | gunSightHalfHeight = gunSight.getHeight() / 2; | |
| 45 | ||
| 46 | screenHeight = settings.getHeight(); | |
| 47 | ||
| 48 | lockTarget(null); | |
| 49 | ||
| 50 | Main.LOG.info("HudController");
| |
| 51 | } | |
| 52 | ||
| 53 | public void lockTarget(Geometry target) {
| |
| 54 | this.target = target; | |
| 55 | if(null == target) {
| |
| 56 | projectGunSight(settings.getWidth() / 2, screenHeight / 2); | |
| 57 | } | |
| 58 | } | |
| 59 | ||
| 60 | public void update(float tpf) {
| |
| 61 | if(null != target) {
| |
| 62 | // Set starting position | |
| 63 | camNode.setLocalTranslation(cam.getLocation()); | |
| 64 | camNode.setLocalRotation(cam.getRotation()); | |
| 65 | ||
| 66 | // Look at enemy | |
| 67 | - | targetNode.lookAt(target.getWorldTranslation(), cam.getUp().add(cam.getLeft()).normalize()); |
| 67 | + | targetNode.lookAt(target.getWorldTranslation(), cam.getUp()); |
| 68 | float[] a = targetNode.getLocalRotation().toAngles(null); | |
| 69 | ||
| 70 | // Limit yaw | |
| 71 | if(a[1] > cam.getFrustumRight()) {
| |
| 72 | a[1] = cam.getFrustumRight(); | |
| 73 | } else if(a[1] < cam.getFrustumLeft()) {
| |
| 74 | a[1] = cam.getFrustumLeft(); | |
| 75 | } | |
| 76 | ||
| 77 | // Limit pitch | |
| 78 | if(a[0] > cam.getFrustumTop()) {
| |
| 79 | a[0] = cam.getFrustumTop(); | |
| 80 | } else if(a[0] < cam.getFrustumBottom()) {
| |
| 81 | a[0] = cam.getFrustumBottom(); | |
| 82 | } | |
| 83 | ||
| 84 | // TODO: left and right side of the screen is correct, top and bottom not. | |
| 85 | // TODO: if we are exactly backwards or upright on Y axis, crosshair is blinking in two positions, super twinposition :) | |
| 86 | targetNode.setLocalRotation(new Quaternion().fromAngles(a)); | |
| 87 | ||
| 88 | // Move towards enemy slightly, less Z = more shaking less twinposition, more Z = no shaking more twinposition | |
| 89 | targetNode.setLocalTranslation(targetNode.getLocalRotation().mult(new Vector3f(0f, 0f, 0.5f))); | |
| 90 | ||
| 91 | cam.getScreenCoordinates(targetNode.getWorldTranslation(), enemyScreenCoords); | |
| 92 | projectGunSight(enemyScreenCoords.x, enemyScreenCoords.y); | |
| 93 | } | |
| 94 | } | |
| 95 | ||
| 96 | protected void projectGunSight(float x, float y) {
| |
| 97 | if(null != gunSight) {
| |
| 98 | gunSight.setConstraintX(SizeValue.px((int)x - gunSightHalfWidth)); | |
| 99 | gunSight.setConstraintY(SizeValue.px(screenHeight - (int)y - gunSightHalfHeight)); | |
| 100 | gunSight.getParent().layoutElements(); | |
| 101 | } | |
| 102 | } | |
| 103 | } |