-
Notifications
You must be signed in to change notification settings - Fork 1
/
tile.rb
189 lines (167 loc) · 5.02 KB
/
tile.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
# encoding: utf-8
require 'yaml'
class Array
def road?
self.include?(:road)
end
def city?
self.include?(:city)
end
def meadow?
self.include?(:meadow)
end
end
class Tile
attr_accessor :north, :east, :south, :west, :center,
:id,
:type,
:graphic, :graphic_packed, :gosu_image,
:rotation, :multiplier,
:dimensions,
:pawn, :sub_grid
def initialize(type=nil,graphic=nil)
self.type = type
self.graphic = graphic
self.rotation = 0.0
self.multiplier = 1
@sub_grid = [
[[],[],[]],
[[],[],[]],
[[],[],[]]
]
end
def rotate_clockwise
new_directions = {
:north => self.west,
:east => self.north,
:south => self.east,
:west => self.south,
}
[:north,:east,:south,:west].each do |direction|
self.send('%s=' % direction,new_directions[direction])
end
self.sub_grid = sub_grid.reverse.transpose
self.rotation += 90
self
end
# Simply delegate to == in this example.
def eql?(comparee)
self == comparee
end
# Objects are equal if they have the same properties
def ==(comparee)
self.north == comparee.north && self.east == comparee.east && self.west == comparee.west && self.south == comparee.south && self.center == comparee.center && self.multiplier == comparee.multiplier && self.type == comparee.type
end
def to_graphs
grass = '·'
city = '#'
monastery = '⌂'
graph = []
if self.north.road?
graph << [grass,'║',grass]
elsif self.north == [:city]
graph << [city,city,city]
else
graph << [grass,grass,grass]
end
middle_graph = []
if self.west == [:city]
middle_graph << city
elsif self.west.road?
middle_graph << '═'
else
middle_graph << grass
end
if self.center == :monastery
middle_graph << monastery
elsif self.center == :city
middle_graph << city
#Four road exits
elsif self.north.road? && self.west.road? && self.south.road? && self.east.road?
middle_graph << '╬'
#Three road exits
elsif self.north.road? && self.west.road? && self.south.road? && !self.east.road?
middle_graph << '╣'
elsif self.north.road? && self.west.road? && !self.south.road? && self.east.road?
middle_graph << '╩'
elsif self.north.road? && !self.west.road? && self.south.road? && self.east.road?
middle_graph << '╠'
elsif !self.north.road? && self.west.road? && self.south.road? && self.east.road?
middle_graph << '╦'
#Two road exits
elsif self.north.road? && self.west.road? && !self.south.road? && !self.east.road?
middle_graph << '╝'
elsif self.north.road? && !self.west.road? && !self.south.road? && self.east.road?
middle_graph << '╚'
elsif !self.north.road? && !self.west.road? && self.south.road? && self.east.road?
middle_graph << '╔'
elsif !self.north.road? && self.west.road? && self.south.road? && !self.east.road?
middle_graph << '╗'
elsif self.north.road? && !self.west.road? && self.south.road? && !self.east.road?
middle_graph << '║'
elsif !self.north.road? && self.west.road? && !self.south.road? && self.east.road?
middle_graph << '═'
else
middle_graph << grass
end
if self.east == [:city]
middle_graph << city
elsif self.east.road?
middle_graph << '═'
else
middle_graph << grass
end
graph << middle_graph
if self.south == [:meadow, :road]
graph << [grass,'║',grass]
elsif self.south == [:city]
graph << [city,city,city]
else
graph << [grass,grass,grass]
end
graph
end
def draw_graphs
self.to_graphs.each{|graph_line|puts graph_line.join('')}
end
def roads
[:north,:east,:south,:west].map{|dir|dir if self.send(dir).road?}.compact
end
def cities
[:north,:east,:south,:west].map{|dir|dir if self.send(dir).city?}.compact
end
def meadows
[:north,:east,:south,:west].map{|dir|dir if self.send(dir).meadow?}.compact
end
def guess_sub_grid
@sub_grid = [
[[],[],[]],
[[],[],[]],
[[],[],[]]
]
@sub_grid[0] = [:city,:city,:city] if north.city?
@sub_grid[0] = [:meadow,:road,:meadow] if north == [:meadow,:road]
@sub_grid[0] = [:meadow,:meadow,:meadow] if north == [:meadow]
@sub_grid[1][0] = :road if west.road?
@sub_grid[1][0] = :city if west.city?
@sub_grid[1][0] = :meadow if west == [:meadow]
if center.nil?
if (west.road? || east.road? || north.road? || south.road?)
@sub_grid[1][1] = :road
else
@sub_grid[1][1] = :meadow
end
elsif center.kind_of?(Array)
@sub_grid[1][1] = center[0]
else
@sub_grid[1][1] = center
end
@sub_grid[1][2] = :road if east.road?
@sub_grid[1][2] = :city if east.city?
@sub_grid[1][2] = :meadow if east == [:meadow]
@sub_grid[2] = [:city,:city,:city] if south.city?
@sub_grid[2] = [:meadow,:road,:meadow] if south == [:meadow,:road]
@sub_grid[2] = [:meadow,:meadow,:meadow] if south == [:meadow]
@sub_grid
end
end