首页 > 代码库 > 11-13 游戏编程行为模式
11-13 游戏编程行为模式
### Behavioral Pattern#### interpreter pattern string -> code instruction set: 提供基本操作virtual machine: 执行指令front-end: 生成效率更高的字节码```void setHealth(int wizard, int amount);void setWisdom(int wizard, int amount);void setAgility(int wizard, int amount);void playSound(int soundId);void spawnParticles(int particleType);enum Instruction{ INST_SET_HEALTH = 0x00, INST_SET_WISDOM = 0x01, INST_SET_AGILITY = 0x02, INST_PLAY_SOUND = 0x03, INST_SPAWN_PARTICLES = 0x04};switch (instruction){ case INST_SET_HEALTH: setHealth(0, 100); break; case INST_SET_WISDOM: setWisdom(0, 100); break; case INST_SET_AGILITY: setAgility(0, 100); break; case INST_PLAY_SOUND: playSound(SOUND_BANG); break; case INST_SPAWN_PARTICLES: spawnParticles(PARTICLE_FLAME); break;}### stack matchineclass VM{public: VM() : stackSize_(0) {} private: void push(int value) { // Check for stack overflow. assert(stackSize_ < MAX_STACK); stack_[stackSize_++] = value; } int pop() { // Make sure the stack isn‘t empty. assert(stackSize_ > 0); return stack_[--stackSize_]; }private: static const int MAX_STACK = 128; int stackSize_; int stack_[MAX_STACK];};case INST_ADD:{ int b = pop(); int a = pop(); push(a + b); break;}```### subclass 沙箱子类使用父类提供的方法#### Type Object```class Monster{}class Drogon: Monster {}class Troll: Monster {} VSclass Breed {}class Monster{ public Breed breed;}Monster drogon = new Monster{ breed= new Breed("drogon")};Breed read config// 好处// monster的breed可以随便修改, 配置生成生物,不用重新编译代码```
11-13 游戏编程行为模式
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。