-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.lua
More file actions
117 lines (89 loc) · 2.81 KB
/
Copy pathutils.lua
File metadata and controls
117 lines (89 loc) · 2.81 KB
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
function getCachedImage (imageFile)
local img
if imageCache and imageCache[imageFile] then
img = imageCache[imageFile]
else
img = love.graphics.newImage(imageFile)
imageCache = imageCache or {}
imageCache[imageFile] = img
end
return img
end
gGfxSourceSizes = {}
function getGfxSourceSize(gfx)
local arr = gGfxSourceSizes[gfx]
if (arr) then return unpack(arr) end
end
function newPaddedImage(filename)
local source = love.image.newImageData(filename)
local w, h = source:getWidth(), source:getHeight()
-- Find closest power-of-two.
local wp = math.pow(2, math.ceil(math.log(w)/math.log(2)))
local hp = math.pow(2, math.ceil(math.log(h)/math.log(2)))
local gfx = nil
-- Only pad if needed:
if wp ~= w or hp ~= h then
local padded = love.image.newImageData(wp, hp)
padded:paste(source, 0, 0)
gfx = love.graphics.newImage(padded)
else
gfx = love.graphics.newImage(source)
end
-- originalgroesse in die groessenfunctionen patchen
local sourceW = source:getWidth()
local sourceH = source:getHeight()
gGfxSourceSizes[gfx] = {sourceW, sourceH}
return gfx
end
function getCachedPaddedImage (imageFile)
local img
if imageCachePadded and imageCachePadded[imageFile] then
img = imageCachePadded[imageFile]
else
img = newPaddedImage(imageFile)
imageCachePadded = imageCachePadded or {}
imageCachePadded[imageFile] = img
end
return img
end
function findNearestObjectInKeyList(x,y, list, ignoreThis)
local nearest, mindist = nil,nil
for k,v in pairs(list) do
if k ~= ignoreThis and k.x and k.y then
local dist = sqlen2(x - k.x, y - k.y)
if mindist == nil or dist < mindist then
nearest = k
mindist = dist
end
end
end
return nearest, mindist and math.sqrt(mindist) or nil
end
function findNearestObjectInKeyLists(x,y, lists, ignoreThis)
local nearest, mindist = nil,nil
for k,list in pairs(lists) do
local n, dist = findNearestObjectInKeyList(x,y,list,ignoreThis)
if mindist == nil or dist < mindist then
nearest = n
mindist = dist
end
end
return nearest, mindist
end
function VectorLength(vx,vy) return math.sqrt(vx*vx + vy*vy) end
function VectorNormalized(vx,vy) local f = VectorLength(vx,vy) f = (f==0) and 1 or 1/f return f*vx,f*vy end
function VectorDot(ax,ay,bx,by) return ax*bx + ay*by end
function VectorNormDot(ax,ay,bx,by)
ax,ay = VectorNormalized(ax,ay)
bx,by = VectorNormalized(bx,by)
return VectorDot(ax,ay,bx,by)
end
function Point2PointDistance(x1, y1, x2, y2) return VectorLength(x2 - x1,y2 - y1) end
function rand2 (vmin,vmax) return vmin + (vmax-vmin)*math.random() end
function sign(x) return (x/abs(x)) end
function playSFX(effect)
love.audio.stop(effect)
if(effect:isPaused() == true or effect:isStopped() == true) then
love.audio.play(effect)
end
end