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 |
int i_WhichClass = rand()%100; if ( i_WhichClass == 1) pClass = new CClass1(); if ( i_WhichClass == 2) pClass = new CClass2(); |
C-/C++-Quelltext |
|
1 |
pClass = new CMainclass:RandomCGeerbteKlasse(); |
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 |
typedef IMonster* (*MONSTER_CREATEPROC)(); . . . int InitMonsters() { MonsterFactory.RegisterMonsterClass( "orc", &CreateOrc ); MonsterFactory.RegisterMonsterClass( "gargoyle", &CreateGargoyle ); } . . . IMonster* MonsterFactory::SpawnRandom() { MONSTER_CREATEPROC create = classList[rand() % classList.getCount()]; if( create ) return create(); else return 0; } IMonster* MonsterFactory::SpawnMonster( const char* className ) { MONSTER_CREATEPROC create = classList[className]; if( create ) return create(); else return 0; } IMonster* CreateOrc() { return new orc; } IMonster* CreateGargoyle() { return new gargoyle; } . . . |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 |
switch( rand() % 100 ) { case 1: pClass = new pClass1; break; case 2: pClass = new pClass2 break; . . . } |
C-/C++-Quelltext |
|
1 |
Create Random Level 5 Monster
|
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
// erstmal den funktionstyp festlegen (für die pointer später) typedef (CMonster*) (*CREATEMONSTERFUNC) (unsigned int); // die tatsächlichen funktionen CMonster* CreateGargoyle(unsigned int uiLevel); CMonster* CreateGolem(unsigned int uiLevel); // zur besseren nutzung mit nem enum auf die funktionen zugreifen enum EMonster {Gargoyle=0, Golem}; // und die anzahl der verfügbaren monster für den random const unsigned int uiNumMonsters=2; // Der array, der die funktionen speicher CREATEMONSTERFUNC functions[uiNumMonsters]; functions[Gargoyle] = CreateGargoyle; functions[Golem] = CreateGolem; |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 |
// die funktion zum erzeugen eines monsters CMonster* CreateRandomMonster(unsigned int uiLevel) { unsigned int uiMonsterType = rand()%uiNumMonsters; return functions[uiMonsterType] (uiLevel); } |
Werbeanzeige