Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

initial

  • Loading branch information...
commit 03a23706abe4fe248df6a99ebf8a22dc6cc51bd3 0 parents
Tobias Leich authored
Showing with 586 additions and 0 deletions.
  1. 0  CHANGELOG
  2. 0  README
  3. 0  TODO
  4. BIN  data/card_0.png
  5. BIN  data/card_1.png
  6. BIN  data/card_10.png
  7. BIN  data/card_11.png
  8. BIN  data/card_12.png
  9. BIN  data/card_13.png
  10. BIN  data/card_14.png
  11. BIN  data/card_15.png
  12. BIN  data/card_16.png
  13. BIN  data/card_17.png
  14. BIN  data/card_18.png
  15. BIN  data/card_19.png
  16. BIN  data/card_2.png
  17. BIN  data/card_20.png
  18. BIN  data/card_21.png
  19. BIN  data/card_22.png
  20. BIN  data/card_23.png
  21. BIN  data/card_24.png
  22. BIN  data/card_25.png
  23. BIN  data/card_26.png
  24. BIN  data/card_27.png
  25. BIN  data/card_28.png
  26. BIN  data/card_29.png
  27. BIN  data/card_3.png
  28. BIN  data/card_30.png
  29. BIN  data/card_31.png
  30. BIN  data/card_32.png
  31. BIN  data/card_33.png
  32. BIN  data/card_34.png
  33. BIN  data/card_35.png
  34. BIN  data/card_36.png
  35. BIN  data/card_37.png
  36. BIN  data/card_38.png
  37. BIN  data/card_39.png
  38. BIN  data/card_4.png
  39. BIN  data/card_40.png
  40. BIN  data/card_41.png
  41. BIN  data/card_42.png
  42. BIN  data/card_43.png
  43. BIN  data/card_44.png
  44. BIN  data/card_45.png
  45. BIN  data/card_46.png
  46. BIN  data/card_47.png
  47. BIN  data/card_48.png
  48. BIN  data/card_49.png
  49. BIN  data/card_5.png
  50. BIN  data/card_50.png
  51. BIN  data/card_51.png
  52. BIN  data/card_6.png
  53. BIN  data/card_7.png
  54. BIN  data/card_8.png
  55. BIN  data/card_9.png
  56. BIN  data/card_back.png
  57. BIN  data/empty_stack.png
  58. BIN  data/empty_target_0.png
  59. BIN  data/empty_target_1.png
  60. BIN  data/empty_target_2.png
  61. BIN  data/empty_target_3.png
  62. +279 −0 lib/SDLx/LayerManager.pm
  63. +307 −0 solitaire.pl
0  CHANGELOG
No changes.
0  README
No changes.
0  TODO
No changes.
BIN  data/card_0.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  data/card_1.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  data/card_10.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  data/card_11.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  data/card_12.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  data/card_13.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  data/card_14.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  data/card_15.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  data/card_16.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  data/card_17.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  data/card_18.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  data/card_19.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  data/card_2.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  data/card_20.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  data/card_21.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  data/card_22.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  data/card_23.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  data/card_24.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  data/card_25.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  data/card_26.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  data/card_27.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  data/card_28.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  data/card_29.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  data/card_3.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  data/card_30.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  data/card_31.png
Diff not rendered
BIN  data/card_32.png
Diff not rendered
BIN  data/card_33.png
Diff not rendered
BIN  data/card_34.png
Diff not rendered
BIN  data/card_35.png
Diff not rendered
BIN  data/card_36.png
Diff not rendered
BIN  data/card_37.png
Diff not rendered
BIN  data/card_38.png
Diff not rendered
BIN  data/card_39.png
Diff not rendered
BIN  data/card_4.png
Diff not rendered
BIN  data/card_40.png
Diff not rendered
BIN  data/card_41.png
Diff not rendered
BIN  data/card_42.png
Diff not rendered
BIN  data/card_43.png
Diff not rendered
BIN  data/card_44.png
Diff not rendered
BIN  data/card_45.png
Diff not rendered
BIN  data/card_46.png
Diff not rendered
BIN  data/card_47.png
Diff not rendered
BIN  data/card_48.png
Diff not rendered
BIN  data/card_49.png
Diff not rendered
BIN  data/card_5.png
Diff not rendered
BIN  data/card_50.png
Diff not rendered
BIN  data/card_51.png
Diff not rendered
BIN  data/card_6.png
Diff not rendered
BIN  data/card_7.png
Diff not rendered
BIN  data/card_8.png
Diff not rendered
BIN  data/card_9.png
Diff not rendered
BIN  data/card_back.png
Diff not rendered
BIN  data/empty_stack.png
Diff not rendered
BIN  data/empty_target_0.png
Diff not rendered
BIN  data/empty_target_1.png
Diff not rendered
BIN  data/empty_target_2.png
Diff not rendered
BIN  data/empty_target_3.png
Diff not rendered
279 lib/SDLx/LayerManager.pm
@@ -0,0 +1,279 @@
+package SDLx::LayerManager;
+use strict;
+use warnings;
+use SDL;
+use SDLx::Surface;
+use SDLx::Sprite;
+use overload ( '@{}' => '_array', );
+
+my @layers = ();
+my @attached_layers = ();
+my @attached_distance = ();
+my @attached_position = ();
+
+sub new {
+ my $class = shift;
+ #my $options = shift;
+ my $self = {};
+ bless( $self, $class );
+
+ #$self->add(@_, $options);
+
+ return $self;
+}
+
+sub add {
+ my $self = shift;
+ my $options = pop;
+ my @layer = @_;
+
+ push @layers, {layer => $_, options => $options} for @layer;
+ return $self;
+}
+
+sub set {
+ my $self = shift;
+ my $index = shift;
+ my $layer = shift;
+ my $options = shift;
+
+ $layers[$index] = {layer => $layer, options => $options};
+ return $self;
+}
+
+sub length {
+ my $self = shift;
+
+ return scalar @layers;
+}
+
+sub _array {
+ my $self = shift;
+ return \@layers;
+}
+
+sub blit {
+ my $self = shift;
+ my $dest = shift;
+ $self->{dest} = $dest;
+
+ my ( $mask, $x, $y ) = @{ SDL::Events::get_mouse_state() };
+ my $layer_index = 0;
+ foreach (@layers) {
+ $_->{layer}->draw($dest) unless join( ',', @attached_layers ) =~ m/\b\Q$layer_index\E\b/;
+ $layer_index++;
+ }
+ foreach (@attached_layers) {
+ $layers[$_]->{layer}->draw_xy($dest, $x + @{$attached_distance[$_]}[0], $y + @{$attached_distance[$_]}[1]);
+ }
+}
+
+sub attach {
+ my $self = shift;
+ #my $dest = shift;
+ my $y = pop;
+ my $x = pop;
+ @attached_distance = ();
+ @attached_position = ();
+ @attached_layers = @_;
+ foreach (@layers) {
+ push( @attached_distance, [$_->{layer}->x - $x, $_->{layer}->y - $y] );
+ push( @attached_position, [$_->{layer}->x, $_->{layer}->y] );
+ }
+}
+
+sub detach {
+ my $self = shift;
+ @attached_distance = ();
+ @attached_position = ();
+ @attached_layers = ();
+}
+
+sub detach_back {
+ my $self = shift;
+ foreach (@attached_layers) {
+ $layers[$_]->{layer}->x(@{$attached_position[$_]}[0]);
+ $layers[$_]->{layer}->y(@{$attached_position[$_]}[1]);
+ }
+ @attached_distance = ();
+ @attached_position = ();
+ @attached_layers = ();
+}
+
+sub detach_xy {
+ my $self = shift;
+ my $x = shift;
+ my $y = shift;
+ my $offset_x;
+ my $offset_y;
+ foreach (@attached_layers) {
+ $offset_x = @{$attached_position[$_]}[0] - $x unless defined $offset_x;
+ $offset_y = @{$attached_position[$_]}[1] - $y unless defined $offset_y;
+
+ $layers[$_]->{layer}->x(@{$attached_position[$_]}[0] - $offset_x);
+ $layers[$_]->{layer}->y(@{$attached_position[$_]}[1] - $offset_y);
+ }
+ my @position_before = @{$attached_position[$attached_layers[0]]};
+
+ @attached_distance = ();
+ @attached_position = ();
+ @attached_layers = ();
+
+ return @position_before;
+}
+
+sub foreground {
+ my $self = shift;
+ my @layers_ = @_;
+ @attached_layers = ();
+
+ for(@layers_) {
+ push(@layers, $layers[$_]);
+ push(@attached_distance, $attached_distance[$_]);
+ push(@attached_position, $attached_position[$_]);
+ }
+
+ for(sort { $b <=> $a } @layers_) {
+ splice(@layers, $_, 1);
+ splice(@attached_distance, $_, 1);
+ splice(@attached_position, $_, 1);
+ }
+
+ for(my $i = $#layers - $#layers_; $layers[$i]; $i++) {
+ push(@attached_layers, $i);
+ }
+
+ return @attached_layers;
+}
+
+sub get_layer_by_position {
+ my $self = shift;
+ my ( $x, $y ) = @_;
+
+ for ( my $i = $#layers ; $i > 0 ; $i-- ) {
+ if ( $layers[$i]->{layer}->x <= $x
+ && $x <= $layers[$i]->{layer}->x + $layers[$i]->{layer}->clip()->w
+ && $layers[$i]->{layer}->y <= $y
+ && $y <= $layers[$i]->{layer}->y + $layers[$i]->{layer}->clip()->h )
+ {
+ my $pixel =
+ $layers[$i]->{layer}->[ $x - $layers[$i]->{layer}->x ][ $y - $layers[$i]->{layer}->y ];
+ my ( $r, $g, $b, $a ) =
+ @{ SDL::Video::get_RGBA( $layers[$i]->{layer}->surface->format, $pixel ) };
+
+ return $i if $a > 0;
+ }
+ }
+
+ return -1;
+}
+
+sub get_layers_ahead_layer {
+ my $self = shift;
+ my $index = shift;
+ #my $map = SDLx::Surface->new( surface => $self->{dest} );
+ my @matches = ();
+ #my @intersections = ();
+ my $layer_index = 0;
+
+ for (@layers) {
+ if (
+ $layer_index > $index
+
+ && (
+
+ # upper left point inside layer
+ ( $layers[$index]->{layer}->x <= $_->{layer}->x
+ && $_->{layer}->x <= $layers[$index]->{layer}->x + $layers[$index]->{layer}->clip->w
+ && $layers[$index]->{layer}->y <= $_->{layer}->y
+ && $_->{layer}->y <= $layers[$index]->{layer}->y + $layers[$index]->{layer}->clip->h
+ )
+
+ # upper right point inside layer
+ || ( $layers[$index]->{layer}->x <= $_->{layer}->x + $_->{layer}->clip->w
+ && $_->{layer}->x + $_->{layer}->clip->w <=
+ $layers[$index]->{layer}->x + $layers[$index]->{layer}->clip->w
+ && $layers[$index]->{layer}->y <= $_->{layer}->y
+ && $_->{layer}->y <=
+ $layers[$index]->{layer}->y + $layers[$index]->{layer}->clip->h )
+
+ # lower left point inside layer
+ || ( $layers[$index]->{layer}->x <= $_->{layer}->x
+ && $_->{layer}->x <= $layers[$index]->{layer}->x + $layers[$index]->{layer}->clip->w
+ && $layers[$index]->{layer}->y <= $_->{layer}->y + $_->{layer}->clip->h
+ && $_->{layer}->y + $_->{layer}->clip->h <=
+ $layers[$index]->{layer}->y + $layers[$index]->{layer}->clip->h)
+
+ # lower right point inside layer
+ || ( $layers[$index]->{layer}->x <= $_->{layer}->x + $_->{layer}->clip->w
+ && $_->{layer}->x + $_->{layer}->clip->w <=
+ $layers[$index]->{layer}->x + $layers[$index]->{layer}->clip->w
+ && $layers[$index]->{layer}->y <= $_->{layer}->y + $_->{layer}->clip->h
+ && $_->{layer}->y + $_->{layer}->clip->h <=
+ $layers[$index]->{layer}->y + $layers[$index]->{layer}->clip->h )
+ )
+ )
+ {
+ push( @matches, $layer_index ); # TODO checking transparency
+ }
+ $layer_index++;
+ }
+
+ return @matches;
+}
+
+sub get_layers_behind_layer {
+ my $self = shift;
+ my $index = shift;
+ #my $map = SDLx::Surface->new( surface => $self->{dest} );
+ my @matches = ();
+ #my @intersections = ();
+ my $layer_index = $#layers;
+
+ for (reverse @layers) {
+ if (
+ $layer_index < $index
+
+ && (
+
+ # upper left point inside layer
+ ( $layers[$index]->{layer}->x <= $_->{layer}->x
+ && $_->{layer}->x <= $layers[$index]->{layer}->x + $layers[$index]->{layer}->clip->w
+ && $layers[$index]->{layer}->y <= $_->{layer}->y
+ && $_->{layer}->y <= $layers[$index]->{layer}->y + $layers[$index]->{layer}->clip->h
+ )
+
+ # upper right point inside layer
+ || ( $layers[$index]->{layer}->x <= $_->{layer}->x + $_->{layer}->clip->w
+ && $_->{layer}->x + $_->{layer}->clip->w <=
+ $layers[$index]->{layer}->x + $layers[$index]->{layer}->clip->w
+ && $layers[$index]->{layer}->y <= $_->{layer}->y
+ && $_->{layer}->y <=
+ $layers[$index]->{layer}->y + $layers[$index]->{layer}->clip->h )
+
+ # lower left point inside layer
+ || ( $layers[$index]->{layer}->x <= $_->{layer}->x
+ && $_->{layer}->x <= $layers[$index]->{layer}->x + $layers[$index]->{layer}->clip->w
+ && $layers[$index]->{layer}->y <= $_->{layer}->y + $_->{layer}->clip->h
+ && $_->{layer}->y + $_->{layer}->clip->h <=
+ $layers[$index]->{layer}->y + $layers[$index]->{layer}->clip->h)
+
+ # lower right point inside layer
+ || ( $layers[$index]->{layer}->x <= $_->{layer}->x + $_->{layer}->clip->w
+ && $_->{layer}->x + $_->{layer}->clip->w <=
+ $layers[$index]->{layer}->x + $layers[$index]->{layer}->clip->w
+ && $layers[$index]->{layer}->y <= $_->{layer}->y + $_->{layer}->clip->h
+ && $_->{layer}->y + $_->{layer}->clip->h <=
+ $layers[$index]->{layer}->y + $layers[$index]->{layer}->clip->h )
+ )
+ )
+ {
+ push( @matches, $layer_index ); # TODO checking transparency
+ }
+ $layer_index--;
+ }
+
+ return @matches;
+}
+
+1;
307 solitaire.pl
@@ -0,0 +1,307 @@
+#!/usr/bin/perl
+
+=pod TODO
+
+( ) Karten ablegen automatisch
+( ) Platzhalter neben Stapel
+( ) LayerManager: indirekte überdeckende Karten
+
+=cut
+
+
+
+package Games::Solitaire;
+
+use strict;
+use warnings;
+use Time::HiRes;
+
+use SDL;
+#use SDL::Color;
+use SDL::Event;
+use SDL::Events;
+#use SDL::GFX::Primitives;
+#use SDL::GFX::Rotozoom;
+#use SDL::Image;
+use SDL::Rect;
+use SDL::Surface;
+#use SDL::TTF;
+#use SDL::TTF::Font;
+use SDL::Video;
+
+#use SDLx::FPS;
+use SDLx::SFont;
+use SDLx::Surface;
+use SDLx::Sprite;
+
+use lib 'lib';
+use SDLx::LayerManager;
+
+SDL::init(SDL_INIT_VIDEO);
+my $display = SDL::Video::set_video_mode(800, 600, 32, SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_HWACCEL);
+my $layers = SDLx::LayerManager->new();
+my $event = SDL::Event->new();
+my $loop = 1;
+my $usec = Time::HiRes::time;
+my $last_click = Time::HiRes::time;
+#my @rects = ();
+#my $fps = SDLx::FPS->new(fps => 30);
+my @selected_cards = ();
+my $left_mouse_down = 0;
+
+#SDL::TTF::init();
+#my $sfont = SDLx::SFont->new('data/font.png');
+
+#my $font = SDL::TTF::Font->new('data/gnuolane free.ttf', 20);
+#my $font_rect = SDL::Rect->new(10, 10, 70, 30);
+
+init_background();
+init_cards();
+$layers->blit($display);
+game();
+
+sub draw {
+ my $usec_ = Time::HiRes::time;
+ if($usec_ > $usec + 5/60) {
+ $layers->blit($display);
+ SDL::Video::flip($display);
+ $usec = $usec_;
+ }
+}
+
+sub event_loop
+{
+ my $handler = shift;
+
+ SDL::Events::pump_events();
+ while(SDL::Events::poll_event($event))
+ {
+ $left_mouse_down = 1 if $event->type == SDL_MOUSEBUTTONDOWN && $event->button_button == SDL_BUTTON_LEFT;
+ $left_mouse_down = 0 if $event->type == SDL_MOUSEBUTTONUP && $event->button_button == SDL_BUTTON_LEFT;
+
+ $handler->{on_quit}->() if defined $handler->{on_quit} && ($event->type == SDL_QUIT || ($event->type == SDL_KEYDOWN && $event->key_sym == SDLK_ESCAPE));
+ $handler->{on_keydown}->() if defined $handler->{on_keydown} && $event->type == SDL_KEYDOWN;
+ $handler->{on_mousemove}->() if defined $handler->{on_mousemove} && $event->type == SDL_MOUSEMOTION && !$left_mouse_down;
+ $handler->{on_drag}->() if defined $handler->{on_drag} && $event->type == SDL_MOUSEMOTION && $left_mouse_down;
+ $handler->{on_drop}->() if defined $handler->{on_drop} && $event->type == SDL_MOUSEBUTTONUP;
+ $handler->{on_click}->() if defined $handler->{on_click} && $event->type == SDL_MOUSEBUTTONDOWN && Time::HiRes::time - $last_click >= 0.3;
+ $handler->{on_dblclick}->() if defined $handler->{on_dblclick} && $event->type == SDL_MOUSEBUTTONDOWN && Time::HiRes::time - $last_click < 0.3;
+
+ $last_click = Time::HiRes::time if $event->type == SDL_MOUSEBUTTONDOWN;
+ draw();
+ }
+}
+
+sub menu
+{
+ my $handler =
+ {
+ on_quit => sub {
+ $loop = 0;
+ },
+ };
+
+ event_loop($handler);
+}
+
+sub game
+{
+ my @selected_cards = ();
+ my $x = 0;
+ my $y = 0;
+ my $handler =
+ {
+ on_quit => sub {
+ $loop = 0;
+ },
+ on_drag => sub {
+ },
+ on_drop => sub {
+ if(scalar @selected_cards) {
+ @selected_cards = $layers->foreground(@selected_cards);
+
+ my @stack = $layers->get_layers_behind_layer($selected_cards[0]);
+
+ my $dropped = 0;
+ my @position_before = ();
+
+ if(scalar @stack) {
+ # auf leeres Feld
+ if($layers->[$stack[0]]->{options}->{id} =~ m/empty_stack/
+ && can_drop($layers->[$selected_cards[0]]->{options}->{id}, $layers->[$stack[0]]->{options}->{id})) {
+ @position_before = $layers->detach_xy($layers->[$stack[0]]->{layer}->x, $layers->[$stack[0]]->{layer}->y);
+ $dropped = 1;
+ }
+
+ # auf offene Karte
+ elsif($layers->[$stack[0]]->{options}->{visible}
+ && can_drop($layers->[$selected_cards[0]]->{options}->{id}, $layers->[$stack[0]]->{options}->{id})) {
+ @position_before = $layers->detach_xy($layers->[$stack[0]]->{layer}->x, $layers->[$stack[0]]->{layer}->y + 20);
+ $dropped = 1;
+ }
+
+ if($dropped) {
+ $position_before[0] += 20; # transparenter Rand
+ $position_before[1] += 20;
+ show_card(@position_before);
+ }
+ }
+
+ $layers->detach_back unless $dropped;
+ }
+ @selected_cards = ();
+ },
+ on_click => sub {
+ unless(scalar @selected_cards) {
+ my $layer = $layers->get_layer_by_position($event->button_x, $event->button_y);
+
+ if($layer > 0 && $layers->[$layer]->{options}->{id} =~ m/\d+/
+ && $layers->[$layer]->{options}->{visible}) {
+ @selected_cards = ($layer, $layers->get_layers_ahead_layer($layer));
+ $layers->attach(@selected_cards, $event->button_x, $event->button_y);
+ }
+ }
+ },
+ on_dblclick => sub {
+ $last_click = 0;
+ $layers->detach_back;
+ my $layer = $layers->get_layer_by_position($event->button_x, $event->button_y);
+ if($layer > 0 && $layers->[$layer]->{options}->{id} =~ m/\d+/
+ && $layers->[$layer]->{options}->{visible}) {
+ my $target = $layers->get_layer_by_position(370 + 110 * int($layers->[$layer]->{options}->{id} / 13), 40);
+
+ if(can_drop($layers->[$layer]->{options}->{id}, $layers->[$target]->{options}->{id})) {
+ $layers->attach($layer, $event->button_x, $event->button_y);
+ $layers->foreground($layer);
+ $layers->detach_xy($layers->[$target]->{layer}->x, $layers->[$target]->{layer}->y);
+ show_card($event->button_x, $event->button_y);
+ }
+ }
+ },
+ };
+
+ while($loop) {
+ event_loop($handler);
+ draw();
+ #$fps->delay;
+ }
+}
+
+sub can_drop {
+ my $card = shift;
+ my $card_color = int($card / 13);
+ my $target = shift;
+ my $stack = $layers->get_layer_by_position(370 + 110 * $card_color, 40);
+
+ printf("%s => %s\n", $card, $target);
+ #my @stack = $layers->get_layers_behind_layer($stack);
+ #my @stack = $layers->get_layers_ahead_layer($stack);
+
+ # Könige dürfen auf leeres Feld
+ if('12,25,38,51' =~ m/\b\Q$card\E\b/) {
+ return $target =~ m/empty_stack/ ? 1 : 0;
+ }
+
+ # Asse dürfen auf leeres Feld rechts oben
+ if('0,13,26,39' =~ m/\b\Q$card\E\b/ && $target =~ m/empty_target_\Q$card_color\E/) {
+ return 1;
+ }
+
+ printf("%s == %s\n", $target, $layers->[$stack]->{options}->{id});
+ if($card =~ m/^\d+$/ && $target =~ m/^\d+$/
+ && $card == $target + 1
+ && $target == $layers->[$stack]->{options}->{id}) {
+ return 1;
+ }
+
+ printf("%s => %s\n", $card, $target);
+
+ return 1 if($card =~ m/^\d+$/ && $target =~ m/^\d+$/
+ && ($card + 14 == $target || $card + 40 == $target
+ || $card - 12 == $target || $card - 38 == $target)
+ );
+
+ return 0;
+}
+
+sub show_card {
+ my @position = @_;
+ my $layer = $layers->get_layer_by_position(@position);
+
+ if($layer > 0 && $layers->[$layer]->{options}->{id} =~ m/\d+/
+ && -e 'data/card_' . $layers->[$layer]->{options}->{id} . '.png') {
+ $layers->set(
+ $layer,
+ SDLx::Sprite->new(
+ image => 'data/card_' . $layers->[$layer]->{options}->{id} . '.png',
+ x => $layers->[$layer]->{layer}->x,
+ y => $layers->[$layer]->{layer}->y),
+ {id => $layers->[$layer]->{options}->{id}, visible => 1}
+ );
+ }
+}
+
+sub init_background {
+ my $background = SDLx::Sprite->new(image => 'res/0002.png');
+ $layers->add($background, {id => 'background'});
+ for(0..6) {
+ my $empty_stack = SDLx::Sprite->new(image => 'data/empty_stack.png');
+ $empty_stack->y( 200 );
+ $empty_stack->x( 20 + 110 * $_ );
+ $layers->add($empty_stack, {id => 'empty_stack'});
+ }
+
+ for(0..3) {
+ my $empty_stack = SDLx::Sprite->new(image => 'data/empty_target_' . $_ . '.png');
+ $empty_stack->y( 20 );
+ $empty_stack->x( 350 + 110 * $_ );
+ $layers->add($empty_stack, {id => 'empty_target_' . $_});
+ }
+}
+
+sub init_cards {
+ my $stack_index = 0;
+ my $stack_position = 0;
+ my @card_value = fisher_yates_shuffle([0..51]);
+ for(0..51)
+ {
+ my $card = SDLx::Sprite->new(image => 'data/card_back.png');
+ my $visible = 0;
+
+ if($_ < 28)
+ {
+ if($stack_position > $stack_index)
+ {
+ $stack_index++;
+ $stack_position = 0;
+ }
+ if($stack_position == $stack_index)
+ {
+ $card = SDLx::Sprite->new(image => 'data/card_' . $card_value[$_] . '.png');
+ $visible = 1;
+ }
+ $card->x( 20 + 110 * $stack_index );
+ $card->y( 200 + 20 * $stack_position);
+ $stack_position++;
+ }
+ else
+ {
+ $stack_index = 7;
+ $card->x( 20 );
+ $card->y( 20 );
+ }
+ $layers->add($card, {id => $card_value[$_], visible => $visible});
+ }
+}
+
+sub fisher_yates_shuffle
+{
+ my $array = shift;
+ my $i;
+ for ($i = @$array; --$i; ) {
+ my $j = int rand ($i+1);
+ next if $i == $j;
+ @$array[$i,$j] = @$array[$j,$i];
+ }
+ return @$array;
+}
Please sign in to comment.
Something went wrong with that request. Please try again.