Skip to content

Instantly share code, notes, and snippets.

@keyle
Created March 28, 2014 01:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keyle/9823187 to your computer and use it in GitHub Desktop.
Save keyle/9823187 to your computer and use it in GitHub Desktop.
/// <reference path="phaser.d.ts" />
declare var require;
var game:Phaser.Game;
var cursors:Phaser.CursorKeys;
var flipperLeft:endice.Flipper;
var flipperRight:endice.Flipper;
var ball:endice.Ball;
module endice {
export class Game {
public static WIDTH = 800;
public static HEIGHT = 850;
constructor() {
game = new Phaser.Game(Game.WIDTH, Game.HEIGHT, Phaser.AUTO, 'content', {
preload: this.preload,
create: this.create,
update: this.update,
render: this.render
});
}
private preload() {
game.stage.backgroundColor = "#f2f2f2";
game.load.image("flipperLeft", "assets/flipper-left.png");
game.load.image("flipperRight", "assets/flipper-right.png");
game.load.image("ball", "assets/ball.png");
game.load.image("item", "assets/item.png");
}
private create() {
cursors = game.input.keyboard.createCursorKeys();
game.physics.startSystem(Phaser.Physics.P2JS);
game.physics.p2.gravity.y = 400;
game.physics.p2.friction = 0;
game.physics.p2.applyDamping = false;
game.physics.p2.frameRate = 1 / 35;
flipperLeft = new Flipper(true);
flipperRight = new Flipper(false);
ball = new Ball();
var flipperMaterial = game.physics.p2.createMaterial('flipperMaterial');
// var flipperMaterial = game.physics.p2.createMaterial('flipperMaterial', flipperRight.sprite.body);
flipperLeft.sprite.body.setMaterial(flipperMaterial);
flipperRight.sprite.body.setMaterial(flipperMaterial);
var ballMaterial = game.physics.p2.createMaterial('ballMaterial', ball.sprite.body);
var contactMaterial = game.physics.p2.createContactMaterial(flipperMaterial, ballMaterial);
contactMaterial.friction = 0; // Friction to use in the contact of these two materials.
contactMaterial.restitution = 1.2; // Restitution (i.e. how bouncy it is!) to use in the contact of these two materials.
contactMaterial.stiffness = 1e7; // Stiffness of the resulting ContactEquation that this ContactMaterial generate.
contactMaterial.relaxation = 0; // Relaxation of the resulting ContactEquation that this ContactMaterial generate.
contactMaterial.frictionStiffness = 1e7; // Stiffness of the resulting FrictionEquation that this ContactMaterial generate.
contactMaterial.frictionRelaxation = 0; // Relaxation of the resulting FrictionEquation that this ContactMaterial generate.
contactMaterial.surfaceVelocity = 1.0; // Will add surface velocity to this material. If bodyA rests on top if bodyB, and the surface velocity is positive, bodyA will slide to the right.
game.add.text(15, Game.HEIGHT-35, "E", null);
game.add.text(450, Game.HEIGHT-35, "P", null);
var wall1 = game.add.sprite(480, 360, 'item');
game.physics.p2.enableBody(wall1, true);
wall1.body.moves = false;
wall1.body.mass = 100;
wall1.body.motionState = Phaser.Physics.P2.Body.STATIC;
// game.physics.startSystem(Phaser.Physics.NINJA);
// game.physics.ninja.gravity = 1.2;
// game.physics.startSystem(Phaser.Physics.ARCADE);
// game.physics.arcade.gravity.y = 1200;
}
private update() {
flipperLeft.update();
flipperRight.update();
// game.physics.arcade.collide(flipperLeft.sprite, ball.sprite);
}
private render() {
// game.debug.bodyInfo(this.tank1.tank, 32, 32);
// game.debug.cameraInfo(game.camera, 32, 32, "#f00");
// game.debug.text('Active Bullets: ' + Game.bullets.countLiving() + ' / ' + Game.bullets.total, 32, 32);
}
}
export class Flipper {
public sprite:Phaser.Sprite;
public isLeft = false;
constructor(isLeft:boolean) {
this.isLeft = isLeft;
var spritename = isLeft ? 'flipperLeft' : 'flipperRight';
var spritePosX = isLeft ? 50 : 450;
this.sprite = game.add.sprite(spritePosX, Game.HEIGHT - 180, spritename);
this.sprite.physicsEnabled = true;
game.physics.p2.enableBody(this.sprite, true);
this.sprite.body.mass = 1000;
this.sprite.body.moves = false;
this.sprite.body.motionState = Phaser.Physics.P2.Body.STATIC;
this.sprite.body.angularVelocity = 1; // that's the bounce
// this.sprite.body.angularAcceleration = 15;
// this.sprite.body.angularDamping = 0;
// this.sprite.body.setRectangle(258, 100, 78, 0); // right but wrong
// this.sprite.body.width = this.sprite.width * 2;
// this.sprite.body.setRectangle(this.sprite.width * 2, this.sprite.height, 0, 0); // right but doesn't work
// this.sprite.body.offset = new Phaser.Point(78, 0);
var pivot = isLeft ? -78 : 78;
this.sprite.pivot = new Phaser.Point(pivot, 0);
// this.sprite.body.pivot = new Phaser.Point(100, 00100);
// this.sprite.fixedToCamera = true;
// this.sprite.anchor.setTo(0, 0);
// this.sprite.body.mass = 0;
// this.sprite.body.position = [150, Game.HEIGHT - 180];
// game.physics.enable([this.sprite], Phaser.Physics.ARCADE);
// game.physics.arcade.enable(this.sprite);
// this.sprite.body.collideWorldBounds = true;
// this.sprite.body.immmovable = true;
// game.physics.ninja.enableAABB(this.sprite);
// game.physics.ninja.
}
public update() {
// this.sprite.position = new Phaser.Point(550, 150);
if(this.isLeft) {
if (game.input.keyboard.isDown(Phaser.Keyboard.E)) {
// this.sprite.angle += (-30 - this.sprite.angle) / 1;
this.sprite.body.angle += (-30 - this.sprite.body.angle) / 1.5;
// this.sprite.body.updateCollisionMask();
// this.sprite.body.rotateLeft(150);
}
else {
this.sprite.body.angle = 30;
}
}
else {
if (game.input.keyboard.isDown(Phaser.Keyboard.P)) {
// this.sprite.angle += (-30 - this.sprite.angle) / 1;
this.sprite.body.angle += (30 - this.sprite.body.angle) / 1.5;
// this.sprite.body.updateCollisionMask();
// this.sprite.body.rotateLeft(150);
}
else {
this.sprite.body.angle = -30;
}
}
// game.physics.arcade.collide(this.sprite, ball.sprite);
}
}
export class Ball {
public sprite:Phaser.Sprite;
constructor() {
this.sprite = game.add.sprite(100, 150, 'ball');
this.sprite.physicsEnabled = true;
this.sprite.anchor.setTo(0.5, 0.5);
game.physics.p2.enableBody(this.sprite, false);
this.sprite.body.setCircle(23);
this.sprite.body.angularVelocity = 0;
this.sprite.body.fixedRotation = true;
// this.sprite.body.angularVelocity = -51;
// this.sprite.body.allowRotation = false;
this.sprite.body.mass = 5;
this.sprite.body.friction = 1;
// game.physics.arcade.enable(this.sprite);
// game.physics.enable([this.sprite], Phaser.Physics.ARCADE);
// this.sprite.body.collideWorldBounds = true;
// this.sprite.body.
// this.sprite.body.bounce.setTo(0.95, 0.95);
// this.sprite.body.immmovable = false;
// game.physics.ninja.enableAABB(this.sprite);
// this.sprite.body.bounce.setTo(1, 1);
}
public update() {
//game.physics.p2.collide(this.sprite, flipperLeft.sprite);
}
}
}
var pinball;
window.onload = () => {
pinball = new endice.Game();
}
function trace(stuff:any):void {
console.log(stuff);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment