Help please understand the organization of the code in the simplest game. and say as a result:
correct first option
correct second option
both options are correct and used depending on the situation
The pattern% patternname% has long existed and is used successfully in this situation.
Suppose there is a 2D landscape, a player’s ship is flying over it. The ship can fire missiles.
I describe the first variant of the organization of the OOP code in js (in js the controller and presentation are combined):
Initialization of the game begins with the creation of spaceView, which in turn initializes:
landscape,
player ship
PlayerShipView - player ship. He can shoot a rocket (PlayerRocketView). That is, a ship object is associated with a rocket object.
The rocket may collide with the landscape. That is, the rocket object is associated with the spaceView object.
As a result:
We have 3 related objects, but each of them contains only its own functionality (this is a plus).
But if the program has to change something (for example, to disable the player’s ship to fire missiles), then you will have to make edits in different places of the program (this is a minus).
There are several global variables (this is a minus)
I describe the second variant of the organization of the OOP code in js (in js the controller and presentation are combined):
Initialization of the game begins with the creation of spaceView, which in turn initializes:
landscape,
player's ship
missiles (there is a fire button pressed)
and generally all other objects
In this way:
If for example the player presses "fire", then the rocket initialization takes place in spaceView, and PlayerShipView does not know anything about the rocket object (this is a plus).
Accordingly, there are no global variables (this is a plus).
But spaceView contains a lot of logic (the whole game, in which there are many other objects besides the player’s ship and the rocket).
For clarity, I implemented the first version here: https://github.com/zlodiak/space-1
It uses various libraries such as backbone, underscore, but ignorance of them does not interfere with the understanding of the overall structure of the program.
Do not hit (kicked), I know what a question for telepaths