Initial script variable support
This commit is contained in:
parent
b3b7109bab
commit
17337d547d
|
|
@ -46,7 +46,7 @@ sub KeyPressed {
|
|||
my($key, $updown, $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");
|
||||
}
|
||||
|
||||
|
|
@ -54,5 +54,5 @@ sub MouseClick {
|
|||
my($x, $y, $btn, $updown, $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));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ use PerlRPG::Drawing;
|
|||
|
||||
use vars qw/@ISA @EXPORT @EXPORT_OK/;
|
||||
@ISA = qw/Exporter/;
|
||||
@EXPORT = qw/CompileScripts RunScript RunScriptTick/;
|
||||
@EXPORT = qw/CompileScripts RunScript RunScriptTick GetGameVar SetGameVar DefGameVar/;
|
||||
@EXPORT_OK = @EXPORT;
|
||||
|
||||
my %labels;
|
||||
|
|
@ -23,6 +23,7 @@ my $current_line;
|
|||
my %script_commands = (
|
||||
'Wait' => {'sub' => sub {}, 'wait' => 1},
|
||||
'AddSayer' => {'sub' => \&AddSayer, 'wait' => 0},
|
||||
'Define' => {'sub' => \&DefGameVar, 'wait' => 0},
|
||||
|
||||
'Jump' => {'sub' => \&PerlRPG::Script::RunScript, 'wait' => 0},
|
||||
|
||||
|
|
@ -39,6 +40,38 @@ my %script_commands = (
|
|||
my %sayers = ();
|
||||
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
|
||||
sub CompileScripts {
|
||||
my @scripts = sort { $a cmp $b } LoadAssets('gs');
|
||||
|
|
@ -113,13 +146,16 @@ sub RunScriptLine {
|
|||
$script_commands{$cmd}{'sub'}->(@opts);
|
||||
return $script_commands{$cmd}{'wait'};
|
||||
} elsif(exists $sayers{$cmd}) {
|
||||
my($text)=$oline=~/\Q$cmd\E\s+"(.+?)"\s*$/;
|
||||
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;
|
||||
}
|
||||
} 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");
|
||||
|
|
@ -134,17 +170,44 @@ sub AddSayer {
|
|||
|
||||
$sayers{$name} = {
|
||||
'Name' => $name,
|
||||
'DisplayName' => $displayname,
|
||||
'DisplayName' => StringSubst($displayname),
|
||||
'Sprite' => $sprite,
|
||||
'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 {
|
||||
my($ref, $text)=@_;
|
||||
LogData(INFO, "%s saying \"%s\"", $ref->{'DisplayName'}, $text);
|
||||
|
||||
$text=~s/\\n/\n/g;
|
||||
$text = StringSubst($text);
|
||||
DrawSpeech($ref, $text);
|
||||
$lastsayer = $ref;
|
||||
return 1;
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
__init__:
|
||||
SetBackground 128 128 128 255
|
||||
SetAssetOption spritesheet.png Animated
|
||||
Define VarName "Foo"
|
||||
|
||||
Show aka spritesheet.png 0 0 1 flip
|
||||
Show aka2 spritesheet.png 100 0 2 Reverse
|
||||
|
|
@ -15,5 +16,6 @@ donothing:
|
|||
Wait
|
||||
Wait
|
||||
h "Some text here\nNext line"
|
||||
h2 "Different text here"
|
||||
h2 "Say Foo here [VarName]"
|
||||
VarName = "[VarName]."
|
||||
Jump donothing
|
||||
|
|
|
|||
Loading…
Reference in New Issue