css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              body {
    background-color: #333333;
    margin: 0 auto;
    text-align:center;
    
}
canvas {
    margin: 0 auto;
}
            
          
!
            
              /**
 * Created by zachary_beyer on 7/21/16.
 */
const SPEED = 10;
const FIRE_COOLDOWN = 3.0;



/**
 *  Window On Load INIT
 */

window.onload = () => {
    var game = new SimpleGame();
};

/**
 *  Lasers
 **/


interface ILaser {
    // class conforms to this interface imlpements these functions...
    cleanup();
    update();
}

class Laser implements ILaser {
    public gameController: any;
    public game: Phaser.Game;
    public momentumX: number;
    public momentumY: number;
    public colorCode: string;
    public size: number;
    public sprite: any;
    public spriteController: any;
    public owern: any;


    constructor(gameController:any, size:number) {
        this.gameController = gameController;
        this.game = gameController.game;
        this.colorCode = "#F57C00";
        this.size = size;
        this.spriteController = new CircleSpriteController(this.size,
            this.game,
            this.colorCode,
            CircleType.Pie,
            ControlType.Button);


        this.sprite = this.spriteController.sprite;
        // var n = this.gameController.lasers.length;
        // this.gameController.lasers.push[n] = this;
        this.gameController.lasers.push(this);
    }

    explode() {
        this.cleanup();
    }

    cleanup() {
        this.sprite.kill();
        console.log("LASERS CLEANING: %o",this.gameController.lasers.length);

        var array = this.gameController.lasers;
        var index = array.indexOf(this, 0);
        array.splice(index, 1);
        
        console.log("LASERS REMAIN: %o",array.length);
    }
    update() {
        this.sprite.x += this.momentumX;
        this.sprite.y += this.momentumY;

        var shouldDie = ((this.sprite.x > this.game.width) ||
        ((this.sprite.x + this.sprite.width) < 0) ||
        (this.sprite.y > this.game.height) ||
        ((this.sprite.y + this.sprite.height) < 0));
        if (shouldDie) {
            this.cleanup();
        }

    }


}

/**
 *  Ships
 */

interface ISpaceShipController {
    // class conforms to this interface imlpements these functions...
    shipInit();
}

class SpaceShipController implements ISpaceShipController {
    public gameController: any;
    public game: Phaser.Game;
    public coolDown: number;
    public ship: any;
    public colorCode: string;

    constructor(gameController:any) {
        this.gameController = gameController;
        this.game = gameController.game;
        this.colorCode = "#7B1FA2";
        this.ship;
    }

    shipInit() {
        this.ship = new CircleSpriteController(64,
            this.game,
            this.colorCode,
            CircleType.Pie,
            ControlType.Button);

        this.ship.sprite.x = (this.game.width -  this.ship.sprite.width) *0.5
        this.ship.sprite.y = this.game.height - this.ship.sprite.height * 2;
    }


    shootLaser() {
        if(this.coolDown > 0.0) {
            return;
        }

        this.coolDown = FIRE_COOLDOWN;

        var laser = new Laser(this.gameController, 24);
        laser.momentumX = 0.0;
        laser.momentumY =-SPEED*1.5;
        laser.owner = this;
        // laser.fire();
        laser.sprite.x = this.ship.sprite.x + (this.ship.sprite.width - laser.sprite.width) * 0.5;
        laser.sprite.y = this.ship.sprite.y - laser.sprite.height * 1.25;

    }
}

class PlayerController extends SpaceShipController {
    constructor(gameController:any){
        super(gameController);
        this.colorCode = "#0288D1";
    }

}

/**
 *  Simple Phaser Game
 */

class SimpleGame {

    public game:Phaser.Game;
    public player:PlayerController;
    public lasers:any[];

    constructor() {
        this.game = new Phaser.Game(640, 640, Phaser.AUTO, 'content', {
            create: this.create,
            update: this.update,
            moveLeft: this.moveLeft,
            moveRight: this.moveRight,
            moveUp: this.moveUp,
            moveDown: this.moveDown,
        });
    }

    create() {
        //This is display logic to stretch / shrink the canvas to the available window space
        this.game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
        this.game.scale.compatibility.scrollTo = false;
        this.game.scale.refresh();
        this.game.stage.backgroundColor = "#F5F5F5";

        this.game.input.mouse.capture = true;
        // this.lasers = this.game.add.group();
        this.lasers = new Array();

        this.player = new PlayerController(this);
        this.player.shipInit();
    }
    update() {

        for (var i = 0; i < this.lasers.length; i++) {
            var laser = this.lasers[i];
            if(laser){
                laser.update();
            }
        }

        if(this.player.coolDown > 0.0){
            this.player.coolDown -= 0.1
        }

        if (this.game.input.activePointer.leftButton.isDown ||
            this.game.input.keyboard.isDown(Phaser.Keyboard.SPACEBAR)) {
            this.player.shootLaser();
        }

        if (this.game.input.keyboard.isDown(Phaser.Keyboard.LEFT)||
            this.game.input.keyboard.isDown(Phaser.Keyboard.A)) {
            this.moveLeft();
        } else if (this.game.input.keyboard.isDown(Phaser.Keyboard.RIGHT)||
            this.game.input.keyboard.isDown(Phaser.Keyboard.D)) {
            this.moveRight();
        }

        if (this.game.input.keyboard.isDown(Phaser.Keyboard.UP)||
            this.game.input.keyboard.isDown(Phaser.Keyboard.W)) {
            this.moveUp();
        } else if (this.game.input.keyboard.isDown(Phaser.Keyboard.DOWN)||
            this.game.input.keyboard.isDown(Phaser.Keyboard.S)) {
            this.moveDown();
        }
    }
    moveLeft() {
        if(this.player.ship.sprite.x > 0 ) {
            this.player.ship.sprite.x -= SPEED;
        }
    }
    moveRight() {
        if((this.player.ship.sprite.x + this.player.ship.sprite.width) < this.game.width ) {
            this.player.ship.sprite.x += SPEED;
        }
    }
    moveUp() {
        if(this.player.ship.sprite.y > 0 ) {
            this.player.ship.sprite.y -= SPEED;
        }
    }
    moveDown() {
        if((this.player.ship.sprite.y + this.player.ship.sprite.height) < this.game.height ) {
            this.player.ship.sprite.y += SPEED;
        }
    }

    // this.game.lasers.forEach(
    //     function(laser : Laser) {
    //         laser.update();
    //     });

}

/**
 *  Circle Sprite Controller
 */

interface ICircleSpriteController {
    // class conforms to this interface imlpements these functions...
    // setProgress(progress : number = 0.5);
    // arcEndForProgress(progress: number);
    spriteInit();
}

enum CircleType {
    Pie,
    Ring,
    Donut
}

enum ControlType {
    Sprite,
    Button
}

class CircleSpriteController implements ICircleSpriteController {
    //Private property decleration
    private pi : number = Math.PI;
    private arcStart : number = 1.4999 * Math.PI;
    private size:number;
    private bitMapData: any;
    private circleType:CircleType;
    private colorCode:string;
    private controlType:ControlType;

    public game:Phaser.Game;
    public sprite: any;

    constructor(size : number = 128,
                game : Phaser.Game,
                colorCode : string = "#212121",
                circleType : any,
                controlType: any) {
        //Constructor runs when new circleSpriteController(...) is called.
        this.game = game;
        this.size = size;
        this.colorCode = colorCode;
        this.circleType = circleType;
        this.controlType = controlType;
        this.sprite;

        this.bitMapData = game.add.bitmapData(size, size);
        this.setProgress();
    }

    spriteInit() {
        if(this.sprite) {
            return;
        }

        this.sprite = this.game.add.sprite(0, 0, this.bitMapData);

        if(this.controlType == ControlType.Button) {
            this.sprite.inputEnabled = true;

            //  This will check the pixel every time the mouse moves, which is really expensive!
            //  You can also only do a pixel perfect check on click, which is much cheaper - so
            //  pick the right one accordingly.
            this.sprite.input.pixelPerfectOver = true;

            //  Enable the hand cursor
            this.sprite.input.useHandCursor = true;
        }
    }

    setProgress(progress : number = 1.0) {
        //Force the BitMapData to display a circle starting from the top-center and rotating around the BMD interpolated between 0 and 1.

        let stroke : number;
        // = CircleType_Pie
        switch (this.circleType) {
            case CircleType.Ring:
                //24 pixel stroke around the circle
                stroke = 24;
                break;

            case CircleType.Donut:
                stroke = this.size*0.25;
                break;

            case CircleType.Pie:
            default:
                stroke = this.size*0.5;
                break;
        }

        let offset : number = stroke;    //offset to prevent circle from exceeding the Bitmap's bounds
        let arcR : number = (this.size-offset)*0.5;  //The radius of the arc about to be drawn

        if(arcR < (stroke * 0.5)) {
            arcR = stroke * 0.5;    //insure that the arc radius
        }

        // Centering the circle at the center of the bitmap image...
        let pathStart : number = (this.size) * 0.5;

        this.bitMapData.clear();
        this.bitMapData.ctx.beginPath();

        this.bitMapData.ctx.arc(pathStart,
            pathStart,
            arcR,
            this.arcStart,
            this.arcEndForProgress(progress));

        this.bitMapData.ctx.lineWidth = stroke;
        this.bitMapData.ctx.strokeStyle = this.colorCode;
        this.bitMapData.ctx.stroke();
        this.bitMapData.ctx.closePath();

        if (!this.sprite) {
            this.spriteInit();
        }

    }

    arcEndForProgress(progress: number) {
        // This formula will end the arc at aproximately where it began if the progress is set to 1.0
        return this.arcStart + 2.001 * this.pi * progress;
    }
}
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console