mirror of
https://github.com/DerTyp7/grow-ai-unity.git
synced 2025-10-30 04:47:10 +01:00
added CameraMovement
This commit is contained in:
105
Assets/Scripts/CameraMovement.cs
Normal file
105
Assets/Scripts/CameraMovement.cs
Normal file
@@ -0,0 +1,105 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class CameraMovement : MonoBehaviour
|
||||
{
|
||||
[Header("Speed")]
|
||||
[SerializeField]
|
||||
float speed = 150f;
|
||||
|
||||
[SerializeField]
|
||||
float fastSpeed = 300f;
|
||||
|
||||
[SerializeField]
|
||||
float slowSpeed = 50f;
|
||||
|
||||
[Header("Zoom")]
|
||||
[SerializeField]
|
||||
float cameraSize = 15.0f;
|
||||
|
||||
[SerializeField]
|
||||
float maxCameraSize = 100f;
|
||||
|
||||
[SerializeField]
|
||||
float minCameraSize = 3f;
|
||||
|
||||
[SerializeField]
|
||||
float cameraSizeSteps = 3f;
|
||||
|
||||
Vector2 cameraMovement;
|
||||
float currentSpeed = 0.0f;
|
||||
|
||||
bool drag = false;
|
||||
|
||||
Vector3 origin;
|
||||
Vector3 difference;
|
||||
|
||||
|
||||
|
||||
Camera cam;
|
||||
Rigidbody2D rb;
|
||||
|
||||
void Start()
|
||||
{
|
||||
cam = GetComponent<Camera>();
|
||||
rb = GetComponent<Rigidbody2D>();
|
||||
}
|
||||
|
||||
void LateUpdate()
|
||||
{
|
||||
if (Input.GetMouseButton(2))
|
||||
{
|
||||
difference = (cam.ScreenToWorldPoint(Input.mousePosition) - cam.transform.position);
|
||||
if (!drag)
|
||||
{
|
||||
drag = true;
|
||||
origin = cam.ScreenToWorldPoint(Input.mousePosition);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
drag = false;
|
||||
}
|
||||
|
||||
if (drag)
|
||||
{
|
||||
cam.transform.position = origin - difference;
|
||||
}
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
currentSpeed = speed;
|
||||
|
||||
cameraMovement.x = Input.GetAxis("Horizontal");
|
||||
cameraMovement.y = Input.GetAxis("Vertical");
|
||||
|
||||
if (Input.GetButton("CameraFast"))
|
||||
currentSpeed = fastSpeed;
|
||||
else if (Input.GetButton("CameraSlow"))
|
||||
currentSpeed = slowSpeed;
|
||||
|
||||
|
||||
if (Input.GetAxis("Mouse ScrollWheel") > 0f)
|
||||
ZoomCameraToPoint(cam.ScreenToWorldPoint(Input.mousePosition), cameraSizeSteps);
|
||||
else if (Input.GetAxis("Mouse ScrollWheel") < 0f)
|
||||
ZoomCameraToPoint(cam.ScreenToWorldPoint(Input.mousePosition), -cameraSizeSteps);
|
||||
|
||||
|
||||
}
|
||||
|
||||
void FixedUpdate()
|
||||
{
|
||||
rb.MovePosition(rb.position + cameraMovement * currentSpeed * Time.fixedDeltaTime);
|
||||
}
|
||||
|
||||
void ZoomCameraToPoint(Vector3 point, float amount)
|
||||
{
|
||||
float multiplier = (1.0f / cam.orthographicSize * amount);
|
||||
|
||||
transform.position += (point - transform.position) * multiplier;
|
||||
|
||||
cam.orthographicSize -= amount;
|
||||
|
||||
cam.orthographicSize = Mathf.Clamp(cam.orthographicSize, minCameraSize, maxCameraSize);
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/CameraMovement.cs.meta
Normal file
11
Assets/Scripts/CameraMovement.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 328f45ec5c7fdbe4ebfa4e5069f0644d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
21
Assets/Scripts/EconomyManager.cs
Normal file
21
Assets/Scripts/EconomyManager.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class EconomyManager : MonoBehaviour
|
||||
{
|
||||
public static EconomyManager instance;
|
||||
|
||||
[SerializeField]
|
||||
float money = 0.0f;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
instance = this;
|
||||
}
|
||||
|
||||
public void AddMoney(float value) => money += value;
|
||||
public float GetMoney() => money;
|
||||
public void SetMoney(float value) => money = value;
|
||||
public void RemoveMone(float value) => money -= value;
|
||||
}
|
||||
11
Assets/Scripts/EconomyManager.cs.meta
Normal file
11
Assets/Scripts/EconomyManager.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d01b08fa5ea32674c8ce7599f2d886ce
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -23,6 +23,8 @@ public class Person : MonoBehaviour
|
||||
TimeManager timeManager;
|
||||
PersonMovement movement;
|
||||
|
||||
public bool isWorking = false;
|
||||
|
||||
public string GetFirstName() => firstName;
|
||||
public string GetLastName() => lastName;
|
||||
public string GetFullName() => firstName + " " + lastName;
|
||||
@@ -41,10 +43,10 @@ public class Person : MonoBehaviour
|
||||
timeManager = GameObject.Find("GameManager").GetComponent<TimeManager>();
|
||||
movement = GetComponent<PersonMovement>();
|
||||
|
||||
TimeManager.OnTimeInterval += OnTimeInterval;
|
||||
TimeManager.OnTimeUpdate += OnTimeUpdate;
|
||||
}
|
||||
|
||||
void OnTimeInterval()
|
||||
void OnTimeUpdate()
|
||||
{
|
||||
if (prevPartOfDay != timeManager.partOfDay)
|
||||
{
|
||||
@@ -55,11 +57,12 @@ public class Person : MonoBehaviour
|
||||
break;
|
||||
case TimeManager.PartOfDay.MORNING:
|
||||
movement.SetTarget(workplace.transform);
|
||||
workplace.AddActiveWorker(this);
|
||||
break;
|
||||
case TimeManager.PartOfDay.AFTERNOON:
|
||||
movement.SetTarget(workplace.transform);
|
||||
break;
|
||||
case TimeManager.PartOfDay.EVENING:
|
||||
workplace.RemoveActiveWorker(this);
|
||||
movement.SetTarget(city.transform);
|
||||
break;
|
||||
default:
|
||||
|
||||
@@ -5,7 +5,14 @@ using UnityEngine;
|
||||
|
||||
public class TimeManager : MonoBehaviour
|
||||
{
|
||||
public static Action OnTimeInterval;
|
||||
public static Action OnTimeUpdate;
|
||||
public static Action OnSecondUpdate;
|
||||
public static Action OnMinuteUpdate;
|
||||
public static Action OnHourUpdate;
|
||||
public static Action OnDayUpdate;
|
||||
public static Action OnMonthUpdate;
|
||||
public static Action OnYearUpdate;
|
||||
|
||||
public static TimeManager instance;
|
||||
|
||||
public enum PartOfDay
|
||||
@@ -25,6 +32,7 @@ public class TimeManager : MonoBehaviour
|
||||
|
||||
public CultureInfo cultureInfo = new CultureInfo("en-us");
|
||||
DateTime dateTime = new DateTime(1, 1, 1, 0, 0, 0);
|
||||
DateTime newDateTime;
|
||||
float timer;
|
||||
|
||||
public DateTime GetDateTime() => dateTime;
|
||||
@@ -40,6 +48,7 @@ public class TimeManager : MonoBehaviour
|
||||
void Start()
|
||||
{
|
||||
timer = intervalTime;
|
||||
newDateTime = dateTime;
|
||||
}
|
||||
|
||||
void Update()
|
||||
@@ -48,11 +57,26 @@ public class TimeManager : MonoBehaviour
|
||||
|
||||
if (timer <= 0)
|
||||
{
|
||||
dateTime = dateTime.AddMinutes(minutesPerInterval);
|
||||
newDateTime = dateTime.AddMinutes(minutesPerInterval);
|
||||
|
||||
if(newDateTime.Second != dateTime.Second)
|
||||
OnSecondUpdate?.Invoke();
|
||||
if (newDateTime.Minute != dateTime.Minute)
|
||||
OnMinuteUpdate?.Invoke();
|
||||
if (newDateTime.Hour != dateTime.Hour)
|
||||
OnHourUpdate?.Invoke();
|
||||
if(newDateTime.Day != dateTime.Day)
|
||||
OnDayUpdate?.Invoke();
|
||||
if(newDateTime.Month != dateTime.Month)
|
||||
OnMonthUpdate?.Invoke();
|
||||
if(dateTime.Year != dateTime.Year)
|
||||
OnYearUpdate?.Invoke();
|
||||
|
||||
dateTime = newDateTime;
|
||||
CheckPartsOfDay();
|
||||
OnTimeInterval?.Invoke();
|
||||
timer = intervalTime;
|
||||
readOnlyTimeString = GetTime() + " " + GetDate();
|
||||
OnTimeUpdate?.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -11,12 +11,12 @@ public class TimeUI : MonoBehaviour
|
||||
private void Start()
|
||||
{
|
||||
timeManager = GameObject.Find("GameManager").GetComponent<TimeManager>();
|
||||
TimeManager.OnTimeInterval += OnInterval;
|
||||
TimeManager.OnTimeUpdate += OnTimeUpdate;
|
||||
dateTimeText = GetComponent<TextMeshProUGUI>();
|
||||
|
||||
}
|
||||
|
||||
void OnInterval()
|
||||
void OnTimeUpdate()
|
||||
{
|
||||
DateTime dateTime = timeManager.GetDateTime();
|
||||
if (dateTimeText != null)
|
||||
|
||||
@@ -14,12 +14,29 @@ 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]
|
||||
City city;
|
||||
|
||||
public void AddActiveWorker(Person worker) => activeWorkers.Add(worker);
|
||||
public void RemoveActiveWorker(Person worker) => activeWorkers.Remove(worker);
|
||||
|
||||
void Awake()
|
||||
{
|
||||
city.AddWorkplace(this);
|
||||
|
||||
}
|
||||
|
||||
void Start()
|
||||
{
|
||||
TimeManager.OnHourUpdate += OnHourUpdate;
|
||||
}
|
||||
|
||||
void OnHourUpdate()
|
||||
{
|
||||
EconomyManager.instance.AddMoney(salary * activeWorkers.Count);
|
||||
}
|
||||
|
||||
public void AddWorker(Person worker)
|
||||
|
||||
Reference in New Issue
Block a user