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); }
private void AddChunksVert(GsVector offset, GsVector mousePt, int cellsNeeded, bool above) { // create a list to hold the chunk pieces List <GridCell> chunkPieces = new List <GridCell>(); // get the starting chunk GridCellChunk startingChunk = mSelection.StartingChunk; // preprocess some values float startY = startingChunk.Y + (above ? -CellHeight : startingChunk.Height); float startX = startingChunk.X; float addY = CellHeight * (above ? -1 : 1); float addX = CellWidth; float endY = mousePt.Y + (cellsNeeded == 1 ? 0 : addY); float endX = startingChunk.Bounds.Right; int comparison = (above ? 1 : -1); // move to the end y for (float y = startY; y.CompareTo(endY).Equals(comparison); y += addY) { for (float x = startX; x < endX; x += addX) { UpdateChunkPieces(ref chunkPieces, cellsNeeded, offset, x, y); } } }
/// <summary> /// Briefs the invader on it's mission. It should go from the start cell and try to /// reach the goal cell. /// </summary> /// <param name="start">The cell to start on.</param> /// <param name="goal">The goal to get to.</param> /// <param name="key">The key to assign to this invader to aid in path finding.</param> public void BriefOnMission(GridCell start, GridCell goal, DijkstraType key) { TargetCell = start; GoalCell = goal; DijkstraKey = key; float dx = (DijkstraKey == DijkstraType.LeftToRight ? TargetCell.Width * 2f : 0); float dy = (DijkstraKey == DijkstraType.TopToBottom ? TargetCell.Height * 2f : 0); float mux = RandomGenerator.NextSingle(); float muy = RandomGenerator.NextSingle(); X = TargetCell.X - (dx * GsMath.SmoothStep(1, 5, mux)); Y = TargetCell.Y - (dy * GsMath.SmoothStep(1, 5, muy)); Width = TargetCell.Width * (Flying ? 1.5f : 1f); Height = TargetCell.Height * (Flying ? 1.5f : 1f); Velocity = new GsVector(mAttributes[InvaderAttributes.Speed]); AdjustOrientation(); var texture = GetImage(); var size = ImageProvider.GetSize(texture); Origin = new GsVector(size.Width / 2f, size.Height / 2f); CurrentLife = MaximumLife; TargetCell = Flying ? goal : start; }
private void AddChunksHorz(GsVector offset, GsVector mousePt, int cellsNeeded, bool left) { // create a list to hold the chunk pieces List <GridCell> chunkPieces = new List <GridCell>(); // get the starting chunk GridCellChunk startingChunk = mSelection.StartingChunk; // preprocess some values float startY = startingChunk.Y; float startX = startingChunk.X + (left ? -CellWidth : startingChunk.Width); float addY = CellHeight; float addX = CellWidth * (left ? -1 : 1); float endY = startingChunk.Bounds.Bottom; float endX = mousePt.X + (cellsNeeded == 1 ? 0 : addX); int comparison = (left ? 1 : -1); // start at the bottom of the chunk and move down until we hit the cursor y for (float x = startX; x.CompareTo(endX).Equals(comparison); x += addX) { for (float y = startY; y < endY; y += addY) { UpdateChunkPieces(ref chunkPieces, cellsNeeded, offset, x, y); } } }
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); }
private void AdjustVelocityFactor() { // start out as one VelocityFactor = GsVector.One; // determine the factor GsVector factor = GsVector.One; // if we have a target, then adjust the sign of the components if (TargetCell != null) { float sX = Math.Sign(TargetCell.X - X); float sY = Math.Sign(TargetCell.Y - Y); factor.X *= sX; factor.Y *= sY; } if (CurrentCell != null) { // here, check to see if the cell has any special properties that we should know about if ((CurrentCell.Piece is SpeedBumpPiece)) { factor *= .1f; CurrentLife -= (CurrentCell.Piece as SpeedBumpPiece).Attack; } } // adjust the velocity factor VelocityFactor *= factor; }
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()); }
public static GsVector ComputeProjectileDirection(float angle) { GsVector v = new GsVector(1, 0); GsMatrix rotMatrix = GsMatrix.CreateRotationZ(angle); return(GsVector.Transform(v, rotMatrix)); }
public static GsVector[] CreateConvexHull(this Texture2D texture) { Color[] colorData = new Color[texture.Width * texture.Height]; texture.GetData <Color>(colorData); List <GsVector> pixels = new List <GsVector>(colorData.Length); int x, y; for (x = 0; x < texture.Width; ++x) { for (y = 0; y < texture.Height; ++y) { Color color = colorData[x + (y * texture.Width)]; if (color.A > 250) { pixels.Add(new GsVector(x, y)); } } } GsVector[] polygon = pixels.ToArray(); GsVector[] H = new GsVector[polygon.Length]; ChainConvexHull.ComputeHull(polygon, polygon.Length, ref H); return(H); }
public static float ComputeOrientation(float orientation, GsVector direction, float turnSpeed) { float desiredAngle = (float)Math.Atan2(direction.Y, direction.X); float difference = GsMath.WrapAngle(desiredAngle - orientation); return(GsMath.WrapAngle(orientation + difference)); }
public ImageParams(GsImage image, GsSize imageSize, GsVector position, GsVector origin, GsVector scale) { Image = image; ImageSize = imageSize; Position = position; Origin = origin; Scale = scale; }
protected virtual void DrawWeaponTower(DrawParams dparams, GsVector offset) { ImageParams data = GetTextureDrawData(offset); GsColor color = GsColor.Gray; var graphics = dparams.Graphics; graphics.DrawImage(data, color, Orientation); }
protected override ImageParams GetTextureDrawData(GsVector offset) { var image = GetImage(); var imgSize = ImageProvider.GetSize(image); var scale = Calculator.ComputeScale(imgSize, Size); return(new ImageParams(image, imgSize, Position + offset, Origin, scale)); }
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 }); }
/// <summary> /// /// </summary> public Sprite() { Orientation = 0f; Bounds = GsRectangle.Empty; previousBounds = Bounds; hullCache = null; VelocityFactor = GsVector.One; Origin = GsVector.Zero; }
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); }
public FlameProjectile(Piece parent, double timeToLiveInSeconds) : base(parent, timeToLiveInSeconds) { Size = new GsSize(20f, 80f); ImageKey = "flame"; var size = ImageProvider.GetSize(GetImage()); Origin = new GsVector(0, size.Height / 2); }
/// <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); }
protected override ImageParams GetTextureDrawData(GsVector offset) { var outin = GetOutsideInsideBounds(offset); var bounds = outin.Outside; var inside = outin.Inside; ImageParams data = base.GetTextureDrawData(offset); GsVector scale = Calculator.ComputeScale(data.ImageSize, bounds.Size); return(new ImageParams(data.Image, data.ImageSize, bounds.Location, GsVector.Zero, scale)); }
protected override ImageParams GetTextureDrawData(GsVector offset) { // get the draw data of the base var data = base.GetTextureDrawData(offset); // get the correct position GsVector position = Position + offset + (Size.ToVector() * .5f); // return the updated draw data return(new ImageParams(data.Image, data.ImageSize, position, data.Origin, data.Scale)); }
public Projectile(Piece parent, double timeToLiveInSeconds) { IsAlive = true; mTimeToLive = timeToLiveInSeconds; ImageKey = "bullet"; var sz = ImageProvider.GetSize(GetImage()); Origin = new GsVector(0, sz.Height / 2f); Color = GsColor.White; Size = new GsSize(20f, 6.5f); StayAlive = false; Parent = parent; }
/// <summary></summary> protected virtual GsVector GetCenter(GsVector offset) { // get the drawing data ImageParams data = GetTextureDrawData(offset); // get the center of the image GsVector center = data.ImageSize.ToVector() / 2f; // compute the transform GsMatrix transform = CreateTransform(data); // return the center transformated return(GsVector.Transform(center, transform)); }
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); }
private void InitializeProperties() { Width = (int)((double)guiSurface.Height * .85); Height = Width; NumCols = (Width / CellWidth) - 1; NumRows = (Height / CellHeight) - 1; MiddleOffset = new GsVector( (Width / 2f) - ((NumCols * CellWidth) / 2f), (Height / 2f) - ((NumRows * CellHeight) / 2f)); X = 0; Y = (guiSurface.Height / 2f) - (Height / 2f); }
protected override ImageParams GetTextureDrawData(GsVector offset) { var projectile = GetImage(); var projectileSize = ImageProvider.GetSize(projectile); var origin = projectileSize.ToVector() * .5f; var scale = Calculator.ComputeScale(projectileSize, Size); var position = mOwnerBounds.Location; position += ((mOwnerBounds.Size.ToVector() * .5f)); // return the data return(new ImageParams(projectile, projectileSize, position + offset, origin, scale)); }
/// <summary></summary> public virtual GsRectangle GetBoundingBox(GsVector offset) { // get the center of the projectile GsVector center = GetCenter(offset); // create a rough box that has the projectile inside of it float dW = Width * .5f; float dH = Height * .5f; return(new GsRectangle( center.X - dW, center.Y - dH, dW * 2f, dH * 2f)); }
protected override ImageParams GetTextureDrawData(GsVector offset) { var outin = GetOutsideInsideBounds(offset); var bounds = outin.Outside; var wtower = GetImage(); var imgSize = ImageProvider.GetSize(wtower); var actSize = new GsSize(bounds.Width, bounds.Height); GsVector scale = Calculator.ComputeScale(imgSize, actSize); GsVector origin = imgSize.ToVector() * .5f; GsVector center = actSize.ToVector() * .5f; return(new ImageParams(wtower, imgSize, bounds.Location + center, origin, scale)); }
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; } }
public void Update(GameTime gameTime) { LastKeyboardState = CurrentKeyboardState; LastMouseState = CurrentMouseState; CurrentKeyboardState = keyboard.GetState(); CurrentMouseState = mouse.GetState(); var device = Game.GraphicsDevice; var pt = new GsVector(CurrentMouseState.X, CurrentMouseState.Y); pt *= new GsVector(device.Viewport.Width, device.Viewport.Height); CursorPosition = pt; SelectPressed = (CurrentMouseState.LeftButton.Down); }
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); }