Permalink
Browse files

Added prototype jumper. Need to refactor this

  • Loading branch information...
Kartik Thakore
Kartik Thakore committed Aug 18, 2010
1 parent c492f04 commit 6c8aee76a22ceab053a9be7207877ed2863e1105
Showing with 98 additions and 42 deletions.
  1. +98 −42 scroller.pl
View
@@ -1,57 +1,113 @@
-package Runner;
+use strict;
+use warnings;
+use SDL;
+use SDL::Rect;
+use SDL::Events;
+use Math::Trig;
+use SDLx::App;
+use SDLx::Controller::Object;
-use base 'SDLx::Controller::Object';
+my $app = SDLx::App->new( w => 400, h => 400, dt => 0.02 );
-sub new {
- my $class = shift;
- my %options = @_;
- my $self = SDLx::Controller::Object->new(%options);
+$app->update();
- my $accel = sub {
+my @update_rects = ();
- my ( $t, $state ) = @_;
- my $k = 10;
- my $b = 1;
- my $ax = ( ( -1 * $k ) * ( $state->x ) - $b * $state->v_x );
+my $obj =
+ SDLx::Controller::Object->new( x => 10, y => 380, v_x => 0, v_y => 0 );
- return ( 10, 0, 0 );
- };
+my $move = '';
+my $moving = -1;
+my $jumping = -1;
+my $jump_count = 20;
+my $print = '';
- $self->set_acceleration($accel);
- $self->acceleration(1);
- $options{app}->add_object( $self, \&render, $options{app} );
- $self = bless $self, $class;
- return $self;
-}
+$obj->set_acceleration(
+ sub {
+ my $time = shift;
+ my $state = shift;
+ my $ay = 180;
-sub render {
- my $state = shift;
- my $app = shift;
- $app->draw_rect( [ 0, 0, $app->w, $app->h ], 0x0 );
- $app->draw_rect( [ $state->x, $state->y, 2, 2 ], 0xFF0FFF );
- $app->update();
+ if ( $state->y > 390 && $move !~ 'up' ) {
+ $state->v_y(0);
+ $ay = 0;
+ $state->y(390);
+ if ( $jumping == 0 ) { $jumping = -1; }
-}
+ }
-package main;
-use strict;
-use warnings;
-use Time::HiRes qw( time sleep );
-use SDL;
-use SDLx::App;
-use SDL::Event;
-use SDL::Events;
+ if ( $move =~ 'up' && $jumping < 0 ) {
+ $state->y(389); #Get his jump started.
+ $state->v_y(-82);
+ $jumping++;
+ }
-use SDLx::Controller::Object;
-my $app = SDLx::App->new( w => 200, h => 200, title => "timestep" );
+ if ( $jumping == -1 ) {
+
+ #We can only stop moving when we are not jumping. But let us know that we should't continue
+ if ( $moving == -1 && $move =~ 'none' ) { $state->v_x(0); }
+
+ if ( $move =~ 'left' ) { $state->v_x(-80); $moving = 1 }
+ if ( $move =~ 'right' ) { $state->v_x(80); $moving = 1 }
+ }
+
+ if ( $move =~ 'stopl' ) { $moving = -1 }
+ if ( $move =~ 'stopr' ) { $moving = -1 }
+
-my $spring = Runner->new( x => 0, y => 150, app => $app );
+ # $print = " $move | $moving ";
+
+ $move = 'none';
+ return ( 0, $ay, 0 );
+ }
+);
+
+my $render_obj = sub {
+
+ my $state = shift;
+
+ #SDL::GFX::Primitives::string_color( $app, 0, 0, "$jumping|$print",
+ # 0xFFFFFFF );
+
+ my $s_rect = SDL::Rect->new( 0, 0, $app->w, $app->h );
+ my $p_rect =
+ SDL::Rect->new( $obj->previous->x - 5, $obj->previous->y - 5, 20, 20 );
+ my $c_rect = SDL::Rect->new( $state->x, $state->y, 10, 10 );
+
+ push @update_rects, $s_rect;
+ $app->draw_rect( $p_rect, 0x0 );
+ $app->draw_rect( $c_rect, 0xFF00CCFF );
-my $event = sub {
- return 0 if $_[0]->type == SDL_QUIT;
- return 1;
};
-$app->add_event_handler($event);
-$app->run_test();
+$app->add_event_handler(
+ sub {
+ return 0 if $_[0]->type == SDL_QUIT;
+
+ if ( $_[0]->type == SDL_KEYDOWN ) {
+ my $key = $_[0]->key_sym;
+ $move .= SDL::Events::get_key_name($key);
+ return 1;
+ }
+ elsif ( $_[0]->type == SDL_KEYUP ) {
+ if ( $_[0]->key_sym == SDLK_LEFT ) {
+ $move = 'stopl'; return 1;
+ }
+ elsif ( $_[0]->key_sym == SDLK_RIGHT ) {
+ $move = 'stopr'; return 1;
+ }
+ }
+ return 1;
+ }
+);
+
+$app->add_show_handler(
+ sub { $app->draw_rect( [ 0, 0, $app->w, $app->h ], 0x0 ); } );
+
+$app->add_object( $obj, $render_obj );
+
+$app->add_show_handler(
+ sub { $app->update( \@update_rects ); @update_rects = (); } );
+
+$app->run_test;

0 comments on commit 6c8aee7

Please sign in to comment.