Advertisement
Guest User

Untitled

a guest
Jan 29th, 2014
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.31 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class DK_MapSystem : MonoBehaviour {
  5.     public float ScrollSpeed = 20.0f;
  6.     public float ZoomSpeed = 10.0f;
  7.     public float RotateSpeed = 5.0f;
  8.     public float Scroll_Width = 10.0f;
  9.    
  10.     public int CameraHeight = 60;
  11.    
  12.     private GameObject[][] ShiftMap = new GameObject[50][];
  13.    
  14.     private Vector2 TileSize;
  15.     private Mesh SingleTile;
  16.    
  17.     private GameObject MapHolder;
  18.    
  19.     private Vector3 mousePos;
  20.     private Vector3 mouserayPos;
  21.     private Vector3 camangle;
  22.     private Vector3 rayget;
  23.    
  24.     private Vector3 Tempv = new Vector3(0,0,0);
  25.     private Vector2 scroller;
  26.    
  27.     private Ray mouseray;
  28.     private Ray ray;
  29.     private RaycastHit hit;
  30.    
  31.     private float hitdistance;
  32.     private float zoomer;
  33.     private float camX, camY;
  34.    
  35.     DK_Tiles DKTiles;
  36.     DK_TileSystem DK_TileOption;
  37.    
  38.     // Use this for initialization
  39.     void Start () {
  40.         for(int ax=0; ax<50; ax++){
  41.             ShiftMap[ax] = new GameObject[40];
  42.         }
  43.         DKTiles = transform.gameObject.GetComponent<DK_Tiles>();
  44.        
  45.         SingleTile = DKTiles.MapPrefabs[0].GetComponent<MeshFilter>().sharedMesh;
  46.         TileSize = new Vector2(SingleTile.bounds.size.x, SingleTile.bounds.size.z);
  47.         for(int ax=0; ax<50; ax++){
  48.             for(int ay=0; ay<40; ay++){
  49.                 ShiftMap[ax][ay] = Instantiate(DKTiles.MapPrefabs[0], new Vector3(ax*TileSize.x, 0, ay*TileSize.y), Quaternion.identity) as GameObject;
  50.                 ShiftMap[ax][ay].transform.name = "Position: " + ax + " | " + ay;
  51.                 DK_TileOption = ShiftMap[ax][ay].GetComponent<DK_TileSystem>();
  52.                 DK_TileOption.Tile_Position = new Vector2(ax, ay);
  53.                 DK_TileOption.Tile_ID = 1;
  54.             }
  55.         }
  56.        
  57.         MapHolder = new GameObject();
  58.         MapHolder.transform.name = "TilemapHolder";
  59.        
  60.         for(int ax=0; ax<50; ax++){
  61.             for(int ay=0; ay<40; ay++){
  62.                 ShiftMap[ax][ay].transform.parent = MapHolder.transform;
  63.             }
  64.         }
  65.        
  66.         DKTiles.TheCamera.transform.position = new Vector3(0, CameraHeight, -34);
  67.         DKTiles.TheCamera.transform.eulerAngles = new Vector3(50.000f, 0.000f, 0.000f);
  68.         DKTiles.TheCameraParent.transform.position = new Vector3((50*TileSize.x)/2, 0, (40*TileSize.y)/2);
  69.     }
  70.        
  71.     // Update is called once per frame
  72.     void Update () {
  73.        
  74.         //==================================================================================================================
  75.         // SCROLL & ZOOM
  76.         //==================================================================================================================
  77.         if(Input.GetAxis("Horizontal") > 0 ){scroller.x = 1;}else if(Input.GetAxis("Horizontal") < 0 ){scroller.x = -1;}
  78.         else if(Input.mousePosition.x < Scroll_Width){scroller.x = -1;}
  79.         else if(Input.mousePosition.x > Screen.width - Scroll_Width){scroller.x = 1;}
  80.         else{scroller.x = 0;}
  81.            
  82.         if(Input.GetAxis("Vertical") > 0 ){scroller.y = 1;}else if(Input.GetAxis("Vertical") < 0 ){scroller.y = -1;}
  83.         else if(Input.mousePosition.y < Scroll_Width){scroller.y = -1;}
  84.         else if(Input.mousePosition.y > Screen.height - Scroll_Width){scroller.y = 1;}
  85.         else{scroller.y = 0;}
  86.        
  87.         DKTiles.TheCameraParent.transform.Translate((scroller.x*ScrollSpeed*(Input.GetKey(KeyCode.LeftShift) ? 2 : 1))*Time.deltaTime, 0, (scroller.y*ScrollSpeed*(Input.GetKey(KeyCode.LeftShift) ? 2 : 1))*Time.deltaTime);
  88.         camX = DKTiles.TheCameraParent.transform.position.x;
  89.         camY = DKTiles.TheCameraParent.transform.position.z;
  90.         camX = Mathf.Clamp(camX, 10, 999);
  91.         camY = Mathf.Clamp(camY, 10, 999);
  92.         Tempv.x = camX;
  93.         Tempv.y = 0;
  94.         Tempv.z = camY;
  95.         DKTiles.TheCameraParent.transform.position = Tempv;
  96.        
  97.         if(Input.GetAxis("Mouse ScrollWheel") > 0){zoomer = -1f;} else if(Input.GetAxis("Mouse ScrollWheel") < 0){zoomer = 1f;}
  98.         else if(Input.GetKey(KeyCode.End)){zoomer = 0.5f;}
  99.         else if(Input.GetKey(KeyCode.Home)){zoomer = -0.5f;}
  100.         else{zoomer = 0f;}
  101.        
  102.         if(Input.GetKey(KeyCode.Delete)){DKTiles.TheCameraParent.transform.Rotate(0,RotateSpeed*Time.deltaTime,0);}
  103.         else if(Input.GetKey(KeyCode.PageDown)){DKTiles.TheCameraParent.transform.Rotate(0,-RotateSpeed*Time.deltaTime,0);}
  104.         else if(Input.GetKey(KeyCode.PageUp)){camangle.x=0;camangle.y=0;camangle.z=0;DKTiles.TheCameraParent.transform.eulerAngles = camangle;}
  105.        
  106.         DKTiles.TheCamera.fieldOfView += (zoomer * ZoomSpeed)*Time.deltaTime;
  107.         DKTiles.TheCamera.fieldOfView = Mathf.Clamp(DKTiles.TheCamera.fieldOfView, 15, 35);
  108.        
  109.         //==================================================================================================================
  110.         // SHIFT MAPPING
  111.         //==================================================================================================================
  112.        
  113.        
  114.        
  115.         //==================================================================================================================
  116.         // CURSOR
  117.         //==================================================================================================================
  118.         ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  119.         if(Physics.Raycast(ray, out hit))
  120.         {
  121.             hitdistance = Vector3.Distance(DKTiles.TheCamera.transform.position, hit.point);
  122.            
  123.             Tempv.x = Input.mousePosition.x;
  124.             Tempv.y = Input.mousePosition.y;
  125.             Tempv.z = hitdistance-2;
  126.             mouserayPos = DKTiles.TheCamera.ScreenToWorldPoint(Tempv);
  127.             Tempv.x = mouserayPos.x;
  128.             Tempv.y = 10;
  129.             Tempv.z = mouserayPos.z;
  130.             rayget.x = mouserayPos.x;
  131.             rayget.y = 10;
  132.             rayget.z = mouserayPos.z;
  133.             DKTiles.CursorLamp.transform.position = rayget;
  134.         }
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement