diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 8527792..7df9600 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -404,7 +404,7 @@ Transform: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 519420028} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: 0, y: 0, z: -10} + m_LocalPosition: {x: 41.65, y: 27.21, z: -10} m_LocalScale: {x: 1, y: 1, z: 1} m_ConstrainProportionsScale: 0 m_Children: [] diff --git a/Assets/Scripts/GridSystem/GridBuildingSystem.cs b/Assets/Scripts/GridSystem/GridBuildingSystem.cs index aeaf4a8..76d3b9f 100644 --- a/Assets/Scripts/GridSystem/GridBuildingSystem.cs +++ b/Assets/Scripts/GridSystem/GridBuildingSystem.cs @@ -15,6 +15,8 @@ public class GridBuildingSystem : MonoBehaviour PlacedObjectTypeSO selectedPlacedObjectTypeSO; Transform selectedGameObjectTransform; + PlacedObject selectedMovingPlacedObject; + // Conveyor Placing Variables Vector3 conveyorStartPosition; List placingConveyorBlueprints = new List(); @@ -73,6 +75,11 @@ public class GridBuildingSystem : MonoBehaviour UpdateSelectedConveyor(); } } + + if (selectedMovingPlacedObject != null) + { + UpdateSelectedMovingPlacedObject(); + } } bool CanBuild(List gridPositionList) @@ -203,8 +210,6 @@ public class GridBuildingSystem : MonoBehaviour Debug.Log(pathPoints.Count); foreach (Vector2Int position in pathPoints) { - Debug.Log("TEST"); - Debug.Log(position); GameObject conveyorBlueprint = Instantiate(selectedPlacedObjectTypeSO.prefab.gameObject, new Vector3(position.x, position.y), Quaternion.identity); placingConveyorBlueprints.Add(conveyorBlueprint); } @@ -247,7 +252,105 @@ public class GridBuildingSystem : MonoBehaviour } #endregion + #region MovingPlacedObject + + public bool MovingObjectIsOnNewPosition(Vector3 mousePosition) + { + buildingGrid.GetXY(mousePosition, out int x, out int y); + + if (selectedMovingPlacedObject.origin.x != x || selectedMovingPlacedObject.origin.y != y) + { + Debug.Log("Moving object is on new position"); + return true; + } + else + { + Debug.Log("Moving object is on same position"); + return false; + } + } + + public void SelectMovingPlacedObject(Vector3 fromPosition) + { + Debug.Log("Selecting moving placed object"); + buildingGrid.GetXY(fromPosition, out int fromX, out int fromY); + + if (buildingGrid.GetGridObject(fromX, fromY).GetPlacedObject() != null) + { + selectedMovingPlacedObject = buildingGrid.GetGridObject(fromX, fromY).GetPlacedObject(); + } + else + { + Debug.Log("No placed object found"); + } + + } + + public void DeselectMovingPlacedObject() + { + Debug.Log("Deselecting moving placed object"); + selectedMovingPlacedObject.gameObject.transform.position = buildingGrid.GetWorldPosition(selectedMovingPlacedObject.origin.x, selectedMovingPlacedObject.origin.y); + selectedMovingPlacedObject = null; + } + + public void UpdateSelectedMovingPlacedObject() + { + + Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); + + buildingGrid.GetXY(mousePosition, out int x, out int y); + + selectedMovingPlacedObject.gameObject.transform.position = buildingGrid.GetWorldPosition(x, y); + + if (Input.GetMouseButtonDown(0) && MenuManager.AllMenusClosed() && MovingObjectIsOnNewPosition(mousePosition)) + { + Debug.Log("Move selected moving placed object"); + MoveSelectedMovingPlacedObject(mousePosition); + DeselectMovingPlacedObject(); + } + else if (Input.GetMouseButtonDown(1) || Input.GetKeyDown(KeyCode.Escape)) + { + DeselectMovingPlacedObject(); + } + } + + public void MoveSelectedMovingPlacedObject(Vector3 position) + { + // Remove old placed object from grid + Debug.Log("Removing old placed object from grid"); + List gridPositionList = selectedMovingPlacedObject.GetGridPositionList(); + + foreach (Vector2Int gridPosition in gridPositionList) + { + buildingGrid.GetGridObject(gridPosition.x, gridPosition.y).ClearPlacedObject(); + Debug.Log("Cleared placed object at " + gridPosition); + } + + // Place new placed object at new position + Debug.Log("Placing new placed object at new position"); + buildingGrid.GetXY(position, out int newX, out int newY); + List newGridPositionList = selectedMovingPlacedObject.placedObjectTypeSO.GetGridPositionList(new Vector2Int(newX, newY)); + + if (CanBuild(newGridPositionList)) + { + selectedMovingPlacedObject.Move(new Vector2Int(newX, newY)); + + foreach (Vector2Int gridPosition in newGridPositionList) + { + buildingGrid.GetGridObject(gridPosition.x, gridPosition.y).SetPlacedObject(selectedMovingPlacedObject); + Debug.Log("Placed placed object at " + gridPosition); + } + } + else + { + Debug.Log("Cannot build here!" + " " + position); + } + } + + #endregion + #region PlacedObjectsTypeSO Methods + public void DemolishPlacedObjectTypeSO(Vector3 position) { if (selectedPlacedObjectTypeSO != null) @@ -269,8 +372,6 @@ public class GridBuildingSystem : MonoBehaviour } } - - public GameObject PlacePlacedObjectTypeSO(Vector3 position, PlacedObjectTypeSO placedObjectTypeSO) { position = new Vector3(position.x, position.y); @@ -278,12 +379,6 @@ public class GridBuildingSystem : MonoBehaviour List gridPositionList = placedObjectTypeSO.GetGridPositionList(new Vector2Int(x, y)); - // DEBUG - foreach (Vector2Int gridPosition in gridPositionList) - { - Debug.Log(gridPosition); - } - if (CanBuild(gridPositionList)) { PlacedObject placedObject = PlacedObject.Create(buildingGrid.GetWorldPosition(x, y), new Vector2Int(x, y), placedObjectTypeSO); diff --git a/Assets/Scripts/GridSystem/PlacedObject.cs b/Assets/Scripts/GridSystem/PlacedObject.cs index 0071413..1ce34f2 100644 --- a/Assets/Scripts/GridSystem/PlacedObject.cs +++ b/Assets/Scripts/GridSystem/PlacedObject.cs @@ -20,14 +20,13 @@ public abstract class PlacedObject : MonoBehaviour { Pathfinding.instance.GetNode(position.x, position.y).SetIsWalkable(true); } - } return placedObject; } public PlacedObjectTypeSO placedObjectTypeSO; - Vector2Int origin; + public Vector2Int origin; [SerializeField] private bool isBlueprint = true; @@ -57,6 +56,30 @@ public abstract class PlacedObject : MonoBehaviour public abstract void OnPlace(); + public void Move(Vector2Int moveToPosition) + { + // Remove old isWalkabe + if (placedObjectTypeSO.isWalkable) + { + foreach (Vector2Int position in GetGridPositionList()) + { + Pathfinding.instance.GetNode(position.x, position.y).SetIsWalkable(false); + } + } + origin = moveToPosition; + gameObject.transform.position = GridBuildingSystem.instance.buildingGrid.GetWorldPosition(moveToPosition.x, moveToPosition.y); + + // add new isWalkable + if (placedObjectTypeSO.isWalkable) + { + foreach (Vector2Int position in GetGridPositionList()) + { + Pathfinding.instance.GetNode(position.x, position.y).SetIsWalkable(true); + } + } + } + + public List GetGridPositionList() { return placedObjectTypeSO.GetGridPositionList(origin); diff --git a/Assets/Scripts/Player/PlayerController.cs b/Assets/Scripts/Player/PlayerController.cs index efbd4b9..263e8c1 100644 --- a/Assets/Scripts/Player/PlayerController.cs +++ b/Assets/Scripts/Player/PlayerController.cs @@ -5,6 +5,7 @@ public class PlayerController : MonoBehaviour Camera cam; bool demolishMode = false; + bool movingMode = false; private void Start() { @@ -13,9 +14,12 @@ public class PlayerController : MonoBehaviour void Update() { + // demolish if (Input.GetButtonDown("Demolish")) { + Debug.Log("Demolish"); demolishMode = !demolishMode; + movingMode = false; } if (demolishMode && Input.GetMouseButton(0) && MenuManager.AllMenusClosed()) @@ -23,5 +27,21 @@ public class PlayerController : MonoBehaviour Vector3 postion = cam.ScreenToWorldPoint(Input.mousePosition); GridBuildingSystem.instance.DemolishPlacedObjectTypeSO(postion); } + + // moving + if (Input.GetButtonDown("Move")) + { + Debug.Log("Move"); + movingMode = !movingMode; + demolishMode = false; + } + + if (movingMode && Input.GetMouseButtonDown(0) && MenuManager.AllMenusClosed()) + { + Debug.Log("Moving this position"); + Vector3 postion = cam.ScreenToWorldPoint(Input.mousePosition); + GridBuildingSystem.instance.SelectMovingPlacedObject(postion); + movingMode = false; + } } } diff --git a/ProjectSettings/InputManager.asset b/ProjectSettings/InputManager.asset index 73a4ada..b06c492 100644 --- a/ProjectSettings/InputManager.asset +++ b/ProjectSettings/InputManager.asset @@ -86,11 +86,11 @@ InputManager: axis: 0 joyNum: 0 - serializedVersion: 3 - m_Name: Jump + m_Name: Demolish descriptiveName: descriptiveNegativeName: negativeButton: - positiveButton: space + positiveButton: v altNegativeButton: altPositiveButton: gravity: 1000 @@ -182,11 +182,11 @@ InputManager: axis: 1 joyNum: 0 - serializedVersion: 3 - m_Name: Fire1 + m_Name: Move descriptiveName: descriptiveNegativeName: negativeButton: - positiveButton: joystick button 0 + positiveButton: m altNegativeButton: altPositiveButton: gravity: 1000