Skip to content

Commit ba9c7fd

Browse files
committed
feat: Add boundary class for camera
1 parent 891388b commit ba9c7fd

File tree

8 files changed

+2017
-55
lines changed

8 files changed

+2017
-55
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* $File: JCS_Boundary.cs $
3+
* $Date: $
4+
* $Revision: $
5+
* $Creator: Jen-Chieh Shen $
6+
* $Notice: See LICENSE.txt for modification and distribution information
7+
* Copyright (c) 2025 by Shen, Jen-Chieh $
8+
*/
9+
using UnityEngine;
10+
using MyBox;
11+
12+
namespace JCSUnity
13+
{
14+
/// <summary>
15+
/// Boundary definition.
16+
/// </summary>
17+
public class JCS_Boundary : MonoBehaviour
18+
{
19+
/* Variables */
20+
21+
#if UNITY_EDITOR
22+
[Separator("Helper Variables (JCS_Boundary)")]
23+
24+
[Tooltip("The wire color for gizmos draw.")]
25+
[SerializeField]
26+
private Color mWireColor = new Color(0.8f, 0.3f, 0.3f, 0.8f);
27+
#endif
28+
29+
/* Setter & Getter */
30+
31+
/* Functions */
32+
33+
#if UNITY_EDITOR
34+
private void OnDrawGizmosSelected()
35+
{
36+
Gizmos.color = mWireColor;
37+
Gizmos.DrawWireCube(transform.position, transform.localScale);
38+
}
39+
#endif
40+
41+
/// <summary>
42+
/// Bounds of this spawner; now is only a cube space.
43+
/// </summary>
44+
public Bounds GetBounds()
45+
{
46+
return new Bounds(transform.position, transform.localScale);
47+
}
48+
}
49+
}

Assets/JCSUnity/Scripts/Actions/JCS_Boundary.cs.meta

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/JCSUnity/Scripts/Interactive/2D/Camera/JCS_2DCamera.cs

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -99,21 +99,6 @@ public class JCS_2DCamera : JCS_Camera
9999

100100
private float mWheelDegree = 0;
101101

102-
[Header("- Scene")]
103-
104-
[Tooltip("Maxinum this camera can go in x-axis.")]
105-
[SerializeField]
106-
private float mMax_X_PositionInScene = float.PositiveInfinity;
107-
[Tooltip("Mininum this camera can go in x-axis.")]
108-
[SerializeField]
109-
private float mMin_X_PositionInScene = float.NegativeInfinity;
110-
[Tooltip("Maxinum this camera can go in y-axis.")]
111-
[SerializeField]
112-
private float mMax_Y_PositionInScene = float.PositiveInfinity;
113-
[Tooltip("Mininum this camera can go in y-axis.")]
114-
[SerializeField]
115-
private float mMin_Y_PositionInScene = float.NegativeInfinity;
116-
117102
/* Setter & Getter */
118103

119104
public bool FreezeX { get { return this.mFreezeX; } set { this.mFreezeX = value; } }
@@ -236,9 +221,6 @@ protected virtual void FixedUpdate()
236221

237222
// do freezing the last
238223
DoFreezing();
239-
240-
// Do the camera boundaries check!!
241-
CameraBoundaries();
242224
}
243225

244226
/// <summary>
@@ -281,41 +263,6 @@ public void ZoomCamera(float depthDistance)
281263
this.mTargetPosition.z += cameraDepth;
282264
}
283265

284-
/// <summary>
285-
/// 4 boundaries (top, bottom, right, left) that camera should not
286-
/// go through.
287-
///
288-
/// check the boundries and do the trick!
289-
/// </summary>
290-
private void CameraBoundaries()
291-
{
292-
Vector3 camPos = this.transform.position;
293-
294-
if (camPos.x > mMax_X_PositionInScene)
295-
{
296-
camPos.x = mMax_X_PositionInScene;
297-
mVelocity.x = 0;
298-
}
299-
else if (camPos.x < mMin_X_PositionInScene)
300-
{
301-
camPos.x = mMin_X_PositionInScene;
302-
mVelocity.x = 0;
303-
}
304-
305-
if (camPos.y > mMax_Y_PositionInScene)
306-
{
307-
camPos.y = mMax_Y_PositionInScene;
308-
mVelocity.y = 0;
309-
}
310-
else if (camPos.y < mMin_Y_PositionInScene)
311-
{
312-
camPos.y = mMin_Y_PositionInScene;
313-
mVelocity.y = 0;
314-
}
315-
316-
this.transform.position = camPos;
317-
}
318-
319266
/// <summary>
320267
/// Do freezing.
321268
/// </summary>

Assets/JCSUnity/Scripts/Interactive/3D/JCS_3DCamera.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,6 @@ protected override void LateUpdate()
214214

215215
UpDownMovement();
216216

217-
218217
this.mRecordPosition = this.transform.position;
219218

220219
{

Assets/JCSUnity/Scripts/JCS_Camera.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
namespace JCSUnity
1515
{
1616
/// <summary>
17-
/// Camera class for JCSUnity framework.
17+
/// Camera based class.
1818
/// </summary>
1919
[RequireComponent(typeof(Camera))]
2020
public abstract class JCS_Camera : MonoBehaviour
@@ -88,6 +88,12 @@ public abstract class JCS_Camera : MonoBehaviour
8888
[SerializeField]
8989
protected bool mSmoothTrack = true;
9090

91+
[Header("- Boundary")]
92+
93+
[Tooltip("The movement boundary.")]
94+
[SerializeField]
95+
protected JCS_Boundary mBoundary = null;
96+
9197
/* Setter & Getter */
9298

9399
public Vector3 PositionOffset { get { return this.mPositionOffset; } set { this.mPositionOffset = value; } }
@@ -106,6 +112,8 @@ public abstract class JCS_Camera : MonoBehaviour
106112

107113
public JCS_TimeType DeltaTimeType { get { return this.mTimeType; } set { this.mTimeType = value; } }
108114

115+
public JCS_Boundary Boundary { get { return this.mBoundary; } set { this.mBoundary = value; } }
116+
109117
/* Functions */
110118

111119
protected virtual void Awake()
@@ -138,6 +146,9 @@ protected virtual void LateUpdate()
138146
#if UNITY_EDITOR
139147
DisplayGameDepthCamera();
140148
#endif
149+
150+
// Do the camera boundaries check!!
151+
DoBoundaries();
141152
}
142153

143154
/// <summary>
@@ -667,6 +678,24 @@ protected virtual void OnResizableIdle()
667678
}
668679
}
669680

681+
/// <summary>
682+
/// 4 boundaries (top, bottom, right, left) that camera should not
683+
/// go through.
684+
///
685+
/// check the boundries and do the trick!
686+
/// </summary>
687+
private void DoBoundaries()
688+
{
689+
if (mBoundary == null)
690+
return;
691+
692+
Bounds bounds = mBoundary.GetBounds();
693+
694+
Vector3 camPos = this.transform.position;
695+
696+
this.transform.position = bounds.ClosestPoint(camPos);
697+
}
698+
670699
/// <summary>
671700
/// Resize the game if screen size changes.
672701
/// </summary>

Assets/_Project/Scenes/Action.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)