This commit is contained in:
j.mei7
2022-03-01 20:47:08 +01:00
parent 37f2c289bc
commit 7e0f3e6930
188 changed files with 218108 additions and 0 deletions

73
Assets/Scripts/City.cs Normal file
View File

@@ -0,0 +1,73 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class City : MonoBehaviour
{
[Header("City")]
[SerializeField]
string cityName;
[SerializeField]
List<Person> citizens = new List<Person>();
[SerializeField]
List<House> houses = new List<House>();
[SerializeField]
List<Workplace> workplaces = new List<Workplace>();
public void AddCitizen(Person citizen)
{
if (!citizens.Contains(citizen))
{
citizens.Add(citizen);
Debug.Log(citizen.GetFullName() + " joined " + cityName);
}
}
public void RemoveCitizen(Person citizen)
{
if (citizens.Contains(citizen))
{
citizens.Remove(citizen);
Debug.Log(citizen.GetFullName() + " left " + cityName);
}
}
public void AddHouse(House house)
{
if (!houses.Contains(house))
{
houses.Add(house);
Debug.Log("House built in " + cityName);
}
}
public void RemoveHouse(House house)
{
if (houses.Contains(house))
{
houses.Remove(house);
Debug.Log("House demolished in " + cityName);
}
}
public void AddWorkplace(Workplace workplace)
{
if (!workplaces.Contains(workplace))
{
workplaces.Add(workplace);
Debug.Log("Workplace built in " + cityName);
}
}
public void RemoveWorkplace(Workplace workplace)
{
if (workplaces.Contains(workplace))
{
workplaces.Remove(workplace);
Debug.Log("Workplace demolished in " + cityName);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 1da1a268031cfd742a614a4d319ddc32
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

39
Assets/Scripts/House.cs Normal file
View File

@@ -0,0 +1,39 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class House : MonoBehaviour
{
[Header("House")]
[SerializeField]
int space = 1;
[SerializeField]
List<Person> persons = new List<Person>();
[SerializeField]
City city;
void Awake()
{
city.AddHouse(this);
}
public void AddPerson(Person person)
{
if (!persons.Contains(person) && persons.Count < space)
{
persons.Add(person);
Debug.Log(person.GetFullName() + " now lives in house");
}
}
public void RemovePerson(Person person)
{
if (persons.Contains(person))
{
persons.Remove(person);
Debug.Log(person.GetFullName() + " does not live in house anymore");
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 5efcb4abb6526ca4fbbd130512bbc14d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

66
Assets/Scripts/Person.cs Normal file
View File

@@ -0,0 +1,66 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Person : MonoBehaviour
{
[Header("Person")]
[SerializeField]
string firstName = "";
[SerializeField]
string lastName = "";
[SerializeField]
City city;
[SerializeField]
House house;
[SerializeField]
Workplace workplace;
TimeManager timeManager;
PersonMovement movement;
public string GetFirstName() => firstName;
public string GetLastName() => lastName;
public string GetFullName() => firstName + " " + lastName;
void Awake()
{
city.AddCitizen(this);
house.AddPerson(this);
workplace.AddWorker(this);
}
void Start()
{
timeManager = GameObject.Find("GameManager").GetComponent<TimeManager>();
movement = GetComponent<PersonMovement>();
TimeManager.OnTimeInterval += OnTimeInterval;
}
void OnTimeInterval()
{
switch (timeManager.partOfDay)
{
case TimeManager.PartOfDay.NIGHT:
movement.SetTarget(house.transform);
break;
case TimeManager.PartOfDay.MORNING:
movement.SetTarget(workplace.transform);
break;
case TimeManager.PartOfDay.AFTERNOON:
movement.SetTarget(workplace.transform);
break;
case TimeManager.PartOfDay.EVENING:
movement.SetTarget(city.transform);
break;
default:
movement.SetTarget(city.transform);
break;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 8647d87746af05e41a97676f479a8da8
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,21 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class PersonMovement : MonoBehaviour
{
NavMeshAgent agent;
private void Awake()
{
agent = GetComponent<NavMeshAgent>();
agent.updateRotation = false;
agent.updateUpAxis = false;
}
public void SetTarget(Transform target)
{
agent.SetDestination(target.position);
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 92efa8c0fa40ca1429bb33b689b384ea
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,68 @@
using System;
using System.Globalization;
using System.Collections.Generic;
using UnityEngine;
public class TimeManager : MonoBehaviour
{
public static Action OnTimeInterval;
public static TimeManager instance;
public enum PartOfDay
{
MORNING,
AFTERNOON,
EVENING,
NIGHT
}
public PartOfDay partOfDay;
[SerializeField]
float intervalTime = 1.0f; // 1.0f -> 1 real second is 1 ingame minute
int minutesPerInterval = 1;
public CultureInfo cultureInfo = new CultureInfo("en-us");
DateTime dateTime = new DateTime(1, 1, 1, 0, 0, 0);
float timer;
public DateTime GetDateTime() => dateTime;
public string GetTime() => dateTime.ToString("hh:mm tt", cultureInfo);
public string GetDate() => dateTime.ToString("dd/mm/yyyy", cultureInfo);
public float GetintervalTime() => intervalTime;
void Start()
{
timer = intervalTime;
}
void Update()
{
timer -= Time.deltaTime;
if (timer <= 0)
{
dateTime = dateTime.AddMinutes(minutesPerInterval);
CheckPartsOfDay();
OnTimeInterval?.Invoke();
timer = intervalTime;
}
}
void CheckPartsOfDay()
{
if (dateTime.Hour >= 22)
partOfDay = PartOfDay.NIGHT;
else if (dateTime.Hour < 6)
partOfDay = PartOfDay.NIGHT;
else if (dateTime.Hour >= 6 && dateTime.Hour < 12)
partOfDay = PartOfDay.MORNING;
else if (dateTime.Hour >= 12 && dateTime.Hour < 17)
partOfDay = PartOfDay.AFTERNOON;
else if (dateTime.Hour >= 17 && dateTime.Hour < 22)
partOfDay = PartOfDay.EVENING;
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 19408a8c9044fc542b7045812e07e7fa
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

25
Assets/Scripts/TimeUI.cs Normal file
View File

@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class TimeUI : MonoBehaviour
{
TextMeshProUGUI dateTimeText;
TimeManager timeManager;
private void Start()
{
timeManager = GameObject.Find("GameManager").GetComponent<TimeManager>();
TimeManager.OnTimeInterval += OnInterval;
dateTimeText = GetComponent<TextMeshProUGUI>();
}
void OnInterval()
{
DateTime dateTime = timeManager.GetDateTime();
if (dateTimeText != null)
dateTimeText.text = dateTime.ToString("hh:mm tt / dd.MM.yyyy", timeManager.cultureInfo);
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 3c60f5ffa1a5b6f4292a33377311dcb6
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,42 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Workplace : MonoBehaviour
{
[Header("Workplace")]
[SerializeField]
int space = 1;
[SerializeField]
float salary = 4.5f;
[SerializeField]
List<Person> workers = new List<Person>();
[SerializeField]
City city;
void Awake()
{
city.AddWorkplace(this);
}
public void AddWorker(Person worker)
{
if (!workers.Contains(worker) && workers.Count < space)
{
workers.Add(worker);
Debug.Log(worker.GetFullName() + " now works");
}
}
public void RemoveWorker(Person worker)
{
if (workers.Contains(worker))
{
workers.Remove(worker);
Debug.Log(worker.GetFullName() + " does not work anymore");
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 2b0d700ec6a861f499e0c4bf7220bb68
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: