EntityEquipment
A protocol for equipment in a game that you render using RealityKit.
Declaration
protocol EntityEquipment : EquipmentOverview
To render equipment using an entity, follow these steps:
Create a structure that conforms to this protocol.
Depending on the type of equipment, set the State type alias to either BaseEquipmentState, DieState, or CardState.
Declare the
idproperty as a EquipmentIdentifier structure.Declare the initialState property as a State structure.
Implement an initializer that sets these properties and the entity property.
struct Piece: EntityEquipment {
typealias State = BaseEquipmentState
var id: EquipmentIdentifier
var entity: Entity
var initialState: State
@MainActor
init(index: Int = 0, position: TableVisualState.Point2D) {
id = .piece(index)
entity = try! ModelEntity.load(named: "chess/queen", in: contentBundle)
initialState = State(parentID: .table, pose: .init(position: position, rotation: .init()), entity: entity)
}
}Optionally, implement the layoutChildren(for:visualState:) method for equipment that represents groups, and the restingOrientation(state:) method to provide a custom resting position. For example, implement the restingOrientation(state:) method to flip a card face down in a card game:
func restingOrientation(state: State) -> Rotation3D {
state.faceUp ? .identity : .init(angle: .init(degrees: +180), axis: .init(x: 0, y: 0, z: 1))
}