This commit is contained in:
Janis
2022-05-29 16:10:42 +02:00
parent cc60b43815
commit 108ce12ef2
113 changed files with 927 additions and 13477 deletions

View File

@@ -14,20 +14,25 @@ public class GridBuildingSystem : MonoBehaviour
PlacedObjectTypeSO selectedPlacedObjectTypeSO;
Transform selectedGameObjectTransform;
Vector3 conveyorStartPosition;
List<GameObject> placingConveyorBlueprints = new List<GameObject>();
bool isPlacingConveyor = false;
public Color cannotBuildColor; // Maybe put this into every single PlacedObjectTypeSO for better customized use
// Debug
public List<PlacedObjectTypeSO> DEBUG_OBJS = new List<PlacedObjectTypeSO>();
public class GridObject
{
int x, y;
bool isAccessable; // if true, can be placed on -> To limit the building area in the future
Grid<GridObject> grid;
PlacedObject placedObject;
public GridObject(Grid<GridObject> _grid, int _x, int _y, bool _isAccessable = true) // FOR DEBUG TRUE
public GridObject(Grid<GridObject> _grid, int _x, int _y) // FOR DEBUG TRUE
{
grid = _grid;
x = _x;
y = _y;
isAccessable = _isAccessable;
}
public void SetPlacedObject(PlacedObject newPlacedObject)
{
@@ -36,14 +41,13 @@ public class GridBuildingSystem : MonoBehaviour
}
public PlacedObject GetPlacedObject() => placedObject;
public void ClearPlacedObject() => placedObject = null;
public void SetIsAccessable(bool _isAccessable) => isAccessable = _isAccessable;
public void SwitchIsAccessable() => isAccessable = !isAccessable;
public bool IsAccessable() => isAccessable;
public bool CanBuild()
{
return placedObject == null && isAccessable;
return placedObject == null;
}
}
void Awake()
{
if (instance == null)
@@ -60,60 +64,43 @@ public class GridBuildingSystem : MonoBehaviour
{
if (selectedGameObjectTransform != null)
{
UpdateSelectedGameObject();
if (selectedPlacedObjectTypeSO.placedObjectCategory == PlacedObjectTypeSO.PlacedObjectCategory.BUILDING)
{
UpdateSelectedBuilding();
}
if (selectedPlacedObjectTypeSO.placedObjectCategory == PlacedObjectTypeSO.PlacedObjectCategory.CONVEYOR)
{
UpdateSelectedConveyor();
}
}
#region debug
// DEBUG
if (Input.GetKeyDown(KeyCode.Alpha1))
{
SelectBuilding(DEBUG_OBJS[0]);
SelectPlacedObjectTypeSO(DEBUG_OBJS[0]);
}
if (Input.GetKeyDown(KeyCode.Alpha2))
{
SelectBuilding(DEBUG_OBJS[1]);
SelectPlacedObjectTypeSO(DEBUG_OBJS[1]);
}
if (Input.GetKeyDown(KeyCode.Alpha3))
{
SelectBuilding(DEBUG_OBJS[2]);
SelectPlacedObjectTypeSO(DEBUG_OBJS[2]);
}
if (Input.GetKeyDown(KeyCode.Alpha4))
{
SelectBuilding(DEBUG_OBJS[3]);
SelectPlacedObjectTypeSO(DEBUG_OBJS[3]);
}
if (Input.GetKeyDown(KeyCode.Alpha5))
{
SelectBuilding(DEBUG_OBJS[4]);
SelectPlacedObjectTypeSO(DEBUG_OBJS[4]);
}
if (Input.GetKeyDown(KeyCode.Alpha6))
{
SelectBuilding(DEBUG_OBJS[5]);
}
if (Input.GetKeyDown(KeyCode.Alpha7))
{
SelectBuilding(DEBUG_OBJS[6]);
}
if (Input.GetKeyDown(KeyCode.Alpha8))
{
SelectBuilding(DEBUG_OBJS[7]);
}
if (Input.GetKeyDown(KeyCode.Alpha9))
{
SelectBuilding(DEBUG_OBJS[8]);
}
#endregion
}
void UpdateSelectedGameObject()
void UpdateSelectedBuilding()
{
Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
@@ -134,18 +121,128 @@ public class GridBuildingSystem : MonoBehaviour
if (Input.GetMouseButtonDown(0))
{
PlaceBuilding(selectedGameObjectTransform.position);
PlacePlacedObjectTypeSO(selectedGameObjectTransform.position, selectedPlacedObjectTypeSO);
}
if (Input.GetMouseButtonDown(1) || Input.GetKeyDown(KeyCode.Escape))
{
DeselectBuilding();
DeselectPlacedObjectTypeSO();
}
}
#region ConveyorPlacing
void UpdateSelectedConveyor()
{
Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
buildingGrid.GetXY(mousePosition, out int x, out int y);
selectedGameObjectTransform.position = buildingGrid.GetWorldPosition(x, y);
if (Input.GetMouseButtonDown(0))
{
if (isPlacingConveyor == false)
{
conveyorStartPosition = buildingGrid.GetWorldPosition(x, y);
isPlacingConveyor = true;
}
else
{
if (ConveyorCanBuild())
{
PlaceConveyorPath();
isPlacingConveyor = false;
}
}
}
DrawConveyorPath();
if (ConveyorCanBuild())
{
RecolorConveyorPath(Color.white);
}
else
{
RecolorConveyorPath(cannotBuildColor);
}
if (Input.GetMouseButtonDown(1) || Input.GetKeyDown(KeyCode.Escape))
{
ClearConveyorPath();
DeselectPlacedObjectTypeSO();
}
}
void PlaceConveyorPath()
{
if (placingConveyorBlueprints.Count > 0)
{
foreach (GameObject blueprint in placingConveyorBlueprints)
{
PlacePlacedObjectTypeSO(blueprint.transform.position, blueprint.gameObject.GetComponent<PlacedObject>().placedObjectTypeSO);
}
ClearConveyorPath();
}
}
void ClearConveyorPath()
{
if (placingConveyorBlueprints.Count > 0)
{
foreach (GameObject blueprint in placingConveyorBlueprints)
{
Destroy(blueprint);
}
placingConveyorBlueprints.Clear();
}
}
void DrawConveyorPath()
{
if (isPlacingConveyor == true)
{
Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
buildingGrid.GetXY(mousePosition, out int x, out int y);
Vector3 endPosition = buildingGrid.GetWorldPosition(x, y);
ClearConveyorPath();
foreach (Vector3 position in Pathfinding.instance.FindPath(conveyorStartPosition, endPosition, true))
{
buildingGrid.GetXY(position, out x, out y);
GameObject conveyorBlueprint = Instantiate(selectedPlacedObjectTypeSO.prefab.gameObject, new Vector3(x, y), Quaternion.identity);
placingConveyorBlueprints.Add(conveyorBlueprint);
}
}
}
void RecolorConveyorPath(Color color)
{
foreach (GameObject blueprint in placingConveyorBlueprints)
{
blueprint.GetComponent<SpriteRenderer>().color = color;
}
}
bool ConveyorCanBuild()
{
bool canBuild = true;
foreach (GameObject blueprint in placingConveyorBlueprints)
{
buildingGrid.GetXY(blueprint.transform.position, out int x, out int y);
List<Vector2Int> gridPositionList = blueprint.GetComponent<PlacedObject>().placedObjectTypeSO.GetGridPositionList(new Vector2Int(x, y));
if (CanBuild(gridPositionList) == false)
{
canBuild = false;
Debug.Log("Cannot build conveyor here");
}
}
return canBuild;
}
#endregion
public void DemolishBuilding(Vector3 position)
{
GridObject gridObject = buildingGrid.GetGridObject(position); // Camera.main.ScreenToWorldPoint(Input.mousePosition)
@@ -179,12 +276,12 @@ public class GridBuildingSystem : MonoBehaviour
return canBuild;
}
public GameObject PlaceBuilding(Vector3 position)
public GameObject PlacePlacedObjectTypeSO(Vector3 position, PlacedObjectTypeSO placedObjectTypeSO)
{
position = new Vector3(position.x, position.y);
buildingGrid.GetXY(position, out int x, out int y);
List<Vector2Int> gridPositionList = selectedPlacedObjectTypeSO.GetGridPositionList(new Vector2Int(x, y));
List<Vector2Int> gridPositionList = placedObjectTypeSO.GetGridPositionList(new Vector2Int(x, y));
// DEBUG
foreach (Vector2Int gridPosition in gridPositionList)
@@ -194,7 +291,7 @@ public class GridBuildingSystem : MonoBehaviour
if (CanBuild(gridPositionList))
{
PlacedObject placedObject = PlacedObject.Create(buildingGrid.GetWorldPosition(x, y), new Vector2Int(x, y), selectedPlacedObjectTypeSO);
PlacedObject placedObject = PlacedObject.Create(buildingGrid.GetWorldPosition(x, y), new Vector2Int(x, y), placedObjectTypeSO);
foreach (Vector2Int gridPosition in gridPositionList)
{
@@ -209,7 +306,7 @@ public class GridBuildingSystem : MonoBehaviour
return null;
}
public void SelectBuilding(PlacedObjectTypeSO placedObjectTypeSO)
public void SelectPlacedObjectTypeSO(PlacedObjectTypeSO placedObjectTypeSO)
{
// Delete all previous blueprints
@@ -226,12 +323,12 @@ public class GridBuildingSystem : MonoBehaviour
selectedPlacedObjectTypeSO = placedObjectTypeSO;
}
public void DeselectBuilding()
public void DeselectPlacedObjectTypeSO()
{
Destroy(selectedGameObjectTransform.gameObject);
selectedPlacedObjectTypeSO = null;
selectedGameObjectTransform = null;
isPlacingConveyor = false;
placingConveyorBlueprints.Clear();
}
}

View File

@@ -1,7 +1,5 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
public abstract class PlacedObject : MonoBehaviour
{
@@ -16,16 +14,15 @@ public abstract class PlacedObject : MonoBehaviour
placedObject.OnPlace();
placedObject.SetIsBlueprint(false);
if (placedObjectTypeSO.isWalkable)
if (placedObjectTypeSO.isWalkable)
{
foreach(Vector2Int position in placedObject.GetGridPositionList())
foreach (Vector2Int position in placedObject.GetGridPositionList())
{
Pathfinding.Instance.GetNode(position.x, position.y).SetIsWalkable(true);
Pathfinding.instance.GetNode(position.x, position.y).SetIsWalkable(true);
}
}
return placedObject;
}
@@ -49,7 +46,7 @@ public abstract class PlacedObject : MonoBehaviour
GetComponent<Collider2D>().enabled = true;
}
}
isBlueprint = newIsBlueprint;
}
@@ -71,7 +68,7 @@ public abstract class PlacedObject : MonoBehaviour
{
foreach (Vector2Int position in GetGridPositionList())
{
Pathfinding.Instance.GetNode(position.x, position.y).SetIsWalkable(false);
Pathfinding.instance.GetNode(position.x, position.y).SetIsWalkable(false);
}
}
Destroy(gameObject);

View File

@@ -1,10 +1,15 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(menuName = "ScriptableObjects/PlacedObjectTypeSO")]
public class PlacedObjectTypeSO : ScriptableObject
{
public enum PlacedObjectCategory
{
CONVEYOR,
BUILDING
}
public string placedObjectID;
public string nameString;
public Transform prefab;
@@ -12,6 +17,7 @@ public class PlacedObjectTypeSO : ScriptableObject
public int height;
public bool isWalkable = false;
public Sprite iconSprite;
public PlacedObjectCategory placedObjectCategory;
public List<Vector2Int> GetGridPositionList(Vector2Int offset)
{
@@ -25,7 +31,4 @@ public class PlacedObjectTypeSO : ScriptableObject
}
return gridPositionList;
}
}
}

View File

@@ -1,55 +1,65 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Pathfinding {
public class Pathfinding
{
private const int MOVE_STRAIGHT_COST = 10;
private const int MOVE_DIAGONAL_COST = 14;
public static Pathfinding Instance { get; private set; }
public static Pathfinding instance { get; private set; }
private Grid<PathNode> grid;
private List<PathNode> openList;
private List<PathNode> closedList;
public Pathfinding(int width, int height, float cellSize) {
Instance = this;
public Pathfinding(int width, int height, float cellSize)
{
instance = this;
grid = new Grid<PathNode>(width, height, cellSize, Vector3.zero, (Grid<PathNode> g, int x, int y) => new PathNode(g, x, y));
foreach (PathNode node in grid.gridArray) {
foreach (PathNode node in grid.gridArray)
{
node.neighbourList = GetNeighbourList(node);
}
}
public Grid<PathNode> GetGrid() {
public Grid<PathNode> GetGrid()
{
return grid;
}
public List<Vector3> FindPath(Vector3 startWorldPosition, Vector3 endWorldPosition, bool ignoreIsWalkable = false) {
public List<Vector3> FindPath(Vector3 startWorldPosition, Vector3 endWorldPosition, bool ignoreIsWalkable = false)
{
grid.GetXY(startWorldPosition, out int startX, out int startY);
grid.GetXY(endWorldPosition, out int endX, out int endY);
List<PathNode> path = FindPath(startX, startY, endX, endY, ignoreIsWalkable);
if (path == null) {
if (path == null)
{
return null;
} else {
}
else
{
List<Vector3> vectorPath = new List<Vector3>();
foreach (PathNode pathNode in path) {
foreach (PathNode pathNode in path)
{
vectorPath.Add(new Vector3(pathNode.x, pathNode.y) * grid.GetCellSize() + Vector3.one * grid.GetCellSize() * .5f);
}
return vectorPath;
}
}
public List<PathNode> FindPath(int startX, int startY, int endX, int endY, bool ignoreIsWalkable = false) {
public List<PathNode> FindPath(int startX, int startY, int endX, int endY, bool ignoreIsWalkable = false)
{
var timer = new System.Diagnostics.Stopwatch();
timer.Start();
PathNode startNode = grid.GetGridObject(startX, startY);
PathNode endNode = grid.GetGridObject(endX, endY);
if (startNode == null || endNode == null) {
if (startNode == null || endNode == null)
{
// Invalid Path
return null;
}
@@ -57,8 +67,10 @@ public class Pathfinding {
openList = new List<PathNode> { startNode };
closedList = new List<PathNode>();
for (int x = 0; x < grid.GetWidth(); x++) {
for (int y = 0; y < grid.GetHeight(); y++) {
for (int x = 0; x < grid.GetWidth(); x++)
{
for (int y = 0; y < grid.GetHeight(); y++)
{
PathNode pathNode = grid.GetGridObject(x, y);
pathNode.gCost = 99999999;
pathNode.CalculateFCost();
@@ -70,9 +82,11 @@ public class Pathfinding {
startNode.hCost = CalculateDistanceCost(startNode, endNode);
startNode.CalculateFCost();
while (openList.Count > 0) {
while (openList.Count > 0)
{
PathNode currentNode = GetLowestFCostNode(openList);
if (currentNode == endNode) {
if (currentNode == endNode)
{
// Reached final node
timer.Stop();
TimeSpan timeTaken = timer.Elapsed;
@@ -83,21 +97,25 @@ public class Pathfinding {
openList.Remove(currentNode);
closedList.Add(currentNode);
foreach (PathNode neighbourNode in currentNode.neighbourList) {
foreach (PathNode neighbourNode in currentNode.neighbourList)
{
if (closedList.Contains(neighbourNode)) continue;
if (!neighbourNode.isWalkable && !ignoreIsWalkable) { // If neighbouring node is not walkable instantly add them to closed
if (!neighbourNode.isWalkable && !ignoreIsWalkable)
{ // If neighbouring node is not walkable instantly add them to closed
closedList.Add(neighbourNode);
continue;
}
int tentativeGCost = currentNode.gCost + CalculateDistanceCost(currentNode, neighbourNode);
if (tentativeGCost < neighbourNode.gCost) {
if (tentativeGCost < neighbourNode.gCost)
{
neighbourNode.cameFromNode = currentNode;
neighbourNode.gCost = tentativeGCost;
neighbourNode.hCost = CalculateDistanceCost(neighbourNode, endNode);
neighbourNode.CalculateFCost();
if (!openList.Contains(neighbourNode)) {
if (!openList.Contains(neighbourNode))
{
openList.Add(neighbourNode);
}
}
@@ -108,24 +126,27 @@ public class Pathfinding {
return null;
}
private List<PathNode> GetNeighbourList(PathNode currentNode) {
private List<PathNode> GetNeighbourList(PathNode currentNode)
{
List<PathNode> neighbourList = new List<PathNode>();
if (currentNode.x - 1 >= 0) {
if (currentNode.x - 1 >= 0)
{
// Left
neighbourList.Add(GetNode(currentNode.x - 1, currentNode.y));
// Left Down
// if (currentNode.y - 1 >= 0) neighbourList.Add(GetNode(currentNode.x - 1, currentNode.y - 1));
// if (currentNode.y - 1 >= 0) neighbourList.Add(GetNode(currentNode.x - 1, currentNode.y - 1));
// Left Up
// if (currentNode.y + 1 < grid.GetHeight()) neighbourList.Add(GetNode(currentNode.x - 1, currentNode.y + 1));
// if (currentNode.y + 1 < grid.GetHeight()) neighbourList.Add(GetNode(currentNode.x - 1, currentNode.y + 1));
}
if (currentNode.x + 1 < grid.GetWidth()) {
if (currentNode.x + 1 < grid.GetWidth())
{
// Right
neighbourList.Add(GetNode(currentNode.x + 1, currentNode.y));
// Right Down
// if (currentNode.y - 1 >= 0) neighbourList.Add(GetNode(currentNode.x + 1, currentNode.y - 1));
// if (currentNode.y - 1 >= 0) neighbourList.Add(GetNode(currentNode.x + 1, currentNode.y - 1));
// Right Up
// if (currentNode.y + 1 < grid.GetHeight()) neighbourList.Add(GetNode(currentNode.x + 1, currentNode.y + 1));
// if (currentNode.y + 1 < grid.GetHeight()) neighbourList.Add(GetNode(currentNode.x + 1, currentNode.y + 1));
}
// Down
if (currentNode.y - 1 >= 0) neighbourList.Add(GetNode(currentNode.x, currentNode.y - 1));
@@ -135,15 +156,18 @@ public class Pathfinding {
return neighbourList;
}
public PathNode GetNode(int x, int y) {
public PathNode GetNode(int x, int y)
{
return grid.GetGridObject(x, y);
}
private List<PathNode> CalculatePath(PathNode endNode) {
private List<PathNode> CalculatePath(PathNode endNode)
{
List<PathNode> path = new List<PathNode>();
path.Add(endNode);
PathNode currentNode = endNode;
while (currentNode.cameFromNode != null) {
while (currentNode.cameFromNode != null)
{
path.Add(currentNode.cameFromNode);
currentNode = currentNode.cameFromNode;
}
@@ -151,17 +175,21 @@ public class Pathfinding {
return path;
}
private int CalculateDistanceCost(PathNode a, PathNode b) {
private int CalculateDistanceCost(PathNode a, PathNode b)
{
int xDistance = Mathf.Abs(a.x - b.x);
int yDistance = Mathf.Abs(a.y - b.y);
int remaining = Mathf.Abs(xDistance - yDistance);
return MOVE_DIAGONAL_COST * Mathf.Min(xDistance, yDistance) + MOVE_STRAIGHT_COST * remaining;
}
private PathNode GetLowestFCostNode(List<PathNode> pathNodeList) {
private PathNode GetLowestFCostNode(List<PathNode> pathNodeList)
{
PathNode lowestFCostNode = pathNodeList[0];
for (int i = 1; i < pathNodeList.Count; i++) {
if (pathNodeList[i].fCost < lowestFCostNode.fCost) {
for (int i = 1; i < pathNodeList.Count; i++)
{
if (pathNodeList[i].fCost < lowestFCostNode.fCost)
{
lowestFCostNode = pathNodeList[i];
}
}

View File

@@ -4,13 +4,13 @@ public class CameraMovement : MonoBehaviour
{
[Header("Speed")]
[SerializeField]
float speed = 150f;
float speed = 100f;
[SerializeField]
float fastSpeed = 300f;
float fastSpeed = 200f;
[SerializeField]
float slowSpeed = 50f;
float slowSpeed = 30f;
[Header("Zoom")]
[SerializeField]
@@ -30,8 +30,6 @@ public class CameraMovement : MonoBehaviour
Vector3 origin;
Vector3 difference;
Camera cam;
Rigidbody2D rb;