iOS9 Day-by-Day :: Day 11 :: Gameplay Kit – Pathfinding

原文地址
這是一個系列文章,查看更多請移步目錄頁

在之前的發布的 iOS 版本中,蘋果就已經非常重視,讓開發者編寫游戲更簡單。他們在 iOS 7 中介紹了 SpriteKit。 SpriteKit 是一個 2D 的圖形和動畫的庫,你可以用來為 iOS 和 OS X 平臺編寫可交互的游戲。2012年的時候,他們又為 Mac 平臺提供了 SceneKit 庫,在 WWDC 2014 時,又將其拓展到了 iOS 平臺,并增加了一些新的特性,例如粒子系統和物理模擬。

同時用過這兩個庫后,我個人可以作證,這兩個庫都是非常好用的。當你在游戲中用來展示可視化的元素時,他們非常有用。但是,畢竟我開發游戲的經驗不多,我經常比較疑惑的是,如何去架構一個游戲項目,如果去構建模型,以及如何處理它們之間的關系。

隨著 iOS 9 的發布,蘋果試圖通過一些方法來幫助開發者解決這些問題。他們介紹了一個新的庫,GameplayKit,他是一組工具集,提供一系列的在 iOS 和 OS X 平臺上開發的技術。

和高級別的游戲引擎,例如 SpriteKit 和 SceneKit 不同,GameplayKit 并不包括動畫和渲染內容等,相應的,你可以使用 GameplayKit 來開發你的游戲玩法和機制,設置模型,使架構做到最小組件化和可伸縮。
-- 來自蘋果文檔中關于 GameplayKit 介紹部分。

這個庫包涵了一下特性

  • Randomisation
  • Entities and Components
  • State Machines
  • Pathfinding
  • Agents, Goals & Behaviours
  • Rule Systems

這篇文章,著重介紹 pathfinding 在 GameplayKit 中的對應 API,當然也會涉及到一些其它部分。

創建一個 PathFinding 的例子

現在我們來創建一個簡單的 SpriteKit 示例項目,來示范一下 GameplayKit 中 pathfinding 相關的API.

首先,在 Xcode 中創建一個 SpriteKit 類型游戲項目。


setup.png

它會自動創建一個基于模版的基本游戲項目,下一步,我們打開 GameScene.sks文件,來添加幾個節點。首先我們創建一個代表玩家的節點,我們希望它在迷宮中可以移動。

player.png

注意一下在右側的 property inspector,我們把name 設置為“player”,后面我們會用它來和這個節點進行關聯。

接下來,我們添加更多的節點。以讓玩家去在迷宮中避讓。否則的話,這個pathfinding 就太簡單了。

maze.png

使用 scene editor 拖拽一些 node 到場景中。你可以想上圖一樣去布置。簡單也好、復雜也可以。只要能夠保證,當玩家點擊了某個特定點后,在通過時需要避讓就行。你無須對這些節點進行修飾,讓他們保持簡單的矩形就好了。

接下來,打開 GameScene.swift 文件,重載 touchesBegan 方法。我們將使用用戶點擊的點,作為路徑的終點。

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch: AnyObject in touches {
    let location = (touch as! UITouch).locationInNode(self)
    self.movePlayerToLocation(location)
}}

一旦我們發現用戶點擊了,我們能要創建一個從玩家的當前點到點擊的點之間的路徑,同時這個路徑要避讓障礙物。為了做這些,我們需要創建一個 movePlayerToLocation 方法。

func movePlayerToLocation(location: CGPoint) {

// Ensure the player doesn't move when they are already moving.
guard (!moving) else {return}
moving = true

首先我們需要獲得 player,我們可以通過 childNodeWithName 方法來獲取。在前面我們已經通過 scene editor 給它命名好了。

 // Find the player in the scene.
let player = self.childNodeWithName("player")

當我們獲取到障礙物的數組后,我們要計算從 player 的當前點到終點的路徑。

// 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 以后,我們要建立一個數組,把其它節點放進去。這是我們需要讓 player 避讓的障礙物數組。

// 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 }

現在我們獲得了 player的路徑,避讓了障礙物。也可以通過 SKAction.followPath(path: CGPath, speed: CGFloat)方法來創建更好的路徑。但這里我們選擇從每個節點通過時是直線移動,可以讓路徑的算法,看起來非常明確。在實際的游戲項目中,或許會更多的使用 SKAction.followPath 方法。

下面的代碼為 moveTO SKAction 創建路徑上的和障礙物之間的間隙,然后把他們串起來。

// 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
})

}

現在,當你在場景中點擊一下, player 就會移動到你點擊的地方,并且避開障礙物。如果你點到某個 Node的中心,或者無法到達的地方,那么 player 就不會移動。

結果

下面的視頻展示了游戲的過程,你可以注意觀察 player是如何避讓障礙并移動到遠點的。

視頻鏈接

這里非常短暫的展示了一下pathfinding的特性。 接下來,我們會在下一篇中更加詳細的展示 GameplayKit 在開發中的一些新特性是如何幫助開發者的。

延伸閱讀

想了解更多關于 GameplayKit的新特性,推薦觀看 WWDC 2015 的 session 108 Introducing GameplayKit.另外,你可以在 Github 下載到我們這篇文章中的 Demo 代碼。

這是一個系列文章,查看更多請移步目錄頁

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容