Ejemplo n.º 1
0
        public static bool TryToChangeState(Beans player)
        {
            // 到達可能な Obstacle を目指す
            var obstacles = GameObject.FindGameObjectsWithTag("Obstacle")
                            .ToList();
            var reachables = new List <GameObject>();

            foreach (var obstacle in obstacles)
            {
                var  hit  = new UnityEngine.AI.NavMeshHit();
                bool ret  = player.Agent.Raycast(obstacle.transform.position, out hit);
                var  diff = (hit.position - obstacle.transform.position);
                diff.y = 0.0f;
                if (diff.magnitude <= 1.0f)
                {
                    reachables.Add(obstacle);
                }
            }

            if (reachables.Any())
            {
                var target = obstacles[UnityEngine.Random.Range(0, reachables.Count)];
                StateToObstacle.ChangeState(player, target);
                return(true);
            }

            return(false);
        }
Ejemplo n.º 2
0
        private bool Find(Vector3 targetPosition)
        {
            if (HorDistance(transform.position, targetPosition) < cornerRadius * 2f)
            {
                return(false);
            }
            //if (HorDistance(targetPosition, lastTargetPosition) < recalculateBadTargetDistance) return false;
            if (UnityEngine.AI.NavMesh.CalculatePath(transform.position, targetPosition, UnityEngine.AI.NavMesh.AllAreas, path))
            {
                return(true);
            }
            else
            {
                UnityEngine.AI.NavMeshHit hit = new UnityEngine.AI.NavMeshHit();

                if (UnityEngine.AI.NavMesh.SamplePosition(targetPosition, out hit, maxSampleDistance, UnityEngine.AI.NavMesh.AllAreas))
                {
                    if (UnityEngine.AI.NavMesh.CalculatePath(transform.position, hit.position, UnityEngine.AI.NavMesh.AllAreas, path))
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Read the data using the reader.
        /// </summary>
        /// <param name="reader">Reader.</param>
        public override object Read(ISaveGameReader reader)
        {
            UnityEngine.AI.NavMeshHit navMeshHit = new UnityEngine.AI.NavMeshHit();
            foreach (string property in reader.Properties)
            {
                switch (property)
                {
                case "position":
                    navMeshHit.position = reader.ReadProperty <UnityEngine.Vector3> ();
                    break;

                case "normal":
                    navMeshHit.normal = reader.ReadProperty <UnityEngine.Vector3> ();
                    break;

                case "distance":
                    navMeshHit.distance = reader.ReadProperty <System.Single> ();
                    break;

                case "mask":
                    navMeshHit.mask = reader.ReadProperty <System.Int32> ();
                    break;

                case "hit":
                    navMeshHit.hit = reader.ReadProperty <System.Boolean> ();
                    break;
                }
            }
            return(navMeshHit);
        }
        static object PerformMemberwiseClone(ref object o)
        {
            var ins = new UnityEngine.AI.NavMeshHit();

            ins = (UnityEngine.AI.NavMeshHit)o;
            return(ins);
        }
Ejemplo n.º 5
0
        public override Vector3[] GetPointsArray(Vector3 startPosition, Vector3 targetPosition, AC.Char _char = null)
        {
            UnityEngine.AI.NavMeshPath _path = new UnityEngine.AI.NavMeshPath();

            if (!UnityEngine.AI.NavMesh.CalculatePath(startPosition, targetPosition, -1, _path))
            {
                // Could not find path with current vectors
                float maxDistance  = 0.001f;
                float originalDist = Vector3.Distance(startPosition, targetPosition);

                UnityEngine.AI.NavMeshHit hit = new UnityEngine.AI.NavMeshHit();
                for (maxDistance = 0.001f; maxDistance < originalDist; maxDistance += 0.05f)
                {
                    if (UnityEngine.AI.NavMesh.SamplePosition(startPosition, out hit, maxDistance, -1))
                    {
                        startPosition = hit.position;
                        break;
                    }
                }

                bool foundNewEnd = false;
                for (maxDistance = 0.001f; maxDistance < originalDist; maxDistance += 0.05f)
                {
                    if (UnityEngine.AI.NavMesh.SamplePosition(targetPosition, out hit, maxDistance, -1))
                    {
                        targetPosition = hit.position;
                        foundNewEnd    = true;
                        break;
                    }
                }

                if (!foundNewEnd)
                {
                    return(new Vector3[0]);
                }

                UnityEngine.AI.NavMesh.CalculatePath(startPosition, targetPosition, -1, _path);
            }

            List <Vector3> pointArray = new List <Vector3>();

            foreach (Vector3 corner in _path.corners)
            {
                pointArray.Add(corner);
            }
            if (pointArray.Count > 1 && pointArray[0].x == startPosition.x && pointArray[0].z == startPosition.z)
            {
                pointArray.RemoveAt(0);
            }
            else if (pointArray.Count == 0)
            {
                pointArray.Clear();
                pointArray.Add(targetPosition);
            }

            return(pointArray.ToArray());
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Write the specified value using the writer.
 /// </summary>
 /// <param name="value">Value.</param>
 /// <param name="writer">Writer.</param>
 public override void Write(object value, ISaveGameWriter writer)
 {
     UnityEngine.AI.NavMeshHit navMeshHit = (UnityEngine.AI.NavMeshHit)value;
     writer.WriteProperty("position", navMeshHit.position);
     writer.WriteProperty("normal", navMeshHit.normal);
     writer.WriteProperty("distance", navMeshHit.distance);
     writer.WriteProperty("mask", navMeshHit.mask);
     writer.WriteProperty("hit", navMeshHit.hit);
 }
Ejemplo n.º 7
0
        public static Vector3 NavSamplePosition(Vector3 srcPosition)
        {
            Vector3 dstPosition = srcPosition;

            UnityEngine.AI.NavMeshHit meshHit = new UnityEngine.AI.NavMeshHit();
            int layer = 1 << UnityEngine.AI.NavMesh.GetAreaFromName("Walkable");

            if (UnityEngine.AI.NavMesh.SamplePosition(srcPosition, out meshHit, 100, layer))
            {
                dstPosition = meshHit.position;
            }
            return(dstPosition);
        }
Ejemplo n.º 8
0
        public static Vector3 RandomOnCircle(Vector3 center, float min, float max)
        {
            Vector3 randomPoint = UnityEngine.Random.insideUnitCircle * max;

            randomPoint += randomPoint.normalized;
            center      += randomPoint;
            UnityEngine.AI.NavMeshHit meshHit = new UnityEngine.AI.NavMeshHit();
            int layer = 1 << UnityEngine.AI.NavMesh.GetAreaFromName("Walkable");

            if (UnityEngine.AI.NavMesh.SamplePosition(center, out meshHit, 100, layer))
            {
                return(meshHit.position);
            }
            return(center);
        }
Ejemplo n.º 9
0
        static void WriteBackInstance(CSHotFix.Runtime.Enviorment.AppDomain __domain, StackObject *ptr_of_this_method, IList <object> __mStack, ref UnityEngine.AI.NavMeshHit instance_of_this_method)
        {
            ptr_of_this_method = ILIntepreter.GetObjectAndResolveReference(ptr_of_this_method);
            switch (ptr_of_this_method->ObjectType)
            {
            case ObjectTypes.Object:
            {
                __mStack[ptr_of_this_method->Value] = instance_of_this_method;
            }
            break;

            case ObjectTypes.FieldReference:
            {
                var ___obj = __mStack[ptr_of_this_method->Value];
                if (___obj is ILTypeInstance)
                {
                    ((ILTypeInstance)___obj)[ptr_of_this_method->ValueLow] = instance_of_this_method;
                }
                else
                {
                    var t = __domain.GetType(___obj.GetType()) as CLRType;
                    t.SetFieldValue(ptr_of_this_method->ValueLow, ref ___obj, instance_of_this_method);
                }
            }
            break;

            case ObjectTypes.StaticFieldReference:
            {
                var t = __domain.GetType(ptr_of_this_method->Value);
                if (t is ILType)
                {
                    ((ILType)t).StaticInstance[ptr_of_this_method->ValueLow] = instance_of_this_method;
                }
                else
                {
                    ((CLRType)t).SetStaticFieldValue(ptr_of_this_method->ValueLow, instance_of_this_method);
                }
            }
            break;

            case ObjectTypes.ArrayReference:
            {
                var instance_of_arrayReference = __mStack[ptr_of_this_method->Value] as UnityEngine.AI.NavMeshHit[];
                instance_of_arrayReference[ptr_of_this_method->ValueLow] = instance_of_this_method;
            }
            break;
            }
        }
Ejemplo n.º 10
0
        static StackObject *Raycast_20(ILIntepreter __intp, StackObject *__esp, IList <object> __mStack, CLRMethod __method, bool isNewObj)
        {
            CSHotFix.Runtime.Enviorment.AppDomain __domain = __intp.AppDomain;
            StackObject *ptr_of_this_method;
            StackObject *__ret = ILIntepreter.Minus(__esp, 4);

            ptr_of_this_method = ILIntepreter.Minus(__esp, 1);
            UnityEngine.AI.NavMeshQueryFilter filter = (UnityEngine.AI.NavMeshQueryFilter) typeof(UnityEngine.AI.NavMeshQueryFilter).CheckCLRTypes(StackObject.ToObject(ptr_of_this_method, __domain, __mStack));
            __intp.Free(ptr_of_this_method);
            ptr_of_this_method = ILIntepreter.Minus(__esp, 2);
            ptr_of_this_method = ILIntepreter.GetObjectAndResolveReference(ptr_of_this_method);
            UnityEngine.AI.NavMeshHit hit = (UnityEngine.AI.NavMeshHit) typeof(UnityEngine.AI.NavMeshHit).CheckCLRTypes(StackObject.ToObject(ptr_of_this_method, __domain, __mStack));
            ptr_of_this_method = ILIntepreter.Minus(__esp, 3);
            UnityEngine.Vector3 targetPosition = (UnityEngine.Vector3) typeof(UnityEngine.Vector3).CheckCLRTypes(StackObject.ToObject(ptr_of_this_method, __domain, __mStack));
            __intp.Free(ptr_of_this_method);
            ptr_of_this_method = ILIntepreter.Minus(__esp, 4);
            UnityEngine.Vector3 sourcePosition = (UnityEngine.Vector3) typeof(UnityEngine.Vector3).CheckCLRTypes(StackObject.ToObject(ptr_of_this_method, __domain, __mStack));
            __intp.Free(ptr_of_this_method);

            var result_of_this_method = UnityEngine.AI.NavMesh.Raycast(sourcePosition, targetPosition, out hit, filter);

            ptr_of_this_method = ILIntepreter.Minus(__esp, 2);
            switch (ptr_of_this_method->ObjectType)
            {
            case ObjectTypes.StackObjectReference:
            {
                var ___dst = *(StackObject **)&ptr_of_this_method->Value;
                __mStack[___dst->Value] = hit;
            }
            break;

            case ObjectTypes.FieldReference:
            {
                var ___obj = __mStack[ptr_of_this_method->Value];
                if (___obj is ILTypeInstance)
                {
                    ((ILTypeInstance)___obj)[ptr_of_this_method->ValueLow] = hit;
                }
                else
                {
                    var t = __domain.GetType(___obj.GetType()) as CLRType;
                    t.SetFieldValue(ptr_of_this_method->ValueLow, ref ___obj, hit);
                }
            }
            break;

            case ObjectTypes.StaticFieldReference:
            {
                var t = __domain.GetType(ptr_of_this_method->Value);
                if (t is ILType)
                {
                    ((ILType)t).StaticInstance[ptr_of_this_method->ValueLow] = hit;
                }
                else
                {
                    ((CLRType)t).SetStaticFieldValue(ptr_of_this_method->ValueLow, hit);
                }
            }
            break;

            case ObjectTypes.ArrayReference:
            {
                var instance_of_arrayReference = __mStack[ptr_of_this_method->Value] as UnityEngine.AI.NavMeshHit[];
                instance_of_arrayReference[ptr_of_this_method->ValueLow] = hit;
            }
            break;
            }

            __ret->ObjectType = ObjectTypes.Integer;
            __ret->Value      = result_of_this_method ? 1 : 0;
            return(__ret + 1);
        }