/
environ.rb
448 lines (318 loc) · 11.2 KB
/
environ.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
begin
# In case you use Gosu via RubyGems.
require 'rubygems'
rescue LoadError
# In case you don't.
end
require 'gosu'
require 'common'
require 'texplay'
require 'interface'
#manages environment (landscape)
class EnvironmentController
TileSize_X = 400 #420
TileSize_Y = 350 #322
Offset_X = 0
Offset_Y = 124
def initialize(window, world, loglevel)
@window = window
@world = world
@logging_level = loglevel
@tile_theme = {}
#mapping functions
@map_to_screen = lambda { |x,y| [x * TileSize_X, Offset_Y + y * TileSize_Y] }
@screen_to_map = lambda { |x,y| [(x / TileSize_X).to_i, ((y - Offset_Y) / TileSize_Y).to_i] }
setup_themes
end
def logging_level
@logging_level
end
def setup_themes
@tile_theme[:desert] = DesertTile
@tile_theme[:lush] = LushTile
@tile_theme[:helga] = HelgaTile
end
def load_env(thememap)
num_tiles = 0
#break up the parameter into its theme and map number components
theme, map = thememap.scan(/\d+|\D+/)
#set path for this theme
theme_path = "assets/#{theme}/"
#map file
mapfile = "#{theme_path}map#{map}"
#load the background
@bg = Gosu::Image.new(@window, "#{theme_path}bg.png")
lines = File.readlines(mapfile).map { |line| line.chop }
puts "loading environment..."
puts "theme is: #{theme}; map is: map#{map}"
@height = lines.size
@width = lines[0].size
num_tiles = @width * @height
puts "#{num_tiles} tiles in the environment..."
@tiles = Array.new(@height) do |y|
Array.new(@width) do |x|
t_type = lines[y][x, 1]
@tile_theme[theme.to_sym].new(@window, @logging_level, self,
@map_to_screen, @screen_to_map, t_type, x, y)
end
end
puts "...done!"
# return x and y limits (in pixels) of map
[ @width * TileSize_X, @height * TileSize_Y ]
end
def get_tile(x,y)
#convert from screen to map coords
map_x, map_y = *@screen_to_map.call(x,y)
if x < 0 || y < 0 then return nil; end
if !@tiles[map_y] then return nil; end
if @tiles[map_y][map_x] then
return @tiles[map_y][map_x]
end
return nil
end
def check_collision(actor, offset_x=0, offset_y=0)
x = actor.x + offset_x
y = actor.y + offset_y
#convert from screen to map coords
map_x, map_y = @screen_to_map.call(x, y)
if x < 0 || y < 0 then return; end
if !@tiles[map_y] then return; end
if @tiles[map_y][map_x] then
tile = @tiles[map_y][map_x]
return tile.check_collision(x, y)
end
end
def draw(ox,oy)
#draw the background
@bg.draw(0, 0, Common::ZOrder::Background)
#draw the tiles
@height.times do |y|
@width.times do |x|
if tile=@tiles[y][x] then tile.draw(x,y,ox,oy) end
end
end
end
end
#abstract base class for tile
class Tile
include InterfaceElementTile
#class counter
@@count_instance = 0
#utility class
Clr = Struct.new(:r,:g,:b,:a)
Radius_Dmg = 25
attr_reader :x,:y
def initialize(window, loglevel, env, map_to_screen, screen_to_map, t_type, x, y)
@@count_instance += 1
@window = window
@logging_level = loglevel
@env = env
@map_to_screen = map_to_screen
@screen_to_map = screen_to_map
@t_type = t_type
@particles = Array.new
#TEMPORARY, FIX SOON
@drillmask = Gosu::Image.new(@window, "assets/drillmask.png")
#for misc animations associated with this tile
@anim_group = AnimGroup.new
@x, @y = @map_to_screen.call(x,y)
#list of Actors within boundaries of this tile (collision list)
@c_list = []
puts "loading tile #{@@count_instance}.."
#define in subclass
set_theme
setup_gfx
setup_sound
end
def logging_level
@logging_level
end
def set_theme
@theme = ""
end
def setup_gfx
# get filename of tile image
file = "assets/#{@theme}/#{@theme}#{@t_type}.png"
@image = Gosu::Image.new(@window, file, false)
@smoke = Gosu::Image.new(@window, "assets/smoke.png")
@explode = Gosu::Image.load_tiles(@window, "assets/explosion.png", 128,
128, false)
@width = @image.width
@height = @image.height
end
# standard, override in subclasses
def setup_sound
@effects = EffectsSystem.new(@window)
@effects.add_effect(:thud, "assets/sand2.ogg")
end
# NOTE: equation is different from Actor's as Tiles are drawn from TOP LEFT, not center
def visible?(sx,sy)
(sx + @width > 0 && sx < Common::SCREEN_X && sy + @height > 0 &&
sy < Common::SCREEN_Y)
end
def add_actor(actor)
@c_list << actor
end
def remove_actor(actor)
@c_list.delete(actor)
end
def collision_list
@c_list
end
def draw(x, y, ox, oy)
x,y = @map_to_screen.call(x,y)
#screen coords
sx = x - ox
sy = y - oy
@particles.each { |v| v.update; v.draw(ox, oy) }
return if !visible?(sx,sy)
@image.draw(sx, sy, Common::ZOrder::Tile)
@anim_group.draw(ox, oy)
#if @x == 400 then TexPlay.leftshift @image, 2, :loop end #CHANGED!
end
def check_collision(cp_x, cp_y)
sx = (cp_x - @x).to_i
sy = (cp_y - @y).to_i
#might need to add tests for other directions too (e.g sx < 0 && sx > MAX && sy > MAX etc)
return if sy < 0
if @image.get_pixel(sx, sy)[3] !=0 then return self; end
end
def splash_damage_center(x, y, x_width, y_width, meth_name)
#splash damage for other tiles
tile = @env.get_tile(x + x_width, y)
if(tile && tile != self) then tile.send meth_name, x - tile.x, y - tile.y; end
tile = @env.get_tile(x - x_width, y)
if(tile && tile != self) then tile.send meth_name, x - tile.x, y - tile.y; end
tile = @env.get_tile(x, y + y_width)
if(tile && tile != self) then tile.send meth_name, x - tile.x, y - tile.y; end
tile = @env.get_tile(x, y - y_width)
if(tile && tile != self) then tile.send meth_name, x - tile.x, y - tile.y; end
tile = @env.get_tile(x - x_width, y - y_width)
if(tile && tile != self) then tile.send meth_name, x - tile.x, y - tile.y; end
tile = @env.get_tile(x + x_width, y - y_width)
if(tile && tile != self) then tile.send meth_name, x - tile.x, y - tile.y; end
tile = @env.get_tile(x - x_width, y + y_width)
if(tile && tile != self) then tile.send meth_name, x - tile.x, y - tile.y; end
tile = @env.get_tile(x + x_width, y + y_width)
if(tile && tile != self) then tile.send meth_name, x - tile.x, y - tile.y; end
#damage for this tile
send meth_name, x - @x, y - @y
end
#override in base-class
def do_collision(actor, offset_x=0, offset_y=0)
message "A #{self.class} collided with a #{actor.class}", :log_level => 2
sx = actor.x + offset_x
sy = actor.y + offset_y
damage_block = lambda do
splash_damage_center(sx, sy, Radius_Dmg + 8, Radius_Dmg + 8, :do_damage_proj)
end
case actor
when Projectile
@effects.play_effect(:thud)
splash_damage_center(sx, sy, Radius_Dmg + 8, Radius_Dmg + 8, :do_damage_proj)
new_anim = @anim_group.new_entry(:blast, :x => actor.x, :y => actor.y,
:anim => ImageSystem.new(@window))
new_anim.make_animation(:blast, @explode,
:timing => 0.06,:loop => false, :hold => false)
new_anim.load_queue(:blast)
5.times {
Particle.new(@window, @smoke,
actor.x, actor.y, @particles, :fade_rate => 4, :scale => 0.5, :speed => 3)
}
when Digger
splash_damage_center(sx, sy, (@drillmask.width / 2), (@drillmask.height / 2), :do_damage_drill)
16.times {
Particle.new(@window, @smoke,
actor.x, actor.y + offset_y - 50 + rand(5), @particles,
:speed => 2, :scale => :rand, :fade_rate => 1,
:x_scatter => @smoke.width, :y_scatter => @smoke.height)
}
else
# no behaviour yet
end
end
def do_damage_drill(x, y)
@image.paint { |c|
c.splice(@drillmask, x - (@drillmask.width / 2), y - (@drillmask.height / 2), :mask => :_white)
}
end
def do_damage_proj(x, y)
@image.paint { |c|
5.times {
dx = rand(16) - 8
dy = rand(16) - 8
c.color :alpha
c.circle x + dx, y + dy, Radius_Dmg
}
}
end
def info
"Object information:\nType: #{self.class}; Sub-type: #{@t_type}"
end
#round x to nearest multiple of y
def round_to_mult(x,y)
(x / y.to_f).round * y
end
private :round_to_mult, :setup_gfx, :setup_sound, :visible?
end
class Particle
def initialize(window, anim, x, y, particles, options={})
@options = {
:scale => 1,
:speed => 1,
:fade_rate => 1,
:x_scatter => anim.width,
:y_scatter => anim.height
}.merge(options)
if @options[:scale] == :rand then
@options[:scale] = rand * 0.70
end
# All Particle instances use the same image
@image = anim
@particles = particles
scale = @options[:scale]
x_scatter = @options[:x_scatter]
y_scatter = @options[:y_scatter]
@x = (x + rand(x_scatter) * scale - (x_scatter * scale / 2)).to_i
@y = (y + rand(y_scatter) * scale - (y_scatter * scale / 2)).to_i
@color = Gosu::Color.new(255, 255, 255, 255)
@particles.push self
end
def update
@y -= @options[:speed]
@x = @x - 1 + rand(3)
fade_rate = @options[:fade_rate]
@color.alpha -= @color.alpha >= fade_rate ? @options[:fade_rate] : 1
# Remove if faded completely.
if @y < 0 || @color.alpha <= 0 then
@particles.delete(self)
end
def draw(ox, oy)
scale = @options[:scale]
@image.draw_rot(@x - ox, @y - oy, 1, 0, 0.5, 0.5, scale, scale, @color)
end
end
end
#desert tile class
class DesertTile < Tile
def set_theme
@theme = "desert"
end
end
#desert tile class
class LushTile < Tile
def set_theme
@theme = "lush"
end
end
#helga tile class
class HelgaTile < Tile
def set_theme
@theme = "helga"
end
end
class EvianTile < Tile
def set_theme
@theme = "evian"
end
end