public void SetMarkColor(FarmField.Field field, int gridIndex)
    {
        int num  = gridIndex % field.fieldHorizontal - this.baseGridPositionX;
        int num2 = gridIndex / field.fieldHorizontal - this.baseGridPositionY;

        this.isPlaceable = true;
        if (0 > num || 0 > num2)
        {
            this.isPlaceable = false;
        }
        else
        {
            for (int i = 0; i < this.sizeY; i++)
            {
                int num3 = (i + num2) * field.fieldHorizontal;
                for (int j = 0; j < this.sizeX; j++)
                {
                    int num4 = num3 + num + j;
                    if (num + j >= field.fieldHorizontal || field.grids.Count <= num4 || field.grids[num4].invalid || field.grids[num4].put)
                    {
                        this.isPlaceable = false;
                        break;
                    }
                }
                if (!this.isPlaceable)
                {
                    break;
                }
            }
        }
        FarmSettingMark componentInChildren = base.GetComponentInChildren <FarmSettingMark>();

        componentInChildren.SetColor(this.isPlaceable);
    }
 public int GetGridIndex(int x, int y)
 {
     FarmField.Field field = this.farmField.GetField();
     if (0 <= x && x <= field.fieldHorizontal && 0 <= y && y <= field.fieldVertical)
     {
         return(y * field.fieldHorizontal + x);
     }
     return(-1);
 }
    public FarmGrid.GridPosition IndexToPosition(int gridIndex)
    {
        FarmField.Field field = this.farmField.GetField();
        int             num   = gridIndex / field.fieldHorizontal;
        int             x     = gridIndex - num * field.fieldHorizontal;

        return(new FarmGrid.GridPosition
        {
            x = x,
            y = num
        });
    }
    public bool IsPutGrid(FarmField.Field field, int[] gridIndexs)
    {
        bool result = false;

        foreach (int index in gridIndexs)
        {
            if (field.grids[index].put)
            {
                result = true;
                break;
            }
        }
        return(result);
    }
    public bool IsInvalidGrid(FarmField farmField, int[] gridIndexs)
    {
        bool result = false;

        FarmField.Field field = farmField.GetField();
        foreach (int num in gridIndexs)
        {
            if (0 > num || field.grids.Count <= num || field.grids[num].invalid)
            {
                result = true;
                break;
            }
        }
        return(result);
    }
    public FarmGrid.GridPosition GetGridPosition(Vector3 position)
    {
        int x = 0;
        int y = 0;

        FarmField.Field field = this.farmField.GetField();
        if (field != null)
        {
            x = Mathf.FloorToInt((position.x - field.originPosition.x) / this.farmField.gridHorizontal);
            y = Mathf.CeilToInt((position.z - field.originPosition.y) / this.farmField.gridVertical) * -1;
        }
        return(new FarmGrid.GridPosition
        {
            x = x,
            y = y
        });
    }
    public void UpdateSelectGrid(Vector3 touchPos)
    {
        this.isSelectedGrid = false;
        FarmField.Field field = this.farmField.GetField();
        if (field == null)
        {
            return;
        }
        Rect rect = new Rect(field.originPosition.x, field.originPosition.y, (float)field.fieldHorizontal * this.farmField.gridHorizontal, (float)field.fieldVertical * this.farmField.gridVertical);

        if (rect.x <= touchPos.x && touchPos.x <= rect.x + rect.width && rect.y >= touchPos.z && touchPos.z >= rect.y - rect.height)
        {
            FarmGrid.GridPosition gridPosition = this.GetGridPosition(touchPos);
            int gridIndex = this.GetGridIndex(gridPosition);
            if (0 <= gridIndex && gridIndex <= field.grids.Count)
            {
                this.isSelectedGrid    = true;
                this.selectedGridIndex = gridIndex;
            }
        }
    }
    public int[] GetGridIndexs(FarmField.Field field, int gridIndex)
    {
        int[] array = new int[this.sizeX * this.sizeY];
        int   num   = gridIndex % field.fieldHorizontal - this.baseGridPositionX;
        int   num2  = gridIndex / field.fieldHorizontal - this.baseGridPositionY;

        if (0 <= num && 0 <= num2)
        {
            int num3 = 0;
            for (int i = 0; i < this.sizeY; i++)
            {
                int num4 = (i + num2) * field.fieldHorizontal;
                for (int j = 0; j < this.sizeX; j++)
                {
                    array[num3] = num4 + num + j;
                    num3++;
                }
            }
        }
        return(array);
    }
    public Vector3 GetPositionGridCenter(int gridIndex, bool localPosition = false)
    {
        Vector3 zero = Vector3.zero;

        FarmField.Field field = this.farmField.GetField();
        if (field != null)
        {
            int   num  = gridIndex % field.fieldHorizontal;
            int   num2 = gridIndex / field.fieldHorizontal;
            float newX = (float)num * this.farmField.gridHorizontal + this.farmField.gridHorizontal * 0.5f + field.originPosition.x;
            float newZ = -((float)num2 * this.farmField.gridVertical + this.farmField.gridVertical * 0.5f) + field.originPosition.y;
            if (!localPosition)
            {
                zero.Set(newX, this.farmField.transform.parent.localPosition.y, newZ);
            }
            else
            {
                zero.Set(newX, 0f, newZ);
            }
        }
        return(zero);
    }
    public void SetPutFlag(FarmObject farmObject, int gridX, int gridY, bool putFlag)
    {
        FarmField.Field       field        = this.GetField();
        FarmGrid.GridPosition gridPosition = farmObject.AdjustGridPosition(gridX, gridY);
        bool impassable = !FarmUtility.IsWalkBuild(farmObject.facilityID);

        for (int i = 0; i < farmObject.sizeY; i++)
        {
            int num = (gridPosition.y + i) * field.fieldHorizontal;
            for (int j = 0; j < farmObject.sizeX; j++)
            {
                int num2 = num + gridPosition.x + j;
                if (num2 >= 0 && field.grids.Count > num2)
                {
                    FarmGrid.Grid value = field.grids[num2];
                    value.put         = putFlag;
                    value.impassable  = impassable;
                    field.grids[num2] = value;
                }
            }
        }
    }
Exemple #11
0
 public bool OnDrag(InputControll inputControll)
 {
     if (this.farmObject.gameObject == inputControll.rayHitColliderObject)
     {
         FarmRoot  instance = FarmRoot.Instance;
         FarmField field    = instance.Field;
         if (field.Grid.isSelectedGrid)
         {
             FarmField.Field field2     = field.GetField();
             int[]           gridIndexs = this.farmObject.GetGridIndexs(field2, field.Grid.selectedGridIndex);
             if (!this.farmObject.IsInvalidGrid(field, gridIndexs))
             {
                 this.farmObject.SetPosition(field.gridHorizontal, field.gridVertical, field.Grid.GetPositionGridCenter(field.Grid.selectedGridIndex, false));
                 this.farmObject.DisplayedInFront(true);
                 bool placeable = false == this.farmObject.IsPutGrid(field2, gridIndexs);
                 this.farmObject.SetPlaceable(placeable);
             }
         }
         return(true);
     }
     return(false);
 }
Exemple #12
0
    public bool SetEditFarmObject(FarmObject farmObject, Vector3 basePos3D)
    {
        FarmRoot  instance = FarmRoot.Instance;
        FarmField field    = instance.Field;

        FarmField.Field field2 = field.GetField();
        this.farmObject  = farmObject;
        this.settingMode = FarmObjectSetting.SettingMode.EDIT;
        FarmGrid.GridPosition gridPosition = field.Grid.GetGridPosition(basePos3D);
        bool flag = field.IsOutsideField(gridPosition);

        if (flag)
        {
            gridPosition = field.Grid.GetGridPosition(Vector3.zero);
        }
        int     gridIndex          = field.Grid.GetGridIndex(gridPosition);
        Vector3 positionGridCenter = field.Grid.GetPositionGridCenter(gridIndex, false);

        this.farmObject.SetPosition(field.gridHorizontal, field.gridVertical, positionGridCenter);
        this.farmObject.DisplayedInFront(true);
        int[] gridIndexs = this.farmObject.GetGridIndexs(field2, gridIndex);
        this.farmObject.isPlaceable = (!this.farmObject.IsInvalidGrid(field, gridIndexs) && !this.farmObject.IsPutGrid(field2, gridIndexs));
        return(flag);
    }
Exemple #13
0
    public bool SetFarmObject(FarmObject farmObject, Vector3 basePos3D)
    {
        FarmField field = FarmRoot.Instance.Field;

        this.farmObject  = farmObject;
        this.settingMode = FarmObjectSetting.SettingMode.BUILD;
        this.farmObject.SetSettingMark(field, FarmRoot.Instance.SettingMark);
        FarmGrid.GridPosition gridPosition = field.Grid.GetGridPosition(basePos3D);
        bool flag = field.IsOutsideField(gridPosition);

        if (flag)
        {
            gridPosition = field.Grid.GetGridPosition(Vector3.zero);
        }
        int     gridIndex          = field.Grid.GetGridIndex(gridPosition);
        Vector3 positionGridCenter = field.Grid.GetPositionGridCenter(gridIndex, false);

        this.farmObject.SetPosition(field.gridHorizontal, field.gridVertical, positionGridCenter);
        this.farmObject.DisplayedInFront(true);
        FarmField.Field field2 = field.GetField();
        this.farmObject.SetMarkColor(field2, gridIndex);
        FarmRoot.Instance.Input.AddTouchDragEvent(new Func <InputControll, bool>(this.OnDrag));
        return(flag);
    }
 public int GetGridIndex(FarmGrid.GridPosition gridPosition)
 {
     FarmField.Field field = this.farmField.GetField();
     return(gridPosition.y * field.fieldHorizontal + gridPosition.x);
 }
 public bool IsOutsideField(FarmGrid.GridPosition gridPosition)
 {
     FarmField.Field field = this.GetField();
     return(0 > gridPosition.x || 0 > gridPosition.y || field.fieldHorizontal <= gridPosition.x || field.fieldVertical <= gridPosition.y);
 }