Example #1
0
    /*public static void SpawnLake(Transform lakePos)
     * {
     *  NatureObject no = NatureObject.Get("Teich");
     *  Vector3 gpos = Grid.ToGrid(lakePos.position);
     *  GameNatureObject go = new GameNatureObject(no, (int)gpos.x, (int)gpos.z);
     *  go.SetPosition(lakePos.transform.position);
     *  go.SetRotation(Quaternion.identity);
     *  SpawnNatureObject(go);
     * }*/

    public static void SpawnForest(int treeAm, Vector3 pos)
    {
        NatureObject     no = NatureObject.Get("Birke");
        GameNatureObject go;

        Vector3 node = Grid.ToGrid(pos);
        int     gx   = (int)node.x;
        int     gy   = (int)node.z;

        for (int i = 0; i < treeAm; i++)
        {
            int x     = 0;
            int z     = 0;
            int count = 0;
            do
            {
                int spread = treeAm / 10 + count + 1;
                x = gx + Random.Range(-spread, spread);
                z = gy + Random.Range(-spread, spread);
            } while (!Grid.ValidNode(x, z) || (Grid.GetNode(x, z).IsOccupied() || (Mathf.Abs(Grid.SpawnY - x) < 5 && Mathf.Abs(Grid.SpawnY - z) < 5)) && (++count) < 100);

            if (count == 100)
            {
                continue;
            }

            go = new GameNatureObject(no, x, z);
            go.SetPosition(Grid.ToWorld(x + no.gridWidth / 2, z + no.gridHeight / 2));
            go.SetRotation(Quaternion.Euler(0, Random.Range(0, 360), 0));
            SpawnNatureObject(go);
        }
    }
Example #2
0
    private void SetupRandomNature()
    {
        // If in editor, spawn less nature to speed up testing
        if (Application.isEditor)
        {
            Spawn(30, 5, 5, 5, 30, 5, 5, 5, 5);                      // Spawn(50, 80, 15, 5, 30, 40, 20); //Spawn(200,50,15,10,70,15);//Spawn(50,20,5,5,30,5);
        }
        // Spawn some random plants
        else
        {
            Spawn(850, 320, 45, 25, 340, 40, 20, 200, 200);

            for (int i = 0; i < forestParent.childCount; i++)
            {
                Forest f = forestParent.GetChild(i).GetComponent <Forest>();
                SpawnForest(f.trees, f.transform.position);
            }
        }

        /*for (int i = 0; i < lakeParent.childCount; i++)
         * {
         *  Transform lk = lakeParent.GetChild(i);
         *  SpawnLake(lk);
         * }*/

        int count = 0;
        int x, z;

        do
        {
            x = Random.Range(-5, 5) + Grid.SpawnX;
            z = Random.Range(-5, 5) + Grid.SpawnY;
        } while ((Grid.GetNode(x, z).IsOccupied() || Grid.GetNode(x, z).IsWater() || (Mathf.Abs(Grid.SpawnX) < 5 && Mathf.Abs(Grid.SpawnY) < 5)) && (++count) < 100);
        if (count < 100)
        {
            NatureObject     no = NatureObject.Get("Pilzstrunk");
            GameNatureObject go = new GameNatureObject(no, x, z);
            go.SetPosition(Grid.ToWorld(x + no.gridWidth / 2, z + no.gridHeight / 2));
            go.SetRotation(Quaternion.Euler(0, Random.Range(0, 360), 0));
            SpawnNatureObject(go);
        }
    }
Example #3
0
    /*private void SpawnNatureObject(NatureO int randSize)
     * {
     *  Terrain terrain = Terrain.activeTerrain;
     *
     *  bool invalid = false;
     *  int species = Random.Range(0, plants[(int)type].Count);
     *
     *  int x = 0;
     *  int z = 0;
     *  int count = 0;
     *  if(type == NatureObjectType.Reed)
     *  {
     *      List<Node> sn = new List<Node>(shore);
     *      if(sn.Count == 0) return;
     *      Node rsn = sn[Random.Range(0, sn.Count)];
     *      while(rsn.nodeObject != null && (++count) < 100)
     *          rsn = sn[Random.Range(0, sn.Count)];
     *      if(count == 100) return;
     *      x = rsn.gridX;
     *      z = rsn.gridY;
     *  }
     *  else
     *  {
     *      x = UnityEngine.Random.Range(0, Grid.WIDTH);
     *      z = UnityEngine.Random.Range(0, Grid.HEIGHT);
     *      while((Grid.GetNode(x,z).IsOccupied() || (Mathf.Abs(Grid.WIDTH / 2 - x) < 5 && Mathf.Abs(Grid.HEIGHT / 2 - z) < 5)) && (++count) < 100)
     *      {
     *          x = UnityEngine.Random.Range(0, Grid.WIDTH);
     *          z = UnityEngine.Random.Range(0, Grid.HEIGHT);
     *      }
     *      if(count == 100) return;
     *      for (int dx = 0; dx < 3; dx++)
     *      {
     *          if (invalid) continue;
     *          for (int dy = 0; dy < 3; dy++)
     *          {
     *              if (invalid) continue;
     *              if (!Grid.ValidNode(x + dx, z + dy)) invalid = true;
     *              else if (Grid.GetNode(x + dx, z + dy).IsOccupied()) invalid = true;
     *          }
     *      }
     *  }
     *  if (invalid) return;
     *
     *  GameNatureObject toSpawn = new GameNatureObject(NatureObject.Get(), Grid.WIDTH / 2 - 1, Grid.HEIGHT / 2 - 1, 3);
     *  toSpawn.SetPosition(Vector3.zero);
     *  toSpawn.SetRotation(Quaternion.Euler(0, -90, 0));
     *
     *  GameObject obj = (GameObject)Instantiate(plants[(int)type][species], Vector3.zero
     *          ,Quaternion.Euler(0, Random.Range(0, 360), 0),
     *      plantsParent);
     *  NatureObjectScript nos = obj.AddComponent<NatureObjectScript>();
     *  nos.tag = "NatureObject";
     *  nos.SetNatureObject(no)
     *
     *  nos.SetPos(gridX, gridY);
     *
     *  if(randSize > 0) nos.SetRandSize(randSize);
     *
     *  // Set correct position of NatureObjectScript in terrain
     *  Vector3 worldPos = Grid.ToWorld(x + nos.gridWidth / 2, z + nos.gridHeight / 2);
     *  float smph = terrain.SampleHeight(worldPos);
     *  worldPos.y = terrain.transform.position.y + smph;
     *
     *  NatureObjectScript.transform.position = worldPos;
     *
     *  nature.Add(NatureObjectScript);
     *
     *  for (int dx = 0; dx < NatureObjectScript.gridWidth; dx++)
     *  {
     *      for (int dy = 0; dy < NatureObjectScript.gridHeight; dy++)
     *      {
     *          Grid.GetNode(x+dx,z+dy).SetNodeObject(obj.transform);
     *          if(type == NatureObjectType.Rock) Grid.GetNode(x+dx, z+dy).objectWalkable = false;
     *      }
     *  }
     * }*/

    // Spawn the given amount of trees,mushrooms,reeds and rocks
    private void Spawn(int countTrees, int countMushrooms, int countMushroomStumps, int countReed, int countCorn, int countRocks, int energySpots, int countPearTree, int countFirTree)
    {
        int[] counts = new int[] { countTrees, countRocks, countCorn, countMushrooms, countMushroomStumps, countReed, energySpots, countPearTree, countFirTree };
        for (int i = 0; i < counts.Length; i++)
        {
            NatureObject baseNo = NatureObject.Get(i);
            if (!baseNo)
            {
                continue;
            }
            if (baseNo.type == NatureObjectType.Water)
            {
                continue;
            }
            for (int j = 0; j < counts[i]; j++)
            {
                SpawnRandomPosNatureObject(baseNo, 3);
            }
        }
    }
Example #4
0
    void Update()
    {
        /*for(int i = 0; i < typeCount.Length; i++)
         *  typeCount[i] = 0;
         * foreach(NatureObjectScript p in nature)
         * {
         *  if ((int)p.Type < typeCount.Length)
         *  {
         *      typeCount[(int)p.Type]++;
         *  }
         * }*/
        // NatureObjectScript SpawningFactor
        //int month = GameManager.GetMonth();
        /* TODO: spawn in right month */
        for (int i = 0; i < natureObjectSpawningTime.Length; i++)
        {
            NatureObject no = NatureObject.Get(i);
            if (no.growth < float.Epsilon)
            {
                continue;
            }
            if (no.type == NatureObjectType.Water)
            {
                continue;
            }

            natureObjectSpawningTime[i] += Time.deltaTime;
            float gt = 60f / (floraSpawningFactor * no.spawningFactor);
            if (natureObjectSpawningTime[i] >= gt)
            {
                natureObjectSpawningTime[i] -= gt;
                // Limit splant spawning
                if (typeCount[i] < no.spawningLimit)
                {
                    SpawnRandomPosNatureObject(no, 0);
                }
            }
        }
    }
Example #5
0
 public GameNatureObject(int id) : this(NatureObject.Get(id))
 {
 }
Example #6
0
 public GameNatureObject(string name) : this(NatureObject.Get(name))
 {
 }
Example #7
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>();
    }