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 |
class Tier { virtual void test() = 0; }; class DummTier: public Tier { int leben; DummTier() : leben(100) {} void test() { std::cout << leben << std::endl; } }; class SchlauTier: public Tier { int hirn; Tier* tier; SchlauTier(Tier* _tier) : hirn(10), tier(_tier) {} void test() { std::cout << tier->leben << ", " << hirn << std::endl; } }; |
Alter Hase
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 |
Tier* tier = new Tier(); //[...] Mutation SuperTier super = new SuperTier(); super->setLebenspunkte(tier->getLebenspunkte()); //[...] noch andere Variablen delete tier; |
Quellcode |
|
1 2 3 4 5 |
class RunBehaviour { public: virtual void run() = 0; }; |
Quellcode |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
class Animal { private: RunBehaviour *rb; public: Animal() { rb = new DefaultRunBehaviour(); } void run() { rb->run(); } void setRunBehaviour(RunBevhaviour *rb) { this->rb = rb; } }; |
Zitat
Dynamisches Vererben geht nicht und macht auch gar keinen Sinn.
Versuchs doch mal mit Komposition.
Oder du erzeugst in dem Moment, wo das Spielobjekt sich verändert, einfach ein neues Objekt.
Alter Hase
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 |
#include<iostream> class Tier { protected: int defence; public: Tier():defence(60){} virtual ~Tier(){} int getDefence(){return defence;} void setDefence(int defence){this->defence = defence;} void mutation(Tier* ziel) { ziel->setDefence(this->defence); } }; class SuperTier : public Tier { protected: int attack; public: SuperTier():attack(70){} ~SuperTier(){} int getAttack(){return attack;} void setAttack(int attack){this->attack = attack;} void mutation(SuperTier* ziel) { ziel->setDefence(this->defence); ziel->setAttack(this->attack); } }; class SuperSuperTier : public SuperTier { protected: int specialeAttack; public: SuperSuperTier():specialeAttack(130){} ~SuperSuperTier(){} int getSpecialeAttack(){return specialeAttack;} void setSpecialeAttack(int specialeAttack){this->specialeAttack = specialeAttack;} void mutation(SuperSuperTier* ziel) { ziel->setDefence(this->defence); ziel->setAttack(this->attack); ziel->setSpecialeAttack(this->specialeAttack); } }; class SuperSuperTierV2 : public SuperTier { protected: int specialeAttack; public: SuperSuperTierV2():specialeAttack(140){} ~SuperSuperTierV2(){} int getSpecialeAttack(){return specialeAttack;} void setSpecialeAttack(int specialeAttack){this->specialeAttack = specialeAttack;} void mutation(SuperSuperTierV2* ziel) { ziel->setDefence(this->defence-10); ziel->setAttack(this->attack+10); ziel->setSpecialeAttack(this->specialeAttack); } }; class EndTier : public SuperSuperTier { private: int specialeDefence; public: EndTier():specialeDefence(150){} ~EndTier(){} void print() { std::cout<<this->defence<<std::endl; std::cout<<this->attack<<std::endl; std::cout<<this->specialeAttack<<std::endl; std::cout<<this->specialeDefence<<std::endl; } }; class EndTierV2 : public SuperSuperTierV2 { private: int specialeDefence; public: EndTierV2():specialeDefence(140){} ~EndTierV2(){} void print() { std::cout<<this->defence<<std::endl; std::cout<<this->attack<<std::endl; std::cout<<this->specialeAttack<<std::endl; std::cout<<this->specialeDefence<<std::endl; } }; int main() { Tier* tier = new Tier(); //Mutation SuperTier* sutier = new SuperTier(); tier->mutation(sutier); delete tier; //weiere Mutation SuperSuperTier* susutier = new SuperSuperTier(); sutier->mutation(susutier); delete sutier; //letzte Mutation EndTier* etier = new EndTier(); susutier->mutation(etier); delete susutier; etier->print(); delete etier; //anderer Mutationsstrang Tier* tierv2 = new Tier(); //Mutation SuperTier* sutierv2 = new SuperTier(); tierv2->mutation(sutierv2); delete tierv2; //weiere Mutation SuperSuperTierV2* susutierv2 = new SuperSuperTierV2(); sutierv2->mutation(susutierv2); delete sutierv2; //letzte Mutation EndTierV2* etierv2 = new EndTierV2(); susutierv2->mutation(etierv2); delete susutierv2; etierv2->print(); delete etierv2; return 1; } |
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 |
class IBehavior { public: virtual void Execute() = 0; }; class Creature; class CreatureMutator; class IMutator { friend class CreatureMutator; public: virtual void Mutate( Creature* creature ) = 0; private: virtual void NextMutator( CreatureMutator* mutator ) = 0; }; class EatBehaviorDefault : public IBehavior { public: void Execute() { printf( "...\n" ); } }; class EatBehaviorCow : public IBehavior { public: void Execute() { printf( "... isst Grass\n" ); } }; class EatBehaviorSheep : public IBehavior { public: void Execute() { printf( "... isst Grass\n" ); } }; class EatBehaviorPig : public IBehavior { public: void Execute() { printf( "... isst Trueffel und Abfaelle\n" ); } }; class EatBehaviorChicken : public IBehavior { public: int x; EatBehaviorChicken( int value ) : x( value ) { } void Execute() { printf( "... isst %d Koerner\n", x ); } }; enum EBehaviorAction { BEHAVIOR_EAT = 0 // weitere }; class Creature { public: typedef IBehavior* BehaviorPtr; typedef std::map< EBehaviorAction, std::vector< BehaviorPtr > > BehaviorList; Creature() { } ~Creature() { // ggf aufräumen etcpp... } void PushBehavior( EBehaviorAction action, BehaviorPtr behavior ) { // hier ggf noch prüfen ob _dieser_ verhaltenstyp bereits vorhanden m_actionMap[ action ].push_back( behavior ); } void RemoveBehavior( EBehaviorAction action, BehaviorPtr behavior ) { // TODO: implementieren } void ExecuteBehavior( EBehaviorAction action ) const { BehaviorList::const_iterator it = m_actionMap.find( action ); if ( it != m_actionMap.end() ) { const std::vector< IBehavior* >& tmp = ( *it ).second; std::for_each( tmp.begin(), tmp.end(), std::mem_fun( &IBehavior::Execute ) ); } } private: BehaviorList m_actionMap; }; class CreatureMutator { private: IMutator* m_pMutationRule; // evtl auch liste von mutatoren public: CreatureMutator() : m_pMutationRule( NULL ) { } void NextStage( Creature* creature ) { if ( !m_pMutationRule ) return; m_pMutationRule->Mutate( creature ); m_pMutationRule->NextMutator( this ); } }; |
Werbeanzeige