在之前的發(fā)布的 iOS 版本中,蘋果就已經(jīng)非常重視,讓開發(fā)者編寫游戲更簡單。他們?cè)?iOS 7 中介紹了 SpriteKit。 SpriteKit 是一個(gè) 2D 的圖形和動(dòng)畫的庫,你可以用來為 iOS 和 OS X 平臺(tái)編寫可交互的游戲。2012年的時(shí)候,他們又為 Mac 平臺(tái)提供了 SceneKit 庫,在 WWDC 2014 時(shí),又將其拓展到了 iOS 平臺(tái),并增加了一些新的特性,例如粒子系統(tǒng)和物理模擬。
同時(shí)用過這兩個(gè)庫后,我個(gè)人可以作證,這兩個(gè)庫都是非常好用的。當(dāng)你在游戲中用來展示可視化的元素時(shí),他們非常有用。但是,畢竟我開發(fā)游戲的經(jīng)驗(yàn)不多,我經(jīng)常比較疑惑的是,如何去架構(gòu)一個(gè)游戲項(xiàng)目,如果去構(gòu)建模型,以及如何處理它們之間的關(guān)系。
隨著 iOS 9 的發(fā)布,蘋果試圖通過一些方法來幫助開發(fā)者解決這些問題。他們介紹了一個(gè)新的庫,GameplayKit,他是一組工具集,提供一系列的在 iOS 和 OS X 平臺(tái)上開發(fā)的技術(shù)。
和高級(jí)別的游戲引擎,例如 SpriteKit 和 SceneKit 不同,GameplayKit 并不包括動(dòng)畫和渲染內(nèi)容等,相應(yīng)的,你可以使用 GameplayKit 來開發(fā)你的游戲玩法和機(jī)制,設(shè)置模型,使架構(gòu)做到最小組件化和可伸縮。
-- 來自蘋果文檔中關(guān)于 GameplayKit 介紹部分。
這個(gè)庫包涵了一下特性
- Randomisation
- Entities and Components
- State Machines
- Pathfinding
- Agents, Goals & Behaviours
- Rule Systems
這篇文章,著重介紹 pathfinding 在 GameplayKit 中的對(duì)應(yīng) API,當(dāng)然也會(huì)涉及到一些其它部分。
創(chuàng)建一個(gè) PathFinding 的例子
現(xiàn)在我們來創(chuàng)建一個(gè)簡單的 SpriteKit 示例項(xiàng)目,來示范一下 GameplayKit 中 pathfinding 相關(guān)的API.
首先,在 Xcode 中創(chuàng)建一個(gè) SpriteKit 類型游戲項(xiàng)目。

它會(huì)自動(dòng)創(chuàng)建一個(gè)基于模版的基本游戲項(xiàng)目,下一步,我們打開 GameScene.sks文件,來添加幾個(gè)節(jié)點(diǎn)。首先我們創(chuàng)建一個(gè)代表玩家的節(jié)點(diǎn),我們希望它在迷宮中可以移動(dòng)。

注意一下在右側(cè)的 property inspector,我們把name 設(shè)置為“player”,后面我們會(huì)用它來和這個(gè)節(jié)點(diǎn)進(jìn)行關(guān)聯(lián)。
接下來,我們添加更多的節(jié)點(diǎn)。以讓玩家去在迷宮中避讓。否則的話,這個(gè)pathfinding 就太簡單了。

使用 scene editor 拖拽一些 node 到場(chǎng)景中。你可以想上圖一樣去布置。簡單也好、復(fù)雜也可以。只要能夠保證,當(dāng)玩家點(diǎn)擊了某個(gè)特定點(diǎn)后,在通過時(shí)需要避讓就行。你無須對(duì)這些節(jié)點(diǎn)進(jìn)行修飾,讓他們保持簡單的矩形就好了。
接下來,打開 GameScene.swift 文件,重載 touchesBegan 方法。我們將使用用戶點(diǎn)擊的點(diǎn),作為路徑的終點(diǎn)。
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch: AnyObject in touches {
let location = (touch as! UITouch).locationInNode(self)
self.movePlayerToLocation(location)
}}
一旦我們發(fā)現(xiàn)用戶點(diǎn)擊了,我們能要?jiǎng)?chuàng)建一個(gè)從玩家的當(dāng)前點(diǎn)到點(diǎn)擊的點(diǎn)之間的路徑,同時(shí)這個(gè)路徑要避讓障礙物。為了做這些,我們需要?jiǎng)?chuàng)建一個(gè) movePlayerToLocation 方法。
func movePlayerToLocation(location: CGPoint) {
// Ensure the player doesn't move when they are already moving.
guard (!moving) else {return}
moving = true
首先我們需要獲得 player,我們可以通過 childNodeWithName 方法來獲取。在前面我們已經(jīng)通過 scene editor 給它命名好了。
// Find the player in the scene.
let player = self.childNodeWithName("player")
當(dāng)我們獲取到障礙物的數(shù)組后,我們要計(jì)算從 player 的當(dāng)前點(diǎn)到終點(diǎn)的路徑。
// Create an array of obstacles, which is every child node, apart from the player node.
let obstacles = SKNode.obstaclesFromNodeBounds(self.children.filter({ (element ) -> Bool in
return element != player
}))
一旦我們獲取到了 player 以后,我們要建立一個(gè)數(shù)組,把其它節(jié)點(diǎn)放進(jìn)去。這是我們需要讓 player 避讓的障礙物數(shù)組。
// Assemble a graph based on the obstacles. Provide a buffer radius so there is a bit of space between the
// center of the player node and the edges of the obstacles.
let graph = GKObstacleGraph(obstacles: obstacles, bufferRadius: 10)
// Create a node for the user's current position, and the user's destination.
let startNode = GKGraphNode2D(point: float2(Float(player!.position.x), Float(player!.position.y)))
let endNode = GKGraphNode2D(point: float2(Float(location.x), Float(location.y)))
// Connect the two nodes just created to graph.
graph.connectNodeUsingObstacles(startNode)
graph.connectNodeUsingObstacles(endNode)
// Find a path from the start node to the end node using the graph.
let path:[GKGraphNode] = graph.findPathFromNode(startNode, toNode: endNode)
// If the path has 0 nodes, then a path could not be found, so return.
guard path.count > 0 else { moving = false; return }
現(xiàn)在我們獲得了 player的路徑,避讓了障礙物。也可以通過 SKAction.followPath(path: CGPath, speed: CGFloat)方法來創(chuàng)建更好的路徑。但這里我們選擇從每個(gè)節(jié)點(diǎn)通過時(shí)是直線移動(dòng),可以讓路徑的算法,看起來非常明確。在實(shí)際的游戲項(xiàng)目中,或許會(huì)更多的使用 SKAction.followPath 方法。
下面的代碼為 moveTO SKAction 創(chuàng)建路徑上的和障礙物之間的間隙,然后把他們串起來。
// Create an array of actions that the player node can use to follow the path.
var actions = [SKAction]()
for node:GKGraphNode in path {
if let point2d = node as? GKGraphNode2D {
let point = CGPoint(x: CGFloat(point2d.position.x), y: CGFloat(point2d.position.y))
let action = SKAction.moveTo(point, duration: 1.0)
actions.append(action)
}
}
// Convert those actions into a sequence action, then run it on the player node.
let sequence = SKAction.sequence(actions)
player?.runAction(sequence, completion: { () -> Void in
// When the action completes, allow the player to move again.
self.moving = false
})
}
現(xiàn)在,當(dāng)你在場(chǎng)景中點(diǎn)擊一下, player 就會(huì)移動(dòng)到你點(diǎn)擊的地方,并且避開障礙物。如果你點(diǎn)到某個(gè) Node的中心,或者無法到達(dá)的地方,那么 player 就不會(huì)移動(dòng)。
結(jié)果
下面的視頻展示了游戲的過程,你可以注意觀察 player是如何避讓障礙并移動(dòng)到遠(yuǎn)點(diǎn)的。
這里非常短暫的展示了一下pathfinding的特性。 接下來,我們會(huì)在下一篇中更加詳細(xì)的展示 GameplayKit 在開發(fā)中的一些新特性是如何幫助開發(fā)者的。
延伸閱讀
想了解更多關(guān)于 GameplayKit的新特性,推薦觀看 WWDC 2015 的 session 108 Introducing GameplayKit.另外,你可以在 Github 下載到我們這篇文章中的 Demo 代碼。
這是一個(gè)系列文章,查看更多請(qǐng)移步目錄頁