Exemplo n.º 1
0
    void OnDrawGizmos()
    {
        Gizmos.DrawSphere(placePos, 0.5f);
        if (pModule)
        {
            if (pModule.moduleType == Module.Type.Structural)
            {
                StructuralModule str = pModule.GetComponent <StructuralModule>();
                for (int i = 0; i < str.colCheckPoints.Length; i++)
                {
                    Vector3 pos = placePos + placeRot * str.colCheckPoints[i];
                    if (hitModule)
                    {
                        pos += Vector3.forward * (hitModule.transform.position.z - 1);
                    }
                    Gizmos.DrawSphere(pos, pModule.moduleClass / 4f);
                }
            }
        }

        Gizmos.DrawWireCube(transform.position, new Vector3(Camera.main.orthographicSize * Camera.main.aspect * 2 - cameraMargin * 2, Camera.main.orthographicSize * 2 - cameraMargin * 2));

        if (isEditingWalls && wallDragStatus != WallDragStatus.Inactive)
        {
            Gizmos.DrawSphere(pos, 0.5f);
            Gizmos.DrawSphere(wallDragStart, 0.5f);
            Gizmos.DrawCube(wallDragStart + (pos - wallDragStart) / 2f, new Vector3(Mathf.Abs(wallDragStart.x - pos.x), Mathf.Abs(wallDragStart.y - pos.y)) + Vector3.one);
        }
    }
Exemplo n.º 2
0
    // Returns a formatted string of reasons why it coulnd't be placed, or empty string if no errors were found.
    string CanPlaceAtPos(Vector3 pos)
    {
        List <string> errors = new List <string> ();

        int hits = 4;

        if (currentCost > Game.credits && !purchaseMenu.stockModules.ContainsKey(pModule.gameObject))
        {
            errors.Add("Not enough credits");
        }

        if (pModule.moduleType == Module.Type.Weapon)
        {
            if (hitModule)
            {
                if (!hitModule.parentBase)
                {
                    errors.Add("Placement must have a parent base");
                }
            }
            else
            {
                errors.Add("Weapons cannot be placed independently");
            }
        }

        for (int i = 0; i < canPlaceTestPos.Length; i++)
        {
            bool isInside = Game.IsInsideBattlefield(pos + canPlaceTestPos[i] * pModule.moduleClass);

            if (Game.currentScene == Scene.Play)
            {
                if (!isInside && !errors.Contains("Placement must be within the battlefield"))
                {
                    errors.Add("Placement must be within the battlefield");
                }

                if (isInside && Game.isWalled[(int)Game.WorldToWallPos(pos + canPlaceTestPos[i] * pModule.moduleClass).x, (int)Game.WorldToWallPos(pos + canPlaceTestPos[i] * pModule.moduleClass).y] != Game.WallType.Player && !errors.Contains("Must be placed on player made walls"))
                {
                    errors.Add("Must be placed on player made walls");
                }
            }

            Ray        ray = new Ray(new Vector3(pos.x + canPlaceTestPos[i].x * pModule.moduleClass, pos.y + canPlaceTestPos[i].y * pModule.moduleClass, camDepth), Vector3.forward * -camDepth * 2f);
            RaycastHit hit;

            Debug.DrawRay(ray.origin, ray.direction, Color.blue);

            if (Physics.Raycast(ray, -camDepth, blockBuildLayer) && !errors.Contains("Placement blocked by something"))
            {
                errors.Add("Placement blocked by something");
            }

            Module locModule = null;
            if (Physics.Raycast(ray, out hit, -camDepth * 2f, turretLayer))
            {
                // Make sure you cannot place modules on weapons
                if (locModule == null)
                {
                    locModule = hit.transform.GetComponent <Module> ();
                }
                else if (locModule != hit.transform.GetComponent <Module> () && !errors.Contains("Cannot be placed on multiple modules"))
                {
                    errors.Add("Cannot be placed on multiple modules");
                }

                if (hitModule)
                {
                    if (hitModule.moduleType == Module.Type.Weapon ||
                        hitModule.moduleType == Module.Type.Independent ||
                        hitModule.moduleClass < pModule.moduleClass ||
                        hitModule.moduleLayer >= 45 ||
                        Game.currentScene == Scene.Play)
                    {
                        hits--;
                    }
                }
            }
            else
            {
                if (hitModule)
                {
                    hits--;
                }
            }
        }

        if (hitModule && (hitModule.moduleType == pModule.moduleType && hitModule.moduleType != Module.Type.Structural) && !errors.Contains("Cannot be placed on the same type."))
        {
            errors.Add("Cannot be placed on the same type.");
        }

        if (hits < 4 && !errors.Contains("Not enough structural support"))
        {
            errors.Add("Not enough structural support");
        }

        // Handle structural module multiple collision checks
        if (pModule.moduleType == Module.Type.Structural)
        {
            StructuralModule str = pModule.GetComponent <StructuralModule>();

            for (int i = 0; i < str.colCheckPoints.Length; i++)
            {
                Vector3 p = placePos + placeRot * str.colCheckPoints[i];
                if (hitModule)
                {
                    p += Vector3.forward * (hitModule.transform.position.z - 1);
                }
                if (Physics.CheckSphere(p, pModule.moduleClass / 4f) && !errors.Contains("Cannot be placed within other modules"))
                {
                    errors.Add("Cannot be placed within other modules");
                }
            }
        }

        string full = "";

        if (errors.Count > 0)
        {
            full += "Cannot be placed here:\n";
        }

        for (int i = 0; i < errors.Count - 1; i++)
        {
            full += " - " + errors[i] + "\n";
        }

        if (errors.Count > 0)
        {
            full += " - " + errors[errors.Count - 1];
        }

        return(full);
    }