-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlevel.lua
More file actions
240 lines (189 loc) · 5.72 KB
/
level.lua
File metadata and controls
240 lines (189 loc) · 5.72 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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
local freeze = require "characters.freeze"
level = {}
-- characters
local bullet = require('characters.bullet')
local human = require('characters.human')
local alien = require('characters.alien')
local bomb = require('characters.bomb')
local json = require('dkjson')
-- dialogue system
local bulletMaster = require('dialogue.bullet-master')
-- ui system
local numberCountdown = require('ui.numberCountDown')
local gameOver = require('ui.gameOver')
local soundfx = require('ui.soundfx')
-- images
local background = nil
-- data
local content = ""
gridXCount = 14
gridYCount = 14
cellSize = 30
offset = 140
scale = cellSize / 50
local state = {
bullet = bullet.getSegments(),
bombs = bomb.getBombs(),
humans = human.getHumans(),
freezes = freeze.getFreezes(),
alienWall = false,
accelerators = {},
dialogues = bulletMaster.getDialogues()
}
local state = {}
function chooseLevel(levelname)
local file = love.filesystem.newFile(levelname..".json","r")
if file then
content = file:read()
state = json.decode(content, 1, nil)
file:close()
print("State Has Been Read !!!")
else
print("File couldnt be open")
end
end
function level.load()
timer = 0
love.graphics.setDefaultFilter("nearest", "nearest")
soundfx.load()
background = love.graphics.newImage("assets/background.png")
spaceShipFloor = love.graphics.newImage("assets/spaceship-floor.png")
numberCountdown.load()
numberCountdown.setCurrValue(10)
numberCountdown.pause()
love.window.setMode(700, 700) -- Remove this after finishing the game
bullet.flush()
bullet.setSegments(state.bullet)
bulletMaster.reset()
bulletMaster.setDialogues(state.dialogues)
human.setHumans(state.humans)
bomb.setBombs(state.bombs)
alien.setEnterAliens(state.alienWall)
freeze.setFreezes(state.freezes)
accelerators = state.accelerators
MouseGridPosX = 0
MouseGridPosY = 0
placeItem = 'bomb'
human.load()
bullet.load()
gameOver.load()
bomb.load()
bulletMaster.load()
freeze.load()
end
function level.restart()
timer = 0
numberCountdown.start()
state = json.decode(content, 1, nil)
soundfx.load()
human.setIsMoveHuman(true)
bomb.setBombs(state.bombs)
bullet.flush()
bullet.setSegments(state.bullet)
human.setHumans(state.humans)
bomb.setBombs(state.bombs)
alien.setEnterAliens(state.alienWall)
freeze.setFreezes(state.freezes)
accelerators = state.accelerators
numberCountdown.setCurrValue(10)
end
function level.mousepressed()
gameOver.mousepressed()
end
function level.update(dt)
timer = timer + dt
if timer >= 0.15 then
timer = 0
dontRemove = false
MouseX, MouseY = love.mouse.getPosition()
MouseGridPosX = math.floor((MouseX - 140) / cellSize) + 1
MouseGridPosY = math.floor((MouseY - 140) / cellSize) + 1
-- Level maker system
if love.mouse.isDown(2) then
local cannotPlaceAccelerator = false
for acceleratorIndex, accelerator in ipairs(accelerators) do
if accelerator.x == MouseGridPosX and accelerator.y == MouseGridPosY then
cannotPlaceAccelerator = true
table.remove(accelerators, acceleratorIndex)
end
end
human.clashWithMouse(MouseGridPosX, MouseGridPosY)
bomb.clashWithMouse(MouseGridPosX, MouseGridPosY)
freeze.clashWithMouse(MouseGridPosX, MouseGridPosY)
if not cannotPlaceAccelerator and placeItem == 'accelerator' then
table.insert(accelerators, { x = MouseGridPosX, y = MouseGridPosY })
end
end
end
gameOver.update(dt)
numberCountdown.update(dt)
human.update(dt)
bullet.update(dt)
bulletMaster.update(dt)
bomb.update(dt)
alien.update(dt)
freeze.update(dt)
end
function level.keypressed(key)
if key == '1' then
placeItem = 'bomb'
end
if key == 'f' then
bomb.freeze()
end
soundfx.keypressed(key)
alien.keypressed(key)
bulletMaster.keypressed(key)
if key == 'e' then
state.bullet = bullet.getSegments()
state.bombs = bomb.getBombs()
state.accelerators = accelerators
state.humans = human.getHumans()
state.dialogues = { "Hi Nijo !!", "I am Bullet#909", "And you are going to control..",
" me for the rest of the game.." }
state.alienWall = alien.getEnterAliens()
for idx, human in ipairs(state.humans) do
print(human.type)
end
local string = json.encode(state, { index = true })
print(string)
local file = io.open('YouAreTheWeapon/state.json', 'w')
file:write(string)
file:close()
print("State Has Been Saved !!!")
end
if key == '2' then
placeItem = 'accelerator'
end
if key == '3' then
placeItem = 'human'
end
if key == '4' then
placeItem = 'moveHumanX'
end
if key == '5' then
placeItem = 'moveHumanY'
end
if key == '6' then
placeItem = 'freeze'
end
bullet.keypressed(key)
end
function level.draw()
love.graphics.draw(background, 0, 0)
love.graphics.draw(spaceShipFloor, offset - 10, offset - 10)
bulletMaster.draw()
bullet.draw()
bomb.draw()
freeze.draw()
alien.draw()
numberCountdown.draw()
love.graphics.setColor(0, 0, 1)
for acceleratorIndex, accelerator in ipairs(accelerators) do
love.graphics.rectangle('fill', ((accelerator.x - 1) * cellSize) + offset, ((accelerator.y - 1) * cellSize) +
offset, cellSize - 1, cellSize - 1)
end
human.draw()
gameOver.draw()
end
return level