Permalink
Browse files

Make plasma move

  • Loading branch information...
1 parent eb649ea commit 3fcf97695af565b59614c28141a6f5069c7aa08a @jtpalmer committed Mar 27, 2011
Showing with 49 additions and 3 deletions.
  1. +49 −3 week4/lib/Games/ElectricLines.pm
@@ -41,6 +41,12 @@ has _horizontal_lines => (
builder => '_build_horizontal_lines',
);
+has _plasma => (
+ is => 'ro',
+ isa => 'ArrayRef',
+ default => sub { [] },
+);
+
sub _build_app {
return SDLx::App->new(
title => 'Electric Lines',
@@ -71,7 +77,7 @@ sub _build_starting_points {
my $app = $self->app;
my $space = $app->h / $count;
- my $x = 0;
+ my $x = 0;
my @points;
@@ -86,7 +92,7 @@ sub _build_starting_points {
sub _build_horizontal_lines {
my ($self) = @_;
- my $x = $self->app->w;
+ my $x = $self->app->w;
my @lines;
foreach my $point ( @{ $self->_starting_points } ) {
@@ -99,6 +105,8 @@ sub _build_horizontal_lines {
sub BUILD {
my ($self) = @_;
+ $self->_add_plasma();
+
my $app = $self->app;
$app->add_event_handler( sub { $self->handle_event(@_) } );
$app->add_move_handler( sub { $self->handle_move(@_) } );
@@ -111,6 +119,10 @@ sub handle_event {
sub handle_move {
my ( $self, $step, $app, $t ) = @_;
+
+ foreach my $plasma ( @{ $self->_plasma } ) {
+ $self->_move_plasma( $plasma, $step );
+ }
}
sub handle_show {
@@ -120,10 +132,44 @@ sub handle_show {
foreach my $line ( @{ $self->_horizontal_lines } ) {
$app->draw_line( @$line, 0xFFFFFFFF );
}
- $self->sprite->draw($app);
+ foreach my $plasma ( @{ $self->_plasma } ) {
+ $self->_draw_plasma($plasma);
+ }
$app->update();
}
+sub _move_plasma {
+ my ( $self, $plasma, $step ) = @_;
+
+ $plasma->{x} += $step;
+}
+
+sub _draw_plasma {
+ my ( $self, $plasma ) = @_;
+
+ my $sprite = $self->sprite;
+ $sprite->x( $plasma->{x} - $sprite->rect->w / 2 );
+ $sprite->y( $plasma->{y} - $sprite->rect->h / 2 );
+ $sprite->draw( $self->app );
+}
+
+sub _add_plasma {
+ my ($self) = @_;
+
+ my @points = @{ $self->_starting_points };
+ my $i = int rand @points;
+ my @start = @{ $points[$i] };
+ my $line = $self->_horizontal_lines->[$i];
+
+ my %plasma = (
+ x => $start[0],
+ y => $start[1],
+ line => $line,
+ );
+
+ push @{ $self->_plasma }, \%plasma;
+}
+
no Mouse;
1;

0 comments on commit 3fcf976

Please sign in to comment.