浏览代码

Initial commit

James Peret 3 年之前
当前提交
cb100fdd7e

+ 71 - 0
.gitignore

@@ -0,0 +1,71 @@
+# This .gitignore file should be placed at the root of your Unity project directory
+#
+# Get latest from https://github.com/github/gitignore/blob/master/Unity.gitignore
+#
+/[Ll]ibrary/
+/[Tt]emp/
+/[Oo]bj/
+/[Bb]uild/
+/[Bb]uilds/
+/[Ll]ogs/
+/[Uu]ser[Ss]ettings/
+
+# MemoryCaptures can get excessive in size.
+# They also could contain extremely sensitive data
+/[Mm]emoryCaptures/
+
+# Asset meta data should only be ignored when the corresponding asset is also ignored
+!/[Aa]ssets/**/*.meta
+
+# Uncomment this line if you wish to ignore the asset store tools plugin
+# /[Aa]ssets/AssetStoreTools*
+
+# Autogenerated Jetbrains Rider plugin
+/[Aa]ssets/Plugins/Editor/JetBrains*
+
+# Visual Studio cache directory
+.vs/
+
+# Gradle cache directory
+.gradle/
+
+# Autogenerated VS/MD/Consulo solution and project files
+ExportedObj/
+.consulo/
+*.csproj
+*.unityproj
+*.sln
+*.suo
+*.tmp
+*.user
+*.userprefs
+*.pidb
+*.booproj
+*.svd
+*.pdb
+*.mdb
+*.opendb
+*.VC.db
+
+# Unity3D generated meta files
+*.pidb.meta
+*.pdb.meta
+*.mdb.meta
+
+# Unity3D generated file on crash reports
+sysinfo.txt
+
+# Builds
+*.apk
+*.aab
+*.unitypackage
+
+# Crashlytics generated file
+crashlytics-build.properties
+
+# Packed Addressables
+/[Aa]ssets/[Aa]ddressable[Aa]ssets[Dd]ata/*/*.bin*
+
+# Temporary auto-generated Android Assets
+/[Aa]ssets/[Ss]treamingAssets/aa.meta
+/[Aa]ssets/[Ss]treamingAssets/aa/*

+ 8 - 0
Runtime.meta

@@ -0,0 +1,8 @@
+fileFormatVersion: 2
+guid: 94835a617f93c7f428150e69242b474c
+folderAsset: yes
+DefaultImporter:
+  externalObjects: {}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 15 - 0
Runtime/BuildingBehaviour.cs

@@ -0,0 +1,15 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+using Sirenix.OdinInspector;
+
+namespace KairoEngine.VoxelBuildingSystem
+{
+    [HideMonoScript]
+    public class BuildingBehaviour : MonoBehaviour
+    {
+        public Vector3Int origin;
+        public BuildingTemplate template;
+    }
+}
+

+ 11 - 0
Runtime/BuildingBehaviour.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: d4191c938ae796a4d874b912ffbec7ad
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 52 - 0
Runtime/BuildingBlueprintController.cs

@@ -0,0 +1,52 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+using KairoEngine.Core;
+
+namespace KairoEngine.VoxelBuildingSystem
+{
+    public class BuildingBlueprintController : MonoBehaviour
+    {
+        public VoxelBuildingSystem buildingSystem;
+        public Transform currentBlueprint;
+
+        private void Start() {
+            RefreshVisual();
+            GenericEvents.StartListening("OnSelectedChanged", OnSelectedChanged);
+        }
+
+        private void OnDisable()
+        {
+            GenericEvents.StopListening("OnSelectedChanged", OnSelectedChanged);
+        }
+
+        private void OnSelectedChanged() {
+            RefreshVisual();
+        }
+
+        private void LateUpdate() {
+            if(currentBlueprint == null) return;
+            Vector3 targetPosition = buildingSystem.GetMouseWorldSnappedPosition();
+            //targetPosition.y = 1f;
+            currentBlueprint.transform.position = Vector3.Lerp(currentBlueprint.transform.position, targetPosition, Time.deltaTime * 15f);
+            currentBlueprint.transform.rotation = Quaternion.Lerp(transform.rotation, buildingSystem.transform.rotation, Time.deltaTime * 15f);
+            if(buildingSystem.tool == VoxelBuildingTool.Build) currentBlueprint.gameObject.SetActive(true);
+            else currentBlueprint.gameObject.SetActive(false);
+        }
+
+        private void RefreshVisual() {
+            if (currentBlueprint != null) {
+                Destroy(currentBlueprint.gameObject);
+                currentBlueprint = null;
+            }
+            BuildingTemplate buildingTemplate = buildingSystem.buildingTemplates[buildingSystem.currentBuildingTemplate];
+            if (buildingTemplate != null) {
+                currentBlueprint = Instantiate(buildingTemplate.visual, Vector3.zero, Quaternion.identity);
+                currentBlueprint.parent = transform;
+                currentBlueprint.localPosition = Vector3.zero;
+                currentBlueprint.localEulerAngles = Vector3.zero;
+            }
+        }
+        
+    }
+}

+ 11 - 0
Runtime/BuildingBlueprintController.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 544b12af1d4c01b4c9c814f4be347e79
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 23 - 0
Runtime/BuildingTemplate.cs

@@ -0,0 +1,23 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+using KairoEngine.Stockpiles;
+using Sirenix.OdinInspector;
+
+
+namespace KairoEngine.VoxelBuildingSystem
+{
+    [CreateAssetMenu(fileName = "BuildingTemplate", menuName = "KairoEngine/VoxelBuildingSystem/BuildingTemplate"), HideMonoScript]
+    public class BuildingTemplate : ScriptableObject
+    {
+        [BoxGroup("Properties", showLabel: false)] public string title;
+        [BoxGroup("Properties")] public Vector3Int size;
+        [BoxGroup("Properties")] public Transform prefab;
+        [BoxGroup("Properties")] public Transform visual;
+        [BoxGroup("Properties")] public Sprite image;
+        [BoxGroup("Properties")] public Sprite icon;
+        [BoxGroup("Properties"), TextArea(2, 8), HideLabel, PropertySpace(4, 4)] public string description;
+
+        [PropertySpace(1, 1)] public List<Stockpile> cost = new List<Stockpile>();
+    }
+}

+ 11 - 0
Runtime/BuildingTemplate.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 8bf48d06b8f525d4585d9b2ceccb0857
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 23 - 0
Runtime/KairoEngine.VoxelBuildingSystem.asmdef

@@ -0,0 +1,23 @@
+{
+    "name": "KairoEngine.VoxelBuildingSystem",
+    "rootNamespace": "",
+    "references": [
+        "GUID:7e5ae6a38d1532248b4c890eca668b06",
+        "GUID:22b3fea425824d6448cb89ed3c76c9a8",
+        "GUID:b5398ac4f2218bd4ba186b0baff7fb21",
+        "GUID:f452697229e6bcc469c0eff1574ac090",
+        "GUID:165d83fc3bb2a4144925c85421871d8e",
+        "GUID:142285d3db5e7e849b02ea3a75bc2de7",
+        "GUID:e048eeec9bdb9d30448017b829deb3f6",
+        "GUID:560b04d1a97f54a4e82edc0cbbb69285"
+    ],
+    "includePlatforms": [],
+    "excludePlatforms": [],
+    "allowUnsafeCode": false,
+    "overrideReferences": false,
+    "precompiledReferences": [],
+    "autoReferenced": true,
+    "defineConstraints": [],
+    "versionDefines": [],
+    "noEngineReferences": false
+}

+ 7 - 0
Runtime/KairoEngine.VoxelBuildingSystem.asmdef.meta

@@ -0,0 +1,7 @@
+fileFormatVersion: 2
+guid: bc35f328c2f71ad42b8e7938996bdcce
+AssemblyDefinitionImporter:
+  externalObjects: {}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 18 - 0
Runtime/VoxelBuildData.cs

@@ -0,0 +1,18 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+
+namespace KairoEngine.VoxelBuildingSystem
+{
+    public struct VoxelBuildData
+    {
+        public Vector3Int pos;
+        public int buildingIndex;
+
+        public VoxelBuildData(Vector3Int pos, int buildingIndex = 0)
+        {
+            this.pos = pos;
+            this.buildingIndex = buildingIndex;
+        }
+    }
+}

+ 11 - 0
Runtime/VoxelBuildData.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: eb6bf626eefd6d3459af21bfc66ddee4
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 87 - 0
Runtime/VoxelBuildingSystem.cs

@@ -0,0 +1,87 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+using KairoEngine.Chunks;
+using KairoEngine.Core;
+using KairoEngine.UI;
+using Utils = KairoEngine.Utility.Utilities;
+using Sirenix.OdinInspector;
+
+namespace KairoEngine.VoxelBuildingSystem
+{
+    [HideMonoScript]
+    public class VoxelBuildingSystem : MonoBehaviour
+    {
+        public static VoxelBuildingSystem instance;
+        public Vector3Int chunkSize = new Vector3Int(16, 16, 16);
+        public Vector3Int chunkCount = new Vector3Int(3, 3, 3);
+        public Vector3 voxelSize = new Vector3(1f, 1f, 1f);
+        public LayerMask layerMask;
+        public List<BuildingTemplate> buildingTemplates = new List<BuildingTemplate>();
+
+        public VoxelBuildingTool tool = VoxelBuildingTool.Build;
+        private ChunkSystem<VoxelBuildData> chunkSystem;
+        [HideInInspector] public Dictionary<Vector3Int, BuildingBehaviour> buildings = new Dictionary<Vector3Int, BuildingBehaviour>();
+        [HideInInspector] public int currentBuildingTemplate;
+
+        private Vector3 pointerPosition;
+
+        public void CreateVoxelSystem()
+        {
+            chunkSystem = new ChunkSystem<VoxelBuildData>(chunkSize, false);
+            for (int x = 0; x < chunkCount.x; x++) {
+                for (int z = 0; z < chunkCount.z; z++) {
+                    for (int y = 0; y < chunkCount.y; y++) {
+                        Vector3Int chunkPos = new Vector3Int(x * chunkSize.x, y * chunkSize.y, z * chunkSize.z);
+                        chunkSystem.CreateChunk(chunkPos, (Chunk<VoxelBuildData> c, Vector3Int pos) => new VoxelBuildData(pos, -1));
+                    }
+                }
+            }
+        }
+
+        void Update()
+        {
+            pointerPosition = Utils.GetMouseWorldPosition(layerMask.value);
+            if(!MouseInputUIBlocker.BlockedByUI)
+            {
+                switch (tool)
+                {
+                    case VoxelBuildingTool.Build:
+                        if(Input.GetKeyDown(KeyCode.R)) RotateBuilding();
+                        if(Input.GetMouseButtonDown(0)) PlaceBuilding(pointerPosition);
+                        break;
+                    case VoxelBuildingTool.Remove:
+                        if(Input.GetMouseButtonDown(0)) RemoveBuilding(pointerPosition);
+                        break;
+                    default:
+                        break;
+                }
+            }
+        }
+
+        public void RotateBuilding() {}
+        
+        public void PlaceBuilding(Vector3 pos) 
+        {
+            Vector3 voxelPos = GetMouseWorldSnappedPosition();
+            Transform newBuilding = Instantiate(buildingTemplates[currentBuildingTemplate].prefab, voxelPos, this.transform.rotation, this.transform);
+        } 
+
+        public void RemoveBuilding(Vector3 pos) {}
+
+        public Vector3Int FindVoxelPosition(Vector3 pos)
+        {
+            return new Vector3Int(Mathf.FloorToInt(pos.x/voxelSize.x), Mathf.FloorToInt(pos.y/voxelSize.y), Mathf.FloorToInt(pos.z/voxelSize.z));
+        }
+
+        public Vector3 GetMouseWorldSnappedPosition() => (Vector3)FindVoxelPosition(pointerPosition);
+        
+    }
+
+    public enum VoxelBuildingTool
+    {
+        None,
+        Build,
+        Remove
+    }
+}

+ 11 - 0
Runtime/VoxelBuildingSystem.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 082a340854e8f6343a98b29e3668a3a7
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: