CoryMartin

Controller.hx

Feb 12th, 2021
755
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package scripts;
  2.  
  3. import com.stencyl.Engine;
  4. import com.stencyl.behavior.Script;
  5. import com.stencyl.behavior.Script.*;
  6. import com.stencyl.utils.Utils;
  7. import com.stencyl.Input;
  8. import com.stencyl.models.Joystick;
  9.  
  10. enum Dir {
  11.     U;
  12.     D;
  13.     L;
  14.     R;
  15. }
  16.  
  17. @:structInit class DirMapping {
  18.     public var u : String;
  19.     public var r : String;
  20.     public var d : String;
  21.     public var l : String;
  22. }
  23.  
  24. class Controller {
  25.     public static var DIAG = 0.7071;
  26.    
  27.     public static var defaultDirMapping : DirMapping = {
  28.         u: "up",
  29.         r: "right",
  30.         d: "down",
  31.         l: "left",
  32.     }
  33.  
  34.     public static function mapDefaultGamepadConfig () {
  35.         // Cancel / Action 2
  36.         Input.mapJoystickButton("0", "action2");
  37.  
  38.         // Affirm / Action 1
  39.         Input.mapJoystickButton("1", "action1");
  40.         Input.mapJoystickButton("2", "action1");
  41.         Input.mapJoystickButton("3", "action1");
  42.  
  43.         // Pause
  44.         Input.mapJoystickButton("6", "enter");
  45.  
  46.         // D-Pad / Arrow Keys
  47.         Input.mapJoystickButton("11", "up");
  48.         Input.mapJoystickButton("12", "down");
  49.         Input.mapJoystickButton("13", "left");
  50.         Input.mapJoystickButton("14", "right");
  51.  
  52.         // Analog Stick
  53.         Input.mapJoystickButton("-axis 1", "up");
  54.         Input.mapJoystickButton("+axis 1", "down");
  55.         Input.mapJoystickButton("-axis 0", "left");
  56.         Input.mapJoystickButton("+axis 0", "right");
  57.  
  58.         Input.setJoySensitivity(0.1);
  59.     }
  60.  
  61.     public static function mapGamepad (map : Map<String,String>) {
  62.         Input.mapJoystickButton(map.get("z"), "z");
  63.         Input.mapJoystickButton(map.get("c"), "c");
  64.         Input.mapJoystickButton(map.get("x"), "x");
  65.         Input.mapJoystickButton(map.get("r"), "r");
  66.         Input.mapJoystickButton(map.get("left"), "left");
  67.         Input.mapJoystickButton(map.get("right"), "right");
  68.         Input.mapJoystickButton(map.get("up"), "up");
  69.         Input.mapJoystickButton(map.get("down"), "down");
  70.  
  71.         Input.setJoySensitivity(0.1);
  72.     }
  73.  
  74.     public static function getAnalogAngle (am : DirMapping) : Float {
  75.         var x = -Input.getButtonPressure(am.l) + Input.getButtonPressure(am.r);
  76.         var y = -Input.getButtonPressure(am.u) + Input.getButtonPressure(am.d);
  77.  
  78.         if (Math.abs(x) == 1 && Math.abs(y) == 1) {
  79.             x = x * DIAG;
  80.             y = y * DIAG;
  81.         }
  82.  
  83.         var angle = Utils.DEG * Math.atan2(y, x);
  84.  
  85.         return angle;
  86.     }
  87.  
  88.     public static function getAnalogPressure (am : DirMapping) : Float {
  89.         var x = -Input.getButtonPressure(am.l) + Input.getButtonPressure(am.r);
  90.         var y = -Input.getButtonPressure(am.u) + Input.getButtonPressure(am.d);
  91.  
  92.         if (Math.abs(x) == 1 && Math.abs(y) == 1) {
  93.             x = x * DIAG;
  94.             y = y * DIAG;
  95.         }
  96.  
  97.         var pressure = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
  98.         if (pressure > 1 || Math.abs(pressure) >= 0.95) pressure = 1;
  99.  
  100.         return pressure;
  101.     }
  102.  
  103.     public static inline function getAnalogXAxis (am : DirMapping) : Float {
  104.         var x = -Input.getButtonPressure(am.l) + Input.getButtonPressure(am.r);
  105.  
  106.         return x;
  107.     }
  108.  
  109.     public static inline function getAnalogYAxis (am : DirMapping) : Float {
  110.         var y = -Input.getButtonPressure(am.u) + Input.getButtonPressure(am.d);
  111.  
  112.         return y;
  113.     }
  114.  
  115.     public static function getHighestInput (am: DirMapping) : Dir {
  116.         var x = getAnalogXAxis(am);
  117.         var y = getAnalogYAxis(am);
  118.  
  119.         if (Math.abs(x) > Math.abs(y)) {
  120.             if (x > 0) return Dir.R;
  121.             else       return Dir.L;
  122.         }
  123.         else {
  124.             if (y > 0) return Dir.D;
  125.             else       return Dir.U;
  126.         }
  127.     }
  128. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×