public Unit(KDefenseTower game, Crossroad spawn, Vector2 offset, Vector2 movement, float speed, int tier, Texture2D textureLiving, Texture2D textureDead, int value, int maxHP) { destination = spawn; // sets the unit's first destination position = spawn.Position + offset; // set the unit's first position scale = 0.2f; this.game = game; this.offset = offset; this.movement = movement; this.speed = speed; dSpeed = speed; this.tier = tier; this.textureLiving = textureLiving; this.textureDead = textureDead; this.value = value; currentTexture = textureLiving; tint = Color.White; health = maxHP; maxHealth = maxHP; armor = 0; }
// Old constructor using a vector public Crossroad(Vector2 crossroadPosition, int width, int height, Crossroad[] connections, int tier) { area.X = (int)crossroadPosition.X; area.Y = (int)crossroadPosition.Y; area.Width = width; area.Height = height; this.connections = connections; this.tier = tier; }
public FastUnit(KDefenseTower game, Crossroad spawn, Vector2 offset, Vector2 movement, float speed, int tier, Texture2D textureLiving, Texture2D textureDead, int value, int maxHP) : base(game, spawn, offset, movement, speed, tier, textureLiving, textureDead, value, maxHP) { tint = Color.Yellow; dSpeed *= 1.5f; this.speed = dSpeed; this.value = value; }
public RegenUnit(KDefenseTower game, Crossroad spawn, Vector2 offset, Vector2 movement, float speed, int tier, Texture2D textureLiving, Texture2D textureDead, int value, int maxHP) : base(game, spawn, offset, movement, speed, tier, textureLiving, textureDead, value, maxHP) { tint = Color.Green; regenAmount = (int)(maxHealth * 0.1f); this.speed = dSpeed; this.value = value; }
protected float shieldTimer = 0; // shield timer #endregion Fields #region Constructors public StrongUnit(KDefenseTower game, Crossroad spawn, Vector2 offset, Vector2 movement, float speed, int tier, Texture2D textureLiving, Texture2D textureDead, int value, int maxHP) : base(game, spawn, offset, movement, speed, tier, textureLiving, textureDead, value, maxHP) { tint = Color.CornflowerBlue; DSpeed *= 0.7f; this.speed = DSpeed; this.value = value; maxHealth *= 2; health = maxHealth; }
int unitType; // type of unit to create #endregion Fields #region Constructors public Wave(KDefenseTower game, int unitType, Crossroad spawn, int numUnits, int tileSize, float interval) { thisWave = game.status.Wave; spawningPoint = spawn; this.game = game; this.tile = tileSize; this.unitType = unitType; toCreate = numUnits; this.interval = interval; CalculateMaxOffsets(); }
public AggressiveUnit(KDefenseTower game, Crossroad spawn, Vector2 offset, Vector2 movement, float speed, int tier, Texture2D textureLiving, Texture2D textureDead, int value, int maxHP, World world,int damage, int range, float attackRate) : base(game, spawn, offset, movement, speed, tier, textureLiving, textureDead, value, maxHP) { this.tint = Color.Yellow; this.dSpeed *= 1.5f; this.speed = dSpeed; this.value = 2; this.world = world; this.damage = damage; this.range = range; this.attackRate = attackRate; this.attackTimer = 0.0f; }
public FlyingUnit(KDefenseTower game, Crossroad spawn, Vector2 offset, Vector2 movement, float speed, int tier, Texture2D textureLiving, Texture2D textureDead, int value, int maxHP, World world) : base(game, spawn, offset, movement, speed, tier, textureLiving, textureDead, value, maxHP) { tint = Color.Yellow; dSpeed *= 1.5f; this.speed = dSpeed; this.value = 2; destination = world.Base.Crossroad; movement = new Vector2(0, 0); Vector2 toMove; float divisor; toMove = destination.Position - position; divisor = Math.Max(Math.Abs(toMove.X), Math.Abs(toMove.Y)); movement = (toMove / divisor); // adjust movement vector tier = destination.tier; // set the unit's tier to its destination's tier }
public Base(World world, Crossroad lc) { this.area = lc.Area; this.world = world; this.last = lc; }
private Random rand = new Random(); // for returning a random connected crossroad #endregion Fields #region Constructors // New constructor public Crossroad(Rectangle area, Crossroad[] connections, int tier) { this.area = area; this.connections = connections; this.tier = tier; }
// Changes the unit's movement depending on its destination protected virtual void ChangeMovement() { // ask the current destination where to go next, and make it the new destination destination = destination.RandomConnection; // Stop moving if the unit doesn't have a new destination (the new one is null) if (destination == null) movement = new Vector2(0, 0); else { // Otherwise, adjust unit's movement 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 tier = destination.tier; // 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 }; #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 #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); filledPaths++; } } }
// 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; }