Welcome back!

Last time we discussed the Entity Component system. We will use it as a fundamental building block in our game. Today we are about to prepare yet another crucial piece of almost any game: the Game Loop.

Feel free to switch to the _ecs_ branch of the repository. It contains the working result of the previous posts and is a great starting point for this one.


Table of contents

  1. Why do we need a Game Loop?
  2. What is a Game Loop?
  3. Updatable Entity and Component
  4. Let the Game Begin!
  5. Conclusion

Why do we need the Game Loop?

When we develop an application that should be executed in a browser, we rely on our users heavily. In fact, the almost entire data flow of an application depends on the user’s input: “click here,” “type in there,” “navigate to that page,” and so on.

Image for post

In a game, user input is essential as well. After all, the game makes zero sense if nobody plays it. But games as software have a whole lot going on regardless of user input.

The game we are building in this series is a turn-based game. It means that each player makes a move one after another, not simultaneously. AI controls one of the players. When the human player ends their move, the game doesn’t “stop.” Ships are still flying; AI makes its calculations, a timer is ticking, and so on. Plenty of processes are happening; some of them are visible to the player, like animations and timers. Some of them are hidden. In any case, they are independent of the user’s input. Contrary to the business applications, where almost everything freezes waiting for users to start interacting with it, games, in a sense, live their own lives.

Considering all this, we must have a way to keep the game moving even if a human player does nothing. And Game Loop exists precisely to help us with that.


What is a Game Loop?

Image for post

The idea is simple. We run an endless loop that notifies every piece of the game about the start of every iteration. The game loop itself does not apply any functionality or logic. It merely says, “yet another moment has passed.”

Every element of the game being notified deals with that information how it sees fit. Ship animation, for example, may slightly change the position of the space ship in the world. AI engine may consider starting a new cycle of calculations. Health or Mana of RPG character may restore a bit

It is vital to understand that this is an endless, always running loop. With regard to the performance, all operations of all elements must be as lightweight as possible.

What kind of elements should receive notification about a new iteration? The loop may not know in advance, and frankly, it does not care. In the previous chapter, we utilized a powerful architectural approach: Entity Component System. We can now extend it to cooperate with the Game Loop. This way, every element of the game that follows ECS will be updated by the loop. Space ships will fly; enemies will be able to think, mana will be restored!

#design-patterns #javascript #algorithms #typescript #game-development #algorithms

Gamedev Patterns and Algorithms with TypeScript.
3.70 GEEK