mirror of
https://github.com/DerTyp7/grow-ai-unity.git
synced 2025-10-30 04:47:10 +01:00
a
This commit is contained in:
@@ -9,7 +9,7 @@ public class City : MonoBehaviour
|
||||
string cityName;
|
||||
|
||||
|
||||
[SerializeField] List<Person> citizens = new List<Person>();
|
||||
[SerializeField] List<Person> persons = new List<Person>();
|
||||
[SerializeField] List<House> houses = new List<House>();
|
||||
[SerializeField] List<Company> companies = new List<Company>();
|
||||
[SerializeField] List<Workplace> workplaces = new List<Workplace>();
|
||||
@@ -21,20 +21,20 @@ public class City : MonoBehaviour
|
||||
public List<House> GetHouses() => houses;
|
||||
public List<Workplace> GetWorkplaces() => workplaces;
|
||||
|
||||
public void AddCitizen(Person citizen)
|
||||
public void AddPerson(Person citizen)
|
||||
{
|
||||
if (!citizens.Contains(citizen))
|
||||
if (!persons.Contains(citizen))
|
||||
{
|
||||
citizens.Add(citizen);
|
||||
persons.Add(citizen);
|
||||
Debug.Log(citizen.GetFullName() + " joined " + cityName);
|
||||
}
|
||||
}
|
||||
|
||||
public void RemoveCitizen(Person citizen)
|
||||
public void RemovePerson(Person citizen)
|
||||
{
|
||||
if (citizens.Contains(citizen))
|
||||
if (persons.Contains(citizen))
|
||||
{
|
||||
citizens.Remove(citizen);
|
||||
persons.Remove(citizen);
|
||||
Debug.Log(citizen.GetFullName() + " left " + cityName);
|
||||
}
|
||||
}
|
||||
@@ -57,6 +57,36 @@ public class City : MonoBehaviour
|
||||
}
|
||||
}
|
||||
|
||||
public House GetAvaiableHouse()
|
||||
{
|
||||
List<House> avaiableHouses = new List<House>();
|
||||
foreach (House house in houses)
|
||||
{
|
||||
if(house.GetAvaiableSpace() > 0)
|
||||
avaiableHouses.Add(house);
|
||||
}
|
||||
|
||||
if(avaiableHouses.Count > 0)
|
||||
{
|
||||
return avaiableHouses[Random.Range(0, avaiableHouses.Count - 1)];
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public int GetAvaiableHousingSpace()
|
||||
{
|
||||
int space = 0;
|
||||
foreach(House house in houses)
|
||||
{
|
||||
space += house.GetAvaiableSpace();
|
||||
}
|
||||
return space;
|
||||
}
|
||||
|
||||
public void AddCompany(Company company)
|
||||
{
|
||||
if (!companies.Contains(company))
|
||||
@@ -126,4 +156,23 @@ public class City : MonoBehaviour
|
||||
Debug.Log("workplaces demolished in " + cityName);
|
||||
}
|
||||
}
|
||||
|
||||
public Workplace GetAvaiableWorkplace()
|
||||
{
|
||||
List<Workplace> avaiableWorkplaces = new List<Workplace>();
|
||||
foreach (Workplace workplace in workplaces)
|
||||
{
|
||||
if (workplace.GetAvaiableSpace() > 0)
|
||||
avaiableWorkplaces.Add(workplace);
|
||||
}
|
||||
|
||||
if (avaiableWorkplaces.Count > 0)
|
||||
{
|
||||
return avaiableWorkplaces[Random.Range(0, avaiableWorkplaces.Count - 1)];
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,24 +8,67 @@ public class GridBuildingSystem : MonoBehaviour
|
||||
public static GridBuildingSystem instance;
|
||||
public Grid<GridObject> buildingGrid;
|
||||
|
||||
public int gridWidth;
|
||||
public int gridHeight;
|
||||
public float cellSize;
|
||||
|
||||
PlacedObjectTypeSO selectedPlacedObjectTypeSO;
|
||||
Transform selectedGameObjectTransform;
|
||||
|
||||
void Start()
|
||||
public class GridObject
|
||||
{
|
||||
int x, y;
|
||||
bool isAccessable;
|
||||
Grid<GridObject> grid;
|
||||
PlacedObject placedObject;
|
||||
public GridObject(Grid<GridObject> _grid, int _x, int _y, bool _isAccessable = true) // FOR DEBUG TRUE
|
||||
{
|
||||
grid = _grid;
|
||||
x = _x;
|
||||
y = _y;
|
||||
isAccessable = _isAccessable;
|
||||
}
|
||||
public void SetPlacedObject(PlacedObject _placedObject) => placedObject = _placedObject;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Awake()
|
||||
{
|
||||
if (instance == null)
|
||||
instance = this;
|
||||
|
||||
int gridWidth = GridInfo.instance.gridWidth;
|
||||
int gridHeight = GridInfo.instance.gridHeight;
|
||||
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));
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if(selectedGameObjectTransform != null)
|
||||
UpdateSelectedGameObject();
|
||||
|
||||
// DEBUG
|
||||
if (Input.GetKeyDown(KeyCode.U))
|
||||
{
|
||||
buildingGrid.GetGridObject(Camera.main.ScreenToWorldPoint(Input.mousePosition)).SwitchIsAccessable();
|
||||
}
|
||||
|
||||
if (Input.GetKeyDown(KeyCode.F))
|
||||
{
|
||||
buildingGrid.GetGridObject(Camera.main.ScreenToWorldPoint(Input.mousePosition)).GetPlacedObject();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void UpdateSelectedGameObject()
|
||||
{
|
||||
if (selectedGameObjectTransform != null)
|
||||
{
|
||||
Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
|
||||
|
||||
@@ -33,7 +76,7 @@ public class GridBuildingSystem : MonoBehaviour
|
||||
|
||||
selectedGameObjectTransform.position = buildingGrid.GetWorldPosition(x, y);
|
||||
|
||||
List<Vector2Int> gridPositionList = selectedPlacedObjectTypeSO.GetGridPositionList(new Vector2Int(x, y), PlacedObjectTypeSO.Dir.Down);
|
||||
List<Vector2Int> gridPositionList = selectedPlacedObjectTypeSO.GetGridPositionList(new Vector2Int(x, y));
|
||||
if (!CanBuild(gridPositionList))
|
||||
{
|
||||
selectedGameObjectTransform.gameObject.GetComponent<SpriteRenderer>().color = Color.red;
|
||||
@@ -46,34 +89,13 @@ public class GridBuildingSystem : MonoBehaviour
|
||||
if (Input.GetMouseButtonDown(0))
|
||||
{
|
||||
PlaceBuilding();
|
||||
}else if (Input.GetMouseButtonDown(1) || Input.GetKeyDown(KeyCode.Escape))
|
||||
}
|
||||
else if (Input.GetMouseButtonDown(1) || Input.GetKeyDown(KeyCode.Escape))
|
||||
{
|
||||
DeselectBuilding();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class GridObject
|
||||
{
|
||||
int x, y;
|
||||
Grid<GridObject> grid;
|
||||
PlacedObject placedObject;
|
||||
public GridObject(Grid<GridObject> _grid, int _x, int _y)
|
||||
{
|
||||
grid = _grid;
|
||||
x = _x;
|
||||
y = _y;
|
||||
}
|
||||
public void SetPlacedObject(PlacedObject _placedObject) => placedObject = _placedObject;
|
||||
public PlacedObject GetPlacedObject() => placedObject;
|
||||
public void ClearPlacedObject() => placedObject = null;
|
||||
|
||||
public bool CanBuild()
|
||||
{
|
||||
return placedObject == null;
|
||||
}
|
||||
}
|
||||
|
||||
public void DemolishBuilding(Vector3 position)
|
||||
{
|
||||
GridObject gridObject = buildingGrid.GetGridObject(position); // Camera.main.ScreenToWorldPoint(Input.mousePosition)
|
||||
@@ -114,13 +136,13 @@ public class GridBuildingSystem : MonoBehaviour
|
||||
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);
|
||||
List<Vector2Int> gridPositionList = selectedPlacedObjectTypeSO.GetGridPositionList(new Vector2Int(x, y));
|
||||
|
||||
|
||||
|
||||
if (CanBuild(gridPositionList))
|
||||
{
|
||||
PlacedObject placedObject = PlacedObject.Create(buildingGrid.GetWorldPosition(x, y), new Vector2Int(x, y), PlacedObjectTypeSO.Dir.Down, selectedPlacedObjectTypeSO);
|
||||
PlacedObject placedObject = PlacedObject.Create(buildingGrid.GetWorldPosition(x, y), new Vector2Int(x, y), selectedPlacedObjectTypeSO);
|
||||
|
||||
foreach (Vector2Int gridPosition in gridPositionList)
|
||||
{
|
||||
|
||||
58
Assets/Scripts/Grid/PathfindingSystem.cs
Normal file
58
Assets/Scripts/Grid/PathfindingSystem.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class PathfindingSystem : MonoBehaviour
|
||||
{
|
||||
public static PathfindingSystem instance { get; private set; }
|
||||
public Pathfinding pathfinding;
|
||||
|
||||
|
||||
|
||||
void Start()
|
||||
{
|
||||
instance = this;
|
||||
|
||||
int gridWidth = GridBuildingSystem.instance.gridWidth;
|
||||
int gridHeight = GridBuildingSystem.instance.gridHeight;
|
||||
float cellSize = GridBuildingSystem.instance.cellSize;
|
||||
|
||||
pathfinding = new Pathfinding(gridWidth, gridHeight, cellSize);
|
||||
}
|
||||
|
||||
void Update()
|
||||
{/*
|
||||
if (Input.GetMouseButtonDown(0))
|
||||
{
|
||||
Vector3 mouseWorldPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
|
||||
pathfinding.GetGrid().GetXY(mouseWorldPosition, out int x, out int y);
|
||||
List<PathNode> path = pathfinding.FindPath(originX, originY, x, y);
|
||||
if (path != null)
|
||||
{
|
||||
float cellSize = pathfinding.GetGrid().GetCellSize();
|
||||
for (int i = 0; i < path.Count - 1; i++)
|
||||
{
|
||||
Debug.DrawLine(new Vector3(path[i].x, path[i].y) * cellSize + Vector3.one * cellSize / 2, new Vector3(path[i + 1].x, path[i + 1].y) * cellSize + Vector3.one * cellSize / 2, Color.green, 5f);
|
||||
}
|
||||
}
|
||||
|
||||
//characterPathfinding.SetTargetPosition(mouseWorldPosition);
|
||||
}
|
||||
|
||||
if (Input.GetMouseButtonDown(1))
|
||||
{
|
||||
Vector3 mouseWorldPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
|
||||
pathfinding.GetGrid().GetXY(mouseWorldPosition, out int x, out int y);
|
||||
pathfinding.GetNode(x, y).SetIsWalkable(!pathfinding.GetNode(x, y).isWalkable);
|
||||
originX = x;
|
||||
originY = y;
|
||||
}
|
||||
|
||||
if (Input.GetMouseButtonDown(2))
|
||||
{
|
||||
Vector3 mouseWorldPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
|
||||
pathfinding.GetGrid().GetXY(mouseWorldPosition, out int x, out int y);
|
||||
pathfinding.GetNode(x, y).SetIsWalkable(!pathfinding.GetNode(x, y).isWalkable);
|
||||
}*/
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Grid/PathfindingSystem.cs.meta
Normal file
11
Assets/Scripts/Grid/PathfindingSystem.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: db134add0ff65f04aa54ed5edf6d6df2
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -3,16 +3,17 @@ using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Tilemaps;
|
||||
|
||||
public class PlacedObject : MonoBehaviour
|
||||
public abstract class PlacedObject : MonoBehaviour
|
||||
{
|
||||
public static PlacedObject Create(Vector3 worldPosition, Vector2Int origin, PlacedObjectTypeSO.Dir dir, PlacedObjectTypeSO placedObjectTypeSO)
|
||||
public static PlacedObject Create(Vector3 worldPosition, Vector2Int origin, PlacedObjectTypeSO placedObjectTypeSO)
|
||||
{
|
||||
Transform placeObjectTransform = Instantiate(placedObjectTypeSO.prefab, worldPosition, Quaternion.identity);
|
||||
|
||||
PlacedObject placedObject = placeObjectTransform.GetComponent<PlacedObject>();
|
||||
placedObject.placedObjectTypeSO = placedObjectTypeSO;
|
||||
placedObject.origin = origin;
|
||||
placedObject.dir = dir;
|
||||
|
||||
placedObject.OnPlace();
|
||||
|
||||
if (placedObjectTypeSO.isWalkable)
|
||||
{
|
||||
@@ -28,11 +29,12 @@ public class PlacedObject : MonoBehaviour
|
||||
|
||||
PlacedObjectTypeSO placedObjectTypeSO;
|
||||
Vector2Int origin;
|
||||
PlacedObjectTypeSO.Dir dir;
|
||||
|
||||
public abstract void OnPlace();
|
||||
|
||||
public List<Vector2Int> GetGridPositionList()
|
||||
{
|
||||
return placedObjectTypeSO.GetGridPositionList(origin, PlacedObjectTypeSO.Dir.Down);
|
||||
return placedObjectTypeSO.GetGridPositionList(origin);
|
||||
}
|
||||
|
||||
public void DestroySelf()
|
||||
|
||||
@@ -6,25 +6,6 @@ using UnityEngine;
|
||||
public class PlacedObjectTypeSO : ScriptableObject
|
||||
{
|
||||
|
||||
public static Dir GetNextDir(Dir dir)
|
||||
{
|
||||
switch (dir)
|
||||
{
|
||||
default:
|
||||
case Dir.Down: return Dir.Left;
|
||||
case Dir.Left: return Dir.Up;
|
||||
case Dir.Up: return Dir.Right;
|
||||
case Dir.Right: return Dir.Down;
|
||||
}
|
||||
}
|
||||
public enum Dir
|
||||
{
|
||||
Down,
|
||||
Left,
|
||||
Up,
|
||||
Right,
|
||||
}
|
||||
|
||||
public string nameString;
|
||||
public Transform prefab;
|
||||
//public Transform visual;
|
||||
@@ -33,56 +14,16 @@ public class PlacedObjectTypeSO : ScriptableObject
|
||||
public bool isWalkable = false;
|
||||
public Sprite iconSprite;
|
||||
|
||||
public int GetRotationAngle(Dir dir)
|
||||
{
|
||||
switch (dir)
|
||||
{
|
||||
default:
|
||||
case Dir.Down: return 0;
|
||||
case Dir.Left: return 90;
|
||||
case Dir.Up: return 180;
|
||||
case Dir.Right: return 270;
|
||||
}
|
||||
}
|
||||
|
||||
public Vector2Int GetRotationOffset(Dir dir)
|
||||
{
|
||||
switch (dir)
|
||||
{
|
||||
default:
|
||||
case Dir.Down: return new Vector2Int(0, 0);
|
||||
case Dir.Left: return new Vector2Int(0, width);
|
||||
case Dir.Up: return new Vector2Int(width, height);
|
||||
case Dir.Right: return new Vector2Int(height, 0);
|
||||
}
|
||||
}
|
||||
|
||||
public List<Vector2Int> GetGridPositionList(Vector2Int offset, Dir dir)
|
||||
public List<Vector2Int> GetGridPositionList(Vector2Int offset)
|
||||
{
|
||||
List<Vector2Int> gridPositionList = new List<Vector2Int>();
|
||||
switch (dir)
|
||||
for (int x = 0; x < width; x++)
|
||||
{
|
||||
default:
|
||||
case Dir.Down:
|
||||
case Dir.Up:
|
||||
for (int x = 0; x < width; x++)
|
||||
{
|
||||
for (int y = 0; y < height; y++)
|
||||
{
|
||||
gridPositionList.Add(offset + new Vector2Int(x, y));
|
||||
}
|
||||
}
|
||||
break;
|
||||
case Dir.Left:
|
||||
case Dir.Right:
|
||||
for (int x = 0; x < height; x++)
|
||||
{
|
||||
for (int y = 0; y < width; y++)
|
||||
{
|
||||
gridPositionList.Add(offset + new Vector2Int(x, y));
|
||||
}
|
||||
}
|
||||
break;
|
||||
for (int y = 0; y < height; y++)
|
||||
{
|
||||
gridPositionList.Add(offset + new Vector2Int(x, y));
|
||||
}
|
||||
}
|
||||
return gridPositionList;
|
||||
}
|
||||
|
||||
64
Assets/Scripts/Managers/AreaManager.cs
Normal file
64
Assets/Scripts/Managers/AreaManager.cs
Normal file
@@ -0,0 +1,64 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class AreaManager : MonoBehaviour
|
||||
{
|
||||
[SerializeField] int areaHeight;
|
||||
[SerializeField] int areaWidth;
|
||||
List<AreaObject> areaObjects;
|
||||
[SerializeField] GameObject testPrefab;
|
||||
void Start()
|
||||
{
|
||||
//int gridWidth = GridBuildingSystem.instance.buildingGrid.GetWidth();
|
||||
//int gridHeight = GridBuildingSystem.instance.buildingGrid.GetWidth();
|
||||
|
||||
int areaWidthCount = GridBuildingSystem.instance.buildingGrid.GetWidth() / areaWidth;
|
||||
int areaHeightCount = GridBuildingSystem.instance.buildingGrid.GetHeight() / areaHeight;
|
||||
|
||||
for(int heightCounter = 0; heightCounter < areaHeightCount; heightCounter++)
|
||||
{
|
||||
Debug.Log("---- New Row -----");
|
||||
for (int widthCounter = 0; widthCounter < areaWidthCount; widthCounter++)
|
||||
{
|
||||
Debug.Log("---- New Area -----");
|
||||
|
||||
List<GameObject> testGameObjs = new List<GameObject>();
|
||||
for (int x = 0; x < areaWidth; x++)
|
||||
{
|
||||
for (int y = 0; y < areaHeight; y++)
|
||||
{
|
||||
|
||||
Debug.Log((x + widthCounter * areaWidth).ToString() + "," + (y + heightCounter * areaHeight).ToString());
|
||||
GameObject testObj = Instantiate(testPrefab);
|
||||
testObj.transform.position = GridBuildingSystem.instance.buildingGrid.GetWorldPosition((x + widthCounter * areaWidth), (y + heightCounter * areaHeight));
|
||||
testObj.transform.name = widthCounter + " - " + heightCounter;
|
||||
testGameObjs.Add(testObj);
|
||||
}
|
||||
}
|
||||
|
||||
Color color = Random.ColorHSV();
|
||||
foreach (GameObject testObj in testGameObjs)
|
||||
{
|
||||
testObj.GetComponent<SpriteRenderer>().color = color;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public class AreaObject
|
||||
{
|
||||
public int[,] tileArray;
|
||||
|
||||
public AreaObject(int[,] _tileArray) {
|
||||
tileArray = _tileArray;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Managers/AreaManager.cs.meta
Normal file
11
Assets/Scripts/Managers/AreaManager.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2be9f8a208335fe43bb797bec633e029
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
36
Assets/Scripts/Managers/PersonManager.cs
Normal file
36
Assets/Scripts/Managers/PersonManager.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class PersonManager : MonoBehaviour
|
||||
{
|
||||
City city;
|
||||
[SerializeField] GameObject personPrefab;
|
||||
void Start()
|
||||
{
|
||||
city = GetComponent<City>();
|
||||
TimeManager.OnDayUpdate += OnDayUpdate;
|
||||
}
|
||||
|
||||
void OnDayUpdate()
|
||||
{
|
||||
int avaiableSpace = city.GetAvaiableHousingSpace();
|
||||
if (avaiableSpace > 5)
|
||||
{
|
||||
SpawnPerson(5);
|
||||
}
|
||||
else
|
||||
{
|
||||
SpawnPerson(avaiableSpace);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void SpawnPerson(int count = 1)
|
||||
{
|
||||
for(int i = 0; i < count; i++)
|
||||
{
|
||||
Instantiate(personPrefab);
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Managers/PersonManager.cs.meta
Normal file
11
Assets/Scripts/Managers/PersonManager.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7a66523eed61c5b44af39762a8d6e476
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -31,7 +31,7 @@ public class TimeManager : MonoBehaviour
|
||||
int minutesPerInterval = 1;
|
||||
|
||||
public CultureInfo cultureInfo = new CultureInfo("en-us");
|
||||
DateTime dateTime = new DateTime(1, 1, 1, 0, 0, 0);
|
||||
DateTime dateTime = new DateTime(1, 1, 1, 23, 0, 0);
|
||||
DateTime prevDateTime;
|
||||
float timer;
|
||||
|
||||
|
||||
@@ -31,14 +31,151 @@ public class Person : MonoBehaviour
|
||||
|
||||
System.DateTime goToWorkDateTime;
|
||||
|
||||
|
||||
void Awake()
|
||||
|
||||
// DEBUG
|
||||
private void Update()
|
||||
{
|
||||
|
||||
if (Input.GetKeyDown(KeyCode.G))
|
||||
{
|
||||
movement.SetTarget(Camera.main.ScreenToWorldPoint(Input.mousePosition));
|
||||
}
|
||||
}
|
||||
|
||||
void Start()
|
||||
{
|
||||
Register();
|
||||
movement = gameObject.GetComponent<PersonMovement>();
|
||||
indicators = gameObject.GetComponent<PersonIndicators>();
|
||||
transform.position = house.transform.position;
|
||||
|
||||
TimeManager.OnDayUpdate += DestroyInactive;
|
||||
TimeManager.OnDayUpdate += CheckWorkplace;
|
||||
TimeManager.OnDayUpdate += OnDayUpdate;
|
||||
TimeManager.OnMinuteUpdate += OnMinuteUpdate;
|
||||
|
||||
SetBehaivorDateTimes();
|
||||
}
|
||||
|
||||
void Register()
|
||||
{
|
||||
// Register To City
|
||||
city = GameObject.Find("GameManager").GetComponent<City>();
|
||||
city.AddPerson(this);
|
||||
// Register To House
|
||||
house = city.GetAvaiableHouse();
|
||||
if(house != null)
|
||||
house.AddPerson(this);
|
||||
// Register To Workplace
|
||||
workplace = city.GetAvaiableWorkplace();
|
||||
if(workplace != null)
|
||||
workplace.AddWorker(this);
|
||||
}
|
||||
|
||||
void CheckWorkplace()
|
||||
{
|
||||
if (workplace == null)
|
||||
{
|
||||
workplace = city.GetAvaiableWorkplace();
|
||||
workplace.AddWorker(this);
|
||||
}
|
||||
}
|
||||
void DestroyInactive()
|
||||
{
|
||||
// Destroy a person if they do not find a house anymore
|
||||
if (house == null)
|
||||
{
|
||||
if(city != null)
|
||||
city.RemovePerson(this);
|
||||
if (workplace != null)
|
||||
workplace.RemoveWorker(this);
|
||||
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void OnDayUpdate()
|
||||
{
|
||||
SetBehaivorDateTimes();
|
||||
}
|
||||
void OnMinuteUpdate()
|
||||
{
|
||||
|
||||
// Work -> FreeTime -> Sleep
|
||||
|
||||
if (TimeManager.instance.GetDateTime() > goToWorkDateTime.AddHours(12)) // Sleep
|
||||
{
|
||||
Sleep();
|
||||
}
|
||||
else if (TimeManager.instance.GetDateTime() > goToWorkDateTime.AddHours(8)) // FreeTime
|
||||
{
|
||||
FreeTime();
|
||||
}
|
||||
else if (TimeManager.instance.GetDateTime() > goToWorkDateTime) // Work
|
||||
{
|
||||
Work();
|
||||
}
|
||||
}
|
||||
void FreeTime()
|
||||
{
|
||||
if (status == PersonStatus.STORE)
|
||||
{
|
||||
if (indicators.GetSupplied() == 1.0f)
|
||||
{
|
||||
status = PersonStatus.FREETIME;
|
||||
}
|
||||
}
|
||||
else if (status != PersonStatus.PARK)
|
||||
{
|
||||
status = PersonStatus.FREETIME;
|
||||
}
|
||||
|
||||
if (indicators.GetSupplied() < 0.3f || status == PersonStatus.STORE)
|
||||
{
|
||||
status = PersonStatus.STORE;
|
||||
Debug.Log(city.GetStores().Count - 1);
|
||||
//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.position);
|
||||
}
|
||||
}
|
||||
|
||||
void Work()
|
||||
{
|
||||
status = PersonStatus.WORK;
|
||||
movement.SetTarget(workplace.transform.position);
|
||||
}
|
||||
|
||||
void Sleep()
|
||||
{
|
||||
status = PersonStatus.SLEEP;
|
||||
Debug.Log(house.transform.position);
|
||||
movement.SetTarget(house.transform.position);
|
||||
}
|
||||
|
||||
void SetBehaivorDateTimes()
|
||||
{
|
||||
System.DateTime currentDateTime = TimeManager.instance.GetDateTime();
|
||||
goToWorkDateTime = new System.DateTime(currentDateTime.Year,
|
||||
currentDateTime.Month,
|
||||
currentDateTime.Day,
|
||||
Random.Range(4, 9),
|
||||
Random.Range(0, 59),
|
||||
currentDateTime.Second);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
void Start ()
|
||||
{
|
||||
|
||||
city.AddCitizen(this);
|
||||
house.AddPerson(this);
|
||||
workplace.AddWorker(this);
|
||||
@@ -66,7 +203,7 @@ public class Person : MonoBehaviour
|
||||
Debug.Log("House added to " + GetFullName());
|
||||
}
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
||||
|
||||
movement = GetComponent<PersonMovement>();
|
||||
@@ -79,6 +216,8 @@ public class Person : MonoBehaviour
|
||||
Sleep();
|
||||
}
|
||||
|
||||
|
||||
|
||||
void OnDayUpdate()
|
||||
{
|
||||
SetBehaivorDateTimes();
|
||||
@@ -150,5 +289,5 @@ public class Person : MonoBehaviour
|
||||
Random.Range(0, 59),
|
||||
currentDateTime.Second);
|
||||
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ public class PersonMovement : MonoBehaviour
|
||||
{
|
||||
private int currentPathIndex;
|
||||
[SerializeField] private List<Vector3> pathVectorList = new List<Vector3>();
|
||||
private const float speed = 40f;
|
||||
private const float speed = 5f;
|
||||
Rigidbody2D rigidbody;
|
||||
|
||||
private void Awake()
|
||||
|
||||
8
Assets/Scripts/PlacedObjects.meta
Normal file
8
Assets/Scripts/PlacedObjects.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 131d1b9c458b14447ae47bfeb1c7680e
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -2,7 +2,7 @@ using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class House : MonoBehaviour
|
||||
public class House : PlacedObject
|
||||
{
|
||||
[Header("House")]
|
||||
[SerializeField]
|
||||
@@ -11,13 +11,13 @@ public class House : MonoBehaviour
|
||||
[SerializeField]
|
||||
List<Person> persons = new List<Person>();
|
||||
|
||||
[SerializeField]
|
||||
City city;
|
||||
|
||||
public int GetAvaiableSpace() => space - persons.Count;
|
||||
|
||||
void Awake()
|
||||
public override void OnPlace()
|
||||
{
|
||||
city = GameObject.Find("GameManager").GetComponent<City>();
|
||||
city.AddHouse(this);
|
||||
}
|
||||
|
||||
12
Assets/Scripts/PlacedObjects/Way.cs
Normal file
12
Assets/Scripts/PlacedObjects/Way.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class Way : PlacedObject
|
||||
{
|
||||
public override void OnPlace()
|
||||
{
|
||||
Debug.Log("Placed Way");
|
||||
}
|
||||
|
||||
}
|
||||
11
Assets/Scripts/PlacedObjects/Way.cs.meta
Normal file
11
Assets/Scripts/PlacedObjects/Way.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5b31ffe74c8ae354abc084445ecb77b6
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
21
Assets/Scripts/UI/BuildingMenu.cs
Normal file
21
Assets/Scripts/UI/BuildingMenu.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class BuildingMenu : MonoBehaviour
|
||||
{
|
||||
|
||||
[SerializeField] GameObject slotPrefab;
|
||||
[SerializeField] List<PlacedObjectTypeSO> placedObjectTypeSOList;
|
||||
|
||||
void Start()
|
||||
{
|
||||
foreach(PlacedObjectTypeSO p in placedObjectTypeSOList)
|
||||
{
|
||||
GameObject slot = Instantiate(slotPrefab, gameObject.transform);
|
||||
slot.GetComponent<BuildingMenuSlot>().placedObjectTypeSO = p;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
11
Assets/Scripts/UI/BuildingMenu.cs.meta
Normal file
11
Assets/Scripts/UI/BuildingMenu.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bcce99e456cd44549aa61d6dd97c9846
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
31
Assets/Scripts/UI/BuildingMenuSlot.cs
Normal file
31
Assets/Scripts/UI/BuildingMenuSlot.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using TMPro;
|
||||
|
||||
public class BuildingMenuSlot : MonoBehaviour
|
||||
{
|
||||
public PlacedObjectTypeSO placedObjectTypeSO;
|
||||
|
||||
Image img;
|
||||
Button btn;
|
||||
TextMeshProUGUI textObj;
|
||||
|
||||
void Start()
|
||||
{
|
||||
img = transform.Find("Image").gameObject.GetComponent<Image>();
|
||||
btn = GetComponent<Button>();
|
||||
textObj = transform.Find("Text").gameObject.GetComponent<TextMeshProUGUI>();
|
||||
|
||||
|
||||
img.sprite = placedObjectTypeSO.iconSprite;
|
||||
textObj.text = placedObjectTypeSO.nameString;
|
||||
btn.onClick.AddListener(OnClick);
|
||||
}
|
||||
|
||||
void OnClick()
|
||||
{
|
||||
GridBuildingSystem.instance.SelectBuilding(placedObjectTypeSO);
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/UI/BuildingMenuSlot.cs.meta
Normal file
11
Assets/Scripts/UI/BuildingMenuSlot.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 49a86965eb0eda744b944b1e786c49ac
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -2,7 +2,7 @@ using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class Workplace : MonoBehaviour
|
||||
public class Workplace : PlacedObject
|
||||
{
|
||||
[Header("Workplace")]
|
||||
[SerializeField] int space = 4;
|
||||
@@ -11,22 +11,16 @@ public class Workplace : MonoBehaviour
|
||||
[SerializeField] List<Person> workers = new List<Person>();
|
||||
[SerializeField] List<Person> activeWorkers = new List<Person>(); // Workers which are currently present and working
|
||||
|
||||
[SerializeField] Company company;
|
||||
public void SetCompany(Company _company) => company = _company;
|
||||
City city;
|
||||
public void AddActiveWorker(Person worker) => activeWorkers.Add(worker);
|
||||
public void RemoveActiveWorker(Person worker) => activeWorkers.Remove(worker);
|
||||
public int GetAvaiableSpace() => space - workers.Count;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
company.AddWorkplace(this);
|
||||
company.GetCity().AddWorkplace(this);
|
||||
}
|
||||
|
||||
void Start()
|
||||
public override void OnPlace()
|
||||
{
|
||||
city = GameObject.Find("GameManager").GetComponent<City>();
|
||||
city.AddWorkplace(GetComponent<Workplace>());
|
||||
TimeManager.OnHourUpdate += OnHourUpdate;
|
||||
|
||||
}
|
||||
|
||||
void OnTriggerEnter2D(Collider2D collision)
|
||||
|
||||
Reference in New Issue
Block a user