Beispiel #1
0
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            if (!isActive)
            {
                goldText.Text = "";
                return;
            }
            goldText.Position = new Vector2(position.X - 8, position.Y - 8);
            goldText.Text = goldCount.ToString();
            if (peonState == PeonState.Moving)
            {
                if (Vector2.Distance(position, curTarget) < slowRadius + 5 && targetPath.Count > 2)
                {
                    if (!graph.IsNodeBlocked(targetPath.Peek()))
                    {
                        curTarget = targetPath.Pop();
                        if (!graph.IsNodeBlocked(targetPath.Peek()))
                        {
                            curTarget = (curTarget + targetPath.Pop()) / 2;
                        }
                        else
                        {
                            peonState = PeonState.Idle;
                            return;
                        }
                    }
                    else
                    {
                        peonState = PeonState.Idle;
                        return;
                    }
                    curTarget = new Vector2(curTarget.X + sprite.Width / 2, curTarget.Y + sprite.Height / 2);
                }
                else if (Vector2.Distance(position, curTarget) < slowRadius + 5 && targetPath.Count > 0)
                {
                    if (!graph.IsNodeBlocked(targetPath.Peek()))
                    {
                        curTarget = targetPath.Pop();
                    }
                    else
                    {
                        peonState = PeonState.Idle;
                    }
                    curTarget = new Vector2(curTarget.X + sprite.Width / 2, curTarget.Y + sprite.Height / 2);
                }
                else if (targetPath.Count == 0 && Vector2.Distance(position, curTarget) < stopRadius)
                {
                    if (graph.IsNodeBlocked(position))
                    {
                        MoveToLocation(targetLocation);
                        return;
                    }
                    graph.SetNodeBlocked(position);
                    peonState = PeonState.Idle;
                }

                //rotate based on orientation
                base.rotation = (float)Math.Atan2(orientation.X, -orientation.Y);
                acceleration = curTarget - this.Position;
                distance = Math.Abs(acceleration.Length());
                if (distance < stopRadius)
                {
                    speed = 0;
                }
                else if (distance < slowRadius)
                {
                    speed = maxSpeed * distance / slowRadius;
                }
                else
                {
                    speed = maxSpeed;
                }

                oldVelocity = velocity;
                acceleration = Vector2.Normalize(curTarget - this.Position) * maxAccel;
                velocity += velocity * gameTime.ElapsedGameTime.Milliseconds + .5f * acceleration * gameTime.ElapsedGameTime.Milliseconds * gameTime.ElapsedGameTime.Milliseconds;
                velocity = Vector2.Normalize(velocity) * speed;
                position += velocity;


                if (velocity != Vector2.Zero)
                {
                    orientation = velocity;
                }
            }
            else if (peonState == PeonState.Idle)
            {
                if (peonObjective == PeonObjective.GoToBuild)
                {
                    curNode = graph.GetNode(position);
                    foreach (Edge edge in curNode.Neighbors)
                    {
                        tempNode = edge.GetNeighbor(curNode);
                        //searches through the neighbor nodes to make sure it isnt on the wall or next to something else.
                        if (!tempNode.IsBlocked)
                        {
                            canBuild = true;
                            foreach (Edge innerEdge in tempNode.Neighbors)
                            {
                                if (innerEdge.GetNeighbor(tempNode).IsBlocked && innerEdge.GetNeighbor(tempNode) != curNode)
                                {
                                    canBuild = false;
                                }
                            }
                            if (canBuild)
                            {
                                agent.IsAgentGoingToBuildSomething = false;
                                if (buildingType == Building.BuildingType.GoldRefinery)
                                {
                                    GoldRefinery newRefinery = new GoldRefinery(game, tempNode, graph, agent);
                                    Game.Components.Add(newRefinery);
                                    peonObjective = PeonObjective.Build;
                                    return;
                                }
                                else if (buildingType == Building.BuildingType.Barracks)
                                {
                                    Barracks newBarracks = new Barracks(game, tempNode, graph, agent);
                                    Game.Components.Add(newBarracks);
                                    peonObjective = PeonObjective.Build;
                                    return;
                                }
                            }
                        }
                    }
                }
                graph.SetNodeBlocked(position);
            }
            else if (peonState == PeonState.Mining)
            {
                graph.SetNodeBlocked(position);

                if (goldMine.GoldRemaining == 0)
                {
                    peonState = PeonState.Idle;
                }
                else if (goldCount != goldCountMax)
                {
                    timer += gameTime.ElapsedGameTime.Milliseconds;
                    if (timer >= timeToGetGold)
                    {
                        timer = 0;
                        if (goldToGetPerTime > goldMine.GoldRemaining)
                        {
                            goldCount += goldMine.TakeResource(goldMine.GoldRemaining);
                            peonState = PeonState.Idle;
                        }
                        if (goldCount + goldToGetPerTime > goldCountMax)
                        {
                            goldCount += goldMine.TakeResource(goldCountMax - goldCount);
                        }
                        else
                        {
                            goldCount += goldMine.TakeResource(goldToGetPerTime);
                        }
                    }
                }
                else
                {
                    peonState = PeonState.Idle;
                }
            }
        }
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            if (!isActive)
            {
                goldText.Text = "";
                return;
            }
            goldText.Position = new Vector2(position.X - 8, position.Y - 8);
            goldText.Text     = goldCount.ToString();
            if (peonState == PeonState.Moving)
            {
                if (Vector2.Distance(position, curTarget) < slowRadius + 5 && targetPath.Count > 2)
                {
                    if (!graph.IsNodeBlocked(targetPath.Peek()))
                    {
                        curTarget = targetPath.Pop();
                        if (!graph.IsNodeBlocked(targetPath.Peek()))
                        {
                            curTarget = (curTarget + targetPath.Pop()) / 2;
                        }
                        else
                        {
                            peonState = PeonState.Idle;
                            return;
                        }
                    }
                    else
                    {
                        peonState = PeonState.Idle;
                        return;
                    }
                    curTarget = new Vector2(curTarget.X + sprite.Width / 2, curTarget.Y + sprite.Height / 2);
                }
                else if (Vector2.Distance(position, curTarget) < slowRadius + 5 && targetPath.Count > 0)
                {
                    if (!graph.IsNodeBlocked(targetPath.Peek()))
                    {
                        curTarget = targetPath.Pop();
                    }
                    else
                    {
                        peonState = PeonState.Idle;
                    }
                    curTarget = new Vector2(curTarget.X + sprite.Width / 2, curTarget.Y + sprite.Height / 2);
                }
                else if (targetPath.Count == 0 && Vector2.Distance(position, curTarget) < stopRadius)
                {
                    if (graph.IsNodeBlocked(position))
                    {
                        MoveToLocation(targetLocation);
                        return;
                    }
                    graph.SetNodeBlocked(position);
                    peonState = PeonState.Idle;
                }

                //rotate based on orientation
                base.rotation = (float)Math.Atan2(orientation.X, -orientation.Y);
                acceleration  = curTarget - this.Position;
                distance      = Math.Abs(acceleration.Length());
                if (distance < stopRadius)
                {
                    speed = 0;
                }
                else if (distance < slowRadius)
                {
                    speed = maxSpeed * distance / slowRadius;
                }
                else
                {
                    speed = maxSpeed;
                }

                oldVelocity  = velocity;
                acceleration = Vector2.Normalize(curTarget - this.Position) * maxAccel;
                velocity    += velocity * gameTime.ElapsedGameTime.Milliseconds + .5f * acceleration * gameTime.ElapsedGameTime.Milliseconds * gameTime.ElapsedGameTime.Milliseconds;
                velocity     = Vector2.Normalize(velocity) * speed;
                position    += velocity;


                if (velocity != Vector2.Zero)
                {
                    orientation = velocity;
                }
            }
            else if (peonState == PeonState.Idle)
            {
                if (peonObjective == PeonObjective.GoToBuild)
                {
                    curNode = graph.GetNode(position);
                    foreach (Edge edge in curNode.Neighbors)
                    {
                        tempNode = edge.GetNeighbor(curNode);
                        //searches through the neighbor nodes to make sure it isnt on the wall or next to something else.
                        if (!tempNode.IsBlocked)
                        {
                            canBuild = true;
                            foreach (Edge innerEdge in tempNode.Neighbors)
                            {
                                if (innerEdge.GetNeighbor(tempNode).IsBlocked&& innerEdge.GetNeighbor(tempNode) != curNode)
                                {
                                    canBuild = false;
                                }
                            }
                            if (canBuild)
                            {
                                agent.IsAgentGoingToBuildSomething = false;
                                if (buildingType == Building.BuildingType.GoldRefinery)
                                {
                                    GoldRefinery newRefinery = new GoldRefinery(game, tempNode, graph, agent);
                                    Game.Components.Add(newRefinery);
                                    peonObjective = PeonObjective.Build;
                                    return;
                                }
                                else if (buildingType == Building.BuildingType.Barracks)
                                {
                                    Barracks newBarracks = new Barracks(game, tempNode, graph, agent);
                                    Game.Components.Add(newBarracks);
                                    peonObjective = PeonObjective.Build;
                                    return;
                                }
                            }
                        }
                    }
                }
                graph.SetNodeBlocked(position);
            }
            else if (peonState == PeonState.Mining)
            {
                graph.SetNodeBlocked(position);

                if (goldMine.GoldRemaining == 0)
                {
                    peonState = PeonState.Idle;
                }
                else if (goldCount != goldCountMax)
                {
                    timer += gameTime.ElapsedGameTime.Milliseconds;
                    if (timer >= timeToGetGold)
                    {
                        timer = 0;
                        if (goldToGetPerTime > goldMine.GoldRemaining)
                        {
                            goldCount += goldMine.TakeResource(goldMine.GoldRemaining);
                            peonState  = PeonState.Idle;
                        }
                        if (goldCount + goldToGetPerTime > goldCountMax)
                        {
                            goldCount += goldMine.TakeResource(goldCountMax - goldCount);
                        }
                        else
                        {
                            goldCount += goldMine.TakeResource(goldToGetPerTime);
                        }
                    }
                }
                else
                {
                    peonState = PeonState.Idle;
                }
            }
        }