예제 #1
0
    // Update is called once per frame
    void Update()
    {
        mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
//		Debug.Log ("Field/Update: mouse position = (" + mousePosition.x + ", " + mousePosition.y + ")");

        Direction newCursorState = Direction.NONE;
        //version 1 mouse detection
//		if (mousePosition.x > width / 2f || mousePosition.x < -width / 2f ||
//		    mousePosition.y > height / 2f || mousePosition.y < -height / 2f) {
//			newCursorState = Direction.NONE;
//		}
//		else {
//			float m = height / width;
//			bool posLine = mousePosition.y > m * mousePosition.x;
//			bool negLine = mousePosition.y > -m * mousePosition.x;
//			if (posLine && negLine) {
//				newCursorState = Direction.FRONT;
//			}
//			else if (!posLine && negLine) {
//				newCursorState = Direction.RIGHT;
//			}
//			else if (!posLine && !negLine) {
//				newCursorState = Direction.BACK;
//			}
//			else if (posLine && !negLine) {
//				newCursorState = Direction.LEFT;
//			}
//		}
        //version 2 mouse detection
        RaycastHit2D hitInfo = Physics2D.Raycast(mousePosition, Vector2.zero, 0, LayerMask.GetMask("Field"));

        if (hitInfo.collider == null)
        {
            newCursorState = Direction.NONE;
        }
        else
        {
            if (hitInfo.collider.gameObject == frontField.gameObject)
            {
                newCursorState = Direction.FRONT;
            }
            else if (hitInfo.collider.gameObject == rightField.gameObject)
            {
                newCursorState = Direction.RIGHT;
            }
            else if (hitInfo.collider.gameObject == backField.gameObject)
            {
                newCursorState = Direction.BACK;
            }
            else if (hitInfo.collider.gameObject == leftField.gameObject)
            {
                newCursorState = Direction.LEFT;
            }
        }

        if (newCursorState != Direction.NONE && directionState [(int)newCursorState] == false)
        {
            newCursorState = Direction.NONE;
        }
        if (cursorState != newCursorState)
        {
            switch (cursorState)
            {
            case (Direction.NONE):
                break;

            case (Direction.FRONT):
                frontField.setHighlightState(false);
                break;

            case (Direction.RIGHT):
                rightField.setHighlightState(false);
                break;

            case (Direction.BACK):
                backField.setHighlightState(false);
                break;

            case (Direction.LEFT):
                leftField.setHighlightState(false);
                break;
            }

            switch (newCursorState)
            {
            case (Direction.NONE):
                break;

            case (Direction.FRONT):
                frontField.setHighlightState(true);
                break;

            case (Direction.RIGHT):
                rightField.setHighlightState(true);
                break;

            case (Direction.BACK):
                backField.setHighlightState(true);
                break;

            case (Direction.LEFT):
                leftField.setHighlightState(true);
                break;
            }
            cursorState = newCursorState;
        }

        if (Input.GetMouseButtonDown(0))
        {
            Debug.Log("Field/Update: select" + cursorState);
            if (inputState == Direction.NONE)
            {
                inputState = cursorState;
            }
        }
    }