Commit d384b84c authored by wildfirecode's avatar wildfirecode

1

parent 975c66c3
export default class PieceControl {
private scene;
public scene;
private isDog: boolean;
private front;
private back;
......@@ -38,7 +38,9 @@ export default class PieceControl {
scaleX: 1,
ease:'Quad.easeInOut',
duration: PieceControl.FLIP_DURTION / 2,
onComplete: () => { callback && callback() }
onComplete: () => {
callback && callback()
}
}]
});
}
......
// import Piece from './Piece';
// import * as utils from './utils'
// import Swap from './Swap';
// export default class Scene extends createjs.Container {
// private _targets: number[];
// private _list: Piece[];
// SHOW_TIME = 1000;
// DELAY = 500;
// BORNS = 1;
// R = 3;
// TIMES = 3;
// N = 4;
// SIZE = 235;
// constructor() {
// super();
// this.initUI();
// this.update();
// }
// initUI() {
// const n = this.R * this.R;
// const targetList = utils.fillN(n);
// const randomTargetList = utils.shuffle(targetList);
// this._targets = randomTargetList.slice(0, this.BORNS);
// this._list = [];
// for (let index = 1; index <= n; index++) {
// const inTargets = this._targets.indexOf(index) != -1;
// const p = new Piece(!inTargets, index);
// p['index'] = index;
// this.addChild(p);
// this._list.push(p);
// p.x = ((index - 1) % this.R) * this.SIZE;
// p.y = Math.floor((index - 1) / this.R) * this.SIZE;
// p.flipImmediately();
// p.addEventListener('click', (e: createjs.MouseEvent) => {
// const p = (e.currentTarget as Piece);
// this.disable();
// let enable = false;
// const index = this._targets.indexOf(p.index);
// if (index != -1) {
// this._targets.splice(index, 1);
// if (this._targets.length == 0) {
// console.log('good.')
// } else {
// enable = true;
// }
// }
// else {
// console.log('sorry.')
// }
// p.flip(() => {
// if (enable) this.enable();
// });
// });
// }
// this.disable();
// }
// update() {
// const onResetComplete = () => {
// setTimeout(() => {
// Swap.startSwap(this._list, this.N, this.TIMES, this._targets[0], () => {
// this.onSwapComplete()
// })
// }, this.DELAY);
// }
// setTimeout(() => {
// for (let i = 0; i < this._list.length; i++) {
// const e = this._list[i];
// e.reset(i == 0 ? onResetComplete : null);
// }
// }, this.SHOW_TIME);
// }
// onSwapComplete() {
// console.log('onSwapComplete');
// this.enable();
// }
// disable() {
// for (const e of this._list) {
// e.mouseEnabled = false;
// }
// }
// enable() {
// for (const e of this._list) {
// e.mouseEnabled = true;
// }
// }
// }
\ No newline at end of file
import PieceControl from './PieceControl';
import * as utils from './utils'
import Swap from './Swap';
export default class SceneControl {
private scene;
private _targets: number[];
private _list: PieceControl[];
SHOW_TIME = 1000;
DELAY = 500;
BORNS = 2;
R = 3;
TIMES = 3;
N = 4;
SIZE = 235;
constructor(scene) {
this.scene = scene;
this.initUI();
this.update();
}
initUI() {
const n = this.R * this.R;
const targetList = utils.fillN(n);
const randomTargetList = utils.shuffle(targetList);
this._targets = randomTargetList.slice(0, this.BORNS);
this._list = [];
for (let index = 1; index <= n; index++) {
const inTargets = this._targets.indexOf(index) != -1;
const p = new PieceControl(this.scene, !inTargets, index);
p['index'] = index;
this._list.push(p);
p.container.x = ((index - 1) % this.R) * this.SIZE + 106 + 50;
p.container.y = Math.floor((index - 1) / this.R) * this.SIZE + 106 + 50;
p.flipImmediately();
// p.addEventListener('click', (e: createjs.MouseEvent) => {
// const p = (e.currentTarget as Piece);
// this.disable();
// let enable = false;
// const index = this._targets.indexOf(p.index);
// if (index != -1) {
// this._targets.splice(index, 1);
// if (this._targets.length == 0) {
// console.log('good.')
// } else {
// enable = true;
// }
// }
// else {
// console.log('sorry.')
// }
// p.flip(() => {
// if (enable) this.enable();
// });
// });
}
this.disable();
}
update() {
const onResetComplete = () => {
setTimeout(() => {
Swap.startSwap(this._list, this.N, this.TIMES, this._targets[0], () => {
this.onSwapComplete()
})
}, this.DELAY);
}
setTimeout(() => {
for (let i = 0; i < this._list.length; i++) {
const e = this._list[i];
e.reset(i == 0 ? onResetComplete : null);
}
}, this.SHOW_TIME);
}
onSwapComplete() {
console.log('onSwapComplete');
this.enable();
}
disable() {
// for (const e of this._list) {
// e.mouseEnabled = false;
// }
}
enable() {
// for (const e of this._list) {
// e.mouseEnabled = true;
// }
}
}
\ No newline at end of file
// import * as utils from './utils'
// import Piece from './Piece';
// export default class Swap {
// static DURATION = 0.25;
// static DELAY = .9;
import * as utils from './utils'
import PieceControl from './PieceControl';
export default class Swap {
static DURATION = 0.45;
static DELAY = .9;
// static startSwap(all: Piece[], n: number, times: number, target: number, onSwapComplete: Function) {
// for (let index = 0; index < times; index++) {
// const list = Swap.getList(all, n, target);
// setTimeout(() => {
// Swap.swap(list, index + 1 == times ? onSwapComplete : null);
// }, Swap.DURATION * 1000 * index + Swap.DELAY * 1000 * index);
// }
// }
// private static getList(all: Piece[], n: number, target: number) {
// const randomizedDeck = utils.shuffle(all);
// let targetPiece;
// const randomizedDeck2 = randomizedDeck.filter(e => {
// if (e.index == target)
// targetPiece = e;
// return e.index != target;
// });
// const list = randomizedDeck2.slice(0, n - 1);
// const m = n / 2;
// const list1 = list.slice(0, m);
// const list2 = list.slice(m);
// list2.push(targetPiece);
// return list1.map(
// (val, index) => { return { a: val, b: list2[index] } }
// )
// }
static startSwap(all: PieceControl[], n: number, times: number, target: number, onSwapComplete: Function) {
for (let index = 0; index < times; index++) {
const list = Swap.getList(all, n, target);
setTimeout(() => {
Swap.swap(list, index + 1 == times ? onSwapComplete : null);
}, Swap.DURATION * 1000 * index + Swap.DELAY * 1000 * index);
}
}
private static getList(all: PieceControl[], n: number, target: number) {
const randomizedDeck = utils.shuffle(all);
let targetPiece;
const randomizedDeck2 = randomizedDeck.filter(e => {
if (e.index == target)
targetPiece = e;
return e.index != target;
});
const list = randomizedDeck2.slice(0, n - 1);
const m = n / 2;
const list1 = list.slice(0, m);
const list2 = list.slice(m);
list2.push(targetPiece);
return list1.map(
(val, index) => { return { a: val, b: list2[index] } }
)
}
// private static swap(list: { a: Piece, b: Piece }[], onComplete: Function = null) {
// let tag = true;
// for (const e of list) {
// if (tag) {
// tag = false;
// Swap.swapOne(e.a, e.b, onComplete);
// } else {
// Swap.swapOne(e.a, e.b);
// }
// }
// }
private static swap(list: { a: PieceControl, b: PieceControl }[], onComplete: Function = null) {
let tag = true;
for (const e of list) {
if (tag) {
tag = false;
Swap.swapOne(e.a, e.b, onComplete);
} else {
Swap.swapOne(e.a, e.b);
}
}
}
// private static swapOne(a: Piece, b: Piece, onComplete: Function = null) {
// const ax = a.x;
// const ay = a.y;
// const bx = b.x;
// const by = b.y;
// const ease = createjs.Ease.quadInOut;
// // if (onComplete)
// // annie.Tween.to(b, Swap.DURATION, { x: ax, y: ay, onComplete: onComplete, ease: ease });
// // else
// // annie.Tween.to(b, Swap.DURATION, { x: ax, y: ay, ease: ease });
// // annie.Tween.to(a, Swap.DURATION, { x: bx, y: by, ease: ease });
// }
// }
\ No newline at end of file
private static swapOne(a: PieceControl, b: PieceControl, onComplete: Function = null) {
const ax = a.container.x;
const ay = a.container.y;
const bx = b.container.x;
const by = b.container.y;
const scene = a.scene;
if (onComplete) {
const tween = scene.tweens.add({
targets: b.container,
x: ax, y: ay,
ease: 'Quad.easeInOut',
duration: Swap.DURATION*1000,
onComplete: onComplete
});
}
else {
const tween = scene.tweens.add({
targets: b.container,
x: ax, y: ay,
ease: 'Quad.easeInOut',
duration: Swap.DURATION*1000,
});
}
const tween = scene.tweens.add({
targets: a.container,
x: bx, y: by,
ease: 'Quad.easeInOut',
duration:Swap.DURATION*1000,
});
}
}
\ No newline at end of file
import PieceControl from "./PieceControl";
import SceneControl from "./SceneControl";
const gameconfig = {
type: Phaser.AUTO,
width: 800,
height: 600,
height: 800,
scene: {
preload: preload,
create: create,
......@@ -20,14 +21,15 @@ function preload() {
}
function create() {
const a = new PieceControl(this, true, 0);
a.container.x = 107;
a.container.y = 107;
new SceneControl(this)
// const a = new PieceControl(this, true, 0);
// a.container.x = 107;
// a.container.y = 107;
// a.container.visible =false
a.resetImmediately();
setTimeout(() => {
a.flip();
}, 1000);
// a.resetImmediately();
// setTimeout(() => {
// a.flip();
// }, 1000);
// a.container.scaleX = 0.5
// a.flipImmediately();
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment