ff3f9c75fccb3fbb7b882ec29e754e6135b53e60..034ed30da10c2117e9c2507ed1fef3cab43ee79c
2025-04-02 miepzerino
#36 load all items into itemDatabase on startup
034ed3 diff | tree
2025-04-02 miepzerino
#38 added minClusterSize to ores
d43c29 diff | tree
2025-04-02 miepzerino
#35 added generateable script to ore
f0315b diff | tree
5 files modified
2 files renamed
4 files added
244 ■■■■ changed files
Assets/Resources.meta 8 ●●●●● patch | view | raw | blame | history
Assets/Resources/Items.meta 8 ●●●●● patch | view | raw | blame | history
Assets/Resources/Items/IronOreItem.prefab patch | view | raw | blame | history
Assets/Resources/Items/IronOreItem.prefab.meta patch | view | raw | blame | history
Assets/Scenes/GameplayScene.unity 19 ●●●●● patch | view | raw | blame | history
Assets/Scripts/GenerateTileMap.cs 105 ●●●● patch | view | raw | blame | history
Assets/Scripts/Inventory/ItemDatabase.cs 27 ●●●●● patch | view | raw | blame | history
Assets/Scripts/Prefab/Drilling/Generateable.cs 29 ●●●●● patch | view | raw | blame | history
Assets/Scripts/Prefab/Drilling/Generateable.cs.meta 11 ●●●●● patch | view | raw | blame | history
Assets/TileSets/Palettes/DrillIronOre.prefab 20 ●●●●● patch | view | raw | blame | history
Assets/TileSets/Palettes/ForestRuleTileCustom.asset 17 ●●●●● patch | view | raw | blame | history
Assets/Resources.meta
New file
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 4ac220fd0e1536641a480531bbb38c65
folderAsset: yes
DefaultImporter:
  externalObjects: {}
  userData:
  assetBundleName:
  assetBundleVariant:
Assets/Resources/Items.meta
New file
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: dd3098fe15f11cf41b0623e380a01529
folderAsset: yes
DefaultImporter:
  externalObjects: {}
  userData:
  assetBundleName:
  assetBundleVariant:
Assets/Resources/Items/IronOreItem.prefab
Assets/Resources/Items/IronOreItem.prefab.meta
Assets/Scenes/GameplayScene.unity
@@ -697,7 +697,7 @@
  m_GameObject: {fileID: 519420028}
  serializedVersion: 2
  m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
  m_LocalPosition: {x: -6.15, y: -24.87, z: -10}
  m_LocalPosition: {x: 62, y: -22.89, z: -10}
  m_LocalScale: {x: 1, y: 1, z: 1}
  m_ConstrainProportionsScale: 0
  m_Children: []
@@ -2576,8 +2576,6 @@
  m_Script: {fileID: 11500000, guid: e4d1fabb0e4577b4d987741f664e3d68, type: 3}
  m_Name: 
  m_EditorClassIdentifier: 
  items:
  - {fileID: 7215668260114684477, guid: ead8eea59ce41c244814a994365e1129, type: 3}
--- !u!1 &1190838619
GameObject:
  m_ObjectHideFlags: 0
@@ -4186,7 +4184,7 @@
  m_GameObject: {fileID: 1624236510}
  serializedVersion: 2
  m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
  m_LocalPosition: {x: -6.15, y: -24.87, z: -10}
  m_LocalPosition: {x: 62, y: -22.89, z: -10}
  m_LocalScale: {x: 1, y: 1, z: 1}
  m_ConstrainProportionsScale: 0
  m_Children:
@@ -4871,13 +4869,8 @@
  m_EditorClassIdentifier: 
  forestRuleTile: {fileID: 11400000, guid: 3999614e192b37546a6b710bf5ceb30c, type: 2}
  borderTile: {fileID: 11400000, guid: dcef846474e534b45ab3b175559c19a2, type: 2}
  ores:
  - name: Iron
    weight: 7
    clusterWeight: 40
    tile: {fileID: 11400000, guid: bbc5a3964d8331546a4dbc880f8ae9fc, type: 2}
    maxSpawnHeight: 250
    minSpawnHeight: 150
  generateables:
  - {fileID: 5943816882416256805, guid: 858e9987645c29748a5b0990923da82f, type: 3}
--- !u!1 &1955008394
GameObject:
  m_ObjectHideFlags: 0
@@ -5748,11 +5741,11 @@
      objectReference: {fileID: 0}
    - target: {fileID: 2368348636056148999, guid: c220ec455fce341408d66d880b464cad, type: 3}
      propertyPath: m_LocalPosition.x
      value: -6.15
      value: 62
      objectReference: {fileID: 0}
    - target: {fileID: 2368348636056148999, guid: c220ec455fce341408d66d880b464cad, type: 3}
      propertyPath: m_LocalPosition.y
      value: -24.87
      value: -22.89
      objectReference: {fileID: 0}
    - target: {fileID: 2368348636056148999, guid: c220ec455fce341408d66d880b464cad, type: 3}
      propertyPath: m_LocalPosition.z
Assets/Scripts/GenerateTileMap.cs
@@ -6,28 +6,28 @@
using UnityEngine.Tilemaps;
using UnityEngine.UIElements;
[Serializable]
public class Ore
{
    public string name;
    /// <summary>
    /// The lower the numer the higher the amount of ores that will spawn
    /// Higher number means less ore
    /// </summary>
    [Tooltip("The lower the numer the higher the amount of ores that will spawn. Higher number means less ore.")]
    [Range(1, 100000)]
    public int weight;
    /// <summary>
    /// The lower the number the more dense the ore will spawn (big clusters
    /// Higher number means little clusters (more spread)
    /// </summary>
    [Tooltip("The lower the number the more dense the ore will spawn (big clusters. Higher number means little clusters (more spread).")]
    [Range(10, 100000)]
    public int clusterWeight;
    public CustomRuleTile tile;
    public int maxSpawnHeight;
    public int minSpawnHeight;
}
//[Serializable]
//public class Ore
//{
//    public string name;
//    /// <summary>
//    /// The lower the numer the higher the amount of ores that will spawn
//    /// Higher number means less ore
//    /// </summary>
//    [Tooltip("The lower the numer the higher the amount of ores that will spawn. Higher number means less ore.")]
//    [Range(1, 100000)]
//    public int weight;
//    /// <summary>
//    /// The lower the number the more dense the ore will spawn (big clusters
//    /// Higher number means little clusters (more spread)
//    /// </summary>
//    [Tooltip("The lower the number the more dense the ore will spawn (big clusters. Higher number means little clusters (more spread).")]
//    [Range(10, 100000)]
//    public int clusterWeight;
//    public CustomRuleTile tile;
//    public int maxSpawnHeight;
//    public int minSpawnHeight;
//}
public class GenerateTileMap : MonoBehaviour
{
@@ -41,7 +41,7 @@
    Tilemap tilemap;
    public CustomRuleTile forestRuleTile;
    public TileBase borderTile;
    public List<Ore> ores;
    public List<Generateable> generateables;
    //public List<TileBase> tiles;
    private void Awake()
@@ -114,24 +114,30 @@
            }
        }
        if (ores != null)
        if (generateables != null)
        {
            foreach (Ore ore in ores)
            foreach (Generateable generateable in generateables)
            {
                for (int x = 0; x < maxWidth; x++)
                {
                    for (int y = ore.minSpawnHeight; y < ore.maxSpawnHeight; y++)
                    for (int y = generateable.minSpawnHeight; y < generateable.maxSpawnHeight; y++)
                    {
                        float xPerlin = ((float)x / maxWidth) * (float)ore.clusterWeight + offsetX;
                        float yPerlin = ((float)y / maxHeight) * (float)ore.clusterWeight + offsetY;
                        float xPerlin = ((float)x / maxWidth) * (float)generateable.clusterWeight + offsetX;
                        float yPerlin = ((float)y / maxHeight) * (float)generateable.clusterWeight + offsetY;
                        float perlinNoise = Mathf.PerlinNoise(xPerlin, yPerlin);
                        if (perlinNoise <= (1f / (float)ore.weight))
                        if (perlinNoise <= (1f / (float)generateable.weight))
                        {
                            Vector3Int tileSpawnCoord = new Vector3Int(x, y);
                            if (!destroyedTiles.Contains(tileSpawnCoord) && tilemap.HasTile(tileSpawnCoord))
                            {
                                tilemap.SetTile(tileSpawnCoord, ore.tile);
                                // Check potential cluster size before placing
                                int clusterSize = CountPotentialClusterSize(x, y, generateable.weight, generateable.clusterWeight);
                                if (clusterSize >= generateable.minClusterSize)
                                {
                                    tilemap.SetTile(tileSpawnCoord, generateable.tile);
                                }
                                //tilemap.SetTile(tileSpawnCoord, generateable.tile);
                            }
                        }
@@ -167,4 +173,43 @@
        yield return null;
        finishedCallback();
    }
    private int CountPotentialClusterSize(int startX, int startY, int weight, int clusterWeight)
    {
        int size = 0;
        Queue<Vector2Int> toCheck = new Queue<Vector2Int>();
        HashSet<Vector2Int> checked_positions = new HashSet<Vector2Int>();
        toCheck.Enqueue(new Vector2Int(startX, startY));
        checked_positions.Add(new Vector2Int(startX, startY));
        while (toCheck.Count > 0)
        {
            Vector2Int current = toCheck.Dequeue();
            size++;
            // Check all 8 neighboring tiles
            for (int dx = -1; dx <= 1; dx++)
            {
                for (int dy = -1; dy <= 1; dy++)
                {
                    if (dx == 0 && dy == 0) continue;
                    Vector2Int neighbor = new Vector2Int(current.x + dx, current.y + dy);
                    if (checked_positions.Contains(neighbor)) continue;
                    float xPerlin = ((float)neighbor.x / maxWidth) * clusterWeight + offsetX;
                    float yPerlin = ((float)neighbor.y / maxHeight) * clusterWeight + offsetY;
                    float perlinNoise = Mathf.PerlinNoise(xPerlin, yPerlin);
                    if (perlinNoise <= (1f / (float)weight))
                    {
                        toCheck.Enqueue(neighbor);
                        checked_positions.Add(neighbor);
                    }
                }
            }
        }
        return size;
    }
}
Assets/Scripts/Inventory/ItemDatabase.cs
@@ -5,9 +5,7 @@
{
    public static ItemDatabase Instance { get; private set; }
    [SerializeField]
    private List<Item> items = new List<Item>();
    private Dictionary<string, Item> itemDictionary = new Dictionary<string, Item>();
    private Dictionary<int, Item> itemIdDictionary = new Dictionary<int, Item>();
@@ -16,6 +14,7 @@
        if (Instance == null)
        {
            Instance = this;
            LoadItemsFromResources();
            InitializeItemDictionary();
        }
        else
@@ -24,6 +23,30 @@
        }
    }
    private void LoadItemsFromResources()
    {
        // Load all Item prefabs from the "Resources/Items" folder
        GameObject[] itemPrefabs = Resources.LoadAll<GameObject>("Items");
        foreach (GameObject prefab in itemPrefabs)
        {
            Item item = prefab.GetComponent<Item>();
            if (item != null)
            {
                items.Add(item);
            }
            else
            {
                Debug.LogWarning($"Prefab {prefab.name} does not have an Item component");
            }
        }
        if (items.Count == 0)
        {
            Debug.LogWarning("No items found in Resources/Items folder");
        }
    }
    private void InitializeItemDictionary()
    {
        itemDictionary.Clear();
Assets/Scripts/Prefab/Drilling/Generateable.cs
New file
@@ -0,0 +1,29 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[Serializable]
public class Generateable : MonoBehaviour
{
    public bool isGenerateable = true;
    //public string nameKey;
    /// <summary>
    /// The lower the numer the higher the amount of ores that will spawn
    /// Higher number means less ore
    /// </summary>
    [Tooltip("The lower the numer the higher the amount of ores that will spawn. Higher number means less ore.")]
    [Range(1, 100000)]
    public int weight;
    /// <summary>
    /// The lower the number the more dense the ore will spawn (big clusters
    /// Higher number means little clusters (more spread)
    /// </summary>
    [Tooltip("The lower the number the more dense the ore will spawn (big clusters. Higher number means little clusters (more spread).")]
    [Range(10, 100000)]
    public int clusterWeight;
    public int minClusterSize;
    public CustomRuleTile tile;
    public int maxSpawnHeight;
    public int minSpawnHeight;
}
Assets/Scripts/Prefab/Drilling/Generateable.cs.meta
New file
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 6b939c0393f507f4fb9e91dfb683f869
MonoImporter:
  externalObjects: {}
  serializedVersion: 2
  defaultReferences: []
  executionOrder: 0
  icon: {instanceID: 0}
  userData:
  assetBundleName:
  assetBundleVariant:
Assets/TileSets/Palettes/DrillIronOre.prefab
@@ -11,6 +11,7 @@
  - component: {fileID: 8973229189939374991}
  - component: {fileID: -776302461885629690}
  - component: {fileID: 3926666667628910059}
  - component: {fileID: 5943816882416256805}
  m_Layer: 6
  m_Name: DrillIronOre
  m_TagString: Untagged
@@ -61,3 +62,22 @@
  isDropable: 1
  dropable: {fileID: 7215668260114684477, guid: ead8eea59ce41c244814a994365e1129, type: 3}
  dropAmount: 2
--- !u!114 &5943816882416256805
MonoBehaviour:
  m_ObjectHideFlags: 0
  m_CorrespondingSourceObject: {fileID: 0}
  m_PrefabInstance: {fileID: 0}
  m_PrefabAsset: {fileID: 0}
  m_GameObject: {fileID: 3549545049718014148}
  m_Enabled: 1
  m_EditorHideFlags: 0
  m_Script: {fileID: 11500000, guid: 6b939c0393f507f4fb9e91dfb683f869, type: 3}
  m_Name:
  m_EditorClassIdentifier:
  isGenerateable: 1
  weight: 7
  clusterWeight: 30
  minClusterSize: 8
  tile: {fileID: 11400000, guid: bbc5a3964d8331546a4dbc880f8ae9fc, type: 2}
  maxSpawnHeight: 250
  minSpawnHeight: 150
Assets/TileSets/Palettes/ForestRuleTileCustom.asset
@@ -12,7 +12,7 @@
  m_Script: {fileID: 11500000, guid: 4f32125565481304cbcd5f6e6cefa637, type: 3}
  m_Name: ForestRuleTileCustom
  m_EditorClassIdentifier: 
  m_DefaultSprite: {fileID: 0}
  m_DefaultSprite: {fileID: 1962712664, guid: 42962cf14c23fee4baaaea0cd52d52e0, type: 3}
  m_DefaultGameObject: {fileID: 3549545049718014148, guid: c82bc431abdeded4ab41cfdd19459501, type: 3}
  m_DefaultColliderType: 1
  m_TilingRules:
@@ -108,14 +108,12 @@
    m_Output: 0
    m_ColliderType: 1
    m_RandomTransform: 0
    m_Neighbors: 010000000200000001000000010000000100000001000000
    m_Neighbors: 01000000020000000100000001000000
    m_NeighborPositions:
    - {x: 0, y: 1, z: 0}
    - {x: 0, y: -1, z: 0}
    - {x: -1, y: 0, z: 0}
    - {x: 1, y: 0, z: 0}
    - {x: -1, y: 1, z: 0}
    - {x: 1, y: 1, z: 0}
    m_RuleTransform: 0
  - m_Id: 6
    m_Sprites:
@@ -242,13 +240,12 @@
    m_Output: 0
    m_ColliderType: 1
    m_RandomTransform: 0
    m_Neighbors: 0100000001000000010000000100000001000000010000000200000001000000
    m_Neighbors: 01000000010000000100000001000000010000000200000001000000
    m_NeighborPositions:
    - {x: 0, y: 1, z: 0}
    - {x: 1, y: 0, z: 0}
    - {x: -1, y: 0, z: 0}
    - {x: 0, y: -1, z: 0}
    - {x: -1, y: -1, z: 0}
    - {x: 1, y: -1, z: 0}
    - {x: -1, y: 1, z: 0}
    - {x: 1, y: 1, z: 0}
@@ -263,16 +260,15 @@
    m_Output: 0
    m_ColliderType: 1
    m_RandomTransform: 0
    m_Neighbors: 0100000001000000010000000100000001000000010000000200000001000000
    m_Neighbors: 01000000010000000100000001000000020000000100000001000000
    m_NeighborPositions:
    - {x: 0, y: 1, z: 0}
    - {x: 1, y: 0, z: 0}
    - {x: -1, y: 0, z: 0}
    - {x: 0, y: -1, z: 0}
    - {x: -1, y: -1, z: 0}
    - {x: 1, y: -1, z: 0}
    - {x: 1, y: 1, z: 0}
    - {x: -1, y: 1, z: 0}
    - {x: 1, y: -1, z: 0}
    m_RuleTransform: 0
  - m_Id: 14
    m_Sprites:
@@ -284,14 +280,13 @@
    m_Output: 0
    m_ColliderType: 1
    m_RandomTransform: 0
    m_Neighbors: 0100000001000000010000000200000001000000020000000100000001000000
    m_Neighbors: 01000000010000000100000002000000010000000100000001000000
    m_NeighborPositions:
    - {x: 0, y: 1, z: 0}
    - {x: 1, y: 0, z: 0}
    - {x: -1, y: 0, z: 0}
    - {x: -1, y: 1, z: 0}
    - {x: 1, y: 1, z: 0}
    - {x: 1, y: -1, z: 0}
    - {x: 0, y: -1, z: 0}
    - {x: -1, y: -1, z: 0}
    m_RuleTransform: 0