Skip to content

StackControls

Gentilhomme edited this page Jul 26, 2016 · 6 revisions

StackControls

A breaking change is comming for v1.3>+.

StackControls is an object-oriented class to manage multiple key entries for the same action. StackControls support keyboard and mouse actions. We work on the gamepad & mobile support.

Exemple

class PlayerControl extends United.StackControls.Controls {

    UP : United.StackControls.Action = new United.StackControls.Action({
        keyboard: ["Z", "UP", "W"]
    });

    DOWN : United.StackControls.Action = new United.StackControls.Action({
        keyboard: ["S", "DOWN"]
    });

    LEFT : United.StackControls.Action = new United.StackControls.Action({
        keyboard: ["Q", "LEFT", "A"]
    });

    RIGHT : United.StackControls.Action =  new United.StackControls.Action({
        keyboard: ["D", "RIGHT", "D"],
    });

    JUMP : United.StackControls.Action =  new United.StackControls.Action({
        keyboard: ["SPACE"],
        mButton: [2]
    });

    USE : United.StackControls.Action =  new United.StackControls.Action({
        keyboard: ["E"]
    });

    __ROULADE : United.StackControls.Action = new United.StackControls.Action({
        keyboard:["SHIFT"]
    });

    roulade() : boolean {
        return this.__ROULADE.isDown() && (this.LEFT.wasJustPressed() || this.RIGHT.wasJustPressed())
    }

    constructor(){
      super(null)
    }

}

Example on echo of life

This is how we use controls on our player update method. Note : we link the controls to the global chunk.

update() {
    if(!this.$.paused && !this.$.isEnd) {
        let velocity : Sup.Math.Vector2 = this.actor.arcadeBody2D.getVelocity();
        const touchBottom : boolean = this.actor.arcadeBody2D.getTouches().bottom;
        const touchLeft : boolean = this.actor.arcadeBody2D.getTouches().left;
        const touchRight : boolean = this.actor.arcadeBody2D.getTouches().right;

        if(this.$.control.JUMP.wasJustPressed()) {
            // Jump action
        }

        // Ladder
        if(this.isTile("ladder",this.ladderLayer)) {
            this.onLadder = true;
            this.actor.spriteRenderer.setAnimation("Ladder");
            if(this.$.control.JUMP.wasJustPressed()) {
                velocity.y += this.jumpVelocity / 2;
            }

            if (this.$.control.UP.isDown()) {
                velocity.y = this.ladderVelocity;
            }
            else if(this.$.control.DOWN.isDown()) {
                velocity.y = -this.ladderVelocity;
            }else {
                velocity.y = 0;
                this.actor.spriteRenderer.setAnimation("IdleLadder");
            }
        }  
        else {
            this.onLadder = false;
        }

        this.actor.arcadeBody2D.setCustomGravityY(this.onLadder ? 0 : -0.005);

        // Redefine max horizontal speed if we are on a ladder!
        const maxSpeed: number = this.onLadder ? this.speed * 0.25 : this.speed;

        // Left / Right / Idle
        if(this.$.control.RIGHT.isDown()) {
            if (velocity.x < maxSpeed) {
                velocity.x += maxSpeed/15;
            }
            this.actor.spriteRenderer.setHorizontalFlip(false);
            if(!this.onLadder) {
                this.actor.spriteRenderer.setAnimation("Walk");
            }
        }
        else if(this.$.control.LEFT.isDown()) {
            if (velocity.x > -maxSpeed) {
                velocity.x -= maxSpeed/15;
            }
            this.actor.spriteRenderer.setHorizontalFlip(true);
            if(!this.onLadder) {
                this.actor.spriteRenderer.setAnimation("Walk");
            }
        }
        else {
            velocity.x /= 1.4;
            if(!this.onLadder) this.actor.spriteRenderer.setAnimation("Idle");
        }


        this.actor.arcadeBody2D.setVelocity(velocity);
    }
}

Actions methods

isDown()
isAllDown()
wasJustPressed()
wasJustReleased()
Clone this wiki locally