我已經插入了一些行爲例程的GKBehavior/GKAgent2D/GKComponent系統,並且無法解決我找不到的這個錯誤。 首先讓我描述一下: 我目前的實體/組件系統基於蘋果的ECS,其中: 1-entitymanager:GKEntity管理所有組件幷包含遊戲中的所有實體,並使用updateWithDelta進入所有GKComponentSystems,在他們的系統內。 目前,更新順序如下: behavior> node> playerNode> sound> interface> physics 2-每個系統都是一個GKComponentSystem,它處理更新其中所有元素。 我有NSViewController的兩個擴展名爲GameRenderer和GameControls,它們都是SCNSceneRendererDelegate的委託,還有一個名爲KBAndTouchDelegate的自定義協議,分別在屏幕和控件上更新渲染(一旦我的訂單更新感覺合適,它將更改爲在ECS中合併)。 現在,在行爲componentSystem中,它更新一個MoveComponent,其中包含一些用於更新的函數(willUpdate和didUpdate)。兩者都有一個2DAgent位置,首先從SCNNode的位置取得(從SCNVector3轉換到Float2,只更改x和z座標),然後在評估行爲算法後反射回節點。 以下是更新片段:黨GameplayKit:NPC元素將會浮動數字很高並且更新
func agentWillUpdate(agent: GKAgent) {
guard let nodeComponent = entity?.componentForClass(SCNNodeComponent.self)
else{
return
}
let nodePos = nodeComponent.node.position
position = float2(Float(nodePos.x), Float(nodePos.z))
}
func agentDidUpdate(agent: GKAgent) {
guard let nodeComponent = entity?.componentForClass(SCNNodeComponent.self)
else{
return
}
let pos = SCNVector3(x: CGFloat(position.x), y: nodeComponent.node.position.y, z: CGFloat(position.y))
print("\(nodeComponent.node.name) changed to \(pos)")
nodeComponent.node.position = pos
}
的@override updateWithDeltaTime例行檢查,其中moveComponent的所有者,並放置在一個陣列用於獲取最接近敵人的距離,並返回敵人。 該行爲具有非常低的maxSpeed和maxVelocity因子。 運行這個應用程序,我爲兩個敵人節點打印了一組動作並監視彼此的位置。敵方搜索程序正常工作,因爲它們以交替順序反映彼此的位置。結果如下:
Optional("zombie") changed to SCNVector3(x: -20.0, y: 0.0, z: -20.0)
enemy position is: (0.0, 0.0)
Optional("player") changed to SCNVector3(x: 0.0, y: 0.0, z: 0.0)
enemy position is: (-20.0, -20.0)
Optional("zombie") changed to SCNVector3(x: -20.0, y: 0.0, z: 41567.44140625)
enemy position is: (0.0, 0.0)
Optional("player") changed to SCNVector3(x: 0.0, y: 0.0, z: 0.0)
enemy position is: (-20.0, 41567.44140625)
HRTF loaded
Optional("zombie") changed to SCNVector3(x: 30.136764526367188, y: 0.0, z: -20.2421875)
enemy position is: (0.0, 0.0)
Optional("player") changed to SCNVector3(x: 0.0, y: 0.0, z: 0.0)
enemy position is: (30.1367645263672, -20.2421875)
Optional("zombie") changed to SCNVector3(x: -38636.87109375, y: 0.0, z: 15289.939453125)
如果在看「殭屍」的第一個位置,你會看到它開始於-20,-20則無二x和z位置(再不可能大值,X和場景世界中的z)。玩家不動,我不使用任何控件只是爲了看看殭屍能夠與以下GKBehavior呼叫
init(targetSpeed: Float, seek: GKAgent, avoid: [GKAgent]){
super.init()
if targetSpeed>0{
setWeight(0.1, forGoal: GKGoal(toReachTargetSpeed: targetSpeed))
setWeight(0.5, forGoal: GKGoal(toSeekAgent: seek))
setWeight(1.0, forGoal: GKGoal(toAvoidAgents: avoid, maxPredictionTime: 1.0))
}
}
上述行爲被稱爲MoveComponent對殭屍的敵人找到播放器(玩家)。意思是殭屍會尋找玩家。 任何人都可以告訴我這些x和z的ginormous值是什麼嗎?殭屍和玩家約爲sqrt(800)或28米外。但是殭屍會在35800米外的東西上x,然後在每個框架上回到50左右。 有一個確定的模式,我不確定這個GKBehavior是否使用了一個星形,在這個點上,概率路標設置了一些非常奇怪的座標樣本,但是在我的代碼中沒有任何標記可以將殭屍移動到很遠的地方並翻轉距離回到每幀合理正常的事情。