Exemple #1
0
 /// <summary>
 /// Initialize只在第一次创建时执行初始化代码,之后切换Scene时都不用再次初始化,所以Data也没有改变
 /// </summary>
 /// <param name="gameObject"></param>
 public IPlayer(GameObject gameObject) : base(gameObject)
 {
     if (GameObjectInScene != null)
     {
         animator = GameObjectInScene.GetComponent <Animator>();
         Rg2d     = GameObjectInScene.GetComponent <Rigidbody2D>();
     }
 }
Exemple #2
0
 public override void Dead()
 {
     base.Dead();
     animator.SetTrigger(aniDead);
     bt.DisableBehavior();
     GameObjectInScene.GetComponent <Collider>().enabled = false;
     GameMainProgram.Instance.eventMgr.InvokeEvent(EventName.BossDead);
 }
Exemple #3
0
    public static void SaveMap(string fileName)
    {
        SaveLoad.savedMap = Game.tiles;

        BinaryFormatter bf = new BinaryFormatter();
        //Application.persistentDataPath is a string, so if you wanted you can put that into debug.log if you want to know where save games are located

        FileStream file = File.Create(string.Format(
                                          "{0}/{1}.OMEGALUL",
                                          folderName,
                                          fileName
                                          ));

        // What it does
        // ============================
        // First, it takes the map's width and height and stores it into the class GameData.
        // Then, it will search the entire map for tiles that aren't Grids. Add them into a List of type GameObjectInScene.
        // The GameObjectInScene will store its position, rotation, name and scale.
        // After storing them into GameData, we will serialize GameData into a Json string. Stream the Json string into a folder.

        MapData gameData = new MapData()
        {
            mapWidth  = Game.gridWidth,
            mapHeight = Game.gridHeight
        };

        List <GameObjectInScene> tileList = new List <GameObjectInScene>();

        int count = 0, index = 0;

        foreach (var t in Game.tiles)
        {
            if (t.name == "Grid")
            {
                continue;
            }

            tileList.Add(new GameObjectInScene(t.name, t.transform.localScale, t.transform.position, t.transform.rotation));

            count++;
        }

        gameData.tiles = new GameObjectInScene[count];

        foreach (GameObjectInScene tile in tileList)
        {
            GameObjectInScene go = new GameObjectInScene(tile.name, tile.scale, tile.position, tile.rotation);

            gameData.tiles[index] = go;
            index++;
        }

        string json = JsonUtility.ToJson(gameData);

        bf.Serialize(file, json);
        file.Close();
    }
Exemple #4
0
 public IEnemy(GameObject gameObject) : base(gameObject)
 {
     if (GameObjectInScene != null)
     {
         animator = GameObjectInScene.GetComponent <Animator>();
         Rg2d     = GameObjectInScene.GetComponent <Rigidbody2D>();
         // 关联中介者
         EnemyMedi = new EnemyMediator(this);
         EnemyMedi.Initialize();
     }
 }
Exemple #5
0
 /// <summary>
 /// Initialize只在第一次创建时执行初始化代码,之后切换Scene时都不用再次初始化,所以Data也没有改变
 /// </summary>
 /// <param name="gameObject"></param>
 public IPlayer(GameObject gameObject) : base(gameObject)
 {
     Speed = 5;
     if (GameObjectInScene != null)
     {
         animator = GameObjectInScene.GetComponent <Animator>();
         Rgbd     = GameObjectInScene.GetComponent <Rigidbody>();
         // 关联中介者
         PlayerMedi = new PlayerMediator(this);
         PlayerMedi.Initialize();
     }
 }
Exemple #6
0
 /// <summary>
 /// Initialize只在第一次创建时执行初始化代码,之后切换Scene时都不用再次初始化,所以Data也没有改变
 /// </summary>
 /// <param name="gameObject"></param>
 public IPlayer(GameObject gameObject) : base(gameObject)
 {
     Props = GameMainProgram.Instance.dataBaseMgr.Props;
     if (GameObjectInScene != null)
     {
         animator = GameObjectInScene.GetComponent <Animator>();
         Rgbd     = GameObjectInScene.GetComponent <Rigidbody>();
         // 关联中介者
         PlayerMedi = new PlayerMediator(this);
         PlayerMedi.Initialize();
     }
 }
Exemple #7
0
        public override void Dead()
        {
            base.Dead();
            animator.SetTrigger(aniDead);
            bt.DisableBehavior();
            hasFlail = true;
            GameObjectInScene.GetComponent <Collider>().enabled = false;
            GameMainProgram.Instance.eventMgr.InvokeEvent(EventName.BossDead);
            // 渐隐消失
            CaptainMono captainMono = EnemyMedi.EnemyMono as CaptainMono;

            CoroutineMgr.Instance.StartCoroutine(captainMono.DeadFadeOut());
        }
Exemple #8
0
 public IEnemy(GameObject gameObject) : base(gameObject)
 {
     Type = EnemyType.Monster;
     if (GameObjectInScene != null)
     {
         animator     = GameObjectInScene.GetComponent <Animator>();
         Rgbd         = GameObjectInScene.GetComponent <Rigidbody>();
         bt           = GameObjectInScene.GetComponent <BehaviorTree>();
         navMeshAgent = GameObjectInScene.GetComponent <NavMeshAgent>();
         // 关联中介者
         EnemyMedi = new EnemyMediator(this);
         EnemyMedi.Initialize();
     }
 }
 /// <summary>
 /// Initialize只在第一次创建时执行初始化代码,之后切换Scene时都不用再次初始化,所以Data也没有改变
 /// </summary>
 /// <param name="gameObject"></param>
 public IPlayer(GameObject gameObject) : base(gameObject)
 {
     EquipPack = new IEquip[35];
     PropPack  = new IProp[35];
     Fit       = new int[8] {
         99, 99, 99, 99, 99, 99, 99, 99
     };                                          // 初始值>34表示未装备
     if (GameObjectInScene != null)
     {
         animator = GameObjectInScene.GetComponent <Animator>();
         Rgbd     = GameObjectInScene.GetComponent <Rigidbody>();
         // 关联中介者
         PlayerMedi = new PlayerMediator(this);
         PlayerMedi.Initialize();
     }
     // LoadData(); 请在子类构造函数最尾处调用
 }
Exemple #10
0
    public void GenerateMapFromJson(string json)
    {
        MapData gameData = JsonUtility.FromJson <MapData>(json);

        GameObjectInScene[] tileArray = new GameObjectInScene[gameData.mapWidth * gameData.mapHeight];
        GameObjectInScene[] tileData  = gameData.tiles;

        // What it does
        // ============================
        // First, it loads the map's width and height from the Json file.
        // Then, it will search for GameObjectInScenes stored in the Json file.
        // Create tiles using the position, rotation, scale and name from the GameObjectInScene.
        // After creating tiles FROM the stored data, we will then create Grids.
        // Grids are created by searching for a tile's position. If the tile is empty, create at its position.

        // Loops from y to mapHeight then x to mapWidth.
        Vector3 tilePos = new Vector3();
        int     count   = 0;

        foreach (GameObjectInScene t in tileData)
        {
            GameObject tile = AddTile(t.name, t.position, t.rotation.eulerAngles.z);
            tileArray[count++] = t;

            //playing the game
            if (!Main.EditorMode)
            {
                switch (t.name)
                {
                case "Main Character":
                    cameraController.player = tile;
                    break;
                }
            }
        }

        // Creating the Grids
        if (Main.EditorMode)
        {
            // Creating the Lines

            for (int i = 0; i < tileArray.Length; i++)
            {
                GameObject tile = GetTile((int)tilePos.x, (int)tilePos.y);

                if (tile == null)
                {
                    tile = AddTile("Grid", tilePos);
                }

                // Loops from y to mapHeight then x to mapWidth.
                if (++tilePos.y >= gameData.mapHeight)
                {
                    tilePos.y  = 0;
                    tilePos.x += 1;
                }
            }
        }

        if (Main.EditorMode)
        {
            return;
        }
        else
        {
            // Game started
            started = true;
            CreatePlayerUI();
        }

        // How it works
        // --------------------------
        // Get tiles on the map (World Space)
        // Convert the colliders from PolyCollider2D to a List of List of Vector2s
        // The List contains the Path<List> and the Points<Vector2>
        // The Path contains the Points<Vector2>
        // Increase each Points by the offset from 0,0 in World Space


        // Generating the list of polygons
        List <List <Vector2> > polygons = new List <List <Vector2> >();
        int polygonPointsCount          = 0;

        foreach (var tile in tiles)
        {
            // Tile is empty (Probably because of the map width/height)
            if (tile == null)
            {
                continue;
            }

            Vector3           worldSpace      = tile.transform.position;
            PolygonCollider2D polygonCollider = tile.GetComponent <PolygonCollider2D>();
            TileType          tileType        = tile.GetComponent <TileType>();

            if (polygonCollider && tileType.tile.name == "Terrain")
            {
                // If the rotation of the tile is not 0, do not combine.
                // There is a problem with rotations and colliders. The position of the points do not change, but the bounds change.
                // With Clipper library, it is not possible to change the bounds. The only solution is to reposition each rotated tile, but the algorithm is unclear.

                /*if (tile.transform.rotation.eulerAngles.z != 0) {
                 *  continue;
                 * }*/

                Vector2[] polyPoints = polygonCollider.points;

                for (int i = 0; i < polyPoints.Length; i++)
                {
                    polyPoints[i] = new Vector2(polyPoints[i].x + worldSpace.x, polyPoints[i].y + worldSpace.y);
                }

                List <Vector2> vectorList = polyPoints.ToList <Vector2>();
                polygonPointsCount += vectorList.Count;

                polygons.Add(vectorList);
                Destroy(polygonCollider);
            }
        }

        print(string.Format("Polygon Count: {0}, Polygon Points Count: {1}", polygons.Count, polygonPointsCount));

        // Combining the list of polygons into huge chunks of polygons
        List <List <Vector2> > unitedPolygon = new List <List <Vector2> >();

        // Creating the game collider which holds all the united polygons
        GameObject gameCollider = new GameObject("Polygon Collider");

        gameCollider.layer = LayerMask.NameToLayer("Ground");
        gameCollider.transform.SetParent(gameHolder.transform);

        PolygonCollider2D collider = gameCollider.AddComponent <PolygonCollider2D>();

        // The method to unite all the polygons
        unitedPolygon = UniteCollisionPolygons(polygons);
        int unitedPolygonPointCount = 0;

        // Debug printing the point positions
        foreach (List <Vector2> polygonPath in unitedPolygon)
        {
            foreach (Vector2 polygonPoint in polygonPath)
            {
                //print(string.Format("x: {0}, y: {1}", polygonPoint.x, polygonPoint.y));
            }
            unitedPolygonPointCount += polygonPath.Count;
        }

        print(string.Format("UnitedPolygon Count: {0}, UnitedPolygon Points Count: {1}", unitedPolygon.Count, unitedPolygonPointCount));

        // Set the number of polygons on the new collider to the amount of polygons we have
        collider.pathCount = unitedPolygon.Count;

        // Adding all the points to the new polygons
        for (int i = 0; i < unitedPolygon.Count; i++)
        {
            Vector2[] points = unitedPolygon[i].ToArray <Vector2>();

            collider.SetPath(i, points);
        }
    }