-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.lua
220 lines (184 loc) · 4.66 KB
/
main.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
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
-- Require List
require "loader"
require "world"
require "player"
require "battery"
require "book"
require "switch"
require "brain"
require "viewport"
require "elevator"
require "hud"
JUMP_MAX = 32*3
PLAYER_SPEED = 300
ELEVATOR_SPEED = 100
go = false
State = {REST = 0, MOVE_LEFT = 1, MOVE_RIGHT = 2}
state = State.REST
keys_down = 0
burn_down = 0
jumping = 0
jumped = 0
burned = {}
function love.load()
-- Size
love.graphics.setMode(800,600,false,true,0)
love.graphics.setCaption("Indiana Josef")
-- Load Initial World
local load_data = Loader:load("world_3")
world = World:new():with(load_data.map)
player = Player:new():with(load_data.start, world)
batteries = {}
for i=1,#load_data.batteries do
batteries[#batteries+1] = Battery:new():with(load_data.batteries[i], world)
end
books = {}
for i=1,#load_data.books do
books[#books+1] = Book:new():with(load_data.books[i], world)
end
brain = Brain:new():with(load_data.brain, world)
viewport = Viewport:new():with(world.width*32,world.height*32,800,560)
viewport:center(player:getX(), player:getY())
player.energy = load_data.energy
elevators = {}
for i=1,#load_data.elevators do
elevator = load_data.elevators[i]
elevators[#elevators+1] = Elevator:new():with({x=elevator.x, y=elevator.y}, elevator.direction, elevator.width, true, elevator.ignore_tile, world)
end
switches = {}
for i=1,#load_data.switches do
switch = load_data.switches[i]
if switch.pos then
switches[#switches+1] = Switch:new():with({x=switch.pos.x, y=switch.pos.y}, i, switch.tags, world)
end
end
hud = Hud:new()
end
-- Events
function love.keypressed(key, unicode)
if go == false then
return
end
if key == "q" then
love.event.push("q")
elseif key == "left" then
-- Move Left
state = State.MOVE_LEFT
keys_down = keys_down + 1
elseif key == "right" then
-- Move Right
state = State.MOVE_RIGHT
keys_down = keys_down + 1
elseif key == "lctrl" or key == "rctrl" then
-- Jump
if jumping == 0 then
jumping = 1
end
elseif key == "lshift" or key == "rshift" then
-- Burn
for i=1,#switches do
if switches[i]:is_collided_with_player(player) then
switches[i]:toggle()
return
end
end
burn_down = 1
end
end
function love.keyreleased(key)
if go == false then
go = true
return
end
if key == "left" then
keys_down = keys_down - 1
elseif key == "right" then
keys_down = keys_down - 1
elseif key == "lctrl" or key == "rctrl" then
jumping = -1
elseif key == "ralt" or key == "lalt" or key == "lshift" or key == "rshift" then
burn_down = 0
end
if keys_down == 0 then
state = State.REST
player:rest()
player:align()
end
end
function love.update(dt)
if go == false then
dt = 0
end
if state == State.MOVE_LEFT then
player:rest()
player:move(-PLAYER_SPEED*dt, world)
player.energy = player.energy - 5 * dt
viewport:center(player:getX(), player:getY())
elseif state == State.MOVE_RIGHT then
player:rest()
player:move(PLAYER_SPEED*dt, world)
player.energy = player.energy - 5 * dt
viewport:center(player:getX(), player:getY())
end
for i=1,#elevators do
elevators[i]:move(ELEVATOR_SPEED*dt, player, world)
end
local i = 1
while i <= #burned do
burned[i].time = burned[i].time - (100*dt)
if burned[i].time <= 0 then
burned[i].wall.visible = true
burned[i] = burned[#burned]
burned[#burned] = nil
else
i = i + 1
end
end
if burn_down == 1 then
wall = player:burn(world)
if wall then
burned[#burned+1] = {wall=wall, time=500}
end
end
if jumping == 1 then
local old_jumped = jumped
jumped = jumped + 500*dt
if jumped > JUMP_MAX then
player:move_y(old_jumped - JUMP_MAX, world)
jumped = JUMP_MAX
jumping = -1
end
player:move_y(-500*dt, world)
viewport:center(player:getX(), player:getY())
else
jumped = jumped - PLAYER_SPEED*dt
if jumped < 0 then
jumped = 0
end
if player:move_y(PLAYER_SPEED*dt, world) == true then
jumping = 0
end
viewport:center(player:getX(), player:getY())
end
world.physics:update(dt)
end
function love.draw()
love.graphics.setBackgroundColor(0,0,0)
viewport:drawBackground(world)
viewport:draw(world)
viewport:draw(player)
for i=1,#batteries do
viewport:draw(batteries[i])
end
for i=1,#books do
viewport:draw(books[i])
end
viewport:draw(brain)
for i=1,#elevators do
viewport:draw(elevators[i])
end
for i=1,#switches do
viewport:draw(switches[i])
end
hud:draw(0,600-40, player.energy, player.books, #books, player.brain == 1)
end