Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package cz.ascaria.borekboritel.controllers;
- import com.jme3.math.Quaternion;
- import com.jme3.math.Vector3f;
- import com.jme3.scene.Geometry;
- import com.jme3.scene.Node;
- import cz.ascaria.borekboritel.Main;
- import de.lessvoid.nifty.Nifty;
- import de.lessvoid.nifty.elements.Element;
- import de.lessvoid.nifty.screen.Screen;
- import de.lessvoid.nifty.tools.SizeValue;
- /**
- * Hud controller.
- * @author Ascaria
- */
- public class HudController extends BaseScreenController
- {
- protected Node camNode;
- protected Node targetNode;
- protected int gunSightHalfWidth = 0;
- protected int gunSightHalfHeight = 0;
- protected int screenHeight = 0;
- protected Vector3f enemyScreenCoords = new Vector3f();
- protected Element gunSight;
- protected Geometry target;
- @Override
- public void bind(Nifty nifty, Screen screen)
- {
- super.bind(nifty, screen);
- camNode = new Node();
- camNode.attachChild(targetNode = new Node());
- gunSight = screen.findElementByName("hud-gun-sight");
- gunSightHalfWidth = gunSight.getWidth() / 2;
- gunSightHalfHeight = gunSight.getHeight() / 2;
- screenHeight = settings.getHeight();
- lockTarget(null);
- Main.LOG.info("HudController");
- }
- public void lockTarget(Geometry target) {
- this.target = target;
- if(null == target) {
- projectGunSight(settings.getWidth() / 2, screenHeight / 2);
- }
- }
- public void update(float tpf) {
- if(null != target) {
- // Set starting position
- camNode.setLocalTranslation(cam.getLocation());
- camNode.setLocalRotation(cam.getRotation());
- // Look at enemy
- targetNode.lookAt(target.getWorldTranslation(), cam.getUp());
- float[] a = targetNode.getLocalRotation().toAngles(null);
- // Limit yaw
- if(a[1] > cam.getFrustumRight()) {
- a[1] = cam.getFrustumRight();
- } else if(a[1] < cam.getFrustumLeft()) {
- a[1] = cam.getFrustumLeft();
- }
- // Limit pitch
- if(a[0] > cam.getFrustumTop()) {
- a[0] = cam.getFrustumTop();
- } else if(a[0] < cam.getFrustumBottom()) {
- a[0] = cam.getFrustumBottom();
- }
- // TODO: left and right side of the screen is correct, top and bottom not.
- // TODO: if we are exactly backwards or upright on Y axis, crosshair is blinking in two positions, super twinposition :)
- targetNode.setLocalRotation(new Quaternion().fromAngles(a));
- // Move towards enemy slightly, less Z = more shaking less twinposition, more Z = no shaking more twinposition
- targetNode.setLocalTranslation(targetNode.getLocalRotation().mult(new Vector3f(0f, 0f, 0.5f)));
- cam.getScreenCoordinates(targetNode.getWorldTranslation(), enemyScreenCoords);
- projectGunSight(enemyScreenCoords.x, enemyScreenCoords.y);
- }
- }
- protected void projectGunSight(float x, float y) {
- if(null != gunSight) {
- gunSight.setConstraintX(SizeValue.px((int)x - gunSightHalfWidth));
- gunSight.setConstraintY(SizeValue.px(screenHeight - (int)y - gunSightHalfHeight));
- gunSight.getParent().layoutElements();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement