• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2021 Google Inc. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 import FlatBuffers
18 
19 typealias Monster = MyGame_Sample_Monster
20 typealias Weapon = MyGame_Sample_Weapon
21 typealias Color = MyGame_Sample_Color
22 typealias Vec3 = MyGame_Sample_Vec3
23 
mainnull24 func main() {
25   let expectedDMG: [Int16] = [3, 5]
26   let expectedNames = ["Sword", "Axe"]
27 
28   var builder = FlatBufferBuilder(initialSize: 1024)
29   let weapon1Name = builder.create(string: expectedNames[0])
30   let weapon2Name = builder.create(string: expectedNames[1])
31 
32   let weapon1Start = Weapon.startWeapon(&builder)
33   Weapon.add(name: weapon1Name, &builder)
34   Weapon.add(damage: expectedDMG[0], &builder)
35   let sword = Weapon.endWeapon(&builder, start: weapon1Start)
36   let weapon2Start = Weapon.startWeapon(&builder)
37   Weapon.add(name: weapon2Name, &builder)
38   Weapon.add(damage: expectedDMG[1], &builder)
39   let axe = Weapon.endWeapon(&builder, start: weapon2Start)
40 
41   let name = builder.create(string: "Orc")
42   let inventory: [Byte] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
43   let inventoryOffset = builder.createVector(inventory)
44 
45   let weaponsOffset = builder.createVector(ofOffsets: [sword, axe])
46 
47   let orc = Monster.createMonster(
48     &builder,
49     pos: MyGame_Sample_Vec3(x: 1, y: 2, z: 3),
50     hp: 300,
51     nameOffset: name,
52     inventoryVectorOffset: inventoryOffset,
53     color: .red,
54     weaponsVectorOffset: weaponsOffset,
55     equippedType: .weapon,
56     equippedOffset: axe)
57   builder.finish(offset: orc)
58 
59   let buf = builder.sizedByteArray
60   let monster = Monster.getRootAsMonster(bb: ByteBuffer(bytes: buf))
61 
62   assert(monster.mana == 150)
63   assert(monster.hp == 300)
64   assert(monster.name == "Orc")
65   assert(monster.color == MyGame.Sample.Color.red)
66   assert(monster.pos != nil)
67   assert(monster.mutablePos != nil)
68   for i in 0..<monster.inventoryCount {
69     assert(i == monster.inventory(at: i))
70   }
71 
72   for i in 0..<monster.weaponsCount {
73     let weap = monster.weapons(at: i)
74     let index = Int(i)
75     assert(weap?.damage == expectedDMG[index])
76     assert(weap?.name == expectedNames[index])
77   }
78   assert(monster.equippedType == .weapon)
79   let equipped = monster.equipped(type: Weapon.self)
80   assert(equipped?.name == "Axe")
81   assert(equipped?.damage == 5)
82   print("Monster Object is Verified")
83 }
84