Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class DK_MapSystem : MonoBehaviour {
- public float ScrollSpeed = 20.0f;
- public float ZoomSpeed = 10.0f;
- public float Scroll_Width = 10.0f;
- public int CameraHeight = 60;
- private GameObject[][] ShiftMap = new GameObject[50][];
- private GameObject[][] ShiftMap_hold = new GameObject[50][];
- private Vector2 TileOffset;
- private Vector2 TileSize;
- private Mesh SingleTile;
- private GameObject MapHolder;
- private Vector2 CameraTile;
- private Vector2 CameraTile_old;
- private Vector2 MoveTiles = new Vector2(0,0);
- DK_Tiles DKTiles;
- DK_TileSystem DK_TileSys;
- // Use this for initialization
- void Start () {
- for(int ax=0; ax<50; ax++){
- ShiftMap[ax] = new GameObject[40];
- ShiftMap_hold[ax] = new GameObject[40];
- }
- DKTiles = transform.gameObject.GetComponent<DK_Tiles>();
- SingleTile = DKTiles.MapPrefabs[0].GetComponent<MeshFilter>().sharedMesh;
- TileSize = new Vector2(SingleTile.bounds.size.x, SingleTile.bounds.size.z);
- for(int ax=0; ax<50; ax++){
- for(int ay=0; ay<40; ay++){
- ShiftMap[ax][ay] = Instantiate(DKTiles.MapPrefabs[0], new Vector3(ax*TileSize.x, 0, ay*TileSize.y), Quaternion.identity) as GameObject;
- ShiftMap[ax][ay].transform.name = "Position: " + ax + " | " + ay;
- }
- }
- MapHolder = new GameObject();
- MapHolder.transform.name = "TilemapHolder";
- for(int ax=0; ax<50; ax++){
- for(int ay=0; ay<40; ay++){
- ShiftMap[ax][ay].transform.parent = MapHolder.transform;
- }
- }
- DKTiles.TheCameraParent.transform.position = new Vector3((50*TileSize.x)/2, CameraHeight, (40*TileSize.y)/10);
- DKTiles.TheCamera.transform.eulerAngles = new Vector3(50.000f, 0.000f, 0.000f);
- CameraTile = new Vector2(Mathf.Round(DKTiles.TheCameraParent.transform.position.x / TileSize.x), Mathf.Round(DKTiles.TheCameraParent.transform.position.z / TileSize.y));
- CameraTile_old = CameraTile;
- }
- // Update is called once per frame
- Vector3 mousePos;
- Vector3 mouserayPos;
- Ray mouseray;
- float hitdistance;
- Vector2 scroller;
- float zoomer;
- void Update () {
- //==================================================================================================================
- // SCROLL & ZOOM
- //==================================================================================================================
- if(Input.GetAxis("Horizontal") > 0 ){scroller.x = 1;}else if(Input.GetAxis("Horizontal") < 0 ){scroller.x = -1;}
- else if(Input.mousePosition.x < Scroll_Width){scroller.x = -1;}
- else if(Input.mousePosition.x > Screen.width - Scroll_Width){scroller.x = 1;}
- else{scroller.x = 0;}
- if(Input.GetAxis("Vertical") > 0 ){scroller.y = 1;}else if(Input.GetAxis("Vertical") < 0 ){scroller.y = -1;}
- else if(Input.mousePosition.y < Scroll_Width){scroller.y = -1;}
- else if(Input.mousePosition.y > Screen.height - Scroll_Width){scroller.y = 1;}
- else{scroller.y = 0;}
- 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);
- if(Input.GetAxis("Mouse ScrollWheel") > 0){zoomer = -1f;} else if(Input.GetAxis("Mouse ScrollWheel") < 0){zoomer = 1f;}
- else if(Input.GetKey(KeyCode.End)){zoomer = 0.5f;}
- else if(Input.GetKey(KeyCode.Home)){zoomer = -0.5f;}
- else{zoomer = 0f;}
- DKTiles.TheCamera.fieldOfView += (zoomer * ZoomSpeed)*Time.deltaTime;
- DKTiles.TheCamera.fieldOfView = Mathf.Clamp(DKTiles.TheCamera.fieldOfView, 15, 35);
- //==================================================================================================================
- // SHIFT MAPPING
- //==================================================================================================================
- CameraTile = new Vector2(Mathf.Round(DKTiles.TheCameraParent.transform.position.x / TileSize.x), Mathf.Round(DKTiles.TheCameraParent.transform.position.z / TileSize.y));
- if(CameraTile.x > CameraTile_old.x)
- {MoveTiles.x = CameraTile.x - CameraTile_old.x;}
- else if(CameraTile.x < CameraTile_old.x)
- {MoveTiles.x = CameraTile.x - CameraTile_old.x;}
- if(CameraTile.y > CameraTile_old.y)
- {MoveTiles.y = CameraTile.y - CameraTile_old.y;}
- else if(CameraTile.y < CameraTile_old.y)
- {MoveTiles.y = CameraTile.y - CameraTile_old.y;}
- DKTiles.TheCamera.transform.name = "MainCamera - Position| X:" + CameraTile.x + " | Y:" + CameraTile.y;
- //RECHTS
- if(MoveTiles.x >= 1){
- MoveTiles.x -= 1;
- TileOffset.x += 1;
- for(int ax=1; ax<50; ax++){
- for(int ay=0; ay<40; ay++){
- ShiftMap_hold[ax-1][ay] = ShiftMap[ax][ay];
- }
- }
- for(int ay=0; ay<40; ay++){
- ShiftMap_hold[49][ay] = ShiftMap[0][ay];
- }
- for(int ax=0; ax<50; ax++){
- for(int ay=0; ay<40; ay++){
- ShiftMap[ax][ay] = ShiftMap_hold[ax][ay];
- if(ax == 49){
- ShiftMap[ax][ay].transform.position = new Vector3(ShiftMap[ax][ay].transform.position.x+(TileSize.x*50),0,ShiftMap[ax][ay].transform.position.z);
- }
- }
- }
- CameraTile_old = CameraTile;
- }
- //LINKS
- else if(MoveTiles.x <= -1){
- MoveTiles.x += 1;
- TileOffset.x -= 1;
- for(int ax=0; ax<49; ax++){
- for(int ay=0; ay<40; ay++){
- ShiftMap_hold[ax+1][ay] = ShiftMap[ax][ay];
- }
- }
- for(int ay=0; ay<40; ay++){
- ShiftMap_hold[0][ay] = ShiftMap[49][ay];
- }
- for(int ax=0; ax<50; ax++){
- for(int ay=0; ay<40; ay++){
- ShiftMap[ax][ay] = ShiftMap_hold[ax][ay];
- if(ax == 0){
- ShiftMap[ax][ay].transform.position = new Vector3(ShiftMap[ax][ay].transform.position.x-(TileSize.x*50),0,ShiftMap[ax][ay].transform.position.z);
- }
- }
- }
- CameraTile_old = CameraTile;
- }
- //HOCH
- if(MoveTiles.y >= 1){
- MoveTiles.y -= 1;
- TileOffset.y += 1;
- for(int ax=0; ax<50; ax++){
- for(int ay=1; ay<40; ay++){
- ShiftMap_hold[ax][ay-1] = ShiftMap[ax][ay];
- }
- }
- for(int ax=0; ax<50; ax++){
- ShiftMap_hold[ax][39] = ShiftMap[ax][0];
- }
- for(int ax=0; ax<50; ax++){
- for(int ay=0; ay<40; ay++){
- ShiftMap[ax][ay] = ShiftMap_hold[ax][ay];
- if(ay == 39){
- ShiftMap[ax][ay].transform.position = new Vector3(ShiftMap[ax][ay].transform.position.x,0,ShiftMap[ax][ay].transform.position.z+(TileSize.y*40));
- }
- }
- }
- CameraTile_old = CameraTile;
- }
- //RUNTER
- else if(MoveTiles.y <= -1){
- MoveTiles.y += 1;
- TileOffset.y -= 1;
- for(int ax=0; ax<50; ax++){
- for(int ay=0; ay<39; ay++){
- ShiftMap_hold[ax][ay+1] = ShiftMap[ax][ay];
- }
- }
- for(int ax=0; ax<50; ax++){
- ShiftMap_hold[ax][0] = ShiftMap[ax][39];
- }
- for(int ax=0; ax<50; ax++){
- for(int ay=0; ay<40; ay++){
- ShiftMap[ax][ay] = ShiftMap_hold[ax][ay];
- if(ay == 0){
- ShiftMap[ax][ay].transform.position = new Vector3(ShiftMap[ax][ay].transform.position.x,0,ShiftMap[ax][ay].transform.position.z-(TileSize.y*40));
- }
- }
- }
- CameraTile_old = CameraTile;
- }
- //==================================================================================================================
- // CURSOR
- //==================================================================================================================
- RaycastHit hit;
- Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
- if(Physics.Raycast(ray, out hit))
- {
- hitdistance = Vector3.Distance(DKTiles.TheCamera.transform.position, hit.point);
- mouserayPos = DKTiles.TheCamera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, hitdistance-2));
- DKTiles.CursorLamp.transform.position = new Vector3(mouserayPos.x, 5, mouserayPos.z);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment