Some progress.
[rbot] / rpg / rpg_creatures.rb
1
2 Position = Struct.new( :x, :y )
3
4 class GameObject
5   
6   attr_accessor :pos, :name, :object_type, :description
7
8   def initialize
9     @pos = Position.new( nil, nil )
10     @name = ""
11     @object_type = ""
12     @description = ""
13   end
14 end
15
16
17 class Creature < GameObject
18
19   attr_accessor :state, :hp, :thac0, :hd, :ac, :xp_value, :inventory
20
21   def initialize
22     super
23
24     @state = "idle"
25     @inventory = []
26   end
27
28   def d4( num = 1 )
29     result = 0
30     num.times { result += rand( 4 ) + 1 }
31     result
32   end
33
34     
35   def d20( num = 1 )
36     result = 0
37     num.times { result += rand( 20 ) + 1 }
38     result
39   end
40
41  
42   def attack( g, target ) 
43     @state = "fighting"
44     target.state = "fighting"
45
46     if d20 < @thac0 - target.ac
47       g.say( "#{name} misses." )
48       return
49     end
50
51     damage = d4( @hd )
52     target.hp -= damage
53
54     g.say( "#{@name} attacks #{target.name}. Hit! (#{damage} damage)."  )
55   end
56
57 end
58  
59
60 class Player < Creature
61
62   attr_accessor :xp
63
64   def initialize
65     super
66
67     @object_type = "Human"
68     @hp = 20
69     @xp = 0
70     @thac0 = 15 
71     @hd = 2 
72     @ac = 4
73
74     @description = "A typical human geek."
75   end
76
77
78   def attack( g, target )
79     super
80
81     if target.hp < 0
82       @xp += target.xp_value
83       g.say( "#{@name} gains #{target.xp_value} experience points!" ) 
84     end
85   end
86
87 end
88
89
90 class Monster < Creature
91
92   @@monsters = [] 
93
94   def Monster.monsters
95     @@monsters
96   end
97     
98
99   def Monster.register( monster )
100     @@monsters << monster
101   end
102
103
104   def act( g )
105     case @state
106     when "idle"
107       return
108     when "fighting"
109       g.objects.each_value do |o| 
110         if o.instance_of?( Player ) and o.pos = @pos
111           attack( g, o )
112         end  
113       end
114     end
115   end
116
117 end    
118
119
120 class Orc < Monster
121   
122   Monster.register Orc
123
124   def initialize
125     super
126
127     @object_type = "Orc"
128     @hp = 14
129     @thac0 = 19
130     @ac = 6
131     @hd = 1
132     @xp_value = 15
133
134     @description = "The Orc is a humanoid creature resembling a caveman. It has dangerous looking fangs and a snout. Somehow, it does not look very smart."
135   end
136
137 end
138
139
140 class Slime < Monster
141
142   Monster.register Slime
143
144   def initialize
145     super
146
147     @object_type = "Slime"
148     @hp = 8
149     @thac0 = 19
150     @ac = 5
151     @hd = 5
152     @xp_value = 60
153
154     @description = "The Slime is a slimy jelly, oozing over the ground. You really don't feel like touching that." 
155   end  
156
157 end
158
159
160 class Weapon < GameObject
161 end
162
163
164 class Sword < Weapon
165
166   def initialize
167     super
168
169     @object_type = "Sword"
170     @description = "A metal sword"
171   end
172
173 end
174