Mgl
Mgl is a mini game programming library written in Haxe + OpenFL.
Install / Use
/learn @abagames/MglREADME
mgl (Mini Game programming Library)
Mgl is a mini game programming library written in Haxe + OpenFL. Mgl is useful for creating a simple Flash game in short term. HTML5/Windows build targets are partially supported.
Using the SiON synthesizer library.
Using the as3gif animated gif encoder.
Sample games
SCAFFOLD NOW / DON'T SEE ME / MISSILE COMES BACK TO ME / PROMINENT MOUNTAIN / BADDALION / SUM10 / WASD THRUST / LURE AWAY / FROM FOUR SIDES / PINCER ATTACK / STRONG BUY STRONG SELL / OVEREXPLODE / DETERMINISTIC PANELS / CUT OFF LINE AMEBA / EARTH DEFENSE STICKS / SIDE WALLS MULTI KICKS / POLE SLIP DOWN / CALC +-*/ / TYPHOON AVENUE / LEFT RIGHT HAND RULE / REFLECTOR SATELLITES / BALLOON BURROWER / SATELLITE CATCH / POLAR NS
Sample games (using the older version mgl)
LONG EDGE WINS / REVGRAV / SPACE SHIPS CONTACT ACCIDENTAL / YOU HAVE ONE SHOT / SIDE SHOT BOOSTER / MAGNETIC ACTION
Advantages
- A basic game loop is automatically managed.
- You can do spawning, moving and removing the actor in an easy-to-write manner.
- Using a method chaining and a shortened name helps to write a logic with a one-liner.
- Since the dot pixel art and the sound effect are generated procedurally, you don't have to care about them.
- Many useful classes for particles, key handling, mouse handling, fiber, random, text, color and 2d vector.
Limitations
- Not suitable for a large scale game because of lacking flexibility in handling a game loop.
- No 3d, neither external bitmaps nor sounds loading support.
Sample code
import mgl.*;
using mgl.Fiber;
using mgl.Util;
// A basic game loop handling class.
class Main extends Game {
static public function main() {
new Main();
}
function new() {
super(this);
}
var bgmDrumSound:Sound;
var endGameSound:Sound;
// initialize() --> Title -> begin() -> update()(every frame) -> Begin the game -v
// ^ v-----------------------------------------------------<
// ^ begin() -> update()(every frame) -> End the game -v
// ^----------------------------------------------------<
// First initializer.
override function initialize() {
Ball.main = this;
// Apply quarter-note quantization.
Sound.setDefaultQuant(4);
// Generate sounds.
bgmDrumSound = new Sound().setDrumMachine();
endGameSound = new Sound().major().setMelody()
.addTone(.3, 10, .7).addTone(.6, 10, .4).end();
// Set the title.
setTitle("BALL 28 IN SPACE");
}
public var ballLeft:Int;
var nextBallCount:Int;
var time:Int;
// Begin the title/game.
override function begin() {
Ball.player = new Player();
nextBallCount = 0;
ballLeft = 28;
time = 0;
// Play the bgm.
bgmDrumSound.play();
}
// Update every frame.
override function update() {
var sc = Std.int(time / 1000);
var ms = '00${time % 1000}';
ms = ms.substr(ms.length - 3);
// Draw elapsed time at the upper right aligned right.
new Text().setXy(.99, .01).alignRight().setText('TIME: $sc.$ms').draw();
// If the game isn't begun then return.
if (!Game.isInGame) return;
time += 16;
// Draw the number of left balls at the upper left.
new Text().setXy(.01, .01).setText('LEFT: $ballLeft').draw();
if (ballLeft <= 0) {
// Play the game ending sound.
endGameSound.play();
// End the game.
Game.endGame();
// Get all ball actors and check a total of them.
} else if (Actor.getActors("Ball").length <= 0) {
nextBallCount++;
for (i in 0...nextBallCount) new Ball();
}
// Instructions drawn only once.
if (Game.ticks == 0) {
new Text().setXy(.1, .1).setText("[urdl]: MOVE").setTicks(180).addOnce();
}
if (Game.ticks == 60) {
new Text().setXy(.1, .15).setText("[Z]: BRAKE").setTicks(180).addOnce();
}
}
}
// Player actor.
class Player extends Actor {
static var tickSound:Sound;
// Static initializer called only once.
override function initialize() {
// Generate the green shape.
dotPixelArt = new DotPixelArt().setColor(Color.green).generateShape(.04, .05);
// Set the hir rect.
setHitRect(.04, .05);
// Set the tick sound.
tickSound = new Sound().minor().addTone(.5, 3, .3).end();
}
// Begin this actor.
override function begin() {
// Set the position to (.5, .5).
position.setNumber(.5);
// Create the fiber to play the tick sound every 30 frames.
new Fiber(this).wait(30).doIt( { tickSound.play(); } );
}
// Update every frame.
override function update() {
// Get the joystick input and add to the velocity.
velocity.add(Key.stick.multiply(.003)).multiply(Key.isButtonPressing ? .6 : .95);
// Loop the position between -.05 and 1.05.
position.setXy(position.x.loopRange(-.05, 1.05), position.y.loopRange(-.05, 1.05));
// Set the way to the velocity way.
way = v.way;
// Add the reddish green particle from the position.
new Particle().setPosition(position).setColor(Color.green.goRed())
.setWay(way + 180, 45).setSpeed(velocity.length).add();
// Check the hit to the Ball actors.
isHit("Ball", function(ball) {
// If the player hit the ball, erase the ball.
ball.erase();
});
}
}
// Ball actor.
class Ball extends Actor {
static public var main:Main;
static public var player:Player;
static var removeSound:Sound;
override function initialize() {
// Set the circle yellow shape.
dotPixelArt = new DotPixelArt().setColor(Color.yellow).generateCircle(.04);
setHitRect(.04);
// Set the removing sound.
removeSound = new Sound().minor().addTone(.7).addRest().addTone(.7).end();
}
override function begin() {
for (i in 0...10) {
// Set the random position from .1 to .9.
position.setXy(.1.randomFromTo(.9), .1.randomFromTo(.9));
// If the distance to the player is far enough then break.
if (position.distanceTo(player.position) > .3) break;
}
}
public function erase() {
// Add 20 particles.
new Particle().setPosition(position).setColor(Color.yellow.goRed())
.setCount(20).setSize(.03).add();
main.ballLeft--;
// Play the removing sound.
removeSound.play();
// Remove this actor.
remove();
}
}
Classes
A shortened form of a class/method name is described in ( ).
Game (G)
A basic game loop handler. You have to override the initialize(), begin() and update() method.
Methods
- (static)isInGame():Bool (ig)
- (static)endGame():Bool (eg)
- (static)ticks:Int (t)
- (static)fillRect(x:Float, y:Float, width:Float, height:Float, color:C):Void (fr)
- (static)drawToForeground():Bool (df)
- (static)drawToBackground():Bool (db)
- setTitle(title:String, title2:String = ""):Game (tt)
- setVersion(version:Int = 1):Game (vr)
- decorateTitle(color:Color, seed:Int = -1):Game (dt)
- enableDebuggingMode():Game (dm)
- enableCaptureMode(scale:Float = 1, fromSec:Float = 5, toSec:Float = 8, intervalSec:Float = .1):Game (cm)
- setYRatio(ratio:Float):Game (yr)
- initializeEnd():Game (ie)
Overriden methods
- initialize():Void
- begin():Void
- update():Void
- updateBackground():Void
- initializeState():Void
- loadState(d:Dynamic):Void
- saveState(d:Dynamic):Void
Actor (A)
An actor moves on a screen. An actor has a position, a velocity and a dot pixel art.
Variables
- position:Vector (p)
- z:Float = 0
- velocity:Vector (v)
- way:Float = 0 (w)
- speed:Float = 0 (s)
- dotPixelArt:DotPixelArt (d)
Methods
- (static)getActors(className:String):Array<Dynamic> (acs)
- (static)clearActors():Bool (cl)
- (static)clearSpecificActors(className:String):Void (cls)
- (static)scroll(className:String, vx:Float, vy:Float = 0, minX:Float = 0, maxX:Float = 0, minY:Float = 0, maxY:Float = 0):Void (sc)
- (static)scrollActors(classNames:Array<String>, vx:Float, vy:Float = 0, minX:Float = 0, maxX:Float = 0, minY:Float = 0, maxY:Float = 0):Void (scs)
- ticks:Int (t)
- remove():Bool (r)
- setHitRect(width:Float = -999, height:Float = -1):Actor (hr)
- setHitCircle(diameter:Float = -999):Actor (hc)
- isHit(className:String, onHit:Dynamic -> Void = null):Bool (ih)
- setDisplayPriority(priority:Int):Actor (dp)
- drawToForeground():Actor (df)
- drawToBackground():Actor (bf)
- sortByZ():Actor (sz)
Overriden methods
- initialize():Void
- begin():Void
- update():Void
DotPixelArt (D)
A pixel art for an actor. You can write a rectangle, a circle and an auto generated shape.
Methods
- new():DotPixelArt (i)
- setColor(color:C):DotPixelArt (c)
- setColorSpot(color:C):DotPixelArt (cs)
- setColorBottom(color:C):DotPixelArt (cb)
- setColorBottomSpot(color:C):DotPixelArt (cbs)
- setSpotInterval(x:Float = 0, y:Float = 0,
