Beispiel #1
0
    public GameObject SetWallTile(Coords2 coords, GameObject tile)
    {
        GameObject placedTile = SetTile(coords, tile, wallTileLayer);

        WallTileChanged(coords);
        return(placedTile);
    }
Beispiel #2
0
    private int WallNeighbours(Coords2 tile)
    {
        int wallNeighbourCount = 0;

        int minX = Mathf.Max(0, tile.x - 1);
        int minY = Mathf.Max(0, tile.y - 1);
        int maxX = Mathf.Max(width - 1, tile.x + 1);
        int maxY = Mathf.Max(height - 1, tile.y + 1);

        for (int x = minX; x <= maxX; ++x)
        {
            for (int y = minY; y <= maxY; ++y)
            {
                if (x == tile.x && y == tile.y)
                {
                    continue;
                }

                if (isWall[x, y])
                {
                    wallNeighbourCount += 1;
                }
            }
        }

        return(wallNeighbourCount);
    }
    private void UpdateTileVisibility(Coords2 coords, bool[,] isVisited)
    {
        //in this type of fog a tile can be either visible or not, fractional opacity is not supported
        const float opacityThreshold = 0.5f;

        if (map.Opacity(coords) > opacityThreshold)
        {
            return;
        }
        if (isVisited[coords.x, coords.y] == true)
        {
            return;
        }

        isVisited[coords.x, coords.y]      = true;
        tileVisibility[coords.x, coords.y] = Visibility.Visible;

        //flood fill room
        if (coords.x > 0)
        {
            UpdateTileVisibility(new Coords2(coords.x - 1, coords.y), isVisited);
        }
        if (coords.y > 0)
        {
            UpdateTileVisibility(new Coords2(coords.x, coords.y - 1), isVisited);
        }
        if (coords.x < textureSize - 1)
        {
            UpdateTileVisibility(new Coords2(coords.x + 1, coords.y), isVisited);
        }
        if (coords.y < textureSize - 1)
        {
            UpdateTileVisibility(new Coords2(coords.x, coords.y + 1), isVisited);
        }
    }
Beispiel #4
0
    //tries to carve through walls
    private int TryMakeConnection(Coords2 pos, Coords2 direction, int currentRegionId)
    {
        Coords2 perpToDirection = new Coords2(direction.y, direction.x);

        int length = 1;
        int x      = pos.x + direction.x;
        int y      = pos.y + direction.y;

        while (isInsideBounds(x, y) && regionIds[x, y] == -1)
        {
            Coords2 perp1 = new Coords2(x, y) + perpToDirection;
            Coords2 perp2 = new Coords2(x, y) - perpToDirection;
            if (!isInsideBounds(perp1.x, perp1.y) || !isWall[perp1.x, perp1.y])
            {
                return(0);                                                                //connection must have walls on each side through whole length
            }
            if (!isInsideBounds(perp2.x, perp2.y) || !isWall[perp2.x, perp2.y])
            {
                return(0);
            }

            ++length;
            x += direction.x;
            y += direction.y;
        }

        if (!isInsideBounds(x, y) || regionIds[x, y] == 0)
        {
            return(0);
        }

        return(length);
    }
Beispiel #5
0
 //emit event
 private void WallTileChanged(Coords2 coords)
 {
     if (OnWallTileChanged != null)
     {
         OnWallTileChanged(coords);
     }
 }
 public override bool Interact(Coords2 coords, Player player)
 {
     PlayMiniGame();
     //If we finish playing mini game we chage status to false
     isGameOn = false;
     return(true);
 }
Beispiel #7
0
        public HexGrid(HexLayout _layout, HexShape _shape, Coords2 _size)
        {
            layout = _layout;
            shape  = _shape;

            hexMap = shape.GenerateHaxMap(_size);
        }
Beispiel #8
0
    private void GetTilesInsideRoomAndFill(List <Coords2> tiles, Coords2 startPos, bool[,] isWall)
    {
        Stack <Coords2> callStack = new Stack <Coords2>();

        callStack.Push(new Coords2(startPos.x, startPos.y));

        while (callStack.Count > 0)
        {
            Coords2 pos = callStack.Pop();
            int     x   = pos.x;
            int     y   = pos.y;

            isWall[x, y] = true;

            tiles.Add(new Coords2(x, y));

            if (x > 0 && !isWall[x - 1, y])
            {
                callStack.Push(new Coords2(x - 1, y));
            }
            if (y > 0 && !isWall[x, y - 1])
            {
                callStack.Push(new Coords2(x, y - 1));
            }
            if (x < width - 1 && !isWall[x + 1, y])
            {
                callStack.Push(new Coords2(x + 1, y));
            }
            if (y < height - 1 && !isWall[x, y + 1])
            {
                callStack.Push(new Coords2(x, y + 1));
            }
        }
    }
Beispiel #9
0
    public GameObject CreateWorldObject(Coords2 coords, GameObject tile)
    {
        GameObject instantiated = Instantiate(tile, new Vector3(coords.x, coords.y, 0f), Quaternion.identity) as GameObject;

        CreateWorldObjectDummies(coords, instantiated);
        return(SetWorldObject(coords, instantiated));
    }
Beispiel #10
0
    Color ChooseColor(Coords2 coords)
    {
        Color color = new Color(0.0f, 0.0f, 0.0f, 0.0f);

        float opacity = map.Opacity(coords);
        color.a = opacity;
        return color;
    }
Beispiel #11
0
 public void SetEnabled(Coords2 coords, bool newIsOn)
 {
     isOn = newIsOn;
     if (OnCameraStateChanged != null)
     {
         OnCameraStateChanged(coords, this);
     }
 }
Beispiel #12
0
 //every modifying function has to have coords passed because they are needed for emitting an event
 //TODO: consider storing coords as a property
 public void SetOrigin(Coords2 coords, Vector2 newOrigin)
 {
     origin = newOrigin;
     if (OnCameraStateChanged != null)
     {
         OnCameraStateChanged(coords, this);
     }
 }
Beispiel #13
0
    private GameObject GetTile(Coords2 coords, GameObject[,] layer)
    {
        if (!IsInsideBounds(coords))
        {
            return(null);
        }

        return(layer[coords.x, coords.y]);
    }
Beispiel #14
0
 public RoomConnection(Coords2 initialPosition, Coords2 finalPosition, Coords2 direction, int initialRegionId, int finalRegionId, int length)
 {
     this.initialPosition = initialPosition;
     this.finalPosition   = finalPosition;
     this.direction       = direction;
     this.initialRegionId = initialRegionId;
     this.finalRegionId   = finalRegionId;
     this.length          = length;
 }
Beispiel #15
0
 private void CreateDummies(Coords2 coords, GameObject tile, GameObject[,] layer, GameObject dummyPrefab, Coords2[] offsets)
 {
     foreach (Coords2 offset in offsets)
     {
         Coords2    pos   = coords + offset;
         GameObject dummy = Instantiate(dummyPrefab, new Vector3(pos.x, pos.y, 0f), Quaternion.identity) as GameObject;
         dummy.GetComponent <MultitileWorldObjectDummyController>().SetOwner(tile);
         SetTile(pos, dummy, layer);
     }
 }
Beispiel #16
0
    private void CreateWallDummies(Coords2 coords, GameObject tile)
    {
        GameObject         wallDummyPrefab = GameManager.instance.GetPrefabs().multitileWallDummy;
        WallTileController controller      = tile.GetComponent <WallTileController>();

        if (controller == null)
        {
            return;
        }
        CreateDummies(coords, tile, wallTileLayer, wallDummyPrefab, controller.GetDummiesToCreate());
    }
Beispiel #17
0
    //only sets reference to passed tile
    private GameObject SetTile(Coords2 coords, GameObject tile, GameObject[,] layer)
    {
        if (!IsInsideBounds(coords))
        {
            return(null);
        }

        RemoveTile(coords, layer);
        layer[coords.x, coords.y] = tile;
        return(tile);
    }
Beispiel #18
0
    private void RemoveTile(Coords2 coords, GameObject[,] layer)
    {
        if (!IsInsideBounds(coords))
        {
            return;
        }
        if (layer[coords.x, coords.y] == null)
        {
            return;
        }

        Destroy(layer[coords.x, coords.y]);
        layer[coords.x, coords.y] = null;
    }
Beispiel #19
0
    private List <Coords2> GetTilesInsideRoom(Coords2 origin)
    {
        if (!isInsideBounds(origin.x, origin.y))
        {
            return(new List <Coords2>());
        }

        bool[,] isWallLocal = isWall.Clone() as bool[, ];
        List <Coords2> tiles = new List <Coords2>();

        GetTilesInsideRoomAndFill(tiles, origin, isWallLocal);

        return(tiles);
    }
Beispiel #20
0
 public override bool Interact(Coords2 coords, Player player)
 {
     spriteRenderer.sprite = dirtSprite[dirtState];
     if (dirtState > 0)
     {
         dirtState--;
     }
     else
     {
         isDestroyed = true;
     }
     if (isDestroyed)
     {
         GameManager.instance.GetMap().RemoveWallTile(coords);
     }
     return(true);
 }
Beispiel #21
0
    //return true if tile has boxcollider if wall controller is not present or value specified by wallcontroller
    public bool HasCollider(Coords2 coords)
    {
        GameObject tile = GetWallTile(coords);

        if (tile == null)
        {
            return(false);
        }

        WallTileController controller = tile.GetComponent <WallTileController>();

        if (controller == null)
        {
            return(tile.GetComponent <BoxCollider2D>() != null);
        }

        return(controller.HasCollider());
    }
Beispiel #22
0
    //fills tiles inside the room and ALL walls
    private void FillFloorTilesInsideRoom(Coords2 origin)
    {
        for (int x = 0; x < width; ++x)
        {
            for (int y = 0; y < height; ++y)
            {
                if (isWall[x, y])
                {
                    floorLayerTiles[x, y] = RandomizeTile(floorTilePrefabs);
                }
            }
        }

        foreach (Coords2 pos in GetTilesInsideRoom(new Coords2(origin.x, origin.y)))
        {
            floorLayerTiles[pos.x, pos.y] = RandomizeTile(floorTilePrefabs);
        }
    }
Beispiel #23
0
    //return 1.0f if tile has boxcollider if wall controller is not present or value specified by wallcontroller
    public float Opacity(Coords2 coords)
    {
        GameObject tile = GetWallTile(coords);

        if (tile == null)
        {
            return(0.0f);
        }

        WallTileController controller = tile.GetComponent <WallTileController>();

        if (controller == null)
        {
            return(tile.GetComponent <BoxCollider2D>() != null ? 1.0f : 0.0f);
        }

        return(controller.Opacity());
    }
Beispiel #24
0
    //removes walls and places door on random location on path
    private void PlaceDoorInsideConnection(RoomConnection con)
    {
        int     length     = con.length;
        Coords2 erasingPos = con.initialPosition;
        Coords2 direction  = con.direction;

        int depth = Random.Range(1, length);

        for (int i = 1; i < length; ++i)
        {
            erasingPos += direction;
            isWall[erasingPos.x, erasingPos.y] = false;
        }
        Coords2 doorPos = con.initialPosition + direction * depth;

        GameObject[] prefabs = direction.x == 0 ? verticalDoorTilePrefabs : horizontalDoorTilePrefabs;
        wallLayerTiles[doorPos.x, doorPos.y] = RandomizeTile(prefabs);
    }
Beispiel #25
0
    //requires object to have worldobjectcontroller
    //should be called when wallobjectcontroller.interact returns false or is not called
    private bool InteractObject(Coords2 coords, Player player)
    {
        GameObject tile = GetWorldObject(coords);

        if (tile == null)
        {
            return(false);
        }

        WorldObjectController controller = tile.GetComponent <WorldObjectController>();

        if (controller == null)
        {
            return(false);
        }

        return(controller.Interact(coords, player));
    }
Beispiel #26
0
    public override void OnHitWall(WallTileController wall)
    {
        Map     map = GameManager.instance.GetMap();
        Coords2 centerOfExplosion = new Coords2((int)(transform.position.x + 0.5f), (int)(transform.position.y + 0.5f));

        for (int i = -1; i <= 1; i++)
        {
            for (int j = -1; j <= 1; j++)
            {
                map.RemoveWallTile(new Coords2(i, j) + centerOfExplosion);
            }
        }
        GameObject explosion = Instantiate(explosionAnimation, new Vector3(centerOfExplosion.x, centerOfExplosion.y), Quaternion.identity) as GameObject;

        explosion.transform.localScale = new Vector3(2.0f, 2.0f, 2.0f);
        Destroy(explosion, 2.0f);
        isDead = true;
    }
Beispiel #27
0
    private void CreateRoomConnections()
    {
        List <RoomConnection> connections = new List <RoomConnection>();

        Coords2[] directions = { new Coords2(0, 1), new Coords2(1, 0) }; //only 2 directions to omit repetitions

        //tries to 'carve' to the other room, saves the connection info if successful
        for (int x = 0; x < width - 1; ++x)
        {
            for (int y = 0; y < height - 1; ++y)
            {
                int currentRegionId = regionIds[x, y];
                if (currentRegionId < 1)
                {
                    continue;
                }

                for (int d = 0; d < directions.Length; ++d)
                {
                    Coords2 direction = directions[d];
                    int     xx        = x + direction.x;
                    int     yy        = y + direction.y;
                    if (isWall[xx, yy])
                    {
                        Coords2 start            = new Coords2(x, y);
                        int     connectionLength = TryMakeConnection(start, direction, currentRegionId);
                        if (connectionLength != 0)
                        {
                            Coords2 destination         = start + direction * connectionLength;
                            int     destinationRegionId = regionIds[destination.x, destination.y];
                            connections.Add(new RoomConnection(start, destination, direction, currentRegionId, destinationRegionId, connectionLength));
                        }
                    }
                }
            }
        }

        List <RoomConnection> chosenConnections = ChooseConnections(connections);

        foreach (RoomConnection con in chosenConnections)
        {
            PlaceDoorInsideConnection(con);
        }
    }
Beispiel #28
0
 void OnCameraStateChanged(Coords2 coords, CameraObjectController cameraController)
 {
     Camera camera;
     if (cameras.TryGetValue(coords, out camera))
     {
         camera.isOn = cameraController.IsOn();
         camera.origin = cameraController.GetOrigin();
     }
     else
     {
         //if camera is not found in registry create a new one
         camera = new Camera();
         camera.origin = cameraController.GetOrigin();
         camera.isOn = cameraController.IsOn();
         //get an unused shader property id from stack to use for that camera
         camera.shaderPropertyId = unusedShaderPropertyIds.Pop();
         cameras.Add(coords, camera);
     }
     UpdateShaderProperty(camera);
 }
Beispiel #29
0
    //samples n discrete points and returns the closest one to the reference point
    private Coords2 SampleClosePoint(List <Coords2> points, Vector2 reference, int numberOfSamples)
    {
        float   minDistSquared = float.MaxValue;
        Coords2 min            = new Coords2(0, 0);

        for (int i = 0; i < numberOfSamples; ++i)
        {
            Coords2 sample      = points[Random.Range(0, points.Count)];
            float   dx          = reference.x - sample.x;
            float   dy          = reference.y - sample.y;
            float   distSquared = dx * dx + dy * dy;

            if (distSquared < minDistSquared)
            {
                minDistSquared = distSquared;
                min            = sample;
            }
        }

        return(new Coords2(min.x, min.y));
    }
Beispiel #30
0
    //Toggles between open/closed, returns true indicating that some action was performed
    public override bool Interact(Coords2 coords, Player player)
    {
        isOpen = !isOpen;

        Sprite spriteToUse;

        if (isOpen)
        {
            audioSource.PlayOneShot(openSounds[Random.Range(0, openSounds.Length)], soundVolume);
            spriteToUse = openedSprite;
        }
        else
        {
            audioSource.PlayOneShot(closeSounds[Random.Range(0, closeSounds.Length)], soundVolume);
            spriteToUse = closedSprite;
        }

        spriteRenderer.sprite = spriteToUse;

        return(true);
    }