1 import java.nio.ByteBuffer; 2 import MyGame.Example.Monster; 3 import MyGame.Example.Stat; 4 import com.google.flatbuffers.FlatBufferBuilder; 5 6 class GameFactory { createMonster(String monsterName, short nestedMonsterHp, short nestedMonsterMana)7 public static Monster createMonster(String monsterName, short nestedMonsterHp, short nestedMonsterMana) { 8 FlatBufferBuilder builder = new FlatBufferBuilder(); 9 10 int name_offset = builder.createString(monsterName); 11 Monster.startMonster(builder); 12 Monster.addName(builder, name_offset); 13 Monster.addHp(builder, nestedMonsterHp); 14 Monster.addMana(builder, nestedMonsterMana); 15 int monster_offset = Monster.endMonster(builder); 16 Monster.finishMonsterBuffer(builder, monster_offset); 17 18 ByteBuffer buffer = builder.dataBuffer(); 19 Monster monster = Monster.getRootAsMonster(buffer); 20 return monster; 21 } 22 createMonsterFromStat(Stat stat, int seqNo)23 public static Monster createMonsterFromStat(Stat stat, int seqNo) { 24 FlatBufferBuilder builder = new FlatBufferBuilder(); 25 int name_offset = builder.createString(stat.id() + " No." + seqNo); 26 Monster.startMonster(builder); 27 Monster.addName(builder, name_offset); 28 int monster_offset = Monster.endMonster(builder); 29 Monster.finishMonsterBuffer(builder, monster_offset); 30 Monster monster = Monster.getRootAsMonster(builder.dataBuffer()); 31 return monster; 32 } 33 createStat(String greeting, long val, int count)34 public static Stat createStat(String greeting, long val, int count) { 35 FlatBufferBuilder builder = new FlatBufferBuilder(); 36 int statOffset = Stat.createStat(builder, builder.createString(greeting), val, count); 37 builder.finish(statOffset); 38 Stat stat = Stat.getRootAsStat(builder.dataBuffer()); 39 return stat; 40 } 41 42 } 43