-
Notifications
You must be signed in to change notification settings - Fork 1
/
Monochrome-Ruby.rb
384 lines (351 loc) · 8.63 KB
/
Monochrome-Ruby.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
require 'Win32API'
# Reference(https://github.com/stonesaw/Ruby-CUI-game#map-class)
class Map
attr_reader :map, :text_hash, :width, :height
attr_accessor :default_text
def initialize(map: [[]], text_hash: {0 => " "}, width: map[0].length, height: map.length, default_text: -1)
@map = map
@text_hash = text_hash
if default_text == -1
@default_text = text_hash.first[1]
else
@default_text = default_text
end
@map.length.times do |y|
@map[y].length.times do |x|
unless @text_hash.include?(@map[y][x]) || @map[y][x] == -1
raise ArgumentError.new("i dont know text_hash => #{@map[y][x]}")
end
end
end
@width = width
@height = height
if @width < @map[0].length
raise ArgumentError.new("Need map#width(#{@width}) >= map length(#{@map[0].length})")
elsif @width > @map[0].length
need = @width - @map[0].length
@map.length.times do |i|
need.times do
@map[i] << -1 # default_text
end
end
end
if @height < @map.length
raise ArgumentError.new("Need map#height(#{@height}) >= map length(#{@map.length})")
elsif @height > @map.length
need = @height - @map.length
need.times do |i|
x_ary = []
@width.times do
x_ary << -1 # default_text
end
@map << x_ary
end
end
end
def draw(drawing = [])
self.height.times do |y|
x = 0
self.width.times do # x
drawed = 0
# draw sprite
drawing.each do |dr|
if dr.class == Array
sprites = dr
else
sprites = [dr]
end
sprites.each do |sp|
break if x >= self.width
if (x == sp.x && sp.vanished? == false && sp.y <= y && y <= sp.y + sp.height - 1)
print sp.text[y - sp.y]
drawed = 1
x += (sp.width(y - sp.y) - 1)
break
end
end
break if drawed == 1
end
# draw map-text
if drawed == 0
if @map[y][x] == -1
print @default_text
else
print @text_hash[@map[y][x]]
end
end
x += 1
end
puts ""
end
end
def render_draw(ox, oy, width, height, drawing = [])
puts "off map!" if ox < 0 || oy < 0
y = (oy + self.height) % self.height
height.times do # y
break if y >= self.height || y - oy >= height
x = (ox + self.width) % self.width
width.times do # x
drawed = 0
break if x >= self.width || x - ox >= width
# draw sprite
drawing.each do |dr|
if dr.class == Array
sprites = dr
else
sprites = [dr]
end
sprites.each do |sp|
break if x - ox >= width
if (x == sp.x && sp.vanished? == false && sp.y <= y && y <= sp.y + sp.height - 1)
print sp.text[y - sp.y]
drawed = 1
x += (sp.width(y - sp.y) - 1)
break
end
end
break if drawed == 1
end
# draw map-text
if drawed == 0
if @map[y][x] == -1
print @default_text
else
print @text_hash[@map[y][x]]
end
end
x += 1
end
y += 1
puts ""
end
end
def text=(ary)
raise ArgumentError.new("Map#text= [hash_num, new_text]") if (ary.length != 2) || !(ary[0].class == Integer || ary[0].class == Symbol) || (ary[1].class != String)
raise ArgumentError.new("Map#text=() text_hash undefined => #{ary[0]}") unless text_hash.has_key?(ary[0])
@text_hash[ary[0]] = ary[1]
end
end
# Reference(https://github.com/stonesaw/Ruby-CUI-game#sprite-class)
class Sprite
attr_accessor :x, :y
def initialize(x, y, text)
raise ArgumentError.new("Sprite#x (#{x}) plz Integer") unless x.class == Integer
raise ArgumentError.new("Sprite#y (#{y}) plz Integer") unless y.class == Integer
@x = x
@y = y
if text == ""
raise ArgumentError.new("Sprite#text Cannot use ''")
elsif text.class == Array
@text = text
@height = @text.length
else
@text = [text]
@height = 1
end
@max_width = 0
@height.times do |i|
@width = text_check(@text[i])
@max_width = @width.round if @max_width <= @width
end
@is_vanish = false
end
def hit(sprites, x: 0, y: 0)
my_x = self.x + x
my_y = self.y + y
my_wid = self.x + x + self.width - 1
my_hei = self.y + y + self.height - 1
if sprites.class == Array
sprites_ary = sprites
else
sprites_ary = [sprites]
end
sprites_ary.each do |sp|
next if sp.vanished?
sp_wid = sp.x + sp.width - 1
sp_hei = sp.y + sp.height - 1
# judge
if my_x <= sp_wid && my_y <= sp_hei && my_wid >= sp.x && my_hei >= sp.y
return true
end
end
return false
end
def ===(sprites_ary)
return hit(sprites_ary)
end
def check(sprites, x: 0, y: 0)
my_x = self.x + x
my_y = self.y + y
my_wid = self.x + x + self.width - 1
my_hei = self.y + y + self.height - 1
if sprites.class == Array
sprites_ary = sprites
else
sprites_ary = [sprites]
end
return_ary = []
sprites_ary.each do |sp|
next if sp.vanished?
sp_wid = sp.x + sp.width - 1
sp_hei = sp.y + sp.height - 1
# judge
if my_x <= sp_wid && my_y <= sp_hei && my_wid >= sp.x && my_hei >= sp.y
return_ary << sp
end
end
return return_ary
end
def touch(sprites_ary)
if (hit(sprites_ary, y: -1) || hit(sprites_ary, y: 1) ||
hit(sprites_ary, x: 1) || hit(sprites_ary, x: -1))
return true
else
return false
end
end
def touch_head(sprites_ary)
return hit(sprites_ary, y: -1)
end
def touch_foot(sprites_ary)
return hit(sprites_ary, y: 1)
end
def touch_right(sprites_ary)
return hit(sprites_ary, x: 1)
end
def touch_left(sprites_ary)
return hit(sprites_ary, x: -1)
end
def vanish
@is_vanish = true
end
def vanished?
return @is_vanish
end
def self.clean(sprites_ary)
sprites_ary.length.times do |i|
check = (sprites_ary.length - 1 - i)
if sprites_ary[check].vanished?
sprites_ary.delete_at(check)
end
end
end
def text
return @text
end
def text=(str)
if str == ""
raise ArgumentError.new("Sprite#text= Cannot use ''")
elsif str.class == Array
@text = str
@height = @text.length
else
@text = [str]
@height = 1
end
@max_width = 0
@height.times do |i|
@width = text_check(@text[i])
@max_width = @width.round if @max_width <= @width
end
end
def width(height = -1)
if height == -1
return @max_width
elsif height > @text.length
raise ArgumentError.new("Sprite#width(height: #{height})over Sprite#height")
else
@width = text_check(@text[height])
return @width
end
end
def height
return @height
end
private
def text_check(text)
width = 0
text.scan(/./) do |i|
if /\A[ぁ-んー-]+\z/ =~ i# 全角ひらがな
width += 1
elsif /\A[ァ-ン゙゚]+\z/ =~ i# 半角型カタカナ
width += 0.5
elsif /\A[ -~。-゜]+\z/ =~ i# 半角
width += 0.5
else
width += 1
end
end
return width.round
end
end
# Key-Reference(https://github.com/stonesaw/Ruby-CUI-game#key-class)
class Key
ESCAPE = 0x1b
RETURN = 0x0d
BACKSPACE = 0x08
DELETE = 0x53
TAB = 0x09
UP = 0x48
DOWN = 0x50
RIGHT = 0x4d
LEFT = 0x4b
SPACE = " "
HOME = 0x47
K_END = 0x4f
PAGEUP = 0x49
PAGEDOWN = 0x51
INSERT = 0x52
F1 = 0x3b
F2 = 0x3c
F3 = 0x3d
F4 = 0x3e
F5 = 0x3f
F6 = 0x40
F7 = 0x41
F8 = 0x42
F9 = 0x43
F10 = 0x44
F11 = 0x45
F12 = 0x46
ANY = :any_key
def initialize
@@kbhit = Win32API.new('msvcrt','_kbhit',[],'l')
@@getch = Win32API.new('msvcrt','_getch',[],'l')
@@pressed = false
end
class << self
def update()
if kbhit()
@@key = getch()
@@pressed = true
else
@@pressed = false
end
end
def down?(key_code)
if @@pressed == true
return true if key_code == ANY
if key_code.class == String
return @@key.chr == key_code ? true : false
else
return @@key == key_code ? true : false
end
else
return false
end
end
private
def kbhit()
if @@kbhit.call != 0
return true
else
return false
end
end
def getch()
return @@getch.call
end
end
end
Key.new()