From 0a763cbc88bd6c1ba9025c35409e670ee5afaa50 Mon Sep 17 00:00:00 2001 From: "j.mei7" Date: Sat, 5 Mar 2022 13:01:45 +0100 Subject: [PATCH] added CameraMovement --- Assets/Scenes/SampleScene.unity | 60 +++++++++- Assets/Scripts/CameraMovement.cs | 105 ++++++++++++++++++ Assets/Scripts/CameraMovement.cs.meta | 11 ++ Assets/Scripts/EconomyManager.cs | 21 ++++ Assets/Scripts/EconomyManager.cs.meta | 11 ++ Assets/Scripts/Person.cs | 9 +- Assets/Scripts/TimeManager.cs | 30 ++++- Assets/Scripts/TimeUI.cs | 4 +- Assets/Scripts/Workplace.cs | 17 +++ .../BurstAotSettings_StandaloneWindows.json | 17 +++ ProjectSettings/CommonBurstAotSettings.json | 6 + ProjectSettings/InputManager.asset | 9 +- 12 files changed, 286 insertions(+), 14 deletions(-) create mode 100644 Assets/Scripts/CameraMovement.cs create mode 100644 Assets/Scripts/CameraMovement.cs.meta create mode 100644 Assets/Scripts/EconomyManager.cs create mode 100644 Assets/Scripts/EconomyManager.cs.meta create mode 100644 ProjectSettings/BurstAotSettings_StandaloneWindows.json create mode 100644 ProjectSettings/CommonBurstAotSettings.json diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 317fb94..bf57abb 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -6154,6 +6154,8 @@ GameObject: - component: {fileID: 519420032} - component: {fileID: 519420031} - component: {fileID: 519420029} + - component: {fileID: 519420033} + - component: {fileID: 519420034} m_Layer: 0 m_Name: Main Camera m_TagString: MainCamera @@ -6196,7 +6198,7 @@ Camera: far clip plane: 1000 field of view: 60 orthographic: 1 - orthographic size: 12 + orthographic size: 50 m_Depth: -1 m_CullingMask: serializedVersion: 2 @@ -6220,13 +6222,53 @@ Transform: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 519420028} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: 0, y: 0, z: -10} + m_LocalPosition: {x: 0, y: 0, z: -15} m_LocalScale: {x: 1, y: 1, z: 1} m_ConstrainProportionsScale: 0 m_Children: [] m_Father: {fileID: 0} m_RootOrder: 3 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!114 &519420033 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 519420028} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 328f45ec5c7fdbe4ebfa4e5069f0644d, type: 3} + m_Name: + m_EditorClassIdentifier: + speed: 150 + fastSpeed: 300 + slowSpeed: 50 + cameraSize: 15 + maxCameraSize: 100 + minCameraSize: 3 + cameraSizeSteps: 3 +--- !u!50 &519420034 +Rigidbody2D: + serializedVersion: 4 + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 519420028} + m_BodyType: 0 + m_Simulated: 1 + m_UseFullKinematicContacts: 0 + m_UseAutoMass: 0 + m_Mass: 1 + m_LinearDrag: 0 + m_AngularDrag: 0 + m_GravityScale: 0 + m_Material: {fileID: 0} + m_Interpolate: 0 + m_SleepingMode: 1 + m_CollisionDetection: 0 + m_Constraints: 4 --- !u!114 &596336087 stripped MonoBehaviour: m_CorrespondingSourceObject: {fileID: 8483988689672967183, guid: 23ae9a68a9ff79940adf19a8638591c2, type: 3} @@ -7588,6 +7630,7 @@ GameObject: m_Component: - component: {fileID: 1360475758} - component: {fileID: 1360475757} + - component: {fileID: 1360475759} m_Layer: 0 m_Name: GameManager m_TagString: Untagged @@ -7625,6 +7668,19 @@ Transform: m_Father: {fileID: 0} m_RootOrder: 0 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!114 &1360475759 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1360475756} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: d01b08fa5ea32674c8ce7599f2d886ce, type: 3} + m_Name: + m_EditorClassIdentifier: + money: 0 --- !u!1001 &1365118269 PrefabInstance: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/CameraMovement.cs b/Assets/Scripts/CameraMovement.cs new file mode 100644 index 0000000..1bd1afb --- /dev/null +++ b/Assets/Scripts/CameraMovement.cs @@ -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(); + rb = GetComponent(); + } + + 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); + } +} diff --git a/Assets/Scripts/CameraMovement.cs.meta b/Assets/Scripts/CameraMovement.cs.meta new file mode 100644 index 0000000..8be89bc --- /dev/null +++ b/Assets/Scripts/CameraMovement.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 328f45ec5c7fdbe4ebfa4e5069f0644d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/EconomyManager.cs b/Assets/Scripts/EconomyManager.cs new file mode 100644 index 0000000..c31b754 --- /dev/null +++ b/Assets/Scripts/EconomyManager.cs @@ -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; +} diff --git a/Assets/Scripts/EconomyManager.cs.meta b/Assets/Scripts/EconomyManager.cs.meta new file mode 100644 index 0000000..2ffa146 --- /dev/null +++ b/Assets/Scripts/EconomyManager.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: d01b08fa5ea32674c8ce7599f2d886ce +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Person.cs b/Assets/Scripts/Person.cs index 65cae5e..fbb9919 100644 --- a/Assets/Scripts/Person.cs +++ b/Assets/Scripts/Person.cs @@ -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(); movement = GetComponent(); - 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: diff --git a/Assets/Scripts/TimeManager.cs b/Assets/Scripts/TimeManager.cs index 46f19ac..f08d8ca 100644 --- a/Assets/Scripts/TimeManager.cs +++ b/Assets/Scripts/TimeManager.cs @@ -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(); } } diff --git a/Assets/Scripts/TimeUI.cs b/Assets/Scripts/TimeUI.cs index 2df5296..4b0ecc3 100644 --- a/Assets/Scripts/TimeUI.cs +++ b/Assets/Scripts/TimeUI.cs @@ -11,12 +11,12 @@ public class TimeUI : MonoBehaviour private void Start() { timeManager = GameObject.Find("GameManager").GetComponent(); - TimeManager.OnTimeInterval += OnInterval; + TimeManager.OnTimeUpdate += OnTimeUpdate; dateTimeText = GetComponent(); } - void OnInterval() + void OnTimeUpdate() { DateTime dateTime = timeManager.GetDateTime(); if (dateTimeText != null) diff --git a/Assets/Scripts/Workplace.cs b/Assets/Scripts/Workplace.cs index f44f060..f5e84e8 100644 --- a/Assets/Scripts/Workplace.cs +++ b/Assets/Scripts/Workplace.cs @@ -14,12 +14,29 @@ public class Workplace : MonoBehaviour [SerializeField] List workers = new List(); + [SerializeField] + List activeWorkers = new List(); // 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) diff --git a/ProjectSettings/BurstAotSettings_StandaloneWindows.json b/ProjectSettings/BurstAotSettings_StandaloneWindows.json new file mode 100644 index 0000000..e02ae33 --- /dev/null +++ b/ProjectSettings/BurstAotSettings_StandaloneWindows.json @@ -0,0 +1,17 @@ +{ + "MonoBehaviour": { + "Version": 4, + "EnableBurstCompilation": true, + "EnableOptimisations": true, + "EnableSafetyChecks": false, + "EnableDebugInAllBuilds": false, + "UsePlatformSDKLinker": false, + "CpuMinTargetX32": 0, + "CpuMaxTargetX32": 0, + "CpuMinTargetX64": 0, + "CpuMaxTargetX64": 0, + "CpuTargetsX32": 6, + "CpuTargetsX64": 72, + "OptimizeFor": 0 + } +} diff --git a/ProjectSettings/CommonBurstAotSettings.json b/ProjectSettings/CommonBurstAotSettings.json new file mode 100644 index 0000000..0293daf --- /dev/null +++ b/ProjectSettings/CommonBurstAotSettings.json @@ -0,0 +1,6 @@ +{ + "MonoBehaviour": { + "Version": 4, + "DisabledWarnings": "" + } +} diff --git a/ProjectSettings/InputManager.asset b/ProjectSettings/InputManager.asset index b16147e..2cc7927 100644 --- a/ProjectSettings/InputManager.asset +++ b/ProjectSettings/InputManager.asset @@ -70,13 +70,13 @@ InputManager: axis: 0 joyNum: 0 - serializedVersion: 3 - m_Name: Fire3 + m_Name: CameraSlow descriptiveName: descriptiveNegativeName: negativeButton: positiveButton: left shift altNegativeButton: - altPositiveButton: mouse 2 + altPositiveButton: gravity: 1000 dead: 0.001 sensitivity: 1000 @@ -86,11 +86,11 @@ InputManager: axis: 0 joyNum: 0 - serializedVersion: 3 - m_Name: Jump + m_Name: CameraFast descriptiveName: descriptiveNegativeName: negativeButton: - positiveButton: space + positiveButton: left ctrl altNegativeButton: altPositiveButton: gravity: 1000 @@ -485,3 +485,4 @@ InputManager: type: 2 axis: 5 joyNum: 0 + m_UsePhysicalKeys: 1