From 9a00d03f80eee94c9fe52f832a0127d9375ad375 Mon Sep 17 00:00:00 2001 From: Jakob Stendahl Date: Fri, 29 Sep 2017 01:12:27 +0200 Subject: Bytt ut eksempel med starten på et Tower Defence spill (#2) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Startet på TD :) * Slett tulleprosjekt * Added enemy as a prefab * Fiksa stygge skygga * Fiksa stygge skygga på android * Tweaked design, cleanup of enemy script * Made enemy spawner, plus changed enemy color to red * Added pan and zoom (Pan is ugly still) * Veit itj ka som e endra :) --- td/Assets/Materials.meta | 9 ++ td/Assets/Materials/Dirt.mat | Bin 0 -> 5012 bytes td/Assets/Materials/Dirt.mat.meta | 9 ++ td/Assets/Materials/Enemy one.mat | Bin 0 -> 5056 bytes td/Assets/Materials/Enemy one.mat.meta | 9 ++ td/Assets/Materials/Grass.mat | Bin 0 -> 5016 bytes td/Assets/Materials/Grass.mat.meta | 9 ++ td/Assets/Prefabs.meta | 9 ++ td/Assets/Prefabs/Enemy.prefab | Bin 0 -> 7752 bytes td/Assets/Prefabs/Enemy.prefab.meta | 9 ++ td/Assets/Prefabs/Grass-dirt.prefab | Bin 0 -> 7776 bytes td/Assets/Prefabs/Grass-dirt.prefab.meta | 9 ++ td/Assets/Prefabs/Path straigth.prefab | Bin 0 -> 9096 bytes td/Assets/Prefabs/Path straigth.prefab.meta | 9 ++ td/Assets/Prefabs/Path turn.prefab | Bin 0 -> 10392 bytes td/Assets/Prefabs/Path turn.prefab.meta | 9 ++ td/Assets/Prefabs/grassPart.prefab | Bin 0 -> 6848 bytes td/Assets/Prefabs/grassPart.prefab.meta | 9 ++ td/Assets/Scenes.meta | 9 ++ td/Assets/Scenes/Level 1.unity | Bin 0 -> 47656 bytes td/Assets/Scenes/Level 1.unity.meta | 8 ++ td/Assets/Scripts.meta | 9 ++ td/Assets/Scripts/Enemy.cs | 39 ++++++++ td/Assets/Scripts/Enemy.cs.meta | 12 +++ td/Assets/Scripts/EnemySpawner.cs | 27 ++++++ td/Assets/Scripts/EnemySpawner.cs.meta | 12 +++ td/Assets/Scripts/PinchZoom.cs | 144 ++++++++++++++++++++++++++++ td/Assets/Scripts/PinchZoom.cs.meta | 12 +++ td/Assets/Scripts/cameraHandler.cs | 139 +++++++++++++++++++++++++++ td/Assets/Scripts/cameraHandler.cs.meta | 12 +++ 30 files changed, 513 insertions(+) create mode 100644 td/Assets/Materials.meta create mode 100644 td/Assets/Materials/Dirt.mat create mode 100644 td/Assets/Materials/Dirt.mat.meta create mode 100644 td/Assets/Materials/Enemy one.mat create mode 100644 td/Assets/Materials/Enemy one.mat.meta create mode 100644 td/Assets/Materials/Grass.mat create mode 100644 td/Assets/Materials/Grass.mat.meta create mode 100644 td/Assets/Prefabs.meta create mode 100644 td/Assets/Prefabs/Enemy.prefab create mode 100644 td/Assets/Prefabs/Enemy.prefab.meta create mode 100644 td/Assets/Prefabs/Grass-dirt.prefab create mode 100644 td/Assets/Prefabs/Grass-dirt.prefab.meta create mode 100644 td/Assets/Prefabs/Path straigth.prefab create mode 100644 td/Assets/Prefabs/Path straigth.prefab.meta create mode 100644 td/Assets/Prefabs/Path turn.prefab create mode 100644 td/Assets/Prefabs/Path turn.prefab.meta create mode 100644 td/Assets/Prefabs/grassPart.prefab create mode 100644 td/Assets/Prefabs/grassPart.prefab.meta create mode 100644 td/Assets/Scenes.meta create mode 100644 td/Assets/Scenes/Level 1.unity create mode 100644 td/Assets/Scenes/Level 1.unity.meta create mode 100644 td/Assets/Scripts.meta create mode 100644 td/Assets/Scripts/Enemy.cs create mode 100644 td/Assets/Scripts/Enemy.cs.meta create mode 100644 td/Assets/Scripts/EnemySpawner.cs create mode 100644 td/Assets/Scripts/EnemySpawner.cs.meta create mode 100644 td/Assets/Scripts/PinchZoom.cs create mode 100644 td/Assets/Scripts/PinchZoom.cs.meta create mode 100644 td/Assets/Scripts/cameraHandler.cs create mode 100644 td/Assets/Scripts/cameraHandler.cs.meta (limited to 'td/Assets') diff --git a/td/Assets/Materials.meta b/td/Assets/Materials.meta new file mode 100644 index 0000000..cfacdab --- /dev/null +++ b/td/Assets/Materials.meta @@ -0,0 +1,9 @@ +fileFormatVersion: 2 +guid: e5b8030d0db574d67b71059df1b02bb3 +folderAsset: yes +timeCreated: 1506602899 +licenseType: Free +DefaultImporter: + userData: + assetBundleName: + assetBundleVariant: diff --git a/td/Assets/Materials/Dirt.mat b/td/Assets/Materials/Dirt.mat new file mode 100644 index 0000000..a493af1 Binary files /dev/null and b/td/Assets/Materials/Dirt.mat differ diff --git a/td/Assets/Materials/Dirt.mat.meta b/td/Assets/Materials/Dirt.mat.meta new file mode 100644 index 0000000..b15b482 --- /dev/null +++ b/td/Assets/Materials/Dirt.mat.meta @@ -0,0 +1,9 @@ +fileFormatVersion: 2 +guid: d42374425bef54eea804d401e3bcc9c6 +timeCreated: 1506603097 +licenseType: Free +NativeFormatImporter: + mainObjectFileID: 2100000 + userData: + assetBundleName: + assetBundleVariant: diff --git a/td/Assets/Materials/Enemy one.mat b/td/Assets/Materials/Enemy one.mat new file mode 100644 index 0000000..22b9592 Binary files /dev/null and b/td/Assets/Materials/Enemy one.mat differ diff --git a/td/Assets/Materials/Enemy one.mat.meta b/td/Assets/Materials/Enemy one.mat.meta new file mode 100644 index 0000000..ba04a9c --- /dev/null +++ b/td/Assets/Materials/Enemy one.mat.meta @@ -0,0 +1,9 @@ +fileFormatVersion: 2 +guid: 3880498a088cc48f69fe11e905a82185 +timeCreated: 1506604738 +licenseType: Free +NativeFormatImporter: + mainObjectFileID: 2100000 + userData: + assetBundleName: + assetBundleVariant: diff --git a/td/Assets/Materials/Grass.mat b/td/Assets/Materials/Grass.mat new file mode 100644 index 0000000..061cd3a Binary files /dev/null and b/td/Assets/Materials/Grass.mat differ diff --git a/td/Assets/Materials/Grass.mat.meta b/td/Assets/Materials/Grass.mat.meta new file mode 100644 index 0000000..4f9c01e --- /dev/null +++ b/td/Assets/Materials/Grass.mat.meta @@ -0,0 +1,9 @@ +fileFormatVersion: 2 +guid: ed001c552b4734cbfaf2a11dbf59102c +timeCreated: 1506602894 +licenseType: Free +NativeFormatImporter: + mainObjectFileID: 2100000 + userData: + assetBundleName: + assetBundleVariant: diff --git a/td/Assets/Prefabs.meta b/td/Assets/Prefabs.meta new file mode 100644 index 0000000..0eccb88 --- /dev/null +++ b/td/Assets/Prefabs.meta @@ -0,0 +1,9 @@ +fileFormatVersion: 2 +guid: a6f0f2d72a07d47468cf9a43dc063c13 +folderAsset: yes +timeCreated: 1506603186 +licenseType: Free +DefaultImporter: + userData: + assetBundleName: + assetBundleVariant: diff --git a/td/Assets/Prefabs/Enemy.prefab b/td/Assets/Prefabs/Enemy.prefab new file mode 100644 index 0000000..e2f8a96 Binary files /dev/null and b/td/Assets/Prefabs/Enemy.prefab differ diff --git a/td/Assets/Prefabs/Enemy.prefab.meta b/td/Assets/Prefabs/Enemy.prefab.meta new file mode 100644 index 0000000..f39477f --- /dev/null +++ b/td/Assets/Prefabs/Enemy.prefab.meta @@ -0,0 +1,9 @@ +fileFormatVersion: 2 +guid: f46c18e8539e0474e9e6e164212d57e3 +timeCreated: 1506607135 +licenseType: Free +NativeFormatImporter: + mainObjectFileID: 100100000 + userData: + assetBundleName: + assetBundleVariant: diff --git a/td/Assets/Prefabs/Grass-dirt.prefab b/td/Assets/Prefabs/Grass-dirt.prefab new file mode 100644 index 0000000..5952387 Binary files /dev/null and b/td/Assets/Prefabs/Grass-dirt.prefab differ diff --git a/td/Assets/Prefabs/Grass-dirt.prefab.meta b/td/Assets/Prefabs/Grass-dirt.prefab.meta new file mode 100644 index 0000000..0c2256d --- /dev/null +++ b/td/Assets/Prefabs/Grass-dirt.prefab.meta @@ -0,0 +1,9 @@ +fileFormatVersion: 2 +guid: dceb27e95f4d342f4abed553a9a0f5d2 +timeCreated: 1506603189 +licenseType: Free +NativeFormatImporter: + mainObjectFileID: 100100000 + userData: + assetBundleName: + assetBundleVariant: diff --git a/td/Assets/Prefabs/Path straigth.prefab b/td/Assets/Prefabs/Path straigth.prefab new file mode 100644 index 0000000..758af59 Binary files /dev/null and b/td/Assets/Prefabs/Path straigth.prefab differ diff --git a/td/Assets/Prefabs/Path straigth.prefab.meta b/td/Assets/Prefabs/Path straigth.prefab.meta new file mode 100644 index 0000000..875d25e --- /dev/null +++ b/td/Assets/Prefabs/Path straigth.prefab.meta @@ -0,0 +1,9 @@ +fileFormatVersion: 2 +guid: a27ee2cf3aa6840dc98f35b567f52e2f +timeCreated: 1506603963 +licenseType: Free +NativeFormatImporter: + mainObjectFileID: 100100000 + userData: + assetBundleName: + assetBundleVariant: diff --git a/td/Assets/Prefabs/Path turn.prefab b/td/Assets/Prefabs/Path turn.prefab new file mode 100644 index 0000000..62e392e Binary files /dev/null and b/td/Assets/Prefabs/Path turn.prefab differ diff --git a/td/Assets/Prefabs/Path turn.prefab.meta b/td/Assets/Prefabs/Path turn.prefab.meta new file mode 100644 index 0000000..ea595d4 --- /dev/null +++ b/td/Assets/Prefabs/Path turn.prefab.meta @@ -0,0 +1,9 @@ +fileFormatVersion: 2 +guid: 5e7563b197d6342e880523cb297bf4d3 +timeCreated: 1506603961 +licenseType: Free +NativeFormatImporter: + mainObjectFileID: 100100000 + userData: + assetBundleName: + assetBundleVariant: diff --git a/td/Assets/Prefabs/grassPart.prefab b/td/Assets/Prefabs/grassPart.prefab new file mode 100644 index 0000000..b525984 Binary files /dev/null and b/td/Assets/Prefabs/grassPart.prefab differ diff --git a/td/Assets/Prefabs/grassPart.prefab.meta b/td/Assets/Prefabs/grassPart.prefab.meta new file mode 100644 index 0000000..0c263b8 --- /dev/null +++ b/td/Assets/Prefabs/grassPart.prefab.meta @@ -0,0 +1,9 @@ +fileFormatVersion: 2 +guid: d2995460fd67949fba6e21a4fd492692 +timeCreated: 1506603208 +licenseType: Free +NativeFormatImporter: + mainObjectFileID: 100100000 + userData: + assetBundleName: + assetBundleVariant: diff --git a/td/Assets/Scenes.meta b/td/Assets/Scenes.meta new file mode 100644 index 0000000..67bfa91 --- /dev/null +++ b/td/Assets/Scenes.meta @@ -0,0 +1,9 @@ +fileFormatVersion: 2 +guid: 4d35142e309a043c39eeb572d3a4ed13 +folderAsset: yes +timeCreated: 1506602782 +licenseType: Free +DefaultImporter: + userData: + assetBundleName: + assetBundleVariant: diff --git a/td/Assets/Scenes/Level 1.unity b/td/Assets/Scenes/Level 1.unity new file mode 100644 index 0000000..d48cc6e Binary files /dev/null and b/td/Assets/Scenes/Level 1.unity differ diff --git a/td/Assets/Scenes/Level 1.unity.meta b/td/Assets/Scenes/Level 1.unity.meta new file mode 100644 index 0000000..ffdc94c --- /dev/null +++ b/td/Assets/Scenes/Level 1.unity.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 0a99bae9617ac475e8cf86569dee83bb +timeCreated: 1506602806 +licenseType: Free +DefaultImporter: + userData: + assetBundleName: + assetBundleVariant: diff --git a/td/Assets/Scripts.meta b/td/Assets/Scripts.meta new file mode 100644 index 0000000..92f3327 --- /dev/null +++ b/td/Assets/Scripts.meta @@ -0,0 +1,9 @@ +fileFormatVersion: 2 +guid: b87d39bb5cbe5414b8af6c81e2696422 +folderAsset: yes +timeCreated: 1506602795 +licenseType: Free +DefaultImporter: + userData: + assetBundleName: + assetBundleVariant: diff --git a/td/Assets/Scripts/Enemy.cs b/td/Assets/Scripts/Enemy.cs new file mode 100644 index 0000000..7273370 --- /dev/null +++ b/td/Assets/Scripts/Enemy.cs @@ -0,0 +1,39 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Enemy : MonoBehaviour { + + public float speed; + public float initialHp; + public Transform pathWay; + + List waypoints = new List(); + Vector3 waypointPos; + int waypointNum = -1; // Using minus one so that first addition returns 0, first element in array + + void Start () { + foreach (Transform child in pathWay) { + waypoints.Add (child.position); + } + } + + void Update () { + updateWaypoint (); + + float transformStep = speed * Time.deltaTime; + transform.position = Vector3.MoveTowards (transform.position, waypointPos, transformStep); + + if (waypointNum == waypoints.Count - 1) { + Destroy (gameObject); + } + } + + void updateWaypoint() { + if ( (transform.position == waypointPos && waypointNum < waypoints.Count - 1) || waypointNum == -1) { + waypointNum++; + waypointPos = new Vector3 (waypoints [waypointNum].x, 0.604f, waypoints [waypointNum].z); + } + } + +} diff --git a/td/Assets/Scripts/Enemy.cs.meta b/td/Assets/Scripts/Enemy.cs.meta new file mode 100644 index 0000000..73d0664 --- /dev/null +++ b/td/Assets/Scripts/Enemy.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 32a95d77f1e1f481886733800e38e797 +timeCreated: 1506604935 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/td/Assets/Scripts/EnemySpawner.cs b/td/Assets/Scripts/EnemySpawner.cs new file mode 100644 index 0000000..eb0dd43 --- /dev/null +++ b/td/Assets/Scripts/EnemySpawner.cs @@ -0,0 +1,27 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class EnemySpawner : MonoBehaviour { + + public Enemy enemyPrefab; + public Transform pathWay; + public Transform gameWorld; + + int wave; + int next = 1; + int n = 0; + + void Update () { + n++; + + if (n == next) { + n = 0; + next = (int)Random.Range (50, 400); + + Enemy newEnemy = Instantiate (enemyPrefab, new Vector3(0, 0, 0), Quaternion.identity, gameWorld); + newEnemy.GetComponent ().pathWay = pathWay; + newEnemy.GetComponent ().speed = Random.Range (0.3f, 1.2f); + } + } +} diff --git a/td/Assets/Scripts/EnemySpawner.cs.meta b/td/Assets/Scripts/EnemySpawner.cs.meta new file mode 100644 index 0000000..3fe504e --- /dev/null +++ b/td/Assets/Scripts/EnemySpawner.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 3e57c25f0405b40cab5c30b2c92671ae +timeCreated: 1506633566 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/td/Assets/Scripts/PinchZoom.cs b/td/Assets/Scripts/PinchZoom.cs new file mode 100644 index 0000000..b3e6d22 --- /dev/null +++ b/td/Assets/Scripts/PinchZoom.cs @@ -0,0 +1,144 @@ +using UnityEngine; +using System.Collections; + +public class PinchZoom : MonoBehaviour +{ + /* Dette vil bare funke for et ortografisk kamera! */ + + private static readonly float PanSpeed = 20f; + private static readonly float ZoomSpeedTouch = 0.1f; + private static readonly float ZoomSpeedMouse = .5f; + + public static readonly float[] BoundsX = new float[]{-10f, 5f}; + public static readonly float[] BoundsZ = new float[]{-18f, -4f}; + public static readonly float[] ZoomBounds = new float[]{10f, 85f}; + + private Camera cam; + + private bool panActive; + private Vector3 lastPanPosition; + private int panFingerId; // Touch mode only + + private bool zoomActive; + private Vector2[] lastZoomPositions; // Touch mode only + + void Awake() { + cam = GetComponent(); + + #if UNITY_ANDROID || UNITY_IOS + cam.fieldOfView = 60f; + #endif + } + + void Update() { + // If there's an open menu, or the clicker is being pressed, ignore the touch. + /* + if (GameManager.Instance.MenuManager.HasOpenMenu || GameManager.Instance.BitSpawnManager.IsSpawningBits) { + return; + }*/ + + if (Input.touchSupported && Application.platform != RuntimePlatform.WebGLPlayer) { + HandleTouch(); + } else { + HandleMouse(); + } + } + + void HandleTouch() { + switch(Input.touchCount) { + + case 1: // Panning + zoomActive = false; + + // If the touch began, capture its position and its finger ID. + // Otherwise, if the finger ID of the touch doesn't match, skip it. + Touch touch = Input.GetTouch(0); + if (touch.phase == TouchPhase.Began) { + lastPanPosition = touch.position; + panFingerId = touch.fingerId; + panActive = true; + } else if (touch.fingerId == panFingerId && touch.phase == TouchPhase.Moved) { + PanCamera(touch.position); + } + break; + + case 2: // Zooming + panActive = false; + + Vector2[] newPositions = new Vector2[]{Input.GetTouch(0).position, Input.GetTouch(1).position}; + if (!zoomActive) { + lastZoomPositions = newPositions; + zoomActive = true; + } else { + // Zoom based on the distance between the new positions compared to the + // distance between the previous positions. + float newDistance = Vector2.Distance(newPositions[0], newPositions[1]); + float oldDistance = Vector2.Distance(lastZoomPositions[0], lastZoomPositions[1]); + float offset = newDistance - oldDistance; + + ZoomCamera(offset, ZoomSpeedTouch); + + lastZoomPositions = newPositions; + } + break; + + default: + panActive = false; + zoomActive = false; + break; + } + } + + void HandleMouse() { + // On mouse down, capture it's position. + // On mouse up, disable panning. + // If there is no mouse being pressed, do nothing. + if (Input.GetMouseButtonDown(0)) { + panActive = true; + lastPanPosition = Input.mousePosition; + } else if (Input.GetMouseButtonUp(0)) { + panActive = false; + } else if (Input.GetMouseButton(0)) { + PanCamera(Input.mousePosition); + } + + // Check for scrolling to zoom the camera + float scroll = Input.GetAxis("Mouse ScrollWheel"); + zoomActive = true; + ZoomCamera(scroll, ZoomSpeedMouse); + zoomActive = false; + } + + void PanCamera(Vector3 newPanPosition) { + if (!panActive) { + return; + } + + // Translate the camera position based on the new input position + Vector3 offset = cam.ScreenToViewportPoint(lastPanPosition - newPanPosition); + Vector3 move = new Vector3(offset.x * PanSpeed, 0, offset.y * PanSpeed); + transform.Translate(move, Space.World); + ClampToBounds(); + + lastPanPosition = newPanPosition; + } + + void ZoomCamera(float offset, float speed) { + if (!zoomActive || offset == 0) { + return; + } + + cam.fieldOfView = Mathf.Clamp(cam.fieldOfView - (offset * speed), ZoomBounds[0], ZoomBounds[1]); + } + + void ClampToBounds() { + Vector3 pos = transform.position; + pos.x = Mathf.Clamp(transform.position.x, BoundsX[0], BoundsX[1]); + pos.z = Mathf.Clamp(transform.position.z, BoundsZ[0], BoundsZ[1]); + + transform.position = pos; + } + + + +} \ No newline at end of file diff --git a/td/Assets/Scripts/PinchZoom.cs.meta b/td/Assets/Scripts/PinchZoom.cs.meta new file mode 100644 index 0000000..6303bbc --- /dev/null +++ b/td/Assets/Scripts/PinchZoom.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 3770bdc9b6c954c1782333bdf04c01c6 +timeCreated: 1506636352 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/td/Assets/Scripts/cameraHandler.cs b/td/Assets/Scripts/cameraHandler.cs new file mode 100644 index 0000000..a4aa660 --- /dev/null +++ b/td/Assets/Scripts/cameraHandler.cs @@ -0,0 +1,139 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class cameraHandler : MonoBehaviour { + + public float PanSpeed = 20f; + public float ZoomSpeedTouch = 0.1f; + public float ZoomSpeedMouse = .5f; + + public static readonly float[] BoundsX = new float[]{-10f, 10f}; + public static readonly float[] BoundsY = new float[]{-5f, 10f}; + public static readonly float[] BoundsZ = new float[]{-8f, 8f}; + public static readonly float[] ZoomBounds = new float[]{1f, 5f}; + + private Camera cam; + + private bool panActive; + private Vector3 lastPanPosition; + private int panFingerId; // Touch mode only + + private bool zoomActive; + private Vector2[] lastZoomPositions; // Touch mode only + + void Awake() { + cam = GetComponent(); + } + + void Update() { + // If there's an open menu, or the clicker is being pressed, ignore the touch. + /* + if (GameManager.Instance.MenuManager.HasOpenMenu || GameManager.Instance.BitSpawnManager.IsSpawningBits) { + return; + }*/ + + if (Input.touchSupported && Application.platform != RuntimePlatform.WebGLPlayer) { + HandleTouch(); + } else { + //HandleMouse(); + } + HandleTouch (); + } + + void HandleTouch() { + switch(Input.touchCount) { + + case 1: // Panning + zoomActive = false; + + // If the touch began, capture its position and its finger ID. + // Otherwise, if the finger ID of the touch doesn't match, skip it. + Touch touch = Input.GetTouch(0); + if (touch.phase == TouchPhase.Began) { + lastPanPosition = touch.position; + panFingerId = touch.fingerId; + panActive = true; + } else if (touch.fingerId == panFingerId && touch.phase == TouchPhase.Moved) { + PanCamera(touch.position); + } + break; + + case 2: // Zooming + panActive = false; + + Vector2[] newPositions = new Vector2[]{Input.GetTouch(0).position, Input.GetTouch(1).position}; + if (!zoomActive) { + lastZoomPositions = newPositions; + zoomActive = true; + } else { + // Zoom based on the distance between the new positions compared to the + // distance between the previous positions. + float newDistance = Vector2.Distance(newPositions[0], newPositions[1]); + float oldDistance = Vector2.Distance(lastZoomPositions[0], lastZoomPositions[1]); + float offset = newDistance - oldDistance; + + ZoomCamera(offset, ZoomSpeedTouch); + + lastZoomPositions = newPositions; + } + break; + + default: + panActive = false; + zoomActive = false; + break; + } + } + + void HandleMouse() { + // On mouse down, capture it's position. + // On mouse up, disable panning. + // If there is no mouse being pressed, do nothing. + if (Input.GetMouseButtonDown(0)) { + panActive = true; + lastPanPosition = Input.mousePosition; + } else if (Input.GetMouseButtonUp(0)) { + panActive = false; + } else if (Input.GetMouseButton(0)) { + PanCamera(Input.mousePosition); + } + + // Check for scrolling to zoom the camera + float scroll = Input.GetAxis("Mouse ScrollWheel"); + zoomActive = true; + ZoomCamera(scroll, ZoomSpeedMouse); + zoomActive = false; + } + + void ZoomCamera(float offset, float speed) { + if (!zoomActive || offset == 0) { + return; + } + + cam.orthographicSize = Mathf.Clamp(cam.orthographicSize - (offset * speed), ZoomBounds[0], ZoomBounds[1]); + } + + void PanCamera(Vector3 newPanPosition) { + if (!panActive) { + return; + } + + // Translate the camera position based on the new input position + Vector3 offset = cam.ScreenToViewportPoint(lastPanPosition - newPanPosition); + Vector3 move = new Vector3(offset.x * PanSpeed, offset.y * PanSpeed, 0f); + transform.Translate(move, Space.World); + ClampToBounds(); + + lastPanPosition = newPanPosition; + } + + void ClampToBounds() { + Vector3 pos = transform.position; + pos.x = Mathf.Clamp(transform.position.x, BoundsX[0], BoundsX[1]); + pos.z = Mathf.Clamp(transform.position.z, BoundsZ[0], BoundsZ[1]); + + transform.position = pos; + } + +} diff --git a/td/Assets/Scripts/cameraHandler.cs.meta b/td/Assets/Scripts/cameraHandler.cs.meta new file mode 100644 index 0000000..569bdd5 --- /dev/null +++ b/td/Assets/Scripts/cameraHandler.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 3a9cb2108387b493a85293ad9842ad3c +timeCreated: 1506636952 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: -- cgit v1.2.3