1 |
#! perl |
2 |
|
3 |
use List::Util; |
4 |
|
5 |
sub set_stack { |
6 |
my ($map, $x, $y, $as) = @_; |
7 |
|
8 |
$_->insert_ob_in_map_at ($map, $_, cf::INS_ON_TOP, $x, $y) |
9 |
for @$as; |
10 |
} |
11 |
|
12 |
sub on_move { |
13 |
my ($event, $who) = @_; |
14 |
|
15 |
# on first call (there is no initialiser callback), initialise |
16 |
my $self = $who->{map_grid_move} ||= { |
17 |
x1 => $who->x, |
18 |
y1 => $who->y, |
19 |
split /(?:\s+|=)/, $event->options, |
20 |
}; |
21 |
|
22 |
my ($x1, $y1) = ($self->{x1}, $self->{y1}); |
23 |
|
24 |
# this is horribly ugly code.. why can't there be a simple function to just move objects, |
25 |
# instead of having 20+ of them that all crash in different ways. |
26 |
|
27 |
if (0.5 <= rand) { |
28 |
# horizontal |
29 |
|
30 |
my $y = $y1 + int rand $self->{height}; |
31 |
|
32 |
my @ass = map [grep $_->remove || 1, $who->map->at ($_, $y)], $x1 .. $x1 + $self->{width} - 1; |
33 |
|
34 |
if (0.5 <= rand) { |
35 |
my $as = pop @ass; unshift @ass, $as; |
36 |
} else { |
37 |
my $as = shift @ass; push @ass, $as; |
38 |
} |
39 |
|
40 |
set_stack $who->map, $_, $y, $ass[$_ - $x1] for $x1 .. $x1 + $self->{width} - 1; |
41 |
|
42 |
} else { |
43 |
# vertical |
44 |
|
45 |
my $x = $x1 + int rand $self->{width}; |
46 |
|
47 |
my @ass = map [grep $_->remove || 1, $who->map->at ($x, $_)], $y1 .. $y1 + $self->{height} - 1; |
48 |
|
49 |
if (0.5 <= rand) { |
50 |
my $as = pop @ass; unshift @ass, $as; |
51 |
} else { |
52 |
my $as = shift @ass; push @ass, $as; |
53 |
} |
54 |
|
55 |
set_stack $who->map, $x, $_, $ass[$_ - $y1] for $y1 .. $y1 + $self->{height} - 1; |
56 |
|
57 |
} |
58 |
|
59 |
1 |
60 |
} |
61 |
|
62 |
|
63 |
|