Initial script variable support

This commit is contained in:
Ryan Shepherd 2018-09-25 13:52:47 -04:00
parent b3b7109bab
commit 17337d547d
3 changed files with 72 additions and 7 deletions

View File

@ -46,7 +46,7 @@ sub KeyPressed {
my($key, $updown, $mods)=@_; my($key, $updown, $mods)=@_;
my @mods = get_keymod($mods); my @mods = get_keymod($mods);
LogData(DEBUG, "Key %s(%s)", $updown, join(',',@mods, $key)); LogData(DEVALL, "Key %s(%s)", $updown, join(',',@mods, $key));
KeyPress($key, "$key-$updown"); KeyPress($key, "$key-$updown");
} }
@ -54,5 +54,5 @@ sub MouseClick {
my($x, $y, $btn, $updown, $mods)=@_; my($x, $y, $btn, $updown, $mods)=@_;
my @mods = get_keymod($mods); my @mods = get_keymod($mods);
LogData(DEBUG, "MouseClick %s %i,%i (%s)", $updown, $x, $y, join(',',@mods, $btn)); LogData(DEVALL, "MouseClick %s %i,%i (%s)", $updown, $x, $y, join(',',@mods, $btn));
} }

View File

@ -13,7 +13,7 @@ use PerlRPG::Drawing;
use vars qw/@ISA @EXPORT @EXPORT_OK/; use vars qw/@ISA @EXPORT @EXPORT_OK/;
@ISA = qw/Exporter/; @ISA = qw/Exporter/;
@EXPORT = qw/CompileScripts RunScript RunScriptTick/; @EXPORT = qw/CompileScripts RunScript RunScriptTick GetGameVar SetGameVar DefGameVar/;
@EXPORT_OK = @EXPORT; @EXPORT_OK = @EXPORT;
my %labels; my %labels;
@ -23,6 +23,7 @@ my $current_line;
my %script_commands = ( my %script_commands = (
'Wait' => {'sub' => sub {}, 'wait' => 1}, 'Wait' => {'sub' => sub {}, 'wait' => 1},
'AddSayer' => {'sub' => \&AddSayer, 'wait' => 0}, 'AddSayer' => {'sub' => \&AddSayer, 'wait' => 0},
'Define' => {'sub' => \&DefGameVar, 'wait' => 0},
'Jump' => {'sub' => \&PerlRPG::Script::RunScript, 'wait' => 0}, 'Jump' => {'sub' => \&PerlRPG::Script::RunScript, 'wait' => 0},
@ -39,6 +40,38 @@ my %script_commands = (
my %sayers = (); my %sayers = ();
my $lastsayer=undef; my $lastsayer=undef;
my %game_vars = ();
sub GetGameVar {
my $key = shift(@_);
if(exists $game_vars{$key}) {
return $game_vars{$key};
}
LogData(WARN, "GetGameVar($key): Unknown variable");
return undef;
}
sub IsString {
return 1 if($_[0]=~/^\s*".+"\s*$/);
}
sub SetGameVar {
my($key, $val)=@_;
if(exists $game_vars{$key}) {
return $game_vars{$key} = StringSubst($val);
}
LogData(WARN, "SetGameVar($key): Unknown variable");
return undef;
}
# It is legal to Define a game variable twice, value will not be reset
sub DefGameVar {
my($key, $val)=@_;
if(!exists $game_vars{$key}) {
$game_vars{$key} = StringSubst($val);
}
}
# Load script files, locate labels # Load script files, locate labels
sub CompileScripts { sub CompileScripts {
my @scripts = sort { $a cmp $b } LoadAssets('gs'); my @scripts = sort { $a cmp $b } LoadAssets('gs');
@ -113,13 +146,16 @@ sub RunScriptLine {
$script_commands{$cmd}{'sub'}->(@opts); $script_commands{$cmd}{'sub'}->(@opts);
return $script_commands{$cmd}{'wait'}; return $script_commands{$cmd}{'wait'};
} elsif(exists $sayers{$cmd}) { } elsif(exists $sayers{$cmd}) {
my($text)=$oline=~/\Q$cmd\E\s+"(.+?)"\s*$/; my($text)=$oline=~/\Q$cmd\E\s+(".+?")\s*$/;
if($text) { if($text) {
return SayLine($sayers{$cmd}, $text); return SayLine($sayers{$cmd}, $text);
} else { } else {
LogData(ERROR, "Invalid sayer text in script at $file:$linenum"); LogData(ERROR, "Invalid sayer text in script at $file:$linenum");
return; return;
} }
} elsif(my($key,$val)=$line=~/^\s*(\S+)\s*=\s*(.+?)\s*$/) {
LogData(DEBUG, "Setting '%s' to '%s'", $key, $val);
return SetGameVar($key, $val);
} }
LogData(ERROR, "Unknown command '$cmd' in script at $file:$linenum"); LogData(ERROR, "Unknown command '$cmd' in script at $file:$linenum");
@ -134,17 +170,44 @@ sub AddSayer {
$sayers{$name} = { $sayers{$name} = {
'Name' => $name, 'Name' => $name,
'DisplayName' => $displayname, 'DisplayName' => StringSubst($displayname),
'Sprite' => $sprite, 'Sprite' => $sprite,
'TextColor' => \@textcolor 'TextColor' => \@textcolor
}; };
} }
sub EvalString {
my $val = shift(@_);
# Perform math calculations and the like
LogData(ERROR, "Doing incomplete EvalString for '$val'");
return $val;
}
sub StringSubst {
my $text=shift(@_);
if(!IsString($text)) {
return EvalString($text);
}
($text)=$text=~/^\s*"(.+)"\s*$/;
while(my($f,$n)=$text=~/(\[(\S+)\])/) {
my $v=GetGameVar($n);
if(!$v) {
$v = '!!INVALID!!';
}
$text=~s/\Q$f\E/$v/g;
}
$text=~s/\\n/\n/g;
return $text;
}
sub SayLine { sub SayLine {
my($ref, $text)=@_; my($ref, $text)=@_;
LogData(INFO, "%s saying \"%s\"", $ref->{'DisplayName'}, $text); LogData(INFO, "%s saying \"%s\"", $ref->{'DisplayName'}, $text);
$text=~s/\\n/\n/g; $text = StringSubst($text);
DrawSpeech($ref, $text); DrawSpeech($ref, $text);
$lastsayer = $ref; $lastsayer = $ref;
return 1; return 1;

View File

@ -1,6 +1,7 @@
__init__: __init__:
SetBackground 128 128 128 255 SetBackground 128 128 128 255
SetAssetOption spritesheet.png Animated SetAssetOption spritesheet.png Animated
Define VarName "Foo"
Show aka spritesheet.png 0 0 1 flip Show aka spritesheet.png 0 0 1 flip
Show aka2 spritesheet.png 100 0 2 Reverse Show aka2 spritesheet.png 100 0 2 Reverse
@ -15,5 +16,6 @@ donothing:
Wait Wait
Wait Wait
h "Some text here\nNext line" h "Some text here\nNext line"
h2 "Different text here" h2 "Say Foo here [VarName]"
VarName = "[VarName]."
Jump donothing Jump donothing