From 2beed4b2bd54d052b4093a02aebb612e7e9d374f Mon Sep 17 00:00:00 2001 From: fannbrian Date: Sun, 28 Feb 2016 15:49:55 -0800 Subject: [PATCH] +Camera System -Added a new camera system. -When you enter a room the camera will stop following the player and zoom out to see the entire room. The camera will remain stationary until the player leaves the room where it will begin following the player again. -The Pan Button has also been changed to zoom back in for the duration of the pan. --- Assets/Graphics.meta | 9 --- Assets/Scenes/GenerationTestScene.unity | Bin 25676 -> 26884 bytes Assets/Scripts/CameraController.cs | 90 ++++++++++++++------- Assets/Scripts/PanButton.cs | 4 +- Assets/Scripts/ProcGen/BoardCreator.cs | 27 +++++-- Assets/Scripts/ProcGen/RoomChecker.cs | 27 +++++++ Assets/Scripts/ProcGen/RoomChecker.cs.meta | 12 +++ ProjectSettings/ProjectSettings.asset | Bin 37045 -> 37337 bytes 8 files changed, 124 insertions(+), 45 deletions(-) delete mode 100644 Assets/Graphics.meta create mode 100644 Assets/Scripts/ProcGen/RoomChecker.cs create mode 100644 Assets/Scripts/ProcGen/RoomChecker.cs.meta diff --git a/Assets/Graphics.meta b/Assets/Graphics.meta deleted file mode 100644 index 2c87af9..0000000 --- a/Assets/Graphics.meta +++ /dev/null @@ -1,9 +0,0 @@ -fileFormatVersion: 2 -guid: 9b9db29c441a07342a58667c7825fa08 -folderAsset: yes -timeCreated: 1455386127 -licenseType: Free -DefaultImporter: - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scenes/GenerationTestScene.unity b/Assets/Scenes/GenerationTestScene.unity index 15260fb1524583563077bdc80535921efbc619b2..0ab9d18e4106c8b04fbad7022fc4d1553a009c31 100644 GIT binary patch delta 1705 zcmbtSeQc9u5WlA%TZ>y^E0y={2P;ko%w~(xWf8OQE;+eA`rh^^*_G2m8MMZIj$`X%fwXGm3?1{mBD)` zF{Si@Z&-xl8gkc=a5j;%?X{9M7n^MwXTX1;iG2P8b`o9=ubtxo6?gm#52^U#SvY5p zkZ^Y16}^Ms3nikH%(-eH!jjO(}7_ z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w^PrkKjZn#&pl`TnbwN}-1A^yd#z9Rq*}M(`=FlmYeBAG3!3nF$U@mz os00T?D`om{=44lRCzn3m<0#}0=Rx=fe@|-s1tO6_-2eap diff --git a/Assets/Scripts/CameraController.cs b/Assets/Scripts/CameraController.cs index b67b7d0..b72a214 100644 --- a/Assets/Scripts/CameraController.cs +++ b/Assets/Scripts/CameraController.cs @@ -3,35 +3,69 @@ public class CameraController : MonoBehaviour { - - public GameObject player; + //Used to disable the CameraController. + public bool follow = true; - public float smoothTime = 0.5f; + //Object to follow. + public GameObject player; + //Smoothness of the Camera when following the player. + public float smoothTime = 0.5f; + //Smoothness of the Camera when zooming out to fit the room or zooming in to follow the player. + public float zoomSmooth = 0.1f; + //Extra Screen Size + public float zoomExtra = 1; + //Game Manager Object in Scene - Need this to get RoomChecker Component + public GameObject gameManager; + //Set relative position to player object + private Vector3 offset; + //Velocity of the camera + private Vector3 currentVelocity; + //Orthographic Size of Camera when following player. + private float originalOrthoSize = 5; - private Vector3 offset; //Set relative position to player object + // Use this for initialization + void Start() + { + //Calculates offset by subtracting player position from camera object position + offset = transform.position; + originalOrthoSize = Camera.main.orthographicSize; + follow = true; + } - private Vector3 currentVelocity; //Velocity of the camera - - // Use this for initialization - void Start() - { - //Calculates offset by subtracting player position from camera object position - offset = transform.position; - } - - // Update is called once per frame - // Changed from LateUpdate to FixedUpdate so that Camera follows character more smoothly. - void FixedUpdate() - { - - if (player == null) - { - player = GameObject.Find("PlayerPlaceholder(Clone)"); - } - else - { - //Camera's new position is the player's position offsetted - transform.position = Vector3.SmoothDamp(transform.position, new Vector3(player.transform.position.x, player.transform.position.y, transform.position.z), ref currentVelocity, smoothTime); - } - } + // Update is called once per frame + // Changed from LateUpdate to FixedUpdate so that Camera follows character more smoothly. + void FixedUpdate() + { + if (follow) + { + if (player == null) + { + player = GameObject.Find("PlayerPlaceholder(Clone)"); + if (player != null) + { + transform.position = new Vector3(player.transform.position.x, player.transform.position.y, offset.z); + } + } + else + { + Room tr = gameManager.GetComponent().getRoomIn(player.transform.position); + //Camera's new position is the player's position offsetted + if (tr == null) + { + Camera.main.orthographicSize = Mathf.Lerp(Camera.main.orthographicSize, originalOrthoSize, zoomSmooth); + transform.position = Vector3.SmoothDamp(transform.position, new Vector3(player.transform.position.x, player.transform.position.y, offset.z), ref currentVelocity, smoothTime); + } + else + { + float screenDist = tr.roomWidth > tr.roomHeight ? tr.roomWidth : tr.roomHeight; + Camera.main.orthographicSize = Mathf.Lerp(Camera.main.orthographicSize, screenDist / 2 + zoomExtra, zoomSmooth); + transform.position = Vector3.SmoothDamp(transform.position, new Vector3(tr.xPos + tr.roomWidth / 2 - 0.5f, tr.yPos + tr.roomHeight / 2 - 0.5f, offset.z), ref currentVelocity, smoothTime); + } + } + } + else + { + Camera.main.orthographicSize = Mathf.Lerp(Camera.main.orthographicSize, originalOrthoSize, zoomSmooth); + } + } } diff --git a/Assets/Scripts/PanButton.cs b/Assets/Scripts/PanButton.cs index c6f9ddc..9468681 100644 --- a/Assets/Scripts/PanButton.cs +++ b/Assets/Scripts/PanButton.cs @@ -30,7 +30,7 @@ void OnTriggerStay2D(Collider2D c) player = c.transform; camPos = Camera.main.transform.position; player.GetComponent().enabled = false; - Camera.main.GetComponent().enabled = false; + Camera.main.GetComponent().follow = false; } } } @@ -57,7 +57,7 @@ void LateUpdate() Camera.main.transform.position = camPos; if (Vector2.Distance(camPos, player.position) < 0.1f) { - Camera.main.GetComponent().enabled = true; + Camera.main.GetComponent().follow = true; panState = state.notpanning; } } diff --git a/Assets/Scripts/ProcGen/BoardCreator.cs b/Assets/Scripts/ProcGen/BoardCreator.cs index 116acca..e02822f 100644 --- a/Assets/Scripts/ProcGen/BoardCreator.cs +++ b/Assets/Scripts/ProcGen/BoardCreator.cs @@ -1,6 +1,7 @@ using System.Collections; using UnityEngine; +[RequireComponent(typeof(RoomChecker))] public class BoardCreator : MonoBehaviour { // The type of tile that will be laid in a specific position. @@ -20,6 +21,7 @@ public enum TileType public GameObject[] wallTiles; // An array of wall tile prefabs. public GameObject[] outerWallTiles; // An array of outer wall tile prefabs. public GameObject player; + public RoomChecker rc; private TileType[][] tiles; // A jagged array of tile types representing the board, like a grid. private Room[] rooms; // All the rooms that are created for this board. @@ -31,6 +33,7 @@ private void Start() { // Create the board holder. boardHolder = new GameObject("BoardHolder"); + rc = transform.GetComponent(); SetupTilesArray(); @@ -41,6 +44,7 @@ private void Start() InstantiateTiles(); InstantiateOuterWalls(); + } @@ -68,20 +72,30 @@ void CreateRoomsAndCorridors() // Create the first room and corridor. rooms[0] = new Room(); - corridors[0] = new Corridor(); + corridors[0] = new Corridor(); // Setup the first room, there is no previous corridor so we do not use one. rooms[0].SetupRoom(roomWidth, roomHeight, columns, rows); + if (rc == null) + { + Debug.Log("Roomchecker doesnt exist"); + } + if (rooms[0] == null) + { + Debug.Log("Room[0] doesnt exist"); + } + rc.RoomList.Add(rooms[0]); - // Setup the first corridor using the first room. - corridors[0].SetupCorridor(rooms[0], corridorLength, roomWidth, roomHeight, columns, rows, true); + // Setup the first corridor using the first room. + corridors[0].SetupCorridor(rooms[0], corridorLength, roomWidth, roomHeight, columns, rows, true); // Set up second room. Check for overlap is not necessary rooms[1] = new Room(); rooms[1].SetupRoom(roomWidth, roomHeight, columns, rows, corridors[0]); + rc.RoomList.Add(rooms[1]); - // Set up the rest of the rooms and corridors, checking for overlaps - for (int i = 2; i < rooms.Length; i++) + // Set up the rest of the rooms and corridors, checking for overlaps + for (int i = 2; i < rooms.Length; i++) { bool goodRoomPlacement = false; @@ -119,7 +133,8 @@ void CreateRoomsAndCorridors() { corridors [i - 1] = corridorToBePlaced; rooms [i] = roomToBePlaced; - } + rc.RoomList.Add(rooms[i]); + } } //Instantiates player in the i-th/2 room created diff --git a/Assets/Scripts/ProcGen/RoomChecker.cs b/Assets/Scripts/ProcGen/RoomChecker.cs new file mode 100644 index 0000000..1822362 --- /dev/null +++ b/Assets/Scripts/ProcGen/RoomChecker.cs @@ -0,0 +1,27 @@ +using UnityEngine; +using System.Collections.Generic; + +public class RoomChecker : MonoBehaviour { + //ArrayList of all the Rooms on the Board. + public List RoomList; + public float bonusThreshhold; + + void Start () { + RoomList = new List(); + } + public void Add(Room r) + { + RoomList.Add(r); + } + public Room getRoomIn(Vector2 pos) + { + foreach(Room r in RoomList) + { + if (pos.x >= r.xPos-bonusThreshhold && pos.x <= r.xPos + r.roomWidth + bonusThreshhold && pos.y >= r.yPos -bonusThreshhold && pos.y <= r.yPos + r.roomHeight + bonusThreshhold) + { + return r; + } + } + return null; + } +} diff --git a/Assets/Scripts/ProcGen/RoomChecker.cs.meta b/Assets/Scripts/ProcGen/RoomChecker.cs.meta new file mode 100644 index 0000000..69b932c --- /dev/null +++ b/Assets/Scripts/ProcGen/RoomChecker.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: cacdf28687c67af4a9143f7238987583 +timeCreated: 1456629712 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/ProjectSettings/ProjectSettings.asset b/ProjectSettings/ProjectSettings.asset index 49f55f55fc2485e941f110d47044907a95dcda76..70fdf2daa2a20c9683e92fc095e8013d90ea7d73 100644 GIT binary patch delta 353 zcmdnGkm=@PCJqLM4j~4Hi8nWLaCGpjQlc$d>#1NnwcI;~F!Iq#a<^w9` q0b*n`_^}&+RlfkyYPA>`1hH5K3Jr7zOpfVLm~7C&vDv3DlL-Ke`&DrO delta 58 zcmcb)m}%=mCJqLM4j~4H30pUEaCGpr0=bP0ApHOT