-
Notifications
You must be signed in to change notification settings - Fork 0
/
enemies.lua
176 lines (147 loc) · 4.52 KB
/
enemies.lua
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
function enemies_load()
defaultvalues = {quadcount=1, quadno=1}
enemiesdata = {}
enemies = {}
--ENEMIIIIEEES
loaddelayed = {}
local fl = love.filesystem.getDirectoryItems("enemies/")
local fl2 = love.filesystem.getDirectoryItems("mappacks/" .. mappack .. "/enemies/")
local realfl = {}
for i = 1, #fl do
table.insert(realfl, "enemies/" .. fl[i]) --STANDARD ENEMIES
end
for i = 1, #fl2 do
table.insert(realfl, "mappacks/" .. mappack .. "/enemies/" .. fl2[i]) --MAPPACK ENEMIES
end
for i = 1, #realfl do
if string.sub(realfl[i], -4) == "json" then
loadenemy(realfl[i])
end
end
end
function loadenemy(filename)
local ss = filename:split("/")
local folder = ""
for i = 1, #ss-1 do
folder = folder .. ss[i] .. "/"
end
local s = string.sub(ss[#ss], 1, -6):lower()
--CHECK FOR - , ; * AND WHATEVER ELSE WOULD BREAK MAPS
local skip = false
local badlist = {",", ";", "-", "*"} --badlist sounds like a movie
for i = 1, #badlist do
if s:find(badlist[i]) then
skip = true
end
end
if not skip then --bad letter
local data = love.filesystem.read(filename)
data = data:gsub("\r", "")
if string.sub(data, 1, 4) == "base" then
local split = data:split("\n")
local base = string.sub(split[1], 6)
if enemiesdata[base] then
local newdata = ""
for i = 2, #split do
newdata = newdata .. split[i]
if i ~= #split then
newdata = newdata .. "\n"
end
end
enemiesdata[s] = usebase(enemiesdata[base])
local temp = JSON:decode(newdata)
for i, v in pairs(temp) do
enemiesdata[s][i] = v
end
else
--Put enemy for later loading because base hasn't been loaded yet
if not success then
if not loaddelayed[base] then
loaddelayed[base] = {}
end
table.insert(loaddelayed[base], filename)
end
print("DON'T HAVE BASE " .. base .. " FOR " .. s)
return
end
else
enemiesdata[s] = JSON:decode(data)
end
--CASE INSENSITVE THING
for i, v in pairs(enemiesdata[s]) do
local a = i:lower()
if a == "offsetx" then
enemiesdata[s]["offsetX"] = v
elseif a == "offsety" then
enemiesdata[s]["offsetY"] = v
elseif a == "quadcenterx" then
enemiesdata[s]["quadcenterX"] = v
elseif a == "quadcentery" then
enemiesdata[s]["quadcenterY"] = v
else
if type(v) == "string" then
enemiesdata[s][a] = v:lower()
else
enemiesdata[s][a] = v
end
end
end
for i, v in pairs(defaultvalues) do
if enemiesdata[s][i] == nil then
enemiesdata[s][i] = v
end
end
--Load graphics if it exists
if love.filesystem.exists(folder .. s .. ".png") then
enemiesdata[s].graphic = love.graphics.newImage(folder .. s .. ".png")
end
--Set up quads if given
if enemiesdata[s].graphic and enemiesdata[s].quadcount then
local imgwidth, imgheight = enemiesdata[s].graphic:getWidth(), enemiesdata[s].graphic:getHeight()
local quadwidth = imgwidth/enemiesdata[s].quadcount
local quadheight = imgheight/4
if math.floor(quadwidth) == quadwidth and math.floor(quadheight) == quadheight then
if enemiesdata[s].nospritesets then
quadheight = quadheight*4
end
enemiesdata[s].quadbase = {}
for y = 1, 4 do
enemiesdata[s].quadbase[y] = {}
for x = 1, enemiesdata[s].quadcount do
local realy = (y-1)*quadheight
if enemiesdata[s].nospritesets then
realy = 0
end
enemiesdata[s].quadbase[y][x] = love.graphics.newQuad((x-1)*quadwidth, realy, quadwidth, quadheight, imgwidth, imgheight)
end
end
end
end
--check if graphic for enemy exists
if enemiesdata[s].quadbase and enemiesdata[s].graphic then
enemiesdata[s].drawable = true
enemiesdata[s].quadgroup = enemiesdata[s].quadbase[spriteset]
if enemiesdata[s].animationtype == "frames" then
enemiesdata[s].quad = enemiesdata[s].quadbase[spriteset][enemiesdata[s].animationstart]
else
enemiesdata[s].quad = enemiesdata[s].quadbase[spriteset][enemiesdata[s].quadno]
end
end
table.insert(enemies, s)
if loaddelayed[s] and #loaddelayed[s] > 0 then
for j = #loaddelayed[s], 1, -1 do
loadenemy(loaddelayed[s][j]) --RECURSIVE PROGRAMMING AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAH
table.remove(loaddelayed, j)
end
end
else
--Bad.
end
end
function usebase(t)
local r = {}
for i, v in pairs(t) do
r[i] = v
end
return r
end