private void ControlAI() { Random rnd = new Random(); for (int i = 2; i < this.Level.Tanks.Count; i++) { if (!this.Level.Tanks[i].Active) { continue; } // střela if (rnd.Next(1, 51) == 20) { this.Level.Tanks[i].Fire(); } int colR, rowR; int col = Math.DivRem((int)(this.Level.Tanks[i].Position.X - this.Level.Tanks[i].Width/2), Wall.width, out colR); int row = Math.DivRem((int)(this.Level.Tanks[i].Position.Y - this.Level.Tanks[i].Height/2), Wall.height, out rowR); if (col < 0) { col = 0; } if (row < 0) { row = 0; } if (this.Level.Tanks[i].Collision || rnd.Next(1, 11) == 5) { List<int> dir = new List<int>(); Position pos1 = new Position(row, col + 2); Position pos2 = new Position(row, col - 1); Position pos3 = new Position(row - 1, col); Position pos4 = new Position(row + 2, col); // vpravo if ((col + 2) < 38 && !this.Level.Map.CheckWallPosition(pos1, 1, 2) && !this.Level.CheckTankPosition(pos1)) { dir.Add(1); } // vlevo if ((col - 1) >= 0 && !this.Level.Map.CheckWallPosition(pos2, 1, 2) && !this.Level.CheckTankPosition(pos2)) { dir.Add(0); } // nahore if ((row - 1) >= 0 && !this.Level.Map.CheckWallPosition(pos3, 2, 1) && !this.Level.CheckTankPosition(pos3)) { dir.Add(3); } // dole if ((row + 2) < 38 && !this.Level.Map.CheckWallPosition(pos4, 2, 1) && !this.Level.CheckTankPosition(pos4)) { dir.Add(2); } if (dir.Count > 0) { int nextDir; // kolize nebo random if (this.Level.Tanks[i].Collision || rnd.Next(1, 10) == 5) { if (dir.Count > 1) { // vlevo/vpravo -> následuje nahoru/dolů if (this.Level.Tanks[i].Direction <= 1 && (dir.Contains(2) || dir.Contains(3))) { dir.Remove(0); dir.Remove(1); } // nahoru/dolů -> následuje vlevo/vpravo else if (this.Level.Tanks[i].Direction >= 2 && (dir.Contains(0) || dir.Contains(1))) { dir.Remove(2); dir.Remove(3); } } // nový náhodný směr if (dir.Count > 0) { nextDir = dir[rnd.Next(0, dir.Count)]; } // původní směr else { nextDir = this.Level.Tanks[i].Direction; } } // původní směr else { nextDir = this.Level.Tanks[i].Direction; } // nastavení směru this.Level.Tanks[i].DirectionNext(nextDir); } this.Level.Tanks[i].Collision = false; } // pohyb switch (this.Level.Tanks[i].Direction) { case 0: this.Level.Tanks[i].MoveLeft(); break; case 1: this.Level.Tanks[i].MoveRight();break; case 2: this.Level.Tanks[i].MoveDown(); break; case 3: this.Level.Tanks[i].MoveUp(); break; } } }
public void Reset(Position pos) { if (this.Lifes == 0) { return; } this.Rotation = 0; this.Orientation.x = 0; this.Orientation.y = -1; this.Position.X = pos.col * Wall.width + this.texture.Width / 2; this.Position.Y = pos.row * Wall.height + this.texture.Height / 2; this.Active = true; // parametry this.Health = Tank.health; this.MoveSpeed = Tank.moveSpeed; this.ProjectilesCount = 1; this.ProjectilesSpeed = Projectile.moveSpeed; this.ProjectilesDamage = Projectile.damage; }
protected void AddTank(TankType type, Position pos, bool ai = true) { // žlutý / zelený tank Texture2D texture; if (type == TankType.Yellow) { texture = this.tankTextures[0]; } else { texture = this.tankTextures[1]; } // Tank tank = new Tank(ai); // inicializace tank.Initialize(this.area, texture, this.prjTextures[0], pos); // ochrana aby se tank nevložil na pozici jiného if (!this.CheckTankPosition(pos, 2, 2)) { this.Tanks.Add(tank); } }
public void Initialize(Vector2 area, Texture2D tankTexture, Texture2D prTexture, Position pos) { // this.area = area; this.texture = tankTexture; this.prjTexture = prTexture; // pozice this.Position.X = pos.col * Wall.width + Wall.width; this.Position.Y = pos.row * Wall.height + Wall.height; this.origin.X = this.texture.Width / 2; this.origin.Y = this.texture.Height / 2; this.Orientation = new Orientation(0, -1); // střely this.Projectiles = new List<Projectile>(); this.ProjectilesCount = 1; this.ProjectilesSpeed = Projectile.moveSpeed; this.ProjectilesDamage = Projectile.damage; this.Active = true; this.MoveSpeed = Tank.moveSpeed; this.Health = Tank.health; Random random = new Random(); this.Direction = random.Next(0, 1); this.Collision = false; }
public bool CheckTankPosition(Position pos, int width = 1, int height = 1) { Tank result = this.Tanks.Find(delegate(Tank t) { if (!t.Active) { return false; } int col = (int)((t.Position.X - t.Width/2) / Wall.width); int row = (int)((t.Position.Y - t.Height/2) / Wall.height); if (col < 0) { col = 0; } if (row < 0) { row = 0; } Rectangle t1 = new Rectangle(col, row, 2, 2); Rectangle t2 = new Rectangle(pos.col, pos.row, width, height); if (t1.Intersects(t2)) { return true; } return false; }); if (result == null) { return false; } return true; }