-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworld.js
More file actions
110 lines (94 loc) · 3.23 KB
/
world.js
File metadata and controls
110 lines (94 loc) · 3.23 KB
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
import { SimpleEntity } from './entity.js';
import { Vector } from './vector.js';
import { XSlideFilter, YSlideFilter, GradientFilter, SwizzleFilter } from './filter.js';
export class World {
constructor(container, worldConfig, snuok, seed) {
this.container = container;
this.worldConfig = worldConfig;
this.filters = [];
this.snuok = snuok;
this.snuok.addTo(container);
this.seed = seed;
if (this.seed == undefined) {
this.seed = Math.floor(Math.random() * 1000000);
}
this.apple = this.createApple();
}
random() {
let x = Math.sin(this.seed++) * 1000000;
return x - Math.floor(x);
}
createApple() {
let possibilities = [];
for (let i = 0; i < this.worldConfig.MAP_WIDTH ; i++) {
for (let j = 0; j < this.worldConfig.MAP_HEIGHT ; j++) {
possibilities.push(`${i}.${j}`);
}
}
let consumedPoints = this.snuok.getPoints();
possibilities = possibilities.filter(x => {
return !this.snuok.getPoints().includes(x)
});
let choice = Math.floor(this.random() * possibilities.length);
let pos = possibilities[choice].split('.').map((x) => {
return parseInt(x);
});
return new Apple(this.container, this.worldConfig, new Vector(pos[0], pos[1]));
}
draw(delta) {
// TODO improve this... the snake should not be the controller of state ticks
let stateTick = this.snuok.draw(delta);
if (stateTick) {
if (this.snuok.checkCollides(this.apple.getHitBox(), 0)) {
// eat the apple
this.snuok.addTailPiece();
window.scoreTicker
.setValue(window.scoreTicker.value + 10);
let filter = this.apple.getFilter(this);
if (filter) {
this.addFilter(filter);
}
// get new apple
this.apple.destroy();
this.apple = this.createApple();
}
this.apple.stateTick();
}
}
addFilter(filter, timeout) {
this.filters.push(filter);
this.container.filters = this.filters;
}
removeFilter(filter) {
this.filters = this.filters.filter(f => f != filter);
this.container.filters = this.filters;
}
}
export class Apple extends SimpleEntity {
constructor(container, worldConfig, position) {
let sprite = new PIXI.Sprite(
PIXI.loader.resources["apple.png"].texture
);
super(worldConfig, sprite, position, new Vector(0,0));
this.addTo(container);
}
stateTick() {}
// Apply the effect
getFilter(world) {
let roll = Math.random() * 100;
let callback = world.removeFilter.bind(world);
if (roll > 70) {
return new XSlideFilter(callback);
}
if (roll > 40) {
return new YSlideFilter(callback);
}
if (roll > 20) {
return new GradientFilter(callback);
}
if (roll > 10) {
return new SwizzleFilter(callback);
}
return;
}
}