I want to do a good job, so the project at the outset I was thinking, whether the need for a flexible and free skills system architecture design, traditional skills design, practice is to fill Excel table, skills need, all forms, very rigid, such as some skills only need a special effect, some to 10, The table also has to reserve 10 special effects fields. In the code is also to write dead things, to add and modify, you have to change the core code, if I want to make the core part of the library package up, it is very troublesome.
Can it be done in a data-driven way?
Change the skill file, even if you want to add functionality, you only need to extend the external code, without changing the core code,
I came to abstract a skill, the skill is composed of a bunch of triggers, such as special effects trigger, action trigger, sound trigger, camera shake trigger, etc., these triggers to a certain conditions to execute the trigger, the trigger condition is generally time, if there are more complex floating space skills, can increase the landing trigger.
Customize a skill file instead of an Excel table that looks like this:
Simple skills:
Each row is a trigger, and these triggers are automatically triggered by a certain condition.
The above means that the No. 0 second starts to target, and the No. 0 second starts playing the action 1000
Complex skills:
The ability to hit the target into the air, complete 3 combos, then hit the ground from the air,
Curvemove (0, 0.413, 104, 0, 0, 0, 0); The meaning is, the first 0.413 seconds, start the curve movement, let the character fly into the air, the curve movement ID is 104,
Use such a file to configure a skill, very flexible, and soon,
[CSharp]View PlainCopy
- Private bool Parsescript (string filename)
- {
- BOOL ret = false;
- Try
- {
- StreamReader sr = filereaderproxy.readfile (filename);
- if (sr! = NULL)
- RET = Loadscriptfromstream (SR);
- }
- catch (Exception e)
- {
- String err = "Exception:" + e.message + "\ n" + e.stacktrace + "\ n";
- Logsystem.errorlog (ERR);
- }
- return ret;
- }
- Private BOOL Loadscriptfromstream (StreamReader SR)
- {
- bool bracket = false;
- Skillinstance skill = null;
- Do
- {
- string line = Sr. ReadLine ();
- if (line = = null)
- Break ;
- line = line. Trim ();
- if (line. StartsWith ("//") | | line = = "")
- continue;
- if (line. StartsWith ("skill"))
- {
- int start = line. IndexOf ("(");
- int end = line. IndexOf (")");
- if (start = =-1 | | end = = 1)
- Logsystem.errorlog ("Parsescript Error, start = =-1 | | end = =-1 {0} ", line);
- int length = end-start-1;
- if (length <= 0)
- {
- Logsystem.errorlog ("Parsescript Error, length <= 1, {0}", line);
- return false;
- }
- string args = line. Substring (start + 1, length);
- int skillid = (int) convert.changetype (args, typeof (int));
- Skill = new Skillinstance ();
- Addskillinstancetopool (Skillid, skill, true);
- }
- Else if (line. StartsWith ("{"))
- {
- Bracket = true;
- }
- Else if (line. StartsWith ("}"))
- {
- Bracket = false;
- //Sort by Time
- Skill.m_SkillTrigers.Sort (left, right) = =
- {
- if (left. GetStartTime () > right. GetStartTime ())
- {
- return-1;
- }
- Else if (left. GetStartTime () = = right. GetStartTime ())
- {
- return 0;
- }
- Else
- {
- return 1;
- }
- });
- }
- Else
- {
- //Parse trigger
- if (skill! = NULL && bracket = = true)
- {
- int start = line. IndexOf ("(");
- int end = line. IndexOf (")");
- if (start = =-1 | | end = = 1)
- Logsystem.errorlog ("Parsescript Error, {0}", line);
- int length = end-start-1;
- if (length <= 0)
- {
- Logsystem.errorlog ("Parsescript Error, length <= 1, {0}", line);
- return false;
- }
- String type = line. Substring (0, start);
- string args = line. Substring (start + 1, length);
- args = args. Replace ("", "");
- Iskilltrigger trigger = SkillTriggerMgr.Instance.CreateTrigger (type, args);
- if (trigger! = null)
- {
- SKILL.M_SKILLTRIGERS.ADD (trigger);
- }
- }
- }
- } while (true);
- return true;
- }
The parsing of the files is also very simple
So how do you do it from the code?
1. Trigger:
Inherit from the same base class,
2. Factory mode to create a registration trigger,
To register the trigger's code externally:
3. Skill instances to manage triggers,
Execution triggers can actually be written here as well.
4. Skill system to manage all skills
Skills can be reused, and the skill system is a skill pool, constantly new skill instances and recycling skills instances
Partial public Interface Code:
To summarize the idea, is
Skillsystem manage Skillinstance, create and reclaim all skills
Skillinstance Management Skilltrigger, responsible for trigger trigger.
Skilltrigger to perform specific effects.
Code encapsulation, the core code can be made into a library, only open the extension of the trigger interface, the project is already in use, very good.
Unity3d hand-Tour Development Diary (2)-Skill system Architecture Design