Design and Development of game scripts-Chapter 1 reading and parsing a script file

Source: Internet
Author: User
Tags addchild

PreviousGame script design and development-PrefaceThis section describes the basic concepts and preparations of a game script.

The parsing script restores each script command to different code logic for execution according to its own defined syntax. For example, I need to draw a rectangular script.

draw rect

And a script for drawing circles.

draw arc

Then, when I read the string "Draw rect", I drew a rectangle on the screen and read the string "Draw arc", I drew a circle on the screen.

If you have installed XAMPP according to the instructions in the previous article, find the htdocs folder in XAMPP, create an lsharp folder in it, and then create some files and folders according to the following structure.
Lsharp
|-Script folder
| -Index.html
|-Main. js
|-Lufylegend. lsharp. js
|-Lufylegend-1.7.5.js
The code of the index.html file is as follows:

<!DOCTYPE html>

Note: lufylegend-1.7.5.js you need to download it yourself.
To parse the script, you need to create a function analysis for parsing the script. Every time a script is parsed, an analysis function is called, so that the parsing continues until all parsing of the script file ends.
First, create an lscript. js class in the lufylegend. lsharp. js file and use it as the parsing object of the script to control parsing and execution of the script.

/** LScript.js**/function LScript(scriptLayer,value){var self = this;LGlobal.script = self;self.scriptLayer = scriptLayer;self.dataList = new Array();var arr=[value];self.dataList.unshift(arr);self.toList(value);}

Code parsing:
Lscript. the JS class accepts two parameters. The first parameter scriptlayer is the display layer and the bottom-layer canvas for the game using the L # script, all subsequent drawings of the script command will be drawn to this display layer. The second parameter is a string script and also lscript. the script that will be parsed immediately after the JS object is created.

LGlobal.script = self;

Lglobal. Script saves the parsing object of the script to facilitate the callback of the parsing function of the script.

self.scriptLayer = scriptLayer;

Save the display layer for future use.

self.dataList = new Array();var arr=[value];self.dataList.unshift(arr);

Save the script to the cache array.

self.toList(value);

This line breaks down the entire string script to obtain an array of single script commands.
Take a look at the tolist function:

toList:function(ltxt){var self = this;self.lineList = ltxt.split(";");self.copyList = self.lineList.slice(0);self.analysis();}

Code parsing:

self.lineList = ltxt.split(";");

In L #, I set that each script command is separated by a semicolon. Therefore, the string split function is used to split the string to obtain an array of single script commands, and save it to the linelist of the script parsing object.

self.copyList = self.lineList.slice(0);

Copylist is used to save the currently executed linelist. When a new script command linelist is added, the current linelist is saved, after all the new script commands linelist have been parsed, the last unparsed script command will continue to be parsed and executed.

self.analysis();

Start parsing script commands.
Next, let's take a look at the key script parsing function analysis.

analysis:function(){var self = this;var lineValue = "";if(self.lineList.length == 0){self.dataList.shift();if(self.dataList.length > 0){arr=self.dataList[0];self.lineList = arr[1];self.copyList = arr[2];self.analysis();}return;}while(self.lineList.length > 0){lineValue = LMath.trim(self.lineList[0]);self.lineList.shift();}if(lineValue.length == 0){self.analysis();return;}trace("analysis lineValue = " + lineValue);var sarr = lineValue.split(".");switch(sarr[0]){default:self.analysis();}}

Code parsing:

if(self.lineList.length == 0){self.dataList.shift();if(self.dataList.length > 0){arr=self.dataList[0];self.lineList = arr[1];self.copyList = arr[2];self.analysis();}return;}

The code above checks whether the script commands in the linelist array are parsed. If the scripts are parsed, check whether there are other unexecuted scripts in the datalist array.

var lineValue = "";while(self.lineList.length > 0 && lineValue.length == 0){lineValue = LMath.trim(self.lineList[0]);self.lineList.shift();}

The above code is used to obtain the script commands in the linelist array.

if(lineValue.length == 0){self.analysis();return;}

If the current script command is empty, execute the next command.

trace("analysis lineValue = " + lineValue);

This line of code is used for debugging. It can be released

var sarr = lineValue.split(".");switch(sarr[0]){default:self.analysis();}

In L #, all executed script commands are [classes. command] format. If you use another format when setting the script, perform corresponding processing according to your format. Therefore, the split function is used to split each command, obtain the required information, and parse it.
Next, we add the following code in Main. JS:

init(50,"mylegend",400,100,main);function main(){LGlobal.setDebug(true);var sc = "aaa;Load.script(script/Main.ls);bbb;";var sp = new LSprite();addChild(sp);var script = new LScript(sp,sc);}

Then the script parsing class will be directed to ["AAA; load. script (script/main. ls); BBB; "] This script is parsed. The script commands in it are not correct.
Run the code,

Your local execution URL is http: // localhost/lsharp/index.html

Check the information output by the debug function, as shown below:

 

Figure 1

As you can see, the parsing class parses the script and splits the script into three commands by semicolons. Then you need to add the parsing of each command, when an unresolvable command occurs, the next command is automatically skipped for parsing.
Next, we will first parse the first script command and use this script to read a script file.
First, I stipulate that the syntax for reading a script file in L # is as follows:

Load.script(script/Main.ls);

Create a new main. ls file in the script folder and use NotePad to open the main. ls file and write the following content.

Text. Label (-, txt, test A, 000000, 30, #000000); text. Label (-, txt, Test B, 30 );

Next, modify the switch part of the resolution function as follows:

switch(sarr[0]){case "Load":ScriptLoad.analysis(lineValue);break;default:self.analysis();}

Therefore, we need a static class scriptload to parse the load command and create a new scriptload class, as shown below:

/** ScriptLoad.js**/var ScriptLoad = function (){};ScriptLoad.data = "";ScriptLoad.urlloader = null;ScriptLoad.analysis = function (value){var start = value.indexOf("(");var end = value.indexOf(")");ScriptLoad.data = value.substring(start+1,end).split(",");switch(LMath.trim(value.substr(0,start))){case "Load.script":ScriptLoad.loadScript();break;default:LGlobal.script.analysis();}};

In the analysis function of the scriptload class, the content inside and outside the brackets of the script [load. script] and [script/main. ls], and then use the switch function for further parsing. When the string "load. script" is encountered, call the loadscript function to read a script file.
See the following code.

ScriptLoad.loadScript = function (){ScriptLoad.urlloader = new LURLLoader();ScriptLoad.urlloader.addEventListener(LEvent.COMPLETE,ScriptLoad.loadScriptOver);ScriptLoad.urlloader.load(ScriptLoad.data[0],"text");};ScriptLoad.loadScriptOver = function (event){var script = LGlobal.script;var data = event.target.data;ScriptLoad.urlloader.die();ScriptLoad.urlloader = null;script.saveList();script.dataList.unshift([data]);script.toList(data);};

Code Parsing
The loadscript function is relatively simple. It uses the lurlloader object to read a file and then calls the loadscriptover function.
The loadscriptover function uses the following three lines of code to store the read content to the array of the script parsing class for next parsing.

script.saveList();script.dataList.unshift([data]);script.toList(data);

The savelist function is as follows:

saveList:function(){var self = this;var arr=self.dataList[0];if(arr){arr[1]=self.lineList;arr[2]=self.copyList;}}

The processing is as mentioned above. Save the array of scripts currently being executed

Run the code. debug outputs the following information:

 

Figure 2

We can see that all the script commands in the main. ls script file have been parsed, but we haven't Parsed the script text. Label, so we just skipped it.
In fact, we can also read the script file in the script file, for example, replace the content in Main. ls with the following:

Text. label (-, txt, test A, 000000, 30, #); load. script (script/test. ls); text. label (-, txt, Test B, 000000, 30 );

In addition, add a test. ls script file. The content in test. ls is as follows:

Text. Label (-, txt, test C, 0, 0, 30, #000000 );

Run the program to view the following information:

 

Figure 3

As you can see, the script commands in test. ls are also read.

Test connection

Http://lufylegend.com/demo/test/lsharp/01/index.html

Currently, the source code is relatively small, so I directly paste main. js

init(50,"mylegend",400,100,main);function main(){LGlobal.setDebug(true);var sc = "Load.script(script/Main.ls);";var sp = new LSprite();addChild(sp);var script = new LScript(sp,sc);}

Lufylegend. lsharp. js

/** LScript.js**/function LScript(scriptLayer,value){var self = this;LGlobal.script = self;self.scriptLayer = scriptLayer;self.dataList = new Array();var arr=[value];self.dataList.unshift(arr);self.toList(value);}LScript.prototype = {toList:function(ltxt){var self = this;self.lineList = ltxt.split(";");self.copyList = self.lineList.slice(0);self.analysis();},saveList:function(){var self = this;var arr=self.dataList[0];if(arr){arr[1]=self.lineList;arr[2]=self.copyList;}},analysis:function(){var self = this;var arr;if(self.lineList.length == 0){self.dataList.shift();if(self.dataList.length > 0){arr=self.dataList[0];self.lineList = arr[1];self.copyList = arr[2];self.analysis();}return;}var lineValue = "";while(self.lineList.length > 0 && lineValue.length == 0){lineValue = LMath.trim(self.lineList[0]);self.lineList.shift();}if(lineValue.length == 0){self.analysis();return;}trace("analysis lineValue = " + lineValue);var sarr = lineValue.split(".");switch(sarr[0]){case "Load":ScriptLoad.analysis(lineValue);break;default:self.analysis();}}};/** ScriptLoad.js**/var ScriptLoad = function (){};ScriptLoad.data = "";ScriptLoad.urlloader = null;ScriptLoad.analysis = function (value){var start = value.indexOf("(");var end = value.indexOf(")");ScriptLoad.data = value.substring(start+1,end).split(",");switch(LMath.trim(value.substr(0,start))){case "Load.script":ScriptLoad.loadScript();break;default:LGlobal.script.analysis();}};ScriptLoad.loadScript = function (){ScriptLoad.urlloader = new LURLLoader();ScriptLoad.urlloader.addEventListener(LEvent.COMPLETE,ScriptLoad.loadScriptOver);ScriptLoad.urlloader.load(ScriptLoad.data[0],"text");};ScriptLoad.loadScriptOver = function (event){var script = LGlobal.script;var data = event.target.data;ScriptLoad.urlloader.die();ScriptLoad.urlloader = null;script.saveList();script.dataList.unshift([data]);script.toList(data);};

This chapter is about this. In the next chapter, we will start with the topic.Hello world!
Finally, I made an advertisement for the award collection activity series-[HTML5 game programming tour]Welcome to http://blog.csdn.net/blogdevteam/article/details/8899926

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.