Skip to content

Behavior

Antarka edited this page Jul 29, 2016 · 4 revisions

Behavior

The United behavior is a high-level implementation of Sup.Behavior that implements own framework feature like Timer & Events.

With the behavior you can easily make timer or acceed to the current scene chunk. You have to put the chunk interface at the generic place to get typings on $.

class TestBehavior extends United.Behavior<any> {
    awake() {
        this.$.playerActor = this.actor; // Register playerActor on the global chunk, retrieve it anywhere on your game
        // Equal to = United.Engine.$<any>().playerActor = this.actor;
        
        // Send and receive events between different behavior with the actor!
        this.$.playerActor.events.on("yolo",function() {
            Sup.log("Yolo emit !!");
        });
    }

    update() {
        // Native timer implementation !
        if(this.walk(60)) {
            this.events.emit("yolo");
        }
    }
}
Sup.registerBehavior(TestBehavior);

United Behavior has the same methods as Sup.Behavior : Awake, Start, Update, Destroy

An another concrete example from Echo of life :

class PicsBehavior extends United.Behavior<Game> {

    update() {
        if(Sup.ArcadePhysics2D.intersects(this.actor.arcadeBody2D,this.$.player.arcadeBody2D)) {
            this.$.triggers.emit("playerDie");
        }
    }
    
}
Sup.registerBehavior(PicsBehavior);

No need to make a getBehavior or to set a global variable. The framework do the job for you :) And more that make eventEmitter more powerfull than never...

Methods

Get current scene chunk.

protected get $<T>() : T

Timer walk (elapsed % interval == 0)

walk(interval: number) : boolean

Timer elapsed (Match a specific elapsedtime)

elapsed(elapsedTime: number) : number

Each united behavior link a "eventEmitter" to the actor. So you can make something like that :

this.actor["events"].on("action",function(msg: string) {
    Sup.log(msg);
});

// On a another script
Sup.getActor("behaviorActor")["events"].emit("action","hello world");

Roadmap

  • Replace United.Timer by United.Clock

Clone this wiki locally