Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
S
Spieleentwicklung
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Adrian Paschkowski
Spieleentwicklung
Commits
8be4e507
Commit
8be4e507
authored
4 years ago
by
Adrian Paschkowski
Browse files
Options
Downloads
Patches
Plain Diff
Don't create new InsetEdges every frame
parent
f2ff74d3
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
Assets/Scripts/BaseCameraController.cs
+41
-13
41 additions, 13 deletions
Assets/Scripts/BaseCameraController.cs
with
41 additions
and
13 deletions
Assets/Scripts/BaseCameraController.cs
+
41
−
13
View file @
8be4e507
...
@@ -20,6 +20,7 @@ public class BaseCameraController : MonoBehaviour
...
@@ -20,6 +20,7 @@ public class BaseCameraController : MonoBehaviour
new
BoxCollider2D
collider
;
new
BoxCollider2D
collider
;
new
Rigidbody2D
rigidbody
;
new
Rigidbody2D
rigidbody
;
InsetCameraEdges
mouseMovementEdges
;
Interpolator
zoomInterpolator
;
Interpolator
zoomInterpolator
;
Interpolator
moveXInterpolator
;
Interpolator
moveXInterpolator
;
Interpolator
moveYInterpolator
;
Interpolator
moveYInterpolator
;
...
@@ -29,12 +30,19 @@ public class BaseCameraController : MonoBehaviour
...
@@ -29,12 +30,19 @@ public class BaseCameraController : MonoBehaviour
get
=>
camera
.
orthographicSize
/
maxZoom
;
get
=>
camera
.
orthographicSize
/
maxZoom
;
}
}
public
void
PlayGlobalAudioClip
(
AudioClip
clip
,
float
volume
=
1f
)
{
audio
.
PlayOneShot
(
clip
,
volume
);
}
void
Awake
()
void
Awake
()
{
{
camera
=
GetComponent
<
Camera
>();
camera
=
GetComponent
<
Camera
>();
audio
=
GetComponent
<
AudioSource
>();
audio
=
GetComponent
<
AudioSource
>();
collider
=
GetComponent
<
BoxCollider2D
>();
collider
=
GetComponent
<
BoxCollider2D
>();
rigidbody
=
GetComponent
<
Rigidbody2D
>();
rigidbody
=
GetComponent
<
Rigidbody2D
>();
mouseMovementEdges
=
new
InsetCameraEdges
(
camera
);
zoomInterpolator
=
new
Interpolator
(
1
/
zoomSpeed
,
camera
.
orthographicSize
,
camera
.
orthographicSize
,
minZoom
,
maxZoom
);
zoomInterpolator
=
new
Interpolator
(
1
/
zoomSpeed
,
camera
.
orthographicSize
,
camera
.
orthographicSize
,
minZoom
,
maxZoom
);
moveXInterpolator
=
new
Interpolator
(
1
/
moveSpeed
);
moveXInterpolator
=
new
Interpolator
(
1
/
moveSpeed
);
moveYInterpolator
=
new
Interpolator
(
1
/
moveSpeed
);
moveYInterpolator
=
new
Interpolator
(
1
/
moveSpeed
);
...
@@ -59,9 +67,9 @@ public class BaseCameraController : MonoBehaviour
...
@@ -59,9 +67,9 @@ public class BaseCameraController : MonoBehaviour
if
(
inputVector
.
magnitude
==
0
)
if
(
inputVector
.
magnitude
==
0
)
{
{
var
mouseViewportPos
=
camera
.
ScreenToWorldPoint
(
Input
.
mousePosition
);
var
mouseViewportPos
=
camera
.
ScreenToWorldPoint
(
Input
.
mousePosition
);
var
mouse
Edges
=
new
InsetEdges
(
camera
,
mouseEdgeDetectionBoxSize
*
zoomRatio
);
mouse
MovementEdges
.
RecalculateBounds
(
mouseEdgeDetectionBoxSize
*
zoomRatio
);
mouseEdges
.
DrawDebug
();
mouse
Movement
Edges
.
DrawDebug
();
inputVector
=
mouseEdges
.
GetOutOfBoundsDirection
(
mouseViewportPos
);
inputVector
=
mouse
Movement
Edges
.
GetOutOfBoundsDirection
(
mouseViewportPos
);
}
}
moveXInterpolator
.
targetValue
=
inputVector
.
x
;
moveXInterpolator
.
targetValue
=
inputVector
.
x
;
moveYInterpolator
.
targetValue
=
inputVector
.
y
;
moveYInterpolator
.
targetValue
=
inputVector
.
y
;
...
@@ -88,29 +96,49 @@ public class BaseCameraController : MonoBehaviour
...
@@ -88,29 +96,49 @@ public class BaseCameraController : MonoBehaviour
void
OnDrawGizmosSelected
()
void
OnDrawGizmosSelected
()
{
{
if
(
camera
==
null
)
if
(
camera
==
null
||
mouseMovementEdges
==
null
)
{
{
camera
=
GetComponent
<
Camera
>();
camera
=
GetComponent
<
Camera
>();
mouseMovementEdges
=
new
InsetCameraEdges
(
camera
);
}
}
var
mouse
Edges
=
new
InsetEdges
(
camera
,
mouseEdgeDetectionBoxSize
*
zoomRatio
);
mouse
MovementEdges
.
RecalculateBounds
(
mouseEdgeDetectionBoxSize
*
zoomRatio
);
mouseEdges
.
DrawGizmos
();
mouse
Movement
Edges
.
DrawGizmos
();
}
}
p
ublic
void
PlayGlobalAudioClip
(
AudioClip
clip
,
float
volume
=
1f
)
p
rivate
class
InsetCameraEdges
{
{
audio
.
PlayOneShot
(
clip
,
volume
)
;
private
Camera
camera
;
}
private
Vector3
insetVector
;
private
class
InsetEdges
{
public
Vector3
bottomLeft
;
public
Vector3
bottomLeft
;
public
Vector3
topRight
;
public
Vector3
topRight
;
private
Vector3
bottomRight
;
private
Vector3
bottomRight
;
private
Vector3
topLeft
;
private
Vector3
topLeft
;
public
InsetEdges
(
Camera
camera
,
float
inset
)
public
InsetCameraEdges
(
Camera
camera
)
:
this
(
camera
,
0
)
{
}
public
InsetCameraEdges
(
Camera
camera
,
float
inset
)
:
this
(
camera
,
inset
,
inset
)
{
}
public
InsetCameraEdges
(
Camera
camera
,
float
insetX
,
float
insetY
)
{
this
.
camera
=
camera
;
RecalculateBounds
(
insetX
,
insetY
);
}
public
void
RecalculateBounds
(
float
newInset
)
{
RecalculateBounds
(
newInset
,
newInset
);
}
public
void
RecalculateBounds
(
float
newInsetX
,
float
newInsetY
)
{
insetVector
=
new
Vector3
(
newInsetX
,
newInsetY
);
RecalculateBounds
();
}
public
void
RecalculateBounds
()
{
{
var
insetVector
=
new
Vector3
(
inset
,
inset
);
// TODO this would be way better if it would just use viewport positions
bottomLeft
=
camera
.
ViewportToWorldPoint
(
Vector3
.
zero
)
+
insetVector
;
bottomLeft
=
camera
.
ViewportToWorldPoint
(
Vector3
.
zero
)
+
insetVector
;
topRight
=
camera
.
ViewportToWorldPoint
(
Vector3
.
one
)
-
insetVector
;
topRight
=
camera
.
ViewportToWorldPoint
(
Vector3
.
one
)
-
insetVector
;
bottomLeft
.
z
=
0
;
bottomLeft
.
z
=
0
;
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment