Ejemplo n.º 1
0
    public void PlaceGearsFromCoordinates(string _text)
    {
        GearPlacer gearScript = transform.GetComponent <GearPlacer>();

        gearScript.Reset();

        if (_text.StartsWith("#[GearPlacementInfo]\n"))
        {
            float xCoord;
            float zCoord;
            int   gearType;

            _text = _text.Remove(0, 20);

            string[] lines = _text.Split(']');

            var halfscale = gearScript.gameObject.transform.localScale / 2;
            for (int i = 0; i < lines.Length - 1; i++)
            {
                int.TryParse(lines[i][lines[i].Length - 1].ToString(), out gearType);
                lines[i] = lines[i].Remove(lines[i].Length - 10, 10);
                lines[i] = lines[i].Remove(0, 2);


                string[] Coords = lines[i].Split(',', '(', ')');

                float.TryParse(Coords[0], out xCoord);


                float.TryParse(Coords[1], out zCoord);

                gearPlacerScript.SpawnGear(xCoord - halfscale.x, 0, zCoord - halfscale.z, gearType);
                gearPlacerScript.showGearToPlace = false;
            }
            gearPlacerScript.activeGears.Reverse();
        }
        UpdateText();
        RotationBehaviour.instance.Rotate();
    }
Ejemplo n.º 2
0
    public void GenerateRandomGearSet()
    {
        gearScript.selectedGear.CloneTo(selectedGearBeforeGenerate);
        List <Gear> spawnedGears = new List <Gear>();

        if (RemoveOld.isOn)
        {
            gearScript.Reset();
        }
        var xScale = Mathf.RoundToInt(gearScript.transform.localScale.x * 100000);
        var zScale = Mathf.RoundToInt(gearScript.transform.localScale.z * 100000);

        if (gearScript.activeGears.Count == 0)
        {
            int selectedGearIndex = 10;
            int count             = 0;
            while (true)
            {
                var index = rand.Next(0, gearTypes.Count);
                if (allowedGears[index].allowed)
                {
                    selectedGearIndex = index;
                    break;
                }
                count++;
                if (count > 100)
                {
                    StopAllCoroutines();
                    return;
                }
            }


            spawnedGears.Add(gearScript.SpawnGear(rand.Next(-xScale, xScale) / (100000f * 20f), -10, rand.Next(-zScale, zScale) / (100000f * 20f), selectedGearIndex));
            gearScript.showGearToPlace = false;
        }



        for (int i = 0; i < gearCount - 1; i++)
        {
            int selectedGearIndex = 10;
            int count             = 0;
            while (true)
            {
                var index = rand.Next(0, gearTypes.Count);
                if (allowedGears[index].allowed)
                {
                    selectedGearIndex = index;
                    break;
                }
                count++;
                if (count > 100)
                {
                    StopAllCoroutines();
                    return;
                }
            }
            if (selectedGearIndex == 10)
            {
                return;
            }
            var selectedGear = gearScript.gearTypes[selectedGearIndex];

            for (int recalcs = 0; recalcs < 15; recalcs++)
            {
                var mousePosition = new Vector3(rand.Next(-xScale, xScale) / 100000f, gearScript.placeGearHeight, rand.Next(-zScale, zScale) / 100000f);

                var gearToSnap    = gearScript.activeGears[rand.Next(0, gearScript.activeGears.Count)];
                var gearToSnapPos = new Vector3(gearToSnap.position.x, mousePosition.y, gearToSnap.position.z);


                var VectorToCloseGear = (gearToSnapPos - mousePosition).normalized * (gearToSnap.radius + selectedGear.radius);
                var gearPlacePoint    = gearToSnapPos - VectorToCloseGear;

                bool needRecalc = false;
                foreach (var gear in gearScript.activeGears)
                {
                    var gearPos = new Vector3(gear.position.x, gearPlacePoint.y, gear.position.z);
                    if (Vector3.Magnitude(gearPos - gearPlacePoint) < gear.radius + selectedGear.radius - 0.0001f)
                    {
                        needRecalc = true;
                    }
                    if (Mathf.Abs(gearPlacePoint.x) >= Mathf.Abs(gearScript.transform.lossyScale.x) / 2 || Mathf.Abs(gearPlacePoint.z) >= Mathf.Abs(gearScript.transform.lossyScale.z) / 2)
                    {
                        needRecalc = true;
                    }
                }
                if (!needRecalc)
                {
                    spawnedGears.Add(gearScript.SpawnGear(gearPlacePoint.x, -10f, gearPlacePoint.z, selectedGearIndex));
                    gearScript.showGearToPlace = false;
                    break;
                }
            }
        }
        RotationBehaviour.instance.Rotate();
        StartCoroutine(AnimateGears(spawnedGears));
    }