/
construction.rb
104 lines (84 loc) · 2.66 KB
/
construction.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
module S2C
module Models
class Construction
attr_reader :id, :planet, :level, :type, :status, :process_tics
# @@statuses = [
# :under_construction,
# :standby,
# :upgrading
# ]
def initialize( planet, type )
@id = Time.now.to_i
S2C::Universe.log( self, "Starting contruction Construction" )
@planet = planet
@level = 0
@type = type
@status = :under_construction
@process_tics = self.upgrade_timing
S2C::Universe.log( self, self.to_s )
end
# I know there are cleaner ways to define this methods
# but for the moment is ok.
def attack
self.property_value( 'attack' )
end
def defense
self.property_value( 'defense' )
end
def power
self.property_value( 'power' )
end
def upgrade_timing
self.property_value( 'upgrade_timing' )
end
def upgrade_black_stuff
self.property_value( 'upgrade_black_stuff' )
end
def upgrade_timing
self.property_value( 'upgrade_timing' )
end
def property_value( property )
init_value = S2C::Config.config[self.type][property]
actual_value = init_value + ( init_value * ( @level * ( 1.1 ** @level ) ) ).round
return actual_value
end
def upgrade
S2C::Universe.log( self, "Upgrading" )
if( self.status != :standby )
raise Exception, "can't upgrade a Construction in status: '#{self.status}'"
end
if( self.planet.black_stuff < self.upgrade_black_stuff )
raise Exception, 'not enough black stuff'
end
self.planet.remove_black_stuff( self.upgrade_black_stuff )
@status = :upgrading
@process_tics = self.upgrade_timing
end
def work_under_construction
S2C::Universe.log( self, "In contruction" )
@process_tics -= 1
if( @process_tics == 0 )
S2C::Universe.log( self, "Built" )
@level = 1
@status = :standby
end
end
def work_upgrading
S2C::Universe.log( self, "Upgrading" )
@process_tics -= 1
if( @process_tics == 0 )
@level += 1
@status = :standby
S2C::Universe.log( self, "Upgraded to level #{self.level}" )
end
end
def work
S2C::Universe.log( self, "Working" )
self.send( "work_#{self.status}" )
end
def to_s
"type:#{self.type} level:#{self.level} status:#{self.status} tics:#{self.process_tics}"
end
end
end
end