G
G
Georgy Pelageikin2015-06-19 16:38:31
Programming
Georgy Pelageikin, 2015-06-19 16:38:31

How to design a "spaceship"?

Hello, it 's me again . There is a spaceship. Ship class . It contains an array of ship compartments. Hardpoint class . Equipment can be installed in each compartment. There is such a scheme - IEquipment - a base abstract class that defines general parameters like mass, size, etc. Descendants of IEquipment - equipment types, Generator , Engine , etc. Well, their specimens lying in the compartments are specific models of this equipment.
The problem is in their operation. A simple example - the engine determines the speed of the ship (physics, sorry). Accordingly, in order for external scripts (movements in this case) to recognize the speed of the ship, we had to place the Speed ​​property in the main class (Ship), which goes through all the engines on the ship and reads the resulting speed from them. The problem is that in the course of development a lot of new properties are added and all this hangs in the Ship. Is this normal or is it bad architecture and normal people do things differently? In general, I ask the gurus to analyze this and make a verdict.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene, 2015-06-19
@ArXen42

Use the stat system. For example you have a class

enum StatType {
   speed;
   power;
   mobility;
}

class StatManager {
    // Выставляет начальное значение стата
    void setStateBase(StatType type, float value);
   // Добавляет новый модификатор для стата
    void addStatModf(StatType type, StatModf modf);
    // Удаляет модификатор стата
    void removeStatModf(StatType type, StatModf modf);
    
    // Возвращает значение стата со всеми повешенными на него StatModf 
    Float getStateValue(StatType type);
}

enum StatModfType {
   set;
   add;
   mult;
}

class StatModf {
   var StatModfType type; // как именно модификатор влияет
   var Float value;  // значение с которым он влияет
}

When executing getStateValue, loop through all modifiers for that stat to the base value.
I recommend applying modifiers in the order of their type (StatModfType) first set then mult, then add and not in the order of addition. Here the simplest option is proposed, think about it and rework it to fit your needs.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question