blob: 889bc980a973b12ea52b4980f6df751a1a50892f [file] [log] [blame]
Austin Schuh58b9b472020-11-25 19:12:44 -08001/*
James Kuszmaul8e62b022022-03-22 09:33:25 -07002 * Copyright 2021 Google Inc. All rights reserved.
Austin Schuh58b9b472020-11-25 19:12:44 -08003 *
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
James Kuszmaul8e62b022022-03-22 09:33:25 -070019typealias Monster = MyGame_Sample_Monster
20typealias Weapon = MyGame_Sample_Weapon
21typealias Color = MyGame_Sample_Color
22typealias Vec3 = MyGame_Sample_Vec3
Austin Schuh272c6132020-11-14 16:37:52 -080023
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])
Austin Schuh58b9b472020-11-25 19:12:44 -080046
47 let orc = Monster.createMonster(
48 &builder,
James Kuszmaul8e62b022022-03-22 09:33:25 -070049 pos: MyGame_Sample_Vec3(x: 1, y: 2, z: 3),
Austin Schuh58b9b472020-11-25 19:12:44 -080050 hp: 300,
James Kuszmaul8e62b022022-03-22 09:33:25 -070051 nameOffset: name,
52 inventoryVectorOffset: inventoryOffset,
Austin Schuh58b9b472020-11-25 19:12:44 -080053 color: .red,
James Kuszmaul8e62b022022-03-22 09:33:25 -070054 weaponsVectorOffset: weaponsOffset,
Austin Schuh58b9b472020-11-25 19:12:44 -080055 equippedType: .weapon,
James Kuszmaul8e62b022022-03-22 09:33:25 -070056 equippedOffset: axe)
Austin Schuh58b9b472020-11-25 19:12:44 -080057 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)
James Kuszmaul8e62b022022-03-22 09:33:25 -070067 assert(monster.mutablePos != nil)
Austin Schuh58b9b472020-11-25 19:12:44 -080068 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}