-
Notifications
You must be signed in to change notification settings - Fork 0
/
rocks.lua
211 lines (160 loc) · 4.46 KB
/
rocks.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
--
-- "Granite Crash" rocks handling
--
-- Author: Hj. Malthaner
-- Date: 2022/06/06
--
local rocks = {}
local function add(x, y, c)
local rock = {}
rock.x = x
rock.y = y
rock.dx = 0
rock.dy = 0
rock.xoff = 0
rock.yoff = 0
rock.type = c
table.insert(rocks.mobs, rock)
-- print("Added a type " .. c .. " rock, now having " .. #rocks.mobs .. " in the rocks")
return rock
end
local function get(x, y)
for i, mob in ipairs(rocks.mobs) do
if mob.x == x and mob.y == y then
return mob
end
end
end
local function remove(x, y)
for i, rock in ipairs(rocks.mobs) do
if rock.x == x and rock.y == y then
table.remove(rocks.mobs, i)
end
end
end
local function turn_right(dx, dy)
return -dy, dx
end
local function turn_left(dx, dy)
return dy, -dx
end
local function load(map)
rocks.mobs = {}
rocks.map = map
rocks.time = 0
end
local function isEmptyCell(x, y, player)
local map = rocks.map
local cell = map.getCell(x, y)
return cell == map.M_SPACE and (x ~= player.x or y ~= player.y)
end
local function slide(mob, delta, player)
mob.xoff = math.floor(mob.dx * delta)
mob.yoff = math.floor(mob.dy * delta)
end
local function move(rock, map, player, gameUi)
local x = rock.x
local y = rock.y
local nx = rock.x + rock.dx
local ny = rock.y + rock.dy
-- did a moving rock hit something?
if rock.dx ~= 0 or rock.dy ~= 0 then
-- the player?
if nx == player.x and ny == player.y then
gameUi.explode(nx, ny, map.M_BOMBER)
player.alive = false
end
-- a mover?
local mob = gameUi.swarm.get(nx, ny)
if mob then
gameUi.explode(nx, ny, mob.type)
player.score = player.score + 25
return
end
local mob = gameUi.swarm.get(x, y)
if mob then
gameUi.explode(x, y, mob.type)
player.score = player.score + 25
return
end
end
rock.dx = 0
rock.dy = 0
-- check map at desired destination
-- is the cell clear and player not under stationary rock?
local cell = map.getCell(x, y+1)
if isEmptyCell(x, y+1, player) then
rock.dx = 0
rock.dy = 1
elseif cell == map.M_ROCK or
cell == map.M_DIAMOND or
cell == map.M_WALL then
-- instable position
-- check for earth left or right below
local l = isEmptyCell(x-1, y, player)
local ld = isEmptyCell(x-1, y+1, player)
if l and ld then
rock.dx = -1
rock.dy = 0
end
local r = isEmptyCell(x+1, y, player)
local rd = isEmptyCell(x+1, y+1, player)
if r and rd then
rock.dx = 1
rock.dy = 0
end
end
-- moving rocks must block the destination for other rocks to move into
if rock.dx ~= 0 or rock.dy ~= 0 then
map.setCell(rock.x + rock.dx, rock.y + rock.dy, map.M_BLOCKER)
end
end
local function update(time, dt, player, gameUi)
rocks.delta = (time - rocks.time) * rocks.map.C_SPEED
-- big step time?
if rocks.delta > 31 then
local map = rocks.map
for i, rock in ipairs(rocks.mobs) do
if rock.dx ~= 0 or rock.dy ~= 0 then
map.setCell(rock.x, rock.y, map.M_SPACE)
rock.x = rock.x + rock.dx
rock.y = rock.y + rock.dy
map.setCell(rock.x, rock.y, rock.type)
-- check cell below, if we hit something?
if not isEmptyCell(rock.x, rock.y + 1, player) then
if rock.type == map.M_ROCK then
sounds.randplay(sounds.rockfall, 1, 0.1)
else
sounds.randplay(sounds.gemfall, 1, 0.1)
end
end
slide(rock, 0)
end
move(rock, map, player, gameUi)
end
rocks.time = time
else
for i, rock in ipairs(rocks.mobs) do
if rock.dx ~= 0 or rock.dy ~= 0 then
slide(rock, rocks.delta, player)
end
end
end
end
local function draw(xoff, yoff)
local sprites = rocks.map.sprites
local quads = rocks.map.quads
for i, mob in ipairs(rocks.mobs) do
love.graphics.draw(sprites, quads[mob.type],
mob.x*32 + mob.xoff + xoff,
mob.y*32 + mob.yoff + yoff,
0, 1, 1, 0, 0, 0, 0)
end
end
rocks.load = load
rocks.update = update
rocks.draw = draw
rocks.add = add
rocks.get = get
rocks.remove = remove
return rocks