/
activeresource.rb
74 lines (61 loc) · 1.93 KB
/
activeresource.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
class String
def validate_existance_of file
raise "no #{file}" unless file.is_file? or file.is_folder?
end
alias_method :is_folder?, :is_dir?
def yml
file = "#{self}.yml"
validate_existance_of file
YAML::load_file(file) # rescue nil # don't cache. because this can be used in funky ways (i think). speak: duplicate data structures. or something like that. bla bla bla
end
def ttf size=12
ttf = "#{self}.ttf"
return Font.cache(ttf, size) if ttf.is_file? # "puit/font/Busk.ttf"
Font.cache(self, size) # "Helvetica"
end
def is_file?
File.file? self
end
def is_yml?
"#{self}.yml".is_file?
end
def anim(tile_width, tile_height, duration=1)
Animation.cache(self, tile_width, tile_height, duration)
end
def img
a = Animation.cache(self) if self.is_yml?
a ||= self.png
return a
end
def png
return Image.cache("#{self}.png")
end
end
class Animation
def initialize file_name, tile_width=nil, tile_height=nil, duration=1
if tile_width # super ugly hack
@frame_pictures = Gosu::Image::load_tiles($window, file_name+".png", tile_width, tile_height, false)
@frames = @frame_pictures.map {|f| [duration, f]}
else
@yml = file_name.yml
@frames = []
@yml['frames'].each do |frame|
@frames << [frame['duration'] || duration, Image.cache(frame['image'])]
end
end
rescue
raise "error while loading animation config #{file_name}"
end
def draw x, y, order=0, zoom_x=1, zoom_y=1
frame_duration = @frames[0][0]
current_frame = (Gosu::milliseconds / (100*frame_duration) % @frames.size) # hackish
@frames[current_frame][1].draw(x, y, order, zoom_x, zoom_y)
end
end
class Object
@@cache = {}
def self.cache identifier, *arguments
cache_hash = identifier + arguments.to_s # hacketyhack: let's hope arguments.to_s works
@@cache[cache_hash] ||= self.new(identifier, *arguments)
end
end