Skip to content

Commit e43310e

Browse files
committed
feat(core): add tutorial 40 code
1 parent 55a7d7b commit e43310e

7 files changed

Lines changed: 177 additions & 112 deletions

File tree

Lines changed: 41 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections;
33
using UnityEngine;
44

@@ -9,7 +9,6 @@ public class CameraManager : MonoBehaviour
99
public float zoomSpeed = 30f;
1010
public float altitude = 40f;
1111

12-
public Transform groundTarget;
1312
public bool autoAdaptAltitude;
1413

1514
private Camera _camera;
@@ -22,10 +21,13 @@ public class CameraManager : MonoBehaviour
2221
private int _mouseOnScreenBorder;
2322
private bool _placingBuilding;
2423

25-
public Material minimapIndicatorMaterial;
26-
private float _minimapIndicatorStrokeWidth = 0.1f; // relative to indicator size
27-
private Transform _minimapIndicator;
28-
private Mesh _minimapIndicatorMesh;
24+
private float _minX;
25+
private float _maxX;
26+
private float _minZ;
27+
private float _maxZ;
28+
private Vector3 _camOffset;
29+
private Vector3 _camHalfViewZone;
30+
private float _camMinimapBuffer = 5f;
2931

3032
private void Awake()
3133
{
@@ -35,10 +37,18 @@ private void Awake()
3537
_mouseOnScreenCoroutine = null;
3638
_mouseOnScreenBorder = -1;
3739
_placingBuilding = false;
40+
}
3841

39-
_PrepareMapIndicator();
42+
private void Start()
43+
{
44+
_minX = 0;
45+
_maxX = GameManager.instance.terrainSize;
46+
_minZ = 0;
47+
_maxZ = GameManager.instance.terrainSize;
4048

41-
groundTarget.position = Utils.MiddleOfScreenPointToWorld();
49+
(Vector3 minWorldPoint, Vector3 maxWorldPoint) = Utils.GetCameraWorldBounds();
50+
_camOffset = transform.position - (maxWorldPoint + minWorldPoint) / 2f;
51+
_camHalfViewZone = (maxWorldPoint - minWorldPoint) / 2f + Vector3.one * _camMinimapBuffer;
4252
}
4353

4454
void Update()
@@ -109,42 +119,50 @@ private IEnumerator _SetMouseOnScreenBorder(int borderIndex)
109119

110120
private void _OnMoveCamera(object data)
111121
{
112-
Vector3 pos = (Vector3) data;
113-
float indicatorW = _minimapIndicatorMesh.vertices[1].x - _minimapIndicatorMesh.vertices[0].x;
114-
float indicatorH = _minimapIndicatorMesh.vertices[2].z - _minimapIndicatorMesh.vertices[0].z;
115-
pos.x -= indicatorW / 2f;
116-
pos.z -= indicatorH / 2f;
117-
Vector3 off = transform.position - Utils.MiddleOfScreenPointToWorld();
118-
Vector3 newPos = pos + off;
119-
transform.position = newPos;
122+
Vector3 pos = _FixBounds((Vector3) data);
123+
SetPosition(pos);
120124

121125
if (autoAdaptAltitude)
122126
_FixAltitude();
123-
_ComputeMinimapIndicator(false);
124127
}
125128

126129
private void _TranslateCamera(int dir)
127130
{
128-
if (dir == 0) // top
131+
if (dir == 0 && transform.position.z - _camOffset.z + _camHalfViewZone.z <= _maxZ) // top
129132
transform.Translate(_forwardDir * Time.deltaTime * translationSpeed, Space.World);
130-
else if (dir == 1) // right
133+
else if (dir == 1 && transform.position.x + _camHalfViewZone.x <= _maxX) // right
131134
transform.Translate(transform.right * Time.deltaTime * translationSpeed);
132-
else if (dir == 2) // bottom
135+
else if (dir == 2 && transform.position.z - _camOffset.z - _camHalfViewZone.z >= _minZ) // bottom
133136
transform.Translate(-_forwardDir * Time.deltaTime * translationSpeed, Space.World);
134-
else if (dir == 3) // left
137+
else if (dir == 3 && transform.position.x - _camHalfViewZone.x >= _minX) // left
135138
transform.Translate(-transform.right * Time.deltaTime * translationSpeed);
136139

137140
if (autoAdaptAltitude)
138141
_FixAltitude();
139-
_ComputeMinimapIndicator(false);
140142
}
141143

142144
private void _Zoom(int zoomDir)
143145
{
144146
_camera.orthographicSize += zoomDir * Time.deltaTime * zoomSpeed;
145147
_camera.orthographicSize = Mathf.Clamp(_camera.orthographicSize, 8f, 26f);
146148

147-
_ComputeMinimapIndicator(true);
149+
(Vector3 minWorldPoint, Vector3 maxWorldPoint) = Utils.GetCameraWorldBounds();
150+
_camOffset = transform.position - (maxWorldPoint + minWorldPoint) / 2f;
151+
_camHalfViewZone = (maxWorldPoint - minWorldPoint) / 2f + Vector3.one * _camMinimapBuffer;
152+
153+
// fix bounds
154+
Vector3 pos = Utils.MiddleOfScreenPointToWorld();
155+
pos = _FixBounds(pos);
156+
SetPosition(pos);
157+
}
158+
159+
private Vector3 _FixBounds(Vector3 pos)
160+
{
161+
if (pos.x - _camHalfViewZone.x < _minX) pos.x = _minX + _camHalfViewZone.x;
162+
if (pos.x + _camHalfViewZone.x > _maxX) pos.x = _maxX - _camHalfViewZone.x;
163+
if (pos.z - _camHalfViewZone.z < _minZ) pos.z = _minZ + _camHalfViewZone.z;
164+
if (pos.z + _camHalfViewZone.z > _maxZ) pos.z = _maxZ - _camHalfViewZone.z;
165+
return pos;
148166
}
149167

150168
private void _FixAltitude()
@@ -160,78 +178,8 @@ private void _FixAltitude()
160178
)) transform.position = _hit.point + Vector3.up * altitude;
161179
}
162180

163-
private void _PrepareMapIndicator()
164-
{
165-
GameObject g = new GameObject("MinimapIndicator");
166-
_minimapIndicator = g.transform;
167-
g.layer = 11; // put on "Minimap" layer
168-
_minimapIndicator.position = Vector3.zero;
169-
_minimapIndicatorMesh = _CreateMinimapIndicatorMesh();
170-
MeshFilter mf = g.AddComponent<MeshFilter>();
171-
mf.mesh = _minimapIndicatorMesh;
172-
MeshRenderer mr = g.AddComponent<MeshRenderer>();
173-
mr.material = new Material(minimapIndicatorMaterial);
174-
_ComputeMinimapIndicator(true);
175-
}
176-
177-
private Mesh _CreateMinimapIndicatorMesh()
178-
{
179-
Mesh m = new Mesh();
180-
Vector3[] vertices = new Vector3[] {
181-
Vector3.zero, Vector3.zero, Vector3.zero, Vector3.zero,
182-
Vector3.zero, Vector3.zero, Vector3.zero, Vector3.zero
183-
};
184-
int[] triangles = new int[] {
185-
0, 4, 1, 4, 5, 1,
186-
0, 2, 6, 6, 4, 0,
187-
6, 2, 7, 2, 3, 7,
188-
5, 7, 3, 3, 1, 5
189-
};
190-
m.vertices = vertices;
191-
m.triangles = triangles;
192-
return m;
193-
}
194-
195-
private void _ComputeMinimapIndicator(bool zooming)
196-
{
197-
Vector3 middle = Utils.MiddleOfScreenPointToWorld();
198-
groundTarget.position = middle;
199-
// if zooming: recompute the indicator mesh
200-
if (zooming)
201-
{
202-
Vector3[] viewCorners = Utils.ScreenCornersToWorldPoints();
203-
float w = viewCorners[1].x - viewCorners[0].x;
204-
float h = viewCorners[2].z - viewCorners[0].z;
205-
for (int i = 0; i < 4; i++)
206-
{
207-
viewCorners[i].x -= middle.x;
208-
viewCorners[i].z -= middle.z;
209-
}
210-
Vector3[] innerCorners = new Vector3[]
211-
{
212-
new Vector3(viewCorners[0].x + _minimapIndicatorStrokeWidth * w, 0f, viewCorners[0].z + _minimapIndicatorStrokeWidth * h),
213-
new Vector3(viewCorners[1].x - _minimapIndicatorStrokeWidth * w, 0f, viewCorners[1].z + _minimapIndicatorStrokeWidth * h),
214-
new Vector3(viewCorners[2].x + _minimapIndicatorStrokeWidth * w, 0f, viewCorners[2].z - _minimapIndicatorStrokeWidth * h),
215-
new Vector3(viewCorners[3].x - _minimapIndicatorStrokeWidth * w, 0f, viewCorners[3].z - _minimapIndicatorStrokeWidth * h)
216-
};
217-
Vector3[] allCorners = new Vector3[]
218-
{
219-
viewCorners[0], viewCorners[1], viewCorners[2], viewCorners[3],
220-
innerCorners[0], innerCorners[1], innerCorners[2], innerCorners[3]
221-
};
222-
for (int i = 0; i < 8; i++)
223-
allCorners[i].y = 100f;
224-
_minimapIndicatorMesh.vertices = allCorners;
225-
_minimapIndicatorMesh.RecalculateNormals();
226-
_minimapIndicatorMesh.RecalculateBounds();
227-
}
228-
// move the game object at the center of the main camera screen
229-
_minimapIndicator.position = middle;
230-
}
231-
232181
public void SetPosition(Vector3 pos)
233182
{
234183
transform.position = pos - _distance * transform.forward;
235-
_ComputeMinimapIndicator(false);
236184
}
237185
}

Assets/Scripts/Managers/CameraManager.cs.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Scripts/Managers/GameManager.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Collections;
1+
using System.Collections;
22
using System.Collections.Generic;
33
using UnityEngine;
44
using UnityEngine.AI;
@@ -15,6 +15,10 @@ public class GameManager : MonoBehaviour
1515
public GameInputParameters gameInputParameters;
1616
public GameObject fov;
1717

18+
[Header("Minimap")]
19+
public Collider mapWrapperCollider;
20+
public int terrainSize = 200;
21+
1822
[HideInInspector]
1923
public bool gameIsPaused;
2024
public Vector3 startPosition;
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/* Adapted from:
2+
* https://medium.com/@alessandrovalcepina/creating-a-rts-like-minimap-with-unity-9cd578dc4522 */
3+
using UnityEngine;
4+
5+
[RequireComponent(typeof(Camera))]
6+
public class MinimapManager : MonoBehaviour
7+
{
8+
private static Material _indicatorMat;
9+
10+
public float lineWidth;
11+
12+
private Camera _minimapCam;
13+
14+
private void Start()
15+
{
16+
if (_indicatorMat == null)
17+
_indicatorMat = new Material(Shader.Find("Sprites/Default"));
18+
_minimapCam = GetComponent<Camera>();
19+
}
20+
21+
public void OnPostRender()
22+
{
23+
(Vector3 minWorldPoint, Vector3 maxWorldPoint) = Utils.GetCameraWorldBounds();
24+
Vector3 minViewportPoint = _minimapCam.WorldToViewportPoint(minWorldPoint);
25+
Vector3 maxViewportPoint = _minimapCam.WorldToViewportPoint(maxWorldPoint);
26+
float minX = minViewportPoint.x;
27+
float minY = minViewportPoint.y;
28+
float maxX = maxViewportPoint.x;
29+
float maxY = maxViewportPoint.y;
30+
31+
GL.PushMatrix();
32+
{
33+
_indicatorMat.SetPass(0);
34+
GL.LoadOrtho();
35+
36+
GL.Begin(GL.QUADS);
37+
GL.Color(new Color(1f, 1f, 0.85f));
38+
{
39+
GL.Vertex(new Vector3(minX, minY + lineWidth, 0));
40+
GL.Vertex(new Vector3(minX, minY - lineWidth, 0));
41+
GL.Vertex(new Vector3(maxX, minY - lineWidth, 0));
42+
GL.Vertex(new Vector3(maxX, minY + lineWidth, 0));
43+
44+
GL.Vertex(new Vector3(minX + lineWidth, minY, 0));
45+
GL.Vertex(new Vector3(minX - lineWidth, minY, 0));
46+
GL.Vertex(new Vector3(minX - lineWidth, maxY, 0));
47+
GL.Vertex(new Vector3(minX + lineWidth, maxY, 0));
48+
49+
GL.Vertex(new Vector3(minX, maxY + lineWidth, 0));
50+
GL.Vertex(new Vector3(minX, maxY - lineWidth, 0));
51+
GL.Vertex(new Vector3(maxX, maxY - lineWidth, 0));
52+
GL.Vertex(new Vector3(maxX, maxY + lineWidth, 0));
53+
54+
GL.Vertex(new Vector3(maxX + lineWidth, minY, 0));
55+
GL.Vertex(new Vector3(maxX - lineWidth, minY, 0));
56+
GL.Vertex(new Vector3(maxX - lineWidth, maxY, 0));
57+
GL.Vertex(new Vector3(maxX + lineWidth, maxY, 0));
58+
}
59+
GL.End();
60+
}
61+
GL.PopMatrix();
62+
}
63+
64+
}

Assets/Scripts/Managers/MinimapManager.cs.meta

Lines changed: 11 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)