-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsokobot.rb
325 lines (324 loc) · 7.82 KB
/
sokobot.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
require 'rubygems'
require 'bundler/setup'
require 'gosu'
require 'chingu'
# Sokobot - A Sokoban game I made for Ludum Dare 22.`
Chingu::Text.trait :asynchronous
$levels = {
:head => [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 1, 0, 0, 0, 0, 1, 1, 1],
[1, 3, 3, 0, 0, 1, 0, 3, 0, 1],
[1, 2, 1, 1, 1, 1, 1, 0, 1, 1],
[1, 1, 0, 0, 0, 0, 1, 3, 1, 1],
[1, 3, 3, 1, 0, 1, 0, 0, 1, 1],
[1, 0, 0, 1, 0, 1, 0, 0, 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 1, 1, 1, 1, 1, 1, 1, 1]]}
$intro_texts = [
"Bot Q-99_0E ('Helper')",
"1045k RAM detected",
"MEGATRANS BIOS V 1.0.99-e",
"Last boot: 150y55d43h ago",
"Scanning environment:",
"** WARNING! RADIATION LEVELS ABOVE HUMAN LIVING STANDARD. EVACUATE ALL REMAINING CITIZENS",
"** NEW DIRECTIVE: FIND ALL LIVING CITIZENS AND EVACUATE CITY",
"Scanning area:",
"Citizens..........0 found",
"Bots..............0 found",
"Living Entities...0 found",
"Scanning bus:",
"Main CPU...........ONLINE",
"Visual Cortex......ONLINE",
"Rotor Control......ONLINE",
"Network Connection...FAILED",
"Network unreachable!",
"Battery Monitor....FAILED!",
"Power levels unknown!",
"SEEK REPAIR IMMEDIATELY",
"Repair facilities...FAILED!",
"Bot network.........FAILED!",
"** NEW DIRECTIVE: BUILD REPAIR BOT",
"Things needed:",
"1 head",
"1 torso (Univega Model 10 or newer)",
"1 pair of arms",
"1 track"]
$outro_texts = [
"Companion parts located",
"** WARNING! MISSING POWER CORE",
"Scanning for power core:",
"1 power core found. Location: Bot Q-99_0E ('Helper')",
"** WARNING! LOW POWER! TRANSFER POWER CORE NOW!"]
class LD22 < Chingu::Window
def setup
@states = [Intro,
Intermission.new(:text => "Find a head"),
Level.new(:level => :head),
Intermission.new(:text => "Find a torso (Univega Model 10 or newer)"),
Level.new(:level => :torso),
Intermission.new(:text => "Find a pair of arms"),
Level.new(:level => :arms),
Intermission.new(:text => "Find a track"),
Level.new(:level => :track),
Outro].reverse.each do |state|
push_game_state(state, :setup => false)
end
current_scope.setup
on_input(:esc) {exit}
end
end
class Intro < Chingu::GameState
trait :timer
def setup
Chingu::Text.destroy_all
@text = Chingu::Text.create(
$intro_texts.first,
:max_width => $window.width,
:size => 20,
:font => "./DisplayOTF.otf")
delay = 0
$intro_texts[1..-1].each do |text|
after(200 + rand() * 500 + delay) do
@text.text += "\n" + text
while(@text.image.height > $window.height)
@text.text = @text.text.split("\n")[1..-1].join("\n")
end
end
delay += 1300
end
after(delay + 2000) do
@text.async do |q|
q.tween(1000, :alpha => 0)
q.exec {pop_game_state}
end
end
on_input(:space) do
@text.async do |q|
q.tween(1000, :alpha => 0)
q.exec {pop_game_state}
end
end
end
end
class Outro < Chingu::GameState
trait :timer
def setup
Chingu::Text.destroy_all
@text = Chingu::Text.create(
$outro_texts.first,
:max_width => $window.width,
:size => 20,
:font => "./DisplayOTF.otf")
delay = 0
$outro_texts[1..-1].each do |text|
after(200 + rand() * 500 + delay) do
@text.text += "\n" + text
while(@text.image.height > $window.height)
@text.text = @text.text.split("\n")[1..-1].join("\n")
end
end
delay += 1300
end
after(delay + 2000) do
@text.async do |q|
q.tween(1000, :alpha => 0)
q.exec {pop_game_state}
end
end
on_input(:space) do
@text.async do |q|
q.tween(1000, :alpha => 0)
q.exec {pop_game_state}
end
end
end
end
class Intermission < Chingu::GameState
def setup
Chingu::Text.destroy_all
@text = Chingu::Text.create(
@options[:text],
:max_width => $window.width,
:font => "./DisplayOTF.otf",
:y => $window.height,
:x => $window.width / 2,
:rotation_center => :center,
:size => 20)
@text.async do |q|
q.tween(1000, :y => $window.height / 2)
end
on_input(:space) do
@text.async do |q|
q.tween(1000, :alpha => 0)
q.exec {pop_game_state if $window.current_scope == self}
q.call :destroy
end
end
end
end
class Level < Chingu::GameState
trait :timer
def setup
@whoosh = Gosu::Sound["whoosh.wav"]
after(3000 + rand() * 3000) do
@whoosh.play
end
@map = $levels[@options[:level]]
@robot = Gosu::Image["robot.png"]
@wall = Gosu::Image["wall.png"]
@block = Gosu::Image["block.png"]
@part = Gosu::Image["part.png"]
@parts = Chingu::Animation.new(:file => "parts.png")
@parts.frame_names = {:head => 0, :torso => 1, :arms => 2, :track => 3}
@bg_color = Gosu::Color.new(0xff888888)
@bg_tile = Gosu::Image["bg_tile.png"]
on_input(:up) do
x, y = *@robot_index
if(y > 0)
case @map[y - 1][x]
when 0
# if(@map[y - 1][x].zero?)
@map[y][x] = 0
@map[y - 1][x] = 2
when 3
# elsif(@map[y - 1][x] == 3)
if(y - 1 > 0 && @map[y - 2][x].zero?)
@map[y][x] = 0
@map[y - 1][x] = 2
@map[y - 2][x] = 3
end
when 4
win
end
end
end
on_input(:down) do
x, y = *@robot_index
if(y < @map.length)
case @map[y + 1][x]
when 0
# if(@map[y + 1][x].zero?)
@map[y][x] = 0
@map[y + 1][x] = 2
when 3
# elsif(@map[y + 1][x] == 3)
if(y + 1 < @map.length && @map[y + 2][x].zero?)
@map[y][x] = 0
@map[y + 1][x] = 2
@map[y + 2][x] = 3
end
when 4
win
end
end
end
on_input(:left) do
x, y = *@robot_index
if(x > 0)
case @map[y][x - 1]
when 0
# if(@map[y][x - 1].zero?)
@map[y][x] = 0
@map[y][x - 1] = 2
when 3
# elsif(@map[y][x - 1] == 3)
if(x - 1 > 0 && @map[y][x - 2].zero?)
@map[y][x] = 0
@map[y][x - 1] = 2
@map[y][x - 2] = 3
end
when 4
win
end
end
end
on_input(:right) do
x, y = *@robot_index
if(x < @map[y].length)
case @map[y][x + 1]
when 0
# if(@map[y][x + 1].zero?)
@map[y][x] = 0
@map[y][x + 1] = 2
when 3
# elsif(@map[y][x + 1] == 3)
if(x + 1 < @map[y].length && @map[y][x + 2].zero?)
@map[y][x] = 0
@map[y][x + 1] = 2
@map[y][x + 2] = 3
end
when 4
win
end
end
end
end
def draw
fill(@bg_color)
($window.height / 32).times do |y|
($window.width / 32).times do |x|
@bg_tile.draw(x * 32, y * 32, 0)
end
end
x, y = 0, 0
@map.each do |row|
row.each do |col|
case col
when 1
@wall.draw(x * 32, y * 32, 0)
when 2
@robot_index = [x, y]
@robot.draw(x * 32, y * 32, 0)
when 3
@block.draw(x * 32, y * 32, 0)
when 4
# @part.draw(x * 32, y * 32, 0)
@parts[@options[:level]].draw(x * 32, y * 32, 0)
end
x += 1
end
x = 0
y += 1
end
super
end
def win
pop_game_state if $window.current_scope == self
end
end
LD22.new(320, 320).show