예제 #1
0
    public void OnDrag(PointerEventData eventData)
    {
        if (eventData.button != PointerEventData.InputButton.Left)
        {
            return;
        }

        if (_status != SlotState.Used || GameManager.instance.gamestate != GameManager.GameState.Placement)
        {
            return;
        }

        List <Cell> authorizedCells = Board.CurrentBoard.GetAuthorizedAllyCells();

        List <Cell> avalaibleCells = new List <Cell>();

        foreach (Cell cell in authorizedCells)
        {
            if (!cell.GetIsOccupied() && cell.GetIsObstacle() == false)
            {
                avalaibleCells.Add(cell);
            }
        }

        if (avalaibleCells.Count == 0)
        {
            return;
        }

        Unit newUnit = Instantiate(unitPrefab).GetComponent <Unit>();

        newUnit.SetName(unitDescription.GetUnitName());
        newUnit.SetSprite(unitDescription.GetSprite());
        newUnit.raceStats = unitDescription.GetRace();
        newUnit.classStat = unitDescription.GetClass();
        newUnit.SetAbilityName(unitDescription.GetAbilityName());
        newUnit.SetGems(unitDescription.GetGems());
        newUnit.Level = unitDescription.GetLevel();
        newUnit.AttachBoard();
        newUnit.transform.SetPositionAndRotation(attachedCamera.ScreenToWorldPoint(Input.mousePosition), Quaternion.identity);

        Cell destinatinCell = avalaibleCells[0];

        newUnit.initialPos = destinatinCell.TileMapPosition;

        newUnit.tag          = Unit.allyTag;
        newUnit.isRandomUnit = false;
        newUnit.id           = unitDescription.GetId();

        newUnit.PrepareForDragNDrop();
        ClearSlot();
    }
예제 #2
0
    public void PutInSlot(UnitDescription unitDescription)
    {
        if (_status != SlotState.Empty)
        {
            return;
        }

        _status = SlotState.Used;

        child = new GameObject(unitDescription.GetUnitName());
        child.transform.SetParent(transform, false);

        float width = GetComponent <RectTransform>().rect.width;

        {
            Image  i      = child.AddComponent <Image>();
            Sprite sprite = unitDescription.GetSprite();
            i.sprite = sprite;

            Rect          spriteRect  = unitDescription.GetSprite().rect;
            float         aspectRatio = spriteRect.width / spriteRect.height;
            RectTransform rt          = child.GetComponent <RectTransform>();
            rt.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, width);
            rt.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, width * aspectRatio);
        }

        this.unitDescription = unitDescription;

        {
            classDisplay = new GameObject("Class display");
            classDisplay.transform.SetParent(transform, false);
            RectTransform rt = classDisplay.AddComponent <RectTransform>();
            rt.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, 0, width * 0.25f);
            rt.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Right, 0, width * 0.25f);
            classDisplay.AddComponent <Image>().sprite = unitDescription.GetClass().classIconSprite;
        }
    }