Sub-sandbox mode of game development design pattern (unity3d example implementation)

Source: Internet
Author: User

Accumulate to provide all operations (implementations) to define the behavior of subclasses
Use one of the simplest examples to illustrate this pattern
Player-manipulated heroes, the protagonist of the game, will have many skills, and we want to define many different skills for the player to use.
First we define a Skillbase class as the base class, and the actions of all our skills are implemented here. We can combine a variety of skills from these primitive actions, even hundreds, to design a doc document to design the operation of various skills, and the sequence of operations. This is why it is called the sub-class sandbox, the implementation of the technique as a sandbox, to the sandbox to add a variety of meta-action to form a variety of skills.
Take the legend of the Phoenix Day drive for example, such as (from the World legend-Dress Up Maze 2), this skill is divided into, back, jump, play animation, front punch, particle effects, play animation and other meta-action composed of



If we do not use the subclass sandbox pattern, and one write skill, there are the following disadvantages:
1. There will be a lot of duplicate code, resulting in code redundancy, because each skill has a duplication of places, such as playing sound, playing animation and so on. After using this mode, each operation method is like a component, free to use, no repetition.
2. Each skill class will be coupled with the game system and the game engine, such as sound, animation, and if the implementation of the operation is written in the base class so that the sub-class composition, only the base class and its coupling. The advantage of this model is that if there is an operation to add and change, it is not necessary to increase the deletion of each skill class, and only change the base class is good, convenient and simple. And reusability is pretty good, and the code can be used in a lot of games (for example, the legend of each series of Legends, Magic Sword)
In the base class skillbase, we need to implement some of the skills of the meta-action, such as move movement, jump jumps, playanimation play animation, PlaySound play the sound and so on, we put them together to achieve a variety of skills, these methods are protected.
Then we need a virtual method action is the final skill defined in the subclass of the implementation, which is why the above original action is protected, because we do not need to invoke these meta-actions, only need their combined skill action () method is good, So just let the subclass get the Meta action method with protected tag.
So we do some protected methods to assemble the whole together in a subclass.
We're going to do these skill classes
1. Build a skill class that inherits from the base class Skillbase named Skill1
2. Overriding the Skillbase action method
3. Implement the meta-action in action
The base class (Skillbase) provides an abstract sandbox method (action) and some meta action actions labeled protected (move, jump, playsound, etc.), which derive some sandbox classes, each of which is a sandbox class (SKILL1, Skill2 ... The Sandbox method (action) is implemented, and the Sandbox method contains various meta-actions (move, jump, PlaySound, etc.).
Sub-class sandbox mode can be used when the following conditions are met
1. A base class and a large number of derived subclasses
2. The base class can provide the actions required by all derived subclasses
3. Operations between subclasses, methods are duplicated
4. Want to reduce the coupling of sub-classes and game systems, game engines, etc.

Note: Because subclasses are closely related to the base class at this point, you may have a problem with the brittle base class, which means that you may have changed the base class safely, but the subclass might have a problem.

Code Implementation

The code is as follows:
Simple implementation of several meta-actions and abstract sandbox methods in Skillbase:

   protected voidActintActid) {Hero. ACT (Actid);//Play attack animations    }    protected voidPlaySound (intSoundid)    {audiosource.playoneshot (Audio[soundid]); }    protected voidMove (Vector3 dir,floatMovespeed,BOOLIsrun)    {Movefunc.move (dir, Movespeed, Isrun); }    protected voidJump () {movefunc.jump (); }    protected voidParticaleffect () {..    Particle effects: }    Virtual  Public voidAction ()//Sandbox method    {    }



Look at the sandbox method implemented by SKILL1, for example, if we want to implement a jump slash, we combine these meta-actions together.

    Override  Public void Action ()    {        playSound (0);        Act (0);        Jump ();        Particaleffect ();    }



It's simple, right?
Let's take a look at the method of base class initialization
Initialization of the base class
Method One: Constructors
To assign a value by means of a constructor, but such an argument constructor must be base on each sandbox class, so if the constructors of the base class have additions and deletions, the subclasses have to follow all the additions.

     Public skillbase (Audiosource _audiosource, audioclip[] _audio, Move1 _movefunc, Hero2 _hero, Gameobject _heroobject)    {        this. Audio = _audio;          this. Audiosource = _audiosource;         this. Movefunc = _movefunc;         this. Hero = _hero;         this. heroobject = _heroobject;    }



Method Two: Initialize the function init
But remember to call at the beginning, or watch the game crash

     Public void Init (Audiosource _audiosource, audioclip[] _audio, Move1 _movefunc, Hero2 _hero, Gameobject _heroobject)    {        this. Audio = _audio;          this. Audiosource = _audiosource;         this. Movefunc = _movefunc;         this. Hero = _hero;         this. heroobject = _heroobject;    }



Method Three: Static initialization methods Init and class variables (static variables)

So all the sand box classes, which are all skill, share a variable, and there are pros and cons.

   Static PrivateAudiosource Audiosource; Static Privateaudioclip[] Audio; Static PrivateMove1 Movefunc; Static PrivateHero2 hero; Static protectedGameobject Heroobject; Static  Public voidInit (Audiosource _audiosource, audioclip[] _audio, Move1 _movefunc, Hero2 _hero, Gameobject _heroobject) { Audio=_audio; Audiosource=_audiosource; Movefunc=_movefunc; Hero=_hero; Heroobject=_heroobject; }





Summary

Small mode, big use. A very simple design pattern, but very useful, the successful reduction of the sand box class and other classes of coupling, but also increased reusability, more importantly, this combination of ways to increase the diversity, which is what the game needs.

All code has been uploaded to GitHub

Blogger's recent renderings: some recent renderings using unity5

----by wolf96


Sub-sandbox mode of game development design pattern (unity3d example implementation)

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.