5

我深入研究了 Apples SpriteKit 和 GameplayKit 示例代码,发现了一个用 Swift 编写的名为“DemoBots”的项目。在那些项目中使用了一些非常有趣的概念,我想将它们应用到我的项目中。

我已经在将碰撞处理封装到一个处理程序类中,这与该示例代码中处理冲突的方式非常相似。

在这个项目中,我为一个名为的结构找到了以下代码RPColliderType

struct RPColliderType: OptionSetType, Hashable, CustomDebugStringConvertible {
    // MARK: Static properties

    /// A dictionary to specify which `ColliderType`s should be notified of contacts with other `ColliderType`s.
    static var requestedContactNotifications = [RPColliderType: [RPColliderType]]()

    /// A dictionary of which `ColliderType`s should collide with other `ColliderType`s.
    static var definedCollisions = [RPColliderType: [RPColliderType]]()

    // MARK: Properties

    let rawValue: UInt32

    // MARK: Options

    static var Obstacle: RPColliderType  { return self.init(rawValue: 1 << 0) }
    static var PlayerBot: RPColliderType { return self.init(rawValue: 1 << 1) }
    static var TaskBot: RPColliderType   { return self.init(rawValue: 1 << 2) }

    // MARK: Hashable

    var hashValue: Int {
        return Int(rawValue)
    }

    // MARK: SpriteKit Physics Convenience

    /// A value that can be assigned to a 'SKPhysicsBody`'s `categoryMask` property.
    var categoryMask: UInt32 {
        return rawValue
    }

    /// A value that can be assigned to a 'SKPhysicsBody`'s `collisionMask` property.
    var collisionMask: UInt32 {
        // Combine all of the collision requests for this type using a bitwise or.
        let mask = RPColliderType.definedCollisions[self]?.reduce(RPColliderType()) { initial, colliderType in
            return initial.union(colliderType)
        }

        // Provide the rawValue of the resulting mask or 0 (so the object doesn't collide with anything).
        return mask?.rawValue ?? 0
    }

    /// A value that can be assigned to a 'SKPhysicsBody`'s `contactMask` property.
    var contactMask: UInt32 {
        // Combine all of the contact requests for this type using a bitwise or.
        let mask = RPColliderType.requestedContactNotifications[self]?.reduce(RPColliderType()) { initial, colliderType in
            return initial.union(colliderType)
        }

        // Provide the rawValue of the resulting mask or 0 (so the object doesn't need contact callbacks).
        return mask?.rawValue ?? 0
    }

    // MARK: ContactNotifiableType Convenience

    /**
        Returns `true` if the `ContactNotifiableType` associated with this `ColliderType` should be
        notified of contact with the passed `ColliderType`.
    */
    func notifyOnContactWithColliderType(colliderType: RPColliderType) -> Bool {
        if let requestedContacts = RPColliderType.requestedContactNotifications[self] {
            return requestedContacts.contains(colliderType)
        }

        return false
    }
}

每次设置这样的 // 属性时都会使用这个结构.collisionBitmask:(我.contactBitmask已经使用组件和实体设计指南实现了这个).categoryBitmaskSKPhysicsBody

class RPPhysicsComponent: GKComponent {

    var physicsBody: SKPhysicsBody

    init(physicsBody: SKPhysicsBody, colliderType: RPColliderType) {

        self.physicsBody = physicsBody
        self.physicsBody.categoryBitMask = colliderType.categoryMask
        self.physicsBody.collisionBitMask = colliderType.collisionMask
        self.physicsBody.contactTestBitMask = colliderType.contactMask
    }
}

到现在为止还挺好。来自 Objective-C 我的问题是我不完全理解 RPColliderType 结构中的以下代码行是做什么的:

/// A value that can be assigned to a 'SKPhysicsBody`'s `collisionMask` property.
var collisionMask: UInt32 {
    // Combine all of the collision requests for this type using a bitwise or.
    let mask = RPColliderType.definedCollisions[self]?.reduce(RPColliderType()) { initial, colliderType in
        return initial.union(colliderType)
    }

    // Provide the rawValue of the resulting mask or 0 (so the object doesn't collide with anything).
    return mask?.rawValue ?? 0
}

这是否意味着每次我调用该计算属性时(这就是它们在 swift 中的调用,对吗?)属性 - 当我将它分配给 a 时我会这样做SKPhysicsBody- 它会将它添加到那些静态类字典中。但我在解释 ' mask' / ' reduce' / ' union' 命令时遇到问题。

那真的有什么作用?

4

1 回答 1

2

collisionMask是计算属性,它返回一个UInt32可以用作物理体的碰撞位掩码的值。如果将此计算属性分解为其功能部分,则更容易理解它是如何工作的。

但首先,让我们将应该与之碰撞的RPColliderType对象的数组添加到字典中:PlayerBotdefinedCollisions

RPColliderType.definedCollisions[.PlayerBot] = [.Obstacle, .TaskBot]

此时,definedCollisions字典包含一个项目,其中PlayerBot[.Obstacle, .TaskBot]分别作为键和值。将此视为可以与 a和冲突的PlayerBot类别。ObstacleTaskBot

我们现在可以使用.PlayerBot从字典中检索值(即数组):

let array = RPColliderType.definedCollisions[.PlayerBot]

由于collisionMask在 中定义RPColliderTypeself用作字典键。此外,array是可选的,因为与键对应的值可能不存在于字典中。

然后,代码使用该方法将RPColliderType对象数组组合成单个对象。接受两个参数:一个初始值(与数组元素的类型相同)和一个函数(或闭包),它接受一个值作为参数并返回一个值。在这种情况下,初始值是一个新对象,闭包的参数和返回值也是对象:RPColliderTypereducereduceRPColliderTypeRPColliderType

array?.reduce(RPColliderType(), aFunction)

Apple 的代码使用尾随闭包而不是将函数传递给reduce. 从文档中,

如果您需要将闭包表达式作为函数的最终参数传递给函数并且闭包表达式很长,则将其编写为尾随闭包会很有用。尾随闭包是一个闭包表达式,它写在它支持的函数调用的括号之外(和之后)。

reduce迭代数组并使用初始值和每个数组元素作为参数调用闭包,返回值用作下一次迭代的初始值:

let mask = array?.reduce(RPColliderType()) {
    initial, colliderType in
    return initial.union(colliderType)
}

where保持数组元素initial的中间并集,并且是 的当前元素。RPColliderTypecolliderTypearray

此时,mask是一个RPColliderType对象,我们可以UInt32

mask?.rawValue

这是collisionMask计算属性的返回值。

于 2016-01-28T17:59:53.203 回答