123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- namespace KairoEngine.Chunks
- {
- [System.Serializable]
- public struct Chunk<TChunkBlock>
- {
- private bool initialized;
- public bool isInitialized() => initialized;
- private Vector3Int size;
- public Vector3Int position { get; private set; }
- public Vector3 globalPosition { get; private set; }
- public TChunkBlock[ , , ] blocks;
- public Chunk(Vector3Int size, Vector3 globalPosition, Vector3Int position, Func<Chunk<TChunkBlock>, Vector3Int, TChunkBlock> createChunkBlock,
- bool debug = false)
- {
- this.size = size;
- this.position = position;
- this.globalPosition = globalPosition;
- this.blocks = new TChunkBlock[size.x, size.y, size.z];
- this.initialized = true;
- for (int x = 0; x < size.x; x++)
- {
- for (int y = 0; y < size.y; y++)
- {
- for (int z = 0; z < size.z; z++)
- {
- blocks[x, y, z] = createChunkBlock(this, new Vector3Int(x, y, z));
- }
-
- }
- }
-
- }
- public TChunkBlock GetBlock(Vector3Int pos)
- {
- if(pos.x > size.x || pos.x < 0) return default(TChunkBlock);
- if(pos.y > size.y || pos.y < 0) return default(TChunkBlock);
- if(pos.z > size.z || pos.z < 0) return default(TChunkBlock);
- return blocks[pos.x, pos.y, pos.z];
- }
- public void SetBlock(Vector3Int pos, TChunkBlock data)
- {
- if(pos.x > size.x || pos.x < 0) return;
- if(pos.y > size.y || pos.y < 0) return;
- if(pos.z > size.z || pos.z < 0) return;
- blocks[pos.x, pos.y, pos.z] = data;
- }
- }
- }
|