示例#1
0
        // Decides which tree the index of an array will store
        // Ratio is 4:1 so getting random number out of 50
        private Tree DecideTree(Random rng)
        {
            Tree outputTree;
            int  chosenNumber = rng.Next(50);

            if (chosenNumber <= 10)
            {
                outputTree = new Spruce();
                return(outputTree);
            }
            else
            {
                outputTree = new Fir();
                return(outputTree);
            }
        }
示例#2
0
    /// <summary>
    /// Creates vertices, uvs and triangles for a branch and adds it to the tree object, which handles the intantiating of the mesh.
    /// </summary>
    /// <param name="hasBot">Determines if the branch needs a bottom face.</param>
    /// <param name="hasTop">Determines if the branch needs a top face.</param>
    /// <param name="sides">Determines how many sides should the resulting polygon have.</param>
    /// <param name="tree">The tree to which this branch belongs.</param>
    private void CreateBranch(bool hasBot, bool hasTop, int sides, Spruce tree)
    {
        var vertices = MakeVertices(currentState, sides).Concat(MakeVertices(nextState, sides)).ToArray();

        // If the tree object has reached the maximum number of vertices for leaves, a mesh of leaves is instantiated.
        if (tree.CurrentVertices + vertices.Length >= Spruce.MaxVertices)
        {
            tree.Instantiate();
        }
        tree.Vertices.AddRange(vertices);
        tree.Uvs.AddRange(MakeUVs(sides));
        var tris = MakeTris(hasBot, hasTop, sides);

        for (int index = 0; index < tris.Count; index++)
        {
            tris[index] += tree.CurrentVertices;
        }
        tree.Tris.AddRange(tris);
        tree.CurrentVertices += vertices.Length;
    }
示例#3
0
    // Update is called once per frame
    void Update()
    {
        if (Controller.current.health <= 0)
        {
            return;
        }

        CheckForEnemies();

        if (lastPosition != (Vector2)transform.position)
        {
            CheckForTiles();
            CheckForTrees();
            lastPosition = transform.position;
        }

        if (_harvestTimer > 0)
        {
            _harvestTimer -= Time.deltaTime;
        }
        else
        {
            _harvestTimer = (1f / speed) + _harvestTimer;

            if (enemies.Count > 0)
            {
                Enemy current = enemies[Random.Range(0, enemies.Count)];

                current.Hit(power);

                if (current.health <= 0 || current.gameObject == null)
                {
                    if (Time.time - lastImpactSound > 0.1f)
                    {
                        _audioSource.PlayOneShot(deathSounds[Random.Range(0, deathSounds.Length)], 0.5f);
                        lastImpactSound = Time.time;
                    }
                }
                else
                {
                    if (Time.time - lastImpactSound > 0.1f)
                    {
                        _audioSource.PlayOneShot(hitSounds[Random.Range(0, hitSounds.Length)], 0.3f);
                        lastImpactSound = Time.time;
                    }
                }
                // Controller.current.crystals += 1;
            }
            else if (spruces.Count > 0)
            {
                Spruce current = spruces[Random.Range(0, spruces.Count)];

                // Failsafe
                if (!current)
                {
                    spruces.Remove(current);
                    return;
                }

                int wood = (int)current.Hit(power);
                Controller.current.wood += wood;

                if (current.health <= 0 || current.gameObject == null)
                {
                    spruces.Remove(current);

                    if (Time.time - lastPlayedChop > 0.05f)
                    {
                        AudioSource.PlayClipAtPoint(fallSound, current.transform.position, 0.5f);
                        lastPlayedChop = Time.time;
                    }
                }
                else
                {
                    if (Time.time - lastPlayedChop > 0.05f)
                    {
                        AudioSource.PlayClipAtPoint(chopSound[Random.Range(0, chopSound.Length)], current.transform.position, 0.25f);
                        lastPlayedChop = Time.time;
                    }
                }
            }
        }

        if (UI.current.selected == this)
        {
            _material.SetFloat("_Pulse", 1);
        }
        else
        {
            _material.SetFloat("_Pulse", 0);
        }
    }
示例#4
0
    /// <summary>
    /// Generates vertices, uvs and triangles of a leaf and adds them to the tree object, which instantiates them later.
    /// The position and rotation of the leaf is specified by the state parameter, while the shape is specified by the lod parameter
    /// (higher LOD = more complex shape).
    /// </summary>
    /// <param name="state">State where the leaf should be created.</param>
    /// <param name="lod">Level of detail fo the leaf.</param>
    /// <param name="tree">The tree to which the leaf belongs.</param>
    private void CreateLeaf(State state, int lod, Spruce tree)
    {
        var offset = currentState.Width;
        var length = currentState.Width * 30;

        Vector3[] vertices;
        Vector2[] uv;
        int[]     triangles;
        // If lod is lower then 6, the simpler shape is chosen. If it is >= 6 a more complex one is chosen.
        if (lod < 6)
        {
            vertices = new Vector3[] {
                state.Position - offset * state.Heading - offset * state.Left,
                state.Position - offset * state.Heading + offset * state.Left,
                state.Position + offset * state.Heading,
                state.Position + state.Up * length * 1.3f + state.Heading * 65 * offset
            };
            uv = new Vector2[] {
                new Vector2(0, 0),
                new Vector2(0, 1),
                new Vector2(1, 0),
                new Vector2(1, 1),
            };
            triangles = new[] {
                3, 1, 0,
                3, 2, 1,
                3, 0, 2
            };
        }
        else
        {
            vertices = new Vector3[] {
                state.Position - offset * state.Heading - offset * state.Left,
                state.Position - offset * state.Heading + offset * state.Left,
                state.Position + offset * state.Heading - offset * state.Left,
                state.Position + offset * state.Heading + offset * state.Left,
                state.Position + state.Up * length + state.Heading * 50 * offset - offset * state.Heading - offset * state.Left,
                state.Position + state.Up * length + state.Heading * 50 * offset - offset * state.Heading + offset * state.Left,
                state.Position + state.Up * length + state.Heading * 50 * offset + offset * state.Heading - offset * state.Left,
                state.Position + state.Up * length + state.Heading * 50 * offset + offset * state.Heading + offset * state.Left,
                state.Position + state.Up * length * 1.3f + state.Heading * 65 * offset
            };
            uv = new Vector2[] {
                new Vector2(0, 0),
                new Vector2(0, 1),
                new Vector2(0.25f, 0),
                new Vector2(0.25f, 1),
                new Vector2(0.5f, 0),
                new Vector2(0.5f, 1),
                new Vector2(0.75f, 0),
                new Vector2(0.75f, 1),
                new Vector2(1, 0.5f)
            };
            triangles = new[] {
                4, 0, 1,
                4, 1, 5,
                6, 2, 0,
                6, 0, 4,
                7, 3, 6,
                6, 3, 2,
                5, 1, 3,
                5, 3, 7,
                8, 6, 4,
                8, 7, 6,
                8, 5, 7,
                8, 4, 5
            };
        }
        // If the tree object has reached the maximum number of vertices for leaves, a mesh of leaves is instantiated.
        if (tree.LeafCurrentVertices + vertices.Length >= Spruce.MaxVertices)
        {
            tree.InstatiateLeaf();
        }
        tree.LeafVertices.AddRange(vertices);
        tree.LeafUvs.AddRange(uv);
        for (int index = 0; index < triangles.Length; index++)
        {
            triangles[index] += tree.LeafCurrentVertices;
        }
        tree.LeafTris.AddRange(triangles);
        tree.LeafCurrentVertices += vertices.Length;
    }