/// <summary> /// Updates the following entity associated with this process. /// </summary> /// <param name="deltaTime">Elapsed time since last frame.</param> public override void Run(float deltaTime) { //if (_entity.IsEnabled == false) return; CameraNode camera = _entity as CameraNode; // Work out the central points. Transformation entityTransform = _entity.CalculateTransformation(); Transformation targetTransform = _targetEntity.CalculateTransformation(); // If its a camera then we need to invert the coordinates as it uses // slightly different ones from normal entities. if (camera != null) { entityTransform.X = -entityTransform.X; entityTransform.Y = -entityTransform.Y; } float entityTransformCenterX = entityTransform.X + ((_entity.BoundingRectangle.Width / 2) * entityTransform.ScaleX); float entityTransformCenterY = entityTransform.Y + ((_entity.BoundingRectangle.Height / 2) * entityTransform.ScaleY); float targetEntityTransformCenterX = targetTransform.X + ((_targetEntity.BoundingRectangle.Width / 2) * targetTransform.ScaleX); float targetEntityTransformCenterY = targetTransform.Y + ((_targetEntity.BoundingRectangle.Height / 2) * targetTransform.ScaleY); float vectorX = entityTransformCenterX - targetEntityTransformCenterX; float vectorY = entityTransformCenterY - targetEntityTransformCenterY; float distance = (float)Math.Sqrt(vectorX * vectorX + vectorY * vectorY); vectorX /= distance; vectorY /= distance; // Are we set to instantly snap to the target? if (_bufferZoneRadius == 0.0f || _followSpeed == 0.0f) { _entity.Position((float)Math.Floor(targetEntityTransformCenterX - ((_entity.BoundingRectangle.Width / 2) * entityTransform.ScaleX)), (float)Math.Floor(targetEntityTransformCenterY - ((_entity.BoundingRectangle.Width / 2) * entityTransform.ScaleX)), targetTransform.Z); return; } // Are we at the correct place? If so why bother with the below code :P. if (entityTransformCenterX == targetEntityTransformCenterX && entityTransformCenterY == targetEntityTransformCenterY) { return; } // Are we in the stationary buffer zone? if (distance <= _bufferZoneRadius) { return; // Yep. } distance = (int)distance; // Work out vector towards entity. float scaleAmount = (distance / _bufferZoneRadius); //if (distance > _bufferZoneRadius * 2) scaleAmount *= 2; float movementX = (float)Math.Round(vectorX * (_followSpeed * scaleAmount), 1); // *deltaTime; float movementY = (float)Math.Round(vectorY * (_followSpeed * scaleAmount), 1); // *deltaTime; movementX = (int)movementX; movementY = (int)movementY; if (!float.IsNaN(movementX) && !float.IsNaN(movementY)) { _entity.Move(-movementX, -movementY, 0.0f); } }
/// <summary> /// Updates the following entity associated with this process. /// </summary> /// <param name="deltaTime">Elapsed time since last frame.</param> public override void Run(float deltaTime) { //if (_entity.IsEnabled == false) return; // Work out the central points. Transformation entityTransform = _entity.CalculateTransformation(); // If its a camera then we need to invert the coordinates as it uses // slightly different ones from normal entities. if (_entity is CameraNode) { entityTransform.X = -entityTransform.X; entityTransform.Y = -entityTransform.Y; } // Are we at the correct place? If so why bother with the below code :P. if (entityTransform.X == _x && entityTransform.Y == _y) { Finish(ProcessResult.Success); return; } // If we can't move then finish. if (_gotPreviousTransformation == true) { if (_previousTransformation.X == entityTransform.X && _previousTransformation.Y == entityTransform.Y && _previousTransformation.Z == entityTransform.Z) { if (_previousTransformationTimer.DurationMillisecond > 500) // Half a second and no movement O_o, oh shiz we must have something in out way. { Finish(ProcessResult.Failed); return; } } else { _previousTransformationTimer.Restart(); _previousTransformation = entityTransform; } } else { _previousTransformation = entityTransform; _previousTransformationTimer.Restart(); _gotPreviousTransformation = true; } float entityTransformCenterX = entityTransform.X + ((_entity.BoundingRectangle.Width / 2) * entityTransform.ScaleX); float entityTransformCenterY = entityTransform.Y + ((_entity.BoundingRectangle.Height / 2) * entityTransform.ScaleY); float vectorX = entityTransformCenterX - _x; float vectorY = entityTransformCenterY - _y; float distance = (float)Math.Sqrt(vectorX * vectorX + vectorY * vectorY); vectorX /= distance; vectorY /= distance; float followSpeed = _followSpeed.Start + (((_followSpeed.Finish - _followSpeed.Start) / _originalDistance) * (_originalDistance - distance)); // Work out vector towards entity. float movementX = (vectorX * followSpeed) * deltaTime; float movementY = (vectorY * followSpeed) * deltaTime; _entity.Move(-movementX, -movementY, 0.0f); // Are we done yet? if (Math.Abs(Math.Round(distance)) <= Math.Max(1, _followSpeed.Finish) * 2)// || followSpeed == _followSpeed.Finish) { Finish(ProcessResult.Success); return; } }