예제 #1
0
        //-------------------------Preperation Step

        //NOTE: if you have 3 actions per edge (none, turn, dodge) and you have 4 edges...
        //you have a total of 3*3*3*3 = 81 possible mappings of tiles
        //this assumes the orientation of the tile MATTERS EX: (none, turn, none, turn) != (turn, none, turn, none)
        //this assumes the orientation of the player to the tile also DOES NOT MATTER
        //IF it did... then 81 mapping * 4 orientation the player can come in would creates 324 possible mappings of tiles
        //NOTE: in either case it makes sense to use this system because there are bound to be alot more than 324 tiles in the game
        void setAllPossibleMappings()
        {
            //GIVEN...
            //tileEdgeAction : byte { none, dodge, turn }; //0 -> 2
            //orientation of player DOES NOT MATTER
            //orientation of tile DOES and is static

            Dictionary <byte, tileEdgeAction> byte_2_TileEdgeAction = new Dictionary <byte, tileEdgeAction>();

            //NOTE: this has to match up with our tileEdgeAction enum
            byte_2_TileEdgeAction.Add(0, tileEdgeAction.none);
            byte_2_TileEdgeAction.Add(1, tileEdgeAction.dodge);
            byte_2_TileEdgeAction.Add(2, tileEdgeAction.turn);

            for (byte n = 0; n < 3; n++)
            {
                for (byte e = 0; e < 3; e++)
                {
                    for (byte s = 0; s < 3; s++)
                    {
                        for (byte w = 0; w < 3; w++)
                        {
                            tileMapping tempTileMapping = new tileMapping();
                            sbyte[]     zero            = dirID_2_dir[0];
                            tempTileMapping.tileActions[_2tuple.combine(zero[0], zero[1])] = byte_2_TileEdgeAction[n];   //N
                            sbyte[] one = dirID_2_dir[1];
                            tempTileMapping.tileActions[_2tuple.combine(one[0], one[1])] = byte_2_TileEdgeAction[e];     //E
                            sbyte[] two = dirID_2_dir[2];
                            tempTileMapping.tileActions[_2tuple.combine(two[0], two[1])] = byte_2_TileEdgeAction[s];     //S
                            sbyte[] three = dirID_2_dir[3];
                            tempTileMapping.tileActions[_2tuple.combine(three[0], three[1])] = byte_2_TileEdgeAction[w]; //W

                            //we have the tileMapping set... now we simply map the 4 number to it
                            tileEdgeActionID_2_tileMapping.Add(_4tuple.combine(n, e, s, w), tempTileMapping); //I assume this passes by REFERENCE
                        }
                    }
                }
            }
        }
예제 #2
0
        IEnumerator readWalls()
        {
            GameObject wallFolder = new GameObject("Walls");

            wallFolder.transform.parent = this.transform;

            GameObject cornerFolder = new GameObject("Corners");

            cornerFolder.transform.parent = this.transform;

            string wallFilePath = System.IO.Path.Combine(Application.streamingAssetsPath, "walls.json");
            string wallFile;

            if (wallFilePath.Contains("://"))
            {
                UnityEngine.Networking.UnityWebRequest www = UnityEngine.Networking.UnityWebRequest.Get(wallFilePath);
                yield return(www.SendWebRequest());

                wallFile = www.downloadHandler.text;
            }
            else
            {
                wallFile = getJsonStringFromFile(wallFilePath);
            }

            AllWallTypes allWallTiles = JsonUtility.FromJson <AllWallTypes>(wallFile);

            //read in the walls from our json file (from prefabs our walls should be set)
            foreach (WallTypes aType in allWallTiles.wallTypes) //for each type of wall
            {
                string objName = aType.wallObjectName;

                //TODO... actually do something different depending on the objName
                //FOR NOW... we spawn the exact same object (wallPrefab)

                foreach (WallData aWall in aType.wallList)
                {
                    //---Read In Data From JSON (understand the readMe.txt first)

                    Vector2 point1 = new Vector2(aWall.point1[0], aWall.point1[1]);
                    Vector2 point2 = new Vector2(aWall.point2[0], aWall.point2[1]);
                    //TODO... read in tint
                    //TODO... read in layer
                    //TODO... read in flip

                    //---Create some vars from JSON

                    //NOTE: one of these will be 0... the other will be some number
                    int   xDist   = (int)Mathf.Abs(point1[0] - point2[0]);
                    int   yDist   = (int)Mathf.Abs(point1[1] - point2[1]);
                    float maxDist = Mathf.Max(xDist, yDist);

                    List <float[]> pointsBetweenTiles = new List <float[]>(); //all the points between 2 tiles that this wall effects

                    bool horizontalWall = (xDist > yDist);

                    //NOTE: our height is always the largest because we need the sprite to spread out in a particular direction
                    float height = maxDist;
                    float width  = (1 / 3f);

                    if (horizontalWall) //horizontal wall
                    {
                        float sameY = point1.y;

                        //given this horizontal wall what are all the points between tiles that we will be effecting
                        float leftMostX = Mathf.Min(point1.x, point2.x);
                        leftMostX += .5f;                             //this is because we start at the very edge of the wall... and this should really be the x center of the leftmost tile

                        for (int extra = 0; extra < maxDist; extra++) //2nd tile because first was taken care off above
                        {
                            pointsBetweenTiles.Add(new float[] { leftMostX + extra, sameY });
                        }
                    }
                    else
                    {
                        float sameX = point1.x;

                        //given this vertical wall what are all the points between tiles that we will be effecting
                        float bottomMostY = Mathf.Min(point1.y, point2.y);
                        bottomMostY += .5f; //this is because we start at the very edge of the wall... and this should really be the x center of the leftmost tile

                        for (int extra = 0; extra < maxDist; extra++)
                        {
                            pointsBetweenTiles.Add(new float[] { sameX, bottomMostY + extra });
                        }
                    }

                    //---Spawn in the Wall

                    GameObject wallTile = Instantiate(wallPrefab, wallFolder.transform);        //spawn and set under folder
                    wallTile.GetComponent <SpriteRenderer>().size = new Vector2(width, height); //set size
                    wallTile.transform.position = Vector2.Lerp(point1, point2, .5f);            //set center
                    if (horizontalWall)
                    {
                        wallTile.transform.rotation = Quaternion.Euler(0, 0, 90);
                    }
                    //TODO... set tint
                    //TODO... set layer
                    //TODO... set flip

                    //---Spawn in the Wall Corners (for both corners)
                    if (vector2ToWallCorner.ContainsKey(point1) == false)
                    {
                        GameObject newCorner = Instantiate(wallCornerPrefab, cornerFolder.transform);
                        newCorner.transform.position = point1;
                    }
                    if (vector2ToWallCorner.ContainsKey(point2) == false)
                    {
                        GameObject newCorner = Instantiate(wallCornerPrefab, cornerFolder.transform);
                        newCorner.transform.position = point2;
                    }

                    //---Go through all the units this wall covers and have them effect the 2 floors to the left and right

                    foreach (float[] arr in pointsBetweenTiles)                       //for each segment (horizontal or vertical)
                    {
                        for (float f = -.05f, iteration = 1; f < 1; f++, iteration++) //for each of the 2 walls affected per segment
                        {
                            float tileX = 0;
                            float tileY = 0;
                            if (horizontalWall)
                            {
                                tileX = Mathf.RoundToInt(arr[0]);
                                tileY = Mathf.RoundToInt(arr[1] + f);
                            }
                            else
                            {
                                tileX = Mathf.RoundToInt(arr[0] + f);
                                tileY = Mathf.RoundToInt(arr[1]);
                            }

                            int tileXInt = Mathf.RoundToInt(tileX);
                            int tileYInt = Mathf.RoundToInt(tileY);

                            ulong z = _2tuple.combine(tileXInt, tileYInt);
                            if (tileID_2_tileAction.ContainsKey(z))
                            {
                                tileAction  regularAction     = tileID_2_tileAction[z];
                                tileMapping regularMaping     = tileAction_2_DefaultMapping[regularAction];
                                tileMapping regularMapingCopy = new tileMapping();

                                ushort n = _2tuple.combine((sbyte)0, (sbyte)1);
                                ushort e = _2tuple.combine((sbyte)1, (sbyte)0);
                                ushort s = _2tuple.combine((sbyte)0, (sbyte)-1);
                                ushort w = _2tuple.combine((sbyte)-1, (sbyte)0);

                                regularMapingCopy.tileActions[n] = regularMaping.tileActions[n];
                                regularMapingCopy.tileActions[e] = regularMaping.tileActions[e];
                                regularMapingCopy.tileActions[s] = regularMaping.tileActions[s];
                                regularMapingCopy.tileActions[w] = regularMaping.tileActions[w];

                                ushort dir = 0;
                                if (horizontalWall)
                                {
                                    //first bottom THEN top
                                    if (iteration == 1)
                                    {
                                        dir = _2tuple.combine((sbyte)0, (sbyte)1);
                                    }
                                    else
                                    {
                                        dir = _2tuple.combine((sbyte)0, (sbyte)-1);
                                    }
                                }
                                else
                                {
                                    //first left THEN right (so towards right and then left)
                                    if (iteration == 1)
                                    {
                                        dir = _2tuple.combine((sbyte)1, (sbyte)0);
                                    }
                                    else
                                    {
                                        dir = _2tuple.combine((sbyte)-1, (sbyte)0);
                                    }
                                }

                                //---other code

                                regularMapingCopy.tileActions[dir] = tileEdgeAction.none;

                                byte north = (byte)regularMapingCopy.tileActions[n];
                                byte east  = (byte)regularMapingCopy.tileActions[e];
                                byte south = (byte)regularMapingCopy.tileActions[s];
                                byte west  = (byte)regularMapingCopy.tileActions[w];

                                //map this new special tile to a number
                                uint newTileEdgeActionsID = _4tuple.combine(north, east, south, west);

                                if (tileID_2_TileEdgeActionsID.ContainsKey(z))
                                {
                                    tileID_2_TileEdgeActionsID[z] = newTileEdgeActionsID;
                                }
                                else
                                {
                                    tileID_2_TileEdgeActionsID.Add(z, newTileEdgeActionsID);
                                }
                            }
                            //ELSE... this tile is out of bounds we cant assign instructions to it
                        }
                    }
                }
            }
        }