Unity3d hand-Tour Development Diary (2)-Skill system Architecture Design

Source: Internet
Author: User

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
  1. Private bool Parsescript (string filename)
  2. {
  3. BOOL ret = false;
  4. Try
  5. {
  6. StreamReader sr = filereaderproxy.readfile (filename);
  7. if (sr! = NULL)
  8. RET = Loadscriptfromstream (SR);
  9. }
  10. catch (Exception e)
  11. {
  12. String err = "Exception:" + e.message + "\ n" + e.stacktrace + "\ n";
  13. Logsystem.errorlog (ERR);
  14. }
  15. return ret;
  16. }
  17. Private BOOL Loadscriptfromstream (StreamReader SR)
  18. {
  19. bool bracket = false;
  20. Skillinstance skill = null;
  21. Do
  22. {
  23. string line = Sr.  ReadLine ();
  24. if (line = = null)
  25. Break ;
  26. line = line. Trim ();
  27. if (line. StartsWith ("//") | | line = = "")
  28. continue;
  29. if (line. StartsWith ("skill"))
  30. {
  31. int start = line.  IndexOf ("(");
  32. int end = line.  IndexOf (")");
  33. if (start = =-1 | | end = = 1)
  34. Logsystem.errorlog ("Parsescript Error, start = =-1 | |  end = =-1 {0} ", line);
  35. int length = end-start-1;
  36. if (length <= 0)
  37. {
  38. Logsystem.errorlog ("Parsescript Error, length <= 1, {0}", line);
  39. return false;
  40. }
  41. string args = line.  Substring (start + 1, length);
  42. int skillid = (int) convert.changetype (args, typeof (int));
  43. Skill = new Skillinstance ();
  44. Addskillinstancetopool (Skillid, skill, true);
  45. }
  46. Else if (line. StartsWith ("{"))
  47. {
  48. Bracket = true;
  49. }
  50. Else if (line. StartsWith ("}"))
  51. {
  52. Bracket = false;
  53. //Sort by Time
  54. Skill.m_SkillTrigers.Sort (left, right) = =
  55. {
  56. if (left. GetStartTime () > right. GetStartTime ())
  57. {
  58. return-1;
  59. }
  60. Else if (left. GetStartTime () = = right. GetStartTime ())
  61. {
  62. return 0;
  63. }
  64. Else
  65. {
  66. return 1;
  67. }
  68. });
  69. }
  70. Else
  71. {
  72. //Parse trigger
  73. if (skill! = NULL && bracket = = true)
  74. {
  75. int start = line.  IndexOf ("(");
  76. int end = line.  IndexOf (")");
  77. if (start = =-1 | | end = = 1)
  78. Logsystem.errorlog ("Parsescript Error, {0}", line);
  79. int length = end-start-1;
  80. if (length <= 0)
  81. {
  82. Logsystem.errorlog ("Parsescript Error, length <= 1, {0}", line);
  83. return false;
  84. }
  85. String type = line.  Substring (0, start);
  86. string args = line.  Substring (start + 1, length);
  87. args = args.  Replace ("", "");
  88. Iskilltrigger trigger = SkillTriggerMgr.Instance.CreateTrigger (type, args);
  89. if (trigger! = null)
  90. {
  91. SKILL.M_SKILLTRIGERS.ADD (trigger);
  92. }
  93. }
  94. }
  95. } while (true);
  96. return true;
  97. }



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

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.