public
Description: RubyStein 3d SUPER MEGA ULTRA MEGAZORD UBER UBER ENTERPRISE EDITION
Homepage:
Clone URL: git://github.com/FooBarWidget/rubystein.git
Hongli Lai (Phusion) (author)
Thu May 14 05:33:25 -0700 2009
rubystein / sprite.rb
100644 369 lines (305 sloc) 8.017 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
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
364
365
366
367
368
369
require 'rubygems'
require 'gosu'
require 'sound'
require 'weapon'
require 'map'
 
module Sprite
  TEX_WIDTH = 64
  TEX_HEIGHT = 64
  
  attr_accessor :x
  attr_accessor :y
  attr_accessor :window
  attr_accessor :slices
  attr_accessor :z_order
end
 
class SpritePool
  @@files = {}
  
  def self.get(window, file_path, sprite_height = Sprite::TEX_HEIGHT, sprite_width = 1)
    file_path = File.expand_path(file_path)
    if !@@files[file_path]
      begin
        @@files[file_path] = Gosu::Image::load_tiles(window, file_path, sprite_width, sprite_height, true)
      rescue
        STDERR.puts "Cannot load #{file_path}"
        raise
      end
    end
    
    return @@files[file_path]
  end
end
 
class MagicPony
  include Sprite
  
  def initialize(window, x, y)
    @window = window
    @x = x
    @y = y
    @slices = SpritePool::get(window, 'magic_pony.png', TEX_HEIGHT)
  end
end
 
class Lamp
  include Sprite
  
  def initialize(window, x, y)
    @window = window
    @x = x
    @y = y
    @slices = SpritePool::get(window, 'lamp.bmp', TEX_HEIGHT)
  end
end
 
class Table
  include Sprite
  
  def initialize(window, x, y)
    @window = window
    @x = x
    @y = y
    @slices = SpritePool::get(window, 'table.png', TEX_HEIGHT)
  end
end
 
class TableWithChairs
  include Sprite
  
  def initialize(window, x, y)
    @window = window
    @x = x
    @y = y
    @slices = SpritePool::get(window, 'tablechairs.png', TEX_HEIGHT)
  end
end
 
class Well
  include Sprite
  
  def initialize(window, x, y)
    @window = window
    @x = x
    @y = y
    @slices = SpritePool::get(window, 'well.png', TEX_HEIGHT)
  end
end
 
class BrownBarrel
  include Sprite
  
  def initialize(window, x, y)
    @window = window
    @x = x
    @y = y
    @slices = SpritePool::get(window, 'brownbarrel.png', TEX_HEIGHT)
  end
end
 
class Chandelier
  include Sprite
  
  def initialize(window, x, y)
    @window = window
    @x = x
    @y = y
    @slices = SpritePool::get(window, 'chandelier.bmp', TEX_HEIGHT)
  end
end
 
class DeadGuard
  include Sprite
  
  def initialize(window, x, y)
    @window = window
    @x = x
    @y = y
    @slices = SpritePool::get(window, 'deadguard.bmp', TEX_HEIGHT)
  end
end
 
class Bones
  include Sprite
  
  def initialize(window, x, y)
    @window = window
    @x = x
    @y = y
    @slices = SpritePool::get(window, 'bones.bmp', TEX_HEIGHT)
  end
end
 
class Skeleton
  include Sprite
  
  def initialize(window, x, y)
    @window = window
    @x = x
    @y = y
    @slices = SpritePool::get(window, 'skeleton.bmp', TEX_HEIGHT)
  end
end
 
class GreenPlant
  include Sprite
  
  def initialize(window, x, y)
    @window = window
    @x = x
    @y = y
    @slices = SpritePool::get(window, 'plantgreen.png', TEX_HEIGHT)
  end
end
 
class Flag
  include Sprite
  
  def initialize(window, x, y)
    @window = window
    @x = x
    @y = y
    @slices = SpritePool::get(window, 'flag.png', TEX_HEIGHT)
  end
end
 
class Armor
  include Sprite
  
  def initialize(window, x, y)
    @window = window
    @x = x
    @y = y
    @slices = SpritePool::get(window, 'armor.png', TEX_HEIGHT)
  end
end
 
class Vase
  include Sprite
  
  def initialize(window, x, y)
    @window = window
    @x = x
    @y = y
    @slices = SpritePool::get(window, 'vase.png', TEX_HEIGHT)
  end
end
 
class ColonelSanders
  include Sprite
  
  def initialize(window, x, y)
    @window = window
    @x = x
    @y = y
    @slices = SpritePool::get(window, 'colonel_sanders.bmp', TEX_HEIGHT)
  end
end
 
 
# An sprite that interacts with the player when they touch each other.
class Interactable
  include Sprite
  
  attr_accessor :window
  attr_accessor :x
  attr_accessor :y
  attr_accessor :map
  
  def initialize(window, map, x, y, slices)
    @window = window
    @x = x
    @y = y
    @map = map
    @slices = slices
    @last_interaction_time = 0
  end
  
  def interact(player)
    if interaction_has_effect?(player)
      @last_interaction_time = Time.now.to_f
      execute_interaction_effect(player)
    end
  end
 
  private
  
  def interaction_has_effect?(player)
    # Hack: do not repeatedly interact with player who's standing still.
    if @last_interaction_time + 10 < Time.now.to_f
      my_row, my_column = Map.matrixify(@y, @x)
      player_row, player_column = Map.matrixify(player.y, player.x)
      my_row == player_row && my_column == player_column
    else
      false
    end
  end
  
  def execute_interaction_effect(player)
  end
end
 
# An item that can be picked up once.
class Item < Interactable
  def initialize(window, map, x, y, slices, text = nil, sound_file = nil)
    super(window, map, x, y, slices)
    @text = text
    @sound = SoundPool::get(window, sound_file || 'ammo.ogg')
  end
  
  private
  
  def execute_interaction_effect(player)
    super(player)
    @sound.play
    @window.show_text(@text) if @text
    @map.items.delete(self)
  end
end
 
# Item which increases or decreases HP. Will only interact when the player's
# HP is < 100% (for positive powerups) and > 0% (for negative power ups).
class Powerup < Item
  def initialize(window, map, x, y, slices, power_up, text = nil, sound_file = nil)
    super(window, map, x, y, slices, text, sound_file)
    @power_up = power_up
  end
  
  private
  
  def interaction_has_effect?(player)
    super(player) && (
      (@power_up > 0 && player.health < player.max_health) ||
      (@power_up < 0 && player.health > 0)
    )
  end
  
  def execute_interaction_effect(player)
    super(player)
    new_health = player.health + @power_up
    if new_health > player.max_health
      new_health = player.max_health
    elsif new_health < 0
      new_health = 0
    end
    player.health = new_health
  end
end
 
class Food < Powerup
  def initialize(window, map, x, y)
    super(window, map, x, y, SpritePool::get(window, 'food.bmp', TEX_HEIGHT), 25, "Food: +25 HP!")
  end
end
 
class Peepcode < Powerup
  def initialize(window, map, x, y)
    super(window, map, x, y, SpritePool::get(window, 'peepcode_powerup.png', TEX_HEIGHT), 50, "Peepcode: +50 HP!")
  end
end
 
class Rails < Powerup
  def initialize(window, map, x, y)
    super(window, map, x, y, SpritePool::get(window, 'rails.bmp', TEX_HEIGHT), 100, "Rails: +100 HP!")
  end
end
 
class PHP < Powerup
  def initialize(window, map, x, y)
    super(window, map, x, y, SpritePool::get(window, 'php.png', TEX_HEIGHT), -25,
          'PHP: "Fuck you!"', 'fuck_you.ogg')
  end
end
 
class Fries < Powerup
  def initialize(window, map, x, y)
    super(window, map, x, y, SpritePool::get(window, 'ronald_dead10.png', TEX_HEIGHT), 40,
          "French Fries: +40 HP!\nBut don't eat too much, it's bad for your health.",
          'fuck_you.ogg')
  end
end
 
class Info < Interactable
  attr_accessor :play_sound
  
  def initialize(window, map, x, y, text = nil, change_bg_song_to = nil, &block)
    super(window, map, x, y, SpritePool::get(window, @image || 'info.png', TEX_HEIGHT))
    @play_sound = true
    @text = text
    @sound = SoundPool::get(window, 'Message_Received.ogg')
    @change_bg_song_to = change_bg_song_to
    @block = block
  end
  
  private
  
  def execute_interaction_effect(player)
    super(player)
    @window.show_text(@text) if @text
    @window.background_song = @change_bg_song_to if @change_bg_song_to
    @block.call(self, player) if @block
    @sound.play if @play_sound
  end
end
 
class InvisibleInfo < Info
  def initialize(window, map, x, y, text = nil, change_bg_song_to = nil, &block)
    @image = 'invisible_item.png'
    super(window, map, x, y, text, change_bg_song_to, &block)
  end
end
 
class Phusion < Item
  def initialize(window, map, x, y, new_max_health)
    super(window, map, x, y, SpritePool::get(window, 'phusion_logo.png', TEX_HEIGHT))
    @new_max_health = new_max_health
  end
  
  private
  
  def execute_interaction_effect(player)
    super(player)
    @window.show_text("Phusion: Max HP increased from #{player.max_health} to #{@new_max_health}!")
    player.max_health = @new_max_health
    player.health = @new_max_health
  end
end