-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
76 lines (62 loc) · 2.26 KB
/
util.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
// returns a random integer between 0 and n-1
function randomInt(n) {
return Math.floor(Math.random() * n);
};
// returns a string that can be used as a rgb web color
function rgb(r, g, b) {
return "rgb(" + r + "," + g + "," + b + ")";
};
// returns a string that can be used as a hsl web color
function hsl(h, s, l) {
return "hsl(" + h + "," + s + "%," + l + "%)";
};
function distance(A, B) {
return Math.sqrt((B.x - A.x) * (B.x - A.x) + (B.y - A.y)*(B.y - A.y));
};
function collide(A, B) {
return (distance(A, B) < A.radius + B.radius);
};
function canSee(A, B) { // if A can see B
return (distance(A, B) < A.visualRadius + B.radius);
};
function getFacing(velocity) {
if (velocity.x === 0 && velocity.y === 0) return 4;
let angle = Math.atan2(velocity.y, velocity.x) / Math.PI;
if (-0.625 < angle && angle < -0.375) return 0;
if (-0.375 < angle && angle < -0.125) return 1;
if (-0.125 < angle && angle < 0.125) return 2;
if (0.125 < angle && angle < 0.375) return 3;
if (0.375 < angle && angle < 0.625) return 4;
if (0.625 < angle && angle < 0.875) return 5;
if (-0.875 > angle || angle > 0.875) return 6;
if (-0.875 < angle && angle < -0.625) return 7;
};
var currentChat;
function loadChat(chat) {
if (!chat) chat = {
message: "", resp1: { text: "" }, resp2: { text: "" }, resp3: { text: "" } };
currentChat = chat;
var chatBox = document.getElementById("message");
chatBox.innerHTML = chat.message;
var resp = document.getElementById("response1");
resp.innerHTML = chat.resp1.text;
resp = document.getElementById("response2");
resp.innerHTML = chat.resp2.text;
resp = document.getElementById("response3");
resp.innerHTML = chat.resp3.text;
};
// creates an alias for requestAnimationFrame for backwards compatibility
window.requestAnimFrame = (function () {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function (/* function */ callback, /* DOMElement */ element) {
window.setTimeout(callback, 1000 / 60);
};
})();
// add global parameters here
var PARAMS = {
DEBUG: true
};