Esempio n. 1
0
    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();

        //EditorGUILayout.LabelField("宽度");
        //mapWidth = EditorGUILayout.IntSlider(mapWidth, 10, 200);

        //EditorGUILayout.LabelField("长度");
        //mapHeight = EditorGUILayout.IntSlider(mapHeight, 10, 200);
        mapGenerator.seed = mapGenerator.mapSetting.seed;


        if (GUILayout.Button("清空地图"))
        {
            mapGenerator.ClearMap();
        }
        using (var check = new EditorGUI.ChangeCheckScope())
        {
            generationType = (MapType)EditorGUILayout.EnumPopup("生成地图类型", generationType);
            Editor editor = CreateEditor(mapGenerator.mapSetting);
            editor.OnInspectorGUI();
            if (check.changed || GUILayout.Button("预览地图"))
            {
                generateMethod = new GenerateMap(GenerateMapType(generationType));
                generateMethod();
                //Debug.Log(generationType);
            }
        }
        if (GUILayout.Button("铺瓦片"))
        {
            mapGenerator.GenerateBinaryMap();
        }
        if (GUILayout.Button("获取瓦片资源"))
        {
            TileManager.Instance.InitData();
            RoomManager.Instance.InitData();
        }
        pos = EditorGUILayout.Vector2IntField("坐标", pos);
        if (GUILayout.Button("击打障碍物 hp 20/次"))
        {
            Debug.Log("button down");
            Vector3Int   targetPos = new Vector3Int(pos.x, pos.y, 0);
            ObstacleTile tile      = (ObstacleTile)mapGenerator.tilemap.GetTile(targetPos);
            tile.hps[targetPos] -= 20;
            //mapGenerator.tilemap.RefreshAllTiles();
            mapGenerator.tilemap.RefreshTile(targetPos);
        }
        if (GUILayout.Button("刷新地图"))
        {
            mapGenerator.tilemap.RefreshAllTiles();
        }
    }
Esempio n. 2
0
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (!collision.gameObject.tag.Contains("Player"))
        {
            if (collision.gameObject.tag.Contains("Enemy") || collision.gameObject.tag.Contains("Destroyable"))
            {
                if (EventDispatcher.OnHitActions.ContainsKey(collision.gameObject))
                {
                    EventDispatcher.DispatchGameobjectAction(collision.gameObject);
                }
                if (collision.gameObject.tag.Contains("Enemy"))
                {
                    DiffultyAdjuster.playerHitTimes += 1;
                }
                timer = 0f;
                this.gameObject.SetActive(false);
                GameManager.playerProjectiles.Enqueue(this.gameObject);
            }
        }
        if (collision.gameObject.tag.Equals("Tilemap"))
        {
            //Debug.Log(collision.gameObject.name);

            //ContactPoint2D[] contacts=new ContactPoint2D[5];
            Vector3 targetPos;
            //collision.GetContacts(contacts);
            //foreach (ContactPoint2D hit in contacts)
            //{
            //    if (hit.point != Vector2.zero)
            //    {
            targetPos = transform.position + new Vector3(preSpeed.normalized.x, preSpeed.normalized.y, 0);
            TileBase tile = TileManager.Instance.GetTile(TileManager.Instance.WorldToCell(targetPos));
            //Debug.Log(targetPos +" "+ TileManager.Instance.WorldToCell(targetPos) + "  " + tile.name);

            if (tile is ObstacleTile)
            {
                ObstacleTile obstacle = (ObstacleTile)tile;
                obstacle.hps[TileManager.Instance.WorldToCell(targetPos)] -= 20;
                TileManager.Instance.RefreshTile(TileManager.Instance.WorldToCell(targetPos));
            }
            //    }
            //}
            this.gameObject.SetActive(false);
            GameManager.playerProjectiles.Enqueue(this.gameObject);
        }
    }
Esempio n. 3
0
 private void OnCollisionEnter2D(Collision2D collision)
 {
     //Debug.Log(collision.contacts.Length);
     if (collision.gameObject.tag.Equals("Player"))
     {
         EventDispatcher.hitPlayer(attackPoint);
     }
     if (collision.gameObject.tag.Equals("Tilemap"))
     {
         foreach (ContactPoint2D hit in collision.contacts)
         {
             if (TileManager.Instance.GetTile(TileManager.Instance.WorldToCell(hit.point)) is ObstacleTile)
             {
                 ObstacleTile temp = (ObstacleTile)TileManager.Instance.GetTile(TileManager.Instance.WorldToCell(hit.point));
                 temp.hps[TileManager.Instance.WorldToCell(hit.point)] -= this.attackPoint;
             }
         }
     }
 }
    public void AddTile(ObjectPool pool, RoadTileManager roadManager, Vector2 tilePosition, float hardness = 1)
    {
        int additionalCells = 0;

        // Get road positions and fix tile position
        Vector2[] roadPositions = roadManager.LookForPositionsOnSameY(tilePosition.y);
        if (roadPositions != null && roadPositions.Length > 0)
        {
            float mostLeft            = 0;
            float mostRight           = 0;
            float sumOfRoadPositionsX = 0;
            for (int pos_i = 0; pos_i < roadPositions.Length; pos_i++)
            {
                sumOfRoadPositionsX += roadPositions[pos_i].x;
                if (mostLeft > roadPositions[pos_i].x)
                {
                    mostLeft = roadPositions[pos_i].x;
                }
                if (mostRight < roadPositions[pos_i].x)
                {
                    mostRight = roadPositions[pos_i].x;
                }
            }

            tilePosition    = new Vector2(sumOfRoadPositionsX / roadPositions.Length, tilePosition.y);
            additionalCells = (int)((mostRight - mostLeft) / CellSize);
        }
        else
        {
            return; // Road is not ready. Skip.
        }
        // Get and setup new Tile
        ObstacleTile newTile = pool.GetObject(ExampleTile.name).GetComponent <ObstacleTile>();

        newTile.transform.parent   = transform;
        newTile.transform.position = tilePosition;
        newTile.Setup(pool, roadPositions, SampleObstacles.obstacles, CellSize, MinCellCount);
        ObstacleTiles.Add(newTile);
    }
Esempio n. 5
0
    private void GenerateGridFromCSV(string filepath, string filename)
    {
        Tile[] obstacleTiles =
        {
            new Tile()
            {
                tile = (GameObject)Instantiate(Resources.Load("tileset-481")),
                type = "ObstacleTile"
            },
            new Tile()
            {
                tile = (GameObject)Instantiate(Resources.Load("tileset-484")),
                type = "ObstacleTile"
            },
            new Tile()
            {
                tile = (GameObject)Instantiate(Resources.Load("tileset-487")),
                type = "ObstacleTile"
            },
            new Tile()
            {
                tile = (GameObject)Instantiate(Resources.Load("tileset-489")),
                type = "ObstacleTile"
            },
            new Tile()
            {
                tile = (GameObject)Instantiate(Resources.Load("tileset-554")),
                type = "ObstacleTile"
            }
        };

        Tile[] normalTiles =
        {
            new Tile()
            {
                tile = (GameObject)Instantiate(Resources.Load("tileset-105")),
                type = "NormalTile"
            }, // uncracked
            new Tile()
            {
                tile = (GameObject)Instantiate(Resources.Load("tileset-81")),
                type = "NormalTile"
            },
            new Tile()
            {
                tile = (GameObject)Instantiate(Resources.Load("tileset-82")),
                type = "NormalTile"
            },
            new Tile()
            {
                tile = (GameObject)Instantiate(Resources.Load("tileset-83")),
                type = "NormalTile"
            },
            new Tile()
            {
                tile = (GameObject)Instantiate(Resources.Load("tileset-103")),
                type = "NormalTile"
            },
            new Tile()
            {
                tile = (GameObject)Instantiate(Resources.Load("tileset-104")),
                type = "NormalTile"
            },
            new Tile()
            {
                tile = (GameObject)Instantiate(Resources.Load("tileset-292")),
                type = "NormalTile"
            },
            new Tile()
            {
                tile = (GameObject)Instantiate(Resources.Load("tileset-293")),
                type = "NormalTile"
            },
            new Tile()
            {
                tile = (GameObject)Instantiate(Resources.Load("tileset-294")),
                type = "NormalTile"
            }
        };

        Tile start = new Tile()
        {
            tile = (GameObject)Instantiate(Resources.Load("StartTile")),
            type = "StartTile"
        };

        Tile end = new Tile()
        {
            tile = (GameObject)Instantiate(Resources.Load("EndTile")),
            type = "EndTile"
        };

        Tile trap = new Tile()
        {
            tile = (GameObject)Instantiate(Resources.Load("SpikeTile")),
            type = "TrapTile"
        };

        Tile ice = new Tile()
        {
            tile = (GameObject)Instantiate(Resources.Load("IceTile")),
            type = "IceTile"
        };


        string[,] GridCSV = CsvUtil.readData(filepath, filename);
        rows = GridCSV.GetLength(0);
        cols = GridCSV.GetLength(1);

        float centerOffsetX = -cols * tileSize / 2; // center
        float centerOffsetY = rows * tileSize / 2;

        for (int i = 0; i < GridCSV.GetLength(0); i++)
        {
            List <Tile> tileRow = new List <Tile>();

            for (int j = 0; j < GridCSV.GetLength(1); j++)
            {
                GameObject tile;
                if (GridCSV[i, j] == "S")
                {
                    tile = (GameObject)Instantiate(start.tile, transform);
                    StartTile startTile = new StartTile()
                    {
                        tile = tile,
                        type = "StartTile"
                    };
                    tileRow.Add(startTile);
                }
                else if (GridCSV[i, j] == "E")
                {
                    tile = (GameObject)Instantiate(end.tile, transform);
                    EndTile endTile = new EndTile()
                    {
                        tile = tile,
                        type = "EndTile"
                    };
                    tileRow.Add(endTile);
                }
                else if (GridCSV[i, j] == "W") //W = wallobstacle
                {
                    int randomChoice = (int)Random.Range(0, obstacleTiles.Length - 0.001f);
                    tile = (GameObject)Instantiate(obstacleTiles[randomChoice].tile, transform);
                    ObstacleTile obstacleTile = new ObstacleTile()
                    {
                        tile = tile,
                        type = "ObstacleTile"
                    };
                    tileRow.Add(obstacleTile);
                }
                else if (GridCSV[i, j] == "T")
                {
                    tile = (GameObject)Instantiate(trap.tile, transform);
                    TrapTile trapTile = new TrapTile()
                    {
                        tile = tile,
                        type = "TrapTile"
                    };
                    tileRow.Add(trapTile);
                }
                else if (GridCSV[i, j] == "I")
                {
                    tile = (GameObject)Instantiate(ice.tile, transform);
                    IceTile iceTile = new IceTile()
                    {
                        tile = tile,
                        type = "IceTile"
                    };
                    tileRow.Add(iceTile);
                }
                else
                {
                    int randomChoice = (int)Random.Range(0, normalTiles.Length - 0.001f);
                    tile = (GameObject)Instantiate(normalTiles[randomChoice].tile, transform);
                    NormalTile normalTile = new NormalTile()
                    {
                        tile = tile,
                        type = "NormalTile"
                    };
                    tileRow.Add(normalTile);
                }

                float posX = j * tileSize + centerOffsetX;
                float posY = i * -tileSize + centerOffsetY;
                if (GridCSV[i, j] == "S")
                {
                    StartPos = new Vector2(posX, posY);
                }
                tile.transform.position = new Vector2(posX, posY);
            }
            Tiles.Add(tileRow);
        }

        foreach (Tile obstacleTile in obstacleTiles)
        {
            Destroy(obstacleTile.tile);
        }
        foreach (Tile normalTile in normalTiles)
        {
            Destroy(normalTile.tile);
        }
        Destroy(start.tile);
        Destroy(end.tile);
        Destroy(trap.tile);
        Destroy(ice.tile);

        //bool x = await Task.FromResult(false);
    }