मैं एप्पल स्प्राइटकिट & गेमप्लेकिट उदाहरण कोड में गहराई से देख रहा था और स्विफ्ट में लिखे गए 'डेमोबॉट्स' नामक एक परियोजना मिली। उन परियोजनाओं में उपयोग की जाने वाली कुछ बहुत ही रोचक अवधारणाएं हैं जिन्हें मैं अपनी परियोजनाओं में अनुकूलित करना चाहता था।इस स्विफ्ट स्प्राइटकिट का वर्णन कैसे करें भौतिकी निकाय बिटमैस्क सिस्टम का उदाहरण कोड
मैं पहले से ही एक हैंडलर-क्लास में टक्कर-हैंडलिंग को समाहित करने के साथ काम कर रहा था जो उस उदाहरण कोड में टकरावों के तरीके के समान ही है।
इस परियोजना में मैंने पाया एक struct के लिए निम्न कोड 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
}
}
यह struct आप एक SKPhysicsBody
इस तरह की .collisionBitmask
/.contactBitmask
/.categoryBitmask
गुण सेट हर बार प्रयोग किया जाता है: (मैं लागू कर दिया है इस घटक & इकाई डिजाइन मार्गदर्शिका का उपयोग करके)
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
}
}
अब तक तो अच्छा।
/// 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
}
कि मतलब यह है कि हर बार जब मैं फोन है, जिसकी गणना (है कि वे क्या कहा जाता है है: ऑब्जेक्टिव-सी से आ मेरी समस्या यह है कि मैं पूरी तरह से समझ में नहीं आता क्या RPColliderType Struct से बाहर कोड के उन लोगों के निम्नलिखित लाइनों करना है तेजी से, सही?) संपत्ति - मैं इसे करता हूं जब मैं इसे SKPhysicsBody
पर असाइन करता हूं - यह इसे उन स्थिर वर्ग शब्दकोशों में जोड़ता है। लेकिन मुझे 'mask
'/'reduce
'/'union
' आदेशों की व्याख्या करने में समस्या है।
वास्तव में ऐसा क्या करता है?
इस तरह के विस्तृत स्पष्टीकरण के लिए धन्यवाद। लगभग कुछ ऐसा है जैसे आपने उदाहरण कोड का टुकड़ा लिखा है !? तथापि। मेरी समझ से यह कोड का एक बहुत ही सुरुचिपूर्ण टुकड़ा है, है ना? –
आपका स्वागत है। यह कोड श्रेणी, टकराव और संपर्क बिट मास्क प्रबंधित करने के लिए 'OptionSetType' का उपयोग करने का एक अच्छा उदाहरण है। – 0x141E