Exemple #1
0
 /* Set prop type & rotation */
 public void Initialise(PropTypes _type, PropRotation _rot)
 {
     rotationOverride = _rot;
     objectType       = _type;
     gameObject.GetComponent <Image>().sprite = PropProperties.GetSpriteSet(objectType).GetByRotation(rotationOverride).sprite;
     gameObject.SetActive((gameObject.GetComponent <Image>().sprite != null));
     if (draggableObject)
     {
         draggableObject.GetComponent <DraggableEditorComponent>().Initialise(_type, _rot);
     }
 }
    public void Initialise(PropTypes _type, List <Tile> _tiles, PropRotation _rotation = PropRotation.FACING_FRONT, bool doSpawnAnim = true)
    {
        if (_tiles.Count == 0)
        {
            Debug.LogError("Failed to init prop! No tiles have been passed for us to occupy!");
            Destroy(this.gameObject);
            return;
        }

        gridTile = _tiles; //_tiles[0] should be our origin. The others can be in any order!
        propType = _type;

        childObject = gameObject.transform.GetChild(0).gameObject;
        ourSprite   = childObject.GetComponent <SpriteRenderer>();
        ourCollider = GetComponent <PolygonCollider2D>();

        if (!doSpawnAnim)
        {
            GetComponent <Animator>().speed = 9999;
        }

        hasInitialised = true;
        spriteSet      = PropProperties.GetSpriteSet(propType);

        //Try and apply ourselves as the prop to our passed tile(s)
        for (int i = 0; i < gridTile.Count; i++)
        {
            gridTile[i].SetProp(null, true, false); //We have to force animations off, else the coroutine time throws off the execution order. TODO: nicer fix?
        }
        for (int i = 0; i < gridTile.Count; i++)
        {
            if (!gridTile[i].SetProp(this))
            {
                Debug.LogWarning("Failed to init prop! Most likely dumb user has tried to decorate an unoccupied tile.");
                foreach (Tile aTile in gridTile)
                {
                    aTile.SetProp(null);
                }
                Destroy(this.gameObject);
                return;
            }
            if (i == 0)
            {
                gridTile[i].SetPropOrigin();
            }
        }

        //Set rotation-based sprite/collider and sorting
        SetRotation(_rotation);
        int offset = (int)(LevelManager.Instance.Grid.GridTileDims.y - _tiles[0].GridPos.y);

        ourSprite.sortingOrder = (5 * offset) + offset + TileProperties.GetZOffset(_tiles[0].Occupier.Type) + PropProperties.GetZOffset(propType) + 3;

        //For neatness in the editor, parent to the grid
        this.gameObject.transform.parent = LevelManager.Instance.Grid.gameObject.transform;

        //Remove touchable layer if out of editor
        if (!LevelManager.Instance.IsInEditor)
        {
            gameObject.layer = 0;
            gameObject.transform.GetChild(0).gameObject.layer = 0;

            //If scriptable object, try to assign script
            if (PropProperties.IsScriptedObject(propType))
            {
                try
                {
                    string componentType = PropProperties.GetScriptClassName(propType);
                    gameObject.AddComponent(System.Type.GetType(componentType));
                }
                catch (System.Exception e)
                {
                    Debug.LogError("Failed to assign script to scripted object!");
                    Debug.LogError(e.Message);
                }
            }
        }
    }
Exemple #3
0
    private void Update()
    {
        //Touch interaction
        if (uiElementInteraction.IsTouchDown())
        {
            draggableObject.transform.SetParent(EditorModeButtonControllers.Instance.gameObject.transform); //HACKY!
            uiElementInteraction.MoveToTouch();
            spawnedWorldObject = false;

            //Show validity markers
            if (!shownInvalidMarkers)
            {
                for (int i = 0; i < LevelManager.Instance.Grid.AllTiles.Count; i++)
                {
                    if (LevelManager.Instance.Grid.AllTiles[i].IsOccupied && !TileIsValidForUs(LevelManager.Instance.Grid.AllTiles[i]))
                    {
                        LevelManager.Instance.Grid.AllTiles[i].Occupier.ShowInvalidMarker(true);
                    }
                }
                shownInvalidMarkers = true;
            }
        }
        else
        {
            if (!spawnedWorldObject)
            {
                Vector3 inWorldPos = Camera.main.ScreenToWorldPoint(draggableObject.transform.position + new Vector3(uiElementInteraction.Rect.width / 2, uiElementInteraction.Rect.height / 2, 0));
                inWorldPos.z = 0;
                if (LevelManager.Instance.Grid.GetTileAtPosition(inWorldPos) == null)
                {
                    Destroy(this);
                }
                List <Tile> worldTileList = LevelManager.Instance.Grid.GetNeighboursFromBounds(LevelManager.Instance.Grid.GetTileAtPosition(inWorldPos), PropProperties.GetSpriteSet(objectType).GetByRotation(rotationOverride).bounds);

                //Validity check
                bool tilesAreValid = true;
                for (int i = 0; i < worldTileList.Count; i++)
                {
                    if (!(worldTileList[i].IsOccupied && TileIsValidForUs(worldTileList[i])))
                    {
                        tilesAreValid = false;
                        break;
                    }
                }

                //If the tiles are valid, spawn us in them
                if (tilesAreValid)
                {
                    GameObject inWorldObject = Instantiate(LevelManager.Instance.WorldPropObject, worldTileList[0].Position, Quaternion.identity) as GameObject;
                    inWorldObject.GetComponent <LevelPropEntity>().Initialise(objectType, worldTileList, rotationOverride);
                }
                //Otherwise, we can't occupy the tile - the user must delete the existing tile
                else
                {
                    Debug.LogWarning("Tile either isn't occupied or doesn't allow props!");
                    //TODO: nice animation here
                }
                spawnedWorldObject = true;

                //Hide the validity markers
                for (int i = 0; i < LevelManager.Instance.Grid.AllTiles.Count; i++)
                {
                    if (LevelManager.Instance.Grid.AllTiles[i].IsOccupied)
                    {
                        LevelManager.Instance.Grid.AllTiles[i].Occupier.ShowInvalidMarker(false);
                    }
                }
                shownInvalidMarkers = false;
            }

            //Hacky fix for new animated UI
            draggableObject.transform.SetParent(gameObject.transform);
            draggableObject.transform.position = gameObject.transform.position;
        }
    }
Exemple #4
0
 /* Set prop type */
 public void SetPropType(PropTypes _type)
 {
     propType = _type;
     gameObject.GetComponent <Image>().sprite = PropProperties.GetSpriteSet(propType).editorUISprite;
 }