예제 #1
0
 /// <summary>
 /// 他のオブジェクトを検出
 /// </summary>
 private void OnDetect(DetectableObject2D obj)
 {
     //武器を起動
     if (weapon)
     {
         weapon.WeaponAwake();
     }
 }
예제 #2
0
 /// <summary>
 /// 検出オブジェクトを解放
 /// </summary>
 private void OnRelease(DetectableObject2D obj)
 {
     if (obj.transform == target)
     {
         thruster.ThrusterStandby();
         target = null;
         weapon.WeaponStandby();
     }
 }
예제 #3
0
 /// <summary>
 /// 他のオブジェクトを検出
 /// </summary>
 private void OnDetect(DetectableObject2D obj)
 {
     //フィルター
     if (obj.tag.Equals(targetTag))
     {
         thruster.ThrusterAwake();
         target = obj.transform;
         weapon.WeaponAwake();
     }
 }
예제 #4
0
 /// <summary>
 /// 他のオブジェクトを解放
 /// </summary>
 private void OnRelease(DetectableObject2D obj)
 {
     //武器を待機
     if (weapon)
     {
         weapon.SetAngle(0f);
         if (detector.GetDetectCount() < 1)
         {
             weapon.WeaponStandby();
         }
     }
 }
예제 #5
0
        /// <summary>
        /// 解除
        /// </summary>
        private void OnRelease(DetectableObject2D obj)
        {
            Debug.Log("OnRelease : " + obj);
            //ターゲッティングの優先順位の更新

            //仮
            if (target == obj)
            {
                target = null;
            }
            wManager.Disactivate();
        }
예제 #6
0
 private void Start()
 {
     collider2D = GetComponent <Collider2D>();
     attackable = GetComponent <AttackableObject2D>();
     if (attackable)
     {
         attackable.OnAttacked.RemoveListener(OnAttacked);
         attackable.OnAttacked.AddListener(OnAttacked);
         attackable.OnDied.RemoveListener(OnDied);
         attackable.OnDied.AddListener(OnDied);
     }
     detectable = GetComponent <DetectableObject2D>();
 }
예제 #7
0
        /// <summary>
        /// 検出
        /// </summary>
        private void OnDetect(DetectableObject2D obj)
        {
            Debug.Log("OnDetect : " + obj);
            //ターゲッティングの優先順位の更新

            //カメラサイズの変更
            if (cameraSize)
            {
                cameraSize.SetCameraSize(detector.GetDetectCount() * 1f + 20f);
            }

            //仮
            target      = obj;
            targetTrans = obj.transform;
            wManager.Activate();
        }
예제 #8
0
 /// <summary>
 /// 対象との角度の調整
 /// </summary>
 private void SetTargetAngle()
 {
     if (detector && detector.GetDetectCount() > 0)
     {
         //距離計算
         float d1 = 0f, d2 = float.MaxValue;
         DetectableObject2D near = null;
         foreach (var obj in detector.Objects)
         {
             d1 = Vector2.Distance(trans.position, obj.transform.position);
             if (d1 < d2)
             {
                 d2   = d1;
                 near = obj;
             }
         }
         weapon.SetTargetAngle(near.transform);
     }
 }
예제 #9
0
 /// <summary>
 /// 初期化
 /// </summary>
 public override void InitCom(ShipStructure structure)
 {
     base.InitCom(structure);
     structure.Marker = this;
     detectableObj    = GetComponent <DetectableObject2D>();
 }
예제 #10
0
 public static bool RemoveDetectableObject2D(DetectableObject2D obj)
 {
     return(_knownDetectableObjects.Remove(obj.Collider.GetInstanceID()));
 }
예제 #11
0
 public static void AddDetectableObject2D(DetectableObject2D obj)
 {
     _knownDetectableObjects[obj.Collider.GetInstanceID()] = obj;
 }
 public PathIntersection(DetectableObject2D obstacle)
 {
     Obstacle  = obstacle;
     Intersect = false;
     Distance  = float.MaxValue;
 }
    /// <summary>
    /// Finds a vehicle's next intersection with a spherical obstacle
    /// </summary>
    /// <param name="vehicle">
    /// The vehicle to evaluate.
    /// </param>
    /// <param name="futureVehiclePosition">
    /// The position where we expect the vehicle to be soon
    /// </param>
    /// <param name="obstacle">
    /// A spherical obstacle to check against <see cref="DetectableObject"/>
    /// </param>
    /// <returns>
    /// A PathIntersection with the intersection details <see cref="PathIntersection"/>
    /// </returns>
    /// <remarks>We could probably spin out this function to an independent tool class</remarks>
    public static PathIntersection FindNextIntersectionWithSphere(Vehicle2D vehicle, Vector2 futureVehiclePosition,
                                                                  DetectableObject2D obstacle)
    {
        // this mainly follows http://www.lighthouse3d.com/tutorials/maths/ray-sphere-intersection/

        var intersection = new PathIntersection(obstacle);

        var combinedRadius = vehicle.Radius + obstacle.Radius;
        var movement       = futureVehiclePosition - vehicle.Position;
        var direction      = movement.normalized;

        var vehicleToObstacle = obstacle.Position - vehicle.Position;

        // this is the length of vehicleToObstacle projected onto direction
        var projectionLength = Vector2.Dot(direction, vehicleToObstacle);

        // if the projected obstacle center lies further away than our movement + both radius, we're not going to collide
        if (projectionLength > movement.magnitude + combinedRadius)
        {
            //print("no collision - 1");
            return(intersection);
        }

        // the foot of the perpendicular
        var projectedObstacleCenter = vehicle.Position + projectionLength * direction;

        // distance of the obstacle to the pathe the vehicle is going to take
        var obstacleDistanceToPath = (obstacle.Position - projectedObstacleCenter).magnitude;

        //print("obstacleDistanceToPath: " + obstacleDistanceToPath);

        // if the obstacle is further away from the movement, than both radius, there's no collision
        if (obstacleDistanceToPath > combinedRadius)
        {
            //print("no collision - 2");
            return(intersection);
        }

        // use pythagorean theorem to calculate distance out of the sphere (if you do it 2D, the line through the circle would be a chord and we need half of its length)
        var halfChord = Mathf.Sqrt(combinedRadius * combinedRadius + obstacleDistanceToPath * obstacleDistanceToPath);

        // if the projected obstacle center lies opposite to the movement direction (aka "behind")
        if (projectionLength < 0)
        {
            // behind and further away than both radius -> no collision (we already passed)
            if (vehicleToObstacle.magnitude > combinedRadius)
            {
                return(intersection);
            }

            var intersectionPoint = projectedObstacleCenter - direction * halfChord;
            intersection.Intersect = true;
            intersection.Distance  = (intersectionPoint - vehicle.Position).magnitude;
            return(intersection);
        }

        // calculate both intersection points
        var intersectionPoint1 = projectedObstacleCenter - direction * halfChord;
        var intersectionPoint2 = projectedObstacleCenter + direction * halfChord;

        // pick the closest one
        var intersectionPoint1Distance = (intersectionPoint1 - vehicle.Position).magnitude;
        var intersectionPoint2Distance = (intersectionPoint2 - vehicle.Position).magnitude;

        intersection.Intersect = true;
        intersection.Distance  = Mathf.Min(intersectionPoint1Distance, intersectionPoint2Distance);

        return(intersection);
    }