… | |
… | |
4 | # TODO: check for player leaving |
4 | # TODO: check for player leaving |
5 | |
5 | |
6 | # implement a 'follow' command |
6 | # implement a 'follow' command |
7 | # don't follow on damned ground |
7 | # don't follow on damned ground |
8 | |
8 | |
|
|
9 | sub teleport { |
|
|
10 | my ($pl, $map, $x, $y) = @_; |
|
|
11 | |
|
|
12 | return if $pl->ob->map->path eq $map |
|
|
13 | && abs ($pl->ob->x - $x) <= 1 |
|
|
14 | && abs ($pl->ob->y - $y) <= 1; |
|
|
15 | |
|
|
16 | my $portal = cf::object::new "exit"; |
|
|
17 | |
|
|
18 | $portal->slaying ($map); |
|
|
19 | $portal->stats->hp ($x); |
|
|
20 | $portal->stats->sp ($y); |
|
|
21 | |
|
|
22 | $portal->apply ($pl->ob); |
|
|
23 | |
|
|
24 | $portal->free; |
|
|
25 | } |
|
|
26 | |
9 | my %follow; |
27 | my %follow; |
|
|
28 | |
|
|
29 | my $timer = Event->timer (interval => 0.2, parked => 1, cb => sub { |
|
|
30 | while (my ($name, $v) = each %follow) { |
|
|
31 | my ($target, $his, $mine) = @$v; |
|
|
32 | my ($who, $other) = (cf::player::find $name, cf::player::find $target); |
|
|
33 | |
|
|
34 | if ($who && $other && $other->ob->map) { |
|
|
35 | my ($map, $x, $y) = ($other->ob->map->path, $other->ob->x, $other->ob->y); |
|
|
36 | |
|
|
37 | if ($map ne $his->[0] || $x != $his->[1] || $y != $his->[2]) { |
|
|
38 | @$mine = @$his; |
|
|
39 | @$his = ($map, $x, $y); |
|
|
40 | } |
|
|
41 | |
|
|
42 | my $map; |
|
|
43 | |
|
|
44 | if ($map = cf::map::find $mine->[0] |
|
|
45 | and !grep $_->flag (cf::FLAG_UNIQUE) && $_->flag (cf::FLAG_IS_FLOOR), |
|
|
46 | $map->at ($mine->[1], $mine->[2])) { |
|
|
47 | teleport $who, @$mine; |
|
|
48 | } else { |
|
|
49 | delete $follow{$name}; |
|
|
50 | $who->ob->message ("You can't follow $target anymore!"); |
|
|
51 | } |
|
|
52 | } else { |
|
|
53 | delete $follow{$name}; |
|
|
54 | $who->ob->message ("$target is gone..."); |
|
|
55 | } |
|
|
56 | } |
|
|
57 | |
|
|
58 | $_[0]->w->stop unless keys %follow; |
|
|
59 | }); |
10 | |
60 | |
11 | cf::register_command follow => 0, sub { |
61 | cf::register_command follow => 0, sub { |
12 | my ($who, $args) = @_; |
62 | my ($who, $args) = @_; |
13 | |
63 | |
14 | my $name = $who->name; |
64 | my $name = $who->name; |
… | |
… | |
23 | $follow{$name} = [ |
73 | $follow{$name} = [ |
24 | $args, |
74 | $args, |
25 | [$other->ob->map->path, $other->ob->x, $other->ob->y], |
75 | [$other->ob->map->path, $other->ob->x, $other->ob->y], |
26 | [$who->map->path, $who->x, $who->y], |
76 | [$who->map->path, $who->x, $who->y], |
27 | ]; |
77 | ]; |
|
|
78 | $timer->start; |
28 | } else { |
79 | } else { |
29 | $who->message ("You must stand directly beside '$args' to follow her/him"); |
80 | $who->message ("You must stand directly beside '$args' to follow her/him"); |
30 | delete $follow{$name}; |
81 | delete $follow{$name}; |
31 | } |
82 | } |
32 | } else { |
83 | } else { |
… | |
… | |
37 | $who->message ("follow mode off"); |
88 | $who->message ("follow mode off"); |
38 | delete $follow{$name}; |
89 | delete $follow{$name}; |
39 | } |
90 | } |
40 | }; |
91 | }; |
41 | |
92 | |
42 | sub teleport { |
93 | cf::attach_to_players |
43 | my ($pl, $map, $x, $y) = @_; |
94 | on_death => sub { |
|
|
95 | my ($pl) = @_; |
44 | |
96 | |
45 | return if $pl->ob->map->path eq $map |
97 | my $name = $pl->ob->name; |
46 | && abs ($pl->ob->x - $x) <= 1 |
|
|
47 | && abs ($pl->ob->y - $y) <= 1; |
|
|
48 | |
98 | |
49 | my $portal = cf::object::new "exit"; |
99 | delete $follow{$name}; |
50 | |
100 | |
51 | $portal->set_slaying ($map); |
|
|
52 | $portal->set_hp ($x); |
|
|
53 | $portal->set_sp ($y); |
|
|
54 | |
|
|
55 | $portal->apply ($pl->ob); |
|
|
56 | |
|
|
57 | $portal->free; |
|
|
58 | } |
|
|
59 | |
|
|
60 | sub on_player_death { |
|
|
61 | my ($event) = @_; |
|
|
62 | |
|
|
63 | my $ob = $event->{who}; |
|
|
64 | my $name = $ob->name; |
|
|
65 | |
|
|
66 | delete $follow{$name}; |
|
|
67 | |
|
|
68 | while (my ($k, $v) = each %follow) { |
101 | while (my ($k, $v) = each %follow) { |
69 | if ($v->[0] eq $name) { |
102 | if ($v->[0] eq $name) { |
70 | delete $follow{$k}; |
103 | delete $follow{$k}; |
|
|
104 | } |
71 | } |
105 | } |
72 | } |
106 | }, |
73 | |
107 | ; |
74 | 0 |
|
|
75 | } |
|
|
76 | |
|
|
77 | sub on_clock { |
|
|
78 | my ($event) = @_; |
|
|
79 | |
|
|
80 | return unless %follow; |
|
|
81 | |
|
|
82 | while (my ($name, $v) = each %follow) { |
|
|
83 | my ($target, $his, $mine) = @$v; |
|
|
84 | my ($who, $other) = (cf::player::find $name, cf::player::find $target); |
|
|
85 | |
|
|
86 | if ($who && $other && $other->ob->map) { |
|
|
87 | my ($map, $x, $y) = ($other->ob->map->path, $other->ob->x, $other->ob->y); |
|
|
88 | |
|
|
89 | if ($map ne $his->[0] || $x != $his->[1] || $y != $his->[2]) { |
|
|
90 | @$mine = @$his; |
|
|
91 | @$his = ($map, $x, $y); |
|
|
92 | } |
|
|
93 | |
|
|
94 | my $map; |
|
|
95 | |
|
|
96 | if ($map = cf::map::map $mine->[0] |
|
|
97 | and !grep $_->flag (cf::FLAG_UNIQUE) && $_->flag (cf::FLAG_IS_FLOOR), |
|
|
98 | $map->at ($mine->[1], $mine->[2])) { |
|
|
99 | teleport $who, @$mine; |
|
|
100 | } else { |
|
|
101 | delete $follow{$name}; |
|
|
102 | $who->ob->message ("You can't follow $target anymore!"); |
|
|
103 | } |
|
|
104 | } else { |
|
|
105 | delete $follow{$name}; |
|
|
106 | $who->ob->message ("$target is gone..."); |
|
|
107 | } |
|
|
108 | } |
|
|
109 | } |
|
|
110 | |
108 | |
111 | |
109 | |
112 | |
110 | |
113 | |
111 | |