… | |
… | |
105 | } |
105 | } |
106 | |
106 | |
107 | ############################################################################# |
107 | ############################################################################# |
108 | # "new" plug-in system |
108 | # "new" plug-in system |
109 | |
109 | |
110 | =item $object->attach ($attachment, ...) |
110 | =item $object->attach ($attachment, key => $value...) |
111 | |
111 | |
112 | Attach a pre-registered attachment to an object. |
112 | Attach a pre-registered attachment to an object. |
113 | |
113 | |
114 | =item $player->attach ($attachment, ...) |
114 | =item $player->attach ($attachment, key => $value...) |
115 | |
115 | |
116 | Attach a pre-registered attachment to a player. |
116 | Attach a pre-registered attachment to a player. |
117 | |
117 | |
118 | =item $map->attach ($attachment, ...) # not yet persistent |
118 | =item $map->attach ($attachment, key => $value...) # not yet persistent |
119 | |
119 | |
120 | Attach a pre-registered attachment to a map. |
120 | Attach a pre-registered attachment to a map. |
121 | |
121 | |
122 | =item cf::attach_global ... |
122 | =item cf::attach_global ... |
123 | |
123 | |
… | |
… | |
245 | |
245 | |
246 | \%undo |
246 | \%undo |
247 | } |
247 | } |
248 | |
248 | |
249 | sub _attach_attachment { |
249 | sub _attach_attachment { |
250 | my ($obj, $name, @args) = @_; |
250 | my ($obj, $name, %arg) = @_; |
251 | |
251 | |
252 | my $res; |
252 | my $res; |
253 | |
253 | |
254 | if (my $attach = $attachment{$name}) { |
254 | if (my $attach = $attachment{$name}) { |
255 | my $registry = $obj->registry; |
255 | my $registry = $obj->registry; |
… | |
… | |
257 | for (@$attach) { |
257 | for (@$attach) { |
258 | my ($klass, @attach) = @$_; |
258 | my ($klass, @attach) = @$_; |
259 | $res = _attach @$registry, $klass, @attach; |
259 | $res = _attach @$registry, $klass, @attach; |
260 | } |
260 | } |
261 | |
261 | |
262 | if (my $cb = delete $registry->[EVENT_OBJECT_INSTANTIATE]) { |
262 | $obj->{$name} = \%arg; |
263 | for (@$cb) { |
|
|
264 | eval { $_->[1]->($obj, @args); }; |
|
|
265 | if ($@) { |
|
|
266 | warn "$@"; |
|
|
267 | warn "... while processing '$name' instantiate with args <@args>.\n"; |
|
|
268 | } |
|
|
269 | } |
|
|
270 | } |
|
|
271 | } else { |
263 | } else { |
272 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
264 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
273 | } |
265 | } |
274 | |
266 | |
275 | push @{$obj->{_attachment}}, $name; |
267 | push @{$obj->{_attachment}}, $name; |
… | |
… | |
277 | $res->{attachment} = $name; |
269 | $res->{attachment} = $name; |
278 | $res |
270 | $res |
279 | } |
271 | } |
280 | |
272 | |
281 | sub cf::object::attach { |
273 | sub cf::object::attach { |
282 | my ($obj, $name, @args) = @_; |
274 | my ($obj, $name, %arg) = @_; |
283 | |
275 | |
284 | _attach_attachment $obj, $name, @args; |
276 | _attach_attachment $obj, $name, %arg; |
285 | } |
277 | } |
286 | |
278 | |
287 | sub cf::player::attach { |
279 | sub cf::player::attach { |
288 | my ($obj, $name, @args) = @_; |
280 | my ($obj, $name, %arg) = @_; |
289 | |
281 | |
290 | _attach_attachment KLASS_PLAYER, $obj, $name, @args; |
282 | _attach_attachment KLASS_PLAYER, $obj, $name, %arg; |
291 | } |
283 | } |
292 | |
284 | |
293 | sub cf::map::attach { |
285 | sub cf::map::attach { |
294 | my ($obj, $name, @args) = @_; |
286 | my ($obj, $name, %arg) = @_; |
295 | |
287 | |
296 | _attach_attachment KLASS_MAP, $obj, $name, @args; |
288 | _attach_attachment KLASS_MAP, $obj, $name, %arg; |
297 | } |
289 | } |
298 | |
290 | |
299 | sub attach_global { |
291 | sub attach_global { |
300 | _attach @CB_GLOBAL, KLASS_GLOBAL, @_ |
292 | _attach @CB_GLOBAL, KLASS_GLOBAL, @_ |
301 | } |
293 | } |
… | |
… | |
364 | |
356 | |
365 | $data = from_json $data; |
357 | $data = from_json $data; |
366 | |
358 | |
367 | for (@$data) { |
359 | for (@$data) { |
368 | my ($name, $args) = @$_; |
360 | my ($name, $args) = @$_; |
369 | attach $obj, $name, @{$args || [] }; |
361 | attach $obj, $name, %{$args || {} }; |
370 | } |
362 | } |
371 | } |
363 | } |
372 | |
364 | |
373 | # basically do the same as instantiate, without calling instantiate |
365 | # basically do the same as instantiate, without calling instantiate |
374 | sub reattach { |
366 | sub reattach { |