Exemplo n.º 1
0
    void DoBuildLogic()
    {
        // Check if we got a building an show its information
        if (doShowInfo)
        {
            if (Physics.Raycast(_cam.ScreenPointToRay(Input.mousePosition), out hit, 100, InfoLayer))
            {
                var info = hit.collider.gameObject.GetComponent <BuildInformation> ();

                if (info != null)
                {
                    if (CurrentInfo == info)
                    {
                        HideBuildInfo();
                    }
                    else
                    {
                        ShowInfo(info);
                    }
                }
            }
            else
            {
                HideBuildInfo();
            }
        }

        // Let the build material flicker
        if (CurrentBuilding != null)
        {
            var canBuild = Money >= CurrentBuilding.Money;

            BuildMaterial.SetColor(buildMatTintColorId, new Color(
                                       canBuild ? 0 : 1,
                                       canBuild ? 1 : 0,
                                       0,
                                       (Mathf.Sin(Time.realtimeSinceStartup * 8f) / 8) + .25f));


            // General building raycast
            if (Physics.Raycast(_cam.ScreenPointToRay(Input.mousePosition), out hit, 100, BuildLayer))
            {
                // Show new highlight
                if (lastHighlight != hit.collider.gameObject)
                {
                    if (lastHighlight != null)
                    {
                        lastHighlight.Hide();
                        lastHighlight = null;
                    }

                    lastHighlight = hit.collider.gameObject.GetComponent <BuildBase> ();
                    lastHighlight.Show();
                }

                if (doBuild && canBuild)
                {
                    lastHighlight.Hide();

                    var go = Instantiate(CurrentBuilding.Prefab, lastHighlight.transform.parent);
                    go.GetComponent <BuildInformation> ().Building = CurrentBuilding;

                    lastHighlight.gameObject.SetActive(false);

                    doBuild = false;
                    Money  -= CurrentBuilding.Money;

                    buildingsBuilt++;

                    // TODO ca-tching
                }
            }
            else if (lastHighlight != null)
            {
                lastHighlight.Hide();
                lastHighlight = null;
            }
        }


        // Platform building raycast
        if (Physics.Raycast(_cam.ScreenPointToRay(Input.mousePosition), out hit, 100, PlatformBuildLayer))
        {
            // See if we hit a floor instead (layers are bitmasks, so no easier check here)
            if (hit.collider.gameObject.HasLayer(FloorLayer))
            {
                if (lastPlatformHighlight != null)
                {
                    if (hit.collider.gameObject != lastPlatformHighlight)
                    {
                        lastPlatformHighlight.SetActive(false);
                    }
                    else if (doBuild && Money >= PlatformCost)
                    {
                        // We got a platform we want to build
                        Money -= PlatformCost;

                        // Reset material
                        var renderer = lastPlatformHighlight.GetComponent <MeshRenderer> ();
                        renderer.sharedMaterial = defaultPlatformMaterial;

                        // Reactivate build base so we can build stuff again
                        platformBuildBase.SetActive(true);

                        // reset for next platform
                        lastPlatformHighlight = null;
                        platformRenderer      = null;

                        platformsBuilt++;
                    }
                }
            }
            else
            {
                var snappedToGrid = new Vector3(
                    Mathf.Round((hit.point.x) / .5f) * .5f,
                    0,
                    Mathf.Round((hit.point.z) / .5f) * .5f);

                if (lastPlatformHighlight != null)
                {
                    // Only allow platforms near others, so we do a simple four-directional raycast
                    lastPlatformHighlight.transform.position = snappedToGrid;
                    lastPlatformHighlight.SetActive(
                        Physics.Raycast(snappedToGrid, Vector3.forward, out hit, .5f, FloorLayer) ||
                        Physics.Raycast(snappedToGrid, Vector3.right, out hit, .5f, FloorLayer) ||
                        Physics.Raycast(snappedToGrid, Vector3.back, out hit, .5f, FloorLayer) ||
                        Physics.Raycast(snappedToGrid, Vector3.left, out hit, .5f, FloorLayer));
                }
                else
                {
                    lastPlatformHighlight = Instantiate(PlatformPrefab, snappedToGrid, Quaternion.identity, PlayerBase);

                    platformRenderer        = lastPlatformHighlight.GetComponent <MeshRenderer> ();
                    defaultPlatformMaterial = defaultPlatformMaterial ?? platformRenderer.sharedMaterial;

                    platformBuildBase = FindChildWithBuildLayer(lastPlatformHighlight);
                    platformBuildBase.SetActive(false);
                }
            }
        }

        doBuild    = false;
        doShowInfo = false;
    }