Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class PlayerControllerNew : MonoBehaviour
- {
- private Vector3 mousePosition;
- public float moveSpeed = 1f;
- GameController globalScript;
- public int w = 9;
- public int h = 19;
- private float previousTime;
- private Vector3 _lastPosition;
- public float fallTime = 0.8f;
- Vector3 mousePoint;
- public bool isBorder = false;
- public bool freeze = false;
- void Start()
- {
- _lastPosition = transform.position;
- globalScript = GameObject.Find("GlobalScript").GetComponent<GameController>();
- }
- void OnMouseDown()
- {
- if (!freeze)
- {
- freeze = true;
- transform.position = roundVec2(mousePoint);
- foreach (Transform child in transform)
- {
- Debug.Log(child.position.x);
- Debug.Log(child.position.y);
- Vector3 v = roundVec2(child.position);
- globalScript.getBlock((int)v.x, (int)v.y);
- }
- }
- }
- // Update is called once per frame
- void Update()
- {
- mousePosition = Input.mousePosition;
- mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
- _lastPosition = transform.position;
- transform.position = roundVec2(Vector2.Lerp(transform.position, mousePosition, moveSpeed));
- if (!isValidGroup())
- transform.position = _lastPosition;
- }
- public static Vector3 roundVec2(Vector3 v)
- {
- return new Vector3(Mathf.Round(v.x), Mathf.Round(v.y), Mathf.Round(v.z));
- }
- public bool isValidGroup()
- {
- foreach (Transform child in transform)
- {
- if (!insideBorder(child.position))
- return false;
- }
- return true;
- }
- void updateGrid()
- {
- if (!freeze)
- {
- mousePoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
- mousePoint.z = 0;
- gameObject.transform.position = roundVec2(mousePoint);
- }
- }
- public bool insideBorder(Vector3 pos)
- {
- return ((int)pos.x >= 0 &&
- (int)pos.x <= w &&
- (int)pos.y >= 0 &&
- (int)pos.y <= h);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement