What is the real system framework for game development? (1)

Source: Internet
Author: User
What is the physical system framework of game development? (1)

 

Link: http://www.richardlord.net/blog/what-is-an-entity-framework

The original text is very long. The translation will be separated into several parts. This is the first part.

 

 

Last week, I released ash (an entity system framework developed by an ActionScript game). Many friends later asked me, "What is an entity system framework )?". The following is a very long answer to this question ...) :

 

Entity systems are becoming increasingly popular, such as unity, ember2, xember, and my own ash. The reason for their popularity is simple: simple game frameworks, concise code modules, and simple use.

 

This article will show you how the traditional game logic (game loop) evolves into an Entity Framework. Currently, I am using ActionScript for development. Therefore, all the instance code in this article uses ActionScript, but this framework applies to all languages.

 

Examples

Throughout the full text, I will use a simple asteroids game as an example. asteroids is like a simplified version of a large game. Although very small, it contains a typical system: refresh the system, physical system, AI system, input system, and NPC system.

 

The game loop)

To understand why the physical system is used, first we need to understand how the traditional game loop works. The main cycle of the asteroids game may work like this:

 

function update( time:Number ):void{  game.update( time );  spaceship.updateInputs( time );  for each( var flyingSaucer:FlyingSaucer in flyingSaucers )  {    flyingSaucer.updateAI( time );  }  spaceship.update( time );  for each( var flyingSaucer:FlyingSaucer in flyingSaucers )  {    flyingSaucer.update( time );  }  for each( var asteroid:Asteroid in asteroids )  {    asteroid.update( time );  }  for each( var bullet:Bullet in bullets )  {    bullet.update( time );  }  collisionManager.update( time );  spaceship.render();  for each( var flyingSaucer:FlyingSaucer in flyingSaucers )  {    flyingSaucer.render();  }  for each( var asteroid:Asteroid in asteroids )  {    asteroid.render();  }  for each( var bullet:Bullet in bullets )  {    bullet.render();  }}

 

 

This main loop is called cyclically at a specific cycle to update the game status, usually 60 times/second or 30 times/second. Generally, in the main loop, we will deal with various important game logic, such as updating various items in the game, detecting collisions between them, and drawing.

 

The above code is a very simple main loop, because:

1. The game itself is simple

2. The game has only one status

 

In the past, I used to insert more than 3000 lines of code into a main loop function of a video game. This makes it look not elegant at all, not concise at all. This is the way we used to write games, and we had to spend the rest of our lives.

 

The physical system framework originated from an attempt to reconstruct the main loop of the game. It assumes that the main loop of a game is the core of a game, and simplifying the main loop of a game in a modern game framework is more important than anything else, for example, separating view and control.

 

Process)

The first step of evolution is to recall the process of calling a module (think about objects called processes ). This module can be initialized, updated, and destroyed. This process interface may look like this:

interface IProcess{  function start():Boolean;  function update( time:Number ):void;  function end():void;}

We can simplify the main loop by decomposing different processes, such as rendering, motion, and collision processing. Then we can create a Process Manager to manage these processes.

class ProcessManager{  private var processes:PrioritisedList;  public function addProcess( process:IProcess, priority:int ):Boolean  {    if( process.start() )    {      processes.add( process, priority );      return true;    }    return false;  }  public function update( time:Number ):void  {    for each( var process:IProcess in processes )    {      process.update( time );    }  }  public function removeProcess( process:IProcess ):void  {    process.end();    processes.remove( process );  }}

This is a simple process manager. Here, we must ensure that the calls of each process are in the correct order (determined by the priority parameter in the add method), and we must process a process) removed from the update loop. In this case, you may think that if our primary loop is divided into multiple sub-processes, the update method of the Process Manager is equivalent to the previous primary loop, the set of sub-processes becomes the core of the game ).

 

The render Process)

Let's take the rendering process as an example. We can pull the previously drawn code from the main loop of the game and put it into a separate process, which looks like this:

class RenderProcess implements IProcess{  public function start() : Boolean  {    // initialise render system    return true;  }  public function update( time:Number ):void  {    spaceship.render();    for each( var flyingSaucer:FlyingSaucer in flyingSaucers )    {      flyingSaucer.render();    }    for each( var asteroid:Asteroid in asteroids )    {      asteroid.render();    }    for each( var bullet:Bullet in bullets )    {      bullet.render();    }  }    public function end() : void  {    // clean-up render system  }}

 

Interface

However, this is not particularly effective. We still need to manually refresh various types of classes. If we have all the interfaces that can be used to refresh the class, the code will be further simplified.

interface IRenderable{  function render();}
class RenderProcess implements IProcess{  private var targets:Vector.<IRenderable>;  public function start() : Boolean  {    // initialise render system    return true;  }  public function update( time:Number ):void  {    for each( var target:IRenderable in targets )    {      target.render();    }  }    public function end() : void  {    // clean-up render system  }}

Then some code of our spaceship class will become like this:

class Spaceship implements IRenderable{  public var view:DisplayObject;  public var position:Point;  public var rotation:Number;  public function render():void  {    view.x = position.x;    view.y = position.y;    view.rotation = rotation;  }}

The code here uses 2D games as an example, but the principles of 3D games are the same. We need to draw an image, and then we need to draw the position and rotation information required by the image. Then, the render method is used to refresh the implementation.

 

To be continued...

 

 

 

The above is the first part of this article. The time relationship is written here first, and the next article will continue, so stay tuned.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.