-
Notifications
You must be signed in to change notification settings - Fork 1
/
ugh.rb
164 lines (128 loc) · 2.52 KB
/
ugh.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
# encoding: utf-8
require "bundler/setup"
require "gaminator"
class UghGame
def initialize(width, height)
@points = 0
@width = width
@height = height
@tick = 0
@taxi = Taxi.new(15, 15)
@passengers = []
create_new_passanger
@destinations = [EndPoint.new(10,15), EndPoint.new(20,30), EndPoint.new(20,1)]
end
def exit_message
'bye bye!'
end
def tick
@tick += 1
fall
take_passanger
create_new_passanger if @passengers.count == 0
any_delivered?
end
def objects
[@taxi] + @passengers + @destinations
end
def input_map
{
?a => :move_left,
?w => :move_up,
?s => :move_down,
?d => :move_right
}
end
def textbox_content
objects.first.inspect + @passengers.inspect + "pass #{@taxi.passanger?.inspect}"
end
def wait?
false
end
def sleep_time
0.01
end
def move_left
objects.first.x -= 1
end
def move_right
objects.first.x += 1
end
def move_down
objects.first.y += 1 if objects.first.y < @height
end
def move_up
objects.first.y -= 1
end
def any_delivered?
@passengers.each do |p|
if p.delivered?(@destinations.first)
delivered(p)
end
end
end
def delivered(passanger)
@points += 1
@passengers.delete(passanger)
@taxi.passanger = nil
end
def fall
move_down if @tick % 30 == 0
end
def create_new_passanger
@passengers << Passenger.new(rand(@width),rand(@height))
end
def take_passanger
@passengers.each { |p| @taxi.take!(p) if [email protected]? && p.touched?(@taxi) }
end
class Obj < Struct.new(:x, :y); end
class EndPoint < Obj
def char
"E"
end
def color
Curses::COLOR_GREEN
end
end
class Passenger < Obj
def char
"X"
end
def touched?(taxi)
[x, x+1, x-1].include?(taxi.x) && taxi.y == (y-1)
end
def delivered?(meta)
meta.x == x && meta.y == y
end
end
class Taxi < Obj
def initialize(*args)
super
@passanger = nil
end
def x=(*args)
super
passanger.x = x if passanger?
end
def y=(*args)
super
passanger.y = y + 1 if passanger?
end
def char
"O"
end
def take!(_passanger)
self.passanger = _passanger
end
def passanger=(_passanger)
@passanger = _passanger
end
def passanger
@passanger ||= nil
end
def passanger?
end
end
end
Gaminator::Runner.new(UghGame, :rows => 40, :cols => 100).run