-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlevel.rb
196 lines (170 loc) · 4.8 KB
/
level.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
require_relative 'vec'
require_relative 'tiles'
require_relative 'movable_tile_path'
require_relative 'map'
RELATIVE_DIR_MAP = {
Vec::UP => {
Vec::LEFT => Vec::LEFT,
Vec::UP => Vec::UP,
Vec::RIGHT => Vec::RIGHT,
Vec::DOWN => Vec::DOWN,
},
Vec::RIGHT => {
Vec::LEFT => Vec::UP,
Vec::UP => Vec::RIGHT,
Vec::RIGHT => Vec::DOWN,
Vec::DOWN => Vec::LEFT,
},
Vec::DOWN => {
Vec::LEFT => Vec::RIGHT,
Vec::UP => Vec::DOWN,
Vec::RIGHT => Vec::LEFT,
Vec::DOWN => Vec::UP,
},
Vec::LEFT => {
Vec::LEFT => Vec::DOWN,
Vec::UP => Vec::LEFT,
Vec::RIGHT => Vec::UP,
Vec::DOWN => Vec::RIGHT,
},
}
LEFT_HANDED_SEARCH = [ Vec::LEFT, Vec::UP, Vec::RIGHT, Vec::DOWN ]
class Level
START_COLOR = Gosu::Color::WHITE
EXIT_COLOR = Gosu::Color::BLACK
MAX_ALPHA = 255
attr_accessor :complete, :last_ms_to_complete, :best_ms_to_complete, :width, :height
attr_writer :map
def self.load(file_name:, number:, high_scores: )
level = Level.new
map = level.map
level.best_ms_to_complete = high_scores.best(number: number)
png = Gosu::Image.new file_name
load_level_meta(level, png)
level.width = png.width
level.height = png.height - 1
colors = []
level.width.times do |c|
level.height.times do |r|
gosu_color = png.get_pixel(c,r+1)
unless gosu_color == Gosu::Color::NONE
if gosu_color.alpha == MAX_ALPHA
special_color = gosu_color.abgr
special = map.special_tile_defs[special_color]
if gosu_color == START_COLOR
map.player_x = c
map.player_y = r
elsif gosu_color == EXIT_COLOR
map.exit_x = c
map.exit_y = r
else
tile = special ? special.dup : ColorSourceTile.from_color(gosu_color)
map.tiles[c][r] = tile
start_loc = vec(c,r)
path_locs = find_path_locs(png, start_loc, gosu_color)
if path_locs.size > 1
tile.path = MovableTilePath.build(path_locs, start_loc, LEFT_HANDED_SEARCH)
end
colors << gosu_color unless special
end
end
end
end
end
if colors.size > 0
avg_red = colors.collect(&:red).sum / colors.size.to_f
avg_green = colors.collect(&:green).sum / colors.size.to_f
avg_blue = colors.collect(&:blue).sum / colors.size.to_f
map.average_color = Gosu::Color.rgba(avg_red, avg_green, avg_blue, 255)
else
map.average_color = map.exit_color
end
level
end
def self.color_close_enough?(color, path_color)
# be forgiving here, because pixel editors suck.
return (color.red - path_color.red).abs < 4 &&
(color.green - path_color.green).abs < 4 &&
(color.blue - path_color.blue).abs < 4 &&
color.alpha < MAX_ALPHA
end
def self.find_path_locs(png, start_loc, path_color)
path_locs = []
open_list = [start_loc]
until open_list.empty?
active_node = open_list.pop
Vec::NEIGHBOR_VECS.map do |n_vec|
loc = active_node + n_vec
color = png.get_pixel(loc.x, loc.y+1)
if color && color.alpha > 0 && !path_locs.include?(loc)
open_list << loc if color_close_enough? color, path_color
end
end
path_locs << active_node
end
path_locs
end
def self.load_level_meta(level, png)
map = level.map
map.exit_color = png.get_pixel(0, 0)
command = nil
(1..png.width-1).each do |c|
a = png.get_pixel(c, 0).alpha
if command.nil? && a > 0
command = [png.get_pixel(c,0)]
elsif command && a == 0
begin
process_command(level, command)
rescue => ex
puts 'failed to process command'
puts ex.inspect
end
command = nil
elsif a > 0
command << png.get_pixel(c, 0)
end
end
end
COLOR_TO_TILE_KLASS = {
Gosu::Color::BLACK => BlackHoleTile,
Gosu::Color::YELLOW => RainbowTile,
Gosu::Color::GREEN => BrightTile,
Gosu::Color::BLUE => BouncyTile,
Gosu::Color::RED => DeathTile,
Gosu::Color::GRAY => EmptyTile,
Gosu::Color::CYAN => GhostTile,
}
def self.process_command(level, command)
map = level.map
klass = COLOR_TO_TILE_KLASS[command[0]]
if klass
tile = klass.from_colors command
map.special_tile_defs[tile.marker_color.abgr] = tile
else
puts "unknown command #{command}"
end
end
def map
@map ||= Map.new
end
def complete!(ms_to_complete:)
@last_ms_to_complete = ms_to_complete
@complete = true
end
def skip!
@complete = true
end
def complete?
@complete
end
def failed!
@failed = true
end
def failed?
@failed
end
def reset!
@complete = false
@failed = false
end
end