我目前正在构建游戏,我想使用 Gameplaykit 来组织元素。
我发现了ECS(实体组件系统)的概念,它可以将功能放在小块(称为组件)中,而这些小块可以添加到实体中以赋予其功能。
我想将它与SpriteKit结合使用,我的第一个想法当然是创建一个SpriteComponent,但由于我也在使用SpriteKit的物理引擎,我想知道我应该如何制作PhysicsBodyComponent,因为对于这个组件要工作它需要直接访问我的SpriteKit节点的物理主体,因此它将依赖于SpriteComponent,但组件彼此依赖是不是错了?
我该怎么办?
谢谢。
答案 0 :(得分:1)
这取决于您希望如何构建组件。就像你说的那样,尽可能保持它们的灵活性,可重用性和独立性。
所以我只想创建一个简单的渲染组件。这就是我在游戏中使用的内容。
import SpriteKit
import GameplayKit
/// GK sprite component
class SpriteComponent: GKComponent {
// MARK: - Properties
/// The rendered node
let node: SKSpriteNode
// MARK: GKComponent Life Cycle
/// Init with image
init(imageNamed: String) {
node = SKSpriteNode(imageNamed: imageNamed)
}
/// Init with texture
init(texture: SKTexture) {
node = SKSpriteNode(texture: texture)
}
}
在您的实体类中,您可以照常添加组件
class Player: GKEntity {
override init() { // pass in an image name if you need to
super.init()
let spriteComponent = SpriteComponent(imageNamed: "Player")
addComponent(spriteComponent)
}
}
比将实体添加到场景并定位它。 将组件添加到实体后,您可以使用组件(ofType:...)方法访问其属性,在本例中为node属性,并对它们执行某些操作。
class GameScene: SKScene {
// code to add entity to scene
...
// position entity
if let playerNode = player.component(ofType: SpriteComponent.self)?.node {
playerNode.position = CGPoint(...)
}
返回实体类并在添加sprite组件后添加物理实体。
...
addComponent(spriteComponent)
// Set physics body
if let sprite = component(ofType: SpriteComponent.self)?.node { // component for class is an optional and nil until component is added to entity.
sprite.physicsBody = SKPhysicsBody(...
sprite.physicsBody?.categoryBitMask = ...
...
}
这样,所有实体都可以使用1个渲染组件,但它们上面有不同的物理实体,并使用不同的位置。
您可以创建一个物理主体组件,并将位掩码等以及要添加到其中的节点传递给init方法。但是我认为那会让它变得非常混乱所以我更喜欢这种方式。
如果确实需要使组件彼此依赖,请记住每个GKComponent都有一个可以使用的实体属性。我会尽量避免这种情况,以使您的组件更加灵活。
class SomeComponent: GKComponent {
func test() {
entity?.component(ofType: SomeOtherComponent.self)?.someMethod() // only works if this component is added to entity (entity?) and the other component is also added to entity (...self)?.
}
class SomeOtherComponent: GKComponent {
func someMethod() {
}
}
如果您需要更多信息,请阅读这些文章,它们非常好。
http://code.tutsplus.com/tutorials/an-introduction-to-gameplaykit-part-1--cms-24483
希望这有帮助