swift_code_9.swift (1999B)
1 import FlatBuffers 2 import Foundation 3 4 func run() { 5 // create a `FlatBufferBuilder`, which will be used to serialize objects 6 let builder = FlatBufferBuilder(initialSize: 1024) 7 8 let weapon1Name = builder.create(string: "Sword") 9 let weapon2Name = builder.create(string: "Axe") 10 11 // start creating the weapon by calling startWeapon 12 let weapon1Start = Weapon.startWeapon(&builder) 13 Weapon.add(name: weapon1Name, &builder) 14 Weapon.add(damage: 3, &builder) 15 // end the object by passing the start point for the weapon 1 16 let sword = Weapon.endWeapon(&builder, start: weapon1Start) 17 18 let weapon2Start = Weapon.startWeapon(&builder) 19 Weapon.add(name: weapon2Name, &builder) 20 Weapon.add(damage: 5, &builder) 21 let axe = Weapon.endWeapon(&builder, start: weapon2Start) 22 23 // Create a FlatBuffer `vector` that contains offsets to the sword and axe 24 // we created above. 25 let weaponsOffset = builder.createVector(ofOffsets: [sword, axe]) 26 27 // Name of the Monster. 28 let name = builder.create(string: "Orc") 29 30 let pathOffset = fbb.createVector(ofStructs: [ 31 Vec3(x: 0, y: 0), 32 Vec3(x: 5, y: 5), 33 ]) 34 35 // startVector(len, elementSize: MemoryLayout<Offset>.size) 36 // for o in offsets.reversed() { 37 // push(element: o) 38 // } 39 // endVector(len: len) 40 41 let orc = Monster.createMonster( 42 &builder, 43 pos: Vec3(x: 1, y: 2), 44 hp: 300, 45 nameOffset: name, 46 color: .red, 47 weaponsVectorOffset: weaponsOffset, 48 equippedType: .weapon, 49 equippedOffset: axe, 50 pathOffset: pathOffset) 51 52 // let start = Monster.startMonster(&builder) 53 // Monster.add(pos: Vec3(x: 1, y: 2), &builder) 54 // Monster.add(hp: 300, &builder) 55 // Monster.add(name: name, &builder) 56 // Monster.add(color: .red, &builder) 57 // Monster.addVectorOf(weapons: weaponsOffset, &builder) 58 // Monster.add(equippedType: .weapon, &builder) 59 // Monster.addVectorOf(paths: weaponsOffset, &builder) 60 // Monster.add(equipped: axe, &builder) 61 // var orc = Monster.endMonster(&builder, start: start) 62 }