public bool anyUnitsInSpaceExcluding(Vector2 pos, Unit me) { foreach(Unit u in friendlyUnits) if((new Rectangle((int) pos.X, (int) pos.Y, 16, 16)).Intersects(new Rectangle((int) u.Position.X, (int) u.Position.Y, 16, 16)) && u != me) return true; foreach(Unit u in enemyUnits) if((new Rectangle((int) pos.X, (int) pos.Y, 16, 16)).Intersects(new Rectangle((int) u.Position.X, (int) u.Position.Y, 16, 16)) && u != me) return true; return false; }
public void createBullet(Unit u, float direction) { Bullet b = new Bullet(); b.position = u.Position + new Vector2(8, 8); b.speed = bulletSpeed * new Vector2((float) Math.Cos(direction), (float) Math.Sin(direction)); b.isEnemy = u.isEnemy; b.damage = u.damage; bullets.Add(b); game.shootEffect.Play(); }
public bool unitInRectangle(Unit u, Rectangle r) { Rectangle uRect = new Rectangle((int) u.Position.X, (int) u.Position.Y, 16, 16); return r.Intersects(uRect); }
public bool isSpaceEmptyForUnit(Vector2 pos, Unit me) { return !mapData[(int) pos.X, (int) pos.Y]/* && !anyUnitsInSpaceExcludingSelected(pos * 16)*/; }
public List<Unit> getEnemyUnitsInRange(Unit me) { List<Unit> found = new List<Unit>(); List<Unit> units = me.isEnemy ? friendlyUnits : enemyUnits; foreach(Unit u in units) if(Vector2.Distance(u.Position, me.Position) <= me.maxRange && u.isEnemy != me.isEnemy) found.Add(u); return found; }
public void createUnit(Vector2 pos, bool enemy) { Unit u = new Unit(game); u.isEnemy = enemy; u.SetPositionAndSnap(pos); u.frame = rand.Next(unitTex.Width / 16); if(enemy) enemyUnits.Add(u); else friendlyUnits.Add(u); }
public override void update() { if(Position.X < 0 || Position.X > game.playingField.mapData.GetLength(0) * 16 - 16 || Position.Y < 0 || Position.Y > game.playingField.mapData.GetLength(1) * 16 - 16) { game.playingField.deadUnits.Add(this); return; } if(!isEnemy) { if(path != null && path.Count > 0) { moving = true; if(Math.Abs(Math.Atan2(directionToNextPoint().Y, directionToNextPoint().X) % 90) < 5) NextDir = directionToNextPoint(); if(NextDir == Vector2.Zero) { path.RemoveAt(0); if(path != null && path.Count > 0) NextDir = directionToNextPoint(); } } else moving = false; } else { if(wanderTimer > 0) { moving = false; wanderTimer--; } else { wanderTimer = wanderTime + game.playingField.rand.Next(20); moving = true; while(NextDir == Dir) NextDir = new Vector2(game.playingField.rand.Next(-1, 2), game.playingField.rand.Next(-1, 2)); } } UpdateMovement(); List<Unit> enemyList = isEnemy ? game.playingField.friendlyUnits : game.playingField.enemyUnits; if(attackTarget == null || !enemyList.Contains(attackTarget) || Vector2.Distance(attackTarget.Position, Position) > maxRange || targetTimer > 200) { List<Unit> inRange = game.playingField.getEnemyUnitsInRange(this); if(inRange != null && inRange.Count > 0) { int num = game.playingField.rand.Next(inRange.Count); attackTarget = inRange[num]; } targetTimer = 0; } else { targetTimer++; if(shootTimer == 0) { game.playingField.createBullet(this, (float) Math.Atan2(attackTarget.Position.Y - Position.Y, attackTarget.Position.X - Position.X)); shootTimer = shootTime; } } if(shootTimer > 0) shootTimer--; }