游戏脚本管理
前言:
在自己制作了单机泡泡堂的游戏之后,感觉应该加入游戏脚本的支持,通过一段时间学习,我将学的一些步骤和经验包括代码写了下来.
一、实现以指令为基础的游戏脚本语言
现在说的是理论方法,你马上可以实现一种以指令为基础的语言。首先设计一组简单的指令,用于显示文本,并加入简单的循环。最终用MessageBox对话框显示出来。
设计语言第一个步骤,建立自己的指令,为了更直观我建立一个指令表,列表如下:
文本控制指令表
命令
参数
描述
PrintString
string
输出字符串
PrintStringLoop
string,count
根据count指定输出次数
NewLine
None
增加一个空行
WaitForKeyPress
None
等待按键
二、编写一个脚本
首先确认你认为所需要的功能,目前它就是上表列的4条指令。它最好为一个标准的文本文件。
PrintString "这是一种以指令为基础的语言"
PrintString "也是一个简单的脚本代码"
Newline
PrintString "但它相当的简单"
Newline
PrintStringLoop "将被输出4次" 4
Newline
PrintString "等下将执行等待按键操作"
WaitForKeyPress
三、编写TScript类
这段代码相当简单,主要体现于分解字符串的操作,根据不同的指令来执行不同的代码。
unit uScripting;
interface
uses
Windows,
Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms;
const
//四个指令的常量定义
COMMAND_PRINTSTRING = ´PrintString´;
COMMAND_NEWLINE = ´Newline´;
COMMAND_PRINTSTRINGLOOP = ´PrintStringLoop´;
COMMAND_WAITFORKEYPRESS = ´WaitForKeyPress´;
type
TScript = class
private
FCommandList: TStrings; //保存脚本
procedure GetCommand(Index: integer; var strCommand: string); //获取一个命令
procedure GetStrParam(Index: integer; var strParam: string); //获取一个字符串参数
procedure GetIntParam(Index: integer; var intParam: integer); //获取一个整形参数
public
constructor Create;
destructor Destroy; override;
procedure LoadScript(AFile: string); //装入脚本
procedure UnLoadScript; //清空脚本
procedure RunScript(); //运行脚本
end;
implementation
{ TScript }
constructor TScript.Create;
begin
FCommandList := TStringList.Create;
end;
destructor TScript.Destroy;
begin
FCommandList.Clear;
FCommandList.Free;
inherited Destroy;
e




