mirror of
				https://github.com/DerTyp7/industrialize-unity.git
				synced 2025-10-31 05:17:10 +01:00 
			
		
		
		
	conveyorPO
This commit is contained in:
		| @@ -1,4 +1,5 @@ | ||||
| using System; | ||||
| using System.Collections.Generic; | ||||
| using UnityEngine; | ||||
|  | ||||
| public class Grid<TGridObject> | ||||
| @@ -47,6 +48,32 @@ public class Grid<TGridObject> | ||||
|  | ||||
|     } | ||||
|  | ||||
|     public List<TGridObject> GetGridObjectsAround(Vector3 position) | ||||
|     { | ||||
|         List<TGridObject> gridObjects = new List<TGridObject>(); | ||||
|  | ||||
|         GetXY(position, out int x, out int y); | ||||
|  | ||||
|         if (x - 1 >= 0) | ||||
|         { | ||||
|             gridObjects.Add(gridArray[x - 1, y]); | ||||
|         } | ||||
|         if (x + 1 < width) | ||||
|         { | ||||
|             gridObjects.Add(gridArray[x + 1, y]); | ||||
|         } | ||||
|         if (y - 1 >= 0) | ||||
|         { | ||||
|             gridObjects.Add(gridArray[x, y - 1]); | ||||
|         } | ||||
|         if (y + 1 < height) | ||||
|         { | ||||
|             gridObjects.Add(gridArray[x, y + 1]); | ||||
|         } | ||||
|  | ||||
|         return gridObjects; | ||||
|     } | ||||
|  | ||||
|     public Vector3 GetWorldPosition(int x, int y) | ||||
|     { | ||||
|         return new Vector3(x, y) * cellSize + originPosition; | ||||
|   | ||||
| @@ -11,8 +11,9 @@ public abstract class PlacedObject : MonoBehaviour | ||||
|         placedObject.placedObjectTypeSO = placedObjectTypeSO; | ||||
|         placedObject.origin = origin; | ||||
|  | ||||
|         placedObject.OnPlace(); | ||||
|  | ||||
|         placedObject.SetIsBlueprint(false); | ||||
|         placedObject.OnPlace(); | ||||
|  | ||||
|         if (placedObjectTypeSO.isWalkable) | ||||
|         { | ||||
|   | ||||
							
								
								
									
										17
									
								
								Assets/Scripts/Items/ItemManager.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								Assets/Scripts/Items/ItemManager.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,17 @@ | ||||
| using UnityEngine; | ||||
|  | ||||
| public class ItemManager : MonoBehaviour | ||||
| { | ||||
|     public static ItemManager instance; | ||||
|  | ||||
|     private void Awake() | ||||
|     { | ||||
|         if (instance == null) | ||||
|         { | ||||
|             instance = this; | ||||
|         } | ||||
|     } | ||||
|  | ||||
|  | ||||
|  | ||||
| } | ||||
							
								
								
									
										11
									
								
								Assets/Scripts/Items/ItemManager.cs.meta
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								Assets/Scripts/Items/ItemManager.cs.meta
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,11 @@ | ||||
| fileFormatVersion: 2 | ||||
| guid: 27ab07ac74967d54c86450327d86934a | ||||
| MonoImporter: | ||||
|   externalObjects: {} | ||||
|   serializedVersion: 2 | ||||
|   defaultReferences: [] | ||||
|   executionOrder: 0 | ||||
|   icon: {instanceID: 0} | ||||
|   userData:  | ||||
|   assetBundleName:  | ||||
|   assetBundleVariant:  | ||||
| @@ -1,7 +1,63 @@ | ||||
| using UnityEngine; | ||||
|  | ||||
| public class ItemObject : ScriptableObject | ||||
| public abstract class ItemObject : MonoBehaviour | ||||
| { | ||||
|     public int maxStackSize = 5; | ||||
|     public int stackSize = 1; | ||||
|     public ItemSO itemSO; | ||||
|     public float timeAlive = 0.0f; | ||||
|     public float maxTimeAlive = 60.0f; | ||||
|     public abstract void OnSpawn(); | ||||
|  | ||||
|     private Rigidbody2D rb; | ||||
|  | ||||
|     void Awake() | ||||
|     { | ||||
|         rb = GetComponent<Rigidbody2D>(); | ||||
|     } | ||||
|  | ||||
|     public void Despawn() | ||||
|     { | ||||
|         Destroy(gameObject); | ||||
|     } | ||||
|  | ||||
|     private void OnCollisionEnter2D(Collision2D collision) | ||||
|     { | ||||
|         if (collision.gameObject.GetComponent<ItemObject>() != null) | ||||
|         { | ||||
|             ItemObject collisionItemObject = collision.gameObject.GetComponent<ItemObject>(); | ||||
|             int addedStackSize = stackSize + collisionItemObject.stackSize; | ||||
|  | ||||
|             if (collisionItemObject.timeAlive < timeAlive) // The other item stacks to me | ||||
|             { | ||||
|                 if (addedStackSize <= maxStackSize) | ||||
|                 { | ||||
|                     stackSize = addedStackSize; | ||||
|                     collisionItemObject.Despawn(); | ||||
|                 } | ||||
|                 else | ||||
|                 { | ||||
|                     stackSize = maxStackSize; | ||||
|                     collisionItemObject.stackSize = addedStackSize - maxStackSize; | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     void Update() | ||||
|     { | ||||
|         timeAlive += Time.deltaTime; | ||||
|         if (timeAlive > maxTimeAlive) | ||||
|         { | ||||
|             Despawn(); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     void FixedUpdate() | ||||
|     { | ||||
|         float speed = 0.3f; // TODO Get speed from ConveyorBelt | ||||
|         Vector2 direction = new Vector2(1f, 0f); // TODO Get direction from ConveyorBelt | ||||
|  | ||||
|         rb.MovePosition(rb.position + (direction * speed) * Time.fixedDeltaTime); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -4,13 +4,22 @@ using UnityEngine; | ||||
| public class ItemSO : ScriptableObject | ||||
| { | ||||
|     public string id; | ||||
|     public string name; | ||||
|     public string itemName; | ||||
|     public GameObject prefab; | ||||
|     public Sprite icon; | ||||
|  | ||||
|     public void Spawn(Vector3 position) | ||||
|     public GameObject Spawn(Vector3 position) | ||||
|     { | ||||
|         GameObject item = Instantiate(prefab, position, Quaternion.identity); | ||||
|         // Snap to grid | ||||
|  | ||||
|         GridBuildingSystem.instance.buildingGrid.GetXY(position, out int x, out int y); | ||||
|  | ||||
|         position = GridBuildingSystem.instance.buildingGrid.GetWorldPosition(x, y); | ||||
|  | ||||
|         GameObject item = Instantiate(prefab, new Vector3(position.x, position.y, 0), Quaternion.identity); | ||||
|         item.name = name; | ||||
|         item.GetComponent<ItemObject>().itemSO = this; | ||||
|  | ||||
|         return item; | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -39,6 +39,13 @@ public class PlayerController : MonoBehaviour | ||||
|             GridBuildingSystem.instance.SelectMovingPlacedObject(postion); | ||||
|             movingMode = false; | ||||
|         } | ||||
|  | ||||
|         //! DEBUG SPAWNING ITEMS | ||||
|         if (Input.GetKeyDown(KeyCode.Y)) | ||||
|         { | ||||
|             Vector3 postion = cam.ScreenToWorldPoint(Input.mousePosition); | ||||
|             ItemDictionary.instance.entries[0].Spawn(postion); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     public void ToggleDemolishMode() | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Janis
					Janis