-
Notifications
You must be signed in to change notification settings - Fork 1
/
helper.js
165 lines (151 loc) · 4.1 KB
/
helper.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* jshint esversion: 6 */
function cons( str ) {
console.log( str );
}
function getRandomInt(min, max) {
return Math.floor(Math.floor(Math.random() * (max - min)) + min);
}
var KEYS = [];
var KEY = (function() {
let obj = {
add: function(name, keyCode) {
let key = {};
key.isDown = false;
key.keyUp = null;
KEY[name] = [keyCode];
KEYS[keyCode] = key;
},
addAlias: function(keycode, name) {
KEYS[keycode] = KEYS[this[name][0]];
this[name].push(keycode);
},
setUpHandler: function(key, upHandler) {
this[key].forEach(function(k) {
KEYS[k].keyUp = upHandler;
});
},
isUp: function(key) {
return !KEYS[KEY[key][0]].isDown;
},
isDown: function(key) {
return KEYS[KEY[key][0]].isDown;
}
};
window.addEventListener("keydown", function(e) {
let key = KEYS[e.keyCode];
if (key) {
key.isDown = true;
e.preventDefault();
}
}, false);
window.addEventListener("keyup", function(e) {
let key = KEYS[e.keyCode];
if (key) {
if (key.keyUp) key.keyUp();
key.isDown = false;
e.preventDefault();
}
}, false);
return obj;
})();
// Define keys
KEY.add("w", 87);
KEY.add("a", 65);
KEY.add("s", 83);
KEY.add("d", 68);
KEY.add("space", 32);
KEY.add("return", 13);
// Allow arrow keys for movement
KEY.addAlias(38, "w");
KEY.addAlias(40, "s");
KEY.addAlias(39, "d");
KEY.addAlias(37, "a");
// Allow pressing e instead of space
KEY.addAlias(69, "space");
function newText( text, callback=null, size=24, fill=0x000000, font="VT323") {
let tmp = new PIXI.Text(text, {fontFamily: font, fontSize: size, fill: fill, align: "center"});
tmp.anchor.set(0.5, 0.5);
if ( callback !== null ) {
tmp.interactive = true;
tmp.click = callback;
}
return tmp;
}
function rtax( rx ) {
return rx * WIDTH;
}
function rtay( ry ) {
return ry * HEIGHT;
}
function getTexture(name) {
return new PIXI.Sprite(PIXI.loader.resources[name].texture);
}
POINTS = (function() {
function Point(x, y) {
this.x = x;
this.y = y;
this.diff = function(p2) {
return new Point(p2.x - this.x, p2.y - this.y);
};
this.length = function() {
return Math.sqrt(Math.pow(this.x,2) + Math.pow(this.y,2));
};
this.dist = function(p2) {
return this.diff(p2).length();
};
this.add = function(p2) {
return new Point(this.x + p2.x, this.y + p2.y);
};
this.clone = function() {
return new Point(this.x, this.y);
};
this.unit = function(l=1) {
let x = this.x / this.length() * l;
let y = this.y / this.length() * l;
return new Point(x, y);
};
this.dot = function(p2) {
return this.x * p2.x + this.y * p2.y;
};
this.mult = function(fact) {
this.x *= fact;
this.y *= fact;
return this;
};
}
return {
ZERO: new Point(0,0),
fromRel: function(rx, ry) {
return new Point(rx*WIDTH, ry*HEIGHT);
},
fromAbs: function(x, y) {
return new Point(x, y);
},
dist: function(p1, p2) {
return p1.dist(p2);
},
new: function(x, y) {
return new Point(x,y);
},
dot: function(p1, p2) {
return p1.dot(p2);
},
fromPIXI: function(pp) {
return new Point(pp.x, pp.y);
}
};
})();
function getPossibleNext(index){
return adjlist[index][getRandomInt(0,adjlist[index].length)];
}
function _test_move(index) {
num = getPossibleNext(index);
GAME.player.pos = pointlist[num];
return num;
}
function start_test(iterations=20, sleep=1, n=12) {
if (iterations > 0) {
n = _test_move(n);
setTimeout(function(){ start_test(iterations-1, sleep, n); }, sleep*1000);
}
}