I made one that returns 1 and 0 for x and y good for horizontals, verticals, and diagonals. real basic not a proper module but it works:
Strict
Import Mojo
Class Stick
Field offx:Float
Field offy:Float
Field x:Float
Field y:Float
Field vx:Float
Field vy:Float
Field length:Float
Field active:Int
Field oldActive:Int
Field radius:Float
Field deadZone:Float
Field maxR:Float
Function create:Stick(offx:Int,offy:Int,radius:Float,deadZone:Float,maxShift:Float)
Local s := New Stick
s.offx = offx
s.offy = offy
s.x = 0
s.y = 0
s.deadZone = deadZone
s.maxR = maxShift
s.active = False
s.oldActive = False
s.radius = radius
Return s
End Function
Method update:Void()
x=0
y=0
vx = 0
vy = 0
length = 0
If MouseDown(0)
vx = MouseX() - offx
vy = MouseY() - offy
length = Sqrt(vx*vx+vy*vy)
If oldActive = True
active = True
If length < deadZone
Elseif length > maxR
Local dx:Float = vx/length
Local dy:Float = vy/length
length = maxR
x = dx*length
y = dy*length
Else
x = vx
y = vy
Endif
Elseif length < deadZone
active = True
Else
active = False
Endif
Else
active = False
Endif
oldActive = active
End Method
Method getdx:Int()
If length > 0
Local l1:Float = length/5*2
If( x > l1) Return 1
If( x < -l1) Return -1
Endif
Return 0
End Method
Method getdy:Int()
If length > 0
Local l1:Float = length/5*2
If(y > l1) Return 1
If(y < -l1) Return -1
Endif
Return 0
End Method
Method display:Void()
Local px:Float = offx + x
Local py:Float = offy + y
SetColor 0,255,0
DrawOval px - radius,py-radius,radius*2,radius*2
End Method
End Class
Class Controller
Field x:Float
Field y:Float
Field vx:Float
Field vy:Float
Field radius:Float
Field deadZone:Float
Field maxR:Float
Field stick:Stick
Function create:Controller(x:Int,y:Int,radius:Int,maxr:Float,deadZone:Int)
Local c := New Controller
c.x = x
c.y = y
c.radius = radius
c.stick = Stick.create(x,y,20,deadZone,maxr)
Return c
End Function
Method update:Void()
stick.update()
End Method
Method getdx:Int()
Return stick.getdx()
End Method
Method getdy:Int()
Return stick.getdy()
End Method
Method display:Void()
SetColor 200,0,0
DrawOval x-(radius/2),y-(radius/2),radius,radius
stick.display()
End Method
End Class
Class Game Extends App
Field controller:Controller
Method OnCreate:Int()
controller = Controller.create(100,100,64,32,20)
SetUpdateRate(30)
Return 0
End Method
Method OnUpdate:Int()
controller.update()
Return 0
End Method
Method OnRender:Int()
Cls()
controller.display()
DrawText "x ="+controller.getdx(),10,10
DrawText "y ="+controller.getdy(),10,30
DrawText "angle ="+Int((ATan2(controller.stick.y,controller.stick.x)+360.0) Mod 360.0),10,50
End Method
End Class
Function Main:Int()
New Game
End Function
|