-
-
Save Pharap/c5766b6033269eb41f8ce565f7b57c74 to your computer and use it in GitHub Desktop.
Example of a monster system
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct MonsterDefinition | |
{ | |
const char * name; | |
uint16_t defaultHP; | |
uint16_t defaultAttack; | |
uint16_t defaultDefence; | |
// etc | |
}; | |
class Monster | |
{ | |
private: | |
const MonsterDefinition * type; | |
uint16_t hp; | |
public: | |
Monster(const MonsterDefinition & type) | |
: type(&type) | |
{ | |
this->hp = pgm_read_word(&type->defaultHP); | |
} | |
bool isAlive(void) | |
{ | |
return this->hp != 0; | |
} | |
void takeDamage(uint16_t amount) | |
{ | |
this->hp = (this->hp > amount) ? this->hp - amount : 0; | |
} | |
void heal(uint16_t amount) | |
{ | |
this->hp = (this->hp + amount > this->hp) ? this->hp + amount : 0xFFFF; | |
} | |
const char * getName(void) | |
{ | |
return reinterpret_cast<const char *>(pgm_read_word(&this->type->name)); | |
} | |
const __FlashStringHelper * getPrintableName(void) | |
{ | |
return reinterpret_cast<const __FlashStringHelper *>(pgm_read_word(&this->type->name)); | |
} | |
}; | |
const char monsterName0[] PROGMEM = "Slime"; | |
const char monsterName1[] PROGMEM = "Skeleton"; | |
const char monsterName2[] PROGMEM = "Dave"; | |
const char monsterName3[] PROGMEM = "Pharap"; | |
const MonsterDefinition monsterDefinitions[] PROGMEM = | |
{ | |
{ monsterName0, 10, 5, 20 }, | |
{ monsterName1, 20, 15, 5 }, | |
{ monsterName2, 1, 0, 0 }, | |
{ monsterName3, 1500, 4500, 8000 }, | |
}; | |
enum class MonsterId : uint8_t | |
{ | |
Unknown, | |
Slime, | |
Skeleton, | |
Dave, | |
Pharap, | |
}; | |
bool spawnMonster(MonsterId id, Monster & monster) | |
{ | |
if(id == MonsterId::Unknown) | |
return false; | |
const MonsterDefinition * = &monsterDefinitions[static_cast<uint8_t>(id) - 1]; | |
monster = Monster(*monsterDefinition); | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment