/// <summary> /// Makes it so that the enemy walks a random path /// </summary> public void randomWalk() { DiDo.Levels.Levels level = new DiDo.Levels.Levels(); // Gets the tiles of the level and looks if they can be walked on if (this.random.Next(0, 30) == 1) // A chance of 1 to 29 that the enemy will walk { this.direction = random.Next(0, 4); // Calculates a random number and walks in the direction of that number } else { if (this.random.Next(0, 360) == 1) // 1 on 120 chance that the enemy will shoot // Shoot if the enemy stands still { this.currentWeapon.reload(); // Reload if (this.currentWeapon.getAmmo() >= 1) // Looks if there are bullets in the magazine { float xPos = random.Next((int)(MainPage.player.x - 70), (int)(MainPage.player.x + 70)); float yPos = random.Next((int)(MainPage.player.y - 70), (int)(MainPage.player.y + 70)); // Makes the chance of shooting straigh very low float xVel = (xPos - x) / 18; float yVel = (yPos - y) / 18; // Calculates the bullet velocity if (xVel > 50) { xVel = 50; } if (yVel > 50) { yVel = 50; } // Makes it so that the bullets wont go to fast MainPage.bullets.Add(new DiDo.Bullet(x, y, xVel, yVel, currentWeapon.getDamage(), name)); // Adds bullets to the list this.currentWeapon.reduceAmmo(); // Reduces the ammo } } } if (this.direction == 0) { Tile nextTile = level.getPlayerTile(this.x, this.y + 1, level.gekozenLevel); // Gets the information of the tile if (nextTile.CanWalk == true) // Checks if the next tile can be walked on. { this.y += stepSize; // Walk down } } else if (this.direction == 1) { Tile nextTile = level.getPlayerTile(this.x + 1, this.y, level.gekozenLevel); // Gets the information of the tile if (nextTile.CanWalk == true) // Checks if the next tile can be walked on. { this.x += stepSize; // Walk to the right } } else if (this.direction == 2) { Tile nextTile = level.getPlayerTile(this.x, this.y - 1, level.gekozenLevel); // Gets the information of the tile if (nextTile.CanWalk == true) // Checks if the next tile can be walked on. { this.y -= stepSize; // Walk up } } else { Tile nextTile = level.getPlayerTile(this.x - 1, this.y, level.gekozenLevel); // Gets the information of the tile if (nextTile.CanWalk == true) // Checks if the next tile can be walked on. { this.x -= stepSize; // Walk to the left } } }
public void bulletHandling(ICanvasAnimatedControl sender, CanvasAnimatedDrawEventArgs args) { List <Bullet> bulletsToRemove = new List <Bullet>(); // Create a list for bullets to delete List <int> enemiesToRemove = new List <int>(); // Create a list for enemies to delete // Show bullets foreach (Bullet bullet in bullets) { bullet.x += bullet.velX; bullet.y += bullet.velY; // Move the bullets with choosen velocity args.DrawingSession.DrawImage(Bullets, bullet.x, bullet.y); if (levels.getPlayerTile(bullet.x, bullet.y, levels.gekozenLevel).TileType.Contains("wall") || // If the bullet hits a wall levels.getPlayerTile(bullet.x, bullet.y, levels.gekozenLevel).TileType.Contains("tree")) // If the bullet hits a tree { bulletsToRemove.Add(bullet); // Add the bullet to the list of bullets to remove } if (bullet.y < 0f || bullet.y > 1080 || bullet.x > 1920f || bullet.x < 0f) // Als de kogel buiten beeld gaat { bulletsToRemove.Add(bullet); // Add the bullet to the list of bullets to remove } int enemiesCount = 0; foreach (Enemy enemy in enemies) // Loop though all the enemies { if ((bullet.y > enemy.y - 16 && bullet.y < enemy.y + 16) && (bullet.x > enemy.x - 16 && bullet.x < enemy.x + 16) && (bullet.eigenaar != enemy.name)) // If the bullet isnt from yourself { enemy.hit(player.currentWeapon.getDamage()); // Give the enemy damage from the bullet if (enemy.getHealth() <= 0) { addItem(enemy); // Drop the weapon enemiesToRemove.Add(enemiesCount); // Add the enemy to the enemies to delete } bulletsToRemove.Add(bullet); // Add the bullet to the list of bullets to remove } enemiesCount++; // Increase the enemiesCount } if ((player.y > player.y - 16 && bullet.y < player.y + 16) && (bullet.x > player.x - 16 && bullet.x < player.x + 16) && (bullet.eigenaar != player.name)) // If the bullet hits the player { player.hit(bullet.damage); // Let the player take damage from the bullet bulletsToRemove.Add(bullet); // Add the bullet to the list of bullets to remove if (!player.alive) { Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { Frame.Navigate(typeof(GameOver)); }).AsTask().Wait(); } } } // Remove Bullets foreach (Bullet bullet in bulletsToRemove) { bullets.Remove(bullet); // Remove bullet } // Remove enemies foreach (int removeEnemy in enemiesToRemove) { enemies.RemoveAt(removeEnemy); // Remove enemy // Check if there are enemies left. If not; game is finished if (enemies.Count == 0) { // Run on UI thread CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { // Navigate to success page Frame.Navigate(typeof(SuccessPage)); }); } } }