Games-SolarConflict

 view release on metacpan or  search on metacpan

lib/Games/SolarConflict.pm  view on Meta::CPAN

package Games::SolarConflict;
{
  $Games::SolarConflict::VERSION = '0.000001';
}

# ABSTRACT: Spacewar! clone

use strict;
use warnings;
use Mouse;

use SDL 2.532;
use SDL::Rect;
use SDLx::App;
use SDLx::Surface;
use SDLx::Sprite;
use SDLx::Sprite::Animated;

use FindBin qw($Bin);
use Path::Class qw(dir);
use File::ShareDir qw(dist_dir);

use Games::SolarConflict::Sprite::Rotatable;
use Games::SolarConflict::Sun;
use Games::SolarConflict::Spaceship;
use Games::SolarConflict::Torpedo;
use Games::SolarConflict::HumanPlayer;
use Games::SolarConflict::ComputerPlayer;
use Games::SolarConflict::Controller::MainMenu;
use Games::SolarConflict::Controller::MainGame;
use Games::SolarConflict::Controller::GameOver;

has app => (
    is       => 'ro',
    isa      => 'SDLx::App',
    required => 1,
    handles  => [qw( run )],
);

has font => (
    is       => 'ro',
    isa      => 'Str',
    required => 1,
);

has background => (
    is       => 'ro',
    isa      => 'SDLx::Surface',
    required => 1,
);

has sun => (
    is       => 'ro',
    isa      => 'Games::SolarConflict::Sun',
    required => 1,
);

has spaceship1 => (
    is       => 'ro',
    isa      => 'Games::SolarConflict::Spaceship',
    required => 1,
);

has spaceship2 => (
    is       => 'ro',
    isa      => 'Games::SolarConflict::Spaceship',
    required => 1,
);

has _controllers => (
    is      => 'ro',
    isa     => 'HashRef',
    lazy    => 1,
    builder => '_build_controllers',
);

sub _build_controllers {
    my ($self) = @_;

    return {
        main_menu => sub {
            return Games::SolarConflict::Controller::MainMenu->new(@_);
        },
        main_game => sub {
            return Games::SolarConflict::Controller::MainGame->new(@_);
        },
        game_over => sub {
            return Games::SolarConflict::Controller::GameOver->new(@_);
        },
    };
}

around BUILDARGS => sub {
    my ( $orig, $class, %args ) = @_;

    my $app = SDLx::App->new(
        w     => 1024,
        h     => 768,
        title => 'SolarConflict',
        eoq   => 1,
    );

    my $assets;
    my $root = dir( $Bin, '..' );
    if ( -f $root->file('dist.ini') ) {
        $assets = $root->subdir('share');
    }
    else {
        $assets = dir( dist_dir('Games-SolarConflict') );
    }

    my %file = (
        background => $assets->file('background.png'),
        sun        => $assets->file('sun.png'),
        spaceship1 => $assets->file('spaceship1.png'),
        spaceship2 => $assets->file('spaceship2.png'),
        explosion  => $assets->file('explosion.png'),
        font       => $assets->file('UbuntuMono-BI.ttf'),
    );

    my %view = (
        background => SDLx::Surface->load( $file{background} ),
        sun        => SDLx::Sprite->new( image => $file{sun} ),
        spaceship1 => Games::SolarConflict::Sprite::Rotatable->new(
            sprite => SDLx::Sprite::Animated->new(
                rect  => SDL::Rect->new( 0, 0, 32, 32 ),
                image => $file{spaceship1},
            ),
        ),
        spaceship2 => Games::SolarConflict::Sprite::Rotatable->new(
            sprite => SDLx::Sprite::Animated->new(
                rect  => SDL::Rect->new( 0, 0, 32, 32 ),
                image => $file{spaceship2},
            ),
        ),
        explosion => SDLx::Sprite::Animated->new(
            rect            => SDL::Rect->new( 0, 0, 32, 32 ),
            image           => $file{explosion},
            max_loops       => 1,
            ticks_per_frame => 2,
        ),
    );



( run in 2.709 seconds using v1.01-cache-2.11-cpan-5837b0d9d2c )