added CameraMovement

This commit is contained in:
j.mei7
2022-03-05 13:01:45 +01:00
parent 0207486c95
commit 0a763cbc88
12 changed files with 286 additions and 14 deletions

View File

@@ -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

View 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);
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 328f45ec5c7fdbe4ebfa4e5069f0644d
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;
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;
}

View File

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

View File

@@ -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:

View File

@@ -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();
}
}

View File

@@ -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)

View File

@@ -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)

View File

@@ -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
}
}

View File

@@ -0,0 +1,6 @@
{
"MonoBehaviour": {
"Version": 4,
"DisabledWarnings": ""
}
}

View File

@@ -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