Beispiel #1
0
        public void GenerateMap()
        {
            if (resourceInstances != null)
            {
                for (int i = 0; i < resourceInstances.Length; i++)
                {
                    MapResourceObject instance = resourceInstances[i];
                    if (instance != null)
                    {
                        Destroy(instance.gameObject);
                    }
                }
            }

            if (generateRandom || seed == 0)
            {
                seed = (uint)Environment.TickCount;
            }

            Random rand = new Random(seed);

            int size = mapSizes[(int)mapSize];

            terrainGenerator.Generate(size, rand, mapHeight);

            resourceInstances = resourceGenerator.GenerateResources(rand, terrainGenerator.terrain, size);

            cameraController.Init();
        }
Beispiel #2
0
        private void Load(BinaryReader reader)
        {
            cameraController.Load(reader);

            seed    = reader.ReadUInt32();
            mapSize = (MapSize)reader.ReadInt32();

            int size = mapSizes[(int)mapSize];

            terrainGenerator.Generate(size, new Random(seed), mapHeight);

            int length = reader.ReadInt32();

            resourceInstances = new MapResourceObject[length];

            for (int i = 0; i < length; i++)
            {
                int index = reader.ReadInt32();
                if (index < 0)
                {
                    continue;
                }

                MapResourceObject obj = resourceGenerator.CreateInstance();
                obj.Load(reader, items.Collection);
            }
        }
Beispiel #3
0
 public void Clear()
 {
     if (resourceInstances == null)
     {
         return;
     }
     for (int i = 0; i < resourceInstances.Length; i++)
     {
         MapResourceObject instance = resourceInstances[i];
         if (instance != null)
         {
             Destroy(instance.gameObject);
         }
     }
 }
Beispiel #4
0
        private void Save(BinaryWriter writer)
        {
            cameraController.Save(writer);

            writer.Write(seed);
            writer.Write((int)mapSize);

            int length = resourceInstances?.Length ?? 0;

            writer.Write(length);

            for (int i = 0; i < length; i++)
            {
                MapResourceObject instance = resourceInstances[i];
                if (instance == null)
                {
                    writer.Write(-i - 1);
                    continue;
                }

                writer.Write(i);
                instance.Save(writer, null);
            }
        }
        public MapResourceObject[] GenerateResources(Random rand, Terrain terrain, int size)
        {
            int featureSize = size / 16;
            int halfSize    = size / 2 - 16 / 2;

            MapResourceObject[] mapResourceObjects = new MapResourceObject[featureSize * featureSize];

            for (int z = 0; z < featureSize; z++)
            {
                for (int x = 0; x < featureSize; x++)
                {
                    uint index = rand.NextUInt(0, 5);

                    ItemObj[] resources;

                    switch (index)
                    {
                    default:
                        continue;

                    case 0:
                        resources = ironResources;
                        break;

                    case 1:
                        resources = copperResources;
                        break;

                    case 2:
                        resources = coalResources;
                        break;
                    }

                    if (resources.Length == 0)
                    {
                        continue;
                    }

                    ItemObj obj = resources[rand.NextInt(0, resources.Length)];

                    switch (obj.Item)
                    {
                    case MapResource resourceObject:
                    {
                        MapResourceObject instance = mapResourceObjects[x + z * featureSize] =
                            Instantiate(resourcePrefab, transform);
                        Vector3 pos = new Vector3(x * 16 - halfSize, 0, z * 16 - halfSize);
                        pos.y = terrain.SampleHeight(pos);

                        Quaternion rotation = Quaternion.Euler(0, rand.NextFloat(-180, 180), 0);

                        instance.Init(pos, rotation, obj.Item as MapResource, rand.NextUInt(3000, 6000));
                        break;
                    }

                    case Factory factory:

                        break;
                    }
                }
            }

            return(mapResourceObjects);
        }
 private void ShowResourceTooltip(MapResourceObject obj)
 {
     label.text = obj.Item.Name;
     info.text  = obj.Count.ToString();
 }