From e546d6cb287c975ef4d6663683de1d1c54e8f1df Mon Sep 17 00:00:00 2001 From: Ryan Shepherd Date: Mon, 24 Sep 2018 12:49:16 -0400 Subject: [PATCH] Initial sayer support --- PerlRPG/Drawing.pm | 20 +++++++++++++++----- PerlRPG/Script.pm | 36 ++++++++++++++++++++++++++++++++++++ Resources/script.gs | 7 ++++++- issues.txt | 2 ++ 4 files changed, 59 insertions(+), 6 deletions(-) create mode 100644 issues.txt diff --git a/PerlRPG/Drawing.pm b/PerlRPG/Drawing.pm index 6a7d524..01ff522 100644 --- a/PerlRPG/Drawing.pm +++ b/PerlRPG/Drawing.pm @@ -9,7 +9,6 @@ use SDLx::Sprite::Animated; use PerlRPG::Console; use PerlRPG::Game; use PerlRPG::Assets; -use Storable qw/dclone/; use vars qw/@ISA @EXPORT @EXPORT_OK %VisibleSprites/; @ISA = qw/Exporter/; @@ -20,7 +19,7 @@ use vars qw/@ISA @EXPORT @EXPORT_OK %VisibleSprites/; *GetOption = \&PerlRPG::Game::GetOption; sub ShowSprite { - my($name, $file, $x, $y, $d, %opt)=@_; + my($name, $file, $x, $y, $d, @opt)=@_; LogData(DEBUG, "Showing sprite $file as $name"); my $t = GetAssetType($file); @@ -38,9 +37,20 @@ sub ShowSprite { $a->start(); } - foreach my $o (keys %opt) { - if($o eq 'type') { - $a->type($opt{$o}); + foreach my $o (@opt) { + my($name, $val)=$o=~/^\s*(\S+?)=(\S*)/; + $name = $o unless($name); + if($o eq 'Reverse' || $o eq 'Circular') { + $a->type($o); + } elsif($name eq 'max_loops') { + $a->max_loops($val); + } elsif($o eq 'flip') { + # FIXME - This doesn't work + my $s=$a->surface(); + $s->flip(); + $a->surface($s); + } else { + LogData(WARN, "Unknown option in ShowSprite '$o' - '$name'='$val'"); } } diff --git a/PerlRPG/Script.pm b/PerlRPG/Script.pm index 7c19df7..c78fe74 100644 --- a/PerlRPG/Script.pm +++ b/PerlRPG/Script.pm @@ -21,6 +21,7 @@ my $current_line; my %script_commands = ( 'Wait' => {'sub' => sub {}, 'wait' => 1}, + 'AddSayer' => {'sub' => \&AddSayer, 'wait' => 0}, 'Jump' => {'sub' => \&PerlRPG::Script::RunScript, 'wait' => 0}, @@ -34,6 +35,9 @@ my %script_commands = ( 'SetAssetOption' => {'sub' => \&PerlRPG::Assets::SetAssetOption, 'wait' => 0}, ); +my %sayers = (); +my $lastsayer=undef; + # Load script files, locate labels sub CompileScripts { my @scripts = sort { $a cmp $b } LoadAssets('gs'); @@ -88,6 +92,9 @@ sub RunScriptTick { sub RunScriptLine { my($file, $linenum, $line)=@_; + # Copy to refer to + my $oline = $line; + # Remove comments $line=~s/#.+$//; # Remove comments $line=~s/^\s+//; # Remove leading whitespace @@ -103,9 +110,38 @@ sub RunScriptLine { if(exists $script_commands{$cmd}) { $script_commands{$cmd}{'sub'}->(@opts); return $script_commands{$cmd}{'wait'}; + } elsif(exists $sayers{$cmd}) { + my($text)=$oline=~/\Q$cmd\E\s+"(.+?)"\s*$/; + if($text) { + return SayLine($sayers{$cmd}, $text); + } else { + LogData(ERROR, "Invalid sayer text in script at $file:$linenum"); + return; + } } LogData(ERROR, "Unknown command '$cmd' in script at $file:$linenum"); return undef; } + +sub AddSayer { + my($name, $displayname, $sprite, @textcolor)=@_; + if(@textcolor < 4) { + $textcolor[3] = 255; + } + + $sayers{$name} = { + 'Name' => $name, + 'DisplayName' => $displayname, + 'Sprite' => $sprite, + 'TextColor' => \@textcolor + }; +} + +sub SayLine { + my($ref, $text)=@_; + LogData(INFO, "%s saying \"%s\"", $ref->{'DisplayName'}, $text); + return 1; +} + 1; diff --git a/Resources/script.gs b/Resources/script.gs index 87e38d4..3834231 100644 --- a/Resources/script.gs +++ b/Resources/script.gs @@ -2,7 +2,11 @@ __init__: SetBackground 128 128 128 255 SetAssetOption spritesheet.png Animated - Show aka spritesheet.png 0 0 1 type Reverse + Show aka spritesheet.png 0 0 1 flip + Show aka2 spritesheet.png 100 0 2 Reverse + Show aka3 spritesheet.png 200 0 3 max_loops=10 + + AddSayer h Hermione aka01.png #Show aka aka.vs 0 0 1 type Reverse #Show aka2 aka.vs 100 0 2 @@ -11,4 +15,5 @@ __init__: donothing: Wait + h "Some text here" Jump donothing diff --git a/issues.txt b/issues.txt new file mode 100644 index 0000000..bc15be3 --- /dev/null +++ b/issues.txt @@ -0,0 +1,2 @@ +ShowSprite with flip option doesn't work +.vs constructed animated sprites don't respect source image's alpha channel