This commit is contained in:
Janis
2022-05-31 20:18:52 +02:00
parent 4ac45a1d9d
commit 87d77d2867
5 changed files with 155 additions and 17 deletions

View File

@@ -404,7 +404,7 @@ Transform:
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 519420028} m_GameObject: {fileID: 519420028}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} 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_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0 m_ConstrainProportionsScale: 0
m_Children: [] m_Children: []

View File

@@ -15,6 +15,8 @@ public class GridBuildingSystem : MonoBehaviour
PlacedObjectTypeSO selectedPlacedObjectTypeSO; PlacedObjectTypeSO selectedPlacedObjectTypeSO;
Transform selectedGameObjectTransform; Transform selectedGameObjectTransform;
PlacedObject selectedMovingPlacedObject;
// Conveyor Placing Variables // Conveyor Placing Variables
Vector3 conveyorStartPosition; Vector3 conveyorStartPosition;
List<GameObject> placingConveyorBlueprints = new List<GameObject>(); List<GameObject> placingConveyorBlueprints = new List<GameObject>();
@@ -73,6 +75,11 @@ public class GridBuildingSystem : MonoBehaviour
UpdateSelectedConveyor(); UpdateSelectedConveyor();
} }
} }
if (selectedMovingPlacedObject != null)
{
UpdateSelectedMovingPlacedObject();
}
} }
bool CanBuild(List<Vector2Int> gridPositionList) bool CanBuild(List<Vector2Int> gridPositionList)
@@ -203,8 +210,6 @@ public class GridBuildingSystem : MonoBehaviour
Debug.Log(pathPoints.Count); Debug.Log(pathPoints.Count);
foreach (Vector2Int position in pathPoints) 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); GameObject conveyorBlueprint = Instantiate(selectedPlacedObjectTypeSO.prefab.gameObject, new Vector3(position.x, position.y), Quaternion.identity);
placingConveyorBlueprints.Add(conveyorBlueprint); placingConveyorBlueprints.Add(conveyorBlueprint);
} }
@@ -247,7 +252,105 @@ public class GridBuildingSystem : MonoBehaviour
} }
#endregion #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<Vector2Int> 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<Vector2Int> 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 #region PlacedObjectsTypeSO Methods
public void DemolishPlacedObjectTypeSO(Vector3 position) public void DemolishPlacedObjectTypeSO(Vector3 position)
{ {
if (selectedPlacedObjectTypeSO != null) if (selectedPlacedObjectTypeSO != null)
@@ -269,8 +372,6 @@ public class GridBuildingSystem : MonoBehaviour
} }
} }
public GameObject PlacePlacedObjectTypeSO(Vector3 position, PlacedObjectTypeSO placedObjectTypeSO) public GameObject PlacePlacedObjectTypeSO(Vector3 position, PlacedObjectTypeSO placedObjectTypeSO)
{ {
position = new Vector3(position.x, position.y); position = new Vector3(position.x, position.y);
@@ -278,12 +379,6 @@ public class GridBuildingSystem : MonoBehaviour
List<Vector2Int> gridPositionList = placedObjectTypeSO.GetGridPositionList(new Vector2Int(x, y)); List<Vector2Int> gridPositionList = placedObjectTypeSO.GetGridPositionList(new Vector2Int(x, y));
// DEBUG
foreach (Vector2Int gridPosition in gridPositionList)
{
Debug.Log(gridPosition);
}
if (CanBuild(gridPositionList)) if (CanBuild(gridPositionList))
{ {
PlacedObject placedObject = PlacedObject.Create(buildingGrid.GetWorldPosition(x, y), new Vector2Int(x, y), placedObjectTypeSO); PlacedObject placedObject = PlacedObject.Create(buildingGrid.GetWorldPosition(x, y), new Vector2Int(x, y), placedObjectTypeSO);

View File

@@ -20,14 +20,13 @@ public abstract class PlacedObject : MonoBehaviour
{ {
Pathfinding.instance.GetNode(position.x, position.y).SetIsWalkable(true); Pathfinding.instance.GetNode(position.x, position.y).SetIsWalkable(true);
} }
} }
return placedObject; return placedObject;
} }
public PlacedObjectTypeSO placedObjectTypeSO; public PlacedObjectTypeSO placedObjectTypeSO;
Vector2Int origin; public Vector2Int origin;
[SerializeField] private bool isBlueprint = true; [SerializeField] private bool isBlueprint = true;
@@ -57,6 +56,30 @@ public abstract class PlacedObject : MonoBehaviour
public abstract void OnPlace(); 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<Vector2Int> GetGridPositionList() public List<Vector2Int> GetGridPositionList()
{ {
return placedObjectTypeSO.GetGridPositionList(origin); return placedObjectTypeSO.GetGridPositionList(origin);

View File

@@ -5,6 +5,7 @@ public class PlayerController : MonoBehaviour
Camera cam; Camera cam;
bool demolishMode = false; bool demolishMode = false;
bool movingMode = false;
private void Start() private void Start()
{ {
@@ -13,9 +14,12 @@ public class PlayerController : MonoBehaviour
void Update() void Update()
{ {
// demolish
if (Input.GetButtonDown("Demolish")) if (Input.GetButtonDown("Demolish"))
{ {
Debug.Log("Demolish");
demolishMode = !demolishMode; demolishMode = !demolishMode;
movingMode = false;
} }
if (demolishMode && Input.GetMouseButton(0) && MenuManager.AllMenusClosed()) if (demolishMode && Input.GetMouseButton(0) && MenuManager.AllMenusClosed())
@@ -23,5 +27,21 @@ public class PlayerController : MonoBehaviour
Vector3 postion = cam.ScreenToWorldPoint(Input.mousePosition); Vector3 postion = cam.ScreenToWorldPoint(Input.mousePosition);
GridBuildingSystem.instance.DemolishPlacedObjectTypeSO(postion); 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;
}
} }
} }

View File

@@ -86,11 +86,11 @@ InputManager:
axis: 0 axis: 0
joyNum: 0 joyNum: 0
- serializedVersion: 3 - serializedVersion: 3
m_Name: Jump m_Name: Demolish
descriptiveName: descriptiveName:
descriptiveNegativeName: descriptiveNegativeName:
negativeButton: negativeButton:
positiveButton: space positiveButton: v
altNegativeButton: altNegativeButton:
altPositiveButton: altPositiveButton:
gravity: 1000 gravity: 1000
@@ -182,11 +182,11 @@ InputManager:
axis: 1 axis: 1
joyNum: 0 joyNum: 0
- serializedVersion: 3 - serializedVersion: 3
m_Name: Fire1 m_Name: Move
descriptiveName: descriptiveName:
descriptiveNegativeName: descriptiveNegativeName:
negativeButton: negativeButton:
positiveButton: joystick button 0 positiveButton: m
altNegativeButton: altNegativeButton:
altPositiveButton: altPositiveButton:
gravity: 1000 gravity: 1000