public override void Update(GameBoard board)
        {
            if (board == null || board.Camps == null || board.Camps.Count == 0)
                return;

            if (Path != null)
            {
                //walk along the path
                int row, col;
                board.Grid.FindCell(m_position.X, m_position.Y, out row, out col);

                int path_index= 0;
                for (int i = 0; i < Path.Count; i++)
                {
                    //we need to find closest - not exact match
                    if (Path[i].Row == row && Path[i].Col == col)
                    {
                        path_index = i;
                        break;
                    }
                }

                int next_path_index = Math.Min(path_index + 1, Path.Count - 1);

                float vx, vy;
                board.Grid.GetCellCenter(Path[next_path_index].Row, Path[next_path_index].Col, out vx, out vy);

                vx -= m_position.X;
                vy -= m_position.Y;
                float distance = (float)Math.Sqrt(vx * vx + vy * vy);

                if (distance > m_speed)
                {
                    m_position.X = (float)(m_position.X + vx * m_speed / distance);
                    m_position.Y = (float)(m_position.Y + vy * m_speed / distance);
                }

            }
            else
            {
                GameCamp camp = board.Camps[0];

                float vx, vy;
                board.Grid.GetCellCenter(camp.Position.Row, camp.Position.Col, out vx, out vy);

                vx -= m_position.X;
                vy -= m_position.Y;
                float distance = (float)Math.Sqrt(vx * vx + vy * vy);

                if (distance > m_speed)
                {
                    m_position.X = (float)(m_position.X + vx * m_speed / distance);
                    m_position.Y = (float)(m_position.Y + vy * m_speed / distance);
                }
            }
        }
Beispiel #2
0
        public override void Update(GameBoard board)
        {
            //compute path
            if (m_counter == 0)
            {
                GameCamp camp = board.Camps[0];

                m_agent_position = board.Grid.GetCellCenter(Position);
                m_path           = board.GetPath(this.Position, camp.Position);
            }

            if (m_counter % m_launch_delay == 0)
            {
                GameAgent agent = m_agent.Clone() as GameAgent;

                agent.Position = m_agent_position;
                agent.Path     = m_path;

                board.Agents.Add(agent);
            }
            m_counter++;
        }