📜 ⬆️ ⬇️

What is wrong with inheritance? Flash game development using the PushButton Engine framework

image
There are not many specialized frameworks that accelerate the development of flash games. Of those that are heard, the following can be noted:

Flixel
Flashpunk
Citrus engine
Pushbutton engine

With this post, I want to draw the attention of fellow flashers to a promising open source engine called PushButton Engine . PBE has already been presented on Adobe Max 2010 and developed on it # 2 Top Facebook game 2010 according to insidefacebook.com . It is used to develop games by companies such as Zynga, Playdom, Hive7.
')
The developers of the engine are quite well-known people in game development circles who created such games as The Incredible Machines, Tribes, Torque and others.

A curious component approach to development is proposed, which, according to the authors, is much more convenient and productive in developing games than traditional object-oriented.

Some of the features of the engine:
Unfortunately, the situation with the documentation leaves much to be desired (there is no information in Russian at all, except for a few official lessons translated by the efforts of Comrade Flashist ). Here they are:

Lesson 1
Lesson 2
Lesson 3
Lesson 4

Well, we will correct the situation. In this introductory article, I will explain how the developers of the PushButton Engine substantiate their component approach, what components are, how and why to use them.

What is wrong with inheritance?


Object-oriented doesn't always work for good. When developing games, situations like the one described below often arise.
Suppose we have created a class that represents the player.

image

Next, we add the class Subject and bring the overall functionality to the parent class of the Game Entity:

image

The game grows, the complexity of the class hierarchies grows with it, but so far everything looks good.

image

But sooner or later, unpleasant questions begin to arise. For example, from whom to inherit the class Moving Item, from the class Subject or from the class Can Move?

image

Most often, no matter what option we choose, in the end we come to an overly bloated base class with a variety of disparate methods that are called by the heirs.

image

The code becomes hard to debug, modify and maintain.

What solution is offered?


The way out in this situation can serve as composition - splitting the game into logical entities that are filled with necessary components. For example, we have 3 entities: Player, Enemy and Subject.

image

Using components, we form entities from the constructor:

image

With this approach, the code becomes more understandable and logical, and once written components can be reused in other games.
This approach does not cancel inheritance, but it is used where it is really suitable, for example, for component hierarchies:

image

How is the component approach implemented in the PushButton Engine?


The code for creating components is in the package.

com.pblabs.engine.entity.*

The entity is a lightweight container of type Entity that implements the IEntity interface. To create, use the factory allocateEntity method:

var myEntity:IEntity = PBE.allocateEntity();

We initialize the created entity, giving it a name, by which we can find it later at any point in our code:

myEntity.initialize("MyEntity");

Is done. It remains to fill it with the necessary components, either provided by the framework itself, or self-written.
A component must always implement the IEntityComponent interface IEntityComponent either directly or indirectly, inheriting from the class EntityComponent or its descendants.

image

After creating and configuring a component instance, we add it to the entity using the addComponent method:

myEntity.addComponent(myComponent, "MyComponent");

As you have probably noticed, each entity or component is assigned a string name. This is necessary so that anywhere in the code we can get them using special lookup-methods:

PBE.lookupEntity
PBE.lookupComponentByName


This concludes the introductory article on PushButton Engine, and the next article will be devoted to the internal architecture of the framework.

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


All Articles