|
SimpleExamples
Some simple examples of HSL use.
The time has come for me to show some quick examples of HSL usage, so here goes. Simple examplepackage;
import hsl.haxe.DirectSignaler;
import hsl.haxe.Signaler;
class Main {
public static function main():Void {
var barbapapa:Shapeshifter = new Shapeshifter();
barbapapa.shapeShiftedSignaler.bind(traceShape);
barbapapa.shiftShape("square");
barbapapa.shiftShape("pear");
}
private static function traceShape(shape:String):Void {
trace("Barbapapa has changed it's shape to a " + shape);
}
}
class Shapeshifter {
public var shapeShiftedSignaler(default, null):Signaler<String>;
public function new():Void {
shapeShiftedSignaler = new DirectSignaler(this);
}
public function shiftShape(newShape:String):Void {
shapeShiftedSignaler.dispatch(newShape);
}
}In the code above, the shapeshifters dispatch shape shifted signals through their shape shifted signaler (the shapeShifterSignaler property). Such signals contain the shape as a string. These are the most important lines: public function new():Void {
shapeShiftedSignaler = new DirectSignaler(this);
}The shapeshifter creates the signaler. In most cases it will construct a direct signaler, and in most cases it will do so in its constructor. this is passed, as that is the subject, don't worry too much about that. barbapapa.shapeShiftedSignaler.bind(traceShape); The main class calls the bind method, which is similar to the addEventListener method of the EventDispatcher class in ActionScript 3.0. The main class binds the shape shifted signaler to the "traceShape" method (the latter then becomes the listener). From this point on whenever the shapeshifter dispatches a shape shifted signal, the bound method - traceShape - is called. public function shiftShape(newShape:String):Void {
shapeShiftedSignaler.dispatch(newShape);
}The shiftShape method dispatches the signal. (In result, traceShape will be called.) bind, bindVoid and bindAdvancedHSL allows very agile development. Partially because of the API of the signalers. The API has three diffenent bind methods. bindYou've already seen the first one. Note: the following example is AVM2-specific (you will have to compile to a flash9+ SWF file). package;
import flash.display.Shape;
import flash.Lib;
import hsl.haxe.DirectSignaler;
import hsl.haxe.Signaler;
class Main {
public static function main():Void {
var player:Warrior = new Warrior();
var healthBar:HealthBar = new HealthBar();
player.healthChangedSignaler.bind(healthBar.redraw);
Lib.current.addChild(healthBar);
player.takeDamage(10);
}
}
class Warrior {
public var healthChangedSignaler(default, null):Signaler<Float>;
private var healthPoints:Int;
private var maximalHealthPoints:Int;
public function new():Void {
healthChangedSignaler = new DirectSignaler(this);
healthPoints = maximalHealthPoints = 24;
}
public function takeDamage(damageHealthPoints:Int):Void {
healthPoints -= damageHealthPoints;
healthChangedSignaler.dispatch(healthPoints / maximalHealthPoints);
}
}
class HealthBar extends Shape {
public function new():Void {
super();
redraw(1);
}
public function redraw(health:Float):Void {
graphics.clear();
graphics.beginFill(0x365700);
graphics.drawRect(10, 10, 250 * health, 30);
graphics.endFill();
graphics.lineStyle(2, 0, 1, true);
graphics.drawRect(8, 8, 254, 34);
}
}As you can see, the regular bind method is used if you want to access the data inside the signal. In this case, that data is the health of the player. bindVoidThere is also a bindVoid method, which is used if you want to know that a signal has been dispatched, but the data inside the signal is not important. This example uses the bindVoid method. Note that the Warrior class hasn't changed from the example above. package;
import hsl.haxe.DirectSignaler;
import hsl.haxe.Signaler;
class Main {
public static function main():Void {
var player:Warrior = new Warrior();
var camera:Camera = new Camera();
player.healthChangedSignaler.bindVoid(camera.shake);
player.takeDamage(10);
}
}
class Warrior {
public var healthChangedSignaler(default, null):Signaler<Float>;
private var healthPoints:Int;
private var maximalHealthPoints:Int;
public function new():Void {
healthChangedSignaler = new DirectSignaler(this);
healthPoints = maximalHealthPoints = 24;
}
public function takeDamage(damageHealthPoints:Int):Void {
healthPoints -= damageHealthPoints;
healthChangedSignaler.dispatch(healthPoints / maximalHealthPoints);
}
}
class Camera {
public function new():Void {
}
public function shake():Void {
trace("Camera is shaking");
}
}bindAdvancedFinally, there is a bindAdvanced method. That method allows you to access the data inside the signal, but also the subject that dispatched the signal. This example uses it. Note that I've only added the name property to the Warrior class, other than that it's the same as the ones above. package;
import hsl.haxe.DirectSignaler;
import hsl.haxe.Signal;
import hsl.haxe.Signaler;
class Main {
public static function main():Void {
var pimm:Warrior = createWarrior("Pimm");
var edo:Warrior = createWarrior("Edo");
var alexander:Warrior = createWarrior("Alexander");
pimm.takeDamage(10);
edo.takeDamage(14);
pimm.takeDamage(2);
}
private static function createWarrior(name:String):Warrior {
var result:Warrior = new Warrior(name);
result.healthChangedSignaler.bindAdvanced(handleHealthChanged);
return result;
}
private static function handleHealthChanged(signal:Signal<Float>):Void {
trace(cast(signal.currentTarget, Warrior).name + "'s health dropped to " + Math.floor(signal.data * 100) + "%");
}
}
class Warrior {
public var healthChangedSignaler(default, null):Signaler<Float>;
private var healthPoints:Int;
private var maximalHealthPoints:Int;
public var name(default, null):String;
public function new(name:String):Void {
healthChangedSignaler = new DirectSignaler(this);
this.name = name;
healthPoints = maximalHealthPoints = 24;
}
public function takeDamage(damageHealthPoints:Int):Void {
healthPoints -= damageHealthPoints;
healthChangedSignaler.dispatch(healthPoints / maximalHealthPoints);
}
}Toy around with the examples above. Proceed to the MoreAdvancedExamples page for more examples. |