Ejemplo n.º 1
0
        private void CheckCollisions(Projectile projectile, GsPolygon projectilePolygon, UpdateParams uparams)
        {
            // if the projectile parent is null, then don't worry about it
            if (projectile.Parent == null)
            {
                return;
            }

            // cycle through all of the entities
            for (int i = mInvaders.Count - 1; i > -1; --i)
            {
                // get the entity
                Invader invader = mInvaders[i];

                // if this invader isn't the type of invader that the projectile can hit, then keep going
                if (!projectile.CanHit(invader))
                {
                    continue;
                }

                // get the entity polygon
                GsPolygon entityPolygon = invader.GetHull(uparams.Offset);

                // let the entity and polygon know they were attacked
                if (entityPolygon.IntersectsWith(projectilePolygon))
                {
                    // let the projectile know it collided
                    projectile.OnCollidedWithInvader(invader);

                    // let the entity know it was attacked
                    invader.OnAttackedByProjectile(projectile);
                }
            }
        }
Ejemplo n.º 2
0
 private void UpdateInvaders(UpdateParams uparams)
 {
     for (int i = mInvaders.Count - 1; i > -1; --i)
     {
         Invader entity = mInvaders[i];
         entity.Update(uparams.Elapsed);
         if (entity.State != InvaderState.Alive)
         {
             mInvaders.RemoveAt(i);
             if (entity.State == InvaderState.MadeIt)
             {
                 Player.InvaderGotThrough(entity);
                 if (!Player.IsAlive)
                 {
                     GameOver = true;
                     i        = -1;
                 }
             }
             else
             {
                 Player.CollectSpoils(entity);
             }
         }
     }
 }
Ejemplo n.º 3
0
 public LightningProjectile(Piece parent, double timeToLiveInSeconds, Invader target)
     : base(parent, timeToLiveInSeconds)
 {
     mTarget  = target;
     Size     = new GsSize(48f, 12f);
     ImageKey = "lightning";
 }
Ejemplo n.º 4
0
        private Invader RetrieveCurrentSelectedInvader(UpdateParams uparams)
        {
            Invader retval = mSelectedInvader;

            if (retval != null && retval.State != InvaderState.Alive)
            {
                retval = null;
            }

            if (uparams.Input.SelectObject)
            {
                // if they click, then reset the selection
                retval = null;
                for (int i = mInvaders.Count - 1; i > -1; --i)
                {
                    Invader invader = mInvaders[i];
                    if (invader.State == InvaderState.Alive)
                    {
                        GsRectangle box = invader.GetBoundingBox(uparams.Offset);
                        if (box.Contains(uparams.Input.CursorPosition))
                        {
                            retval = invader;
                            i      = -1;
                        }
                    }
                }
            }

            mSelectedInvader = retval;
            return(retval);
        }
Ejemplo n.º 5
0
        public IEnumerable <Invader> GenerateWave(GridDestination horizontal, GridDestination vertical, IEnumerable <Piece> pieces, GridCell[,] grid)
        {
            // create a list to store the invaders created
            List <Invader> retval = new List <Invader>();

            // if the invader level is past the maximum, then return an empty invader list
            if (SentAllInvaders)
            {
                return(retval);
            }

            // TODO: This could be something to adjust based on the difficulty
            // decide how many invaders we're going to send
            int invaderCount = 10;

            // create a class that holds the default weights of everything
            InvaderLevelUpInfo info = new InvaderLevelUpInfo(pieces, mMadeItCount, mDestroyedCount);

            // now then, create a loop to generate the invaders
            for (int i = 0; i < invaderCount; ++i)
            {
                // determine the boolean properties for the invader
                bool flying    = RandomGenerator.NextBool();
                bool leftRight = RandomGenerator.NextBool();

                // get the start/goal and key
                GridCell     start = leftRight ? horizontal.Start : vertical.Start;
                GridCell     goal  = leftRight ? horizontal.End : vertical.End;
                DijkstraType key   = leftRight ? DijkstraType.LeftToRight : DijkstraType.TopToBottom;

                // create a base invader. This invader will have a 50% chance of flying.
                Invader invader = new Invader(this, flying);

                // set the experience of the invader given the weights
                invader.LevelUp((InvaderLevel * 10), InvaderLevel, info);

                // send the invader to the map
                invader.BriefOnMission(start, goal, key);

                // next, add the invader to the list
                retval.Add(invader);
            }

            // increase the level
            ++InvaderLevel;

            // if we increased the level to the maximum invader level, then we've sent all of the invaders
            if (InvaderLevel > Invader.MaxInvaderLevel)
            {
                SentAllInvaders = true;
                InvaderLevel    = Invader.MaxInvaderLevel;
            }

            // return the invaders
            return(retval);
        }
Ejemplo n.º 6
0
 public void ReportBackIn(Invader invader)
 {
     // here, if the invader made it, then add it's experience to the experience pool
     if (invader.State == InvaderState.MadeIt)
     {
         ++mMadeItCount;
     }
     else if (invader.State == InvaderState.Dead)
     {
         ++mDestroyedCount;
     }
 }
Ejemplo n.º 7
0
        public static GsVector ComputeDirection(Piece piece, Invader invader)
        {
            float    speed    = piece.ProjectileSpeed;
            GsVector position = invader.Bounds.Center - piece.Bounds.Center;
            GsVector velocity = invader.Velocity * invader.VelocityFactor;

            float a = speed * speed - velocity.LengthSquared();
            float b = GsVector.Dot(position, velocity);
            float c = position.LengthSquared();

            // Cope with rare special case where bullet and target have same speed, to avoid dividing by zero
            if (a == 0)
            {
                if (b < 0)
                {
                    // Meet halfway...
                    float time = -0.5f * c / b;
                    return((position + velocity * time) / (speed * time));
                }
                else
                {
                    // Can't hit target
                }
            }
            else
            {
                float bSqPlusAC = b * b + a * c;
                // Can't take square root of negative number
                if (bSqPlusAC >= 0)
                {
                    float solution = (b + (float)Math.Sqrt(bSqPlusAC)) / a;
                    if (solution >= 0)
                    {
                        float time = solution;
                        return((position + velocity * time) / (speed * time));
                    }
                    else
                    {
                        // Can't hit target
                    }
                }
                else
                {
                    // Can't hit target
                }
            }

            return(GsVector.Zero);
        }
Ejemplo n.º 8
0
        private void ClearSelection()
        {
            // if there is a selection
            if (mSelectedPiece != null)
            {
                // clear it away
                mSelectedPiece.Selected = false;
            }

            // set the selected piece to null
            mSelectedPiece = null;

            // set the selected entity to null
            mSelectedInvader = null;
        }
Ejemplo n.º 9
0
        public void ChooseTarget(IEnumerable <Invader> invaders, GsVector offset)
        {
            Invader     bestInvader    = null;
            float       bestFuzzyValue = 0.0f;
            float       radiusSquared  = Radius * Radius;
            GsRectangle pieceBounds    = GsRectangle.Offset(Bounds, offset);

            foreach (Invader invader in invaders)
            {
                // calculate the position. In the future, we should get this directly from the invader
                GsVector position = invader.Position + offset + (invader.Size.ToVector() * .5f);
                GsVector origin   = invader.Origin;
                GsSize   size     = invader.Size;

                // calculate the bounds
                GsRectangle invaderBounds = new GsRectangle(position - (origin * .5f), size);

                // determine the distance
                float distance = GsVector.DistanceSquared(pieceBounds.Center, invaderBounds.Center);
                if (InvalidTarget(distance, radiusSquared, invader, invaderBounds))
                {
                    continue;
                }

                // determine the fuzzy number
                float fuzzy = 0.0f;
                fuzzy += CalculateFuzzyLife(invader) * FuzzyLifeWeight;
                fuzzy += CalculateFuzzyTime(invader) * FuzzyTimeWeight;
                fuzzy += CalculateFuzzyDistance(distance) * FuzzyDistanceWeight;

                // if the fuzzy value is better, then select this invader
                if (fuzzy > bestFuzzyValue)
                {
                    bestInvader    = invader;
                    bestFuzzyValue = fuzzy;
                }
            }

            if (bestInvader == null)
            {
                Target = null;
            }
            else if (bestInvader != Target)
            {
                mTimeChasingTarget = TimeSpan.Zero;
                Target             = bestInvader;
            }
        }
Ejemplo n.º 10
0
        public static void CollectSpoils(Invader invader)
        {
            // update the cash and the invaders
            Cash       += invader.Value;
            Experience += invader.Experience;

            // if we have enough experience and we aren't at the maximum level
            if (Experience >= ExperienceNeeded && Level < MaximumLevel)
            {
                // level up
                ++Level;

                // set the experience needed
                ExperienceNeeded = Level * Level * ExpFactor;
            }
        }
Ejemplo n.º 11
0
        protected bool InvalidTarget(float distance, float radiusSquared, Invader target, GsRectangle targetBounds)
        {
            // if the target isn't our specialty
            bool notOurSpecialty = !InvaderMatchesSpecialty(target);

            if (notOurSpecialty)
            {
                return(true);
            }

            // if we can't see the target, then return true
            bool cantSeeTarget = distance > radiusSquared;

            if (cantSeeTarget)
            {
                return(true);
            }

            // if the target reached the end, then return true
            bool targetReachedDestination = (target.TargetCell == null);

            if (targetReachedDestination)
            {
                return(true);
            }

            // if the target cell hasn't even started yet
            bool targetNotStartedYet = !GridBounds.Contains(targetBounds);

            if (targetNotStartedYet)
            {
                return(true);
            }

            // if the target is moving too fast, then return true
            bool targetMovingTooFast = Calculator.ComputeDirection(this, target) == GsVector.Zero;

            if (targetMovingTooFast)
            {
                return(true);
            }

            // return false if we get here
            return(false);
        }
Ejemplo n.º 12
0
        private bool AssertCanGetThrough(Piece piece)
        {
            // if the piece isn't blocking, then we can get through
            if (!piece.IsBlocking)
            {
                return(true);
            }

            // check the horizontal path
            ResetAStarGrid();
            AStarNode startHorz = AStarGrid[HorzStartCell.Column, HorzStartCell.Row];
            AStarNode endHorz   = AStarGrid[HorzGoalCell.Column, HorzGoalCell.Row];

            // if there is a path from the start to the end
            if (!AStar.PathExists(AStarGrid, AStarMovement.Orthogonal, startHorz, endHorz))
            {
                return(false);
            }

            // check the vertical path
            ResetAStarGrid();
            AStarNode startVert = AStarGrid[VertStartCell.Column, VertStartCell.Row];
            AStarNode endVert   = AStarGrid[VertGoalCell.Column, VertGoalCell.Row];

            // if there is a path from the start to the end
            if (!AStar.PathExists(AStarGrid, AStarMovement.Orthogonal, startVert, endVert))
            {
                return(false);
            }

            // solve the current grid
            SolveGrid();

            // if each of the entities can get through
            bool canGetThrough = true;

            for (int i = 0; canGetThrough && i < mInvaders.Count; ++i)
            {
                Invader invader = mInvaders[i];
                canGetThrough &= invader.CanPlacePiece(piece);
            }

            // return if we can get through
            return(canGetThrough);
        }
Ejemplo n.º 13
0
        private void UpdateDescriptionText(UpdateParams uparams)
        {
            Piece   piece   = RetrieveCurrentInformationPiece(uparams);
            Invader invader = RetrieveCurrentSelectedInvader(uparams);

            txtDescription.Visible = false;
            btnSell.Visible        = (piece != null & mSelectedPiece != null && mSelectedPiece.Equals(piece));
            btnUpgrade.Visible     = btnSell.Visible;
            btnUpgrade.Enabled     = btnSell.Visible && piece.CanUpgrade;

            if (piece != null || invader != null)
            {
                txtDescription.Visible = true;
                ITextDisplay provider = (piece != null ? (ITextDisplay)piece : (ITextDisplay)invader);
                DisplayDescriptionForTextProvider(provider);
                DisplayCaptionForTextProvider(provider);
            }
        }
Ejemplo n.º 14
0
        private float CalculateFuzzyTime(Invader invader)
        {
            // to calcuate the fuzzy time value, first we figure out how long we've
            // been chasing this mouse. For most mice, this will of course be no
            // time at all.
            TimeSpan time = TimeSpan.Zero;

            if (invader == Target)
            {
                time = mTimeChasingTarget;
            }

            // next we calculate fuzzyTime, which is a value ranging from 0 to 1. It
            // will increase as the amount of time we have spent chasing this mouse
            // increases. The value must be clamped to enforce the 0 to 1 rule.
            float fuzzyTime = (float)((time - MinTime).TotalSeconds / (MaxTime - MinTime).TotalSeconds);

            return(GsMath.Clamp(fuzzyTime, 0, 1));
        }
Ejemplo n.º 15
0
 public override void OnCollidedWithInvader(Invader invader)
 {
     // we don't care!
 }
Ejemplo n.º 16
0
 public virtual void OnCollidedWithInvader(Invader invader)
 {
     IsAlive = StayAlive || false;
 }
Ejemplo n.º 17
0
 public virtual bool CanHit(Invader invader)
 {
     return((Parent != null) && (Parent.InvaderMatchesSpecialty(invader)));
 }
Ejemplo n.º 18
0
 public bool InvaderMatchesSpecialty(Invader target)
 {
     return((Specialty == PieceSpecialty.Both) ||
            (target.Flying && Specialty == PieceSpecialty.Flight) ||
            (!target.Flying && Specialty == PieceSpecialty.Ground));
 }
Ejemplo n.º 19
0
 public static void InvaderGotThrough(Invader invader)
 {
     --Civilians;
 }
Ejemplo n.º 20
0
 private float CalculateFuzzyLife(Invader invader)
 {
     return(1f - (invader.CurrentLife / invader.MaximumLife));
 }