-
Notifications
You must be signed in to change notification settings - Fork 0
/
hunt.rb
363 lines (296 loc) · 10.2 KB
/
hunt.rb
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# Encoding: UTF-8
require "rubygems"
require "gosu"
Dir[File.join(__dir__, "objects", "*.rb")].each { |file| require file }
require "./random_object_generator"
require "./target_score_calculator"
TILES_X, TILES_Y = 40, 20
TILE_SIZE = 16
GAME_STATE = {
current_mode: :menu,
tiles_generated: false,
game_won: false,
game_over: false,
target_score: 50,
score: 0
}
NULL_OBJECT = NullObject.new
class EmeraldHunt < Gosu::Window
def initialize
super((TILES_X * TILE_SIZE + TILE_SIZE * 2) * 2, (TILES_Y * TILE_SIZE + TILE_SIZE * 2) * 2)
self.caption = "Emerald Hunt"
@player = Player.new
@font = Gosu::Font.new(10)
@big_font = Gosu::Font.new(100)
@last_debug_dump = Gosu.milliseconds
end
def update
case GAME_STATE[:current_mode]
when :menu
if Gosu::button_down?(Gosu::KbReturn)
GAME_STATE[:current_mode] = :gameplay
end
when :gameplay
unless GAME_STATE[:tiles_generated]
BOARD.generate_tiles
GAME_STATE[:tiles_generated] = true
BOARD.touch_global_last_move_time
end
unless GAME_STATE[:game_over] || GAME_STATE[:game_won]
BOARD.each_tile_in_reverse do |tile|
tile.update
end
if !@player.activated? && BOARD.everything_still?
player_position = BOARD.random_blank_tile
@player.activate_at(player_position.x, player_position.y)
exit_position = BOARD.random_blank_tile
exit_position.set_contents(Exit.new(exit_position.x, exit_position.y))
GAME_STATE[:target_score] = TargetScoreCalculator.calculate
end
end
if Gosu::button_down?(Gosu::KbD) && Gosu.milliseconds - @last_debug_dump > 1000
@last_debug_dump = Gosu.milliseconds
puts "\n\n\n"
puts BOARD.inspect
end
end
end
def draw
self.scale(2) do
case GAME_STATE[:current_mode]
when :menu
x = (TILES_X * TILE_SIZE + TILE_SIZE * 2) / 2
y = (TILES_Y * TILE_SIZE + TILE_SIZE * 2) / 2
@big_font.draw_rel("EMERALD HUNT", x, y - 20, 99, 0.5, 0.5, 0.4, 0.4)
@font.draw_rel("Press Enter to start", x, y + 20, 99, 0.5, 0.5)
when :gameplay
BOARD.each_tile do |tile|
x_position = TILE_SIZE + tile.x * TILE_SIZE
y_position = TILE_SIZE + tile.y * TILE_SIZE
tile.draw(x_position, y_position)
end
hud_text = "SCORE: #{GAME_STATE[:score]} | GOAL: #{GAME_STATE[:target_score]} | GRENADES: #{@player.grenade_count}"
@font.draw(hud_text, TILE_SIZE, TILE_SIZE * 0.4, 0)
if GAME_STATE[:game_over]
x = (TILES_X * TILE_SIZE + TILE_SIZE * 2) / 2
y = (TILES_Y * TILE_SIZE + TILE_SIZE * 2) / 2
@big_font.draw_rel("GAME OVER", x, y, 99, 0.5, 0.5, 1, 1, 0xff_ff0000)
elsif GAME_STATE[:game_won]
x = (TILES_X * TILE_SIZE + TILE_SIZE * 2) / 2
y = (TILES_Y * TILE_SIZE + TILE_SIZE * 2) / 2
@big_font.draw_rel("CONGRATULATIONS", x, y, 99, 0.5, 0.5, 0.7, 0.7, 0xff_ff0000)
end
end
end
end
end
class Board
def initialize
@random_object_generator = RandomObjectGenerator.new(self)
@global_last_move_time = 0
@matrix = Array.new(TILES_Y) do |y|
Array.new(TILES_X) do |x|
Tile.new(x, y, NULL_OBJECT)
end
end
end
def generate_tiles
each_tile_in_reverse do |tile|
tile.set_contents(@random_object_generator.for_tile(tile.x, tile.y))
end
end
def each_tile(&block)
@matrix.flatten.each do |tile|
yield(tile)
end
end
def each_tile_in_reverse(&block)
@matrix.flatten.reverse_each do |tile|
yield(tile)
end
end
def try_move(moving_object, x_direction, y_direction)
destination_x = moving_object.x + x_direction
destination_y = moving_object.y + y_direction
return false unless tile_in_bounds?(destination_x, destination_y)
destination_tile = tile_at(destination_x, destination_y)
# move into the tile if it's empty
if destination_tile.empty?
execute_move(moving_object, destination_x, destination_y)
# try crushing the destination tile's contents.
elsif destination_tile.contents.can_be_crushed_by?(moving_object, x_direction, y_direction)
destination_tile.contents.get_crushed_by(moving_object)
execute_move(moving_object, destination_x, destination_y)
# try pushing the destination tile's contents out of the way.
# this will recursively call try_move.
elsif y_direction >= 0 && # objects cannot be pushed upwards.
destination_tile.contents.can_be_pushed_by?(moving_object) &&
try_move(destination_tile.contents, x_direction, y_direction)
execute_move(moving_object, destination_x, destination_y)
# if the object wants to move straight down, check if the destination tile's contents
# are slippery - that means we can try moving to the left or right instead.
# try left first
elsif y_direction.positive? &&
x_direction.zero? &&
destination_tile.contents.slippery? &&
moving_object.slippable? &&
tile_in_bounds?(moving_object.x - 1, moving_object.y + 1) &&
tile_at(moving_object.x - 1, moving_object.y + 1).empty? &&
tile_in_bounds?(moving_object.x - 1, moving_object.y) &&
tile_at(moving_object.x - 1, moving_object.y).empty?
execute_move(moving_object, moving_object.x - 1, moving_object.y)
# then try right
elsif y_direction.positive? &&
x_direction.zero? &&
destination_tile.contents.slippery? &&
moving_object.slippable? &&
tile_in_bounds?(moving_object.x + 1, moving_object.y + 1) &&
tile_at(moving_object.x + 1, moving_object.y + 1).empty? &&
tile_in_bounds?(moving_object.x + 1, moving_object.y) &&
tile_at(moving_object.x + 1, moving_object.y).empty?
execute_move(moving_object, moving_object.x + 1, moving_object.y)
# if the object is falling on top of something and it should explode on contact,
# explode it
elsif y_direction.positive? &&
x_direction.zero? &&
moving_object.in_motion &&
moving_object.explode_on_contact?
moving_object.explode
false
# or if the object is falling on top of something else that should explode on contact,
# explode the other thing
elsif y_direction.positive? &&
x_direction.zero? &&
moving_object.in_motion &&
destination_tile.contents.explode_on_contact?
destination_tile.contents.explode
false
else
false
end
end
def execute_move(moving_object, destination_x, destination_y)
free_tile(moving_object.x, moving_object.y)
tile_at(destination_x, destination_y).set_contents(moving_object)
moving_object.update_position(destination_x, destination_y)
moving_object.touch_last_move_time
touch_global_last_move_time
true
end
def trigger_explosion_at(x, y)
neighbouring_tiles_of(x, y).each do |tile|
tile.mark_for_explosion
end
# we also need to deal with the contents of the tile where the explosion started.
origin_tile = tile_at(x, y)
unless origin_tile.empty?
if origin_tile.object_type == :player
# if the player's there, kill 'em.
origin_tile.mark_for_explosion
else
# if anything else is there, just delete it.
free_tile(x, y)
end
end
end
def set_tile_contents(x, y, contents)
tile_at(x, y).set_contents(contents)
end
def free_tile(x, y)
tile_at(x, y).set_contents(NULL_OBJECT)
end
def tile_at(x, y)
@matrix[y][x]
end
def neighbouring_tiles_of(x, y)
# return all of the tiles surrounding a coordinate
[
# wow this is like a visual representation of the coordinates we're setting up.
# that's deep, man.
[x - 1, y - 1], [x , y - 1], [x + 1, y - 1],
[x - 1, y ], [x + 1, y ],
[x - 1, y + 1], [x , y + 1], [x + 1, y + 1]
].select { |coordinate_set|
tile_in_bounds?(*coordinate_set)
}.map { |coordinate_set|
tile_at(*coordinate_set)
}
end
def tile_sturdy?(x, y)
if !tile_in_bounds?(x, y + 1)
# if the tile below is out of bounds, then we're on the bottom of the board.
true
elsif [:brick, :dirt, :grenade, :bomb].include?(tile_at(x, y + 1).object_type)
# these objects are not slippery, so a slippable object won't slip off them.
# we could do this in a smarter way, but this'll do for now.
true
else
false
end
end
def tile_in_bounds?(x, y)
x.between?(0, TILES_X - 1) && y.between?(0, TILES_Y - 1)
end
def random_blank_tile
@matrix.flatten.select(&:empty?).sample
end
def touch_global_last_move_time
@global_last_move_time = Gosu.milliseconds
end
def everything_still?
Gosu.milliseconds - @global_last_move_time > 200
end
end
class Tile
attr_reader :x, :y, :contents, :secondary_contents
def initialize(x, y, contents)
@x = x
@y = y
@contents = contents
@secondary_contents = NULL_OBJECT
# secondary_contents is used for objects that need to temporarily occupy
# the same tile as another object, like live grenades. it's dumb.
@about_to_explode = false
end
def update
if @about_to_explode
@contents.explode
@contents = Explosion.new(@x, @y)
@about_to_explode = false
end
@contents.update
if secondary_contents?
@secondary_contents.update
set_secondary_contents(NULL_OBJECT) if @secondary_contents.expired?
end
end
def draw(x_position, y_position)
if object_type != :null_object
@contents.draw(x_position, y_position)
elsif secondary_contents?
@secondary_contents.draw(x_position, y_position)
end
end
def set_contents(contents)
@contents = contents
end
def set_secondary_contents(secondary_contents)
@secondary_contents = secondary_contents
end
def object_type
@contents.object_type
end
def empty?
object_type == :null_object && @secondary_contents.object_type == :null_object
end
def secondary_contents?
@secondary_contents.object_type != :null_object
end
def mark_for_explosion
if @contents.flammable?
@about_to_explode = true
end
end
end
BOARD = Board.new
EmeraldHunt.new.show if __FILE__ == $0