示例#1
0
        public void Circle_ContainsVector2()
        {
            var circle = new CircleF(new Vector2(200.0f, 300.0f), 100.0f);

            var p1 = new Vector2(-1, -1);
            var p2 = new Vector2(110, 300);
            var p3 = new Vector2(200, 300);
            var p4 = new Vector2(290, 300);
            var p5 = new Vector2(400, 400);

            bool result;

            circle.Contains(ref p1, out result);
            Assert.AreEqual(false, result);
            circle.Contains(ref p2, out result);
            Assert.AreEqual(true, result);
            circle.Contains(ref p3, out result);
            Assert.AreEqual(true, result);
            circle.Contains(ref p4, out result);
            Assert.AreEqual(true, result);
            circle.Contains(ref p5, out result);
            Assert.AreEqual(false, result);

            Assert.AreEqual(false, circle.Contains(p1));
            Assert.AreEqual(true, circle.Contains(p2));
            Assert.AreEqual(true, circle.Contains(p3));
            Assert.AreEqual(true, circle.Contains(p4));
            Assert.AreEqual(false, circle.Contains(p5));
        }
示例#2
0
        public void Circle_ContainsCircle()
        {
            var circle  = new CircleF(new Vector2(200.0f, 300.0f), 100.0f);
            var circle1 = new CircleF(new Vector2(199.0f, 299.0f), 100.0f);
            var circle2 = new CircleF(new Vector2(200.0f, 300.0f), 25.0f);
            var circle3 = new CircleF(new Vector2(200.0f, 300.0f), 100.0f);
            var circle4 = new CircleF(new Vector2(201.0f, 301.0f), 100.0f);

            bool result;

            circle.Contains(ref circle1, out result);
            Assert.AreEqual(false, result);

            circle.Contains(ref circle2, out result);
            Assert.AreEqual(true, result);

            circle.Contains(ref circle3, out result);
            Assert.AreEqual(true, result);

            circle.Contains(ref circle4, out result);
            Assert.AreEqual(false, result);

            Assert.AreEqual(false, circle.Contains(circle1));
            Assert.AreEqual(true, circle.Contains(circle2));
            Assert.AreEqual(true, circle.Contains(circle3));
            Assert.AreEqual(false, circle.Contains(circle4));
        }
示例#3
0
        public void Circle_ContainsFloats()
        {
            var circle = new CircleF(new Vector2(200.0f, 300.0f), 100.0f);

            float x1 = -1; float y1 = -1;
            float x2 = 110; float y2 = 300;
            float x3 = 200; float y3 = 300;
            float x4 = 290; float y4 = 300;
            float x5 = 400; float y5 = 400;

            Assert.AreEqual(false, circle.Contains(x1, y1));
            Assert.AreEqual(true, circle.Contains(x2, y2));
            Assert.AreEqual(true, circle.Contains(x3, y3));
            Assert.AreEqual(true, circle.Contains(x4, y4));
            Assert.AreEqual(false, circle.Contains(x5, y5));
        }
示例#4
0
        private AShip SearchClosestShip(AShip ship, QuadTree quadTree, float range)
        {
            var possibleCollisions = new List <AShip>();

            quadTree.Retrieve(possibleCollisions, ship);
            AShip nearestShip = null;

            foreach (var potentialEnemyShip in possibleCollisions)
            {
                //only add ships that are new
                var rangeCircle = new CircleF(ship.Position, range); //scan radius
                if (rangeCircle.Contains(potentialEnemyShip.Position) && potentialEnemyShip.Owned &&
                    potentialEnemyShip.Hp >= 0)
                {
                    if (nearestShip == null)
                    {
                        nearestShip = potentialEnemyShip;
                    }
                    else if (Vector2.Distance(nearestShip.Position, ship.Position) > Vector2.Distance(potentialEnemyShip.Position, ship.Position))
                    {
                        nearestShip = potentialEnemyShip;
                    }
                }
            }
            return(nearestShip);
        }
示例#5
0
        private bool Pick(TNode node, PointF p)
        {
            CircleF boundary = GetBoundary(node);

            boundary.Radius += m_theme.PickTolerance;
            return(boundary.Contains(new Vec2F(p.X, p.Y)));
        }
示例#6
0
        public void CircCircIntersectionDiagonalCircleTest()
        {
            var circle = new CircleF(new Point2(16.0f, 16.0f), 16.0f);
            var point  = new Point2(0, 0);

            Assert.False(circle.Contains(point));
        }
示例#7
0
        /// <summary>
        /// Update defending ships
        /// Check for enemy Ships to shoot at while defending(Only defending Ships)
        /// </summary>
        /// <param name="gameTime"></param>
        private void UpdateDefendingShips(GameTime gameTime)
        {
            for (int i = 0; i < mDefendingShipList.Count; i++)
            {
                mDefendingShipList[i].Update(gameTime);
                //check if there are enemy ships in range
                if (mDefendingShipList[i].TargetShip == null)
                {
                    List <AShip> possibleCollisions = new List <AShip>();
                    mShipsQuadTree.Retrieve(possibleCollisions, (AShip)mDefendingShipList[i]);
                    foreach (var ship in possibleCollisions)
                    {
                        var     deShip      = (AShip)mDefendingShipList[i];
                        CircleF rangeCircle = new CircleF(deShip.Position, 150f);
                        if (rangeCircle.Contains(ship.Position) && !(ship.Owned && deShip.Owned) && !(!ship.Owned && !deShip.Owned))
                        {
                            mDefendingShipList[i].TargetShip = ship;
                            deShip.Moving = false;

                            break;//first ship to be seen will be attacked
                        }
                    }
                }
                //remove not defending ships from list
                var battleShip = mDefendingShipList[i] as BattleShip;
                if (battleShip != null)
                {
                    var defShip = battleShip;
                    if (defShip.CurrentBState != BattleShip.ShipBattleState.Defending)
                    {
                        mDefendingShipList.RemoveAt(i);
                    }
                }
                else if (mDefendingShipList[i] is TradingShip)
                {
                    var defShip = (TradingShip)mDefendingShipList[i];
                    if (defShip.CurrentBState != TradingShip.ShipBattleState.Defending)
                    {
                        mDefendingShipList.RemoveAt(i);
                    }
                }
            }
        }
示例#8
0
        public override Vector2 Calculate()
        {
            // The detection box is the current velocity divided by the max velocity of the entity
            // range is the maximum size of the box
            Vector2 viewBox = this.Entity.Velocity / this.Entity.MaxSpeed * this.range;

            // Add the check points in front of the entity
            IEnumerable <Vector2> checkpoints = new[]
            {
                this.Entity.Position,
                this.Entity.Position + viewBox / 2f, // Halfway
                this.Entity.Position + viewBox,      // At the end
                this.Entity.Position + viewBox * 2   // Double
            };

            foreach (Rock o in this.world.Entities.OfType <Rock>())
            {
                // Add a circle around the obstacle which can't be crossed
                CircleF notAllowedZone = new CircleF(o.Position, o.Scale);

                if (checkpoints.Any(checkpoint => notAllowedZone.Contains(checkpoint)))
                {
                    Vector2 dist          = new Vector2(o.Position.X - this.Entity.Position.X, o.Position.X - this.Entity.Position.Y);
                    Vector2 perpendicular = Vector2Helper.PerpendicularRightAngleOf(dist);

                    Vector2 perpendicularPositivePos = o.Position + perpendicular;
                    Vector2 perpendicularNegativePos = o.Position - perpendicular;

                    float perpDistPositive = Vector2.DistanceSquared(this.Entity.Position + this.Entity.Velocity, perpendicularPositivePos);
                    float perpDistNegative = Vector2.DistanceSquared(this.Entity.Position + this.Entity.Velocity, perpendicularNegativePos);

                    Vector2 targetRelative = (perpDistPositive > perpDistNegative ? perpendicularNegativePos : perpendicularPositivePos) - this.Entity.Position;

                    return(Vector2Helper.PerpendicularRightAngleOf(targetRelative));
                }
            }

            // Return identity vector if there will be no collision
            return(new Vector2());
        }
示例#9
0
        /// <summary>
        ///  Check for possible actions for the Fleets and single Ships controled by the AI.
        /// </summary>
        public void Update(GameTime gameTime, QuadTree quadTree)
        {
            //FisherShips
            var counter = mAiFisherShipList.Count;

            for (var i = 0; i < counter; i++)
            {
                var fShip = mAiFisherShipList[i];
                if (!fShip.Moving)
                {
                    if (mFishingMovingDelay <= 0)
                    {
                        var randomNumber = new Random();
                        //moving to a random position in the Map
                        var x = randomNumber.Next(0, 5760);
                        var y = randomNumber.Next(0, 5760);
                        fShip.Move(PathFinder.CalculatePath(fShip.Position, new Vector2(x, y), false));
                        mFishingMovingDelay = 8;
                    }
                    else
                    {
                        mFishingMovingDelay -= gameTime.ElapsedGameTime.TotalSeconds;
                    }
                }
                if (!fShip.Owned && fShip.Hp > 0)
                {
                    continue;
                }
                mAiFisherShipList.RemoveAt(i);
                counter--;
                fShip.HealthBar = null;
                fShip.CrewBar   = null;
                if (fShip.Owned
                    )
                {
                    Game1.mEventScreen.mEventManager.UpdateStatTotal("Ships Hijacked", 1);
                }
            }

            //TradingShips
            counter = mAiTradingShipList.Count;
            for (var i = 0; i < counter; i++)
            {
                var tShip = mAiTradingShipList[i];
                if (!tShip.Moving)
                {
                    if (mTradingMovingDelay <= 0)
                    {
                        var randomNumber = new Random();
                        var number       = randomNumber.Next(0, IslandManager.IslandCount);
                        var destination  = new Vector2(IslandManager.Islands[IslandManager.mIslandNames[number]].X + randomNumber.Next(20, 50),
                                                       IslandManager.Islands[IslandManager.mIslandNames[number]].Y + randomNumber.Next(20, 50));
                        if (Vector2.Distance(destination, tShip.Position) > 100 &&
                            Game1.mMapScreen.mGridMap.GetCell(destination).IsWalkable &&
                            !Game1.mMapScreen.mGridMap.GetCell(destination).Occupied)
                        {
                            tShip.Move(PathFinder.CalculatePath(tShip.Position, destination, false));
                            mTradingMovingDelay = 5;
                        }
                    }
                    else
                    {
                        mTradingMovingDelay -= gameTime.ElapsedGameTime.TotalSeconds;
                    }
                }

                if (tShip.Hp < tShip.MaxHp) //shoot back
                {
                    if (!mDefendingShipList.Contains(tShip))
                    {
                        tShip.Defend(tShip.ShipPath);
                        mDefendingShipList.Add(tShip);
                    }
                    if (tShip.Hp <= 40) //fleeing
                    {
                        tShip.TargetShip = null;
                        tShip.Move(tShip.ShipPath);
                        tShip.CurrentBState = TradingShip.ShipBattleState.Idle;
                        mDefendingShipList.Remove(tShip);
                    }
                }

                if (!tShip.Owned && tShip.Hp > 0)
                {
                    continue;
                }
                mAiTradingShipList.RemoveAt(i);
                counter--;
                if (tShip.Owned)
                {
                    tShip.CrewBar   = null;
                    tShip.HealthBar = null;
                    Game1.mEventScreen.mEventManager.UpdateStatTotal("Ships Hijacked", 1);
                    var random = new Random();
                    if (random.Next(0, 4) == 1)// 25% dropchance
                    {
                        RessourceManager.AddRessource("mapParts", 1);
                    }
                }
            }


            //BattleShips
            counter = mAiBattleShipsList.Count;
            for (var i = 0; i < counter; i++)
            {
                var bShip = mAiBattleShipsList[i];
                if (bShip.CurrentBState == BattleShip.ShipBattleState.Idle)
                {
                    if (!bShip.Moving)
                    {
                        if (mBattleMovingDelay <= 0)
                        {
                            //moving to a random position in the Map
                            var randomNumber = new Random();
                            var x            = randomNumber.Next(0, 5760);
                            var y            = randomNumber.Next(0, 5760);
                            bShip.Move(PathFinder.CalculatePath(bShip.Position, new Vector2(x, y), false));
                            mBattleMovingDelay = 3;
                        }
                        else
                        {
                            mBattleMovingDelay -= gameTime.ElapsedGameTime.TotalSeconds;
                        }
                    }

                    //scan for possible enemies in reach and attack them if they aren't to powefull
                    var possibleCollisions = new List <AShip>();
                    var enemyShipsCount    = 0;
                    quadTree.Retrieve(possibleCollisions, bShip);
                    if (possibleCollisions.Count > 0)
                    {
                        AShip nearestShip = null;
                        for (var index = 0; index < possibleCollisions.Count; index++)
                        {
                            var rangeCircle = new CircleF(bShip.Position, 500f); //scan radius
                            if (rangeCircle.Contains(possibleCollisions[index].Position) &&
                                possibleCollisions[index].Owned && possibleCollisions[index].Hp >= 0)
                            {
                                if (nearestShip == null)
                                {
                                    nearestShip = possibleCollisions[index];
                                }
                                enemyShipsCount++;
                                //save the nearest Ship
                                if (Vector2.Distance(bShip.Position, possibleCollisions[index].Position) <
                                    Vector2.Distance(bShip.Position, nearestShip.Position))
                                {
                                    nearestShip = possibleCollisions[index];
                                }
                            }
                        }
                        //only attack up to 3 ships
                        if (enemyShipsCount > 0 && enemyShipsCount <= 3 && nearestShip != null)
                        {
                            var random = new Random();
                            var number = random.Next(0, 2);
                            if ((number == 1) && nearestShip.EnterAttackValue - 8 < bShip.EnterAttackValue)
                            {
                                bShip.Move(PathFinder.CalculatePath(bShip.Position, nearestShip.Position, false));
                                bShip.Enter(nearestShip);
                                mEnteringShipList.Add(bShip);
                            }
                            else
                            {
                                bShip.Move(PathFinder.CalculatePath(bShip.Position, nearestShip.Position, false));
                                bShip.Attack(nearestShip);
                                mAttackingShipList.Add(bShip);
                            }
                        }
                        else if (enemyShipsCount > 3)
                        {
                            bShip.CurrentBState = BattleShip.ShipBattleState.Defending;
                            mDefendingShipList.Add(bShip);
                        }
                    }
                }

                if (!bShip.Owned && bShip.Hp > 0)
                {
                    continue;
                }
                mAiBattleShipsList.RemoveAt(i);
                counter--;
                bShip.CrewBar   = null;
                bShip.HealthBar = null;
                if (!Game1.mMapScreen.mTechDemo && bShip.Owned)
                {
                    Game1.mEventScreen.mEventManager.UpdateStatTotal("Ships Hijacked", 1);
                    var random = new Random();
                    if (random.Next(0, 3) == 1)// 33% dropchance
                    {
                        RessourceManager.AddRessource("mapParts", 1);
                    }
                }
            }

            //Fleets
            counter = mAiFleetList.Count;
            for (var t = 0; t < counter; t++)
            {
                var fleet = mAiFleetList[t];
                //check if the ships are still in the fleet
                var bCounter = fleet.BattleShips.Count;
                for (var i = 0; i < bCounter; i++)
                {
                    if (!(fleet.BattleShips[i].Hp <= 0) && !fleet.BattleShips[i].Owned)
                    {
                        continue;
                    }
                    fleet.BattleShips.RemoveAt(i);
                    bCounter--;
                }
                var tCounter = fleet.TradingShips.Count;
                for (var i = 0; i < tCounter; i++)
                {
                    if (!(fleet.TradingShips[i].Hp <= 0) && !fleet.TradingShips[i].Owned)
                    {
                        continue;
                    }
                    fleet.TradingShips.RemoveAt(i);
                    tCounter--;
                }

                //ceck if fleets are whiped out
                if (fleet.FleetCount() <= 0)
                {
                    mAiFleetList.Remove(fleet);
                    counter--;
                }


                if (!fleet.InBattle)
                {
                    //search for enemies when not in Battle
                    var possibleCollisions = new List <AShip>();
                    var toAttack           = new List <AShip>();
                    quadTree.Retrieve(possibleCollisions, fleet.FlagShip);
                    //check if the ships are enemies and in range
                    foreach (var potentialEnemyShip in possibleCollisions)
                    {
                        var rangeCircle = new CircleF(fleet.FlagShip.Position, 600f); //scan radius
                        if (rangeCircle.Contains(potentialEnemyShip.Position) && potentialEnemyShip.Owned &&
                            potentialEnemyShip.Hp >= 0)
                        {
                            toAttack.Add(potentialEnemyShip);
                        }
                    }
                    //if there are any enemies, attack them
                    if (toAttack.Count > 0)
                    {
                        fleet.Attack(toAttack);
                    }

                    //random movement. Determines with the Flagship if the fleet is moving.
                    if (!fleet.FlagShip.Moving && !fleet.Defending)
                    {
                        if (mBattleMovingDelay <= 0)
                        {
                            //moving to a random position in the Map
                            var randomNumber = new Random();
                            var x            = randomNumber.Next(0, 5760);
                            var y            = randomNumber.Next(0, 5760);
                            fleet.Move(new Vector2(x, y));
                            mBattleMovingDelay = 3;
                        }
                        else
                        {
                            mBattleMovingDelay -= gameTime.ElapsedGameTime.TotalSeconds;
                        }
                    }
                }
                else//when in battle
                {
                    //check if own ships were entered
                    for (int i = 0; i < fleet.BattleShips.Count; i++)
                    {
                        if (fleet.BattleShips[i].Owned)
                        {
                            fleet.MarkedShips.Add(fleet.BattleShips[i]);
                            fleet.BattleShips.RemoveAt(i);
                        }
                    }
                    //check if own ships were entered
                    for (int i = 0; i < fleet.TradingShips.Count; i++)
                    {
                        if (fleet.TradingShips[i].Owned)
                        {
                            fleet.MarkedShips.Add(fleet.TradingShips[i]);
                            fleet.TradingShips.RemoveAt(i);
                        }
                    }


                    //check if the marked ships still exist and the distance
                    var kCounter = fleet.MarkedShips.Count;
                    for (var i = 0; i < kCounter; i++)
                    {
                        if (fleet.MarkedShips[i].Hp <= 0 || !fleet.MarkedShips[i].Owned || Vector2.Distance(fleet.MarkedShips[i].Position, fleet.FlagShip.Position) > 600)
                        {
                            fleet.MarkedShips.RemoveAt(i);
                            kCounter--;
                            i--;
                        }
                    }

                    fleet.UpdateBattle();
                    //check for enemies
                    var possibleCollisions = new List <AShip>();
                    var toAttack           = new List <AShip>();
                    quadTree.Retrieve(possibleCollisions, fleet.FlagShip);
                    foreach (var potentialEnemyShip in possibleCollisions)
                    {
                        //only add ships that are new
                        var rangeCircle = new CircleF(fleet.FlagShip.Position, 700f); //scan radius
                        if (!fleet.MarkedShips.Contains(potentialEnemyShip) && rangeCircle.Contains(potentialEnemyShip.Position) && potentialEnemyShip.Owned &&
                            potentialEnemyShip.Hp >= 0)
                        {
                            toAttack.Add(potentialEnemyShip);
                        }
                    }
                    if (toAttack.Count > 0)
                    {
                        fleet.MarkedShips.AddRange(toAttack);
                    }


                    //if there is no enemy left, the fleet will stop battling
                    if (fleet.MarkedShips.Count <= 0)
                    {
                        fleet.InBattle = false;
                        fleet.StopBattle();
                    }
                }
            }

            //GhostShip
            if (GhostShip != null)
            {
                if (GhostShip.CurrentBState == GhostShip.ShipBattleState.Idle)
                {
                    //check if there are any enemyShips nearby
                    var enemyShips         = new List <AShip>();
                    var possibleCollisions = new List <AShip>();
                    quadTree.Retrieve(possibleCollisions, GhostShip);
                    foreach (var potentialEnemyShip in possibleCollisions)
                    {
                        //only add ships that are new
                        var rangeCircle = new CircleF(GhostShip.Position, 600f); //scan radius
                        if (!GhostShip.TargetShips.Contains(potentialEnemyShip) && rangeCircle.Contains(potentialEnemyShip.Position) && potentialEnemyShip.Owned &&
                            potentialEnemyShip.Hp >= 0)
                        {
                            enemyShips.Add(potentialEnemyShip);
                        }
                    }
                    if (enemyShips.Count > 0)
                    {
                        GhostShip.Attack(enemyShips);
                    }
                    //move to the island if not already next to it.
                    if (Vector2.Distance(GhostShip.Position, GhostShip.TreasureIslandPosition) >= 200 && !GhostShip.Moving)
                    {
                        GhostShip.Move(PathFinder.CalculatePath(GhostShip.Position, GhostShip.TreasureIslandPosition, true));
                    }
                }
                else if (GhostShip.CurrentBState == GhostShip.ShipBattleState.Attacking)
                {
                    GhostShip.Update(gameTime);
                    //check if there are any new enemyShips nearby
                    var possibleCollisions = new List <AShip>();
                    quadTree.Retrieve(possibleCollisions, GhostShip);
                    foreach (var potentialEnemyShip in possibleCollisions)
                    {
                        //only add ships that are new
                        var rangeCircle = new CircleF(GhostShip.Position, 600f); //scan radius
                        if (!GhostShip.TargetShips.Contains(potentialEnemyShip) && rangeCircle.Contains(potentialEnemyShip.Position) && potentialEnemyShip.Owned &&
                            potentialEnemyShip.Hp >= 0)
                        {
                            GhostShip.TargetShips.Add(potentialEnemyShip);
                        }
                    }

                    //moving
                    mGhostMovementDelay -= gameTime.ElapsedGameTime.TotalSeconds;
                    if (mGhostMovementDelay <= 0 && GhostShip.NearestTarget != null)
                    {
                        Vector2 movementTarget;
                        if (Vector2.Distance(GhostShip.NearestTarget.Position, GhostShip.Position) <= 120)
                        {
                            if (GhostShip.MoveRight)
                            {
                                movementTarget = GhostShip.Position +
                                                 (Vector2.Normalize(GhostShip.Position - GhostShip.NearestTarget.Position).PerpendicularCounterClockwise() * 100f);
                                GhostShip.MoveRight = false;
                            }
                            else
                            {
                                movementTarget = GhostShip.Position +
                                                 (Vector2.Normalize(GhostShip.Position - GhostShip.NearestTarget.Position).PerpendicularClockwise() * 100f);
                                GhostShip.MoveRight = true;
                            }
                            mGhostMovementDelay = 1.5;
                        }
                        else
                        {
                            movementTarget = GhostShip.NearestTarget.Position +
                                             Vector2.Normalize(GhostShip.Position - GhostShip.NearestTarget.Position) * 20f;
                            mGhostMovementDelay = 1;
                        }
                        GhostShip.Move(PathFinder.CalculatePath(GhostShip.Position, movementTarget, true));
                    }
                }
                if (GhostShip.Hp <= 0)
                {
                    GhostShip.DropLoot();
                    mAllShipList.Remove(GhostShip);
                    GhostShip = null;
                }
            }
            else
            {
                //spawn ships if necessary
                RespawnShips();
            }



            if (Octopus != null)
            {
                //search for enemies
                if (Octopus.CurrentBState == Octopus.ShipBattleState.Idle)
                {
                    /*
                     * var possibleCollisions = new List<AShip>();
                     * quadTree.Retrieve(possibleCollisions, Octopus);
                     * AShip nearestShip = null;
                     * foreach (var potentialEnemyShip in possibleCollisions)
                     * {
                     *  //only add ships that are new
                     *
                     *  var rangeCircle = new CircleF(Octopus.Position, 400f); //scan radius
                     *  if (rangeCircle.Contains(potentialEnemyShip.Position) && potentialEnemyShip.Owned &&
                     *       potentialEnemyShip.Hp >= 0)
                     *  {
                     *      if (nearestShip == null)
                     *      {
                     *          nearestShip = potentialEnemyShip;
                     *      }
                     *      else if (Vector2.Distance(nearestShip.Position, Octopus.Position) > Vector2.Distance(potentialEnemyShip.Position, Octopus.Position))
                     *      {
                     *          nearestShip = potentialEnemyShip;
                     *      }
                     *  }
                     * }*/
                    var nearestShip = SearchClosestShip(Octopus, quadTree, 400f);//testing
                    if (nearestShip != null)
                    {
                        Octopus.Enter(nearestShip);//attack the closest enemy
                    }

                    //move back to original position
                    if (Vector2.Distance(Octopus.Position, Octopus.HousingPosition) >= 200 && !Octopus.Moving)
                    {
                        Octopus.Move(PathFinder.CalculatePath(Octopus.Position, Octopus.HousingPosition, true));
                    }
                }
                else if (Octopus.CurrentBState == Octopus.ShipBattleState.Entering)
                {
                    Octopus.Update(gameTime);
                    mOctopusChasingDelay -= gameTime.ElapsedGameTime.TotalSeconds;
                    if (Octopus.TargetShip != null && Vector2.Distance(Octopus.Position, Octopus.TargetShip.Position) >= 70 && mOctopusChasingDelay <= 0)
                    {
                        mOctopusChasingDelay = 2;
                        Octopus.Move(PathFinder.CalculatePath(Octopus.Position, Octopus.TargetShip.Position, true));
                    }
                }
                if (Octopus.Hp <= 0)
                {
                    Octopus.DropLoot();
                    mAllShipList.Remove(Octopus);
                    Octopus = null;
                }
            }

            if (Dragon != null)
            {
                Dragon.Update(gameTime, quadTree);//special Updates for the dragon..
                Dragon.UpdateDragonMovement(gameTime);

                //check if the Dragoon is fighting or not
                if (Dragon.CurrentBState == Dragon.ShipBattleState.Idle)
                {
                    if (!Dragon.Moving)
                    {
                        //moving to a random position in the Map
                        var randomNumber = new Random();
                        var x            = randomNumber.Next(0, 5760);
                        var y            = randomNumber.Next(0, 5760);
                        Dragon.MoveDragon(new Vector2(x, y));
                    }

                    /*
                     * var possibleCollisions = new List<AShip>();
                     * quadTree.Retrieve(possibleCollisions, Dragon);
                     * AShip nearestShip = null;
                     * foreach (var potentialEnemyShip in possibleCollisions)
                     * {
                     *  //only add ships that are new
                     *  var rangeCircle = new CircleF(Octopus.Position, 200f); //scan radius
                     *  if (rangeCircle.Contains(potentialEnemyShip.Position) && potentialEnemyShip.Owned &&
                     *       potentialEnemyShip.Hp >= 0)
                     *  {
                     *      if (nearestShip == null)
                     *      {
                     *          nearestShip = potentialEnemyShip;
                     *      }
                     *      else if (Vector2.Distance(nearestShip.Position, Octopus.Position) > Vector2.Distance(potentialEnemyShip.Position, Octopus.Position))
                     *      {
                     *          nearestShip = potentialEnemyShip;
                     *      }
                     *  }
                     * }*/
                    var nearestShip = SearchClosestShip(Dragon, quadTree, 200f);
                    if (nearestShip != null)
                    {
                        Dragon.Attack(nearestShip); //attack the closest enemy
                    }
                }
                else if (Dragon.CurrentBState == Dragon.ShipBattleState.Attacking)
                {
                    //Chasing
                    if (Dragon.TargetShip != null && Vector2.Distance(Dragon.Position, Dragon.TargetShip.Position) >= 70)
                    {
                        Dragon.MoveDragon(Dragon.TargetShip.Position);
                    }
                }


                if (Dragon.Hp <= 0)
                {
                    Dragon.DropLoot();
                    mAllShipList.Remove(Dragon);
                    Dragon = null;
                }
            }
        }
示例#10
0
        /// <summary>
        /// Finds node and/or edge hit by the given point</summary>
        /// <param name="graph">Graph to test</param>
        /// <param name="priorityEdge">Graph edge to test before others</param>
        /// <param name="p">Point to test in graph space</param>
        /// <param name="g">D2dGraphics object</param>
        /// <returns>Hit record containing node and/or edge hit by the given point</returns>
        public override GraphHitRecord <TNode, TEdge, NumberedRoute> Pick(
            IGraph <TNode, TEdge, NumberedRoute> graph,
            TEdge priorityEdge,
            PointF p,
            D2dGraphics g)
        {
            TNode         pickedNode = null;
            TEdge         pickedEdge = null;
            NumberedRoute fromRoute  = null;
            NumberedRoute toRoute    = null;

            Vec2F v = new Vec2F(p.X, p.Y);

            if (priorityEdge != null &&
                Pick(priorityEdge, v))
            {
                pickedEdge = priorityEdge;
            }
            else
            {
                foreach (TEdge edge in graph.Edges.Reverse())
                {
                    if (Pick(edge, v))
                    {
                        pickedEdge = edge;
                        break;
                    }
                }
            }

            foreach (TNode node in graph.Nodes.Reverse())
            {
                if (Pick(node, p))
                {
                    pickedNode = node;

                    CircleF boundary = GetBoundary(node);
                    boundary.Radius -= m_theme.PickTolerance;
                    bool onEdge = !boundary.Contains(v);

                    if (pickedEdge == null)
                    {
                        if (onEdge)
                        {
                            // edge of node can be source or destination
                            fromRoute = new NumberedRoute();
                            toRoute   = new NumberedRoute();
                        }
                    }
                    else // hit on edge and node
                    {
                        if (onEdge)
                        {
                            if (pickedEdge.FromNode == pickedNode)
                            {
                                fromRoute = new NumberedRoute();
                            }
                            else if (pickedEdge.ToNode == pickedNode)
                            {
                                toRoute = new NumberedRoute();
                            }
                        }
                    }
                    break;
                }
            }

            var result = new GraphHitRecord <TNode, TEdge, NumberedRoute>(pickedNode, pickedEdge, fromRoute, toRoute);

            PointF clientP = Matrix3x2F.TransformPoint(g.Transform, p);

            if (fromRoute != null)
            {
                result.FromRoutePos = clientP;
            }
            if (toRoute != null)
            {
                result.ToRoutePos = clientP;
            }

            if (pickedNode != null && pickedEdge == null)
            {
                // label is centered in entire node
                RectangleF labelBounds = GetBounds(pickedNode, g);
                float      dHeight     = labelBounds.Height - m_theme.TextFormat.FontHeight;
                labelBounds = new RectangleF(
                    labelBounds.X, labelBounds.Y + dHeight / 2, labelBounds.Width, labelBounds.Height - dHeight);

                if (labelBounds.Contains(p))
                {
                    DiagramLabel label = new DiagramLabel(
                        Rectangle.Truncate(labelBounds),
                        TextFormatFlags.SingleLine |
                        TextFormatFlags.HorizontalCenter);

                    return
                        (new GraphHitRecord <TNode, TEdge, NumberedRoute>(pickedNode, label));
                }
            }
            else if (pickedEdge != null)
            {
                RectangleF   labelBounds = GetLabelBounds(pickedEdge, g);
                DiagramLabel label       = new DiagramLabel(
                    Rectangle.Truncate(labelBounds),
                    TextFormatFlags.SingleLine |
                    TextFormatFlags.HorizontalCenter);

                if (labelBounds.Contains(p))
                {
                    return
                        (new GraphHitRecord <TNode, TEdge, NumberedRoute>(pickedEdge, label));
                }
            }

            return(result);
        }
示例#11
0
 public bool Contains(Vector2 position)
 {
     return(BoundingCircle.Contains(position));
 }
示例#12
0
 public void ContainsPoint(CircleF circle, Point2 point, bool expectedToContainPoint)
 {
     Assert.AreEqual(expectedToContainPoint, circle.Contains(point));
     Assert.AreEqual(expectedToContainPoint, CircleF.Contains(circle, point));
 }
示例#13
0
        internal void HandleInput(GameTime gameTime)
        {
            //MouseState mouseState = Mouse.GetState();
            // Calculate the mousePosition in the map
            //mMouseInWorldPosition = mCamera2D.ScreenToWorld(new Vector2(mouseState.X, mouseState.Y));
            mMouseInWorldPosition = Game1.mMapScreen.GetWorldPosition(InputManager.MousePosition().ToVector2());
            float range = ResolutionManager.Scale().X *20;

            if (InputManager.LeftMouseButtonDown())
            {
                //check if there are ships selected to sort out how the input should be processed.
                if (sSelectShipList.Count == 0)
                {
                    //check for every Ship, if it is selected
                    foreach (var ship in sAllShipList)
                    {
                        // todo: How to rotate the selection "Box" of the Ship acording to its direction?
                        //testweise ein quadrat über die mitte des schiffs.
                        CircleF selectRange = new CircleF(ship.Position, range);
                        if (selectRange.Contains(mMouseInWorldPosition))
                        {
                            ship.Selected = true;
                            sSelectShipList.Add(ship);
                            break;//when the mouse was clicked, obviously one ship can be selected.
                        }
                    }
                }
                else
                {
                    //check for attack commands
                    if (InputManager.KeyPressed(Keys.C))
                    {
                        //check if there is a ship at the position that was clicked
                        foreach (var ship in sAllShipList)
                        {
                            //testweise ein quadrat über die mitte des schiffs.
                            Rectangle selectRange = new Rectangle((int)ship.Position.X - 20, (int)ship.Position.Y - 20, 40, 40);//new Rectangle(ship.Position.ToPoint(), new Point(100, 40));
                            if (selectRange.Contains(mMouseInWorldPosition) && !ship.Owned)
                            {
                                //every selected ship attacks the target ship
                                foreach (var selectedShip in sSelectShipList)
                                {
                                    if (selectedShip.Owned && !selectedShip.IsEntered)
                                    {
                                        selectedShip.Move(mPathFinder.CalculatePath(selectedShip.Position, ship.Position, true));
                                        var attackingShip = (IBattleObject)selectedShip;
                                        attackingShip.Attack(ship);
                                        mAttackingShipList.Add(attackingShip);
                                    }
                                }
                                break;//only one ship can be attacked at a time
                            }
                        }
                        UnselectShips();
                    }
                    else if (InputManager.KeyPressed(Keys.F)) //Defending Mode
                    {
                        foreach (var ship in sSelectShipList)
                        {
                            if (ship.Owned && !ship.IsEntered)
                            {
                                var defendingShip = (IBattleObject)ship;
                                defendingShip.Defend(mPathFinder.CalculatePath(ship.Position, mMouseInWorldPosition, true));
                                mDefendingShipList.Add(defendingShip);
                            }
                        }
                        UnselectShips();
                    }
                    else if (InputManager.KeyPressed(Keys.Q)) //Entering
                    {
                        foreach (var ship in sAllShipList)
                        {
                            //testweise ein quadrat über die mitte des schiffs.
                            var selectRange = new Rectangle((int)ship.Position.X - 20, (int)ship.Position.Y - 20, 40, 40);
                            if (selectRange.Contains(mMouseInWorldPosition) && !ship.Owned)
                            {
                                //every selected ship attacks the target ship
                                foreach (var selectedShip in sSelectShipList)
                                {
                                    if (selectedShip.Owned && !selectedShip.IsEntered && !selectedShip.Equals(ship))
                                    {
                                        if (selectedShip is BattleShip)
                                        {
                                            var enteringShip = (BattleShip)selectedShip;
                                            enteringShip.Move(mPathFinder.CalculatePath(selectedShip.Position, ship.Position, true));
                                            enteringShip.Enter(ship);
                                            if (ship.Moving)
                                            {
                                                enteringShip.Chasing = true;
                                            }
                                            if (!mEnteringShipList.Contains(enteringShip))
                                            {
                                                mEnteringShipList.Add(enteringShip);
                                            }
                                        }
                                    }
                                }
                                break;//only one ship can be entered at a time
                            }
                        }
                        UnselectShips();
                    }
                    else
                    {
                        // Set the Move command for every Selected Ship and calculates the Path
                        foreach (var ship in sSelectShipList)
                        {
                            //only if the ship is owned and selected.
                            if (ship.Owned)
                            {
                                ship.Move(mPathFinder.CalculatePath(ship.Position, mMouseInWorldPosition, true));
                                if (ship is BattleShip)
                                {
                                    var battleShip = (BattleShip)ship;
                                    battleShip.CurrentBState = BattleShip.ShipBattleState.Idle;
                                }
                                else if (ship is TradingShip)
                                {
                                    var tradingShip = (TradingShip)ship;
                                    tradingShip.CurrentBState = TradingShip.ShipBattleState.Idle;
                                }
                            }
                        }

                        UnselectShips();
                    }
                }
            }

            //Selectbox selecting
            if (InputManager.mSelectionBoxFinished)
            {
                // Inputmanager no longer supports "mouseInWorld" -> added these three lines, code still working - Jonas
                InputManager.mSelectionBoxFinished = false;
                var selBoxOld  = InputManager.SelectBox();
                var newCoords  = Game1.mMapScreen.GetWorldPosition(new Vector2(selBoxOld.X, selBoxOld.Y));
                var selBox     = new Rectangle((int)newCoords.X, (int)newCoords.Y, selBoxOld.Width, selBoxOld.Height);
                var shipsOwned = false;
                foreach (var ship in sAllShipList)
                {
                    if (!selBox.Contains(ship.Position))
                    {
                        continue;
                    }
                    ship.Selected = true;
                    sSelectShipList.Add(ship);
                    if (ship.Owned)
                    {
                        shipsOwned = true;
                    }
                }
                if (shipsOwned)
                {
                    for (var i = 0; i < sSelectShipList.Count; i++)
                    {
                        if (sSelectShipList[i].Owned == false)
                        {
                            sSelectShipList[i].Selected = false;
                            sSelectShipList.RemoveAt(i);
                        }
                    }
                }
            }

            //check for repairing commands
            if (InputManager.KeyPressed(Keys.R))
            {
                foreach (var ship in sSelectShipList)
                {
                    if (ship.Owned && !mRepairingShipList.Contains(ship))
                    {
                        ship.Repairing = true;
                        mRepairingShipList.Add(ship);
                    }
                }
            }



            // deselect all ships.
            if (InputManager.RightMouseButtonDown())
            {
                UnselectShips();
            }


            //Hud aufrufen
            if (sSelectShipList.Count >= 1)
            {
                HudScreen.HudScreenInstance.ShowShipControl(sSelectShipList);
            }
            else
            {
                HudScreen.HudScreenInstance.HideShipControl();
            }
        }
示例#14
0
        /// <summary>
        ///  Calls update() on every ship. Checks for input and manages Selecting
        /// and the execution of commands.
        /// </summary>
        public void Update(GameTime gameTime)
        {
            mShipsQuadTree.Clear();
            foreach (var shipu in AllShips)
            {
                mShipsQuadTree.Insert(shipu);
            }
            //general update for all ships
            for (int i = 0; i < sAllShipList.Count; i++)
            {
                sAllShipList[i].UpdateMoving(gameTime);
                if (sAllShipList[i].Hp <= 0)
                {
                    sAllShipList.RemoveAt(i);
                }
            }
            //Call Update for every Attacking Ship
            for (int i = 0; i < mAttackingShipList.Count; i++)
            {
                mAttackingShipList[i].Update(gameTime);

                if (mAttackingShipList[i].Chasing && mAttackingRefreshRate <= 0)
                {
                    mAttackingRefreshRate = 2;
                    var atkShip = (AShip)mAttackingShipList[i];
                    atkShip.Move(mPathFinder.CalculatePath(atkShip.Position, mAttackingShipList[i].TargetShip.Position, true));
                }
                else
                {
                    mAttackingRefreshRate -= gameTime.ElapsedGameTime.TotalSeconds;
                }

                //remove not attacking ships
                var ship = mAttackingShipList[i] as BattleShip;
                if (ship != null)
                {
                    var atkShip = ship;
                    if (atkShip.CurrentBState != BattleShip.ShipBattleState.Attacking)
                    {
                        mAttackingShipList.RemoveAt(i);
                    }
                }
                else if (mAttackingShipList[i] is TradingShip)
                {
                    var atkShip = (TradingShip)mAttackingShipList[i];
                    if (atkShip.CurrentBState != TradingShip.ShipBattleState.Attacking)
                    {
                        mAttackingShipList.RemoveAt(i);
                    }
                }
            }

            //Check for enemy Ships to shoot at while defending(Only defending Ships)
            for (int i = 0; i < mDefendingShipList.Count; i++)
            {
                mDefendingShipList[i].Update(gameTime);
                //check if there are enemy ships in range
                if (mDefendingShipList[i].TargetShip == null)
                {
                    /* old version, as backup...
                     * foreach (var ship in sAllShipList)
                     * {
                     *
                     *  var deShip = (AShip) mDefendingShipList[i];
                     *  CircleF rangeCircle = new CircleF(deShip.Position, 100f);
                     *  if (rangeCircle.Contains(ship.Position) && !(ship.Owned && deShip.Owned) && !(!ship.Owned && !deShip.Owned))
                     *  {
                     *      mDefendingShipList[i].TargetShip = ship;
                     *      //deShip.Moving = false;
                     *
                     *      break;//first ship to be seen will be attacked
                     *  }
                     * } */

                    //QuadTree test
                    List <AShip> possibleCollisions = new List <AShip>();
                    mShipsQuadTree.Retrieve(possibleCollisions, (AShip)mDefendingShipList[i]);
                    foreach (var ship in possibleCollisions)
                    {
                        var     deShip      = (AShip)mDefendingShipList[i];
                        CircleF rangeCircle = new CircleF(deShip.Position, 150f);
                        if (rangeCircle.Contains(ship.Position) && !(ship.Owned && deShip.Owned) && !(!ship.Owned && !deShip.Owned))
                        {
                            mDefendingShipList[i].TargetShip = ship;
                            deShip.Moving = false;

                            break;//first ship to be seen will be attacked
                        }
                    }
                }


                //remove not defending ships from list
                var battleShip = mDefendingShipList[i] as BattleShip;
                if (battleShip != null)
                {
                    var defShip = battleShip;
                    if (defShip.CurrentBState != BattleShip.ShipBattleState.Defending)
                    {
                        mDefendingShipList.RemoveAt(i);
                    }
                }
                else if (mDefendingShipList[i] is TradingShip)
                {
                    var defShip = (TradingShip)mDefendingShipList[i];
                    if (defShip.CurrentBState != TradingShip.ShipBattleState.Defending)
                    {
                        mDefendingShipList.RemoveAt(i);
                    }
                }
            }

            //Update and Pathrefreshing for Entering Ships
            for (int i = 0; i < mEnteringShipList.Count; i++)
            {
                //remove not entering ships from list
                if (mEnteringShipList[i].CurrentBState != BattleShip.ShipBattleState.Entering)
                {
                    mEnteringShipList.RemoveAt(i);
                }
                else
                {
                    //Restriction to Pathrefreshing for Chases
                    if (mEnteringRefreshRate <= 0)
                    {
                        mEnteringRefreshRate = 2;
                        //check if the targetShips are moving and sets new Paths
                        //aktuell immer, weil targetShip nichtmehr verfolgt wird nachdem es stehen bleibt.

                        if (mEnteringShipList[i].Chasing)
                        {
                            mEnteringShipList[i].Move(mPathFinder.CalculatePath(mEnteringShipList[i].Position,
                                                                                mEnteringShipList[i].TargetShip.Position, true));
                        }

                        /*
                         * Alternative: zu inteligent wahrscheinlich...
                         * if (mEnteringShipList[i].Chasing &&
                         *  !mEnteringShipList[i].ShipPath.TargetNode.Equals(mEnteringShipList[i].TargetShip.ShipPath.TargetNode))
                         * {
                         *  mEnteringShipList[i].Move(mPathFinder.CalculatePath(mEnteringShipList[i].Position,
                         *      mEnteringShipList[i].TargetShip.ShipPath.TargetNode.Cell.Position,true));
                         * }
                         */
                    }
                    else
                    {
                        mEnteringRefreshRate -= gameTime.ElapsedGameTime.TotalSeconds;
                    }
                    mEnteringShipList[i].Update(gameTime);
                }
            }

            //todo: check if hudScreen is in selecting process for attack/defense/enter
            //only check input if the specified boolean(from hud) is false



            //update for all ships repairing
            for (var i = 0; i < mRepairingShipList.Count; i++)
            {
                if (mRepairingShipList[i].Hp < mRepairingShipList[i].MaxHp &&
                    RessourceManager.GetRessourceFloat("wood") >= mRepairingShipList[i].RepairValue * 0.01f)
                {
                    mRepairingShipList[i].Hp += mRepairingShipList[i].RepairValue * 0.005f;
                    //Wood -= mRepairingShipList[i].RepairValue*0.01f;
                    RessourceManager.AddRessourceFloat("wood", -mRepairingShipList[i].RepairValue * 0.01f);
                }
                else
                {
                    //that no ship can get to much hP with repairing.
                    if (mRepairingShipList[i].Hp > mRepairingShipList[i].MaxHp)
                    {
                        mRepairingShipList[i].Hp = (int)mRepairingShipList[i].MaxHp;
                    }
                    mRepairingShipList[i].Repairing = false;
                    mRepairingShipList.RemoveAt(i);
                }
            }
        }
示例#15
0
        private void HandleInputGameTypeSwapper()
        {
            var mouseState = Mouse.GetState();

            // if you've just clicked select the current tile
            if (mouseState.LeftButton == ButtonState.Pressed && oldMouseState.LeftButton == ButtonState.Released)
            {
                foreach (var tile in tiles.Where(x => x.isHome == false).OrderByDescending(x => x.sprite.Depth))
                {
                    if (tile.GetBoundingBox().Contains(mouseState.Position))
                    {
                        // If No tile is selected, select the tile.
                        if (selectedTile == null)
                        {
                            selectedTile = tile;
                        }
                        else if (selectedTile.Position == tile.Position)
                        {
                            selectedTile = null;
                        }
                        else //swap tile positions and deselect Tile.
                        {
                            Vector2 selectedTilePosition = selectedTile.Position;
                            selectedTile.Position = tile.Position;
                            tile.Position         = selectedTilePosition;
                            selectedTile          = null;
                        }
                        break;
                    }
                }
            }

            // Right Click for Rotation
            if (gameSettings.randomlyRotateTiles && mouseState.RightButton == ButtonState.Pressed && oldMouseState.RightButton == ButtonState.Released && selectedTile == null)
            {
                foreach (var tile in tiles.Where(x => x.isHome == false).OrderByDescending(x => x.sprite.Depth))
                {
                    if (tile.GetBoundingBox().Contains(mouseState.Position) && tile.isHome == false)
                    {
                        tile.rotation = RotationHelper.Rotate90Degrees(tile.rotation);

                        // Handle Snapping
                        if (tile.rotation == 0f)
                        {
                            CircleF circle = new CircleF(tile.homePosition, 20f);
                            if (circle.Contains(tile.Position))
                            {
                                tile.Position     = tile.homePosition;
                                tile.isHome       = true;
                                tile.sprite.Depth = Constants.Depth.GameDepthVariance;
                                UpdatePercentageComplete();
                            }
                        }
                        break;
                    }
                }
            }

            // Right Click for Rotation while holding left click and having a tile selected
            if (gameSettings.randomlyRotateTiles && mouseState.LeftButton == ButtonState.Pressed && oldMouseState.LeftButton == ButtonState.Pressed && selectedTile != null &&
                mouseState.RightButton == ButtonState.Pressed && oldMouseState.RightButton == ButtonState.Released)
            {
                selectedTile.rotation = RotationHelper.Rotate90Degrees(selectedTile.rotation);
            }

            oldMouseState = mouseState;
        }
示例#16
0
        private void HandleInputGameTypeShuffle()
        {
            var mouseState = Mouse.GetState();

            // if you've just clicked select the current tile
            if (mouseState.LeftButton == ButtonState.Pressed && oldMouseState.LeftButton == ButtonState.Released)
            {
                foreach (var tile in tiles.Where(x => x.isHome == false).OrderByDescending(x => x.sprite.Depth))
                {
                    if (tile.GetBoundingBox().Contains(mouseState.Position))
                    {
                        RectangleF shadowTileHitBox = shadowTile.GetBoundingBox();
                        var        minX             = shadowTileHitBox.X - (shadowTileHitBox.Width * 1.1f);
                        var        minY             = shadowTileHitBox.Y - (shadowTileHitBox.Height * 1.1f);
                        var        maxX             = shadowTileHitBox.X + shadowTileHitBox.Width + (shadowTileHitBox.Width * 1.1f);
                        var        maxY             = shadowTileHitBox.Y + shadowTileHitBox.Height + (shadowTileHitBox.Height * 1.1f);

                        if (minX < 0)
                        {
                            minX = 0;
                        }
                        if (minY < 0)
                        {
                            minY = 0;
                        }

                        var width  = maxX - minX;
                        var height = maxY - minY;

                        // Two hitboxes create a + shape hitbox to exclude diagonals.
                        RectangleF extendedHitboxX = new RectangleF(minX, shadowTileHitBox.Y, width, shadowTileHitBox.Height);
                        RectangleF extendedHitboxY = new RectangleF(shadowTileHitBox.X, minY, shadowTileHitBox.Width, height);

                        if (extendedHitboxX.Contains(tile.GetBoundingBox().Center) || extendedHitboxY.Contains(tile.GetBoundingBox().Center)) // if close to shadowTile
                        {
                            Vector2 selectedTilePosition = tile.Position;
                            tile.Position       = shadowTile.Position;
                            shadowTile.Position = selectedTilePosition;
                            selectedTile        = null;
                        }
                        break;
                    }
                }
            }

            // Right Click for Rotation
            if (gameSettings.randomlyRotateTiles && mouseState.RightButton == ButtonState.Pressed && oldMouseState.RightButton == ButtonState.Released && selectedTile == null)
            {
                foreach (var tile in tiles.Where(x => x.isHome == false).OrderByDescending(x => x.sprite.Depth))
                {
                    if (tile.GetBoundingBox().Contains(mouseState.Position) && tile.isHome == false)
                    {
                        tile.rotation = RotationHelper.Rotate90Degrees(tile.rotation);

                        // Handle Snapping
                        if (tile.rotation == 0f)
                        {
                            CircleF circle = new CircleF(tile.homePosition, 20f);
                            if (circle.Contains(tile.Position))
                            {
                                tile.Position     = tile.homePosition;
                                tile.isHome       = true;
                                tile.sprite.Depth = Constants.Depth.GameDepthVariance;
                                UpdatePercentageComplete();
                            }
                        }
                        break;
                    }
                }
            }

            // Right Click for Rotation while holding left click and having a tile selected
            if (gameSettings.randomlyRotateTiles && mouseState.LeftButton == ButtonState.Pressed && oldMouseState.LeftButton == ButtonState.Pressed && selectedTile != null &&
                mouseState.RightButton == ButtonState.Pressed && oldMouseState.RightButton == ButtonState.Released)
            {
                selectedTile.rotation = RotationHelper.Rotate90Degrees(selectedTile.rotation);
            }

            oldMouseState = mouseState;
        }
示例#17
0
        // TODO: Use Engine.GetVisibleRegions(Husk husk);

        public bool Judge(Husk husk)
        {
            if (Ids?.Count() > 0)
            {
                return(Ids.Contains(husk.Location.Id));
            }

            if (Region.HasValue)
            {
                Location location = husk.Location.GetLocation();

                if (Structure.HasValue)
                {
                    var results = location.Filter(Structure.Value);

                    if (results.Any(x => (Interaction == InteractionType.View ? husk.Hitbox.Sight : husk.Hitbox.Reach)
                                    .Intersects(x.Shape)))
                    {
                        return(true);
                    }
                }

                if (Location.HasValue)
                {
                    if (Construct.HasValue && (location as Construct) != null)
                    {
                        if (!Construct.Value.HasFlag((location as Construct).Tag))
                        {
                            return(false);
                        }
                    }

                    if (!Location.Value.HasFlag(location.Type))
                    {
                        return(false);
                    }
                }

                if (!Region.Value.HasFlag(location.Subtype))
                {
                    return(false);
                }
            }

            if (X.HasValue && Y.HasValue)
            {
                float x = husk.Location.X;
                float y = husk.Location.Y;

                if (!string.IsNullOrWhiteSpace(Id))
                {
                    if (Id != husk.Location.Id)
                    {
                        return(false);
                    }
                }

                if (Width > 0 && Height > 0)
                {
                    return(RegionF.Contains(X.Value, Y.Value, Width, Height, x, y));
                }

                if (Radius > 0)
                {
                    return(CircleF.Contains(X.Value, Y.Value, Radius, x, y));
                }

                return((Interaction == InteractionType.View ? husk.Hitbox.Sight : husk.Hitbox.Reach)
                       .Contains(X.Value, Y.Value));
            }

            if (!string.IsNullOrWhiteSpace(Id))
            {
                if (Depth == BindDepth.In)
                {
                    Location location = Engine.World.Find(Id);

                    return(location.GetChildren().Any(x => x.Id == husk.Location.Id));
                }

                return(Id == husk.Location.Id);
            }

            Console.WriteLine("No eligible regions were matched.");
            return(false);
        }
示例#18
0
 public bool Contains(Vector2 position)
 {
     return(_shape.Contains(position));
 }
示例#19
0
 /// <summary>
 /// Tests whether or not the point supplied is contained within the target.
 /// </summary>
 /// <param name="pt">The point to test.</param>
 /// <returns>True if the point is contained; false otherwise.</returns>
 /// <remarks>Note that is is not sufficient to use the TargetBounds property
 /// to hit-test the point, since not all targets are rectangular in shape.</remarks>
 public override bool TargetContains(PointF pt)
 {
     return(_thisCircle.Contains(pt));
 }
示例#20
0
        /// <summary>
        /// Handle all Input
        /// </summary>
        internal void HandleInput()
        {
            //MouseState mouseState = Mouse.GetState();
            // Calculate the mousePosition in the map
            //mMouseInWorldPosition = mCamera2D.ScreenToWorld(new Vector2(mouseState.X, mouseState.Y));
            mMouseInWorldPosition = Game1.mMapScreen.GetWorldPosition(InputManager.MousePosition().ToVector2());
            var range = ResolutionManager.Scale().X *20;

            // Some stuff only used for demonstration.
            // Spawn a battle Ship to the right of the flagship.
            if (InputManager.KeyDown(Keys.I))
            {
                mAiShipManager.SpawnAiBattleShip(FlagShip.Position + 200 * Vector2.UnitX);
            }
            // Spawn the Admiral fleet.
            if (InputManager.KeyDown(Keys.K))
            {
                Game1.mEventScreen.mEventManager.StartQuest3();
            }
            // Spawn the GhostShip.
            if (InputManager.KeyDown(Keys.N))
            {
                Game1.mMapScreen.mPlayerShipManager.mAiShipManager.SpawnGhostShip(new Vector2(4200, 860));
                Game1.mEventScreen.mEventManager.mGhostShipinGame = true;
            }
            // Add KartenFragment
            if (InputManager.KeyDown(Keys.L))
            {
                RessourceManager.AddRessource("mapParts", +1);
            }
            // Spawn own Battleship near us.
            if (InputManager.KeyDown(Keys.J))
            {
                SpawnBattleShip(FlagShip.Position - 200 * Vector2.UnitX, true);
            }

            if (InputManager.LeftMouseButtonDown())
            {
                //check if there are ships selected to sort out how the input should be processed.
                if (mSelectShipList.Count == 0)
                {
                    //check for every Ship, if it is selected
                    foreach (var ship in mAllShipList)
                    {
                        //testweise ein quadrat über die mitte des schiffs.
                        var selectRange = new CircleF(ship.Position, range);
                        if (!selectRange.Contains(mMouseInWorldPosition))
                        {
                            continue;
                        }
                        if (VisibilityManager.IsVisible(mMouseInWorldPosition))
                        {
                            ship.Selected = true;
                            mSelectShipList.Add(ship);
                        }

                        break; //when the mouse was clicked, obviously one ship can be selected.
                    }
                }
                else//unselect Ships
                {
                    UnselectShips();
                }
            }
            else if (InputManager.RightMouseButtonDown() && mSelectShipList.Count > 0)
            {
                if (InputManager.KeyPressed(InputManager.HotKey("Hk1")) || mShipState == ShipState.Attacking)
                {
                    SetShipsAttacking();
                }
                else if (InputManager.KeyPressed(InputManager.HotKey("Hk3")) || mShipState == ShipState.Defending)
                {
                    SetShipsDefending();
                }
                else if (InputManager.KeyPressed(InputManager.HotKey("Hk2")) || mShipState == ShipState.Boarding)
                {
                    SetShipsBoarding();
                }
                else
                {
                    SetShipsMoving();
                }
            }


            //Selectbox selecting
            if (InputManager.mSelectionBoxFinished)
            {
                InputManager.mSelectionBoxFinished = false;
                var selBoxOld  = InputManager.SelectBox();
                var shipsOwned = false;
                foreach (var ship in mAllShipList)
                {
                    if (!selBoxOld.Contains(ship.Position))
                    {
                        continue;
                    }
                    if (VisibilityManager.IsVisible(selBoxOld))
                    {
                        ship.Selected = true;
                        mSelectShipList.Add(ship);
                        if (ship.Owned)
                        {
                            shipsOwned = true;
                        }
                    }
                }
                if (shipsOwned)
                {
                    var actuallySelected = new List <AShip>();
                    foreach (var ship in SelectShipList)
                    {
                        if (ship.Owned)
                        {
                            actuallySelected.Add(ship);
                        }
                        else
                        {
                            ship.Selected = false;
                        }
                    }
                    SelectShipList = actuallySelected;
                }
            }

            //check for repairing commands
            if (InputManager.KeyPressed(InputManager.HotKey("Hk4")))
            {
                AddRepairingShips();
            }
            else if (InputManager.KeyReleased(InputManager.HotKey("Hk5"))) // Rum
            {
                foreach (var ship in mSelectShipList)
                {
                    if (RessourceManager.GetRessourceInt("rum") > 0)
                    {
                        Random random = new Random();
                        SoundManager.PlayEfx("efx/burping/" + random.Next(1, 7));
                        //increase crew if its the first rum
                        if (ship.RumBuffDuration <= 0f)
                        {
                            ship.AttackValue      += 5;
                            ship.EnterAttackValue += 5;
                            ship.RepairingValue   += 5;
                            ship.MovementSpeed    += 0.1f;
                        }
                        //increase duration and rumCounter
                        ship.RumDrunken++;
                        ship.RumBuffDuration += 5;
                        //ship.RumBuffDuration += 7;
                        RessourceManager.AddRessource("rum", -1);
                    }
                    else
                    {
                        SoundManager.PlayAmb("No_Rum", false);
                    }
                }
            }


            //Hud aufrufen

            /* if (mSelectShipList.Count >= 1)
             * {
             *   HudScreen.HudScreenInstance.ShowShipControl(mSelectShipList);
             * }
             * else
             * {
             *   HudScreen.HudScreenInstance.HideShipControl();
             * }*/
        }
示例#21
0
        public void Update(GameTime gameTime, QuadTree quadTree)
        {
            if (Hp <= 0)
            {
                CurrentBState = ShipBattleState.Idle;
            }
            if (CurrentBState == ShipBattleState.Attacking && TargetShip != null &&
                ((!TargetShip.Owned && Owned) || (!Owned && TargetShip.Owned)))
            {
                Chasing = TargetShip.Moving;

                if (Vector2.Distance(Position, TargetShip.Position) <= 150f)
                {
                    if (!Chasing)
                    {
                        Moving = false;
                    }
                    else if (Vector2.Distance(Position, TargetShip.Position) <= 100f)
                    {
                        Moving = false;
                    }


                    //Chasing = false;
                    //if a movement command is set, the ship will only move and stop shooting

                    //shooting
                    mShootingDelay -= gameTime.ElapsedGameTime.TotalSeconds;
                    if (mShootingDelay <= 0 && TargetShip.Hp > 0)
                    {
                        Game1.mMapScreen.ShootBullet(Position, TargetShip.Position);
                        mShootingDelay = 3f;//reset the timer
                        TargetShip.Hp -= (int)((AttackValue + AttackValueUpgrade) * 0.25f);
                        //now deal damage to the Ships around the TargetShip
                        var possibleCollisions = new List <AShip>();
                        quadTree.Retrieve(possibleCollisions, TargetShip);
                        foreach (var potentialVictim in possibleCollisions)
                        {
                            //only dmg ships in a certain range
                            var rangeCircle = new CircleF(TargetShip.Position, 40f); //scan radius
                            if (rangeCircle.Contains(potentialVictim.Position) && potentialVictim.Owned &&
                                potentialVictim.Hp >= 0)
                            {
                                potentialVictim.Hp -= (int)((AttackValue + AttackValueUpgrade) * 0.25f);//dmg dealed
                            }
                        }

                        if (TargetShip.Hp <= 0)
                        {
                            Game1.mMapScreen.Drowning(TargetShip.Position, 6);
                            CurrentBState = ShipBattleState.Idle;
                            TargetShip    = null;
                        }
                    }
                    else if (TargetShip.Hp <= 0)
                    {
                        CurrentBState = ShipBattleState.Idle;
                        TargetShip    = null;
                    }
                }
            }
        }
示例#22
0
        public IEnumerable <Vector2> SmoothPath(IEnumerable <Vector2> path)
        {
            LinkedList <Vector2> linkedPath = new LinkedList <Vector2>(path);
            // Select the first item of the path and yield return it
            LinkedListNode <Vector2> node = linkedPath.First;

            yield return(node.Value);

            LinkedListNode <Vector2> lastNode = linkedPath.Last;
            bool collides = false;

            foreach (BaseGameEntity baseGameEntity in world.Entities.OfType <Rock>())
            {
                CircleF notAllowedZone = new CircleF(baseGameEntity.Position.ToPoint(), baseGameEntity.Scale);
                if (notAllowedZone.Contains(lastNode.Value))
                {
                    collides = true;

                    break;
                }
            }

            if (collides)
            {
                linkedPath.Remove(lastNode);
            }

            // Loop through the path
            for (int i = 0; i < linkedPath.Count(); i++)
            {
                // Setting the distanceOrignal and add the first node to the calculation
                LinkedListNode <Vector2> add = node;
                float distOriginal           = 0;

                // Loop through the amount of times is allowed
                for (int j = 0; j < nodesAhead; j++)
                {
                    if (add.Next == null)
                    {
                        break;
                    }
                    add = add.Next;

                    if (add != node)
                    {
                        distOriginal += Vector2.DistanceSquared(node.Value, add.Value);
                    }
                }

                // Small check to see if add was changed or is not null
                if (add != node)
                {
                    // Checking if the distance from the source to the second next node is faster by approaching it directly or stayed the same
                    // if that is the case, yield return the new add value
                    float distNew      = Vector2.DistanceSquared(node.Value, add.Value);
                    bool  collidesLine = false;
                    if (distNew <= distOriginal)
                    {
                        foreach (BaseGameEntity baseGameEntity in world.Entities.OfType <Rock>())
                        {
                            CircleF notAllowedZone =
                                new CircleF(baseGameEntity.Position.ToPoint(), baseGameEntity.Scale);
                            Vector2 line = new Vector2(add.Value.X - node.Value.X, add.Value.Y - node.Value.Y);
                            IEnumerable <Vector2> checkpoints = new[]
                            {
                                line,
                                line / nodesAhead,
                            };
                            if (checkpoints.Any(checkpoint => notAllowedZone.Contains(checkpoint)))
                            {
                                collidesLine = true;

                                break;
                            }
                        }

                        if (!collidesLine)
                        {
                            node = add;
                            yield return(add.Value);
                        }
                    }

                    if (distNew > distOriginal || collidesLine)
                    {
                        add = node;
                        yield return(node.Value);

                        for (int j = 0; j < nodesAhead; j++)
                        {
                            if (add.Next == null)
                            {
                                break;
                            }
                            add = add.Next;

                            if (add != node)
                            {
                                yield return(add.Value);
                            }
                        }
                    }
                }
            }
        }