public static void CommitMsg()
    {
        if (!IsChatActive())
        {
            return;
        }

        string msgText = Instance.chatInput.text;

        if (msgText.Length == 0)
        {
            return;
        }
        MessageType type = MessageType.PlayerChat;

        if (msgText.StartsWith("/") && GameManager.IsDebugging())
        {
            // command
            type    = MessageType.Debug;
            msgText = msgText.Substring(1);
            string[] arguments = new string[0];
            if (msgText.Contains(" "))
            {
                arguments = msgText.Substring(msgText.IndexOf(' ') + 1).Split(' ');
                msgText   = msgText.Substring(0, msgText.IndexOf(' '));
            }
            switch (msgText.ToLower())
            {
            case "help":
                string resText = "";
                for (int i = 0; i < ResourceData.allResources.Count; i++)
                {
                    resText += i + "=" + ResourceData.allResources[i].name + " ,";
                }
                resText = resText.Substring(0, resText.Length - 2);
                msgText =
                    "Spielgeschwindigkeit verändern = /speed [faktor]" +
                    "\nJahre vergehen lassen = /years [jahre]" +
                    "\nRessourcen an ausgewählte Person = /give [resNr] [anzahl]\n" + resText +
                    "\nMännlichen Bewohner spawnen = /bornman [alter]" +
                    "\nWeiblichen Bewohner spawnen = /bornwoman [alter]" +
                    "\nGebäudekosten (de)aktivieren = /tcost";
                break;

            case "speed":
                try
                {
                    float fact = float.Parse(arguments[0]);
                    fact = Mathf.Clamp(fact, 0.1f, 5000);
                    GameManager.speedFactor = fact;
                    msgText = "Spielgeschwindigkeit auf " + fact + " gesetzt";
                }
                catch
                {
                    msgText = "Falsche Argumente!";
                    type    = MessageType.Error;
                }
                break;

            case "years":
                try
                {
                    int yrs = int.Parse(arguments[0]);
                    yrs = Mathf.Clamp(yrs, 1, 100);
                    GameManager.PassYears(yrs);
                    msgText = yrs + " Jahre sind vergangen";
                }
                catch
                {
                    msgText = "Falsche Argumente!";
                    type    = MessageType.Error;
                }
                break;

            case "give":
                try
                {
                    int id = int.Parse(arguments[0]);
                    int am = int.Parse(arguments[1]);
                    am = Mathf.Clamp(am, 0, 1000);
                    PersonScript ps = PersonScript.FirstSelectedPerson();
                    if (ps)
                    {
                        ps.SetInventory(new GameResources(id, am));
                        msgText = am + "x " + ps.InventoryMaterial.Name + " wurden " + ps.FirstName + "s Inventar hinzugefügt";
                    }
                    else
                    {
                        msgText = "Keine Person ausgewählt";
                        type    = MessageType.Error;
                    }
                }
                catch
                {
                    msgText = "Falsche Argumente!";
                    type    = MessageType.Error;
                }
                break;

            case "bornman":
            case "bornwoman":
                try
                {
                    Gender Gender = (msgText.ToLower() == "bornman") ? Gender.Male : Gender.Female;
                    int    age    = int.Parse(arguments[0]);
                    age = Mathf.Clamp(age, 0, 80);
                    GamePerson p = GameManager.village.PersonBirth(-1, Gender, age);
                    msgText = "Bewohner gespawnt: Name=" + p.firstName + ",Alter=" + p.age;
                }
                catch
                {
                    msgText = "Falsche Argumente!";
                    type    = MessageType.Error;
                }
                break;

            case "tcost":
                GameManager.noCost = !GameManager.noCost;
                msgText            = "Gebäudekosten " + (GameManager.noCost ? "de" : "") + "aktiviert";
                break;

            default:
                msgText = "Falscher Befehl!";
                type    = MessageType.Error;
                break;
            }
        }
        else
        {
            msgText = GameManager.Username + ": " + msgText;
        }

        Msg(msgText, type);
        Instance.chatInput.text = "";
        Instance.chatShowTime   = 0;
    }
Exemple #2
0
    // PRE: Building and game building have to be set before strat is called
    private void Start()
    {
        // set nr of building if not already given by game building
        if (gameBuilding.nr == -1)
        {
            gameBuilding.nr = allBuildingScripts.Count;
            BuildingScript par = Identify(ParentBuildingNr);
            if (par)
            {
                par.AddChildBuilding(this);
            }
        }

        // Update allBuildings collection
        allBuildingScripts.Add(this);
        tag = Building.Tag;

        // make building a clickable object
        //co = gameObject.AddComponent<ClickableObject>();
        // co.clickable = false;

        if (currentModel == null)
        {
            for (int i = 0; i < MaxStages; i++)
            {
                transform.GetChild(i).gameObject.SetActive(false);
            }
            SetCurrentModel();
        }

        audioSource = Instantiate(AudioManager.Instance.buildingAudioPrefab, transform).GetComponent <AudioSource>();

        // Add and disable Campfire script
        if (HasFire)
        {
            campfire            = gameObject.AddComponent <Campfire>();
            campfire.woodAmount = gameBuilding.campFireWoodAmount;
            campfire.enabled    = !Blueprint;
            if (Name == "Lagerfeuer")
            {
                campfire.SetIsBigCampfire(false);
            }
            else if (Name == "Grosse Feuerstelle")
            {
                campfire.SetIsBigCampfire(true);
            }

            audioSource.clip = AudioManager.Instance.campfire;
            audioSource.loop = true;
            //cf.maxIntensity = Mathf.Clamp(GridWidth, 1, 1.8f);
        }

        fieldPlant = NatureObject.Get("Korn");

        // init blueprint UI
        blueprintCanvas = transform.Find("CanvasBlueprint");
        panelMaterial   = new List <Transform>();
        textMaterial    = new List <Text>();
        for (int i = 0; i < blueprintCanvas.Find("Cost").childCount; i++)
        {
            Transform pm = blueprintCanvas.Find("Cost").GetChild(i);
            panelMaterial.Add(pm);
            textMaterial.Add(pm.Find("TextMat").GetComponent <Text>());
        }
        LayoutRebuilder.ForceRebuildLayoutImmediate(blueprintCanvas.GetComponent <RectTransform>());
        buildingMaterial  = meshRenderer.materials;
        blueprintMaterial = new Material[buildingMaterial.Length];
        for (int i = 0; i < buildingMaterial.Length; i++)
        {
            blueprintMaterial[i] = BuildManager.Instance.blueprintMaterial;
        }

        // Finish building if no costs
        if (BlueprintBuildCost.Count == 0)
        {
            FinishBuilding();
        }
        else
        {
            ChangeTerrainGround();
        }

        //blueprintCanvas.gameObject.SetActive(false);

        blueprintCanvas.gameObject.SetActive(false);

        // init range canvas
        rangeCanvas = transform.Find("CanvasRange").transform;
        rangeImage  = rangeCanvas.Find("Image").GetComponent <Image>();

        // Make selected person go build this building
        PersonScript ps = PersonScript.FirstSelectedPerson();

        if (ps)
        {
            ps.AddTargetTransform(transform, true);
        }

        // if building has a custom collision radius, disable default collider and add a capsule collider

        /*if (CollisionRadius > float.Epsilon)
         * {
         *  gameObject.AddComponent<CapsuleCollider>();
         *  myCollider.enabled = false;
         * }*/


        if (ViewRange > 0)
        {
            /* TODO: optimize code! */

            /*
             * foreach (NatureObjectScript p in Nature.nature)
             * {
             *  p.UpdateBuildingViewRange();
             * }
             * foreach (ItemScript p in ItemScript.allItemScripts)
             * {
             *  p.UpdateBuildingViewRange();
             * }
             * foreach (PersonScript p in PersonScript.wildPeople)
             * {
             *  p.UpdateBuildingViewRange();
             * }
             * foreach (AnimalScript a in AnimalScript.allAnimals)
             * {
             *  a.UpdateBuildingViewRange();
             * }*/

            gameObject.AddComponent <SimpleFogOfWar.FogOfWarInfluence>().ViewDistance = ViewRange;
        }

        // start coroutine
        StartCoroutine(GameBuildingTransform());

        //recruitingTroop = new List<Troop>();
    }
    void Update()
    {
        inputState = (UIManager.Instance.InMenu() || ChatManager.IsChatActive()) ? 0 : (BuildManager.placing) ? 1 : 2;
        bool inputEnabled = inputState > 0;

        dx = 0;
        dy = 0;

        PersonScript sp = PersonScript.FirstSelectedPerson();

        if (cameraMode == 1 && sp == null)
        {
            cameraModeChanging = true;
            cameraMode         = 0;
        }

        if (inputEnabled)
        {
            float deltaAngle = 0f;
            if (Input.GetKey(KeyCode.E))
            {
                deltaAngle = -1;
            }
            else if (Input.GetKey(KeyCode.Q))
            {
                deltaAngle = 1;
            }
            lerpLookAtRotation += deltaAngle * rotateSpeed * Time.deltaTime;

            float scrollAmount = Input.GetAxis("Mouse ScrollWheel") * scrollSensitivity;
            if (invertedMousehweel)
            {
                scrollAmount *= -1f;
            }
            if (EventSystem.current.IsPointerOverGameObject())
            {
                scrollAmount = 0f;
            }

            if (Input.GetKey(KeyCode.A))
            {
                dx = -1;
            }
            else if (Input.GetKey(KeyCode.D))
            {
                dx = 1;
            }
            else if (Input.GetKeyUp(KeyCode.A) || Input.GetKeyUp(KeyCode.D))
            {
                dx = 0;
            }

            if (Input.GetKey(KeyCode.W))
            {
                dy = 1;
            }
            else if (Input.GetKey(KeyCode.S))
            {
                dy = -1;
            }
            else if (Input.GetKeyUp(KeyCode.W) || Input.GetKeyUp(KeyCode.S))
            {
                dy = 0;
            }

            // additional camera movement when in bird view
            if (cameraMode == 0)
            {
                cameraDistance += scrollAmount * -1f;
                cameraDistance  = Mathf.Clamp(cameraDistance, 0.5f, 12f);

                Vector3 mousePos            = Input.mousePosition;
                float   mouseMoveScreenPerc = 0.01f;
                float   border = Mathf.Max(8f, Screen.width * mouseMoveScreenPerc);
                bool    ed     = Application.isEditor;
                if (mousePos.x < border && (!ed || mousePos.x >= 0))
                {
                    dx = -1;
                }
                if (mousePos.x > Screen.width - border && (!ed || mousePos.x <= Screen.width))
                {
                    dx = 1;
                }
                if (mousePos.y < border && (!ed || mousePos.y >= 0))
                {
                    dy = -1;
                }
                if (mousePos.y > Screen.height - border && (!ed || mousePos.y <= Screen.height))
                {
                    dy = 1;
                }
            }
            else if (cameraMode == 1)
            {
                sp.Move(dx, dy);
                sp.Rotate(deltaAngle);
            }
        }

        // scale movement relatively to camera distance
        Vector3 delta = new Vector3(dx, 0, dy) * keyMoveSpeed * Mathf.Pow(cameraDistance, 0.3f) * Time.deltaTime;

        if (inputEnabled)
        {
            // center camera around average position of selected people to make them all visible
            if (PersonScript.selectedPeople.Count > 0)
            {
                if (Input.GetKey(KeyCode.Space))
                {
                    ZoomSelectedPeople();
                }

                /*else if(Input.GetKeyDown(KeyCode.Z))
                 * {
                 *  cameraMode = 1 - cameraMode;
                 *  cameraModeChanging = true;
                 *  ZoomSelectedPeople();
                 * }*/
            }
            else if (Input.GetKey(KeyCode.Space))
            {
                Instance.lerplookAtPosition = Grid.SpawnpointNode.Position;
                cameraDistance = 3f;
                cameraDistance = Mathf.Clamp(cameraDistance, 2f, 12f);
            }

            if (Input.GetMouseButtonDown(2))
            {
                bDragging = true;
                panOrigin = Camera.main.ScreenToViewportPoint(Input.mousePosition); //Get the ScreenVector the mouse clicked
            }

            if (Input.GetMouseButton(2))
            {
                // Get the difference between where the mouse clicked and where it moved
                Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition) - panOrigin;
                // Move the position of the camera to simulate a drag, speed * 10 for screen to worldspace conversion
                Vector3 newPos = -pos * panSpeed;
                //delta -=  new Vector3(newPos.x, 0, newPos.y);
                lerpLookAtRotation += Mathf.Clamp(newPos.x * 20f, -15f, 15f);
            }

            if (Input.GetMouseButtonUp(2))
            {
                bDragging = false;
            }
        }
        else
        {
            bDragging = false;
        }

        // birds eye view
        if (cameraMode == 0)
        {
            delta = Quaternion.AngleAxis(transform.rotation.eulerAngles.y, Vector3.up) * delta;

            lookAtRotation      = Mathf.Lerp(lookAtRotation, lerpLookAtRotation, Time.deltaTime * 5f);
            lerplookAtPosition += delta;
            lerplookAtPosition  = Vector3.ClampMagnitude(lerplookAtPosition, Grid.WIDTH / 2);
            lookAt.position     = Vector3.Lerp(lookAt.position, lerplookAtPosition, Time.deltaTime * 10f);

            Vector3 lookAtOffset = new Vector3(2 + 0.5f * cameraDistance, 0.5f + 1f * cameraDistance, 0);
            currentLookAtOffset = Vector3.Lerp(currentLookAtOffset, lookAtOffset, Time.deltaTime * 5f);
            Vector3 targetPos = lookAt.position + Quaternion.AngleAxis(lookAtRotation, Vector3.up) * currentLookAtOffset;
            if (cameraModeChanging)
            {
                transform.position = Vector3.Lerp(transform.position, targetPos, Time.deltaTime * 5f);
                Quaternion targetRot = Quaternion.LookRotation(lookAt.position - transform.position);
                transform.rotation     = Quaternion.Lerp(oldRot, targetRot, camerModeChangingTime);
                camerModeChangingTime += Time.deltaTime * 3f;
                if (Vector3.Distance(transform.position, targetPos) <= 0.01f && Quaternion.Angle(transform.rotation, targetRot) <= 2f)
                {
                    cameraModeChanging = false;
                }
            }
            else
            {
                transform.position = targetPos;
                transform.LookAt(lookAt);
            }
        }
        else if (cameraMode == 1)
        {
            if (cameraModeChanging)
            {
                Transform target = sp.GetShoulderCamPos();
                transform.position = Vector3.Lerp(transform.position, target.position, Time.deltaTime * 5f);
                Quaternion targetRot = Quaternion.Euler(target.rotation.eulerAngles.x + 5, target.rotation.eulerAngles.y, target.rotation.eulerAngles.z);
                transform.rotation = Quaternion.Lerp(transform.rotation, targetRot, Time.deltaTime * 5f);

                oldRot = transform.rotation;
                camerModeChangingTime = 0;

                // zoom out on this person
                lookAt.position    = target.position;
                lerpLookAtRotation = target.rotation.eulerAngles.y + 90;
                lookAtRotation     = lerpLookAtRotation;
            }
        }
    }