Пример #1
0
        protected virtual void DrawBackground(DrawParams dparams, GsRectangle bounds, GsRectangle inside)
        {
            GsColor bgColor = (Level == MaxLevel ? GsMath.SmoothStep(GsColor.Beige, GsColor.SkyBlue, .5f) : GsColor.Beige);
            GsColor color   = Selected ? GsColor.DarkGreen : bgColor;

            dparams.Graphics.FillRectangle(color, bounds);
        }
Пример #2
0
        private void UpdateChunkPieces(ref List <GridCell> chunkPieces, int cellsNeeded, GsVector offset, float x, float y)
        {
            int r = (int)((y - offset.Y) / CellHeight);
            int c = (int)((x - offset.X) / CellWidth);

            if ((-1 < r && r < NumRows) && (-1 < c && c < NumCols))
            {
                // add this cell to the current chunk
                chunkPieces.Add(Grid[c, r]);
            }

            // if we have the correct number of chunks
            if (chunkPieces.Count == (cellsNeeded * cellsNeeded))
            {
                // compute the bounds of the cell pieces
                GsRectangle bounds = ComputeBounds(chunkPieces, CellWidth, CellHeight);

                // for now, we only care if the chunk is available (in terms of it being valid)
                GridCellChunk item = new GridCellChunk
                {
                    Bounds = bounds,
                    Cells  = chunkPieces.ToArray(),
                    Valid  = chunkPieces.TrueForAll(GridCell.CellIsAvailable),
                };
                mSelection.AddChunk(item);
                chunkPieces.Clear();
            }
        }
Пример #3
0
        public static void DrawImage(this IGsGraphics graphics, ImageParams data, GsColor color, GsVector offset, float rotation, GsImageFlip flip)
        {
            var size = data.ImageSize.ToVector() * data.Scale;
            var dest = new GsRectangle(data.Position + offset, size.ToSize());

            graphics.DrawImage(data.Image, dest, null, data.Origin, rotation, flip, color);
        }
Пример #4
0
        private void OnSelectClick(UpdateParams uparams)
        {
            // ignore this function if the buttons have the mouse
            if (btnSell.MouseCaptured || btnUpgrade.MouseCaptured)
            {
                return;
            }

            // always clear the current selection
            ClearSelection();

            // if the user has no selection, then select the piece
            if (lstPieces.SelectedIndex == -1)
            {
                // the piece is invalid. Find the piece that they clicked on.
                bool found = false;
                for (int i = 0; !found && i < mPieces.Count; ++i)
                {
                    Piece       piece = mPieces[i];
                    GsRectangle box   = new GsRectangle(piece.Position + uparams.Offset, piece.Size);
                    if (box.Contains(uparams.Input.CursorPosition))
                    {
                        // set selection
                        found = true;
                        SetSelection(piece);
                    }
                }
            }
        }
Пример #5
0
        public static GsRectangle CreateBoxAroundPoint(GsVector pt, GsVector offset, float width, float height)
        {
            GsRectangle box = new GsRectangle(pt.X - (width / 2), pt.Y - (height / 2), width, height);

            box.Offset(offset);
            return(box);
        }
Пример #6
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);
        }
Пример #7
0
        private static GridCell[] GetCellsWithCenterContainedIn(GridCell[,] grid, GsRectangle box, GsVector cellOffset)
        {
            int ColCount = grid.GetLength(0);
            int RowCount = grid.GetLength(1);

            List <GridCell> cells = new List <GridCell>(100);

            for (int c = 0; c < ColCount; ++c)
            {
                for (int r = 0; r < RowCount; ++r)
                {
                    GridCell cell   = grid[c, r];
                    GsVector center = cell.Bounds.Location + cellOffset;

                    center.X += (cell.Width / 2f);
                    center.Y += (cell.Height / 2f);

                    if (box.Contains(center))
                    {
                        cells.Add(cell);
                    }
                }
            }
            return(cells.ToArray());
        }
Пример #8
0
        public void Draw(DrawParams dparams)
        {
            if (State != InvaderState.Alive)
            {
                return;
            }

            var graphics = dparams.Graphics;
            var offset   = dparams.Offset;
            var data     = GetTextureDrawData(offset);

            //draw the entity itself
            graphics.DrawImage(data, Color, offset, Orientation);

            // compute the bounds of the life bar
            GsVector barPosition = data.Position - (Size.ToVector() * .5f);

            barPosition.Y -= 4f;
            GsRectangle bar = new GsRectangle(barPosition, new GsSize(Width, 3f));

            // draw the life bar
            float width = bar.Width * Calculator.CalculatePercent(CurrentLife, 0, MaximumLife);

            graphics.FillRectangle(GsColor.Green, bar.X, bar.Y, width, bar.Height);
            graphics.DrawRectangle(GsColor.Black, bar);

            // draw the level
            var      font = FontProvider.InvaderLevelFont;
            string   text = Level.ToString();
            GsVector pos  = new GsVector(bar.X, bar.Y - (GsTextMeasurer.MeasureString(font, text).Height + 5f));

            graphics.DrawString(font, text, pos, GsColor.Gold);
        }
Пример #9
0
        public static void DrawImage(this IGsGraphics graphics, ImageParams data, GsColor color, GsRectangle source)
        {
            var size = data.ImageSize.ToVector() * data.Scale;
            var dest = new GsRectangle(data.Position, size.ToSize());

            graphics.DrawImage(data.Image, dest, source, data.Origin, 0f, GsImageFlip.None, color);
        }
Пример #10
0
 public FlamewaveProjectile(Piece parent, GsRectangle ownerBounds, double timeToLiveInSeconds)
     : base(parent, timeToLiveInSeconds)
 {
     mOwnerBounds = ownerBounds;
     Color        = GsColor.White;
     ImageKey     = "flamewave";
     StayAlive    = true;
 }
Пример #11
0
        protected OutsideInside GetOutsideInsideBounds(GsVector offset)
        {
            GsRectangle bounds = new GsRectangle(X + offset.X, Y + offset.Y, Width, Height);
            GsRectangle inside = new GsRectangle(bounds.X + Pad, bounds.Y + Pad, bounds.Width - TwoPad, bounds.Height - TwoPad);

            return(new OutsideInside {
                Inside = inside, Outside = bounds
            });
        }
Пример #12
0
 /// <summary>
 ///
 /// </summary>
 public Sprite()
 {
     Orientation    = 0f;
     Bounds         = GsRectangle.Empty;
     previousBounds = Bounds;
     hullCache      = null;
     VelocityFactor = GsVector.One;
     Origin         = GsVector.Zero;
 }
Пример #13
0
        protected override Projectile CreateProjectile()
        {
            GsRectangle         bounds     = new GsRectangle(Position, Size);
            ShockwaveProjectile projectile = new ShockwaveProjectile(this, bounds, ProjectileLifeInSeconds);

            //projectile.Size = new SizeF(Width * .25f, Height * .25f);
            projectile.Size = new GsSize(Radius * 2f, Radius * 2f);
            return(projectile);
        }
Пример #14
0
        protected override void DrawWeaponTower(DrawParams dparams, GsVector offset)
        {
            ImageParams data   = GetTextureDrawData(offset);
            GsRectangle source = new GsRectangle(
                mIndex * FrameSize.Width, 0,
                FrameSize.Width, FrameSize.Height);
            var graphics = dparams.Graphics;

            graphics.DrawImage(data, Color, source);
        }
Пример #15
0
        protected virtual void DrawWeaponBase(DrawParams dparams, GsRectangle bounds, GsRectangle inside)
        {
            var wbase     = ImageProvider.GetFramedImage("towerBase").Image;
            var wbaseSize = ImageProvider.GetSize(wbase);
            var scale     = Calculator.ComputeScale(wbaseSize, bounds.Size);
            var color     = new GsColor(GsColor.Gray, 128);
            var graphics  = dparams.Graphics;

            graphics.DrawImage(wbase, color, bounds.Location, scale);
        }
Пример #16
0
 /// <summary></summary>
 public GsPolygon GetHull(GsVector offset)
 {
     GsVector[] polygon = GetImageHull();
     if (hullCache == null || Bounds != previousBounds)
     {
         GsMatrix transform = CreateTransform(GetTextureDrawData(offset));
         hullCache      = new GsPolygon(polygon, transform);
         previousBounds = Bounds;
     }
     return(hullCache);
 }
Пример #17
0
        protected override void DrawWeaponBase(DrawParams dparams, GsRectangle bounds, GsRectangle inside)
        {
            // draw a speed bump!
            var speedbump     = GetImage();
            var speedbumpSize = ImageProvider.GetSize(speedbump);

            var     scale    = Calculator.ComputeScale(speedbumpSize, bounds.Size);
            GsColor color    = GsColor.White;
            var     graphics = dparams.Graphics;

            graphics.DrawImage(speedbump, color, bounds.Location, scale);
        }
Пример #18
0
        private static GsRectangle ComputeBounds(IEnumerable <GridCell> cells, float cellWidth, float cellHeight)
        {
            // create a copy of the cells
            List <GridCell> lst = new List <GridCell>(cells);

            // sort the list by Y, then X. This will make the cells go left to right and then top to bottom.
            lst.Sort();

            // get the first and last locations
            GridCell tl = lst[0];
            GridCell br = lst[lst.Count - 1];

            // finally, return the box
            return(GsRectangle.FromLTRB(tl.X, tl.Y, br.X + cellWidth, br.Y + cellHeight));
        }
Пример #19
0
        private void UpdateSelectedPieceRadius(UpdateParams uparams)
        {
            Piece piece = RetrieveCurrentSelectedPiece(uparams);

            currentArea = GsRectangle.Empty;

            bool validPiece = (piece != null) && (piece.State == PieceState.Idle);

            if (validPiece && !piece.Size.IsEmpty)
            {
                float dx = piece.Width * .5f;
                float dy = piece.Height * .5f;
                currentArea = new GsRectangle(piece.X + dx, piece.Y + dy, piece.Radius, piece.Radius);
            }
        }
Пример #20
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;
            }
        }
Пример #21
0
        public void DrawImage(GsImage image, GsRectangle dest, GsRectangle?source, GsVector origin, float angle, GsImageFlip flip, GsColor tint)
        {
            SwitchMode(Mode.Sprites);
            Rectangle?src = null;

            if (source.HasValue)
            {
                var s = source.Value;
                src = new Rectangle((int)s.X, (int)s.Y, (int)s.Width, (int)s.Height);
            }

            var dst = new RectangleF(dest.X, dest.Y, dest.Width, dest.Height);

            spriteBatch.Draw(image.Data as Texture2D,
                             dst, src, tint.ToColor(), angle, new Vector2(origin.X, origin.Y), flip.ToSpriteEffects(), 0f);
        }
Пример #22
0
        protected void DrawProgressState(DrawParams dparams, GsRectangle bounds, GsRectangle inside)
        {
            float width  = inside.Width;
            float height = (float)Math.Round(inside.Height / 3f);

            float x = inside.X + ((inside.Width / 2f) - (width / 2f));
            float y = inside.Y + ((inside.Height / 2f) - (height / 2f));

            float progressWidth = width * Calculator.CalculatePercent(ProgressValue, 0, MaxProgress);
            float factor        = ProgressValue / (MaxProgress);

            GsColor barFill = GsMath.Lerp(GsColor.Red, GsColor.DarkGreen, factor);
            GsColor barBder = GsColor.Black;

            dparams.Graphics.FillRectangle(barFill, x, y, progressWidth, height);
            dparams.Graphics.DrawRectangle(barBder, x, y, width, height);
        }
Пример #23
0
        protected override Projectile CreateProjectile()
        {
            Projectile projectile = null;

            if (Level == MaxLevel)
            {
                GsRectangle bounds = new GsRectangle(Position, Size);
                projectile      = new FlamewaveProjectile(this, bounds, ProjectileLifeInSeconds);
                projectile.Size = new GsSize(Width, Height);
            }
            else
            {
                float extra = Radius / 3f;
                projectile      = new FlameProjectile(this, ProjectileLifeInSeconds);
                projectile.Size = new GsSize(Radius, projectile.Height + extra);
            }
            return(projectile);
        }
Пример #24
0
        protected void FireProjectile(TimeSpan elapsed)
        {
            if (Target != null)
            {
                GsRectangle inside  = new GsRectangle(X + Pad, Y + Pad, Width - TwoPad, Height - TwoPad);
                GsSize      actSize = new GsSize(Width - Pad, inside.Height);

                GsVector myCenter = actSize.ToVector() * .5f;
                for (int i = 0; i < NumberProjectilesToFire; ++i)
                {
                    Projectile projectile = CreateProjectile();
                    projectile.Velocity    = Velocity * ProjectileSpeed;
                    projectile.Position    = inside.Location + myCenter;
                    projectile.Orientation = Orientation;
                    projectile.Attack      = Attack;
                    projectile.UpdateByFrameCount(elapsed, i * NumberProjectilesToFire);
                    mQueuedProjectiles.Add(projectile);
                }
            }
        }
Пример #25
0
        protected virtual void DrawCurrentLevel(DrawParams dparams, GsRectangle bounds, GsRectangle inside)
        {
            if (Level < MaxLevel)
            {
                float spacing   = 2f;
                float dimension = (bounds.Width - (spacing * 5f)) / 8f;

                float x = bounds.X + spacing;
                float y = bounds.Bottom - (spacing + dimension);

                GsFont font        = FontProvider.PieceLevelFont;
                var    lineSpacing = FontProvider.GetLineSpacing(font);

                string   text = Level.ToString();
                GsVector pos  = new GsVector(bounds.Left + spacing, bounds.Bottom - (lineSpacing + spacing));

                var graphics = dparams.Graphics;
                graphics.DrawString(font, text, pos, GsColor.Black);
            }
        }
Пример #26
0
        private void DrawGrid(DrawParams dparams)
        {
            GsVector offset = dparams.Offset;

            for (int c = 0; c < NumCols; ++c)
            {
                for (int r = 0; r < NumRows; ++r)
                {
                    GridCell    cell   = Grid[c, r];
                    GsRectangle bounds = new GsRectangle(cell.X + offset.X, cell.Y + offset.Y, cell.Width, cell.Height);

                    if (cellsToFlash.ContainsKey(cell))
                    {
                        // draw the flashing cells
                        int     index = ((int)((SecondsToFlashCell - cellsToFlash[cell]) * FlashesPerSecond)) % MaxFlashes;
                        float   mu    = ((float)index) / ((float)MaxFlashes);
                        GsColor color = GsMath.SmoothStep(FlashStart, FlashEnd, mu);
                        color = new GsColor(color, 200);
                        dparams.Graphics.FillRectangle(color, bounds);
                    }

                    if (cell.IsOuter && !cell.IsThroughway)
                    {
                        switch (dparams.FillMode)
                        {
                        case GridFillMode.Solid:
                        {
                            dparams.Graphics.FillRectangle(GsColor.White, bounds);
                            break;
                        }

                        case GridFillMode.Polygons:
                        {
                            dparams.Graphics.DrawRectangle(GsColor.White, bounds);
                            break;
                        }
                        }
                    }
                }
            }
        }
Пример #27
0
        public void Draw(DrawParams dparams)
        {
            if (Chunk != null)
            {
                GsColor     color  = Chunk.Valid ? GsColor.Green : GsColor.Red;
                GsRectangle bounds = new GsRectangle(Chunk.Location + dparams.Offset, Chunk.Size);

                dparams.Graphics.FillRectangle(new GsColor(color, 100), bounds);
                dparams.Graphics.DrawRectangle(color, bounds);

                // draw the chunks being edited
                foreach (GridCellChunk item in chunksBeingEdited.Keys)
                {
                    GsColor     itemClr    = item.Valid ? GsColor.Green : GsColor.Red;
                    GsRectangle itemBounds = new GsRectangle(item.Location + dparams.Offset, item.Size);

                    dparams.Graphics.FillRectangle(new GsColor(itemClr, 100), itemBounds);
                    dparams.Graphics.DrawRectangle(itemClr, itemBounds);
                }
            }
        }
Пример #28
0
        private void UpdateProjectiles(UpdateParams uparams)
        {
            float dx = CellWidth * 2f;
            float dy = CellHeight * 2f;

            GsRectangle viewport = new GsRectangle(X - dx, Y - dy, Width + dx, Height + dy);

            for (int i = mProjectiles.Count - 1; i > -1; --i)
            {
                // get the projectile
                Projectile projectile = mProjectiles[i];

                // update the projectile data
                projectile.Update(uparams.Elapsed);

                // get the polygon data
                GsPolygon polygon = projectile.GetHull(uparams.Offset);

                // if the projectile is still alive, check to see if it went out of bounds
                if (projectile.IsAlive)
                {
                    bool projectileIntersectsWithBounds = viewport.IntersectsWith(projectile.Bounds);
                    bool projectileInsideBounds         = viewport.Contains(projectile.Bounds);
                    projectile.IsAlive = projectile.StayAlive || (projectileInsideBounds || projectileIntersectsWithBounds);
                }

                // if the projectile is still alive, check to see if it hit anything
                if (projectile.IsAlive)
                {
                    CheckCollisions(projectile, polygon, uparams);
                }

                // if the projectile is dead, then remove it from the list
                if (!projectile.IsAlive)
                {
                    mProjectiles.RemoveAt(i);
                    OnProjectileRemoved(projectile);
                }
            }
        }
Пример #29
0
        private void InitializeGrid()
        {
            int midCol = (NumCols - 1) / 2;
            int midRow = (NumRows - 1) / 2;

            int midColLeft  = midCol - HalfThroughWay;
            int midColRight = midCol + HalfThroughWay;

            int midRowUp   = midRow - HalfThroughWay;
            int midRowDown = midRow + HalfThroughWay;

            int dc = (NumCols - 1) % 2;
            int dr = (NumRows - 1) % 2;

            Grid = new GridCell[NumCols, NumRows];
            for (int c = 0; c < NumCols; ++c)
            {
                for (int r = 0; r < NumRows; ++r)
                {
                    bool isOuter      = (c == 0 || r == 0 || c == (NumCols - 1) || r == (NumRows - 1));
                    bool isThroughway = isOuter && (GsMath.InRange(c, midColLeft + dc, midColRight) || GsMath.InRange(r, midRowUp + dr, midRowDown));

                    GridCell cell = new GridCell(c, r, isOuter, isThroughway);
                    cell.Bounds = new GsRectangle(c * CellWidth, r * CellHeight, CellWidth, CellHeight);
                    Grid[c, r]  = cell;
                }
            }

            HorzGoalCell = Grid[NumCols - 1, midRow];
            VertGoalCell = Grid[midCol, NumRows - 1];

            HorzStartCell = Grid[0, midRow];
            VertStartCell = Grid[midCol, 0];

            gridBounds = GsRectangle.FromLTRB(Grid[0, 0].X, Grid[0, 0].Y, Grid[NumCols - 1, 0].X + CellWidth, Grid[0, NumRows - 1].Y + CellHeight);
            gridBounds.Offset(MiddleOffset + Position);
        }
Пример #30
0
        public Piece BuildFromChunk(GsRectangle gridBounds, GridCellChunk editedChunk)
        {
            GridCell[] cells = editedChunk.Cells.ToList().ToArray();
            Piece      piece = CreatePiece(cells);

            piece.GridBounds = gridBounds;

            for (int i = 0; i < cells.Length; ++i)
            {
                GridCell cell = cells[i];
                cell.SetPiece(piece);
            }

            piece.Cells  = cells;
            piece.Bounds = editedChunk.Bounds;
            piece.SavePriceInfo();

            if (!Player.PurchasePiece(piece))
            {
                throw new Exception("Somehow you placed this without enough money!");
            }

            return(piece);
        }