forked from DizzyDogg/Wilderness
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObject.pm
305 lines (267 loc) · 7.55 KB
/
Object.pm
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
package Object;
use strict;
use warnings;
use Container;
use Data::Dumper;
use overload
'bool' => sub {
my $self = shift;
return $self;
},
'""' => sub {
my $self = shift;
return $self->name();
},
'eq' => sub {
my $a = shift;
my $b = shift;
$a = $a->name() if (ref $a && $a->can('name'));
$b = $b->name() if (ref $b && $b->can('name'));
return $a eq $b;
},
'ne' => sub {
my $a = shift;
my $b = shift;
$a = $a->name() if (ref $a && $a->can('name'));
$b = $b->name() if (ref $b && $b->can('name'));
return $a ne $b;
},
'==' => sub {
my $a = shift;
my $b = shift;
$a = $a->name() if (ref $a && $a->can('name'));
$b = $b->name() if (ref $b && $b->can('name'));
return $a eq $b;
},
'!=' => sub {
my $a = shift;
my $b = shift;
$a = $a->name() if (ref $a && $a->can('name'));
$b = $b->name() if (ref $b && $b->can('name'));
return $a ne $b;
},
;
sub new {
my $package = shift;
my $self = {@_};
my $name = lc((split '::', $package)[-1]);
(my $path = $package) =~ s/::/\//;
$path .= '.pm';
require $path;
$self->{'inventory'} = Container->new();
$self->{'visible'} = Container->new();
$self->{'composition'} = Container->new();
bless $self, $package;
$self->initialize();
$self->{'name'} ||= $name;
return $self;
}
sub initialize { }
sub is_item { return }
sub is_character { return }
sub is_fixture { return }
sub is_player { return }
sub is_place { return }
sub is_biome { return }
sub is_obstruction { return }
#find a way to check the 'required_action' instead of 'is_choppable'
sub is_choppable { return }
sub get_health { undef }
sub name {
my $self = shift;
return $self->{'name'};
}
sub required_sharpness { return 0 }
sub required_weight { return 0 }
sub sharpness { return 0 }
sub weight { return 0 }
sub has_requirements {
my $self = shift;
return $self->required_sharpness()
|| $self->required_weight()
;
}
sub has_can_damage {
my $self = shift;
my $item = shift;
my @equips = $self->get_visible();
foreach my $equip (@equips) {
return $equip if $equip->can_damage($item);
}
warn "\tYou have nothing equipped strong enough to affect the $item\n";
return;
}
sub can_damage {
my $self = shift;
my $victim = shift;
my $sharp = $self->sharpness();
my $weight = $self->weight();
my $req_sharp = $victim->required_sharpness();
my $req_weight = $victim->required_weight();
return $sharp >= $req_sharp && $weight >= $req_weight;
}
# Same as is_here, but skipping characters and their visible.
sub can_reach {
my $self = shift;
my $thing = shift;
my $place = $self->where();
my @items = $place->get_items();
push @items, $self->get_inventory(), $self->get_visible();
foreach my $item (@items) {
next if $item->is_character();
return $item if $item eq $thing;
my @deep_items = $item->get_deep_visible();
foreach my $deep_item (@deep_items) {
return $deep_item if $deep_item eq $thing;
}
}
return 0;
}
sub drop {
my $self = shift;
my $what = shift;
my $here = $self->where();
return warn "\tYou don't have a $what\n" unless $self->has($what);
$self->inventory_remove($what) || $self->visible_remove($what);
$here->add_item($what);
print "\tYou place the $what gently on the ground\n" if $self->is_player();
return $self;
}
sub has {
my $self = shift;
my $item = shift;
return ( $self->has_in_visible($item) || $self->has_in_inventory($item) ) ? 1 : 0;
}
sub has_in_inventory {
my $self = shift;
my $item = shift;
return $self->{'inventory'}->contains($item);
}
sub has_in_visible {
my $self = shift;
my $item = shift;
return $self->{'visible'}->contains($item);
}
sub has_in_composition {
my $self = shift;
my $item = shift;
return $self->{'composition'}->contains($item);
}
sub where {
my $self = shift;
return $self->{'location'};
}
sub desc {
return 'There does not appear to be anything special about it';
}
sub describe {
my $self = shift;
return join ("\n\t", $self->desc(), $self->get_sub_description());
}
sub process {
my $self = shift;
return "Congratulations! You have just made a $self"
}
sub get_ingredients { return }
sub get_tools { return }
sub get_sub_description {
my $self = shift;
my @items = $self->get_visible();
my @lines;
if ( @items ) {
foreach my $item (@items) {
push @lines, "\tThe $self has a $item";
my @items_items = $item->get_visible();
foreach my $items_item (@items_items) {
push @lines, "\tThe $item has a $items_item";
}
}
}
return @lines;
}
sub equip {
my $self = shift;
my $item = shift;
return warn "\tWhat would you like to equip?\n" unless $item;
return warn "\tI do not know what a $item is\n" unless ref $item;
return warn "\tYou already have a $item equipped\n" if $self->has_in_visible($item);
return warn "\tYou do not have a $item to equip\n" unless $self->has_in_inventory($item);
$self->inventory_remove($item);
$self->visible_add($item);
print "\tYou take the $item out of your pack and place it in your hand\n";
return 1;
}
sub unequip {
my $self = shift;
my $item = shift;
return warn "\tWhat would you like to unequip?\n" unless $item;
return warn "\tI do not know what a $item is\n" unless ref $item;
return warn "\tYou do not have a $item equipped\n" unless $self->has_in_visible($item);
$self->visible_remove($item);
$self->inventory_add($item);
print "\tYou place the $item back in your pack\n";
return 1;
}
sub inventory_add { _add-> (@_, 'inventory') }
sub visible_add { _add->(@_, 'visible') }
sub composition_add { _add->(@_, 'composition') }
sub _add {
my $self = shift;
my $item = shift;
my $container = shift;
my $added = $self->{$container}->add($item);
$item->{'location'} = $self if $added;
return $added;
}
sub inventory_remove { _remove-> (@_, 'inventory') }
sub visible_remove { _remove->(@_, 'visible') }
sub composition_remove { _remove->(@_, 'composition') }
sub _remove {
my $self = shift;
my $item = shift;
my $container = shift;
my $removed = $self->{$container}->remove($item);
return $removed;
}
sub get_inventory { _get-> (@_, 'inventory') }
sub get_visible { _get->(@_, 'visible') }
sub get_composition { _get->(@_, 'composition') }
sub _get {
my $self = shift;
my $container = shift;
my @items = $self->{$container}->get_all();
return @items;
}
sub get_deep_visible {
my $self = shift;
my @items = $self->get_visible();
my @all_items;
push @all_items, @items;
foreach my $item (@items) {
push @all_items, $item->get_deep_visible();
}
return @all_items;
}
sub visible_containers {
my $self = shift;
my $find = shift;
return $self if $find == $self;
my @items = $self->get_visible();
foreach my $item (@items) {
my @deep_items = $item->visible_containers($find);
return (@deep_items, $self) if @deep_items;
}
return;
}
sub has_on_ground {
my $place = shift;
my $item = shift;
my @containers = $place->visible_containers($item);
return $containers[1] == $place;
}
sub get_all {
my $self = shift;
my @possessions = ( $self->get_visible(), $self->get_inventory() );
return @possessions;
}
1;