Advertisement
Guest User

Untitled

a guest
Aug 7th, 2012
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.47 KB | None | 0 0
  1. package hello;
  2.  
  3. import java.io.IOException;
  4. import com.jme3.export.JmeExporter;
  5. import com.jme3.export.JmeImporter;
  6. import com.jme3.input.InputManager;
  7. import com.jme3.input.KeyInput;
  8. import com.jme3.input.controls.ActionListener;
  9. import com.jme3.input.controls.KeyTrigger;
  10. import com.jme3.math.Vector3f;
  11. import com.jme3.renderer.Camera;
  12. import com.jme3.renderer.RenderManager;
  13. import com.jme3.renderer.ViewPort;
  14. import com.jme3.scene.Spatial;
  15. import com.jme3.scene.control.Control;
  16.  
  17. public class RtsCam implements Control, ActionListener {
  18.  
  19.     public enum Degree {
  20.         SIDE,
  21.         FWD,
  22.         ROTATE,
  23.         TILT,
  24.         DISTANCE
  25.     }
  26.      
  27.     private InputManager inputManager;
  28.     private final Camera cam;
  29.  
  30.     private int[] direction = new int[5];
  31.     private float[] accelPeriod = new float[5];
  32.  
  33.     private float[] maxSpeed = new float[5];
  34.     private float[] maxAccelPeriod = new float[5];
  35.     private float[] minValue = new float[5];
  36.     private float[] maxValue = new float[5];
  37.  
  38.     private Vector3f position = new Vector3f();
  39.      
  40.     private Vector3f center = new Vector3f();
  41.     private float tilt = (float)(Math.PI / 4);
  42.     private float rot = 0;
  43.     private float distance = 15;
  44.      
  45.     private static final int SIDE = Degree.SIDE.ordinal();
  46.     private static final int FWD = Degree.FWD.ordinal();
  47.     private static final int ROTATE = Degree.ROTATE.ordinal();
  48.     private static final int TILT = Degree.TILT.ordinal();
  49.     private static final int DISTANCE = Degree.DISTANCE.ordinal();
  50.      
  51.     public RtsCam(Camera cam, Spatial target) {
  52.         this.cam = cam;
  53.          
  54.         setMinMaxValues(Degree.SIDE, Float.NEGATIVE_INFINITY, Float.POSITIVE_INFINITY);
  55.         setMinMaxValues(Degree.FWD, Float.NEGATIVE_INFINITY, Float.POSITIVE_INFINITY);
  56.         setMinMaxValues(Degree.ROTATE, Float.NEGATIVE_INFINITY, Float.POSITIVE_INFINITY);
  57.         setMinMaxValues(Degree.TILT, 0.2f, (float)(Math.PI / 2) - 0.001f);
  58.         setMinMaxValues(Degree.DISTANCE, 2, Float.POSITIVE_INFINITY);
  59.  
  60.         setMaxSpeed(Degree.SIDE,10f,0.2f);
  61.         setMaxSpeed(Degree.FWD,10f,0.2f);
  62.         setMaxSpeed(Degree.ROTATE,2f,0.2f);
  63.         setMaxSpeed(Degree.TILT,1f,0.2f);
  64.         setMaxSpeed(Degree.DISTANCE,15f,0.2f);
  65.                 target.addControl(this);
  66.     }
  67.  
  68.     public void setMaxSpeed(Degree deg, float maxSpd, float accelTime) {
  69.         maxSpeed[deg.ordinal()] = maxSpd/accelTime;
  70.         maxAccelPeriod[deg.ordinal()] = accelTime;
  71.     }
  72.  
  73.     public void registerWithInput(InputManager inputManager) {
  74.         this.inputManager = inputManager;
  75.  
  76.         String[] mappings = new String[] { "+SIDE", "+FWD", "+ROTATE", "+TILT", "+DISTANCE",
  77.                 "-SIDE", "-FWD", "-ROTATE", "-TILT", "-DISTANCE", };
  78.  
  79.         inputManager.addMapping("-SIDE", new KeyTrigger(KeyInput.KEY_A));
  80.         inputManager.addMapping("+SIDE", new KeyTrigger(KeyInput.KEY_D));
  81.         inputManager.addMapping("+FWD", new KeyTrigger(KeyInput.KEY_S));
  82.         inputManager.addMapping("-FWD", new KeyTrigger(KeyInput.KEY_W));
  83.         inputManager.addMapping("+ROTATE", new KeyTrigger(KeyInput.KEY_Q));
  84.         inputManager.addMapping("-ROTATE", new KeyTrigger(KeyInput.KEY_E));
  85.         inputManager.addMapping("+TILT", new KeyTrigger(KeyInput.KEY_R));
  86.         inputManager.addMapping("-TILT", new KeyTrigger(KeyInput.KEY_F));
  87.         inputManager.addMapping("-DISTANCE", new KeyTrigger(KeyInput.KEY_Z));
  88.         inputManager.addMapping("+DISTANCE", new KeyTrigger(KeyInput.KEY_X));
  89.  
  90.         inputManager.addListener(this, mappings);
  91.         inputManager.setCursorVisible(true);
  92.     }
  93.  
  94.     public void write(JmeExporter ex) throws IOException {
  95.  
  96.     }
  97.  
  98.     public void read(JmeImporter im) throws IOException {
  99.  
  100.     }
  101.  
  102.     public Control cloneForSpatial(Spatial spatial) {
  103.         RtsCam other = new RtsCam(cam, spatial);
  104.         other.registerWithInput(inputManager);
  105.         return other;
  106.     }
  107.  
  108.     public void setSpatial(Spatial spatial) {
  109.          
  110.     }
  111.  
  112.     public void setEnabled(boolean enabled) {
  113.  
  114.     }
  115.  
  116.     public boolean isEnabled() {
  117.  
  118.         return true;
  119.     }
  120.  
  121.     public void update(final float tpf) {
  122.  
  123.         for (int i = 0; i < direction.length; i++) {
  124.             int dir = direction[i];
  125.             switch (dir) {
  126.             case -1:
  127.                 accelPeriod[i] = clamp(-maxAccelPeriod[i],accelPeriod[i]-tpf,accelPeriod[i]);
  128.                 break;
  129.             case 0:
  130.                 if (accelPeriod[i] != 0) {
  131.                     double oldSpeed = accelPeriod[i];
  132.                     if (accelPeriod[i] > 0) {
  133.                         accelPeriod[i] -= tpf;
  134.                     } else {
  135.                         accelPeriod[i] += tpf;
  136.                     }
  137.                     if (oldSpeed * accelPeriod[i] < 0) {
  138.                         accelPeriod[i] = 0;
  139.                     }
  140.                 }
  141.                 break;
  142.             case 1:
  143.                 accelPeriod[i] = clamp(accelPeriod[i],accelPeriod[i]+tpf,maxAccelPeriod[i]);
  144.                 break;
  145.             }
  146.              
  147.         }
  148.          
  149.  
  150.         distance += maxSpeed[DISTANCE] * accelPeriod[DISTANCE] * tpf;
  151.         tilt += maxSpeed[TILT] * accelPeriod[TILT] * tpf;
  152.         rot += maxSpeed[ROTATE] * accelPeriod[ROTATE] * tpf;
  153.          
  154.         distance = clamp(minValue[DISTANCE],distance,maxValue[DISTANCE]);
  155.         rot = clamp(minValue[ROTATE],rot,maxValue[ROTATE]);
  156.         tilt = clamp(minValue[TILT],tilt,maxValue[TILT]);
  157.  
  158.         double offX = maxSpeed[SIDE] * accelPeriod[SIDE] * tpf;
  159.         double offZ = maxSpeed[FWD] * accelPeriod[FWD] * tpf;
  160.  
  161.         center.x += offX * Math.cos(-rot) + offZ * Math.sin(rot);
  162.         center.z += offX * Math.sin(-rot) + offZ * Math.cos(rot);
  163.  
  164.         position.x = center.x + (float)(distance * Math.cos(tilt) * Math.sin(rot));
  165.         position.y = center.y + (float)(distance * Math.sin(tilt));
  166.         position.z = center.z + (float)(distance * Math.cos(tilt) * Math.cos(rot));
  167.  
  168.          
  169.         cam.setLocation(position);
  170.         cam.lookAt(center, new Vector3f(0,1,0));
  171.          
  172.  
  173.     }
  174.      
  175.      
  176.     private static float clamp(float min, float value, float max) {
  177.         if ( value < min ) {
  178.             return min;
  179.         } else if ( value > max ) {
  180.             return max;
  181.         } else {
  182.             return value;
  183.         }
  184.     }
  185.      
  186.     public float getMaxSpeed(Degree dg) {
  187.         return maxSpeed[dg.ordinal()];
  188.     }
  189.      
  190.     public float getMinValue(Degree dg) {
  191.         return minValue[dg.ordinal()];
  192.     }
  193.      
  194.     public float getMaxValue(Degree dg) {
  195.         return maxValue[dg.ordinal()];
  196.     }
  197.      
  198.     // SIDE and FWD min/max values are ignored
  199.     public void setMinMaxValues(Degree dg, float min, float max) {
  200.         minValue[dg.ordinal()] = min;
  201.         maxValue[dg.ordinal()] = max;
  202.     }
  203.      
  204.     public Vector3f getPosition() {
  205.         return position;
  206.     }
  207.      
  208.     public void setCenter(Vector3f center) {
  209.         this.center.set(center);
  210.     }
  211.  
  212.     public void render(RenderManager rm, ViewPort vp) {
  213.  
  214.     }
  215.  
  216.     public void onAction(String name, boolean isPressed, float tpf) {
  217.         int press = isPressed ? 1 : 0;
  218.          
  219.         char sign = name.charAt(0);
  220.         if ( sign == '-') {
  221.             press = -press;
  222.         } else if (sign != '+') {
  223.             return;
  224.         }
  225.          
  226.         Degree deg = Degree.valueOf(name.substring(1));
  227.         direction[deg.ordinal()] = press;
  228.     }
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement