-
Notifications
You must be signed in to change notification settings - Fork 14
/
door.rb
62 lines (51 loc) · 965 Bytes
/
door.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
require 'map'
class Door
attr_accessor :pos
attr_reader :state
attr_reader :opened_at
OPEN_CLOSE_STEP = 8
STAYS_SECONDS_OPEN = 4
FULL_VOLUME_WITHIN_GRID_BLOCKS = 5.0
def initialize
@state = :closed
@pos = 0
@opened_at = 0
end
def open!
if self.closed?
@state = :opening
@opened_at = Time.now.to_i
end
if !self.open? && @state == :opening
@pos += OPEN_CLOSE_STEP
end
end
def open?
return @pos == Map::GRID_WIDTH_HEIGHT
end
def close!
if self.open?
@state = :closing
end
if !self.closed? && @state == :closing
@pos -= OPEN_CLOSE_STEP
end
end
def closed?
return @pos == 0
end
def interact
if @state == :opening
self.open!
elsif @state == :closing
self.close!
end
end
def inspect
if open?
"#<Door:#{object_id} open>"
else
"#<Door:#{object_id} open>"
end
end
end