private void FireWeapon(ProjectileManager pm) { if (maxWeapons > 0) { currentFireTimer -= 1; if (currentFireTimer <= 0 && alive && currentEnergy>0) { currentFireTimer = fireTimer; foreach (Weapon w in weapons) { w.FireProjectile(pm); } } } }
public void FireProjectile(ProjectileManager pm) { if (fireTimer <= 0) { //loop through projectiles until an alive projectile is found for (int p = 0; p < projectiles.Length; p++ ) { //set the found projectile to be live and initiate for moving if (!projectiles[p].alive) { projectiles[p].Prepare(originator.position - (projectiles[p].center), 0); projectiles[p].SetTrajectory(originator.facing); projectiles[p].safePosition = projectiles[p].position; projectiles[p].collided = false; projectiles[p].totalLife = 0; projectiles[p].ResetAnimation(); projectiles[p].lastSideCollided = -1; pm.liveProjectiles.Add(projectiles[p]); p = projectiles.Length; } } fireTimer = fireRate; } }
public void Update(Map map, Player[] players, List<Enemy> enemies, ProjectileManager pm) { if (currentEnergy > 0) CaptureMovement(pm); bool newLife = true; //if energy goes too high then give some more life if (energySaver > initialEnergySaver) { energySaver = initialEnergySaver; currentEnergy += 50; newLife = false; } //if health goes too high then get new life if (currentEnergy > initialEnergy) { bool iWantToGiveNewLife = false; if (newLife && iWantToGiveNewLife) lives += 1; currentEnergy = initialEnergy; } if (dieTimerCurrent==2) { //decrease lives and reset energy lives -= 1; dieTimerCurrent = 0; //if there are still more lives then reset player position if (lives > 0) { currentEnergy = initialEnergy; energySaver = initialEnergySaver; safetyTimer = 100; //reset all weapon bays ChangeWeapon(0, 4, 15, 1); for (int w = 1; w < weapons.Length; w++) { ChangeWeapon(w, 0, 10, 1); } Prepare(startPosition); } else { alive = false; } } if (safetyTimer > 0) { alpha = 0.6f; collidable = false; } else { alpha = 1.0f; collidable = true; } score += MoveObjects(map, players, enemies); if (rumbleTimer > 0) { rumbleTimer -= 1; if(energySaverOn) input.DoRumble(playerIndex, out actualPlayerIndex, 0.15f, 0.15f); else input.DoRumble(playerIndex, out actualPlayerIndex, 0.3f, 0.5f); } else { rumbleTimer = 0; input.DoRumble(playerIndex, out actualPlayerIndex, 0.0f, 0.0f); } if (position.X < map.drawArea.Left + (GetBounds().Width/2)) position.X = map.drawArea.Left + (GetBounds().Width/2); if (position.X > map.drawArea.Right - (GetBounds().Width/2)) position.X = map.drawArea.Right - (GetBounds().Width/2); if (position.Y < map.drawArea.Top + (GetBounds().Height )) position.Y = map.drawArea.Top + (GetBounds().Height ); if (position.Y > map.drawArea.Bottom - (GetBounds().Height )) position.Y = map.drawArea.Bottom - (GetBounds().Height ); Decelerate(); }
private void CaptureMovement(ProjectileManager pm) { input.Update(); //check movement //check fire is pressed for specific weapon if (input.IsNewKeyHeld(Keys.Space, playerIndex, out actualPlayerIndex) || input.IsNewButtonHeld(Buttons.A, playerIndex, out actualPlayerIndex) || input.IsNewButtonHeld(Buttons.RightTrigger, playerIndex, out actualPlayerIndex)) { weapons[0].FireProjectile(pm); } if (input.IsNewKeyHeld(Keys.LeftShift, playerIndex, out actualPlayerIndex) || input.IsNewButtonHeld(Buttons.B, playerIndex, out actualPlayerIndex) || input.IsNewButtonHeld(Buttons.LeftTrigger, playerIndex, out actualPlayerIndex)) { weapons[1].FireProjectile(pm); } bool defaultLine = false; int energyAdder = 0; if ((input.IsNewKeyHeld(Keys.LeftControl, playerIndex, out actualPlayerIndex) || input.IsNewButtonHeld(Buttons.X, playerIndex, out actualPlayerIndex) || input.IsNewButtonHeld(Buttons.LeftShoulder, playerIndex, out actualPlayerIndex) || input.IsNewButtonHeld(Buttons.RightShoulder, playerIndex, out actualPlayerIndex) || safetyTimer > 0) && energySaver>0) { safetyTimer -= 1; if (safetyTimer < 0) safetyTimer = 0; if(safetyTimer==0) energySaver -= 2; if (energySaver < 0) energySaver = 0; energyAdder = 4; defaultLine = true; energySaverOn = true; } else { defaultLine = false; energySaverOn = false; } //check for movement keys if (input.IsNewKeyHeld(Keys.A, playerIndex, out actualPlayerIndex) || input.IsNewButtonHeld(Buttons.DPadLeft, playerIndex, out actualPlayerIndex) || input.IsNewButtonHeld(Buttons.LeftThumbstickLeft, playerIndex, out actualPlayerIndex)) { velocity.X = -0.5f; acceleration += 2.0f; defaultLine = false; } else if (input.IsNewKeyHeld(Keys.D, playerIndex, out actualPlayerIndex) || input.IsNewButtonHeld(Buttons.DPadRight, playerIndex, out actualPlayerIndex) || input.IsNewButtonHeld(Buttons.LeftThumbstickRight, playerIndex, out actualPlayerIndex)) { velocity.X = 0.5f; acceleration += 2.0f; defaultLine = false; } else { Decelerate(ref velocity.X); defaultLine = false; } if (input.IsNewKeyHeld(Keys.W, playerIndex, out actualPlayerIndex) || input.IsNewButtonHeld(Buttons.DPadUp, playerIndex, out actualPlayerIndex) || input.IsNewButtonHeld(Buttons.LeftThumbstickUp, playerIndex, out actualPlayerIndex)) { velocity.Y = -0.5f; acceleration += 1.0f; sizeModifier = new Vector2(0, 0); defaultLine = true; SetSpriteLine(3 + energyAdder, 5); } else if (input.IsNewKeyHeld(Keys.S, playerIndex, out actualPlayerIndex) || input.IsNewButtonHeld(Buttons.DPadDown, playerIndex, out actualPlayerIndex) || input.IsNewButtonHeld(Buttons.LeftThumbstickDown, playerIndex, out actualPlayerIndex)) { velocity.Y = 0.5f; acceleration += 1.0f; sizeModifier = new Vector2(0, 0); defaultLine = true; SetSpriteLine(2 + energyAdder, 5); } else { Decelerate(ref velocity.Y); sizeModifier = new Vector2(0, -25); if (defaultLine == false) { SetSpriteLine(1 + energyAdder, 1); } } }
public void FireWeapon(int weaponIndex, ProjectileManager pm) { weapons[weaponIndex].FireProjectile(pm); }
private void LoadLevel(int levelID, int totalPlayers) { if (!File.Exists(Path.Combine(StorageContainer.TitleLocation, "Content\\Maps\\level_" + levelID + ".xml"))) { WinGame(); return; } levelsXml.Load(Path.Combine(StorageContainer.TitleLocation, "Content\\Maps\\level_" + levelID + ".xml")); map = new Map(gameArea); XmlNode levelData = levelsXml.DocumentElement.SelectSingleNode("/levels/level[@id='" + levelID + "']"); //If there is no level data then the game must have been completed if (levelData == null) { WinGame(); return; } //Load Map Data XmlNode mapData = levelData.SelectSingleNode("map"); Texture2D tileTextures = content.Load<Texture2D>(levelData.SelectSingleNode("map/@texture_path").InnerText); XmlNodeList enemyTexturesNodes = levelData.SelectNodes("enemyTextures/texture"); Texture2D[] enemyTextures = new Texture2D[enemyTexturesNodes.Count]; for (int t = 0; t < enemyTexturesNodes.Count; t++) enemyTextures[t] = content.Load<Texture2D>(enemyTexturesNodes[t].SelectSingleNode("@path").InnerText); enemies = new EnemySpawner(enemyTextures); projectiles = new ProjectileManager(); Point scrollSpeed = new Point(Convert.ToInt32(levelData.SelectSingleNode("map/@scroll_x").InnerText), Convert.ToInt32(levelData.SelectSingleNode("map/@scroll_y").InnerText)); XmlNodeList tileColumns = mapData.SelectNodes("column"); map.tiles = new Tile[tileColumns.Count][]; int tileWidth = 0; int tileHeight = 0; for (int x = 0; x < tileColumns.Count; x++) { XmlNodeList tileRows = tileColumns[x].SelectNodes("tile"); map.tiles[x] = new Tile[tileRows.Count]; for (int y = 0; y < tileRows.Count; y++) { XmlNode tileCell = tileRows[y]; //get all graphic types XmlNodeList graphics = tileCell.SelectNodes("graphics/graphic"); Vector4[] types = new Vector4[graphics.Count]; for (int g = 0; g < graphics.Count; g++) { XmlNode graphic = graphics[g]; types[g] = new Vector4(Convert.ToInt32(graphic.SelectSingleNode("@sprite_x").InnerText), Convert.ToInt32(graphic.SelectSingleNode("@sprite_y").InnerText), (float)Convert.ToDouble(graphic.SelectSingleNode("@rotation").InnerText), Convert.ToInt32(graphic.SelectSingleNode("@flip").InnerText)); } int power = Convert.ToInt32(tileCell.SelectSingleNode("@power").InnerText); int energy = Convert.ToInt32(tileCell.SelectSingleNode("@energy").InnerText); int timer = 2; int worth = Convert.ToInt32(tileCell.SelectSingleNode("@worth").InnerText); bool destroyable = System.Convert.ToBoolean(tileCell.SelectSingleNode("@destroyable").InnerText); bool collidable = System.Convert.ToBoolean(tileCell.SelectSingleNode("@collidable").InnerText); tileWidth = Convert.ToInt32(tileCell.SelectSingleNode("dimensions/@width").InnerText); tileHeight = Convert.ToInt32(tileCell.SelectSingleNode("dimensions/@height").InnerText); //digest enemy spawn points XmlNodeList enemiesToSpawn = tileCell.SelectNodes("enemies/enemy"); List<int> enemySpawns = new List<int>(); for (int e = 0; e < enemiesToSpawn.Count; e++) { XmlNode enemy = enemiesToSpawn[e]; int enemyToAdd=Convert.ToInt32(enemy.SelectSingleNode("@type").InnerText); enemySpawns.Add(enemyToAdd); endOfLevel += enemies.AddLevelEnders(enemyToAdd); } map.tiles[x][y] = new Tile(enemySpawns, 0.0f, new Point(x, y), types, destroyable, collidable, power, energy, timer, worth, tileTextures, tileWidth, tileHeight); map.tiles[x][y].alive = true; } } map.scrollSpeed = scrollSpeed; map.tileSize = new Point(tileWidth, tileHeight); //Load Player Data XmlNodeList playerNodes = levelData.SelectNodes("players/player"); players = new Player[totalPlayers]; playerHuds = new PlayerHud[totalPlayers]; SpriteFont playerhudFont = content.Load<SpriteFont>("Fonts/hudfont"); Texture2D pixelDot = content.Load<Texture2D>("Sprites/pixel"); PlayerIndex[] playerIndexes = new PlayerIndex[] { PlayerIndex.One, PlayerIndex.Two, PlayerIndex.Three, PlayerIndex.Four }; Color[] playerColors = new Color[] { Color.Red, Color.Green }; for (int p = 0; p < totalPlayers; p++) { XmlNodeList weapons = playerNodes[p].SelectNodes("weapons/weapon"); Texture2D projectileTexture = content.Load<Texture2D>(playerNodes[p].SelectSingleNode("@projectile_path").InnerText); Texture2D playerTexture = content.Load<Texture2D>(playerNodes[p].SelectSingleNode("@texture_path").InnerText); float pWeight = (float) Convert.ToDouble(playerNodes[p].SelectSingleNode("@weight").InnerText); int pPower = Convert.ToInt32(playerNodes[p].SelectSingleNode("@power").InnerText); //energy and shield is reset to full every map load int pEnergy = Convert.ToInt32(playerNodes[p].SelectSingleNode("@energy").InnerText); int pShield = Convert.ToInt32(playerNodes[p].SelectSingleNode("@shield").InnerText); int pWorth = Convert.ToInt32(playerNodes[p].SelectSingleNode("@worth").InnerText); //lives are set to current int pLives = Convert.ToInt32(playerNodes[p].SelectSingleNode("@lives").InnerText); if (playerLives[p] > -1) pLives = playerLives[p]; int pWidth = Convert.ToInt32(playerNodes[p].SelectSingleNode("dimensions/@width").InnerText); int pHeight = Convert.ToInt32(playerNodes[p].SelectSingleNode("dimensions/@height").InnerText); int pTileX = Convert.ToInt32(playerNodes[p].SelectSingleNode("tile_start/@x").InnerText); int pTileY = Convert.ToInt32(playerNodes[p].SelectSingleNode("tile_start/@y").InnerText); float xpos = ((pTileX * tileWidth)) + gameArea.X; float ypos = ((pTileY * tileHeight)) + gameArea.Y; players[p] = new Player(new Vector2(xpos, ypos), pShield, playerIndexes[p], projectileTexture, weapons.Count, pLives, pWeight, 0.01f, new Vector2(-4.0f, 5.0f), true, true, pPower, pEnergy, 18, pWorth, playerTexture, pWidth, pHeight, 1, 0.025f, 1, false); //set the player score to the current level score held by the player players[p].score = playerScores[p]; players[p].Prepare(players[p].startPosition); //set player huds playerHuds[p] = new PlayerHud(pixelDot, playerhudFont, players[p], playerColors[p], new Vector2(60 + (600 * p), gameArea.Top + 60), 0.4f); //set weapons //If there are no set weapons then use default weapons if (playerWeapons[p]==null) { playerWeapons[p] = new Weapon[weapons.Count]; for (int w = 0; w < weapons.Count; w++) { int wProjectiles = Convert.ToInt32(weapons[w].SelectSingleNode("@max_projectiles").InnerText); int wRate = Convert.ToInt32(weapons[w].SelectSingleNode("@fire_rate").InnerText); int wType = Convert.ToInt32(weapons[w].SelectSingleNode("@projectile_type").InnerText); players[p].ChangeWeapon(w, wProjectiles, wRate, wType); } } else { for (int w = 0; w < playerWeapons[p].Length; w++) { Weapon tmpWep = playerWeapons[p][w]; players[p].ChangeWeapon(w, tmpWep.maxProjectiles, tmpWep.fireRate, tmpWep.ammoType); } } } }
public int UpdateEnemies(Map map, Player[] players, List<Enemy> enemies, ProjectileManager pm) { int returnVal=0; List<Enemy> deletedIndex = new List<Enemy>(); List<Enemy> addIndex = new List<Enemy>(); foreach (Enemy enemy in liveEnemies) { //expand draw area to check to see if enemy has left area map.drawArea.Inflate(map.tileSize.X+(int)enemy.width, map.tileSize.Y+(int)enemy.height); if (!map.drawArea.Contains(enemy.GetBounds())) enemy.alive = false; //return map to original size map.drawArea.Inflate(-(map.tileSize.X + (int)enemy.width), -(map.tileSize.Y + (int)enemy.height)); enemy.Update(map, players, enemies, pm); if (enemy.currentEnergy == 0 && !enemy.spawned) { enemy.spawned = true; addIndex.Add(enemy); } //if the enemy is not alive then remove it from the list if (!enemy.alive) deletedIndex.Add(enemy); } foreach (Enemy addEnemy in addIndex) { //respawn another enemy foreach (Point respawns in addEnemy.respawn) { for (int i = 0; i < respawns.Y; i++) SpawnEnemy(respawns.X, addEnemy.position); } } //Remove enemies tagged for deletion foreach (Enemy deleteEnemy in deletedIndex) { if (deleteEnemy.levelEnder) returnVal += 1; liveEnemies.Remove(deleteEnemy); } return returnVal; }
public void Update(Map map, Player[] players, List<Enemy> enemies, ProjectileManager pm) { CalculateNextVector(); MoveObjects(map, players, enemies); FireWeapon(pm); }