📜 ⬆️ ⬇️

The architecture of the game client multiplayer Tower Defense. Christmas story

Hi, Habr!

Some may remember that half a year ago I wrote about how we wrote down a prototype multiplayer game in six months .
According to the results of the voting, then the audience decided that it was worth writing a sequel about the architecture of the game. Under the cut, the second series in the now undeservedly forgotten genre of production drama, a lot of pictures, video of the actual gameplay at the moment, and the New Year cat.

Summary of the previous series


Sometime at the end of December 2014, a team of four people decided to create a clone of one of the most popular mods for WarCraft 3 - Legion TD. Having collected the simplest prototype for six months in their free time, the young programmers suddenly realized that they badly needed artists, modelers and designers. Having entered the spamming path of spam in specialized communities and groups, while simultaneously rewriting the game server on Go, they managed to draw to the dark side a whole group of remarkable experts from the FSU. At that time, far beyond the ocean, a similar clone for three years was developed by a simple American boy Brent "Lisk" Batas. He had good artwork and almost zero code, but refused to cooperate with our heroes. Even the a11aud 'server-client bundle demonstrated during a business trip to San Francisco did not convince the American. It was decided to continue the development based only on their own strength. Modelers, texture designers and designers worked hard styluses to have time to roll out the MVP by the appointed date - the eve of the new 2016. And in general, we succeeded.
')

And what is actually done?


Total to date, together we have the Tower Defense with the first game race, constantly improving graphics:

Orc in armor

Steam drone

Self-propelled turret

Goblin Mechanic

Alchemist


But back to the promised topic. I will try to give the main design solutions that have confirmed their worth. To many, they certainly seem obvious, but believe me, as it turned out, this is not quite so. The low threshold for entry into Unity makes even the most childlike design mistakes possible.

First thought


If the game state is described by a complex serialized data structure, as in our case, you should always have easy access to any information that interests you. Since our project is somewhat more complicated than the casual mainstream, ready-made Unity recipes do not suit us. Due to the fact that we used our own technology stack, serialization and deserialization are not done in two accounts. Let me remind you that in our project, the server sends eight players in the delta state of the game in json format 10 times per second. As a solution, we wrote a singleton, which parses server messages, updates the game state on the client and provides a convenient interface to any game data.
With this approach, obtaining the necessary information for different components of clients, for example, coordinates for the unit manager and mini-map is not difficult.
No matter how great the temptation is to hang all the functionality on such a class up to
StateHub.Instance.GetLastKilledWithBlackMagicDwarfUID() 
Be prudent, remember the God Object. Apparently Sergey Sychev meant this in his speech on OOP in Unity at the last meeting of the Unity User Group in St. Petersburg (I will add a link to the report when it will be published) when I urged not to use singletons at all. But it seems to me that a sense of proportion and common sense will tell you when to stop.

Second thought


In our case, it makes sense to use the event model. In the "Legion" there is the concept of phase. There is a construction phase, a combat phase, an arena battle phase. As mentioned above, the server sends status updates 10 times per second. We generate events of arrival of the new message and phase change. This is quite enough to simply and logically organize the instantiation of units, the assembly of corpses, the switching of musical themes and other utilitarian operations. Since our game client does not calculate any game logic and is essentially just a “TV with remote control,” these few events are enough.

Third thought


Separate MonoBehaviour as View and directly game information for units. The thing is trivial, but as it turned out, quite a few Unity programmers write almost all the code in one script. This is not a big deal in a small project, but if you are thinking of something more serious than a flapiberd, better think about it beforehand.

Before and after


Half a year ago

Now

There is a lot of work, of course, but progress is evident.

In the following series


Brent "Lisk" Batas announced his intention to go to crowdfunding by the end of February and start a closed beta test by the end of 2016.
Our team of Spiced Jackal plans to continue developing, searching for investments, promoting the game called KrownGuards and catch up ahead of competitors. These are the results of our hard work year. We want to thank everyone who worked and works with us and congratulate everyone on the upcoming new year. See you in 2016!
PS We will be happy to answer questions, if any.

Source: https://habr.com/ru/post/274367/


All Articles