Skip to content

Instantly share code, notes, and snippets.

@maxmonax
Created June 27, 2017 05:53
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 maxmonax/fd8ea7605a7558986aa152c63338f9fd to your computer and use it in GitHub Desktop.
Save maxmonax/fd8ea7605a7558986aa152c63338f9fd to your computer and use it in GitHub Desktop.
Memory card class
module PhaserGame.Client {
const TIME_OPEN = 350;
const TURN_WITH_ANIM = true;
export class Card3 extends Phaser.Sprite {
private dummyMain: Phaser.Sprite;
private dummyShirt: Phaser.Sprite;
private dummyFace: Phaser.Sprite;
private imgBack: Phaser.Sprite;
private fr: Phaser.Sprite;
private shirt: Phaser.Sprite;
private img: Phaser.Sprite;
id: number;
isOpened: boolean = false;
isBusy: boolean = false;
isFound: boolean = false;
onOpenedSignal = new Phaser.Signal();
constructor(game, x, y, cid: number) {
super(game, x, y);
this.id = cid;
this.dummyMain = new Phaser.Sprite(game, 0, 0);
this.addChild(this.dummyMain);
this.dummyFace = new Phaser.Sprite(game, 0, 0);
this.dummyFace.visible = false;
this.dummyMain.addChild(this.dummyFace);
this.dummyShirt = new Phaser.Sprite(game, 0, 0);
this.dummyMain.addChild(this.dummyShirt);
// shirt
this.shirt = new Phaser.Sprite(game, 2, 2, 'game', Params.isTablet ? 'card_back_t' : 'card_back');
this.shirt.anchor.set(0.5);
this.dummyShirt.addChild(this.shirt);
// blank
this.imgBack = new Phaser.Sprite(game, 0, 0, 'game', Params.isTablet ? 'card_blank_t' : 'card_blank');
this.imgBack.anchor.set(0.5);
this.imgBack.scale.set(1.04);
this.dummyFace.addChild(this.imgBack);
// card img
try {
this.img = new Phaser.Sprite(game, 0, 0, 'game', 'symbol' + String(cid));
this.img.anchor.set(0.5);
if (!Params.isTablet) this.img.scale.set(0.75);
this.dummyFace.addChild(this.img);
}
catch (e) {
LogMng.error('fail loading card image: ' + e);
}
// frame
var fr_key = 'card_v' + String(MyMath.randomIntInRange(1, 2));
if (Params.isTablet) fr_key += '_t';
this.fr = new Phaser.Sprite(this.game, 5, 5, 'game', fr_key);
this.fr.anchor.set(0.5);
this.dummyFace.addChild(this.fr);
}
doOpen() {
if (this.isOpened) return;
this.isOpened = true;
this.isBusy = true;
if (TURN_WITH_ANIM) {
this.game.add.tween(this.dummyShirt.scale).to({ x: 0 }, TIME_OPEN / 2, Phaser.Easing.Linear.None, true).onComplete.add(this.onOpenComplete1, this);
this.game.add.tween(this.dummyMain.scale).to({ x: 1.1, y: 1.1 }, TIME_OPEN, Phaser.Easing.Sinusoidal.InOut, true, 50, 0, true);
}
else {
this.onOpenComplete1();
}
}
onOpenComplete1() {
if (TURN_WITH_ANIM) this.dummyFace.scale.x = 0;
this.dummyFace.visible = true;
this.dummyShirt.visible = false;
if (TURN_WITH_ANIM) {
this.game.add.tween(this.dummyFace.scale).to({ x: 1 }, TIME_OPEN / 2, Phaser.Easing.Linear.None, true).onComplete.add(this.onOpenComplete2, this);
}
else {
this.onOpenComplete2();
}
}
onOpenComplete2() {
this.isBusy = false;
this.onOpenedSignal.dispatch();
}
doClose() {
if (!this.isOpened) return;
this.isOpened = false;
this.isBusy = true;
if (TURN_WITH_ANIM) {
this.game.add.tween(this.dummyFace.scale).to({ x: 0 }, TIME_OPEN / 2, Phaser.Easing.Linear.None, true).onComplete.add(this.onCloseComplete1, this);
this.game.add.tween(this.dummyMain.scale).to({ x: 1.1, y: 1.1 }, TIME_OPEN, Phaser.Easing.Sinusoidal.InOut, true, 50, 0, true);
}
else {
this.onCloseComplete1();
}
}
onCloseComplete1() {
this.dummyFace.visible = false;
if (TURN_WITH_ANIM) this.dummyShirt.scale.x = 0;
this.dummyShirt.visible = true;
if (TURN_WITH_ANIM) {
this.game.add.tween(this.dummyShirt.scale).to({ x: 1 }, TIME_OPEN / 2, Phaser.Easing.Linear.None, true).onComplete.add(this.onCloseComplete2, this);
}
else {
this.onCloseComplete2();
}
}
onCloseComplete2() {
this.isBusy = false;
}
doFound() {
this.isOpened = false;
this.isBusy = false;
this.isFound = true;
}
doHelp() {
var tw1 = this.game.add.tween(this.dummyMain).to({ angle: -5 }, 500, Phaser.Easing.Sinusoidal.InOut);
var tw2 = this.game.add.tween(this.dummyMain).to({ angle: 5 },
1000, Phaser.Easing.Sinusoidal.InOut, false, 0, 1, true);
var tw3 = this.game.add.tween(this.dummyMain).to({ angle: 0 }, 500, Phaser.Easing.Sinusoidal.InOut);
tw1.chain(tw2);
tw2.chain(tw3);
tw1.start();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment