Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 271 lines (210 sloc) 7.066 kb
504a3ed @kthakore added scrolling again
authored
1 use strict;
2 use warnings;
3 use SDL;
4 use SDL::Rect;
5 use SDL::Events;
6 use Math::Trig;
7 use Collision::2D ':all';
8 use Data::Dumper;
9 use SDLx::App;
ca47400 @kthakore Switched to Interface.
authored
10 use SDLx::Controller::Interface;
504a3ed @kthakore added scrolling again
authored
11 use SDLx::Sprite::Animated;
12
13 my $app = SDLx::App->new( w => 400, h => 400, dt => 0.02, title => 'Pario' );
14
15 $app->update();
16
17 my @update_rects = ();
18
19 my $sprite = SDLx::Sprite::Animated->new(
4974fb7 @kthakore using bmp for people who don't have image support. bmp support is def…
authored
20 image => 'data/m.bmp',
504a3ed @kthakore added scrolling again
authored
21 rect => SDL::Rect->new( 0, 0, 16, 28 ),
99ef459 @garu fixing left and right animation
garu authored
22 ticks_per_frame => 6,
504a3ed @kthakore added scrolling again
authored
23 );
24
25 $sprite->set_sequences(
26 left => [ [ 0, 1 ], [ 1, 1 ], [ 2, 1 ], [ 3, 1 ], [ 4, 1 ] ],
27 right => [ [ 0, 0 ], [ 1, 0 ], [ 2, 0 ], [ 3, 0 ], [ 4, 0 ] ],
28 stopl => [ [ 0, 1 ] ],
29 stopr => [ [ 0, 0 ] ],
30 jumpr => [ [ 5, 0 ] ],
31 jumpl => [ [ 5, 1 ] ],
32 );
33
34 $sprite->sequence('stopr');
35 $sprite->start();
36
37 my $obj =
ca47400 @kthakore Switched to Interface.
authored
38 SDLx::Controller::Interface->new( x => 10, y => 380, v_x => 0, v_y => 0 );
504a3ed @kthakore added scrolling again
authored
39
40 my @collide_blocks = (
41 [ 19, 330 ],
42 [ 40, 330 ],
43 [ 30, 309 ],
44 [ 120, 400 ],
45 [ 141, 400 ],
46 [ 141, 379 ]
47 );
48
49 foreach ( ( 0 .. 2, 10 ... 15, 17 ... 25 ) ) {
50 push @collide_blocks, [ $_ * 20 + $_ * 1, 400 ];
51 }
52
53 foreach (@collide_blocks) {
54 $_->[1] -= 20;
55 $_->[2] = 20;
56 $_->[3] = 20;
57 }
58
59 my $pressed = {};
60 my $lockjump = 0;
61 my $vel_x = 100;
62 my $vel_y = -102;
63 my $quit = 0;
64 my $gravity = 180;
65 my $dashboard = '';
66 my $w = 16;
67 my $h = 28;
68 my $scroller = 0;
69
70 $obj->set_acceleration(
71 sub {
72 my $time = shift;
73 my $state = shift;
74 $state->v_x(0); #Don't move by default
75 my $ay = 0;
76
77 #Basic movements
78 if ( $pressed->{right} ) {
79 $state->v_x($vel_x);
80 if ( $pressed->{up} ) { $sprite->sequence('jumpr') }
99ef459 @garu fixing left and right animation
garu authored
81 elsif ($sprite->sequence() ne 'right') { $sprite->sequence('right'); }
504a3ed @kthakore added scrolling again
authored
82
83 }
84 elsif ( $sprite->sequence() eq 'right' && !$pressed->{left} ) {
85 $sprite->sequence('stopr');
86 }
87
88 if ( $pressed->{left} ) {
89 $state->v_x( -$vel_x );
90 if ( $pressed->{up} ) { $sprite->sequence('jumpl') }
99ef459 @garu fixing left and right animation
garu authored
91 elsif ($sprite->sequence() ne 'left') { $sprite->sequence('left'); }
504a3ed @kthakore added scrolling again
authored
92 }
93 elsif ( $sprite->sequence() eq 'left' && !$pressed->{right} ) {
94 $sprite->sequence('stopl');
95 }
96 if ( $pressed->{up} && !$lockjump ) {
97
684629b @kthakore Clean up image and sprite sequences
authored
98 $sprite->sequence('jumpr') if ( $sprite->sequence() =~ 'r');
99 $sprite->sequence('jumpl') if ( $sprite->sequence() =~ 'l');
100
504a3ed @kthakore added scrolling again
authored
101 $state->v_y($vel_y);
102 $lockjump = 1;
103
104 }
105
106 my $collision = check_collision( $state, \@collide_blocks );
107 $dashboard = 'Collision = ' . Dumper $collision;
108
109 if ( $collision != -1 && $collision->[0] eq 'x' ) {
110 my $block = $collision->[1];
111
112 #X-axis collision_check
113 if ( $state->v_x() > 0 ) { #moving right
114
115 $state->x( $block->[0] - $w - 3 ); #set to edge of block
116
117 }
118 if ( $state->v_x() < 0 ) { #moving left
119
120 $state->x( $block->[0] + 3 + $block->[2] )
121 ; #set to edge of block
122 }
123 }
124
125 #y-axis collision_check
126
127 if ( $state->v_y() < 0 ) { #moving up
128 if ( $collision != -1 && $collision->[0] eq 'y' ) {
129 my $block = $collision->[1];
130 $state->y( $block->[1] + $block->[3] + 3 );
131 $state->v_y(0);
684629b @kthakore Clean up image and sprite sequences
authored
132
133
504a3ed @kthakore added scrolling again
authored
134 }
135 else {
136
137 #continue falling
138 $ay = $gravity;
139 }
140 }
141
142 else #moving down on ground
143 {
144 if ( $collision != -1 && $collision->[0] eq 'y' ) {
145 my $block = $collision->[1];
146 $state->y( $block->[1] - $h - 1 );
147 $state->v_y(0); # Causes test again in next frame
148 $ay = 0;
149 $lockjump = 0 if ( !$pressed->{up} );
99ef459 @garu fixing left and right animation
garu authored
150 $sprite->sequence( 'stopr' ) if $sprite->sequence eq 'jumpr';
151 $sprite->sequence( 'stopl' ) if $sprite->sequence eq 'jumpl';
504a3ed @kthakore added scrolling again
authored
152 }
684629b @kthakore Clean up image and sprite sequences
authored
153 else
154 { #falling in air
504a3ed @kthakore added scrolling again
authored
155 $ay = $gravity;
99ef459 @garu fixing left and right animation
garu authored
156 # $lockjump = 1;
504a3ed @kthakore added scrolling again
authored
157 }
158 }
159
160 if ( $state->y + 10 > $app->h ) {
161
162 $quit = 1;
163 }
164
165
166
167 if ($scroller) {
168 my $dir = 0;
169 $scroller-- and $dir = +1 if $scroller > 0;
170 $scroller++ and $dir = -1 if $scroller < 0;
171
172 $state->x( $state->x() + $dir );
173
174 $_->[0] += $dir foreach (@collide_blocks);
175
176 }
177 else {
178 if ( $state->x() > $app->w - 100 ) {
179 $scroller = -5;
180 }
181 if ( $state->x() < 100 ) {
182 $scroller = 5;
183 }
184
185 }
186
187 return ( 0, $ay, 0 );
188 }
189 );
190
191 my $render_obj = sub {
192
193 my $state = shift;
194 my $c_rect = SDL::Rect->new( $state->x, $state->y, 16, 28 );
195 $sprite->x( $state->x );
196 $sprite->y( $state->y );
4974fb7 @kthakore using bmp for people who don't have image support. bmp support is def…
authored
197 $sprite->draw($app->surface);
198 #$app->draw_rect( [ $state->x, $state->y, 16, 28 ], 0xFF00FFFF );
504a3ed @kthakore added scrolling again
authored
199
200 };
201
202 $app->add_event_handler(
203 sub {
4f10e2f @kthakore need stop not
authored
204 $_[1]->stop if $_[0]->type == SDL_QUIT || $quit;
504a3ed @kthakore added scrolling again
authored
205
206 my $key = $_[0]->key_sym;
207 my $name = SDL::Events::get_key_name($key) if $key;
208
209 if ( $_[0]->type == SDL_KEYDOWN ) {
210 $pressed->{$name} = 1;
211 }
212 elsif ( $_[0]->type == SDL_KEYUP ) {
213 $pressed->{$name} = 0;
214 }
215 }
216 );
217
218 $app->add_show_handler(
219 sub {
220 $app->draw_rect( [ 0, 0, $app->w, $app->h ], 0x0 );
9743105 @kthakore Fixed up scroller
authored
221 $app->draw_rect( $_, 0xFF0000FF ) foreach @collide_blocks;
504a3ed @kthakore added scrolling again
authored
222
223 #::GFX::Primitives::string_color( $app, $app->w/2-100, 0, $dashboard, 0xFF0000FF);
224 SDL::GFX::Primitives::string_color(
225 $app,
226 $app->w / 2 - 100,
227 $app->h / 2,
228 "Mario is DEAD", 0xFF0000FF
229 ) if $quit;
230
231 }
232 );
233
9743105 @kthakore Fixed up scroller
authored
234 $obj->attach( $app, $render_obj );
504a3ed @kthakore added scrolling again
authored
235 $app->add_show_handler( sub { $app->update(); } );
236
9743105 @kthakore Fixed up scroller
authored
237 $app->run();
504a3ed @kthakore added scrolling again
authored
238
239 sub check_collision {
240 my ( $mario, $blocks ) = @_;
241
242 my @collisions = ();
243
244 foreach (@$blocks) {
245 my $hash = {
246 x => $mario->x,
247 y => $mario->y,
248 w => $w,
249 h => $h,
250 xv => $mario->v_x * 0.02,
251 yv => $mario->v_y * 0.02
252 };
253 my $rect = hash2rect($hash);
254 my $bhash = { x => $_->[0], y => $_->[1], w => $_->[2], h => $_->[3] };
255 my $block = hash2rect($bhash);
256 my $c =
257 dynamic_collision( $rect, $block, interval => 1, keep_order => 1 );
258 if ($c) {
259
260 my $axis = $c->axis() || 'y';
261
262 return [ $axis, $_ ];
263
264 }
265
266 }
267
268 return -1;
269
270 }
Something went wrong with that request. Please try again.