mirror of
https://github.com/DerTyp7/grow-ai-unity.git
synced 2025-10-30 04:47:10 +01:00
idk
This commit is contained in:
@@ -6,11 +6,12 @@ using UnityEngine;
|
||||
public class GridBuildingSystem : MonoBehaviour
|
||||
{
|
||||
public static GridBuildingSystem instance;
|
||||
[SerializeField] List<PlacedObjectTypeSO> placedObjectTypeSOList = new List<PlacedObjectTypeSO>(); // https://youtu.be/dulosHPl82A?t=1192
|
||||
PlacedObjectTypeSO placedObjectTypeSO;
|
||||
|
||||
public Grid<GridObject> buildingGrid;
|
||||
void Awake()
|
||||
|
||||
PlacedObjectTypeSO selectedPlacedObjectTypeSO;
|
||||
Transform selectedGameObjectTransform;
|
||||
|
||||
void Start()
|
||||
{
|
||||
if (instance == null)
|
||||
instance = this;
|
||||
@@ -20,8 +21,38 @@ public class GridBuildingSystem : MonoBehaviour
|
||||
float cellSize = GridInfo.instance.cellSize;
|
||||
|
||||
buildingGrid = new Grid<GridObject>(gridWidth, gridHeight, cellSize, Vector3.zero, (Grid<GridObject> g, int x, int y) => new GridObject(g, x, y));
|
||||
placedObjectTypeSO = placedObjectTypeSOList[0];
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if(selectedGameObjectTransform != null)
|
||||
{
|
||||
Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
|
||||
|
||||
buildingGrid.GetXY(mousePosition, out int x, out int y);
|
||||
|
||||
selectedGameObjectTransform.position = buildingGrid.GetWorldPosition(x, y);
|
||||
|
||||
List<Vector2Int> gridPositionList = selectedPlacedObjectTypeSO.GetGridPositionList(new Vector2Int(x, y), PlacedObjectTypeSO.Dir.Down);
|
||||
if (!CanBuild(gridPositionList))
|
||||
{
|
||||
selectedGameObjectTransform.gameObject.GetComponent<SpriteRenderer>().color = Color.red;
|
||||
}
|
||||
else
|
||||
{
|
||||
selectedGameObjectTransform.gameObject.GetComponent<SpriteRenderer>().color = Color.white;
|
||||
}
|
||||
|
||||
if (Input.GetMouseButtonDown(0))
|
||||
{
|
||||
PlaceBuilding();
|
||||
}else if (Input.GetMouseButtonDown(1) || Input.GetKeyDown(KeyCode.Escape))
|
||||
{
|
||||
DeselectBuilding();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class GridObject
|
||||
{
|
||||
int x, y;
|
||||
@@ -42,59 +73,79 @@ public class GridBuildingSystem : MonoBehaviour
|
||||
return placedObject == null;
|
||||
}
|
||||
}
|
||||
void Update()
|
||||
|
||||
public void DemolishBuilding(Vector3 position)
|
||||
{
|
||||
// PLACE
|
||||
if (Input.GetKeyDown(KeyCode.B))
|
||||
GridObject gridObject = buildingGrid.GetGridObject(position); // Camera.main.ScreenToWorldPoint(Input.mousePosition)
|
||||
PlacedObject placedObject = gridObject.GetPlacedObject();
|
||||
|
||||
if (placedObject != null)
|
||||
{
|
||||
Vector3 mousePosition = new Vector3(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
|
||||
buildingGrid.GetXY(mousePosition, out int x, out int y);
|
||||
placedObject.DestroySelf();
|
||||
|
||||
List<Vector2Int> gridPositionList = placedObjectTypeSO.GetGridPositionList(new Vector2Int(x, y), PlacedObjectTypeSO.Dir.Down);
|
||||
List<Vector2Int> gridPositionList = placedObject.GetGridPositionList();
|
||||
|
||||
bool canBuild = true;
|
||||
foreach (Vector2Int gridPosition in gridPositionList)
|
||||
{
|
||||
if(!buildingGrid.GetGridObject(gridPosition.x, gridPosition.y).CanBuild())
|
||||
{
|
||||
// Cannot build here
|
||||
canBuild = false;
|
||||
break;
|
||||
}
|
||||
buildingGrid.GetGridObject(gridPosition.x, gridPosition.y).ClearPlacedObject();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (canBuild)
|
||||
{
|
||||
PlacedObject placedObject = PlacedObject.Create(buildingGrid.GetWorldPosition(x, y), new Vector2Int(x, y), PlacedObjectTypeSO.Dir.Down, placedObjectTypeSO);
|
||||
|
||||
|
||||
foreach(Vector2Int gridPosition in gridPositionList)
|
||||
{
|
||||
buildingGrid.GetGridObject(gridPosition.x, gridPosition.y).SetPlacedObject(placedObject);
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log("Cannot build here!" + " " + mousePosition);
|
||||
}
|
||||
|
||||
}else if (Input.GetKeyDown(KeyCode.N)) // DEMOLISH
|
||||
bool CanBuild(List<Vector2Int> gridPositionList)
|
||||
{
|
||||
bool canBuild = true;
|
||||
foreach (Vector2Int gridPosition in gridPositionList)
|
||||
{
|
||||
GridObject gridObject = buildingGrid.GetGridObject(Camera.main.ScreenToWorldPoint(Input.mousePosition));
|
||||
PlacedObject placedObject = gridObject.GetPlacedObject();
|
||||
if (placedObject != null)
|
||||
if (!buildingGrid.GetGridObject(gridPosition.x, gridPosition.y).CanBuild())
|
||||
{
|
||||
placedObject.DestroySelf();
|
||||
// Cannot build here
|
||||
canBuild = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return canBuild;
|
||||
}
|
||||
|
||||
List<Vector2Int> gridPositionList = placedObject.GetGridPositionList();
|
||||
public void PlaceBuilding()
|
||||
{
|
||||
Vector3 position = selectedGameObjectTransform.position;
|
||||
|
||||
foreach (Vector2Int gridPosition in gridPositionList)
|
||||
{
|
||||
buildingGrid.GetGridObject(gridPosition.x, gridPosition.y).ClearPlacedObject();
|
||||
}
|
||||
Vector3 mousePosition = new Vector3(position.x, position.y);
|
||||
buildingGrid.GetXY(mousePosition, out int x, out int y);
|
||||
|
||||
List<Vector2Int> gridPositionList = selectedPlacedObjectTypeSO.GetGridPositionList(new Vector2Int(x, y), PlacedObjectTypeSO.Dir.Down);
|
||||
|
||||
|
||||
|
||||
if (CanBuild(gridPositionList))
|
||||
{
|
||||
PlacedObject placedObject = PlacedObject.Create(buildingGrid.GetWorldPosition(x, y), new Vector2Int(x, y), PlacedObjectTypeSO.Dir.Down, selectedPlacedObjectTypeSO);
|
||||
|
||||
foreach (Vector2Int gridPosition in gridPositionList)
|
||||
{
|
||||
buildingGrid.GetGridObject(gridPosition.x, gridPosition.y).SetPlacedObject(placedObject);
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log("Cannot build here!" + " " + mousePosition);
|
||||
}
|
||||
}
|
||||
|
||||
public void SelectBuilding(PlacedObjectTypeSO placedObjectTypeSO)
|
||||
{
|
||||
Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
|
||||
selectedGameObjectTransform = Instantiate(placedObjectTypeSO.prefab, mousePosition, Quaternion.identity);
|
||||
selectedPlacedObjectTypeSO = placedObjectTypeSO;
|
||||
}
|
||||
|
||||
public void DeselectBuilding()
|
||||
{
|
||||
Destroy(selectedGameObjectTransform.gameObject);
|
||||
selectedPlacedObjectTypeSO = null;
|
||||
selectedGameObjectTransform = null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -31,6 +31,7 @@ public class PlacedObjectTypeSO : ScriptableObject
|
||||
public int width;
|
||||
public int height;
|
||||
public bool isWalkable = false;
|
||||
public Sprite iconSprite;
|
||||
|
||||
public int GetRotationAngle(Dir dir)
|
||||
{
|
||||
|
||||
@@ -34,48 +34,51 @@ public class Person : MonoBehaviour
|
||||
|
||||
void Awake()
|
||||
{
|
||||
city.AddCitizen(this);
|
||||
|
||||
}
|
||||
|
||||
void Start()
|
||||
void Start ()
|
||||
{
|
||||
foreach (Workplace w in city.GetWorkplaces())
|
||||
{
|
||||
if(workplace == null)
|
||||
{
|
||||
if (w.AddWorker(this))
|
||||
{
|
||||
workplace = w;
|
||||
Debug.Log("Workplace added to " + GetFullName());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
city.AddCitizen(this);
|
||||
house.AddPerson(this);
|
||||
workplace.AddWorker(this);
|
||||
/* foreach (Workplace w in city.GetWorkplaces())
|
||||
{
|
||||
if(workplace == null)
|
||||
{
|
||||
if (w.AddWorker(this))
|
||||
{
|
||||
workplace = w;
|
||||
Debug.Log("Workplace added to " + GetFullName());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
foreach (House h in city.GetHouses())
|
||||
{
|
||||
if(house == null)
|
||||
{
|
||||
if (h.AddPerson(this))
|
||||
{
|
||||
house = h;
|
||||
Debug.Log("House added to " + GetFullName());
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
foreach (House h in city.GetHouses())
|
||||
{
|
||||
if(house == null)
|
||||
{
|
||||
if (h.AddPerson(this))
|
||||
{
|
||||
house = h;
|
||||
Debug.Log("House added to " + GetFullName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
movement = GetComponent<PersonMovement>();
|
||||
indicators = GetComponent<PersonIndicators>();
|
||||
|
||||
/* TimeManager.OnMinuteUpdate += OnMinuteUpdate;
|
||||
TimeManager.OnMinuteUpdate += OnMinuteUpdate;
|
||||
TimeManager.OnDayUpdate += OnDayUpdate;
|
||||
|
||||
SetBehaivorDateTimes();
|
||||
Sleep();*/
|
||||
Sleep();
|
||||
}
|
||||
/*
|
||||
|
||||
void OnDayUpdate()
|
||||
{
|
||||
SetBehaivorDateTimes();
|
||||
@@ -115,25 +118,26 @@ public class Person : MonoBehaviour
|
||||
{
|
||||
status = PersonStatus.STORE;
|
||||
Debug.Log(city.GetStores().Count - 1);
|
||||
movement.SetTarget(city.GetStores()[Random.Range(0, city.GetStores().Count-1)].transform);
|
||||
movement.SetTarget(city.GetStores()[Random.Range(0, city.GetStores().Count-1)].transform.position);
|
||||
}
|
||||
else if(status != PersonStatus.PARK)
|
||||
{
|
||||
status = PersonStatus.PARK;// Check if any Object exsits
|
||||
movement.SetTarget(city.GetFreeTimeObjects()[Random.Range(0, city.GetFreeTimeObjects().Count)].transform);
|
||||
movement.SetTarget(city.GetFreeTimeObjects()[Random.Range(0, city.GetFreeTimeObjects().Count)].transform.position);
|
||||
}
|
||||
}
|
||||
|
||||
void Work()
|
||||
{
|
||||
status = PersonStatus.WORK;
|
||||
movement.SetTarget(workplace.transform);
|
||||
movement.SetTarget(workplace.transform.position);
|
||||
}
|
||||
|
||||
void Sleep()
|
||||
{
|
||||
status = PersonStatus.SLEEP;
|
||||
movement.SetTarget(house.transform);
|
||||
Debug.Log(house.transform.position);
|
||||
movement.SetTarget(house.transform.position);
|
||||
}
|
||||
|
||||
void SetBehaivorDateTimes()
|
||||
@@ -146,5 +150,5 @@ public class Person : MonoBehaviour
|
||||
Random.Range(0, 59),
|
||||
currentDateTime.Second);
|
||||
|
||||
}*/
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,14 +8,19 @@ public class PersonMovement : MonoBehaviour
|
||||
private int currentPathIndex;
|
||||
[SerializeField] private List<Vector3> pathVectorList = new List<Vector3>();
|
||||
private const float speed = 40f;
|
||||
Rigidbody2D rigidbody;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
rigidbody = GetComponent<Rigidbody2D>();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
void Update()
|
||||
{
|
||||
HandleMovementList();
|
||||
|
||||
/*
|
||||
//Debug
|
||||
if (Input.GetMouseButtonDown(0))
|
||||
{
|
||||
Pathfinding pathfinding = PathfindingSystem.instance.pathfinding;
|
||||
@@ -35,16 +40,22 @@ public class PersonMovement : MonoBehaviour
|
||||
}
|
||||
|
||||
SetTarget(mouseWorldPosition);
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
||||
void HandleMovementList()
|
||||
{
|
||||
if(pathVectorList.Count > 0)
|
||||
{
|
||||
if(GetPosition() == pathVectorList[0])
|
||||
{
|
||||
|
||||
if(pathVectorList != null && pathVectorList.Count > 0)
|
||||
{
|
||||
// Move our position a step closer to the target.
|
||||
float step = speed * Time.deltaTime; // calculate distance to move
|
||||
transform.position = Vector3.MoveTowards(transform.position, pathVectorList[0], step);
|
||||
|
||||
// Check if the position of the cube and sphere are approximately equal.
|
||||
if (Vector3.Distance(transform.position, pathVectorList[0]) < 0.001f)
|
||||
{
|
||||
pathVectorList.RemoveAt(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -55,7 +66,6 @@ public class PersonMovement : MonoBehaviour
|
||||
}
|
||||
public void SetTarget(Vector3 targetTransform)
|
||||
{
|
||||
pathVectorList = Pathfinding.Instance.FindPath(GetPosition(), targetTransform);
|
||||
Debug.Log(pathVectorList);
|
||||
pathVectorList = Pathfinding.Instance.FindPath(GetPosition(), targetTransform);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user