運用GamePlayKit的GKEntity及GKComponent 的iOS遊戲開發實例
GameplayKit是一個面向對象的框架,為構建遊戲提供基礎工具和技術。 GameplayKit包含用於設計具有功能性,可重用架構的遊戲的工具,以及用於構建和增強諸如角色移動和對手行為的遊戲玩法特征的技術。
GamePlayKit我們這裏主要講GKEntity和GKComponent這二個類;
GKEntity類(實體): 可以容納很多組件的容器,根據自己的需求來加入相應的Component組件。
GKComponent類(組件):代表一種功能和行為,不同的組件代表不同的功能。
實用功能
:
(1)方便通過聚合多種組件,構建復雜的新實體Entity。
(3)不必重復寫組件,組件可以復用,也易於擴展。
(4)實體可以實現動態添加組件,以動態獲得或者刪除某些功能。
直接通過代碼來理解這二個類:
Flying PenguinPenguinEntity.swift 是用來管理 Component三個組件的
import SpriteKit
import GameplayKit
class PenguinEntity:GKEntity {
var spriteComponent:SpriteComponent! // 屬性 大小 texture
var moveComponent:MoveComponent! // 移動組件功能;
var animationComponent:AnimationComponent! //拍打翅膀的組件;
init(imageName:String) {
super.init()
let texture = SKTexture(imageNamed: imageName)
spriteComponent = SpriteComponent(entity: self, texture: texture, size: texture.size())
addComponent(spriteComponent)
// 加入上下飛動的組件
moveComponent = MoveComponent(entity: self)
addComponent(moveComponent)
// 加入拍打翅膀的動畫
let textureAltas = SKTextureAtlas(named: "penguin")
var textures = [SKTexture]()
for i in 1...textureAltas.textureNames.count {
let imageName = "penguin0\(i)"
textures.append(SKTexture(imageNamed: imageName))
}
animationComponent = AnimationComponent(entity: self, textures: textures)
addComponent(animationComponent)
}
// Add Physics
func addPhysics(){
let spriteNode = spriteComponent.node
spriteNode.physicsBody = SKPhysicsBody(texture: spriteNode.texture!, size: spriteNode.frame.size)
spriteNode.physicsBody?.categoryBitMask = PhysicsCategory.Player
spriteNode.physicsBody?.contactTestBitMask = PhysicsCategory.Coin | PhysicsCategory.Obstacle | PhysicsCategory.Floor
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
SpriteComponent 組件:精靈的皮膚、大小
import SpriteKit
import GameplayKit
class SpriteComponent :GKComponent {
let node:SKSpriteNode
init(entity:GKEntity,texture:SKTexture,size:CGSize) {
node = SKSpriteNode(texture: texture, color: SKColor.clear, size: size)
node.entity = entity
super.init()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
MoveComponent 上下飛動
import Foundation
import GameplayKit
import SpriteKit
class MoveComponent:GKComponent {
// The node on which animations should be run for this animation component.
// 引入SpriteComponent 就不用每次都需要像在SpriteComponent裏建立精靈的屬性了
// node = SKSpriteNode(texture: texture, color: SKColor.clear, size: size)
let spriteComponent: SpriteComponent
init(entity:GKEntity) {
self.spriteComponent = entity.component(ofType: SpriteComponent.self)!
super.init()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// 組件 上下飛動;
func startWobble(){
let moveUp = SKAction.moveBy(x: 0, y: 50, duration: 0.5)
moveUp.timingMode = .easeInEaseOut
let moveDown = moveUp.reversed()
let sequence = SKAction.sequence([moveUp,moveDown])
let repeatWobble = SKAction.repeatForever(sequence)
spriteComponent.node.run(repeatWobble, withKey: "Wobble")
}
}
AnimationComponent 拍打翅膀
import GameplayKit
import SpriteKit
class AnimationComponent:GKComponent {
let textures:[SKTexture]
let spriteComponent: SpriteComponent
init(entity:GKEntity,textures:[SKTexture]) {
self.spriteComponent = entity.component(ofType: SpriteComponent.self)!
self.textures = textures
super.init()
}
// 翅膀拍動
func startAnimation(){
let flyAction = SKAction.animate(with: textures, timePerFrame: TimeInterval(0.02))
let repeatAction = SKAction.repeatForever(flyAction)
spriteComponent.node.run(repeatAction)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
在場景GameScene加入管理器Entity對象
func setupEntityComponent(){
let penguin = PenguinEntity(imageName: "penguin01") // 企鵝屬於worldNode的子層級;
let penguinNode = penguin.spriteComponent.node
penguinNode.position = CGPoint(x: 320, y: 500)
penguinNode.zPosition = 5
worldNode.addChild(penguinNode)
// penguin有移動的功能
penguin.moveComponent.startWobble()
// 有拍打翅膀的功能
penguin.animationComponent.startAnimation()
}
以上就是應用GKEntity來管理三個組件GKComponent的運用實例。
源碼傳送門:https://github.com/apiapia/FlyingPenguinSpriteKitGameTutorial
更多遊戲教學:http://www.iFIERO.com
補充:英文夠好的話,建議上蘋果的官網看看
GameplayKit
的案例代碼:
-
Boxes: GameplayKit Entity-Component Basics
-
Dispenser: GameplayKit State Machine Basics
-
Pathfinder: GameplayKit Pathfinding Basics
-
AgentsCatalog: Using the Agents System in GameplayKit
-
FourInARow: Using the GameplayKit Minmax Strategist for Opponent AI
-
DemoBots: Building a Cross Platform Game with SpriteKit and GameplayKit
運用GamePlayKit的GKEntity及GKComponent 的iOS遊戲開發實例