Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 1 | /* |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 2 | * Copyright 2021 Google Inc. All rights reserved. |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 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 Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 17 | import FlatBuffers |
| 18 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 19 | typealias Monster = MyGame_Sample_Monster |
| 20 | typealias Weapon = MyGame_Sample_Weapon |
| 21 | typealias Color = MyGame_Sample_Color |
| 22 | typealias Vec3 = MyGame_Sample_Vec3 |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 23 | |
| 24 | func main() { |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 25 | let expectedDMG: [Int16] = [3, 5] |
| 26 | let expectedNames = ["Sword", "Axe"] |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 27 | |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 28 | var builder = FlatBufferBuilder(initialSize: 1024) |
| 29 | let weapon1Name = builder.create(string: expectedNames[0]) |
| 30 | let weapon2Name = builder.create(string: expectedNames[1]) |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 31 | |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 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) |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 40 | |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 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) |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 44 | |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 45 | let weaponsOffset = builder.createVector(ofOffsets: [sword, axe]) |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 46 | |
| 47 | let orc = Monster.createMonster( |
| 48 | &builder, |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 49 | pos: MyGame_Sample_Vec3(x: 1, y: 2, z: 3), |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 50 | hp: 300, |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 51 | nameOffset: name, |
| 52 | inventoryVectorOffset: inventoryOffset, |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 53 | color: .red, |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 54 | weaponsVectorOffset: weaponsOffset, |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 55 | equippedType: .weapon, |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 56 | equippedOffset: axe) |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 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) |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 67 | assert(monster.mutablePos != nil) |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 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 Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 83 | } |