Skip to content

Commit

Permalink
feat: add game logic for winning
Browse files Browse the repository at this point in the history
  • Loading branch information
benox3 committed Jan 27, 2019
1 parent 692ca59 commit eb76120
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 34 deletions.
99 changes: 70 additions & 29 deletions src/scenes/Main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Player, Block } from '../sprites';
import { Block, Player, Vip } from '../sprites';
import { FRAMES, SCENES, SHEET, TEXTURES } from '../constants';
import { Scene } from 'phaser';
import { Score } from '../texts';
Expand All @@ -7,10 +7,18 @@ const DEGREES = {
'90': Math.PI / 2,
};

const cratesLeftTextTemplate = count => `Crates: ${count}`;

export default class Main extends Scene {
constructor() {
super({ key: SCENES.MAIN });
}

init() {
this.Mark = null;
this.hasBallLaunched = false;
this.cratesAllowed = 3;
this.cratesPlaced = 0;
}

create() {
Expand All @@ -25,50 +33,83 @@ export default class Main extends Scene {
);
this.add.image(0, 0, TEXTURES.LIVING_ROOM).setOrigin(0, 0);

// this.matter.add.sprite(200, 50, TEXTURES.SHEET, FRAMES.CRATE, { shape: shapes[FRAMES.CRATE] });
// this.matter.add.sprite(250, 250, TEXTURES.SHEET, FRAMES.BANANA, {
// shape: shapes[FRAMES.BANANA],
// });
// this.matter.add.sprite(360, 50, TEXTURES.SHEET, FRAMES.ORANGE, {
// shape: shapes[FRAMES.ORANGE],
// });
// this.matter.add.sprite(400, 250, TEXTURES.SHEET, FRAMES.CHERRIES, {
// shape: shapes[FRAMES.CHERRIES],
// });
this.Mark = new Vip(this.matter.world, 200, 750);

this.Mark = this.matter.add.sprite(200, 200, TEXTURES.BEAR, { isStatic: true })
this.cratesLeftText = this.add.text(
20,
20,
cratesLeftTextTemplate(this.cratesAllowed - this.cratesPlaced),
{
fontSize: 64,
fontFamily: 'Arial',
fill: '#ffffff',
}
);

this.input.on(
'pointerdown',
pointer => {
new Block(this.matter.world, pointer.x, pointer.y, TEXTURES.CRATE, 'sheet');
}, this
if (this.cratesPlaced < this.cratesAllowed) {
const block = new Block(this.matter.world, pointer.x, pointer.y);
block.init();
this.cratesPlaced++;
this.cratesLeftText.text = cratesLeftTextTemplate(
this.cratesAllowed - this.cratesPlaced
);
}
},
this
);

this.time.addEvent({
delay: 1000,
callback: this.launchBall,
callbackScope: this,
this.matter.world.on('collisionstart', function(event, bodyA, bodyB) {
if (bodyA && bodyA.gameObject && bodyA.gameObject.onCollision) {
bodyA.gameObject.onCollision(bodyB);
}
if (bodyB && bodyB.gameObject && bodyB.gameObject.onCollision) {
bodyB.gameObject.onCollision(bodyA);
}
});
}

launchBall() {
const ball = this.matter.add.sprite(0, 0, TEXTURES.SHEET, FRAMES.ORANGE, {
shape: this.shapes.orange,
this.ball = this.matter.add.sprite(0, 0, TEXTURES.SHEET, FRAMES.ORANGE, {
shape: this.shapes[FRAMES.ORANGE],
});
ball.setMass(30);
ball.setVelocity(4, -2);
ball.setBounce(1);
ball.setFriction(0, 0, 0);
ball.setFrictionAir(0.005);
this.ball.setMass(30);
this.ball.setVelocity(4, -2);
this.ball.setBounce(1);
this.ball.setFriction(0, 0, 0);
this.ball.setFrictionAir(0.005);
}
update() {

update(time, delta) {
if (
this.Mark.body.angle >= DEGREES['90'] ||
this.Mark.body.angle <= -(DEGREES['90'])
this.Mark.body.angle <= -DEGREES['90']
) {
this.scene.start(SCENES.GAME_OVER);
}

if (this.ball && this.ball.body) {
const motion =
this.ball.body.speed * this.ball.body.speed +
this.ball.body.angularSpeed * this.ball.body.angularSpeed;

const isResting = motion < 0.1;

if (isResting) {
this.add.text(100, 400, 'DAN RULES. TRY AGAIN?', {
color: '#00ff00',
fontFamily: 'Arial',
fontSize: 48,
});

this.input.once('pointerdown', () => this.scene.start(SCENES.MAIN));
}
}

if (!this.hasBallLaunched && this.cratesPlaced === this.cratesAllowed) {
this.hasBallLaunched = true;
this.launchBall();
}
}
}
17 changes: 12 additions & 5 deletions src/sprites/Block.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@ import { Physics } from 'phaser';
import { FRAMES, TEXTURES } from '../constants';

export default class Block extends Physics.Matter.Sprite {
constructor(world, x, y, texture, frame) {
super(world, x, y, TEXTURES.SHEET, FRAMES.CRATE);

const shapes = world.scene.cache.json.get('shapes');

constructor(world, x, y, texture, frame, options) {
super(world, x, y, TEXTURES.SHEET, FRAMES.CRATE, {
shape: world.scene.cache.json.get('shapes')[FRAMES.CRATE],
});
world.scene.add.existing(this);
}

init() {
this.setVelocity(0);
this.setBounce(0);
this.setFriction(1);
this.setFrictionAir(0);
this.setMass(300);
}
}
1 change: 1 addition & 0 deletions src/sprites/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { default as Player } from './Player';
export { default as Block } from './Block';
export { default as Vip } from './Vip';

0 comments on commit eb76120

Please sign in to comment.