Пример #1
0
    /// <summary>
    /// I override this callback so that I can respond on the grid being
    /// changed and place/ update the actual tile objects
    /// </summary>
    public override void OnGridChanged(bool created)
    {
        // The parent object that will hold all the instantiated tile objects
        Transform parent = gameObject.transform;

        // Remove existing tiles and place new ones if map was (re)created
        // since the number of tiles might be different now
        if (created)
        {
            for (int i = parent.childCount - 1; i >= 0; i--)
            {
                // was called from editor
                Object.DestroyImmediate(parent.GetChild(i).gameObject);
            }

            // Place tiles according to the generated grid
            for (int idx = 0; idx < grid.Length; idx++)
            {
                // make sure it is a valid node before placing tile here
                if (false == grid[idx].isValid)
                {
                    continue;
                }

                // create the visual tile
                GameObject go = (GameObject)Instantiate(tileFab);
                go.name  = "Tile " + idx.ToString();
                go.layer = tileLayer;                 // set the layer since I will need it in Sample4Controller to check what the player clicked on
                go.tag   = "Tile";
                go.transform.position = grid[idx].position;
                go.transform.parent   = parent;

                // MapNav created the node as the custom Sample4Tile type
                // Now init the tileName property in it
                NavalTile node = grid[idx] as NavalTile;
                node.tileName = go.name;
                node.tileObj  = go;
            }
        }

        // else, simply update the position of existing tiles
        else
        {
            for (int idx = 0; idx < grid.Length; idx++)
            {
                // make sure it is a valid node before processing it
                if (false == grid[idx].isValid)
                {
                    continue;
                }

                // Since I gave the tiles proper names I can easily find them by name
                GameObject go = parent.Find("Tile " + idx.ToString()).gameObject;
                go.transform.position = grid[idx].position;
            }
        }
    }
Пример #2
0
    /// <summary>
    /// I use this to link the tile and unit with each other so that the
    /// tile and unit knows which unit is on the tile. I will pass null
    /// to simply unlink the tile and unit.
    /// </summary>
    public void LinkWithTile(NavalTile t)
    {
        // first unlink the unit from the tile
        if (tile != null)
        {
            tile.unit = null;
        }
        tile = t;

        // if t == null then this was simply an unlink and it ends here
        if (tile == null)
        {
            return;
        }

        // else tell the tile that this unit is on it
        tile.unit = this;
    }
Пример #3
0
    public void SetDamage(int damage)
    {
        if (canDamage)
        {
            m_life   -= damage;
            canDamage = false;
            StartCoroutine(ChangeColor());
        }
        if (m_life <= 0)
        {
            m_life = 0;
            OnDeath(this);
            // 取消敌船与地图网格的关联
            LinkWithTile(null);
            NavalTile last = path[path.Count - 1] as NavalTile;
            last.target = false;

            Destroy(this.gameObject);
        }
    }
Пример #4
0
    void SpawnEnemy()
    {
        // 在六边形网格地图最右侧随机生成敌船
        NavalTile sourceTile = null;

        while (true)
        {
            int idx = map.mapHorizontalSize * (map.mapVerticalSize - 1) + Random.Range(0, map.mapHorizontalSize);
            // make sure this is a valid node
            if (map.ValidIDX(idx))
            {
                // now check if there is not already a unit on it
                sourceTile = map.grid[idx] as NavalTile;
                if (sourceTile.unit == null)
                {
                    // no unit on it, lets use it
                    break;
                }
            }
        }

        // create the unit and place it on the tile
        GameObject go = (GameObject)Instantiate(enemyFab);

        go.transform.position = sourceTile.position;
        go.layer = 9;         // Units must be in layer 9
        go.tag   = "Enemy";

        // be sure to tell the tile that this Unit is on it
        EnemyUnit unit = go.GetComponent <EnemyUnit> ();

        unit.Resetunit();          // reset its moves now too
        unit.LinkWithTile(sourceTile);
        unit.OnDeath += OnDeath;
        enemyList.Add(unit);          // keep a list of all enemies for quick access

        // 设置敌船的随机目标
        NavalTile targetTile = null;

        while (true)
        {
            int idy = map.mapHorizontalSize * 1 + Random.Range(6, map.mapHorizontalSize - 6);
            // set each enemy's targets
            if (map.ValidIDX(idy))
            {
                targetTile = map.grid[idy] as NavalTile;
                if (targetTile.target == false)
                {
                    targetTile.target = true;
                    break;
                }
            }
        }

        List <MapNavNode> unitPath = map.Path <MapNavNode> (sourceTile, targetTile, OnNodeCostCallback);

        if (unitPath != null)
        {
            unit.Move(unitPath, null);
        }

        // 更新敌人数量
        curEnemyCount++;
    }