Ejemplo n.º 1
0
    void FixedUpdate()
    {
        _bodyRect = new Rect((Vector2)transform.position, transform.localScale);

        _velocity = Vector2.ClampMagnitude(_velocity + GRAVITY * Vector2.up * Time.fixedDeltaTime, maxSpeed);



        var move = _velocity * Time.fixedDeltaTime;

        if (move != Vector2.zero)
        {
            var     expectedPos = _bodyRect.position + move;
            Vector2 newPos; List <Vector2> blockedDirections;
            Phys.MoveRect(_bodyRect, move, out newPos, out blockedDirections);
            _blockedDirections = blockedDirections;
            transform.position = newPos;

            if (expectedPos.x != newPos.x)
            {
                _velocity.x = 0;
            }
            if (expectedPos.y != newPos.y)
            {
                _velocity.y = 0;
            }
        }

        _velocity *= 1f / (1f + drag * Time.fixedDeltaTime);
    }
Ejemplo n.º 2
0
            public static bool GetClosestIndex(Ray ray, PlanetData localPlanet, out int closestVeinGroupIndex, out int closestVeinIndex, out float closestVeinDistance, out float closestVeinDistance2D)
            {
                float realRadius = localPlanet.realRadius;

                closestVeinGroupIndex = -1;
                closestVeinIndex      = -1;
                closestVeinDistance   = -1;
                closestVeinDistance2D = -1;
                float   closestVeinGroupDistance = 100f;
                Vector3 vector = Vector3.zero;

                if (Phys.RayCastSphere(ray.origin, ray.direction, 600f, Vector3.zero, realRadius + 1f, out var rch))
                {
                    Dictionary <int, float> distMap = new Dictionary <int, float>();

                    // First pass check for vein Group. Uses veinGroups (cheaper)
                    for (int i = 0; i < localPlanet.veinGroups.Length; i++)
                    {
                        PlanetData.VeinGroup veinGroup = localPlanet.veinGroups[i];
                        if (veinGroup.type == EVeinType.None)
                        {
                            continue;
                        }

                        float currentveinGroupDistance = Vector3.Distance(rch.point, veinGroup.pos * realRadius);
                        //Debug.Log("Comp: veinGroup: " + veinGroup.ToString() + " index: " + i + " Pos: " + veinGroup.pos.ToString() + " dist: " + currentveinGroupDistance);
                        distMap[i] = currentveinGroupDistance;
                        if (currentveinGroupDistance < closestVeinGroupDistance)
                        {
                            closestVeinGroupDistance = currentveinGroupDistance;
                            closestVeinGroupIndex    = i;
                            vector = veinGroup.pos * (realRadius + 2.5f);
                        }
                    }

                    // Second Pass. Looks up distance to specific vein nodes.
                    var limitedCandidates = distMap.OrderBy(key => key.Value).Take(Math.Min(distMap.Count(), 5));

                    //var veinCandidates = from vg in limitedCandidates select from vp in localPlanet.factory.veinPool where vp;
                    closestVeinDistance   = closestVeinGroupDistance;
                    closestVeinDistance2D = closestVeinGroupDistance;
                    foreach (var candkv in limitedCandidates)
                    {
                        //Debug.Log("Cand: VeinGroup Idx=" + candkv.Key + "\t Dist: " + candkv.Value);

                        for (int i = 1; i < localPlanet.factory.veinCursor; i++)
                        {
                            var vein = localPlanet.factory.veinPool[i];
                            if (vein.id != i || vein.groupIndex != candkv.Key)
                            {
                                continue;
                            }

                            float veinDistance = Vector3.Distance(rch.point, vein.pos);
                            if (veinDistance < closestVeinDistance)
                            {
                                closestVeinDistance   = veinDistance;
                                closestVeinGroupIndex = candkv.Key;
                                closestVeinIndex      = vein.id;
                            }

                            float veinDistance2D = Vector2.Distance(rch.point, vein.pos);
                            if (veinDistance2D < closestVeinDistance2D)
                            {
                                closestVeinDistance2D = veinDistance2D;
                                closestVeinGroupIndex = candkv.Key;
                                closestVeinIndex      = vein.id;
                            }
                        }
                    }
                    //Debug.Log("Closest: VgIdx=" + closestVeinGroupIndex + " Vein Idx=" + closestVeinIndex + "\t Dist: " + closestVeinDistance + ", Dist2D: " + closestVeinDistance2D);



                    // Cheat for now
                    //Assert.Equals(closestVeinGroupIndex, limitedCandidates.First().Key);
                    //closestVeinGroupIndex = candidates.First().Key;
                }

                // Check if there are any vein colliders inside a radius 5f sphere.
                if (closestVeinGroupIndex >= 0)
                {
                    /*
                     * if (!Phys.RayCastSphere(ray.origin, ray.direction, 600f, vector, 5f, out rch))
                     * {
                     *  Debug.Log("Resetting to -1! ");
                     *  closestVeinGroupIndex = -1;
                     *  closestVeinIndex = -1;
                     *  return false;
                     * }
                     */
                    if (closestVeinDistance2D < 5)
                    {
                        return(true);
                    }
                    else
                    {
                        //Debug.Log("Resetting to -1! Distance is above: 5");
                        closestVeinGroupIndex = -1;
                        closestVeinIndex      = -1;
                        closestVeinDistance2D = -1;
                        return(false);
                    }
                }
                return(false);
            }
Ejemplo n.º 3
0
 void Awake()
 {
     Phys.SetMap(this);
 }