Exemplo n.º 1
0
        public void InitPath()
        {
            int startSectionId = 0;

            sectionIndex = 0;
            ClearPath();

            SectionModel firstCornerSection = new SectionModel(
                startSectionId,
                SectionType.corner,
                Direction.right,
                new Node(0, 0),
                pathWidth, pathWidth, MapManager.Instance.verticalCornerSectionContent.terrains);

            int unitLength = Random.Range(3, 6);

            unitLength = 2;
            int            firstStraightSectionLength = unitLength * 3;
            SectionContent sectionContent             = MapManager.Instance.GetSectionContentByUnitLength(unitLength);
            SectionModel   firstStraightSection       = new SectionModel(
                firstCornerSection.sectionId + 1,
                SectionType.straight,
                Direction.right,
                new Node(3, 0),
                firstStraightSectionLength, pathWidth);

            Direction secondCornerSectionDirection = Random.Range(0, 100) < 50 ? Direction.up : Direction.down;

            secondCornerSectionDirection = pm.playMode == PlayMode.campaign ? Direction.up : Direction.down;
            SectionModel secondCornerSection = new SectionModel(
                firstStraightSection.sectionId + 1,
                SectionType.corner,
                secondCornerSectionDirection,
                new Node(3 + firstStraightSectionLength, 0),
                pathWidth, pathWidth, secondCornerSectionDirection == Direction.up ? MapManager.Instance.decreaseCornerSectionContent.terrains : MapManager.Instance.increaseCornerSectionContent.terrains);

            SectionComponent firstCornerSectionComponent = MakeSection(firstCornerSection, firstStraightSection);

            PutSectionComponent(firstCornerSectionComponent);
            SectionComponent firstStraightSectionComponent = MakeSection(firstStraightSection, secondCornerSection);

            PutSectionComponent(firstStraightSectionComponent);
            SectionComponent secondCornerSectionComponent = MakeSection(secondCornerSection);

            PutSectionComponent(secondCornerSectionComponent);

            firstCornerSectionComponent.nextSectionComponent     = firstStraightSectionComponent;
            firstStraightSectionComponent.beforeSectionComponent = firstCornerSectionComponent;
            firstStraightSectionComponent.nextSectionComponent   = secondCornerSectionComponent;
            secondCornerSectionComponent.beforeSectionComponent  = firstStraightSectionComponent;
            currentSectionComponent = firstCornerSectionComponent;


            for (int i = 0; i < 5; i++)
            {
                AddSection(pm.playMode);
            }
        }
Exemplo n.º 2
0
        SectionComponent MakeSection(SectionModel sectionData, SectionModel nextSectionData = null, bool isLastSection = false)
        {
            // Debug.Log("PathManager.MakeSection(" + sectionData.sectionId + ")");
            Transform sectionTransform = Instantiate(sectionPrefab) as Transform;

            sectionTransform.gameObject.name = "section#" + sectionData.sectionId.ToString();
            sectionTransform.SetParent(sectionContainer);
            SectionComponent sectionComponent = sectionTransform.GetComponent <SectionComponent>();

            sectionComponent.InitSectionComponent(sectionData, nextSectionData, lastSectionComponent, isLastSection);

            return(sectionComponent);
        }
Exemplo n.º 3
0
        public void InitSectionComponent(SectionModel sectionData, SectionModel nextSectionData, SectionComponent lastSectionComponent, bool isLastSection = false)
        {
            // Debug.Log("SectionComponent.InitSectionComponent(" + sectionData.sectionId.ToString() + ")");
            isCollapsing = false;
            blockComponents.Clear();
            this.sectionData = sectionData;
            SetSectionPosition(this.sectionData.origin);

            // Debug.Log("pm.pathManager.sectionList.Count: " + pm.pathManager.sectionList.Count.ToString());
            // if (lastSectionComponent != null) { Debug.Log("lastSectionComponent: " + lastSectionComponent); }
            // else { Debug.Log("lastSectionComponent is null"); }

            int baseProgress = lastSectionComponent != null ? lastSectionComponent.maxProgress : 0;

            minProgress = baseProgress;
            int _maxProgress = -1;

            // Debug.Log("sectionData.height: " + sectionData.height.ToString());
            // Debug.Log("sectionData.width: " + sectionData.width.ToString());
            for (int i = 0; i < this.sectionData.height; i++)
            {
                for (int j = 0; j < this.sectionData.width; j++)
                {
                    TerrainGid terrainGid = TerrainGid.empty;
                    if (sectionData.terrains != null)
                    {
                        terrainGid = (TerrainGid)sectionData.terrains[i, j];
                    }
                    else
                    {
                        //terrain정보가 들어오지 않는 경우는 코너섹션일 때
                        terrainGid = TerrainGid.basic_dirt;
                    }
                    if (terrainGid == TerrainGid.empty)
                    {
                        continue;
                    }

                    GameObject blockPrefab     = PrefabManager.Instance.GetBlockPrefab(terrainGid);
                    GameObject blockGameObject = Instantiate(blockPrefab) as GameObject;
                    // GameObject blockGameObject = ObjectPool.Spawn(blockPrefab);

                    blockGameObject.name = "block#" + pm.pathManager.lastBlockComponentId.ToString();
                    blockGameObject.transform.SetParent(blockContainer);
                    BlockComponent blockComponent = blockGameObject.GetComponent <BlockComponent>();
                    BlockCategory  blockCategory  = this.sectionData.sectionType == SectionType.corner ? BlockCategory.corner : BlockCategory.straight;
                    if (this.sectionData.sectionType == SectionType.corner)
                    {
                        SectionComponent _beforeSectionComponent = pm.pathManager.GetSectionComponent(this.sectionData.sectionId - 1);
                        // SectionComponent _beforeBeforeSectionComponent = pm.pathManager.GetSectionComponent(this.sectionData.sectionId - 2);
                        if (_beforeSectionComponent == null) //first corner section
                        {
                            blockCategory = BlockCategory.corner;
                        }
                        else if (this.sectionData.direction == Direction.up || (this.sectionData.direction == Direction.right && (_beforeSectionComponent.sectionData.direction == Direction.up)))
                        {
                            if (i + j >= 2 && !(i == 2 && j == 2))
                            {
                                blockCategory = BlockCategory.turn;
                            }
                            else
                            {
                                blockCategory = BlockCategory.corner;
                            }
                        }
                        else if (this.sectionData.direction == Direction.down || (this.sectionData.direction == Direction.right && (_beforeSectionComponent.sectionData.direction == Direction.down)))
                        {
                            if (j >= i && !(i == 0 && j == 2))
                            {
                                blockCategory = BlockCategory.turn;
                            }
                            else
                            {
                                blockCategory = BlockCategory.corner;
                            }
                        }

                        if (isLastSection)
                        {
                            blockCategory = BlockCategory.straight;
                        }
                    }
                    else if (this.sectionData.sectionType == SectionType.straight)
                    {
                        if (this.sectionData.direction == Direction.up)
                        {
                            if (j == this.sectionData.width - 1 && i == this.sectionData.height - 1)
                            {
                                blockCategory = BlockCategory.shortcut_start;
                            }
                            else if (j == 0 && i == 0)
                            {
                                blockCategory = BlockCategory.shortcut_end;
                            }
                            else
                            {
                                blockCategory = BlockCategory.straight;
                            }
                        }
                        else if (this.sectionData.direction == Direction.down)
                        {
                            if (j == this.sectionData.width - 1 && i == 0)
                            {
                                blockCategory = BlockCategory.shortcut_start;
                            }
                            else if (j == 0 && i == this.sectionData.height - 1)
                            {
                                blockCategory = BlockCategory.shortcut_end;
                            }
                            else
                            {
                                blockCategory = BlockCategory.straight;
                            }
                        }
                        else if (this.sectionData.direction == Direction.right)
                        {
                            SectionComponent _beforeSectionComponent       = pm.pathManager.GetSectionComponent(this.sectionData.sectionId - 1);
                            SectionComponent _beforeBeforeSectionComponent = pm.pathManager.GetSectionComponent(this.sectionData.sectionId - 2);
                            if ((nextSectionData.direction == Direction.up) && (j == this.sectionData.width - 1 && i == this.sectionData.height - 1))
                            {
                                blockCategory = BlockCategory.shortcut_start;
                            }
                            else if ((nextSectionData.direction == Direction.down) && (j == this.sectionData.width - 1 && i == 0))
                            {
                                blockCategory = BlockCategory.shortcut_start;
                            }
                            else if (_beforeBeforeSectionComponent != null)
                            {
                                if (_beforeBeforeSectionComponent.sectionData.direction == Direction.up && (j == 0 && i == 0))
                                {
                                    blockCategory = BlockCategory.shortcut_end;
                                }
                                else if (_beforeBeforeSectionComponent.sectionData.direction == Direction.down && (j == 0 && i == this.sectionData.height - 1))
                                {
                                    blockCategory = BlockCategory.shortcut_end;
                                }
                                else
                                {
                                    blockCategory = BlockCategory.straight;
                                }
                            }
                            else
                            {
                                blockCategory = BlockCategory.straight;
                            }
                        }
                        //다음 코너에서 방향이 어떨게 되냐에따라 현재 스트레이트 섹션의 숏컷이 결정된다

                        /*
                         *
                         * if ((this.sectionData.direction == Direction.right && nextSectionData.direction == Direction.up) ||
                         * (this.sectionData.direction == Direction.up && nextSectionData.direction == Direction.right))
                         * {
                         *  if (j == this.sectionData.width - 1 && i == this.sectionData.height - 1)
                         *  {
                         *      blockCategory = BlockCategory.shortcut_start;
                         *  }
                         *  else if (this.sectionData.direction == Direction.right && j == this.sectionData.width - 1)
                         *  {
                         *      blockCategory = BlockCategory.straight_edge;
                         *  }
                         *  else if (this.sectionData.direction == Direction.up && i == this.sectionData.height - 1)
                         *  {
                         *      blockCategory = BlockCategory.straight_edge;
                         *  }
                         *  else if (j == 0 && i == 0)
                         *  {
                         *      Debug.Log("???");
                         *      blockCategory = BlockCategory.shortcut_end;
                         *  }
                         *  else
                         *  {
                         *      blockCategory = BlockCategory.straight;
                         *  }
                         * }
                         * else
                         * {
                         *  if (j == this.sectionData.width - 1 && i == 0)
                         *  {
                         *      blockCategory = BlockCategory.shortcut_start;
                         *  }
                         *  else if (this.sectionData.direction == Direction.right && j == this.sectionData.width - 1)
                         *  {
                         *      blockCategory = BlockCategory.straight_edge;
                         *  }
                         *  else if (this.sectionData.direction == Direction.down && i == 0)
                         *  {
                         *      blockCategory = BlockCategory.straight_edge;
                         *  }
                         *  else if (j == 0 && i == this.sectionData.height - 1)
                         *  {
                         *      Debug.Log("!!!");
                         *      blockCategory = BlockCategory.shortcut_end;
                         *  }
                         *  else
                         *  {
                         *      blockCategory = BlockCategory.straight;
                         *  }
                         * }
                         */
                    }
                    int progress = 0;

                    if (sectionData.sectionType == SectionType.straight)
                    {
                        if (sectionData.direction == Direction.right)
                        {
                            progress = baseProgress + j + 1;
                        }
                        else if (sectionData.direction == Direction.up)
                        {
                            progress = baseProgress + i + 1;
                        }
                        else if (sectionData.direction == Direction.down)
                        {
                            progress = baseProgress + (this.sectionData.height - i);
                        }
                    }
                    else if (sectionData.sectionType == SectionType.corner)
                    {
                        progress = baseProgress + 1;
                    }

                    if (progress > _maxProgress)
                    {
                        _maxProgress = progress;
                    }

                    BlockModel blockData = new BlockModel(
                        pm.pathManager.lastBlockComponentId++,
                        blockCategory,
                        this.sectionData.direction,
                        this.sectionData.origin + new Node(j, i),
                        progress);

                    blockComponent.InitBlockComponent(this, blockData);
                    blockComponents.Add(blockComponent);
                    pm.pathManager.PutBlockComponent(blockComponent);
                }
            }

            if (sectionData.objects != null)
            {
                // Debug.Log("---------");
                // Debug.Log(sectionData.objects.Count);
                // Debug.Log("sectionData.direction: " + sectionData.direction);
                // Debug.Log("sectionData.width: " + sectionData.width.ToString());
                // Debug.Log("sectionData.height: " + sectionData.height.ToString());
                foreach (KeyValuePair <Node, TiledObject> kv in sectionData.objects)
                {
                    Node node = kv.Key;
                    // Debug.Log("node: " + node.ToString());
                    TiledObject tiledObject = kv.Value;
                    int         objectGid   = tiledObject.gid;

                    BlockComponent blockComponent = pm.pathManager.GetBlockComponentByOrigin(this.sectionData.origin + node);
                    if (blockComponent == null)
                    {
                        Debug.Log("blockComponent is null");
                        continue;
                    }

                    if (17 <= objectGid && objectGid < 33) //item
                    {
                        Node       itemOrigin = this.sectionData.origin + node;
                        GameObject itemPrefab = PrefabManager.Instance.GetItemPrefab((ItemGid)objectGid);
                        // GameObject itemInstance = Instantiate(itemPrefab) as GameObject;
                        GameObject itemInstance = ObjectPool.Spawn(itemPrefab);
                        itemInstance.transform.SetParent(blockComponent.objectContainer);
                        ItemComponent itemComponent = itemInstance.GetComponent <ItemComponent>();
                        itemComponent.InitItemComponent(this, itemOrigin, blockComponent.blockData.direction);
                        itemComponents.Add(itemComponent);
                        pm.pathManager.PutItemComponent(itemComponent);
                        if (itemComponent.itemType == ItemType.food)
                        {
                            FoodItemComponent foodItemComponent = itemComponent.GetComponent <FoodItemComponent>();
                            foodItemComponent.InitFoodRender();
                        }
                    }
                    else if (33 <= objectGid && objectGid < 49) //trap
                    {
                        Node       trapOrigin = this.sectionData.origin + node;
                        GameObject trapPrefab = PrefabManager.Instance.GetTrapPrefab((TrapGid)objectGid);
                        // GameObject trapInstance = Instantiate(trapPrefab) as GameObject;
                        GameObject trapInstance = ObjectPool.Spawn(trapPrefab);
                        trapInstance.transform.SetParent(blockComponent.objectContainer);
                        TrapComponent trapComponent = trapInstance.GetComponent <TrapComponent>();
                        trapComponent.name = trapComponent.GetInstanceID().ToString();
                        Direction trapDirection = blockComponent.blockData.direction;
                        if (tiledObject.properties != null)
                        {
                            //TODO TileProperty의 direction을 그대로 사용하는것이 아니라
                            //섹션의 방향에 따라 수정된 direction을 사용해야 한다
                            if (sectionData.direction == Direction.right)
                            {
                                trapDirection = tiledObject.properties.direction;
                            }
                            else if (sectionData.direction == Direction.up)
                            {
                                if (tiledObject.properties.direction == Direction.up)
                                {
                                    trapDirection = Direction.left;
                                }
                                else if (tiledObject.properties.direction == Direction.right)
                                {
                                    trapDirection = Direction.up;
                                }
                                else if (tiledObject.properties.direction == Direction.down)
                                {
                                    trapDirection = Direction.right;
                                }
                                else if (tiledObject.properties.direction == Direction.left)
                                {
                                    trapDirection = Direction.down;
                                }
                            }
                            else if (sectionData.direction == Direction.down)
                            {
                                if (tiledObject.properties.direction == Direction.up)
                                {
                                    trapDirection = Direction.right;
                                }
                                else if (tiledObject.properties.direction == Direction.right)
                                {
                                    trapDirection = Direction.down;
                                }
                                else if (tiledObject.properties.direction == Direction.down)
                                {
                                    trapDirection = Direction.left;
                                }
                                else if (tiledObject.properties.direction == Direction.left)
                                {
                                    trapDirection = Direction.up;
                                }
                            }
                        }
                        trapComponent.InitTrapComponent(this, trapOrigin, trapDirection, tiledObject);
                        trapComponents.Add(trapComponent);
                        pm.pathManager.PutTrapComponent(trapComponent);
                    }
                    else if (49 <= objectGid && objectGid < 65) //enemy
                    {
                        Node       itemOrigin  = this.sectionData.origin + node;
                        GameObject enemyPrefab = PrefabManager.Instance.GetEnemyPrefab((EnemyGid)objectGid);
                        // GameObject enemyInstance = Instantiate(enemyPrefab) as GameObject;
                        GameObject enemyInstance = ObjectPool.Spawn(enemyPrefab);
                        enemyInstance.transform.SetParent(blockComponent.objectContainer);
                        EnemyComponent enemyComponent = enemyInstance.GetComponent <EnemyComponent>();
                        enemyComponent.InitEnemyComponent(itemOrigin, blockComponent.blockData.direction);
                        enemyComponents.Add(enemyComponent);
                        pm.pathManager.PutEnemyComponent(enemyComponent);
                    }
                }
            }

            this.maxProgress = _maxProgress;
        }
Exemplo n.º 4
0
        public void AddSection(PlayMode playMode)
        {
            SectionContent sectionContent        = null;
            int            unitLength            = -1;
            int            straightSectionLength = -1;

            if (playMode == PlayMode.infinity)
            {
                unitLength            = Random.Range(2, 6);
                straightSectionLength = unitLength * 3; //6, 9, 12, 15
                sectionContent        = MapManager.Instance.GetSectionContentByUnitLength(unitLength);
            }
            else if (playMode == PlayMode.campaign)
            {
                sectionContent = MapManager.Instance.GetSectionContentBySectionIndex(sectionIndex++);
                if (sectionContent == null)
                {
                    // Debug.Log("sectionContent is null, sectionIndex : " + sectionIndex.ToString());
                    return;
                }
                unitLength            = sectionContent.unitLength;
                straightSectionLength = unitLength * 3;
            }

            Node straightSectionOrigin = lastSectionComponent.sectionData.origin;
            int  straightSectionWidth  = 0;
            int  straightSectionHeight = 0;

            if (lastSectionComponent.sectionData.direction == Direction.up)
            {
                straightSectionOrigin += new Node(0, pathWidth);
                straightSectionWidth   = pathWidth;
                straightSectionHeight  = straightSectionLength;
            }
            else if (lastSectionComponent.sectionData.direction == Direction.right)
            {
                straightSectionOrigin += new Node(pathWidth, 0);
                straightSectionWidth   = straightSectionLength;
                straightSectionHeight  = pathWidth;
            }
            else if (lastSectionComponent.sectionData.direction == Direction.down)
            {
                straightSectionOrigin += new Node(0, -straightSectionLength);
                straightSectionWidth   = pathWidth;
                straightSectionHeight  = straightSectionLength;
            }
            int[,] terrains = new int[straightSectionHeight, straightSectionWidth];
            Dictionary <Node, TiledObject> objects = new Dictionary <Node, TiledObject>();

            if (lastSectionComponent.sectionData.direction == Direction.right)
            {
                terrains = sectionContent.terrains;

                foreach (KeyValuePair <Node, TiledObject> kv in sectionContent.objects)
                {
                    Node node = new Node(kv.Key.y, kv.Key.x);
                    objects.Add(node, kv.Value);
                }
            }
            else if (lastSectionComponent.sectionData.direction == Direction.up)
            {
                for (int i = 0; i < sectionContent.terrains.GetLength(0); i++)
                {
                    for (int j = 0; j < sectionContent.terrains.GetLength(1); j++)
                    {
                        terrains[j, sectionContent.terrains.GetLength(0) - 1 - i] = sectionContent.terrains[i, j];
                    }
                }

                foreach (KeyValuePair <Node, TiledObject> kv in sectionContent.objects)
                {
                    Node node = new Node(sectionContent.terrains.GetLength(0) - 1 - kv.Key.x, kv.Key.y);
                    objects.Add(node, kv.Value);
                }
            }
            else if (lastSectionComponent.sectionData.direction == Direction.down)
            {
                for (int i = 0; i < sectionContent.terrains.GetLength(0); i++)
                {
                    for (int j = 0; j < sectionContent.terrains.GetLength(1); j++)
                    {
                        terrains[sectionContent.terrains.GetLength(1) - 1 - j, i] = sectionContent.terrains[i, j];
                    }
                }

                foreach (KeyValuePair <Node, TiledObject> kv in sectionContent.objects)
                {
                    objects.Add(new Node(kv.Key.x, sectionContent.terrains.GetLength(1) - 1 - kv.Key.y), kv.Value);
                }
            }

            SectionModel addStraightSection = new SectionModel(
                lastSectionComponent.sectionData.sectionId + 1,
                SectionType.straight,
                lastSectionComponent.sectionData.direction,
                straightSectionOrigin,
                straightSectionWidth, straightSectionHeight,
                terrains,
                objects);


            Direction cornerSectionDirection = Direction.none;
            Node      cornerSectionOrigin    = addStraightSection.origin;

            if (addStraightSection.direction == Direction.up)
            {
                cornerSectionDirection = Direction.right;
                cornerSectionOrigin   += new Node(0, addStraightSection.height);
            }
            else if (addStraightSection.direction == Direction.right)
            {
                cornerSectionDirection = Random.Range(0, 100) < 50 ? Direction.up : Direction.down;
                cornerSectionOrigin   += new Node(addStraightSection.width, 0);
            }
            else if (addStraightSection.direction == Direction.down)
            {
                cornerSectionDirection = Direction.right;
                cornerSectionOrigin   += new Node(0, -pathWidth);
            }
            SectionContent cornerSectionContent = null;

            if (playMode == PlayMode.campaign && sectionIndex == MapManager.Instance.campaignSectionContent.Count)
            {
                cornerSectionContent = MapManager.Instance.finishSectionContent;
            }
            else if (cornerSectionDirection == Direction.up)
            {
                cornerSectionContent = MapManager.Instance.decreaseCornerSectionContent;
            }
            else if (cornerSectionDirection == Direction.down)
            {
                cornerSectionContent = MapManager.Instance.increaseCornerSectionContent;
            }
            else if (cornerSectionDirection == Direction.right)
            {
                if (addStraightSection.direction == Direction.up)
                {
                    cornerSectionContent = MapManager.Instance.decreaseCornerSectionContent;
                }
                else if (addStraightSection.direction == Direction.down)
                {
                    cornerSectionContent = MapManager.Instance.increaseCornerSectionContent;
                }
            }

            bool         isLastSection    = playMode == PlayMode.campaign && sectionIndex == MapManager.Instance.campaignSectionContent.Count;
            SectionModel addCornerSection = new SectionModel(
                addStraightSection.sectionId + 1,
                SectionType.corner,
                isLastSection ? addStraightSection.direction : cornerSectionDirection,
                cornerSectionOrigin,
                pathWidth, pathWidth, cornerSectionContent.terrains);

            SectionComponent straightSectionComponent = MakeSection(addStraightSection, addCornerSection);

            lastSectionComponent.nextSectionComponent       = straightSectionComponent;
            straightSectionComponent.beforeSectionComponent = lastSectionComponent;
            PutSectionComponent(straightSectionComponent);
            SectionComponent cornerSectionComponent = MakeSection(addCornerSection, null, isLastSection);

            straightSectionComponent.nextSectionComponent = cornerSectionComponent;
            cornerSectionComponent.beforeSectionComponent = straightSectionComponent;
            PutSectionComponent(cornerSectionComponent);

            if (sectionList.Count > maxMaintainSectionCount)
            {
                SectionComponent firstStraightSection = sectionList[0];
                SectionComponent firstCornerSection   = sectionList[1];
                RemoveSectionComponent(firstStraightSection);
                RemoveSectionComponent(firstCornerSection);
                firstStraightSection.RemoveSection();
                firstCornerSection.RemoveSection();
            }
        }