Side-board fighting game combat: event subscription and publishing system

During the game battle, the player is killed by a monster, and then the value of the UI health bar on the interface will change, as shown in the figure: 1.7-1

(Figure 1.7-1)

According to the general processing method, after we lose blood in the battle logic, we need to find a node in the UI interface and set the value for it. One search is fine. If the value needs to be changed in multiple places, multiple nodes need to be changed. Players battle logic, it is impossible to search for each node and then set it up, which is not flexible and not easy to expand. This is equivalent to the player Player object coupled with these objects to update the display. So you need to write a module to decouple, this module is the event subscription and publishing module. In view of the above situation, how to decouple it?

How to design the event subscription and publishing module? Create a new singleton event management component class EventMgr, which is instantiated on the GameApp node. Figure 1.7-2

(Figure 1.7-2)

events_map is a table that stores which listeners are interested in which events. When there is data in events_map, the data structure is as follows: { "event name 1": [listener 1{caller, func}, listener 2], "event name 2": []}

Next, let's take a look at the listener event interface provided to the listener as shown in Figure 1.7.3:

(Figure 1..7-3)

When we want to listen to an event, we pass in the name of the event, the this object (caller) of the callback, and the callback function func.

Next, let's see how to dispatch and publish an event, as shown in Figure 1.7.4:

(Fig. 1..7-4)

Today's tutorial sharing is here. For the specific code, you can read EventMgr.ts, read the code again, and completely master the writing and implementation of the event subscription and publishing module.

Supongo que te gusta

Origin blog.csdn.net/voidinit/article/details/126698865
Recomendado
Clasificación