private ResourceField CreateFieldWithPosition(ResourceField resourceField, int position)
        {
            var field = _mapper.Map <ResourceField, ResourceField>(resourceField);

            field.Position = position;

            return(field);
        }
Ejemplo n.º 2
0
        public ResourceAssert AssertField(ResourceField field, object value)
        {
            var actualValue = _resource.GetValue(field);
            var equals      = Equals(value, actualValue);

            Assert.True(equals, $"Resource {_resource.Id} {field.Name} should have been '{value}' but was '{actualValue}'");
            return(this);
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Adds a ResourceField to the Manager.
 /// </summary>
 /// <param name="field">The field to be added.</param>
 public void Register(ResourceField field)
 {
     if (WorldToLocal(field.transform.position, out Vector2Int local) == false || resourceFields.InBounds(local) == false)
     {
         Debug.LogError("Could not add field at position " + field.transform.position + "! OutOfBounds!");
         return;
     }
     resourceFields.Get(local).Add(field);
 }
        /// <summary>
        /// Creates a resource field for the village on the given position.
        /// </summary>
        /// <param name="resourceField">A base resource field.</param>
        /// <param name="village">The village to seed.</param>
        /// <param name="position">The position of the field.</param>
        /// <returns></returns>
        private VillageResourceField CreateVillageResourceField(ResourceField resourceField, Village village)
        {
            var villageResourceField = new VillageResourceField()
            {
                Position      = resourceField.Position,
                ResourceField = resourceField,
                Village       = village
            };

            return(villageResourceField);
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Removes a ResourceField from the Manager.
 /// </summary>
 /// <param name="field">The field to be removed</param>
 public void DeRegister(ResourceField field)
 {
     foreach (List <ResourceField> resourceField in resourceFields)
     {
         for (int i = 0; i < resourceField.Count; i++)
         {
             if (resourceField[i] == field)
             {
                 resourceField.RemoveAt(i);
                 return;
             }
         }
     }
     Debug.LogError("Could not find " + field + " in map");
 }
Ejemplo n.º 6
0
        public override void OnInspectorGUI()
        {
            ResourceField r = target as ResourceField;

            if (r)
            {
                bool changed = false;
                EditorGUI.BeginChangeCheck();
                r.resourceType = EditorGUILayout.IntSlider("Type", r.resourceType, 0, r.settingData.Length - 1);
                if (EditorGUI.EndChangeCheck())
                {
                    changed = true;
                }
                EditorGUI.BeginChangeCheck();
                EditorGUILayout.PropertyField(serializedObject.FindProperty("settingData"), true);
                if (EditorGUI.EndChangeCheck())
                {
                    serializedObject.ApplyModifiedProperties();
                    changed = true;
                }
                if (changed)
                {
                    if (r.resourceType >= 0 && r.resourceType < r.settingData.Length)
                    {
                        ResourceData rd = r.settingData[r.resourceType];

                        Text t = r.GetComponentInChildren <Text>();
                        if (t)
                        {
                            t.color = rd.textColor;
                        }

                        Image img = r.GetComponentInChildren <Image>();
                        if (img && rd.icon)
                        {
                            Sprite spr = Sprite.Create(rd.icon, new Rect(0, 0, rd.icon.width, rd.icon.height),
                                                       Vector2.zero);
                            img.sprite = spr;
                        }
                    }


                    EditorUtility.SetDirty(r);
                }
            }
        }
Ejemplo n.º 7
0
    public override void OnBuildingPlaced()
    {
        base.OnBuildingPlaced();

        // Add this rice to a new rice resource field
        Transform newParent = new GameObject("Rice Resource Field").transform;

        newParent.parent = transform.parent;

        ResourceField   field     = newParent.gameObject.AddComponent <ResourceField>();
        List <Resource> resources = new List <Resource>();
        Rice            rice      = GetComponent <Rice>();

        rice.ParentField = field;
        resources.Add(rice);

        field.Set(resources);
    }
Ejemplo n.º 8
0
    void Start()
    {
        Entitas entity = Selection.instance.selectedEntities.Values.First();

        if (entity is ResourceField)
        {
            //print("is resources field");
            Thealth.text = entity.healthPoint.ToString() + '/' + entity.maxHealth.ToString();
            Building b = (Building)entity;
            Tlevel.text = "Lv " + b.level.ToString();
            ResourceField rf = (ResourceField)entity;
            Tproduction.text = '+' + rf.production.ToString();
        }
        else if (entity is Villager)
        {
            //print("is villager");
            Thealth.text = entity.healthPoint.ToString() + '/' + entity.maxHealth.ToString();
        }
        else if (entity is Unit)
        {
            //print("is villager");
            Thealth.text = entity.healthPoint.ToString() + '/' + entity.maxHealth.ToString();
        }
    }
Ejemplo n.º 9
0
    private void GenerateRessourceFields(Fast2DArray <Vector3> verts, Fast2DArray <bool> alreadyPlaced, float minHeight, float maxHeight)
    {
        Transform resourceFieldsParent = new GameObject("Resource Fields").transform;

        resourceFieldsParent.parent = map.transform;

        // Set all fast noise values from the fields of this class
        FastNoise noiceGenerator = new FastNoise(seed);

        noiceGenerator.SetNoiseType(FastNoise.NoiseType.SimplexFractal);
        noiceGenerator.SetFrequency(resourceFrequency);
        noiceGenerator.SetFractalOctaves(resourceOctaves);
        noiceGenerator.SetFractalLacunarity(resourceLacunarity);
        noiceGenerator.SetFractalGain(resourceGain);

        float biomeMinValue = float.MaxValue, biomeMaxValue = float.MinValue;

        Fast2DArray <float> resourceNoise = new Fast2DArray <float>(xSize, ySize);

        for (int x = 0; x < xSize; x++)
        {
            for (int y = 0; y < ySize; y++)
            {
                float noise = noiceGenerator.GetSimplexFractal(x, y);
                resourceNoise.Set(noise, x, y);

                // normalize values
                if (noise < biomeMinValue)
                {
                    biomeMinValue = noise;
                }
                if (noise > biomeMaxValue)
                {
                    biomeMaxValue = noise;
                }
            }
        } // end noise gen

        // normalize values
        float   maxMinusMin = biomeMaxValue - biomeMinValue;
        Vector2 minVector   = new Vector2(biomeMinValue, biomeMinValue);
        Vector2 forestRange = (this.forestRange * maxMinusMin) + minVector;
        Vector2 bambooRange = (this.bambooRange * maxMinusMin) + minVector;
        Vector2 stoneRange  = (this.stoneRange * maxMinusMin) + minVector;

        for (int x = 0; x < xSize; x++)
        {
            for (int y = 0; y < ySize; y++)
            {
                if (alreadyPlaced.Get(x, y) == true)
                {
                    continue;
                }

                float             noise = resourceNoise.Get(x, y);
                List <Vector2Int> positions;
                Vector2           range;
                Resource[]        resourcePrefabs;

                // Go through all ranges and assign a resource to the above created values
                if (MathUtil.InRangeInclusive(forestRange.x, forestRange.y, noise))
                {
                    resourcePrefabs = forestPrefabs;
                    range           = forestRange;
                }
                else if (MathUtil.InRangeInclusive(bambooRange.x, bambooRange.y, noise))
                {
                    resourcePrefabs = bambooPrefabs;
                    range           = bambooRange;
                }
                else if (MathUtil.InRangeInclusive(stoneRange.x, stoneRange.y, noise))
                {
                    resourcePrefabs = stonePrefabs;
                    range           = stoneRange;
                }
                else
                {
                    continue;
                }

                // get all positions from the range
                positions = AllNeighbours(resourceNoise, new Vector2Int(x, y), range.x, range.y, alreadyPlaced);

                // if the amount of positions is less then the minimum or the random chance a field should not spawn, simply continue the loop
                if (positions.Count < minAmountInResourceField || Random.value > resourceSpawnChance)
                {
                    positions.ForEach(t => alreadyPlaced.Set(false, t));
                    continue;
                }

                // Create the resourceField
                Transform resourceFieldTransform = new GameObject().transform;
                resourceFieldTransform.parent = resourceFieldsParent;
                ResourceField resourceField = resourceFieldTransform.gameObject.AddComponent <ResourceField>();

                // Create the resources
                List <Resource> resources = new List <Resource>();
                for (int i = 0; i < positions.Count; i++)
                {
                    RaycastHit[] hits = GetHitsFromRandomPoint(verts.Get(positions[i]), minHeight, maxHeight);

                    for (int j = 0; j < hits.Length; j++)
                    {
                        if (hits[j].collider.gameObject == map)
                        {
                            Resource resource = Instantiate(ArrayUtil <Resource> .RandomElement(resourcePrefabs));
                            resource.ParentField = resourceField;
                            resources.Add(resource);

                            resource.transform.localScale = scaleVector;
                            resource.transform.position   = hits[j].point;
                            resource.transform.rotation   = Quaternion.Euler(0, Random.Range(0f, 360f), 0);

                            break;
                        }
                    } // end iteration hits
                }     // end iteration positions

                // if something went wrong simply delete the resourcefield again
                if (resources.Count == 0)
                {
                    positions.ForEach(t => alreadyPlaced.Set(false, t));
                    Destroy(resourceField.gameObject);
                    continue;
                }

                // assign the resources to the field
                resourceField.Set(resources);
            }
        } // end iteration through noise
    }     // end generate resourceFields
Ejemplo n.º 10
0
    /// <summary>
    /// Gets the clostest ResourceField.
    /// </summary>
    /// <param name="type">The type of the resource field.</param>
    /// <param name="position">The position of the person who wants a resource fields.</param>
    /// <param name="resourceField">The best resource field.</param>
    /// <returns>Wheter it successeded or not.</returns>
    public bool TryGetNextResourceField(ResourceType type, Vector3 position, out ResourceField resourceField)
    {
        if (WorldToLocal(position, out Vector2Int start) == false)
        {
            Debug.LogError("Character was out of bounds!" + position);
            resourceField = default;
            return(false);
        }

        int  incrementer = -1;
        bool running     = true;

        while (running)
        {
            tempFields.Clear();
            running = false;
            incrementer++;

            // iterate through the most outer potential fields
            for (int x = -incrementer; x < incrementer + 1; x++)
            {
                for (int y = -incrementer; y < incrementer + 1; y++)
                {
                    if (x == -incrementer || x == incrementer || y == -incrementer || y == incrementer)
                    {
                        // If they are in bounds add them to the temp fields
                        Vector2Int newPos = start + new Vector2Int(x, y);
                        if (resourceFields.InBounds(newPos))
                        {
                            running = true;
                            tempFields.Add(resourceFields.Get(x, y));
                        }
                    }
                }
            }

            // try and find the best field, if any are present
            ResourceField bestField    = null;
            float         bestDistance = float.MaxValue;
            foreach (List <ResourceField> potentialFields in tempFields)
            {
                for (int i = 0; i < potentialFields.Count; i++)
                {
                    if (potentialFields[i].Type == type)
                    {
                        // calculate new distance
                        float newDistance = (potentialFields[i].transform.position - position).sqrMagnitude;
                        if (newDistance < bestDistance)
                        {
                            bestDistance = newDistance;
                            bestField    = potentialFields[i];
                        }
                    }
                }
            }

            // If a field was assigned then it is the clostest field
            if (bestField != null)
            {
                resourceField = bestField;
                return(true);
            }

            // If the incrementer is larger then half the xSize and half the ySize then stop the search
            if (incrementer > resourceFields.XSize / 2 && incrementer > resourceFields.YSize / 2)
            {
                break;
            }
        }

        // nothing found return an error
        Debug.LogError("Could not find resourcefield. Everything is exhausted or were none registered?");
        resourceField = default;
        return(false);
    }