cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

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.

            
              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;
const SHIP_SIZE = 64;
var cursors;



/**
 *  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);
        
        
        this.game.physics.p2.enable(this.sprite, false);
        // this.sprite.body.setCircle(this.size);
        // this.sprite.body.fixedRotation = true;

        var shipCollisionGroup = this.gameController.shipCollisionGroup;
        var laserCollisionGroup = this.gameController.laserCollisionGroup;
        var itemCollisionGroup = this.gameController.itemCollisionGroup;

        //  Set the ships collision group
        this.sprite.body.setCollisionGroup(laserCollisionGroup);
        //  The ship will collide with the pandas, and when it strikes one the hitPanda callback will fire, causing it to alpha out a bit
        //  When pandas collide with each other, nothing happens to them.
        this.sprite.body.collides(shipCollisionGroup, this.explode, this);
        this.sprite.body.collides(laserCollisionGroup, this.explode, this);
    }

    explode() {
        // this.cleanup();
    }

    cleanup() {
        this.sprite.kill();

        var array = this.gameController.lasers;
        var index = array.indexOf(this, 0);
        array.splice(index, 1);
        
    }
    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();
    takeDamage();
}

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(SHIP_SIZE,
            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;
        
        this.game.physics.p2.enable(this.ship.sprite, false);
        // this.ship.sprite.body.setCircle(SHIP_SIZE);
        // this.ship.sprite.body.fixedRotation = true;

        var shipCollisionGroup = this.gameController.shipCollisionGroup;
        var laserCollisionGroup = this.gameController.laserCollisionGroup;
        var itemCollisionGroup = this.gameController.itemCollisionGroup;

        //  Set the ships collision group
        this.ship.sprite.body.setCollisionGroup(shipCollisionGroup);
        //  The ship will collide with the pandas, and when it strikes one the hitPanda callback will fire, causing it to alpha out a bit
        //  When pandas collide with each other, nothing happens to them.
        this.ship.sprite.body.collides(shipCollisionGroup, this.takeDamage, this);
        this.ship.sprite.body.collides(laserCollisionGroup, this.takeDamage, this);
        this.ship.sprite.body.collides(itemCollisionGroup, this.getItem, this);
            // shipCollisionGroup
            // this.ship.spite
    }
    
    takeDamage() {
        console.log("I AM HIT!");
    }
    
    getItem() {
        console.log("ITEM GET!");
    }

    
    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[];

    public shipCollisionGroup:any;
    public laserCollisionGroup:any;
    public itemCollisionGroup: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() {
        // var game = this.game;
        
        //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";

        
        //  Enable P2
        this.game.physics.startSystem(Phaser.Physics.P2JS);

        //  Turn on impact events for the world, without this we get no collision callbacks
        this.game.physics.p2.setImpactEvents(true);
        this.game.physics.p2.restitution = 0.8;

        //  Create our collision groups. One for the player, one for the pandas
        this.shipCollisionGroup = this.game.physics.p2.createCollisionGroup();
        this.laserCollisionGroup = this.game.physics.p2.createCollisionGroup();
        this.itemCollisionGroup = this.game.physics.p2.createCollisionGroup();
        
        
        this.game.input.mouse.capture = true;
        // this.lasers = this.game.add.group();
        this.lasers = new Array();

        cursors = this.game.input.keyboard.createCursorKeys();

        
        this.player = new PlayerController(this);
        this.player.shipInit();
    }
    update() {
        
        var ship = this.player.ship.sprite;    
        ship.body.setZeroVelocity();

        if (cursors.left.isDown)
        {
            ship.body.moveLeft(200);
        }
        else if (cursors.right.isDown)
        {
            ship.body.moveRight(200);
        }

        if (cursors.up.isDown)
        {
            ship.body.moveUp(200);
        }
        else if (cursors.down.isDown)
        {
            ship.body.moveDown(200);
        }

//         if (!game.camera.atLimit.x)
//         {
//             starfield.tilePosition.x += (ship.body.velocity.x * 16) * this.game.time.physicsElapsed;
//         }

//         if (!game.camera.atLimit.y)
//         {
//             starfield.tilePosition.y += (ship.body.velocity.y * 16) * this.game.time.physicsElapsed;
//         }


        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
Loading ..................

Console