bool IComponent.Update(float elapsed) { if (paused) { return(true); } timerCheck += elapsed; if (timerCheck >= timerCheckLenght) { FollowBehaviorComponent_TriangleChangedEvent(); timerCheck = 0; } pathRequestRateCounter += elapsed; pathRequestRateCounter = 0f; if (path == null) { owner.body.LinearVelocity = Vector2.Zero; return(false); } var nextNode = path[1]; Vector2 posToNode = nextNode.position - owner.getPosNode().position; Vector2 posToTarget = target.body.Position - owner.body.Position; if (path.Count > 2 && posToNode.Length() < 0.3) { path.RemoveAt(1); posToNode = nextNode.position - owner.getPosNode().position; posToTarget = target.body.Position - owner.body.Position; } if (posToTarget.Length() * Bloodbender.meterToPixel > escapeZoneRadius) { posToNode.Normalize(); posToNode *= owner.velocity * Bloodbender.pixelToMeter; owner.body.LinearVelocity = posToNode; } else { owner.body.LinearVelocity = Vector2.Zero; } return(true); }
public FollowBehaviorComponent(PhysicObj obj, PhysicObj target, float escapeZoneRadius) { this.owner = obj; this.target = target; this.escapeZoneRadius = escapeZoneRadius; maxVertexDistance = maxLenghtCentroidVertex(); nextNode = null; target.getPosNode().TriangleChangedEvent += FollowBehaviorComponent_TriangleChangedEvent; previousTargetPosition = target.body.Position; previousTargetPosition += new Vector2(10, 10); }
public void Remove() { target.getPosNode().TriangleChangedEvent -= FollowBehaviorComponent_TriangleChangedEvent; }