blob: 46ab99be1f5ab3488af320bb32bc5334d4d034f9 [file] [log] [blame]
Austin Schuh58b9b472020-11-25 19:12:44 -08001/*
2 * Copyright 2020 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
Austin Schuh272c6132020-11-14 16:37:52 -080017import FlatBuffers
18
19typealias Monster = MyGame.Sample.Monster
20typealias Weapon = MyGame.Sample.Weapon
21typealias Color = MyGame.Sample.Color
22typealias Vec3 = MyGame.Sample.Vec3
23
24func main() {
Austin Schuh58b9b472020-11-25 19:12:44 -080025 let expectedDMG: [Int16] = [3, 5]
26 let expectedNames = ["Sword", "Axe"]
Austin Schuh272c6132020-11-14 16:37:52 -080027
Austin Schuh58b9b472020-11-25 19:12:44 -080028 var builder = FlatBufferBuilder(initialSize: 1024)
29 let weapon1Name = builder.create(string: expectedNames[0])
30 let weapon2Name = builder.create(string: expectedNames[1])
Austin Schuh272c6132020-11-14 16:37:52 -080031
Austin Schuh58b9b472020-11-25 19:12:44 -080032 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)
Austin Schuh272c6132020-11-14 16:37:52 -080040
Austin Schuh58b9b472020-11-25 19:12:44 -080041 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)
Austin Schuh272c6132020-11-14 16:37:52 -080044
Austin Schuh58b9b472020-11-25 19:12:44 -080045 let weaponsOffset = builder.createVector(ofOffsets: [sword, axe])
46 let pos = MyGame.Sample.createVec3(x: 1, y: 2, z: 3)
47
48 let orc = Monster.createMonster(
49 &builder,
50 structOfPos: pos,
51 hp: 300,
52 offsetOfName: name,
53 vectorOfInventory: inventoryOffset,
54 color: .red,
55 vectorOfWeapons: weaponsOffset,
56 equippedType: .weapon,
57 offsetOfEquipped: axe)
58 builder.finish(offset: orc)
59
60 let buf = builder.sizedByteArray
61 let monster = Monster.getRootAsMonster(bb: ByteBuffer(bytes: buf))
62
63 assert(monster.mana == 150)
64 assert(monster.hp == 300)
65 assert(monster.name == "Orc")
66 assert(monster.color == MyGame.Sample.Color.red)
67 assert(monster.pos != 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")
Austin Schuh272c6132020-11-14 16:37:52 -080083}