我已经为某些行为例程插入了GKBehavior / GKAgent2D / GKComponent系统,但无法解决我不知道的错误。
首先让我描述一下:
我当前的实体/组件系统基于Apple的ECS,其中:
1-entitymanager:GKEntity管理所有组件并包含游戏中的所有实体,并使用updateWithDelta进入所有GKComponentSystems中,这些GKComponentSystems本身会更新系统中的每个组件。
当前,更新顺序如下:
行为>节点> playerNode>声音>接口>物理
2-每个系统都是一个GKComponentSystem,它负责更新其中的所有元素。
我有两个名为GameRenderer和GameControls的NSViewController扩展,它们都是SCNSceneRendererDelegate的委托,还有一个名为KBAndTouchDelegate的自定义协议,分别更新屏幕和控件上的渲染(一旦我的订单更新正确,将更改为合并到ECS中)。
现在,在行为componentSystem中,它更新了一个MoveComponent,其中包含一些用于更新的功能(willUpdate和didUpdate)。两者都有一个2DAgent位置,该位置首先从SCNNode的位置获取(从SCNVector3转换为Float2,仅更改了x和z坐标),然后在评估了行为算法后,将其反射回该节点。
以下是更新的摘录:
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的这些巨大值到底是什么?僵尸和玩家大约在sqrt(800)或28米外。但是僵尸在x上到达35800米远的地方,然后切换回大约50,然后在每个帧之间来回移动。
有一个确定的模式,我不确定这个GKBehavior是否使用A星,此时概率路线图设置了一些非常奇异的坐标样本,但是在我的代码中没有任何标量可以将僵尸移动得如此之远并向后翻转距离到合理的每一帧。
最佳答案
对于可能有相同问题的任何人,截至昨天(2016年3月21日),Apple已使用最新的x代码7.3公共更新解决了该问题。
关于swift - GameplayKit:NPC元素将具有很高的浮点数坐标并在更新后恢复,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35782773/