Advertisement
Guest User

Untitled

a guest
Sep 12th, 2023
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.35 KB | None | 0 0
  1. public class Joystick {
  2.     private Sprite joySprite;
  3.  
  4.     public float VelocityX;
  5.     public float VelocityY;
  6.  
  7.     public float OriginX, OriginY;
  8.     private float fingerX, fingerY;
  9.     private int joyFinger;
  10.  
  11.     public Joystick() {
  12.         joySprite = Sprite.load("ui_button.png");
  13.  
  14.         OriginX = -999;
  15.         OriginY = -999;
  16.     }
  17.  
  18.     private float clamp(float a, float min, float max) {
  19.         return a < min ? min : (a > max ? max : a);
  20.     }
  21.  
  22.     public void update() {
  23.         int finger = 0;
  24.  
  25.         if((finger = Engine.Current.Input.isTouchingZone(0, 0, Engine.Current.Graphics.ViewWidth, Engine.Current.Graphics.ViewHeight)) != -1) {
  26.             if(OriginX == -999) {
  27.                 OriginX = Engine.Current.Input.Touches[finger].X;
  28.                 OriginY = Engine.Current.Input.Touches[finger].Y;
  29.             }
  30.  
  31.             float xdiff = (Engine.Current.Input.Touches[finger].X - OriginX) / Engine.Current.Graphics.ViewWidth;
  32.             float ydiff = (Engine.Current.Input.Touches[finger].Y - OriginY) / Engine.Current.Graphics.ViewHeight;
  33.  
  34.             VelocityX = clamp(xdiff / 0.2f, -1, 1);
  35.             VelocityY = clamp(ydiff / 0.2f, -1, 1);
  36.         } else {
  37.             OriginX = -999;
  38.             OriginY = -999;
  39.         }
  40.     }
  41.  
  42.     public void draw() {
  43.         VelocityX = 0;
  44.         VelocityY = 0;
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement