Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!
Werbeanzeige
C-/C++-Quelltext |
|
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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 |
#ifndef _RPGCHARACTER_H_ #define _RPGCHARACTER_H_ /// \namespace rpgLib /// \brief The only namespace of this Library. Holds all classes. namespace rpgLib { /** * \brief The Event-Listener for the Class rpgCharacter */ class rpgICharacterListener { public: /// \brief This method is called when the Character dings /// \param level The level, the character achieved. virtual void onLevelUp(const uint level) = 0; }; /** * \brief This class represents a Character. * * * While the name "Character" is commonly used * for the PC (PlayerCharacter), we use it for every * Character in a RPG-Game. It may be a PC, a NPC or even * a monster. */ class RPGLIB_CALL rpgCharacter { public: typedef std::list<rpgCharacterAttribute> rpgAttributeList; /// \brief Default Ctor rpgCharacter(); /// \brief Copy-Ctor /// \param c A Reference to the class to copy rpgCharacter(const rpgCharacter& c); /// \brief Default DeCtor virtual ~rpgCharacter(); /// \brief Sets the Listener for the Character. /// \param l A pointer to a class, implementing ICharacterListener void setCharacterListener(rpgICharacterListener* l); /// \brief The Character receives EXP /// \param exp The amount of EXP the Character gained. void receiveEXP(const uint exp); /// \brief Returns the current Experience-Points. inline int getEXP() const { return m_exp; } /** * \brief Sets the offset for the experience per level * \param offset The offset to use for this Character. * * This specifies the EXP-curve. * Example: If you set this offset to 1000, the Character * will need 1000*level EXP to ding. * You can change it anytime, to slow/fasten dings. * \note foo */ void setEXPOffset(const float offset); /// \brief Returns the current EXP-Offset inline float getEXPOffset() const { return m_expOffset; } /// \brief Returns the currentLevel inline uint getLevel() const { return m_level; } /// \brief Overloaded operator=() rpgCharacter& operator=(const rpgCharacter& c); /// \brief Returns the current Mana inline uint getMana() const { return m_mana; } /// \brief Sets the Manaoffset /// \param offset The offset to use. void setManaOffset(const uint offset); /// \brief Returns the current Hitpoints inline uint getHitPoints() { return m_hitpoints; } /// \brief Sets the currerent HitPoint-Offset /// \param offset The offset to use. void setHitpointsOffset(const uint offset); /** \brief Sets the Attribute responsible for the Hitpoints * \param attrib A pointer to a valid rpgCharacterAttribute * * The attribute will be multiplied with the Offset, to calculate * the actual amount of hitpoints. If this is NULL, the current level will * be multiplied with the offset. */ void setHitpointAttribute(rpgCharacterAttribute* attrib); /** \brief Sets the Attribute responsible for the Manapoints * \param attrib A pointer to a valid rpgCharacterAttribute * * The attribute will be multiplied with the Offset, to calculate * the actual amount of mana. If this is NULL, the current level will * be multiplied with the offset. */ void setManaAttribute(rpgCharacterAttribute* attrib); /// \brief Returns how much of the current level is done, in percent. float getPercentLevel(); /** \brief Adds an Attribute to the internal List * \param attrib A Reference to a valid rpgCharacterAttribute */ void addAttribute(rpgCharacterAttribute& attrib); /** \brief Returns a Attribute by ID from the list. * \param id The id to search for. * \throw std::exception if the attribute is not found. */ rpgCharacterAttribute& getAttribute(const int id); protected: /** \brief Checks for level up * * Normally, you don't need to call this method by yourself. * It is called everytime we receive EXP. */ virtual void checkEP(); /** \brief Calculates the Mana. * * This Method calculates the Mana of the character. * You may overwrite it, if you don't like the way we do it. * \see Techpaper "Calculations" for details. */ virtual void calcMana(); /** \brief Calculates the Hitpoints. * * This Method calculates the Hitpoints of the character. * You may overwrite it, if you don't like the way we do it. * \see Techpaper "Calculations" for details. */ virtual void calcHP(); private: // Experience long m_exp; float m_expOffset; // Level int m_level; // Mana & HP uint m_mana; uint m_hitpoints; uint m_manaOffset; uint m_hpOffset; rpgCharacterAttribute* m_manaAttribute; rpgCharacterAttribute* m_hpAttribute; // Listener rpgICharacterListener* m_listener; // List rpgAttributeList m_attribList; }; }// NameSpace #endif /* _RPGCHARACTER_H_ */ #ifndef _RPGCHARACTERATTRIBUTE_H_ #define _RPGCHARACTERATTRIBUTE_H_ namespace rpgLib { /** * \brief This is a class for a Attribute. * * Much RPGs use different Styles of Attribute-Sets, * some don't use them at all. With the help of this class * you can easily add them to our rpgCharacter-Class. */ class RPGLIB_CALL rpgCharacterAttribute { public: /// \brief Default Ctor rpgCharacterAttribute(); /// \brief Default DeCtor ~rpgCharacterAttribute(); /// \brief Default Copy-Ctor rpgCharacterAttribute(const rpgCharacterAttribute& c); /// \brief Ctor with Values /// \param name The Name for the Attribute, e.g. Strength /// \param id The ID for the Attribute. /// \param value The initial value of the Attribute /// \param offset This value will be added every levelup. rpgCharacterAttribute(const rpgString& name, const uint value, const uint offset, const int id = -1); /// \brief Sets the Name of the Attribute /// \param name The new name. void setName(const rpgString& name); /// \brief Sets the Value of the Attribute /// \param value The new Value. void setValue(const uint value); /// \brief Sets the offset per Level. /// /// Each time you level up, this value will be /// added to the attribute. void setOffset(const uint offset); /// \brief Sets both Attributes /// \param name The Name for the Attribute, e.g. Strength /// \param id The ID for the Attribute. /// \param value The initial value of the Attribute /// \param offset This value will be added every levelup. void set(const rpgString& name, const uint value, const uint offset, const int id = -1); /// \brief Returns the Name of the Attribute inline rpgString getName()const { return m_name; } /// \brief Returns the Value of the Attribute inline uint getValue()const { return m_value; } /// \brief Returns the offset of the Attribute inline uint getOffset() const { return m_offset; } /// \brief Overloaded operator= rpgCharacterAttribute& operator=(const rpgCharacterAttribute& c); /// \brief Increments the Attribute by offset. void doLevelUp(); /// \brief Sets the ID of the Attribute. inline void setID(const int id) { m_id = id; } /// \brief Returns the current ID. inline int getID() const { return m_id; } private: rpgString m_name; uint m_value; uint m_offset; int m_id; }; }// namespace #endif /* _RPGCHARACTERATTRIBUTE_H_ */ void rpgLib::rpgCharacter::addAttribute( rpgCharacterAttribute& attrib ) { m_attribList.push_back(attrib); } rpgLib::rpgCharacterAttribute& rpgLib::rpgCharacter::getAttribute( const int id) { rpgAttributeList::iterator it_end = m_attribList.end(); rpgAttributeList::iterator it_begin = m_attribList.begin(); while (it_begin != it_end) { if (it_begin->getID() == id) { return *it_begin; } else { it_begin++; } } throw std::exception("Attribute not found!"); } rpgLib::rpgCharacterAttribute::rpgCharacterAttribute() : m_value(0), m_offset(0), m_id(-1) { } rpgLib::rpgCharacterAttribute::rpgCharacterAttribute( const rpgCharacterAttribute& c ) { m_name = c.getName(); m_value = c.getValue(); m_id = c.getID(); m_offset= c.getOffset(); } rpgLib::rpgCharacterAttribute::rpgCharacterAttribute( const rpgString& name, const uint value, const uint offset, const int id) { set(name, value, offset, id); } rpgLib::rpgCharacterAttribute::~rpgCharacterAttribute() { } void rpgLib::rpgCharacterAttribute::setName( const rpgString& name ) { m_name = name; } void rpgLib::rpgCharacterAttribute::setValue( const uint value ) { m_value = value; } void rpgLib::rpgCharacterAttribute::set( const rpgString& name, const uint value, const uint offset, const int id) { m_name = name; m_value = value; m_offset= offset; m_id = id; } rpgLib::rpgCharacterAttribute& rpgLib::rpgCharacterAttribute::operator=( const rpgCharacterAttribute& c ) { m_name = c.getName(); m_value = c.getValue(); m_offset= c.getOffset(); m_id = c.getID(); return *this; } void rpgLib::rpgCharacterAttribute::setOffset( const uint offset ) { m_offset = offset; } void rpgLib::rpgCharacterAttribute::doLevelUp() { m_value += this->getOffset(); } |
C-/C++-Quelltext |
|
1 2 3 4 |
rpgCharacterAttribute m_strength(...); myCharacter.addAttribute(m_strength); myCharacter.getAttribute(ID_STRENGTH).setValue(2000); myCharacter.getAttribute(ID_STRENGTH).setOffset(2000); |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 |
#ifdef _UNICODE # define _T(x) L ## x # define _TEXT(x) _T(x) typedef std::basic_string<wchar_t> rpgString; #else # define _T(x) x typedef std::basic_string<char> rpgString; #endif /* _UNICODE */ |
Quellcode |
|
1 |
o Methoden als inline zu deklarieren welche in der Funktionsdefinition definiert werden ist total unsinnig. |
Zitat
o Primitive Datentypen bei Parametern als const zu deklarieren ist genauso unsinnig
Zitat
o Referenzen sind Zeigern tunlichst vorzuziehen.
Zitat
o Copy c'tor & Assignment Operator machen keinen Sinn wenn nicht tief kopiert werden muss. Aber das hatten wir ja bereits, genau wie...
Zitat
o ... leere Standardkonstruktoren und/oder leere Destruktoren witzlos sind.
Zitat von »"Nox"«
Ich würde ja mal per Debugger schauen, was für Werte in SetOffset angezeigt werde. Vielleicht hilft das weiter.
Zitat von »"Nico"«
Quellcode
1 o Methoden als inline zu deklarieren welche in der Funktionsdefinition definiert werden ist total unsinnig.
Unsinnig ist das nicht. Hat was mit der Doku zu tun. Ausserdem ändert es nix am Verhalten des Codes.
Zitat von »"Nico"«
Zitat
o Primitive Datentypen bei Parametern als const zu deklarieren ist genauso unsinnig
Nein ist es nicht. Es zeigt dem Verwender der Api, dass der Wert nicht mehr geändert wird. Ausserdem ist eine Art Schutz für den Programmierer selber. Man kann nicht aus versehen eine Variable ändern, für die das nicht vorgesehen ist.
Zitat von »"Nico"«
Zitat
o Referenzen sind Zeigern tunlichst vorzuziehen.
Auf was bezieht sich das? Und warum?
Zitat von »"Nico"«
Zitat
o Copy c'tor & Assignment Operator machen keinen Sinn wenn nicht tief kopiert werden muss. Aber das hatten wir ja bereits, genau wie...
Es macht aber auch nichts sie trotzdem zu definieren und zu implementieren.
Zitat von »"Nico"«
Zitat
o ... leere Standardkonstruktoren und/oder leere Destruktoren witzlos sind.
Mein Standardkonstruktor ist nicht leer. Die Variablen werden per Initialisierungsliste auf einen Wert festgelegt.
Zitat von »"Nico"«
Über leere Destruktoren kann man sich jetzt streiten, mein Klassengenerator legt es halt an... seh hier keinen fehler. Und falls ich ihn doch noch brauche, habe ich den zur Hand.
Zitat von »"Nico"«
Das Fehlverhalten äussert sich darin, dass zum Beispiel der Offset nach dem ändern plötzlich einen bogus-Wert enthält, wie 3456789, obwohl er auf beispielsweise 20 oder 2000 etc gesetzt wurde.
Rebuild hab ich bereits durchgeführt.
lg
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
void foo( int bar) { if (bar = 1) { foobar(); } } // Ein wenig an den Haaren herbeigezogen, ich weiss, aber nur ein Beispiel. // So wäre das nicht passiert: void foo (const int bar) { if (bar = 1) // -> Compilerfehler, const kann nicht verändert werden { foobar(); } } |
Zitat
Auf manche deiner Methoden und weil Referenzen weniger gefährlich benutzt werden können als Zeiger und oftmals ausreichend sind! In allen anderen Fällen kann man immer noch auf Zeiger zurückgreifen.
Zitat
Wenn das so ist. Aber über einen leeren Desturktor kann man sich nicht streiten, der ist sinnlos!
Zitat
Nein das nicht. Aber siehst du einen tieferen Sinn darin das zu tun?
Zitat
Es ändert nichts am Verhalten, aber es ist unsinnig! Und wo sowas mit Dokumentation zu tun hat ist mir schleierhaft.
Werbeanzeige