Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- namespace Kalita
- {
- public class PointAvoid : AvoidBase
- {
- public List<Vector2> Points = new List<Vector2>();
- protected override IEnumerator AvoidCoroutine(MobComponent mob, Collider2D collision)
- {
- var polygon = collision as PolygonCollider2D;
- if (polygon != null)
- {
- var numberOfPoints = polygon.GetTotalPointCount();
- Vector2 center = polygon.bounds.center;
- var OriginalPoints = polygon.GetPath(0);
- for (int i = 0; i <= numberOfPoints - 1; i++)
- {
- var modifyingVector = (polygon.points[i] - center).normalized; //!!!!!!!!!!!!!!!
- Points.Add(polygon.transform.TransformPoint(OriginalPoints[i])); //+ modifyingVector*2
- }
- for (int i = 0; i <= numberOfPoints - 1; i++)
- {
- Debug.Log(Points[i]);
- }
- var closestPoint = Points[0];
- var indexOfClosestPoint = 0;
- for (int i = 1; i <= Points.Count - 1; i++)
- {
- if (Vector3.Distance(mob.Position, Points[i]) < Vector3.Distance(mob.Position, closestPoint))
- {
- closestPoint = Points[i];
- indexOfClosestPoint = i;
- }
- }
- Debug.Log("Индекс ближайшей точки — " + indexOfClosestPoint);
- Debug.Log("Позиция моба — " + mob.Position);
- int sign;
- if (indexOfClosestPoint == numberOfPoints - 1)
- {
- sign = Vector3.Distance(mob.Position, Points[indexOfClosestPoint - 1]) <
- Vector3.Distance(mob.Position, Points[0])
- ? -1
- : 1;
- }
- else if (indexOfClosestPoint == 0)
- {
- sign = Vector3.Distance(mob.Position, Points[Points.Capacity - 1]) < //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- Vector3.Distance(mob.Position, Points[indexOfClosestPoint + 1])
- ? -1
- : 1;
- }
- else
- {
- sign = Vector3.Distance(mob.Position, Points[indexOfClosestPoint - 1]) <
- Vector3.Distance(mob.Position, Points[indexOfClosestPoint + 1])
- ? -1
- : 1;
- }
- Debug.Log("Индекс ближайшей точки — "+indexOfClosestPoint);
- var indexOfDestination = DetectIndexOfDestination(mob, indexOfClosestPoint, sign);
- Debug.Log("Индекс ближайшей точки после этого хитровыебанного метода — " + indexOfClosestPoint);
- Rotation(mob, Points[indexOfDestination]);
- Debug.Log("Куда ты сука идешь? Сюда бля — "+ Points[indexOfDestination]);
- var wait = new WaitForSeconds(0.01f);
- for (int i = 0; i <= 666; i++)
- {
- while (Vector3.Distance(mob.Position, Points[indexOfDestination]) > 0.05f)
- {
- Vector2 mobPos = mob.Position;
- Vector3 direction = mobPos - Points[indexOfDestination];
- if (!mob.MobLifeModule.IsAlive)
- StopAvoid();
- mob.Position = mob.Position + direction*1*Time.deltaTime;
- yield return wait;
- }
- if (IsPathClear(mob, collision))
- {
- StopAvoid();
- break;
- }
- indexOfDestination = DetectIndexOfDestination(mob, indexOfDestination, sign);
- Rotation(mob,Points[indexOfDestination]);
- }
- }
- }
- protected int DetectIndexOfDestination(MobComponent mob, int currentIndex, int sign)
- {
- if (currentIndex == Points.Capacity - 1 && sign > 0)
- {
- return 0;
- }
- if (currentIndex == 0 && sign < 0)
- {
- return Points.Count - 1;
- }
- return currentIndex + sign;
- }
- protected bool IsPathClear(MobComponent mob, Collider2D collision)
- {
- var lm = LayerMask.GetMask("Obstacles", "IgnorBulletObst");
- Debug.DrawRay(mob.transform.position,
- (mob.MobTargeterModule.Target.transform.position - mob.transform.position).normalized*15, Color.red, 1);
- //Physics2D.queriesStartInColliders = false;
- if (
- Physics2D.Raycast(mob.transform.position,
- mob.MobTargeterModule.Target.transform.position - mob.transform.position, 15, lm).collider ==
- collision)
- {
- return false;
- }
- return true;
- }
- protected void Rotation(MobComponent mob, Vector3 newDirection)
- {
- var currentDirection = mob.MobGraphicsModule.RotatableGraphics.up.normalized;
- float rotAngleStep = 5f;
- var minus = Random.Range(0, 2) == 1 ? -1 : 1;
- while (Vector2.Angle(currentDirection, newDirection) >= rotAngleStep)
- {
- if (!mob.MobLifeModule.IsAlive)
- StopAvoid();
- currentDirection = Quaternion.Euler(0, 0, -rotAngleStep * minus) * currentDirection;
- mob.MobGraphicsModule.RotatableGraphics.up = currentDirection;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement