#! /opt/perl/bin/perl package Mob; use Moose; use SDL::Events; use Data::Dumper; has 'position' => (is => 'rw', builder => 'build_pos'); has 'velocity' => (is => 'rw', builder => 'build_vel'); has 'max_speed' => (is => 'rw', default => 50); # Delta velocity, or how much we change the speed each event has 'dv' => (is => 'rw', default => 3); sub build_pos { return {x => 200, y=> 200}; } sub build_vel { return {x => 2, y=> 2}; } sub move_react { my $self = shift; my $event = shift; SDL::Events::pump_events; my $keys = SDL::Events::get_key_state; if ($keys->[SDLK_LEFT]) { $self->{velocity}->{x} -= $self->{dv} unless ($self->{velocity}->{x} < -$self->{max_speed}); } if ($keys->[SDLK_RIGHT]) { $self->{velocity}->{x} += $self->{dv} unless ($self->{velocity}->{x} > $self->{max_speed}); } # Slow horizontal movement if neither modifier keys are pressed if (!$keys->[SDLK_LEFT] && !$keys->[SDLK_RIGHT]) { if ($self->{velocity}->{x} > 0) { $self->{velocity}->{x} -= 1; } elsif ($self->{velocity}->{x} < 0) { $self->{velocity}->{x} += 1; } } if ($keys->[SDLK_UP]) { $self->{velocity}->{y} -= $self->{dv} unless ($self->{velocity}->{y} < -$self->{max_speed}); } if ($keys->[SDLK_DOWN]) { $self->{velocity}->{y} += $self->{dv} unless ($self->{velocity}->{y} > $self->{max_speed}); } # Slow vertical movement if neither modifier keys are pressed if (!$keys->[SDLK_UP] && !$keys->[SDLK_DOWN]) { if ($self->{velocity}->{y} > 0) { $self->{velocity}->{y} -= 1; } elsif ($self->{velocity}->{y} < 0) { $self->{velocity}->{y} += 1; } } } sub move { my ($self, $step, $app, $time) = @_; SDL::Events::pump_events; my $keys = SDL::Events::get_key_state; $self->{position}->{x} += $self->{velocity}->{x} * $step; $self->{position}->{y} += $self->{velocity}->{y} * $step; # No keys? Slow them ALL DOWN if (!$keys->[SDLK_UP] && !$keys->[SDLK_DOWN] && !$keys->[SDLK_LEFT] && !$keys->[SDLK_RIGHT]) { if ($self->{velocity}->{x} > 0) { $self->{velocity}->{x} -= 1; } elsif ($self->{velocity}->{x} < 0) { $self->{velocity}->{x} += 1; } if ($self->{velocity}->{y} > 0) { $self->{velocity}->{y} -= 1; } elsif ($self->{velocity}->{y} < 0) { $self->{velocity}->{y} += 1; } } } sub draw { my ($self, $step, $app) = @_; $app->draw_circle_filled([$self->{position}->{x}, $self->{position}->{y}], 10, [250,255,250,255]); } 1;