Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package scripts;
- import com.stencyl.Engine;
- import com.stencyl.behavior.Script;
- import com.stencyl.behavior.Script.*;
- import com.stencyl.utils.Utils;
- import com.stencyl.Input;
- import com.stencyl.models.Joystick;
- enum Dir {
- U;
- D;
- L;
- R;
- }
- @:structInit class DirMapping {
- public var u : String;
- public var r : String;
- public var d : String;
- public var l : String;
- }
- class Controller {
- public static var DIAG = 0.7071;
- public static var defaultDirMapping : DirMapping = {
- u: "up",
- r: "right",
- d: "down",
- l: "left",
- }
- public static function mapDefaultGamepadConfig () {
- // Cancel / Action 2
- Input.mapJoystickButton("0", "action2");
- // Affirm / Action 1
- Input.mapJoystickButton("1", "action1");
- Input.mapJoystickButton("2", "action1");
- Input.mapJoystickButton("3", "action1");
- // Pause
- Input.mapJoystickButton("6", "enter");
- // D-Pad / Arrow Keys
- Input.mapJoystickButton("11", "up");
- Input.mapJoystickButton("12", "down");
- Input.mapJoystickButton("13", "left");
- Input.mapJoystickButton("14", "right");
- // Analog Stick
- Input.mapJoystickButton("-axis 1", "up");
- Input.mapJoystickButton("+axis 1", "down");
- Input.mapJoystickButton("-axis 0", "left");
- Input.mapJoystickButton("+axis 0", "right");
- Input.setJoySensitivity(0.1);
- }
- public static function mapGamepad (map : Map<String,String>) {
- Input.mapJoystickButton(map.get("z"), "z");
- Input.mapJoystickButton(map.get("c"), "c");
- Input.mapJoystickButton(map.get("x"), "x");
- Input.mapJoystickButton(map.get("r"), "r");
- Input.mapJoystickButton(map.get("left"), "left");
- Input.mapJoystickButton(map.get("right"), "right");
- Input.mapJoystickButton(map.get("up"), "up");
- Input.mapJoystickButton(map.get("down"), "down");
- Input.setJoySensitivity(0.1);
- }
- public static function getAnalogAngle (am : DirMapping) : Float {
- var x = -Input.getButtonPressure(am.l) + Input.getButtonPressure(am.r);
- var y = -Input.getButtonPressure(am.u) + Input.getButtonPressure(am.d);
- if (Math.abs(x) == 1 && Math.abs(y) == 1) {
- x = x * DIAG;
- y = y * DIAG;
- }
- var angle = Utils.DEG * Math.atan2(y, x);
- return angle;
- }
- public static function getAnalogPressure (am : DirMapping) : Float {
- var x = -Input.getButtonPressure(am.l) + Input.getButtonPressure(am.r);
- var y = -Input.getButtonPressure(am.u) + Input.getButtonPressure(am.d);
- if (Math.abs(x) == 1 && Math.abs(y) == 1) {
- x = x * DIAG;
- y = y * DIAG;
- }
- var pressure = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
- if (pressure > 1 || Math.abs(pressure) >= 0.95) pressure = 1;
- return pressure;
- }
- public static inline function getAnalogXAxis (am : DirMapping) : Float {
- var x = -Input.getButtonPressure(am.l) + Input.getButtonPressure(am.r);
- return x;
- }
- public static inline function getAnalogYAxis (am : DirMapping) : Float {
- var y = -Input.getButtonPressure(am.u) + Input.getButtonPressure(am.d);
- return y;
- }
- public static function getHighestInput (am: DirMapping) : Dir {
- var x = getAnalogXAxis(am);
- var y = getAnalogYAxis(am);
- if (Math.abs(x) > Math.abs(y)) {
- if (x > 0) return Dir.R;
- else return Dir.L;
- }
- else {
- if (y > 0) return Dir.D;
- else return Dir.U;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement