Ik speel nu al een tijdje met allerlei programmeertalen met heb toch regelmatig moeite met het bedenken van goede namen voor classes, met name (abstracte) super-classes.
Zijn er ergens richtlijnen voor het bedenken voor namen voor classes en hun methods, met name bij de ontwikkeling van (kleine) games?
Zelf gebruik ik "Entity" als (abstracte) root-class voor alle in-game objecten. Dit is bijv. mijn Entity root-class in een huidig projectje:
Dit is de root-class voor alle objecten die zich kunnen verplaatsen voor in een 2D game van bovenaf. Denk aan kogeltjes, raketjes, tankjes, vliegtuigjes, etc. Er komt nog een superclass boven (dat wordt dus de nieuwe root-class) die ook gebruikt zal worden voor objecten die zich niet (nooit) kunnen verplaatsen, maar daar weet ik nog geen naam voor. Denk aan stenen, gebouwen, muren, etc.
Bovenstaande code is gebaseerd op een rommelig, half "prototype" in Javascript: http://kutcomputers.nl/jsrts/
Verder code- en structuur-gerelateerd commentaar welkom.
Zijn er ergens richtlijnen voor het bedenken voor namen voor classes en hun methods, met name bij de ontwikkeling van (kleine) games?
Zelf gebruik ik "Entity" als (abstracte) root-class voor alle in-game objecten. Dit is bijv. mijn Entity root-class in een huidig projectje:
C++:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
| #ifndef _ENTITY_H #define _ENTITY_H #include "model.h" class Entity { private: // The last known determined coordinate of this entity before the start of // any kind of applicable motion. Coordination is measured in points. EntityCoordinate2D coordinate; // The last known determined orientation of this entity before the start of // ant kind of applicable motion. Orientation is measured in radians. EntityTheta1D orientation; // The current velocity of the entity. The angle is relative to the angle // of the entity, which is relative to the world. Speed is measured in // points per second. Rotation is measured in radians per second. EntityVelocity2D velocity; // The current speed of the angular movement, in radians per second. EntityTheta1D angularSpeed; // The motion-duration of the current velocity and rotation in milliseconds. EntityTime motionDuration; private: // A method to apply the new coordinate and orientation based on the current // active motion (velocity and angular speed) and motion duration. // properties "coordinate" and "orientation" will be updated and property // "motionDuration" will be reset to 0. // This method doesn't affect the "velocity" and "angularSpeed" properties. void applyMotion(); protected: // Construct a new entity based on a coordinate and an orientation. Entity(EntityCoordinate2D coordinate = EntityCoordinate2D(0,0), EntityTheta1D orientation = EntityTheta1D(0)); // Set the velocity of this entity. virtual void setVelocity(EntityVelocity2D velocity); // Set the angular speed of this entity. virtual void setAngularSpeed(EntityTheta1D speed); // Get the maximum speed of this entity in a certain direction. virtual EntityCoordinate1D getMaxSpeedInDirection(EntityTheta1D theta); public: // Get the current coordinate of this entity. Might be calculated if there // is a movement active. virtual EntityCoordinate2D getCoordinate() const; // Get the current orientation of this entity. Might be calculated if there // is an angular motion active. virtual EntityTheta1D getOrientation() const; // Get the dimension of this entity. virtual EntityDimension2D getDimension() const = 0; // Increment the time of this entity by milliseconds. virtual void incrementTime(EntityTime1D time); // Get whether this entity is solid and should be checked for collisions // with other entities. virtual bool isSolid() const = 0; // Get the speed of the angular motion of this entity. virtual EntityTheta1D getAngularSpeed() const {return this->angularSpeed;} // Get the velocity of this entity. virtual EntityVelocity2D getVelocity() const {return this->velocity;} // Apply a transformation (translation + rotation) in a 2D coordinatable. virtual void applyTransformIn(EntityCoordinatable2D * c2d) const; // Apply a translation in a 2D coordinatable. virtual void applyTranslateIn(EntityCoordinatable2D * c2d) const; // Deconstructor virtual ~Entity(){}; }; #endif |
Dit is de root-class voor alle objecten die zich kunnen verplaatsen voor in een 2D game van bovenaf. Denk aan kogeltjes, raketjes, tankjes, vliegtuigjes, etc. Er komt nog een superclass boven (dat wordt dus de nieuwe root-class) die ook gebruikt zal worden voor objecten die zich niet (nooit) kunnen verplaatsen, maar daar weet ik nog geen naam voor. Denk aan stenen, gebouwen, muren, etc.
Bovenstaande code is gebaseerd op een rommelig, half "prototype" in Javascript: http://kutcomputers.nl/jsrts/
Verder code- en structuur-gerelateerd commentaar welkom.
Let op: Mijn post bevat meningen, aannames of onwaarheden