Advertisement
Guest User

Untitled

a guest
Feb 20th, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.32 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerControllerNew : MonoBehaviour
  6. {
  7.     private Vector3 mousePosition;
  8.     public float moveSpeed = 1f;
  9.     GameController globalScript;
  10.     public int w = 9;
  11.     public int h = 19;
  12.     private float previousTime;
  13.  
  14.     private Vector3 _lastPosition;
  15.     public float fallTime = 0.8f;
  16.     Vector3 mousePoint;
  17.  
  18.     public bool isBorder = false;
  19.  
  20.     public bool freeze = false;
  21.     void Start()
  22.     {
  23.         _lastPosition = transform.position;
  24.         globalScript = GameObject.Find("GlobalScript").GetComponent<GameController>();
  25.     }
  26.  
  27.     void OnMouseDown()
  28.     {
  29.         if (!freeze)
  30.         {
  31.             freeze = true;
  32.             transform.position = roundVec2(mousePoint);
  33.  
  34.             foreach (Transform child in transform)
  35.             {
  36.                 Debug.Log(child.position.x);
  37.                 Debug.Log(child.position.y);
  38.                 Vector3 v = roundVec2(child.position);
  39.                 globalScript.getBlock((int)v.x, (int)v.y);
  40.             }
  41.         }
  42.     }
  43.  
  44.     // Update is called once per frame
  45.     void Update()
  46.     {
  47.         mousePosition = Input.mousePosition;
  48.         mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
  49.  
  50.         _lastPosition = transform.position;
  51.         transform.position = roundVec2(Vector2.Lerp(transform.position, mousePosition, moveSpeed));
  52.  
  53.         if (!isValidGroup())
  54.             transform.position = _lastPosition;
  55.  
  56.     }
  57.  
  58.     public static Vector3 roundVec2(Vector3 v)
  59.     {
  60.         return new Vector3(Mathf.Round(v.x), Mathf.Round(v.y), Mathf.Round(v.z));
  61.     }
  62.  
  63.     public bool isValidGroup()
  64.     {
  65.         foreach (Transform child in transform)
  66.         {
  67.             if (!insideBorder(child.position))
  68.                 return false;
  69.         }
  70.  
  71.         return true;
  72.     }
  73.  
  74.     void updateGrid()
  75.     {
  76.         if (!freeze)
  77.         {
  78.             mousePoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
  79.             mousePoint.z = 0;
  80.             gameObject.transform.position = roundVec2(mousePoint);
  81.         }
  82.     }
  83.  
  84.     public bool insideBorder(Vector3 pos)
  85.     {
  86.         return ((int)pos.x >= 0 &&
  87.                 (int)pos.x <= w &&
  88.                 (int)pos.y >= 0 &&
  89.                 (int)pos.y <= h);
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement