Last active
August 29, 2015 14:27
-
-
Save Hasufel/b943831402e48009e5f6 to your computer and use it in GitHub Desktop.
Mini Shooter sample
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 { | |
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 { | |
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...100) addEnnemy(i); | |
Lib.application.window.onGamepadButtonDown.add (onButtonDown); | |
Lib.application.window.onGamepadButtonUp.add (onButtonUp); | |
Lib.application.window.onGamepadAxisMove.add (onAxisMove); | |
Lib.application.window.onGamepadConnect.add(onGamepadConnect); | |
Lib.application.window.onGamepadDisconnect.add(onGamepadDisconnect); | |
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": | |
//trace('LEFT_X:' + v); | |
moveShip(g.id,0,v*_speedRange); | |
case "LEFT_Y": | |
//trace('LEFT_Y:' + v); | |
moveShip(g.id,1,v*_speedRange); | |
case "RIGHT_X": | |
//trace('RIGHT_X:' + v); | |
setTurretDir(g.id, 0, v); | |
case "RIGHT_Y": | |
//trace('RIGHT_Y:' + v); | |
setTurretDir(g.id, 1, v); | |
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'); | |
_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(12,16); | |
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment