示例#1
0
 public BaseStrategy GetStrategy(VariableFlag.Strategy p_id)
 {
     if (strategyMappingTable != null && strategyMappingTable.ContainsKey(p_id))
     {
         return(strategyMappingTable[p_id]);
     }
     return(null);
 }
示例#2
0
        public void OnUpdate()
        {
            if (_mapGrid == null || OnDestroyCallback == null)
            {
                return;
            }

            Vector3 unitPosition = transform.position;

            //Update map information
            TileNode standTile = FindValidStandPosition(unitPosition);

            if (standTile.TilemapMember != null && standTile.GridIndex != _currentTile.GridIndex)
            {
                if (_currentTile.TilemapMember != null)
                {
                    _mapGrid.EditUnitState(_currentTile.GridIndex.x, _currentTile.GridIndex.y, this, false);
                }

                _mapGrid.EditUnitState(standTile.GridIndex.x, standTile.GridIndex.y, this, true);
            }

            //Add self under mapObject
            BlockComponent blockComponent = _mapBlockManager.GetMapComponentByPos(unitPosition);

            if (blockComponent != null && currentBlockComp != blockComponent)
            {
                if (currentBlockComp == null || (!currentBlockComp.isMoving && !blockComponent.isMoving))
                {
                    currentBlockComp = blockComponent;

                    this.transform.SetParent(currentBlockComp.unitHolder);
                }
            }

            UpdateActiveState(currentBlockComp);

            if (currentState == ActiveState.Action)
            {
                //Fallback function
                if (_strategy == null)
                {
                    AgentMove();
                }
                else
                {
                    //_strategy shouldn't put any where but here, all units share the same strategy object
                    _strategy.SetUp(this, _uniqueUnitID, _monsterStats, _mapGrid, _gameDamageManager);

                    _currentStrategy = _strategy.Think(_currentTile, _currentStrategy);

                    _strategy.Execute(_currentTile, _currentStrategy);
                }
            }

            _currentTile = standTile;
        }
示例#3
0
        public Vector2 GetFlowFieldPath(VariableFlag.Strategy pathTag)
        {
            if (pathTag == VariableFlag.Strategy.MoveStraight)
            {
                return(VariableFlag.Vector.Down);
            }

            Vector2 pathDir = VariableFlag.Vector.Zero;

            if (FlowFieldDirectionSet == null || !FlowFieldDirectionSet.TryGetValue(pathTag, out pathDir))
            {
                return(pathDir);
            }

            return(pathDir);
        }
示例#4
0
        public void AddFlowField(VariableFlag.Strategy pathTag, Vector2 direction)
        {
            if (FlowFieldDirectionSet == null)
            {
                FlowFieldDirectionSet = new Dictionary <VariableFlag.Strategy, Vector2>();
            }

            if (!FlowFieldDirectionSet.ContainsKey(pathTag))
            {
                FlowFieldDirectionSet.Add(pathTag, direction);
            }
            else
            {
                FlowFieldDirectionSet[pathTag] = direction;
            }
        }
示例#5
0
        public void Execute(TileNode currentNode, VariableFlag.Strategy strategy)
        {
            if (currentNode.towerUnit != null)
            {
                //Enter attack mode
                AttackOnTower(currentNode.towerUnit);
                return;
            }


            if (strategy == VariableFlag.Strategy.None)
            {
                return;
            }

            moveDelta.Set((currentNode.GetFlowFieldPath(strategy).x),
                          currentNode.GetFlowFieldPath(strategy).y, 0);

            moveDelta *= Time.deltaTime * monsterStat.moveSpeed * 0.3f;

            unit.transform.position += moveDelta;
        }
        public override VariableFlag.Strategy Think(TileNode currentNode, VariableFlag.Strategy currentStrategy)
        {
            //If is under destination block, then go with other strategy
            if (!currentNode.IsValidNode || currentNode.BlockComponent.map_type == BlockComponent.Type.Exist)
            {
                return(ChooseMoveStrategy(currentNode, _strategyList, _fallbackStrategy));
            }

            //Find destination direction
            Vector3 direction = (mapGrid.DestinationNode[0].WorldSpace - unit.transform.position).normalized;

            directionVector.Set(0, (direction.y > 0) ? 1 : -1);

            //Move toward castle, but in straight line
            _modifiedVector2.Set(currentNode.GridIndex.x, currentNode.GridIndex.y + (int)directionVector.y);

            //Go Straight Forward
            TileNode nextStraightNode = mapGrid.FindTileNodeByIndex(_modifiedVector2);

            //During Castle/Tower, if movement is downwward and the next step is empty, back to MoveStraight
            if (currentStrategy == VariableFlag.Strategy.CastleFirst || currentStrategy == VariableFlag.Strategy.TowersFirst)
            {
                if (nextStraightNode.IsValidNode && nextStraightNode.IsWalkable && currentNode.GetFlowFieldPath(currentStrategy) == directionVector)
                {
                    return(VariableFlag.Strategy.MoveStraight);
                }
                return(currentStrategy);
            }

            //Go Straight Forward
            if (nextStraightNode.IsValidNode && nextStraightNode.IsWalkable)
            {
                return(VariableFlag.Strategy.MoveStraight);
            }

            return(ChooseMoveStrategy(currentNode, _strategyList, _fallbackStrategy));
        }
示例#7
0
    public void Execute(ref TileNode[,] tileNodes, TileNode[] targetNodes, Vector2Int nodeSize, VariableFlag.Strategy pathTag)
    {
        var frontier  = new Queue <TileNode>();
        var came_from = new HashSet <Vector2Int>();

        int targetNodeLength = targetNodes.Length;

        for (int t = 0; t < targetNodeLength; t++)
        {
            frontier.Enqueue(tileNodes[targetNodes[t].GridIndex.x, targetNodes[t].GridIndex.y]);
            came_from.Add(targetNodes[t].GridIndex);
        }

        var length = frontier.Count;

        //Hard code exit points
        //frontier.Enqueue(tileNodes[4, 0]);
        //frontier.Enqueue(tileNodes[5, 0]);
        while (length > 0)
        {
            TileNode current = frontier.Dequeue();
            length--;

            TileNode[] neighbours = GetNeighbours(tileNodes, nodeSize, current);
            for (int n = 0; n < directionSetLength; n++)
            {
                var neighbour = neighbours[n];

                if (neighbour.TileBase == null || !neighbour.IsWalkable || neighbour.towerUnit != null)
                {
                    continue;
                }

                if (!came_from.Contains(neighbour.GridIndex))
                {
                    frontier.Enqueue(neighbour);
                    came_from.Add(neighbour.GridIndex);


                    //tileNodes[neighbour.GridIndex.x, neighbour.GridIndex.y].FlowFieldDirection = current.GridIndex - neighbour.GridIndex;
                    tileNodes[neighbour.GridIndex.x, neighbour.GridIndex.y].AddFlowField(pathTag, current.GridIndex - neighbour.GridIndex);
                    length++;
                }
            }
        }
    }
示例#8
0
        public static MonsterStats FindMonsterByStrategy(List <MonsterStats> monsterStats, VariableFlag.Strategy strategy)
        {
            List <MonsterStats> mList = monsterStats.FindAll(x => x.strategy == strategy);
            int mLength = mList.Count;

            if (mList != null && mLength > 0)
            {
                return(mList[Random.Range(0, mLength)]);
            }
            return(null);
        }
示例#9
0
        public override VariableFlag.Strategy Think(TileNode currentNode, VariableFlag.Strategy currentStrategy)
        {
            strategy = ChooseMoveStrategy(currentNode, _strategyList, _fallbackStrategy);

            return(strategy);
        }
示例#10
0
 public virtual VariableFlag.Strategy Think(TileNode blockComponent, VariableFlag.Strategy currentStrategy)
 {
     return(VariableFlag.Strategy.None);
 }
示例#11
0
        protected VariableFlag.Strategy ChooseMoveStrategy(TileNode currentNode, VariableFlag.Strategy[] strategyOrder, VariableFlag.Strategy defaultStrategy)
        {
            if (strategyOrder == null || strategyOrder.Length <= 0)
            {
                return(defaultStrategy);
            }

            int sLength = strategyOrder.Length;

            for (int i = 0; i < sLength; i++)
            {
                if (currentNode.GetFlowFieldPath(strategyOrder[i]) != zeroDelta)
                {
                    return(strategyOrder[i]);
                }
            }

            return(defaultStrategy);
        }