using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Item : MonoBehaviour {

    public GameObject itemWindow;

    public ItemSpawner spawner;

    Transform graphics;

	// Use this for initialization
	void Start () {
        graphics = transform.GetChild(0);
	}

	// Update is called once per frame
	void Update () {
        graphics.localRotation = Quaternion.Euler(0, 180 * Time.deltaTime, 0) * graphics.localRotation;
	}

    void OnTriggerEnter(Collider other)
    {
        spawner.PickedUp();

        GameObject.Destroy(gameObject);

        GameObject container = GameObject.FindGameObjectWithTag("WindowContainer");

        RectTransform rt = GameObject.Instantiate(itemWindow, container.transform).GetComponent<RectTransform>();

        if (rt == null)
            return;

        Vector2 windowPos = new Vector2(Random.Range(0, 1 - rt.anchorMax.x), Random.Range(0, 1 - rt.anchorMax.y));

        rt.anchorMin = windowPos;
        rt.anchorMax += windowPos;
    }
}
