G
G
Georgy Pelageykin2016-09-22 23:22:52
OOP
Georgy Pelageykin, 2016-09-22 23:22:52

What is the best division of responsibility?

Good day. Such a situation: there is a certain device in the game that can consume some power and a generator that can produce it. The consumption level can vary from min to max and 0 (off), there is control from the GUI, for example.
The device received a command to operate at 20 MW, and only 15 MW is available from the generator. There are two options:

  • The device will throw an exception. This means that the conditional interface must independently check the amount of energy, or at least "ask" the power distributor if it is enough, and if so, then let the user move the slider further away.
  • The device will automatically avoid overload and supply itself with the maximum possible power, in this case 15MW, and, of course, it will trigger some PowerLevelChanged event. The GUI will be updated on this event, as a result, the player will not be able to move the slider further - it will be thrown back.

Similarly, with on / off - you can simply not turn it on if the available power is less than min, or you can throw an exception "what you turn on, there is no power."
In the project, there was a confusion of these two approaches - in one place I throw an exception, in another I "keep silent" - but which is better in terms of design principles, etc.? I myself tend to the second approach, because. it seems simpler and more concise (for example, the GUI will "self-balance" without any additional checks on its part), but it became interesting how people do it.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
N
Neron, 2016-09-23
@ArXen42

Depends on the type of game. If this is an economic strategy, with an emphasis on resource management AND electricity is one of the resources taken into account, then you always need to give the user a choice of how to dispose of them, no automation. If the amount of electricity is not taken into account, you can arrange full automation so that the device itself manages it. As an example, the game PlanetBase - you need to support a colony on an alien planet and build, among other things, electric generators and electric storage. The user can turn off consumers, and that's all he can do - if there is not enough electricity, then the connected consumers are immediately de-energized, and when the generators resume operation, all connected consumers automatically begin to suck energy from the batteries. Done like this

L
lega, 2016-09-23
@lega

If a "compromise" is acceptable, then the 2nd option, if not, then the 1st. In general, it depends on the task.

A
AtomKrieg, 2016-09-23
@AtomKrieg

You need to do as the game designer orders

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question