Game Event Editor Preliminary analysis and implementation (2)

Source: Internet
Author: User

The next step is to build the event class with the event Manager:

First, consider how an event is categorized, using virtual functions and inheritance.

The constructed event classes are as follows:

Class event{

BOOL eventcontroller* Evencontroller ();

BOOL conditioncontroller* Conditioncontroller ();

void actioncontroller* Actioncontroller ();

void Check () {

if (Eventcontroller->fucntion ())

if (Conditioncontroller->function ())

Actioncontroller->function ()

}

};

Class eventcontroller{

Public

virtual bool Function ();

};

Class conditioncontroller{

Public

virtual bool Function ();

};

Class actioncontroller{

Public

virtual void Function ();

};

For example, in the example above, we have to inherit Eventcontroller,conditioncontroller and Actioncontroller three classes separately:

Class Evententer:public eventcontroller{

Public

Man m;

Region R;

virtual bool Function ()

{

if (M in R)

return true;

return false;

}

};

Class Conditionis:public conditioncontroller{

Public

Man m;

Unit u;

virtual bool Function ()

{

if (M==u.master)

return true;

return false;

}

};

Class Actionattack:public actioncontroller{

Public

Mans &m;

Unit &u;

virtual void Action ()

{

Order u attack m;

}

};

The above variables are defined for public, for ease of operation.

And the event manager is fairly simple

Class eventmanager{

Vector<event> events;

Public

void Checkevents ();

void Addevent ();

...

};

After you have completed these classes, you begin to join the event.

Please see the following code for details:

#include <iostream> #include <Windows.h> #include <time.h> #include <vector> using namespace std ; struct man{int pos;//position int ID; int HP; Man () {pos=0; Id=0; hp=100; } bool Equals (man P) {if (id==p.id) return true; return false;}}; struct unit{man master; Man attacking; }; struct region{int rl,rr;}; Class eventcontroller{public:virtual bool Function () {return 0;}}; Class Evententer:public eventcontroller{Public:man m; Region R; virtual bool Function () {if (R.RL&LT;M.POS&AMP;&AMP;M.POS&LT;R.RR) return true; return false;}}; struct conditioncontroller{virtual bool Function () {return 0;}}; struct conditionis:conditioncontroller{man m; Unit u; BOOL Function () {if (M.equals (U.master)) return true;}}; struct actioncontroller{virtual void Function () {}}; struct actionattack:actioncontroller{man *m; Unit *u; void Function () {}}; struct event{eventcontroller* Eventcontroller; conditioncontroller* Conditioncontroller; actioncontroller* ActioncOntroller; Event (eventcontroller* e,conditioncontroller* c,actioncontroller* a) {eventcontroller=e; conditioncontroller=c; Actioncontroller=a; } void Check () {if (Eventcontroller->function ()) {cout << "A event happened" << Endl; if (conditioncontroll Er->function ()) {cout << "Conditon is Enough" << Endl; cout << "action!" << Endl; actioncontro Ller->function (); } else{cout << "Condition is not Enough" << Endl, cout << "Nothing happened" << Endl;} } }; Class eventmanager{vector<event> Events; vector<event>::iterator t; static eventmanager* instance; EventManager () {} public:static eventmanager* getinstance () {if (instance==null) instance=new EventManager (); return ins tance; } void Addevent (Event e) {events.push_back (e),} void Checkevents () {for (T=events.begin (); T!=events.end (); ++t) {(*t). Check (); }} ~eventmanager () {delete instance;}}; eventmanager* Eventmanager::instance=null; int main () {Man M; Mans p; Unit Dog; Region R; Dog.master=m; p.pos=5; Change the value of POS to debug p.id=1; Change the value of ID to debug r.rl=3; r.rr=10; Srand (Time (0)); Evententer Evententer; Evententer.m=p; Evententer.r=r; Conditionis Conditionis; Conditionis.m=p; Conditionis.u=dog; Actionattack Actionattack; actionattack.m=&m; actionattack.u=&dog; Event e (&evententer,&conditionis,&actionattack); Eventmanager::getinstance ()->addevent (e); Eventmanager::getinstance ()->checkevents (); } 

This completes the design and addition of an event, and if there are more event classes in the game, it is necessary to derive 3 classes such as Eventcontroller. That's probably it.

I don't know how to write an article, maybe some mistakes in the language, please understand.

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.