Design and Development of game scripts-Chapter 3 basic syntax (comments, variables, functions, and condition statements)

Source: Internet
Author: User

This chapter parses script syntaxes that are essential in any language, including comments, variables, functions, and condition statements. The script format is as follows.

/* Chapter 3 of game script design and development * // set the variable num to 5var. set (Num, 5); // conditional statement script to test if (@ num> 10); var. set (name, lufy); elseif (@ num> 4); If (@ num = 5); var. set (name, hello); else; var. set (name, hellow); endif; else; var. set (name, legend); endif; text. label (-, txt01, variable test: Hello @ name, 000000, 30, #); // declare the test function test (value); text. label (-, txt02, function test: @ value, 000000, 30, #); endfunction; call. test (parameter test );

Next we will implement the parsing of these scripts one by one.

1. Comment

Annotations are inevitably used in scripts, but there are two common annotation methods.

//Text.label(-,txt01,Hello World,0,0,30,#000000);/*Text.label(-,txt01,Hello World,0,0,30,#000000);*/

The method to remove these annotations is to locate the start and end positions of the two annotations, and then remove them.

removeComment:function(str){var self = this;var sIndex;var eIndex;var sStr;var eStr;sIndex = str.indexOf("/*");while(sIndex >=0){eIndex = str.indexOf("*/",sIndex + 2);sStr = str.substr(0,sIndex);eStr = str.substr(eIndex + 2);str = sStr + eStr;sIndex = str.indexOf("/*");}sIndex = str.indexOf("//");while(sIndex >=0){eIndex = str.indexOf("\n",sIndex);if(eIndex >= 0){sStr = str.substr(0,sIndex);eStr = str.substr(eIndex);str = sStr + eStr;sIndex = str.indexOf("//");}else{sStr = str.substr(0,sIndex);str = sStr;sIndex = -1;}}return str;}

Then, each time you read the script file, use the above function to filter and delete the comment.

2. Variables

For any language, variables are indispensable, and scripts are the same. For example, there are many branch options in the game, and the game should make different choices based on the players, for different plot development, we must use a large number of variables to save these choices.

Expand the lscriptarray class as follows:

function LScriptArray(){var self = this;self.textList = new Array();self.layerList = new Array();self.varList = new Array();self.funList = new Array();}

Varlist is used to save the variable, and funlist is used to save the function. After talking about the variable, we will talk about it.

First, define the variable syntax.

// Set the variable VAR. set (name, lufy); // use the variable @ + variable name text. label (-, txt01, variable test: Hello @ name, 000000, 30 );

First, implement the variable settings and modify the switch part of the parsing function.

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

The scriptvarlable class code is as follows:

/** ScriptVarlable.js**/var ScriptVarlable = function (){};ScriptVarlable.analysis = function (value){var start = value.indexOf("(");var end = value.indexOf(")");switch(value.substr(0,start)){case "Var.set":ScriptVarlable.setVarlable(value,start,end);break;default:LGlobal.script.analysis();}};ScriptVarlable.setVarlable = function (value,start,end){var script = LGlobal.script;var lArr = value.substring(start+1,end).split(",");script.scriptArray.varList[lArr[0]] = lArr[1];script.analysis();};

This is easy to understand. In the setvarlable function, the content of Var. Set (name, lufy) is separated by semicolons (;) and saved as the variable name and value to the varlist array.

When using a variable, I define the rule @ + variable name. when parsing each script line, find the @ symbol to see if the variable name after it exists, the implementation method is as follows.

ScriptVarlable.getVarlable = function (str){var script = LGlobal.script;var iIndex = 0;var sIndex;var eIndex;var sStr;var eStr;var vStr;var result = "";var r=/^([a-z]|[A-Z]|_)+$/;sIndex = str.indexOf("@");while(sIndex >=0){eIndex = str.indexOf("@",sIndex+1);if(sIndex + 1 == eIndex){sStr = str.substr(iIndex,sIndex);vStr = "@";eStr = str.substr(eIndex + 1);iIndex = eIndex + 1;}else{sStr = str.substring(iIndex,sIndex);vStr = "";sIndex++;while(r.exec(str.charAt(sIndex))){vStr += str.charAt(sIndex);sIndex++;}vStr = script.scriptArray.varList[vStr];eStr = str.substr(sIndex);iIndex = sIndex;};result += (sStr + vStr);sIndex = str.indexOf("@",iIndex);}result += str.substr(iIndex);return result;};

If you call this function before parsing the script for each row, the variable part in this row is replaced with the variable value to use the variable.
Modify the content of Main. ls as follows:

Var. Set (name, lufy); text. Label (-, txt01, variable test: Hello @ name, 000000, 30 );

Test the function as follows:

Figure 1

As you can see, when the text is displayed in the second line, "@ name" has been replaced with "lufy ".

3. Functions

The script for defining the function is as follows:

Function Test (value); text. Label (-, txt02, function test: @ value, 000000, 30, #); endfunction;

To parse the above script, you can find the keyword function and endfunction,

First, find a script containing the function keyword, indicating that the function starts to be declared, and then find spaces, left and right brackets in the line to separate the function name and parameter.

Then, start searching for the endfunction keyword from the next script. If there is no such keyword, it indicates that the script for this row belongs to the function body. If there is one, it indicates that the function is declared to end, and then according to the function name, save both the function body and parameters.

The specific implementation method is as follows.

/** ScriptFunction.js**/var ScriptFunction = function (){};ScriptFunction.setFunction = function (value){var script = LGlobal.script;var startNameIndex = value.indexOf(" ");var child;var funArr = new Array();var start = value.indexOf("(");var end = value.indexOf(")");var strParam = value.substring(start + 1,end);var param = strParam.split(",");funArr["param"] = new Array();var i;for(i=0;i<param.length;i++){param[i] = LMath.trim(param[i]);if(param[i].length > 0){funArr["param"].push("param_" + param[i]);}}funArr["name"] = LMath.trim(value.substring(startNameIndex + 1,start));var funLineArr = new Array();while(script.lineList[0].indexOf("endfunction") < 0){child = script.lineList.shift();for(i=0;i<param.length;i++){if(param[i].length > 0)child = child.replace("@"+param[i],"@param_"+param[i]);}funLineArr.push(child);}script.lineList.shift();funArr["function"] = funLineArr;script.scriptArray.funList[funArr["name"]] = funArr;script.analysis();};

Now that the function can be declared, the next step is how to call this function. The script syntax for calling the function is as follows:

Call. Test (parameter test );

This line of script calls the function test and passes in the "parameter test" string as the function parameter.

How can I find the test function and run it.

ScriptFunction.analysis = function (value){var script = LGlobal.script;var point = value.indexOf(".");var start = value.indexOf("(");var end = value.indexOf(")");var name = value.substring(point + 1,start);var funArr = script.scriptArray.funList[name];if(funArr == null){script.analysis();return;}_data = value.substring(start+1,end).split(",");var param = funArr["param"];var i;for(i=0;i<param.length;i++){script.scriptArray.varList[param[i]] = _data[i];}var funLineArr = funArr["function"];for(i=funLineArr.length-1;i>=0;i--)script.lineList.unshift(funLineArr[i]);script.analysis();};

In fact, the function name "test" is used to find the corresponding function from the funlist array, and then add the script stored in the function body to the script parsing queue linelist, in this way, the parsing function automatically parses these scripts to call the function.

Modify the switch part of the DNS function as follows:

switch(sarr[0]){case "Load":ScriptLoad.analysis(lineValue);break;case "Text":ScriptText.analysis(lineValue);break;case "Var":ScriptVarlable.analysis(lineValue);break;case "Call":ScriptFunction.analysis(lineValue);break;default:if(lineValue.indexOf("function") >= 0){ScriptFunction.setFunction(lineValue);}else{self.analysis();}}

Next, let's test the function script. Modify the main. ls file as follows:

Function Test (value); text. Label (-, txt02, function test: @ value, 0, 50, 30, #000000); endfunction; call. Test (parameter test );

Run the program to get the effect

Figure 2

The function "test" has been successfully called and the parameters have been successfully passed in.

4. conditional statements

The Condition Statement is if... else.... This is relatively complicated because it involves multi-layer nesting and if is included.

The Condition Statement is defined as follows:

If (condition); script else if (condition); script else; script endif;

Like functions, the key to parsing these scripts is the keywords "if", "else if", "else", and "endif", as long as they are correctly searched, you can parse these scripts.
If Condition Statement parsing functions are as follows:

/** ScriptIF.js**/var ScriptIF = function (){};ScriptIF.getIF = function (value){var script = LGlobal.script;var startifIndex = 0;var endifIndex = 0;var ifArr;var childArray = new Array();var start = value.indexOf("(");var end = value.indexOf(")");var str = value.substring(start + 1,end);ifArr = str.split("&&");var ifvalue = ScriptIF.checkCondition(ifArr);var ifvalueend = false;var sCount = 0;var eCount = 0;while(startifIndex<script.lineList.length){sCount = 0;if(script.lineList[startifIndex].indexOf("elseif") >= 0){if(ifvalue){ifvalueend = true;startifIndex++;continue;}start = script.lineList[startifIndex].indexOf("(");end = script.lineList[startifIndex].indexOf(")");str = script.lineList[startifIndex].substring(start + 1,end);str = ScriptVarlable.getVarlable(str);ifArr = str.split("&&");ifvalue = ScriptIF.checkCondition(ifArr);startifIndex++;continue;}else if(script.lineList[startifIndex].indexOf("else") >= 0){if(ifvalue){ifvalueend = true;startifIndex++;continue;}ifvalue = true;endifIndex = startifIndex;startifIndex++;continue;}else if(script.lineList[startifIndex].indexOf("endif") >= 0){startifIndex++;break;}else if(script.lineList[startifIndex].indexOf("if") >= 0){if(ifvalue && !ifvalueend){childArray.push(script.lineList[startifIndex]);}startifIndex++;sCount = 1;eCount = 0;while(sCount > eCount){if(script.lineList[startifIndex].indexOf("if") >= 0 && script.lineList[startifIndex].indexOf("else") < 0 && script.lineList[startifIndex].indexOf("end") < 0){sCount++;}else if(script.lineList[startifIndex].indexOf("endif") >= 0){eCount++;}if(ifvalue && !ifvalueend){childArray.push(script.lineList[startifIndex]);}startifIndex++;}}if(sCount==0){if(ifvalue && !ifvalueend){childArray.push(script.lineList[startifIndex]);}startifIndex++;}}script.lineList.splice(0,startifIndex);for(var i=childArray.length - 1;i>=0;i--){script.lineList.unshift(childArray[i]);}script.analysis();};ScriptIF.checkCondition = function (arr){for(var i = 0;i<arr.length;i++){if(!ScriptIF.condition(arr[i])){return false;}}return true;};ScriptIF.condition = function (value){var arr;if(value.indexOf("===") >= 0){//===arr=ScriptIF.getCheckStr(value,"===");return arr[0] == arr[1];}else if(value.indexOf("!==") >= 0){//!==arr=ScriptIF.getCheckStr(value,"!==");return arr[0] != arr[1];}else if(value.indexOf("==") >= 0){//==arr=ScriptIF.getCheckInt(value,"==");return arr[0] == arr[1];}else if(value.indexOf("!=") >= 0){//!=arr=ScriptIF.getCheckInt(value,"!=");return arr[0] != arr[1];}else if(value.indexOf(">=") >= 0){//>=arr=ScriptIF.getCheckInt(value,">=");return arr[0] >= arr[1];}else if(value.indexOf("<=") >= 0){//<=arr=ScriptIF.getCheckInt(value,"<=");return arr[0] <= arr[1];}else if(value.indexOf(">") >= 0){//>arr=ScriptIF.getCheckInt(value,">");return arr[0] > arr[1];}else if(value.indexOf("<") >= 0){//<arr=ScriptIF.getCheckInt(value,"<");return arr[0] < arr[1];}return false;};ScriptIF.getCheckInt = function (value,s){var arr;arr = value.split(s);arr[0] = parseInt(arr[0]);arr[1] = parseInt(arr[1]);return arr;};ScriptIF.getCheckStr = function (value,s){var arr;arr = value.split(s);arr[0] = LMath.trim(arr[0].replace('"',''));arr[1] = LMath.trim(arr[1].replace('"',''));return arr;};

Modify the switch part of the DNS function as follows:

switch(sarr[0]){case "Load":ScriptLoad.analysis(lineValue);break;case "Text":ScriptText.analysis(lineValue);break;case "Var":ScriptVarlable.analysis(lineValue);break;case "Call":ScriptFunction.analysis(lineValue);break;default:if(lineValue.indexOf("if") >= 0){ScriptIF.getIF(lineValue);}else if(lineValue.indexOf("function") >= 0){ScriptFunction.setFunction(lineValue);}else{self.analysis();}}

Finally, let's test it. Modify the main. ls script file as follows:

Var.set(num,1);if(@num==0);Var.set(name,lufy);else;Var.set(name,legend);endif;

Run the program to get the result

Figure 3

Here is an example of a slightly more complex point. Continue to modify the main. ls script file as follows:

Var. set (Num, 5); If (@ num> 10); var. set (name, lufy); elseif (@ num> 4); If (@ num = 5); var. set (name, hello); else; var. set (name, hellow); endif; else; var. set (name, legend); endif;

Run the program to get the result

Figure 4

After the basic syntax is finished, I wanted to implement the next loop, but I cannot use it for the time being. I will discuss it in detail later,

So far, the script is only text. Do you think it is boring?The next chapter begins parsing scripts such as graphics and images.To start with an image.

The test connection is as follows:

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

The source code of lufylegend. lsharp. js as of this chapter is as follows:

Http://lufylegend.com/demo/test/lsharp/03/lufylegend.lsharp.js

Game script design and development articles

Http://blog.csdn.net/lufy_legend/article/details/8888787

This chapter is here. Welcome to continue to follow my blog

Reprinted Please note:From lufy_legend's blog http://blog.csdn.net/lufy_legend
Final ad time, prize collection series-HTML5 game programming tour]Http://blog.csdn.net/blogdevteam/article/details/8899926
Welcome to join us and have the opportunity to get my new book "HTML5 canvas game development practices"

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.