public Base(World world, Crossroad lc) { this.world = world; this.area = lc.Area; this.currentHealth = 20; this.maxHealth = 20; last = lc; }
public Wave(KDefGame game, Crossroad spawn, UnitType unitType, int numUnits, int tileSize, float interval) { // set thisWave this.game = game; this.spawningPoint = spawn; this.unitType = unitType; this.tile = tileSize; this.unitsToCreate = numUnits; this.interval = interval; CalculateMaxOffsets (); }
public StrongUnit(KDefGame game, Crossroad spawn, float speed, int level, int goldValue, int maxHP, Vector2 offset, Vector2 movement, Texture2D textureLiving, Texture2D textureDead) : base(game, spawn, speed, level, goldValue, maxHP, offset, movement, textureLiving, textureDead) { this.defaultSpeed *= speedModifier; this.currentSpeed *= speedModifier; this.maxHealth = (int) (maxHealth * healthModifier); this.currentHealth = (int) (maxHealth * currentHealth); this.tint = Color.CornflowerBlue; this.physicalResistModifier = 0.7f; this.magicalResistModifier = 1.5f; }
public Unit(KDefGame game, Crossroad spawn, float speed, int level, int goldValue, int maxHP, Vector2 offset, Vector2 movement, Texture2D textureLiving, Texture2D textureDead) { this.game = game; this.defaultSpeed = speed; this.currentSpeed = speed; this.level = level; this.goldValue = goldValue; this.maxHealth = maxHP; this.currentHealth = maxHP; this.textureLiving = textureLiving; this.textureDead = textureDead; this.tint = Color.White; this.destination = spawn; this.position = spawn.Position + offset; this.offset = offset; this.movement = movement; this.scale = 0.2f; }
private Random rand = new Random(); // for returning a random connected crossroad #endregion Fields #region Constructors public Crossroad(Rectangle area, Crossroad[] connections, int tier) { this.area = area; this.connections = connections; this.level = tier; }
// TODO Refactoing this crappy method // Changes the unit's movement depending on its destination protected virtual void ChangeMovement() { destination = destination.RandomConnection; // Don't move if there is no destination if (destination == null) { movement = new Vector2 (0, 0); } else { movement = destination.Position + offset - position; // get total displacement toMove = movement.Length(); // store the length of the displacement movement.Normalize(); // normalize the movement into a unit vector level = destination.level; // set the unit's tier to its destination's tier } }
// Loads the crossroads and then starts building paths private void SetupMap() { int mapWidth = 2048; int mapHeight = 2048; #region Siege Final Version Map Crossroad x1 = new Crossroad(new Rectangle(0, 0, tile, tile), null, 1); Crossroad x2 = new Crossroad(new Rectangle(200, 200, tile, tile), null, 1); Crossroad x3 = new Crossroad(new Rectangle(520, 280, tile, tile), null, 1); Crossroad x4 = new Crossroad(new Rectangle(540,80, tile, tile), null, 1); Crossroad x5 = new Crossroad(new Rectangle(960, 40, tile, tile), null, 1); Crossroad x6 = new Crossroad(new Rectangle(1000, 200, tile, tile), null, 1); Crossroad x7 = new Crossroad(new Rectangle(900, 300, tile, tile), null, 1); Crossroad x8 = new Crossroad(new Rectangle(860, 520, tile, tile), null, 1); Crossroad x9 = new Crossroad(new Rectangle(1200, 620, tile, tile), null, 1); Crossroad x10 = new Crossroad(new Rectangle(1400, 500, tile, tile), null, 1); Crossroad x11 = new Crossroad(new Rectangle(1720, 320, tile, tile), null, 1); Crossroad x12 = new Crossroad(new Rectangle(1880, 380, tile, tile), null, 1); Crossroad x13 = new Crossroad(new Rectangle(1940, 520, tile, tile), null, 1); Crossroad x14 = new Crossroad(new Rectangle(1780, 600, tile, tile), null, 1); Crossroad x15 = new Crossroad(new Rectangle(1580, 780, tile, tile), null, 1); Crossroad x16 = new Crossroad(new Rectangle(1800, 880, tile, tile), null, 1); Crossroad x17 = new Crossroad(new Rectangle(1880, 1160, tile, tile), null, 1); Crossroad x18 = new Crossroad(new Rectangle(1600, 1200, tile, tile), null, 1); Crossroad x19 = new Crossroad(new Rectangle(1740, 1420, tile, tile), null, 1); Crossroad x20 = new Crossroad(new Rectangle(1460, 1560, tile, tile), null, 1); Crossroad x21 = new Crossroad(new Rectangle(1180, 1560, tile, tile), null, 1); Crossroad x22 = new Crossroad(new Rectangle(1000, 1800, tile, tile), null, 1); Crossroad x23 = new Crossroad(new Rectangle(720, 1940, tile, tile), null, 1); Crossroad x24 = new Crossroad(new Rectangle(600, 1760, tile, tile), null, 1); Crossroad x25 = new Crossroad(new Rectangle(720, 1580, tile, tile), null, 1); Crossroad x26 = new Crossroad(new Rectangle(600, 1400, tile, tile), null, 1); Crossroad x27 = new Crossroad(new Rectangle(420, 1560, tile, tile), null, 1); Crossroad x28 = new Crossroad(new Rectangle(340, 1660, tile, tile), null, 1); Crossroad x29 = new Crossroad(new Rectangle(80, 1560, tile, tile), null, 1); Crossroad x30 = new Crossroad(new Rectangle(200, 1280, tile, tile), null, 1); Crossroad x31 = new Crossroad(new Rectangle(400, 1220, tile, tile), null, 1); Crossroad x32 = new Crossroad(new Rectangle(360, 840, tile, tile), null, 1); Crossroad x33 = new Crossroad(new Rectangle(60, 780, tile, tile), null, 1); Crossroad x34 = new Crossroad(new Rectangle(280, 460, tile, tile), null, 1); Crossroad end = new Crossroad(new Rectangle(1800, 1800, tile, tile), null, 1); crossroads = new Crossroad[] { x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16,x17,x18,x19,x20 ,x21,x22,x23,x24,x25,x26,x27,x28,x29,x30,x31,x32,x33,x34,end}; spawns = new Crossroad[] { x1 }; #endregion #region connections x1.connections = new Crossroad[] { x2 }; x2.connections = new Crossroad[] { x3, x34 }; x3.connections = new Crossroad[] { x4 }; x4.connections = new Crossroad[] { x5 }; x5.connections = new Crossroad[] { x6 }; x6.connections = new Crossroad[] { x7 }; x7.connections = new Crossroad[] { x8 }; x8.connections = new Crossroad[] { x9 }; x9.connections = new Crossroad[] { x10 }; x10.connections = new Crossroad[] { x11 }; x11.connections = new Crossroad[] { x12 }; x12.connections = new Crossroad[] { x13 }; x13.connections = new Crossroad[] { x14 }; x14.connections = new Crossroad[] { x15 }; x15.connections = new Crossroad[] { x16 }; x16.connections = new Crossroad[] { x17 }; x17.connections = new Crossroad[] { x18 }; x18.connections = new Crossroad[] { x19 }; x19.connections = new Crossroad[] { x20 }; x20.connections = new Crossroad[] { end }; x34.connections = new Crossroad[] { x33 }; x33.connections = new Crossroad[] { x32 }; x32.connections = new Crossroad[] { x31 }; x31.connections = new Crossroad[] { x30 }; x30.connections = new Crossroad[] { x29 }; x29.connections = new Crossroad[] { x28 }; x28.connections = new Crossroad[] { x27 }; x27.connections = new Crossroad[] { x26 }; x26.connections = new Crossroad[] { x25 }; x25.connections = new Crossroad[] { x24 }; x24.connections = new Crossroad[] { x23 }; x23.connections = new Crossroad[] { x22 }; x22.connections = new Crossroad[] { x21 }; x21.connections = new Crossroad[] { x20 }; foreach (Crossroad c in crossroads) { if (c.connections == null) c.connections = new Crossroad[] { }; } #endregion theBase = new Base(this, x1); mapRatio = new Vector2((float)ColorKey.Width / mapWidth, (float)ColorKey.Height / mapHeight); // Calculate and store paths paths = new Path[CalculateNumPaths(spawns) + CalculateNumPaths(crossroads)]; CalculatePaths(crossroads); // builds and stores all crossroad connections in paths CalculatePaths(spawns); // builds and stores all spawn connections in paths // Setup map bounds map = new Rectangle(0, 0, mapWidth, mapHeight); baseArea = new Rectangle(end.X - 30 + tile/2, end.Y - 30 + tile/2, 60, 60); }
// Calculate paths between crossroads and store them into the paths array (one time) private void CalculatePaths(Crossroad[] sourceXRS) { Vector2 origin; // a path rectangle's origin for drawing Vector2 target; // a path rectangle's ending point for drawing Vector2 difference; // difference between target and origin in x,y values Crossroad[] targetXRS; // array of connected crossroads coming from a source crossroad float angle; // angle between source and target int height; // height of the path's rectangle int distance; // the distance between source and target int quadrant; // the region of the coordinate system the path is in int filledPaths = 0; // the index of the paths array float lBound = 0, rBound = 0, tBound1 = 0, bBound1 = 0, vHeight = 0; // boundaries for the path // Gets the first unfilled spot in the paths array as the index for adding paths for (int i = 0; i < paths.Length; i++) { if (paths[i] == null) { filledPaths = i; // set the index to start adding paths to the first empty spot i = paths.Length; // stop searching } } // For every source crossroad... for (int i = 0; i < sourceXRS.Length; i++) { // Find all crossroads connected to this source crossroad targetXRS = sourceXRS[i].connections; // Calculate the path for each of these connected crossroads for (int j = 0; j < sourceXRS[i].connections.Length; j++) { origin = sourceXRS[i].Position; target = targetXRS[j].Position; difference = target - origin; // Calculate the angle if (target.X < origin.X) { angle = (float)(Math.Atan(difference.Y / difference.X) + Math.PI); } else { angle = (float)(Math.Atan(difference.Y / difference.X)); } // Height of the path rectangle adjusted by the angle height = (int)(tile + (tile / Math.Sin(Math.PI / 4) - tile) * Math.Abs(Math.Sin(angle * 2))); vHeight = 0; // Distance between target and origin distance = (int)Math.Sqrt(Math.Pow((double)(target.X - origin.X), 2) + Math.Pow((double)(target.Y - origin.Y), 2)); // Determine the quadrant the path's angle is in and shift the // path's origin to the corner of a tile that is appropriate if (difference.X > 0 && difference.Y < 0) { // quadrant 1 // origin is top-left corner of a tile (default) lBound = origin.X; rBound = target.X + tile; bBound1 = origin.Y + tile; tBound1 = target.Y; vHeight = (float)(height / Math.Sin(MathHelper.PiOver2 - angle)); quadrant = 1; } else if (difference.X < 0 && difference.Y < 0) { // quadrant 2 // origin is bottom left corner (shift down) origin.Y += tile; target.Y += tile; lBound = target.X; rBound = origin.X + tile; tBound1 = target.Y - tile; bBound1 = origin.Y; vHeight = (float)(height / Math.Sin(MathHelper.PiOver2 - angle)); quadrant = 2; } else if (difference.X < 0 && difference.Y > 0) { // quadrant 3 // origin is bottom right corner (shift right & down) origin.Y += tile; origin.X += tile; target.Y += tile; target.X += tile; lBound = target.X - tile; rBound = origin.X; tBound1 = origin.Y - tile; bBound1 = target.Y; vHeight = (float)(height / Math.Sin(angle)); quadrant = 3; } else if (difference.X > 0 && difference.Y > 0) { // quadrant 4 // origin is top right corner (shift to the right) origin.X += tile; target.X += tile; lBound = origin.X - tile; rBound = target.X; bBound1 = target.Y + tile; tBound1 = origin.Y; vHeight = (float)(height / Math.Sin(angle)); quadrant = 4; } else if (difference.X == 0 && difference.Y < 0) { // y-axis upper origin.Y += tile; target.Y += tile; lBound = origin.X; rBound = origin.X + tile; bBound1 = origin.Y; tBound1 = target.Y - tile; quadrant = 5; } else if (difference.X < 0 && difference.Y == 0) { // x-axis left origin.X += tile; origin.Y += tile; target.X += tile; target.Y += tile; lBound = target.X - tile; rBound = origin.X; bBound1 = origin.Y; tBound1 = target.Y - tile; quadrant = 6; } else if (difference.X == 0 && difference.Y > 0) { // y-axis lower origin.X += tile; target.X += tile; lBound = origin.X - tile; rBound = origin.X; bBound1 = target.Y + tile; tBound1 = origin.Y; quadrant = 7; } else { // x-axis right lBound = origin.X; rBound = target.X + tile; bBound1 = origin.Y + tile; tBound1 = origin.Y; quadrant = 8; } // Finally, add this path to the paths array paths[filledPaths++] = new Path((int)origin.X, (int)origin.Y, distance, height, angle, quadrant, lBound, rBound, bBound1, tBound1, vHeight, tile); } } }
// Finds the number of paths for a set of crossroads private int CalculateNumPaths(Crossroad[] anyXRS) { int paths = 0; for (int i = 0; i < anyXRS.Length; i++) { for (int j = 0; j < anyXRS[i].connections.Length; j++) { paths++; } } return paths; }