Advertisement
Guest User

Untitled

a guest
Jun 27th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.02 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. namespace Kalita
  6. {
  7.     public class PointAvoid : AvoidBase
  8.     {
  9.         public List<Vector2> Points = new List<Vector2>();
  10.  
  11.         protected override IEnumerator AvoidCoroutine(MobComponent mob, Collider2D collision)
  12.         {
  13.            
  14.             var polygon = collision as PolygonCollider2D;
  15.            
  16.             if (polygon != null)
  17.             {
  18.                 var numberOfPoints = polygon.GetTotalPointCount();
  19.                
  20.  
  21.                 Vector2 center = polygon.bounds.center;
  22.                 var OriginalPoints = polygon.GetPath(0);
  23.                 for (int i = 0; i <= numberOfPoints - 1; i++)
  24.                 {
  25.                     var modifyingVector = (polygon.points[i] - center).normalized; //!!!!!!!!!!!!!!!
  26.                    
  27.                     Points.Add(polygon.transform.TransformPoint(OriginalPoints[i])); //+ modifyingVector*2
  28.                 }
  29.  
  30.                 for (int i = 0; i <= numberOfPoints - 1; i++)
  31.                 {
  32.                     Debug.Log(Points[i]);
  33.                 }
  34.  
  35.                 var closestPoint = Points[0];
  36.                 var indexOfClosestPoint = 0;
  37.                 for (int i = 1; i <= Points.Count - 1; i++)
  38.                 {
  39.                     if (Vector3.Distance(mob.Position, Points[i]) < Vector3.Distance(mob.Position, closestPoint))
  40.                     {
  41.                         closestPoint = Points[i];
  42.                         indexOfClosestPoint = i;
  43.                     }
  44.                 }
  45.  
  46.                 Debug.Log("Индекс ближайшей точки — " + indexOfClosestPoint);
  47.                 Debug.Log("Позиция моба — " + mob.Position);
  48.                 int sign;
  49.  
  50.                 if (indexOfClosestPoint == numberOfPoints - 1)
  51.                 {
  52.                     sign = Vector3.Distance(mob.Position, Points[indexOfClosestPoint - 1]) <
  53.                            Vector3.Distance(mob.Position, Points[0])
  54.                         ? -1
  55.                         : 1;
  56.                 }
  57.                 else if (indexOfClosestPoint == 0)
  58.                 {
  59.                     sign = Vector3.Distance(mob.Position, Points[Points.Capacity - 1]) < //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  60.                            Vector3.Distance(mob.Position, Points[indexOfClosestPoint + 1])
  61.                         ? -1
  62.                         : 1;
  63.                 }
  64.                 else
  65.                 {
  66.                     sign = Vector3.Distance(mob.Position, Points[indexOfClosestPoint - 1]) <
  67.                            Vector3.Distance(mob.Position, Points[indexOfClosestPoint + 1])
  68.                         ? -1
  69.                         : 1;
  70.                 }
  71.                 Debug.Log("Индекс ближайшей точки — "+indexOfClosestPoint);
  72.  
  73.                 var indexOfDestination = DetectIndexOfDestination(mob, indexOfClosestPoint, sign);
  74.                 Debug.Log("Индекс ближайшей точки после этого хитровыебанного метода — " + indexOfClosestPoint);
  75.                 Rotation(mob, Points[indexOfDestination]);
  76.                 Debug.Log("Куда ты сука идешь? Сюда бля — "+ Points[indexOfDestination]);
  77.  
  78.  
  79.                 var wait = new WaitForSeconds(0.01f);
  80.  
  81.  
  82.  
  83.  
  84.                 for (int i = 0; i <= 666; i++)
  85.                 {
  86.                     while (Vector3.Distance(mob.Position, Points[indexOfDestination]) > 0.05f)
  87.                     {
  88.                         Vector2 mobPos = mob.Position;
  89.                         Vector3 direction =  mobPos - Points[indexOfDestination];
  90.                         if (!mob.MobLifeModule.IsAlive)
  91.                             StopAvoid();
  92.                         mob.Position = mob.Position + direction*1*Time.deltaTime;
  93.                         yield return wait;
  94.                     }
  95.  
  96.                     if (IsPathClear(mob, collision))
  97.                     {
  98.                         StopAvoid();
  99.                         break;
  100.                     }
  101.  
  102.                     indexOfDestination = DetectIndexOfDestination(mob, indexOfDestination, sign);
  103.                     Rotation(mob,Points[indexOfDestination]);
  104.  
  105.                 }
  106.             }
  107.         }
  108.  
  109.  
  110.         protected int DetectIndexOfDestination(MobComponent mob, int currentIndex, int sign)
  111.         {
  112.  
  113.             if (currentIndex == Points.Capacity - 1 && sign > 0)
  114.             {
  115.                 return 0;
  116.             }
  117.  
  118.             if (currentIndex == 0 && sign < 0)
  119.             {
  120.                 return Points.Count - 1;
  121.             }
  122.  
  123.             return currentIndex + sign;
  124.         }
  125.  
  126.         protected bool IsPathClear(MobComponent mob, Collider2D collision)
  127.         {
  128.  
  129.             var lm = LayerMask.GetMask("Obstacles", "IgnorBulletObst");
  130.             Debug.DrawRay(mob.transform.position,
  131.                 (mob.MobTargeterModule.Target.transform.position - mob.transform.position).normalized*15, Color.red, 1);
  132.             //Physics2D.queriesStartInColliders = false;
  133.             if (
  134.                 Physics2D.Raycast(mob.transform.position,
  135.                     mob.MobTargeterModule.Target.transform.position - mob.transform.position, 15, lm).collider ==
  136.                 collision)
  137.             {
  138.                 return false;
  139.             }
  140.  
  141.             return true;
  142.         }
  143.  
  144.         protected void Rotation(MobComponent mob, Vector3 newDirection)
  145.         {
  146.             var currentDirection = mob.MobGraphicsModule.RotatableGraphics.up.normalized;
  147.             float rotAngleStep = 5f;
  148.             var minus = Random.Range(0, 2) == 1 ? -1 : 1;
  149.  
  150.             while (Vector2.Angle(currentDirection, newDirection) >= rotAngleStep)
  151.             {
  152.                 if (!mob.MobLifeModule.IsAlive)
  153.                     StopAvoid();
  154.                 currentDirection = Quaternion.Euler(0, 0, -rotAngleStep * minus) * currentDirection;
  155.                 mob.MobGraphicsModule.RotatableGraphics.up = currentDirection;
  156.             }
  157.  
  158.         }
  159.  
  160.     }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement