-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
78 lines (66 loc) · 2.07 KB
/
utils.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
game.utils = {
avg: function(array) {
if (!array.length) return 0;
let sum = 0;
array.forEach(x => sum+=x);
return sum/array.length;
},
discoverObjectsInRange: function(x, y, objects) {
// TODO: use viewport or some space partitioning to make this more performant? Will do for now...
objects.forEach(function(obj) {
if (obj.isHidden && game.utils.dist(x, y, obj.x, obj.y) < OBJECT_DISCOVERY_RANGE) {
if (obj.forceHide) return; // such objects need explicit reveal
game.utils.fadeInObject(obj);
}
});
},
dist: function(x0, y0, x1, y1) {
return Math.sqrt(
(x0-x1)*(x0-x1) + (y0-y1)*(y0-y1)
);
},
fadeInObject: function(obj) {
if (!obj.isHidden) {
// can be common once the beach is reached - ie. with footsteps
//console.warn('Object is already displayed or fading in:', obj);
return;
}
obj.isHidden = false;
delete obj.forceHide;
obj.isFadingIn = true;
obj.fadeCounter = 0;
},
fadeOutObject: function(obj) {
if (obj.isFadingOut) {
console.warn('Object is already fading out:', obj);
return;
}
obj.isFadingOut = true;
obj.fadeCounter = 0;
},
getTaskIndexFromID: function(taskID) {
let taskIndex;
game.tasks.forEach(function(task, i) {
if (task.id === taskID) {
taskIndex = i;
}
});
return taskIndex;
},
swapObjects: function(idA, idB) {
const objA = game.utils.findObjectByID(idA, game.state.objects);
game.utils.fadeOutObject(objA);
const objB = game.utils.findObjectByID(idB, game.state.objects);
game.utils.fadeInObject(objB);
},
isObjectInProximity: function(playerCoords, objectCoords, largeObject) {
const xDist = Math.abs(playerCoords.x - objectCoords.x);
const yDist = Math.abs(playerCoords.y - objectCoords.y);
const distSquared = xDist*xDist + yDist*yDist;
const radius = largeObject? ACTIVITY_RADIUS*2 : ACTIVITY_RADIUS;
return distSquared < radius*radius;
},
findObjectByID: function(id, objects) {
return objects.filter(o => o.id === id)[0];
}
};