private TICKRESULT _GetDistance(Guid target, out float distance) { distance = 0f; var entity = _FieldOfVision.Find((e) => e.Id == target); if (entity == null) { return(TICKRESULT.FAILURE); } var mesh = _BuildMesh(entity); float dis; Vector2 point; Vector2 normal; if (RayPolygonIntersect( _Entiry.GetPosition(), Vector2.AngleToVector(_Entiry.Direction), mesh.Points, out dis, out point, out normal)) { distance = dis - _Entiry.GetDetectionRange(); return(TICKRESULT.SUCCESS); } return(TICKRESULT.FAILURE); }
private TICKRESULT _GetTargetAngle(Guid id, ref float angle) { var target = _Find(id); if (target == null) { return(TICKRESULT.FAILURE); } var position = _Entiry.GetPosition(); var diff = target.Position - position; var a = Vector2.VectorToAngle(diff.GetNormalized()); a += 360; a %= 360; angle = a - _Entiry.Direction; #if UNITY_EDITOR var distance = target.Position.DistanceTo(position); var trunForce = Vector2.AngleToVector(a); var forcePos = position + trunForce * (distance); UnityEngine.Debug.DrawLine(new UnityEngine.Vector3(position.X, 0, position.Y), new UnityEngine.Vector3(forcePos.X, 0, forcePos.Y), UnityEngine.Color.green, _DecisionTime); //UnityEngine.Debug.Log("TurnDirection = " + _GoblinWisdom.TurnDirection); #endif return(TICKRESULT.SUCCESS); }
private Hit _Detect(float scan_angle, float max_distance) { scan_angle += 360f; scan_angle %= 360f; var pos = _Entiry.GetPosition(); var view = Vector2.AngleToVector(scan_angle); //Unity Debug Code #if UNITY_EDITOR UnityEngine.Debug.DrawRay(new UnityEngine.Vector3(pos.X, 0, pos.Y), new UnityEngine.Vector3(view.X, 0, view.Y), UnityEngine.Color.blue, 0.5f); #endif var hit = new Hit(); hit.HitPoint = pos + view * max_distance; hit.Distance = max_distance; foreach (var visible in _FieldOfVision) { var mesh = StandardBehavior._BuildMesh(visible); float dis; Vector2 normal; Vector2 point; if (RayPolygonIntersect(pos, view, mesh.Points, out dis, out point, out normal)) { if (dis < hit.Distance) { hit.Visible = visible; hit.HitPoint = point; hit.Distance = dis; } } } return(hit); }