-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.js
95 lines (84 loc) · 1.97 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
function euclideanDistance(point1, point2) { //returns distance between 2 points or hypoteneuse of 1 point
if (point2)
return (Math.sqrt(Math.pow(point2.x - point1.x, 2) + Math.pow(point2.y - point1.y, 2)));
else
return Math.sqrt(Math.pow(point1.x, 2) + Math.pow(point1.y, 2));
}
function toDirection(vector) //returns angle in radians of the given vector from 0,0
{
return Math.atan2(vector.y, vector.x);
}
function toVector(direction) //returns a unit vector pointing in the specified angle in radians from 0,0
{
return new Vector(Math.cos(direction), Math.sin(direction));
}
function findClosest(closestTo, list)
{
var minDistance = 52560000;
var ownPosition = closestTo.position;
var closest;
for(agent in list)
{
var dist = euclideanDistance(ownPosition, list[agent].position);
if(dist < minDistance)
{
minDistance = dist;
closest = list[agent];
}
}
return closest;
}
function getTile(point)
{
var tile = new Vector(0,0);
tile.x = Math.floor((point.x + game.canvas.width/2) / tileSize);
tile.y = Math.floor((point.y + game.canvas.height/2) / tileSize);
return tile;
}
function getRect(tile)
{
var rect = new Object();
rect.x1 = (tile.x * tileSize) - game.canvas.width/2;// + tileOffset;
rect.x2 = rect.x1 + tileSize;
rect.y1 = (tile.y * tileSize) - game.canvas.height/2;// + tileOffset;
rect.y2 = rect.y1 + tileSize;
return rect;
}
function horizontalDistance(point, tile)
{
var rect = getRect(tile);
if(rect.x1 > point.x)
{
return rect.x1 - point.x;
}
if(rect.x2 < point.x)
{
return point.x - rect.x2;
}
return 0;
}
function verticalDistance(point, tile)
{
var rect = getRect(tile);
if(rect.y1 > point.y)
{
return rect.y1 - point.y;
}
if(rect.y2 < point.y)
{
return point.y - rect.y2;
}
return 0;
}
//DOESN'T WORK and I don't care.
function isImpassible(tile)
{
if(game.map[tile.x][tile.y] == 2)
return true;
if(game.map[tile.x][tile.y] == 3)
return true;
if(game.map[tile.x][tile.y] > 4)
return true;
return false;
}
//*/