/
CameraFocus.cs
101 lines (87 loc) · 4.08 KB
/
CameraFocus.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
UModules::CameraFocus
by: Rajin Shankar
part of: UM_Camera2D
available to use according to UM_Camera2D/LICENSE
*/
using System;
using System.Collections.Generic;
using UnityEngine;
namespace UModules
{
/// <summary>
/// Generic camera targeting component shared by all targeting implementations.
/// Used by the CameraMotion component to provide following behavior
/// </summary>
/// <module>UM_Camera2D</module>
[DisallowMultipleComponent]
public abstract class CameraFocus : ExtendedBehaviour
{
/// <summary>All currently active focal points</summary>
/// <access>protected List<CameraFocalPoint></access>
protected List<CameraFocalPoint> activeFocalPoints = new List<CameraFocalPoint>();
/// <summary>Add a new focal point to be included in average calculation</summary>
/// <access>public void</access>
/// <param name="target" type="CameraFocalPoint">CameraFocalPoint to track</param>
public void AddFocalPoint(CameraFocalPoint target)
{
activeFocalPoints.Add(target);
}
/// <summary>Remove a focal point from average calculation</summary>
/// <access>public bool</access>
/// <param name="target" type="CameraFocalPoint">CameraFocalPoint to remove</param>
/// <returns>True if any targets were removed, false otherwise</returns>
public bool RemoveFocalPoint(CameraFocalPoint target)
{
return activeFocalPoints.Remove(target);
}
/// <summary>Base focal point to use as distance reference</summary>
[SerializeField]
protected CameraFocalPoint baseFocalPoint;
/// <summary>Public read-only reference to base focal point</summary>
public CameraFocalPoint BaseFocalPoint { get { return baseFocalPoint; } }
/// <summary>Enum for whether distance calculation should be done relative to the base target (ie a player) or to the camera.</summary>
protected enum DistanceCalculationMode
{
RelativeToBaseTarget,
RelativeToCamera
}
/// <summary>
/// Should distance calculation be done relative to the base target or to the camera?
/// Note: Camera-relative distance calculation can cause motion to get stuck on one focal point.
/// </summary>
///<access>protected DistanceCalculationMode</access>
protected DistanceCalculationMode calculationMode = DistanceCalculationMode.RelativeToBaseTarget;
/// <summary>
/// Structure holding a target focal point and a speed to move toward that point.
/// Used by the CameraMotion component to actually move the object
/// </summary>
public struct MovementTarget
{
/// <summary>The target position to move toward</summary>
///<access>public readonly Vector2</access>
public readonly Vector2 position;
/// <summary>The speed at which to move toward the target</summary>
///<access>public readonly float</access>
public readonly float speed;
/// <summary>The zoom value at the target</summary>
///<access>public readonly float</access>
public readonly float zoom;
/// <summary>The pull value at the target</summary>
///<access>public readonly< float/access>
public readonly float pull;
public MovementTarget(Vector2 position, float zoom, float pull, float speed)
{
this.position = position;
this.zoom = zoom;
this.pull = pull;
this.speed = speed;
}
}
/// <summary>Get the final target information based on some calculation (in child classes)</summary>
/// <returns>A MovementTarget structure containing the target point and speed to move there</returns>
/// <seealso cref="CameraFocus.MovementTarget" />
///<access>public virtual MovementTarget</access>
public virtual MovementTarget GetMovementTarget() { return new MovementTarget(); }
}
}