Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Attempt to use Collision::Util.

  • Loading branch information...
commit 492bdf442a8a31fd11693b4ca8502d640e4fabb2 1 parent 9b8e83c
@jtpalmer authored
View
2  lib/Games/Maze/SDL.pm
@@ -41,7 +41,7 @@ sub run {
my $height = $cell_height * $cells_y;
my $player_width = 24;
my $player_height = 24;
- my $dt = 0.025;
+ my $dt = 0.0025;
my $maze_model = Games::Maze::SDL::Model::Maze->new(
cells_x => $cells_x,
View
3  lib/Games/Maze/SDL/Model/Maze.pm
@@ -4,6 +4,7 @@ package Games::Maze::SDL::Model::Maze;
use Moose;
use Games::Maze;
+use Games::Maze::SDL::Model::Wall;
use Games::Maze::SDL::Role::Observable;
use POSIX 'floor';
@@ -225,7 +226,7 @@ sub cell_walls {
}
}
- return \@walls;
+ return [ map { Games::Maze::SDL::Model::Wall->new(%$_) } @walls ];
}
no Moose;
View
79 lib/Games/Maze/SDL/Model/Player.pm
@@ -6,8 +6,9 @@ use Moose;
use Games::Maze::SDL::Types;
use Games::Maze::SDL::Role::Observable;
use Games::Maze::SDL::Model::Maze;
-use Collision::2D ':all';
+use Collision::Util ':interval';
use POSIX 'floor';
+use Data::Dumper;
with 'Games::Maze::SDL::Role::Observable';
@@ -64,7 +65,7 @@ has 'velocity_y' => (
has 'max_velocity' => (
is => 'ro',
isa => 'Num',
- default => 2.5,
+ default => 0.25,
);
has 'acceleration_y' => (
@@ -91,6 +92,26 @@ sub _build_y {
- $self->height / 2;
}
+sub v_x {
+ my ($self) = @_;
+ return $self->velocity_x;
+}
+
+sub v_y {
+ my ($self) = @_;
+ return $self->velocity_y;
+}
+
+sub w {
+ my ($self) = @_;
+ return $self->width;
+}
+
+sub h {
+ my ($self) = @_;
+ return $self->height;
+}
+
after qw( x y direction velocity_x velocity_y ) => sub {
my $self = shift;
@@ -106,18 +127,18 @@ after 'direction' => sub {
my $d = shift;
if ( $d eq 'north' ) {
$self->acceleration_x(0);
- $self->acceleration_y(-1.0);
+ $self->acceleration_y(-0.1);
}
if ( $d eq 'south' ) {
$self->acceleration_x(0);
- $self->acceleration_y(1.0);
+ $self->acceleration_y(0.1);
}
if ( $d eq 'west' ) {
- $self->acceleration_x(-1.0);
+ $self->acceleration_x(-0.1);
$self->acceleration_y(0);
}
if ( $d eq 'east' ) {
- $self->acceleration_x(1.0);
+ $self->acceleration_x(0.1);
$self->acceleration_y(0);
}
@@ -141,19 +162,9 @@ sub move {
my %v = ( x => $self->velocity_x, y => $self->velocity_y );
my %a = ( x => $self->acceleration_x, y => $self->acceleration_y );
- my $rect = hash2rect(
- { x => $d{x},
- y => $d{y},
- xv => $v{x},
- yv => $v{y},
- w => $self->width,
- h => $self->height,
- }
- );
-
foreach my $dim (qw( x y )) {
if ( $a{$dim} == 0 ) {
- $v{$dim} *= 0.9;
+ $v{$dim} *= 0.99;
}
else {
$v{$dim} += $dt * $a{$dim};
@@ -170,38 +181,28 @@ sub move {
$d{$dim} += $dt * $v{$dim};
}
- my $cell_x = floor( $self->x / $self->maze->cell_width ) + 1;
- my $cell_y = floor( $self->y / $self->maze->cell_height ) + 1;
-
+ my $cell_x = floor( ( $self->x + $self->width / 2 ) / $self->maze->cell_width ) + 1;
+ my $cell_y = floor( ( $self->y + $self->height / 2 ) / $self->maze->cell_height ) + 1;
my @collisions;
foreach my $wall ( @{ $self->maze->cell_walls( $cell_x, $cell_y ) } ) {
- my $c = dynamic_collision(
- $rect,
- hash2rect($wall),
- interval => $dt,
- keep_order => 1,
- );
+ my $c = $self->check_collision_interval( $wall, 1 );
push @collisions, [ $wall, $c ] if $c;
}
- if (@collisions) {
- my ($c) = sort { $a->[1]->time <=> $b->[1]->time } @collisions;
+ foreach my $c (@collisions) {
+ my ( $wall, $axis ) = @$c;
- my ( $wall, $collision ) = @$c;
+ print Dumper( $axis, $wall ) if $axis->[0] || $axis->[1];
- if ( $collision->axis eq 'x' ) {
- $d{x}
- = ( $v{x} <=> 0 ) == 1
- ? $wall->{x} - $self->width - 1
- : $wall->{x} + 2;
+ if ( $axis->[0] ) {
+ $d{x} = $wall->x - $self->width - 1 if $axis->[0] == -1;
+ $d{x} = $wall->x + 2 if $axis->[0] == 1;
$v{x} = 0;
}
- else {
- $d{y}
- = ( $v{y} <=> 0 ) == 1
- ? $wall->{y} - $self->height - 1
- : $wall->{y} + 2;
+ if ( $axis->[1] ) {
+ $d{y} = $wall->y - $self->height - 1 if $axis->[1] == 1;
+ $d{y} = $wall->y + 2 if $axis->[1] == -1;
$v{y} = 0;
}
}
View
16 lib/Games/Maze/SDL/Model/Wall.pm
@@ -0,0 +1,16 @@
+package Games::Maze::SDL::Model::Wall;
+
+use strict;
+use warnings;
+
+use Class::XSAccessor {
+ constructor => 'new',
+ accessors => [ 'x', 'y', 'w', 'h' ],
+};
+
+sub v_x { 0 }
+sub v_y { 0 }
+
+1;
+
+__END__
View
2  lib/Games/Maze/SDL/View/Player.pm
@@ -70,7 +70,7 @@ sub handle_event {
$self->sprite->y( $self->model->y );
my $v = $self->model->velocity;
- $self->sprite->ticks_per_frame( floor( 5 / $v ) + 5 )
+ $self->sprite->ticks_per_frame( floor( 1 / $v ) + 5 )
unless $v == 0;
}
elsif ( $event->{type} eq 'turned' ) {
Please sign in to comment.
Something went wrong with that request. Please try again.