Sixth day of development of the project similar to LOL
By straw hat
OK, because the update module has been processed, and then start writing the login. Then we need to manage the state machine.
The state machine is the code logic in which the State is executed:
Then we started to write gamestatemanager for management:
We first define the game status type in definecommon:
/// <Summary> /// game status /// </Summary> Public Enum gamestatetype {gs_continue, // intermediate state, that is, gs_login, which is an intermediate state between two States, // login status gs_user, // create user status gs_lolobby, // Hall, similar to LOL main interface status gs_room, // create room status gs_hero, // select hero gs_loading, // loading status gs_play, // Battle Status gs_over, // end of the battle}
There are so many states, if there are other states, then add:
Then, create a dictionary cache state in gamestatemanager. This management method is very similar to windowmanage.
Using unityengine; using system. collections. generic; using game. common; // <summary> // game status manager /// </Summary> public class gamestatemanager: Singleton <gamestatemanager> {public dictionary <gamestatetype, igamestate> m_dicgamestates; igamestate m_ocurrentstate; Public igamestate currentstate {get {return this. m_ocurrentstate;} public gamestatemanager () {m_dicgamestates = new dictionary <Gamestatetype, igamestate> (); m_dicgamestates.add (gamestatetype. gs_login, new loginstate ());} /// <summary> /// change the game status /// </Summary> /// <Param name = "statetype"> </param> Public void changegamestateto (gamestatetype statetype) {If (m_ocurrentstate! = NULL & m_ocurrentstate.getstatetype ()! = Gamestatetype. gs_loading & m_ocurrentstate.getstatetype () = statetype) return; If (m_dicgamestates.containskey (statetype) {If (m_ocurrentstate! = NULL) {m_ocurrentstate.exit (); // first exit the previous state} m_ocurrentstate = m_dicgamestates [statetype]; m_ocurrentstate.enter (); // enter this status }}/// <summary> // enter the default status. The default status is logon. /// </Summary> Public void enterdefaultstate () {changegamestateto (gamestatetype. gs_login);} public void fixedupdate (float fixeddeltatime) {If (m_ocurrentstate! = NULL) {m_ocurrentstate.fixedupdate (fixeddeltatime) ;}} public void Update (float fdeltatime) {gamestatetype nextstatetype = gamestatetype. gs_continue; If (m_ocurrentstate! = NULL) {nextstatetype = equals (fdeltatime);} If (nextstatetype> gamestatetype. gs_continue) {changegamestateto (nextstatetype);} public igamestate getstate (gamestatetype type) {If (! M_dicgamestates.containskey (type) {return NULL;} return m_dicgamestates [type] ;}}
We abstract all public method attributes in the State: igamestate interface:
It also complies with the interface-oriented programming principles. Benefits I have mentioned before.
Igamestate:
Using unityengine; using system. collections; using game. common; // <summary> // game Status Interface /// </Summary> Public interface igamestate {gamestatetype getstatetype (); // obtain the status type void setstateto (gamestatetype gstype) of the Implementation class; // set the status type of the implementation class (change the status) void enter (); // enter the status of the implementation class, perform logic processing of the main status gamestatetype Update (float fdeltatime); // status change, return the changed status void fixedupdate (float fixeddeltatime); // fixed Update Status void exit (); // exit this status}
Then write the sub-implementation class that implements the interface. Here we first write loginstate:
Using unityengine; using system. collections; using game. common; // <summary> // log on to the game status /// </Summary> public class loginstate: igamestate {gamestatetype m_estateto; Public loginstate () {} public gamestatetype getstatetype () {return gamestatetype. gs_login;} public void setstateto (gamestatetype statetype) {This. m_estateto = statetype;} public void enter () {setstateto (gamestatetype. gs_continue); loginctrl. singleton. enter ();} public void exit () {loginctrl. singleton. exit ();} public void fixedupdate (float fixeddeltatime) {} public gamestatetype Update (float fdeltatime) {return m_estateto ;}}
Then, add the gamestatemanager constructor to the cache dictionary.
OK, this state machine. If we call changestateto of gamestatemanager (the desired state type), we will execute the enter method of the desired state implementation class.
Therefore, we write the login logic in the enter field of loginstate. Because loginstate mainly changes the management status, we put all the login logic in the: loginctrl control class for the single responsibility of the class.
Public void enter () {setstateto (gamestatetype. gs_continue); // first go to the intermediate state loginctrl. Singleton. Enter ();}
Loginctrl:
OK, write it here. When we know that the version update is complete or there is no need to update it, the user name input box for logging on to the UI is displayed.
Therefore, we have to write a loginwindow to manage the login UI, so first write loginwindow:
Using unityengine; using system. collections; using game. common; Using utility; using system; // <summary> // logon Interface UI /// </Summary> public class loginwindow: basewindow {public loginwindow () {This. mresname = "guis/loginwindow"; this. mresident = false;} public override void Init () {eventcenter. addlistener (egameevent. egameevent_loginenter, show); eventcenter. addlistener (egameevent. egameevent_loginexit, hide);} public override void realse () {eventcenter. removelistener (egameevent. egameevent_loginenter, show); eventcenter. removelistener (egameevent. protected, hide);} protected override void initwidget () {} protected override void realsewidget () {} protected override void onaddlistener () {} protected override void onremovelistener () {} public override void onenable () {} public override void ondisable () {} public override void Update (float deltatime) {base. update (deltatime );}}
OK. we can add the display logon interface and hide logon interface event listening in loginwindow.
First, we know that when playing LOL, when we log in successfully, we also need to select the game region server. Therefore, we classify the selected server into the login interface for management.
Therefore, we still need to create a selectserver interface. Here I also casually engaged in the following interface:
Not complete ...... No time to continue
Sixth day of development of the project similar to LOL