Skip to content

Commit

Permalink
Pushing now for the heck of it. Slightly fundamental redesign, still …
Browse files Browse the repository at this point in the history
…not quite working yet
  • Loading branch information
Blaizer committed Sep 28, 2011
1 parent 206f656 commit 6e185fc
Show file tree
Hide file tree
Showing 56 changed files with 386 additions and 330 deletions.
4 changes: 2 additions & 2 deletions bin/nhc
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ BEGIN {
'help|?' => sub { require Pod::Usage; Pod::Usage::pod2usage(1) }, 'help|?' => sub { require Pod::Usage; Pod::Usage::pod2usage(1) },
'normal-window' => sub { $Games::Neverhood::Fullscreen = 0; $Games::Neverhood::NoFrame = 0 }, 'normal-window' => sub { $Games::Neverhood::Fullscreen = 0; $Games::Neverhood::NoFrame = 0 },
'share-dir=s' => \$Games::Neverhood::ShareDir, 'share-dir=s' => \$Games::Neverhood::ShareDir,
'start-name=s' => sub { ($Games::Neverhood::StartSetName, $Games::Neverhood::StartUnsetName) = split /,/, $_[0] }, 'start-name=s' => sub { ($Games::Neverhood::StartSetName, $Games::Neverhood::StartUnsetName) = split /,/, $_[1] },
'window' => sub { $Games::Neverhood::Fullscreen = 0; }, 'window' => sub { $Games::Neverhood::Fullscreen = 0; },
) or require Pod::Usage, Pod::Usage::pod2usage(2); ) or require Pod::Usage, Pod::Usage::pod2usage(2);
} }


use Games::Neverhood; use Games::Neverhood;
$_->app->run; $;->app->run;


1; 1;


Expand Down
32 changes: 17 additions & 15 deletions lib/Games/Neverhood.pm
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ sub app {
title => 'The Neverhood', title => 'The Neverhood',
width => 640, width => 640,
height => 480, height => 480,
depth => 8, depth => 16,
min_t => $FPSLimit && 1 / $FPSLimit, min_t => $FPSLimit && 1 / $FPSLimit,
init => ['video', 'audio'], init => ['video', 'audio'],
no_cursor => 1, no_cursor => 1,
Expand Down Expand Up @@ -141,27 +141,29 @@ sub app {
}, },
$event_window_pause, $event_window_pause,
$event_pause, $event_pause,
sub{$_->event(@_)}, sub{$;->event(@_)},
], ],
move_handlers => [ move_handlers => [
sub{$_->move(@_)} sub{$;->move(@_)}
], ],
show_handlers => [ show_handlers => [
sub{$_->show(@_)}, sub{SDL::Video::fill_rect($_[1], SDL::Rect->new(0, 0, 640, 480), 0)},
sub{$;->show(@_)},
sub{$_[1]->flip}, sub{$_[1]->flip},
sub { sub {
my (undef, $app) = @_; my (undef, $app) = @_;
return unless defined(my $set_name = $_->set); return unless defined(my $set_name = $;->set);
$_->set(undef); $;->set(undef);
my $unset_name = "$_"; my $unset_name = "$;";


$_->on_destroy($set_name); $;->on_destroy($set_name);
undef $_; $;->cursor->clicked(undef);
undef $;;


# inside this, $_ gets set to the new object # inside this, $; gets set to the new object
"Games::Neverhood::$set_name"->new($unset_name); "Games::Neverhood::$set_name"->new($unset_name);


$app->dt(1 / $_->fps); $app->dt(1 / $;->fps);
}, },
], ],
); );
Expand All @@ -179,19 +181,19 @@ sub app {
sub new { sub new {
# TODO: what else should go here? Is this redundant? # TODO: what else should go here? Is this redundant?
my ($class) = @_; my ($class) = @_;
$_ = bless {}, ref $class || $class; $; = bless {}, ref $class || $class;
} }


sub set { sub set {
if(@_ > 1) { $_[0]->{set} = $_[1]; return $_[0]; } if(@_ > 1) { $_[0]->{set} = $_[1]; return $_[0]; }
$_[0]->{set}; $_[0]->{set};
} }


# inside this, $_ gets set to the new object # inside this, $; gets set to the new object
"Games::Neverhood::$StartSetName"->new($StartUnsetName); "Games::Neverhood::$StartSetName"->new($StartUnsetName);
$_->app->dt(1 / $_->fps); $;->app->dt(1 / $;->fps);


# you're NOT allowed to use $_ to refer to the current app if you're a game class, okay? PROMISE? # you're NOT allowed to use $; to refer to the current app if you're a game class, okay? PROMISE?
# no seriously, I mean it... # no seriously, I mean it...
# we're going on the assumption that if you're a sprite or something, you're only being asked to do something because you're in the current app # we're going on the assumption that if you're a sprite or something, you're only being asked to do something because you're in the current app
# no two apps should ever access one another # no two apps should ever access one another
Expand Down
175 changes: 72 additions & 103 deletions lib/Games/Neverhood/Scene.pm
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use parent
'Games::Neverhood', 'Games::Neverhood',
'Exporter', 'Exporter',
; ;

# The user entered text for the "cheat" system # The user entered text for the "cheat" system
our $Cheat = ''; our $Cheat = '';


Expand All @@ -27,37 +26,35 @@ use Games::Neverhood qw/$Remainder $Debug/;
use Games::Neverhood::Sprite; use Games::Neverhood::Sprite;
use Games::Neverhood::OrderedHash; use Games::Neverhood::OrderedHash;


# Overloadable Methods: # Overloadable Methods and what they should return:


# sub on_new # sub on_new
# sub on_destroy # sub on_destroy


# use constant # use constant
# vars # vars {}
# sprites # sprites Games::Neverhood::OrderedHash->new
# frame # frame 0
# sprites_list # sprites_list []
# all_dir # fps 0
# fps # cursor_type ""
# cursor_type # music 0
# klaymen_move_bounds # rect
# music

# event
# sub event # sub on_space
# sub move # sub on_out
# sub show # sub on_left

# sub on_right
# sub on_move # sub on_up
# sub on_show # sub on_down
# sub on_space # move
# sub on_click # sub on_click
# sub on_out # sub on_move
# sub on_left # show
# sub on_right # sub on_show
# sub on_up
# sub on_down # don't overload this, use on_new, sprites_list and vars

# don't overload this, use on_new and vars
sub new { sub new {
my ($self, $unset_name) = @_; my ($self, $unset_name) = @_;
$self = $self->SUPER::new; $self = $self->SUPER::new;
Expand All @@ -70,9 +67,13 @@ sub new {
$name = $sprite->name or Carp::confess("All sprites must have a (unique) name"); $name = $sprite->name or Carp::confess("All sprites must have a (unique) name");
} }
else { else {
no strict 'refs';
$name = $sprite; $name = $sprite;
my $sprite_class = ref($self) .'::'. $name; my $sprite_class = ref($self) .'::'. $name;
my $sprite_isa = $sprite_class . '::ISA';
{
no strict 'refs';
@$sprite_isa = 'Games::Neverhood::Sprite' unless @$sprite_isa;
}
$sprite = $sprite_class->new; $sprite = $sprite_class->new;
$sprite->{name} = $name; $sprite->{name} = $name;
} }
Expand All @@ -90,7 +91,7 @@ sub new {
} }
else { else {
# gotta still call that on_move from within frame # gotta still call that on_move from within frame
$sprite->frame(0); $sprite->frame($sprite->frame // 0);
} }
} }
$self; $self;
Expand Down Expand Up @@ -122,23 +123,13 @@ use constant {
sprites_list => [], sprites_list => [],
fps => 24, fps => 24,
cursor_type => 'click', cursor_type => 'click',
move_klaymen_bounds => undef,
music => undef, music => undef,
rect => undef,
}; };


############################################################################### ###############################################################################
# handler subs # handler subs


sub on_move {}
sub on_show {}
sub on_space {}
sub on_click { 'no' }
sub on_out {}
sub on_left {}
sub on_right {}
sub on_up {}
sub on_down {}

sub event { sub event {
my ($self, $e) = @_; my ($self, $e) = @_;
if($e->type == SDL_MOUSEMOTION) { if($e->type == SDL_MOUSEMOTION) {
Expand All @@ -150,18 +141,21 @@ sub event {
if($self->cursor->sequence eq 'click') { if($self->cursor->sequence eq 'click') {
$self->cursor->clicked($pos); $self->cursor->clicked($pos);
} }
elsif($self->cursor_type eq 'out') {
$self->cursor->clicked(undef);
$self->on_out;
}
else { else {
my $method = "on_" . $self->cursor->sequence; my $method = "on_" . $self->cursor->sequence;
$self->$method; $self->$method;
$self->cursor->clicked(undef);
} }
} }
elsif($e->type == SDL_KEYDOWN) { elsif($e->type == SDL_KEYDOWN) {
return if $e->key_mod & (KMOD_ALT | KMOD_CTRL | KMOD_SHIFT | KMOD_META); return if $e->key_mod & (KMOD_ALT | KMOD_CTRL | KMOD_SHIFT | KMOD_META);
my $name = SDL::Events::get_key_name($e->key_sym); my $name = SDL::Events::get_key_name($e->key_sym);
given($name) { given($name) {
when('escape') { when('escape') {
# $self->set('Menu'); $self->set('Menu');
} }
when('space') { when('space') {
$self->on_space; $self->on_space;
Expand All @@ -173,11 +167,11 @@ sub event {
when('return') { when('return') {
if($Cheat eq 'fastforward') { if($Cheat eq 'fastforward') {
$FastForward = !$FastForward; $FastForward = !$FastForward;
$self->dt(1 / ($self->fps * 3)); $self->app->dt(1 / ($self->fps * 3));
} }
elsif($Cheat eq 'screensnapshot') { elsif($Cheat eq 'screensnapshot') {
my $file = File::Spec->catfile('', 'NevShot.bmp'); my $file = File::Spec->catfile('', 'NevShot.bmp'); # TODO: this is wrong on stuff other than windows...
SDL::Video::save_BMP($self, $file) and warn "Error saving screenshot to $file: ", SDL::get_error; SDL::Video::save_BMP($self->app, $file) and warn "Error saving screenshot to $file: ", SDL::get_error;
} }
elsif($Cheat eq 'happybirthdayklaymen' and $self eq 'Scene::Nursery::One') { elsif($Cheat eq 'happybirthdayklaymen' and $self eq 'Scene::Nursery::One') {
$self->set('Scene::Nursery::Two'); $self->set('Scene::Nursery::Two');
Expand All @@ -199,80 +193,55 @@ sub event {
} }
} }
} }
sub on_space {}
sub on_out {}
sub on_left {}
sub on_right {}
sub on_forward {}
sub on_up {}
sub on_down {}


sub move { sub move {
my ($self, $step) = @_; my ($self, $step) = @_;
return unless $step; return unless $step;

&_move_click;
&_move_sprites;
# &_move_klaymen;
}

sub _move_click {
my ($self, $step) = @_;
my $click = $self->cursor->clicked;

my $return = $self->on_click // '';
if($return eq 'no_but_keep') {
return;
}
elsif($return ne 'no') {
$self->cursor->clicked(undef);
return;
}

for my $sprite (@{$self->sprites}) {
my $return = $self->on_click // '';
if($return eq 'no_but_keep') {
return;
}
elsif($return ne 'no') {
$self->cursor->clicked(undef);
return;
}
}
if($self->sprites->{klaymen} and !$self->klaymen->no_interrupt) {
my $bound;
if(
$bound = $self->move_klaymen_bounds and
$bound->[0] <= $click->[0] and $bound->[1] <= $click->[1] and
$bound->[2] >= $click->[0] and $bound->[3] >= $click->[1] and

!$self->klaymen->sprite eq 'idle' ||
($click->[0] < $self->klaymen->pos->[0] - 38 || $click->[0] > $self->klaymen->pos->[0] + 38)
) {
$self->klaymen->move_to(to => $click->[0]);
}
$self->cursor->clicked(undef);
return;
}
}

sub _move_sprites {
my ($self, $step) = @_;
$Remainder += $step; $Remainder += $step;
if($Remainder >= 1) { if($Remainder >= 1) {
$Remainder--; $Remainder--;


# on_move is called inside the frame method $self->on_click if $self->cursor->clicked;
$self->frame($self->frame + 1) if $self->frames;


for my $sprite (@{$self->sprites}, $self->cursor) { # on_move is called within the frame method
# on_move is called inside the frame method $self->frame($self->frame + 1);
$sprite->frame($sprite->frame + 1); $self->cursor->frame($self->cursor->frame + 1);
} }
}
sub on_click {
my ($self) = @_;
$self->cursor->clicked(undef);
}
sub on_move {
my ($self) = @_;
for my $sprite (@{$self->sprites}) {
# on_move is called within the frame method
$sprite->frame($sprite->frame + 1);
} }
} }


sub show { sub show {
my ($self, $time) = @_; my ($self, $time) = @_;

$self->on_show($time); $self->on_show($time);

$self->cursor->show;
for my $sprite (reverse @{$self->sprites}, $self->cursor) { }
$sprite->on_show; sub on_show {
my ($self) = @_;
for my $sprite (reverse @{$self->sprites}) {
$sprite->show;
} }
} }


###############################################################################
# other

BEGIN { *in_rect = \&Games::Neverhood::Sprite::in_rect }

1; 1;
Loading

0 comments on commit 6e185fc

Please sign in to comment.