-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
129 lines (116 loc) · 3.92 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import '../styles/index.scss';
import * as PIXI from 'pixi.js';
import TWEEN from '@tweenjs/tween.js';
import PatternMatcher from './patterns';
import audio from './audio';
import ui from './ui';
import { popIn, shrink, bounce } from './tween'
const app = new PIXI.Application(
{
width: 870,
height: 870
}
);
const patternMatcher = new PatternMatcher();
document.body.appendChild(app.view);
const loader = PIXI.Loader.shared;
const animals = ['bear', 'buffalo', 'chick', 'chicken',
'cow', 'crocodile', 'dog', 'duck', 'elephant', 'frog', 'giraffe',
'goat', 'gorilla', 'hippo', 'horse', 'monkey', 'moose',
'narwhal', 'owl', 'panda', 'parrot', 'penguin', 'pig',
'rabbit', 'rhino', 'sloth', 'snake', 'walrus', 'whale', 'zebra'
], TILES_OX = 6, TILES_OY = 6, SPRITE_WIDTH = 140, SPRITE_HEIGHT = 140, PADDING = 15, sprites = [];
let selected = undefined, selectedBounce = undefined, resources;
function getRandomAnimalName() {
return animals[Math.trunc(Math.random() * 10)];
}
function onTexturesLoaded() {
for (let x = 0; x < TILES_OX; x++) {
sprites[x] = [];
for (let y = 0; y < TILES_OY; y++) {
const sprite = createSprite(getRandomAnimalName(), x, y);
sprites[x][y] = sprite;
app.stage.addChild(sprite);
}
}
}
function onSpriteClick(sprite) {
audio.fx.click();
if (!selected) {
selected = sprite;
selectedBounce?.stop();
selectedBounce = bounce(selected);
} else {
if (selected === sprite) {
// can't swap element with itself
return;
}
// swap pixi coordinates
const xa = selected.x, ya = selected.y;
selected.x = sprite.x;
selected.y = sprite.y;
sprite.x = xa;
sprite.y = ya;
// swap array position
const positionA = selected.customData, positionB = sprite.customData;
selected.customData = positionB;
sprite.customData = positionA;
sprites[positionA.x][positionA.y] = sprite;
sprites[positionB.x][positionB.y] = selected;
selectedBounce?.stop();
selected = undefined;
runPatternCheck();
}
}
function runPatternCheck() {
const strGroups = sprites.map(col => col.map(element => element.name));
const groups = patternMatcher.matchAllGroups(strGroups);
if (groups.length) {
audio.fx.nextLine();
groups.forEach((group) => {
ui.log(group);
group.points.map(({ x, y }) => sprites[x][y]).forEach(sprite => {
const { x, y } = sprite.customData;
const newSprite = createSprite(getRandomAnimalName(), x, y);
sprites[x][y] = newSprite;
shrink(sprite).then(() => {
app.stage.removeChild(sprite);
app.stage.addChild(newSprite);
ui.score += 100;
runPatternCheck();
})
})
});
}
}
function createSprite(randomAnimal, x, y) {
const texture = resources[randomAnimal].texture;
const sprite = new PIXI.Sprite(texture);
sprite.name = randomAnimal;
sprite.anchor.x = 0.5;
sprite.anchor.y = 0.5;
sprite.x = x * SPRITE_WIDTH + SPRITE_WIDTH / 2 + PADDING;
sprite.y = y * SPRITE_HEIGHT + SPRITE_HEIGHT / 2 + PADDING;
sprite.interactive = true;
sprite.buttonMode = true;
// store array position
sprite.customData = { x, y };
sprite.on('pointerdown', ({ target }) => onSpriteClick(target));
popIn(sprite);
return sprite;
}
function update() {
TWEEN.update(performance.now())
ui.update();
}
// executable beeps-and-boops
const imageFilesToLoad = animals.map(str => ({ name: str, url: `public/images/${str}.png` }));
loader.add(imageFilesToLoad).load(
(loader, res) => {
resources = res;
onTexturesLoaded();
runPatternCheck();
}
);
// Listen for frame updates
app.ticker.add(update);