Contents

EntityEquipment

A protocol for equipment in a game that you render using RealityKit.

Declaration

protocol EntityEquipment : Equipment

Overview

To render equipment using an entity, follow these steps:

  1. Create a structure that conforms to this protocol.

  2. Depending on the type of equipment, set the State type alias to either BaseEquipmentState, DieState, or CardState.

  3. Declare the id property as a EquipmentIdentifier structure.

  4. Declare the initialState property as a State structure.

  5. 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))
}

Topics

Displaying the equipment

See Also

Equipment