Created
September 7, 2016 01:15
-
-
Save Hasufel/eab41ce2b8e8a152f40632069c970711 to your computer and use it in GitHub Desktop.
Gamepad double analog test for PS Vita -- openfl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package; | |
import openfl.display.Sprite; | |
import openfl.events.Event; | |
import openfl.Lib; | |
import lime.ui.Gamepad; | |
import lime.ui.GamepadAxis; | |
import lime.ui.GamepadButton; | |
class Main extends Sprite { | |
var pS:Array<Float>; //analog axis flood filter, to include lime side | |
private var _ships:Map<Int,Particle>; | |
private var _shipsTurret:Map<Int, Array<Float>>; | |
private var _gamepads:Array<Int> = []; | |
private static inline var _speedRange:Int = 5; | |
private var _bullets:Array<Particle>; | |
private static inline var _bulletSpeed:Int = 10; | |
private var _ennemies:Map<Int,Particle>; | |
private var _counter:Int=0; | |
public function new() { | |
super(); | |
init(); | |
} | |
private function init():Void { | |
pS = [0,0,0,0]; | |
this.mouseEnabled = false; | |
this.mouseChildren = false; | |
_ships = new Map<Int,Particle>(); | |
_shipsTurret = new Map<Int, Array<Float>>(); | |
_bullets = []; | |
_ennemies = new Map<Int,Particle>(); | |
for (i in 0...30) addEnnemy(i); | |
Gamepad.onConnect.add (onGamepadConnect); | |
addEventListener(Event.ENTER_FRAME, onEnter); | |
} | |
private function onButtonDown (g:Gamepad, b:GamepadButton):Void { | |
if (!searchGamepad(g.id)) return; | |
trace(b + ' pressed' + ' on gamepad ' + g.id); | |
} | |
private function onButtonUp (g:Gamepad, b:GamepadButton):Void { | |
if (!searchGamepad(g.id)) return; | |
trace(b + ' released' + ' on gamepad ' + g.id); | |
} | |
private function onAxisMove (g:Gamepad, a:GamepadAxis, v:Float):Void { | |
if ((v > 0 && v < 0.1) || (v < 0 && v > -0.1)) v = 0; //additional analog jitter filter | |
switch (Std.string(a)) { | |
case "LEFT_X": | |
if (pS[0] == v) return; | |
pS[0] = v; | |
//trace('LEFT_X:' + v); | |
moveShip(g.id,0,v*_speedRange); | |
case "LEFT_Y": | |
if (pS[1] == v) return; | |
pS[1] = v; | |
//trace('LEFT_Y:' + v); | |
moveShip(g.id,1,-v*_speedRange); //ps vita, y axis is reversed | |
case "RIGHT_X": | |
if (pS[2] == v) return; | |
pS[2] = v; | |
//trace('RIGHT_X:' + v); | |
setTurretDir(g.id, 0, v); | |
case "RIGHT_Y": | |
if (pS[3] == v) return; | |
pS[3] = v; | |
//trace('RIGHT_Y:' + v); | |
setTurretDir(g.id, 1, -v);//ps vita, y axis is reversed | |
case "TRIGGER_LEFT": trace('TRIGGER_LEFT:' + v); | |
case "TRIGGER_RIGHT": trace('TRIGGER_RIGHT:' + v); | |
default: trace(Std.string(a) + ':' + v); | |
} | |
} | |
private function onGamepadConnect (g:Gamepad):Void { | |
if (searchGamepad(g.id)) return; | |
else { | |
trace('gamepad ' + g.id + ' ('+ g.name + ') connected'); | |
g.onButtonDown.add (onButtonDown.bind(g)); | |
g.onButtonUp.add (onButtonUp.bind(g)); | |
g.onAxisMove.add (onAxisMove.bind(g)); | |
g.onDisconnect.add (onGamepadDisconnect.bind(g)); | |
_gamepads.push(g.id); | |
addShip(g.id); | |
} | |
} | |
private function onGamepadDisconnect (g:Gamepad):Void { | |
trace('gamepad ' + g.id + ' disconnected'); | |
_gamepads.remove(g.id); | |
removeShip(g.id); | |
} | |
private function searchGamepad (id:Int):Bool { | |
if (_gamepads.indexOf(id) == -1) return false; | |
return true; | |
} | |
private function addShip(n:Int):Void { | |
var ship:Particle = new Particle(randomInt(10,stage.stageWidth-10),randomInt(10,stage.stageHeight-10),0,0,10,10,Std.int(Math.random()*0xFFFFFF)); | |
_ships.set(n,ship); | |
addTurret(n); | |
addChild(ship); | |
} | |
private function removeShip(n:Int):Void { | |
var ship:Particle = _ships.get(n); | |
_ships.remove(n); | |
removeTurret(n); | |
removeChild(ship); | |
} | |
private function addTurret(n:Int):Void { | |
_shipsTurret.set(n, [0,0]); | |
} | |
private function removeTurret(n:Int):Void { | |
_shipsTurret.remove(n); | |
} | |
private function addBullet(n:Int,ar:Array<Float>):Void { | |
var ship:Particle = _ships.get(n); | |
var angle = Math.atan2(ar[0], ar[1])-1.57; | |
var vx:Float = (_bulletSpeed * Math.cos(angle)); | |
var vy:Float = -(_bulletSpeed * Math.sin(angle)); | |
var bullet:Particle = new Particle(Std.int(ship.x)+Std.int(ship.width*.5),Std.int(ship.y)+Std.int(ship.height*.5),vx,vy,1,1,ship._c); | |
_bullets.push(bullet); | |
addChild(bullet); | |
} | |
private function removeBullet(n:Int):Void { | |
var bullet:Particle = _bullets[n]; | |
removeChild(bullet); | |
_bullets.splice(n,1); | |
} | |
private function setTurretDir(n:Int, a:Int, d:Float):Void { | |
var ar:Array<Float> = _shipsTurret.get(n); | |
if (a == 0) _shipsTurret.set(n,[d,ar[1]]); | |
else _shipsTurret.set(n,[ar[0],d]); | |
} | |
private function moveShip(n:Int,axis:Int,v:Float):Void { | |
var ship:Particle = _ships.get(n); | |
if (axis == 0) ship._vx = v; | |
else if (axis == 1) ship._vy = v; | |
} | |
private function addEnnemy(n:Int,ww:Int = -1,?x:Int=null,?y:Int=null,?vx:Float=null,?vy:Float=null):Void { | |
var r:Int = randomInt(50,150); | |
var t = ( r << 16 ) | ( (r+50) << 8 ) | (r+100); | |
var w:Int = randomInt(0,1); | |
if (ww==-1) ww = randomInt(8,12); | |
var nme:Particle = new Particle(x!=null?x:randomInt(10,stage.stageWidth-10),y!=null?y:randomInt(-500,-10),vx!=null?vx:w==0?randomInt(0,2):-randomInt(0,2),vy!=null?vy:randomInt(1,3),ww,ww,t); | |
_ennemies.set(n,nme); | |
addChild(nme); | |
_counter++; | |
} | |
private function removeEnnemy(n:Int):Void { | |
var nme:Particle = _ennemies.get(n); | |
removeChild(nme); | |
_ennemies.remove(n); | |
var a:Int = Std.int(nme.width/2); | |
if (a>4) { | |
for (i in 0...a) { | |
addEnnemy(_counter++,Std.int(nme.width*.5), Std.int(nme.x), Std.int(nme.y), nme._vx + (randomInt(0,1)>0?-randomInt(0,1):randomInt(0,1)), nme._vy + (randomInt(0,1)>0?-randomInt(0,1):randomInt(0,1))); | |
} | |
} | |
} | |
private function onEnter(e:Event):Void { | |
for (ship in _ships) { | |
ship.x += ship._vx; | |
ship.y += ship._vy; | |
if (ship._vx < .1 && ship._vx > -.1) ship._vx = 0; | |
if (ship._vy < .1 && ship._vy > -.1) ship._vy = 0; | |
if (ship.x < 0) ship.x = 0; | |
else if (ship.x+ship.width > stage.stageWidth) ship.x = stage.stageWidth-ship.width; | |
if (ship.y < 0) ship.y = 0; | |
else if (ship.y+ship.height > stage.stageHeight) ship.y = stage.stageHeight-ship.height; | |
} | |
var i = _bullets.length; | |
var bullet:Particle; | |
var o:Int = 0; | |
while (i-- > 0) { | |
o = 0; | |
bullet = _bullets[i]; | |
bullet.x += bullet._vx; | |
bullet.y += bullet._vy; | |
var nme:Particle; | |
for (k in _ennemies.keys()) { | |
nme = _ennemies.get(k); | |
if (bullet.x >= nme.x && bullet.x < nme.x+nme.width && bullet.y >= nme.y && bullet.y < nme.y+nme.height) { | |
removeBullet(i); | |
removeEnnemy(k); | |
o = 1; | |
} | |
} | |
if (o == 0 && bullet.x < 0 || bullet.x+bullet.width > stage.stageWidth || bullet.y < 0 || bullet.y+bullet.height > stage.stageHeight) removeBullet(i); | |
} | |
var l:Int = _gamepads.length; | |
var t:Array<Float> = []; | |
for (i in 0...l) { | |
t = _shipsTurret.get(i); | |
if (t[0] != 0 || t[1] != 0) addBullet(i,t); | |
} | |
var nme:Particle; | |
for (k in _ennemies.keys()) { | |
nme = _ennemies.get(k); | |
nme.x += nme._vx; | |
nme.y += nme._vy; | |
nme.rotation+=nme._vy; | |
if (nme.y>stage.stageHeight) { | |
nme.y = randomInt(-100,-10); | |
nme.x = randomInt(0,Std.int(stage.stageWidth - nme.width)); | |
nme._vy = randomInt(1,3); | |
} | |
if (nme.x<0 || nme.x > stage.stageWidth - nme.width) nme._vx = -nme._vx; | |
} | |
} | |
private function randomInt(low:Int, high:Int):Int{ | |
return Math.round(Math.random() * (high - low) + low); | |
} | |
} | |
class Particle extends Sprite { | |
public var _vx:Float; | |
public var _vy:Float; | |
public var _c:Int; | |
public function new(x:Int, y:Int, vx:Float, vy:Float, w:Int, h:Int, c:Int) { | |
super(); | |
_vx = vx; | |
_vy = vy; | |
_c = c; | |
this.graphics.beginFill(_c); | |
this.graphics.drawRect(0,0,w,h); | |
this.graphics.endFill(); | |
this.x = x; | |
this.y = y; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<project> | |
<meta title="GamepadTest" package="com.puzzl.gamepadtest" version="1.0.0" company="Puzzl" description="GamepadTest"/> | |
<config:memory haxeMB=" 128" engineMB=" 16" soundMB=" 1" pakMB=" 1" systemMB="30" totalMB=" 256" if="vita" /> | |
<app main="Main" file="Main" path="bin" /> | |
<window orientation="landscape" background="0"/> | |
<window vsync="true"/> | |
<window fps="60" /> | |
<window allow-shaders="true" /> | |
<window width="0" height="0" borderless="true" fullscreen="true" resizable="false" antialiasing="0" hardware="true" /> | |
<source path="src" /> | |
<haxelib name="openfl" /> | |
<assets path="assets" exclude="*.svg" /> | |
<haxedef name="display" if="display" /> | |
<haxeflag name="--no-output" if="display" /> | |
</project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment