Frame synchronous system of the mobile Tankedaizhan

The festival talked about matching system, after the match finished, we need to start the frame sync. However, note that only homeowners just beginning frame synchronization method can be called. Thus, the need to add a layer of judgment.

file

Then, the other players in the room needs to listen to the beginning of the frame synchronization of the broadcast, after receiving the message start frame synchronization, enter the game scene.

file

game_scene: The main game scene. (Finally to the most critical part of the logic)

First, after entering the game the main scene, some players initialization (such as location, blood, attack, defense and other information), as well as rocker initialization.

Note that, the player's position in this part of the logic born a little more complicated. Because there is no logical server support, we can only think of ways on the client. So, I pre-defined groups, a total of six position players. Then, in the server room pass over the players are grouped, and finally the players are good maps and location information within the two groups on it. This part of the method GameScene.ts logic initPlayerPoss's.

Divided into left and right rocker rocker rocker. Left joystick to control the position and direction of the tank, and the right stick to control the direction of the head tank guns and shooting. (Tanks, artillery direction of head orientation and tanks independently of each other, need to take note)

After completion of all initializing operation, it is necessary frame message processing logic and the transmission frame message. Code for receiving the main frame of the message and sending the message frame

file

After each message frame is processed, put the information into the logical frame synchronizing presentation layer, and then sends the message to the server the current frame.

Before sending the message frame to the server, I was listening to every rocker sends are subject to change. This will cause frame message is sent too frequently, there is no need to do so. In addition, also lead to a fatal problem, the message frame sequence disorder. Because the frame message is sent asynchronously, there is no way to guarantee which message is first come, after which the. Therefore, the tank will always lead to sometimes move not stop (if I was moving according to isMoving judgment). Look at the following screenshot, timestamp order has been reversed, leading to the final ismoving should be false, and now it is true.

file

处理帧消息是逻辑层和表现层分离的。逻辑层的逻辑都在GameState.ts模块里。逻辑帧帧率默认15,也就是1秒钟有15个帧消息。处理每帧的消息在calFrame方法里。需要注意的是,处理每帧消息之前,都需要初始化一个随机种子,方便游戏中有些逻辑需要用到随机的地方,比如地图随机生成道具等。(计算机中的随机数都是伪随机的,只要随机种子指定的一样,随机的结果也是一模一样的)

逻辑层每一帧处理完之后,会计算出当前帧最终玩家坦克所在的位置,然后拷贝到表现层。

file

表现层,是用每个类的update来驱动的。以玩家类Hero.ts为例。

表现层根据当前的坦克位置和逻辑层传过来的当前帧的最终位置,做一个插值运算,以平滑处理坦克的移动。这部分逻辑参看 Hero.ts 里的updatePosition方法和update方法。主要思想就是,计算出来当前位置到最终位置所用的总时间,把时间跑完,也就走到了最终的位置。插值时,需要传入一个所用时间除以总时间的比值。这样,就会让表现层表现移动更平滑一些。虽然,逻辑层是固定15帧,但是表现层是按渲染帧来的。

说明一下:

  1. 项目代码可能看起来比较多。其实,有很多方法和ts类都没用到,那是之前写的逻辑。所以,没必要所有代码都需要看,只需要按照我说的思想一步步往下看即可。重要的代码,主要看这几个就可以了:StartScene.ts,对应start_scene场景
    HomeScene.ts, 对应home_scene场景
    ChooseScene.ts,对应choose_scene场景
    GameScene.ts,对应game_scene主场景
    GameState.ts,帧同步逻辑层
    Hero.ts,坦克移动的表现层
    moveSticker.ts,坦克移动监听脚本

  2. About frame synchronization of floating-point operations, in fact, no special treatment, simply multiply by 1000. Some places that need to be all over the place are converted into integers, including location information. For floating-point operations, I still have a lot of questions, such as conversion to an integer, and sometimes may be out of range; converted to an integer, floating-point calculation of the time it does not appear, it should be how to do. There are places that float problem does not become a bottleneck, war game, had a very short time bureau, a bureau could one thousand million hands appears a Board of sync.

  3. Moving smooth interpolation algorithm is used. This need to ensure that each frame can arrive at the client in time, then the position of the frame synchronization logic to smooth the presentation layer. When bad network status, may also appear Caton phenomenon. At present, we have not found a better way. (Dead reckoning still need to look at)

Guess you like

Origin www.cnblogs.com/starry-skys/p/11980418.html