-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
80 lines (63 loc) · 1.91 KB
/
index.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
//Welcome to NinJS, trust me this is a super cool library
/*To use functions related to physics and graphics note that
the sprites used as parameters need to be an array! It is
recommended that you use nested arrays to store clones of
sprites. Hopefully this library will shorten your code!*/
mySprite = [[0,0,100,100]] //An example to help you
//Sprite functions
function boxCollision(sprite1, sprite2) {
if (sprite1[0] + sprite1[2]/2 > sprite2[0] - sprite2[2]/2 && sprite1[0] - sprite1[2]/2 < sprite2[0] + sprite2[2]/2 && sprite1[1] + sprite1[3]/2 > sprite2[1] - sprite2[3]/2 && sprite1[1] - sprite1[3]/2 < sprite2[1] + sprite2[3]/2) {
return true
} else {
return false
}
}
function center(sprite) {
sprite[0] -= sprite[2]
sprite[1] -= sprite[3]
}
function distance(sprite1, sprite2) {
return Math.sqrt(((sprite1[0] - sprite2[0])*(sprite1[0] - sprite2[0]))+((sprite1[1] - sprite2[1])*(sprite1[1] - sprite2[1])))
}
//Data functions
function filterInt(value) {
//Filters a string into a number
if (/^[-+]?(\d+|Infinity)$/.test(value)) {
return Number(value)
} else {
return NaN
}
}
function intArray(input, output) {
for (i=0;i<input.length;i++) {
output[i] = filterInt(input[i])
}
}
function addLastItem(item, array) {
array.push(item)
return array.length
}
function deleteLastItem(array) {
array.pop()
return array.length
}
function deleteRange(start, end, array) {
array.splice(start, end - start)
return array
}
function deleteIndex(index, array) {
array.splice(index, index + 1)
return array
}
function deleteItem(item, array) {
array.splice(array.indexOf(item), 1)
return item
}
function replaceIndex(index, newItem, array) {
array.splice(index, 1, newItem)
return array
}
function replaceItem(item, newItem, array) {
array.splice(array.indexOf(item), 1, newItem)
return array
}