public override void Update(float deltaTime) { float s = speed.MinMax(MAX_SPEED, MIN_SPEED); var sw = new Stopwatch(); sw.Restart(); if (pressedKeys.Contains(Keys.Up) || pressedKeys.Contains(Keys.W)) { direction.Y = -1 * s; } else if (pressedKeys.Contains(Keys.Down) || pressedKeys.Contains(Keys.S)) { direction.Y = 1 * s; } else { direction.Y = 0; } if (pressedKeys.Contains(Keys.Left) || pressedKeys.Contains(Keys.A)) { direction.X = -1 * s; } else if (pressedKeys.Contains(Keys.Right) || pressedKeys.Contains(Keys.D)) { direction.X = 1 * s; } else { direction.X = 0; } var sw1 = sw.ElapsedMilliseconds; X += direction.X * deltaTime; Y += direction.Y * deltaTime; sw.Restart(); KeepInsideOwner(); var sw2 = sw.ElapsedMilliseconds; sw.Restart(); CheckForPowerUps(); var sw3 = sw.ElapsedMilliseconds; sw.Restart(); if (CheckForCollision()) { if (!shieldActivated) { Explosion.Burst(Parent, Center, 1000, 30, 450, 2, 2, -1); Explosion.Burst(Parent, TopLeft, 500, 30, 400, 1, 3, -1); Explosion.Burst(Parent, TopRight, 500, 30, 400, 1, 3, -1); Explosion.Burst(Parent, BottomLeft, 500, 30, 400, 1, 3, -1); Explosion.Burst(Parent, BottomRight, 500, 30, 400, 1, 3, -1); Delete(); } } else if (pressedKeys.Contains(Keys.Space) || pressedKeys.Contains(Keys.Enter)) { Shoot(); } var sw4 = sw.ElapsedMilliseconds; }
/// <summary> /// Updates the world by updating the lists that contain the objects of the world. /// </summary> /// <param name="s"></param> private void ProcessObject(string s) { Ship temp; Star tempStar; Projectile tempProj; object[] tempArr = new object[2]; // if the object is a ship if (s.Length >= 4 && s[2] == 's' && s[3] == 'h') { temp = JsonConvert.DeserializeObject <Ship>(s); //logic for explosion if (theWorld.GetShipsActive().Any <Ship>(x => x.ID == temp.ID) && temp.hp == 0) { Explosion exp = new Explosion(temp); theWorld.AddExplosion(exp); } if (theWorld.GetExplosions().Any(item => item.GetCount() == 109 && item.GetID() == temp.ID)) { theWorld.RemoveExplosion(temp.ID); } // logic for active ships // if the ship isn't in the current list, and it is not dead if (!theWorld.GetShipsActive().Any(item => item.ID == temp.ID && temp.hp != 0)) { theWorld.AddShipActive(temp); } // if the ship is in the current list, and it is not dead if (theWorld.GetShipsActive().Any(item => item.ID == temp.ID) && temp.hp != 0) { // remove old ship theWorld.RemoveShipActive(temp.ID); // add new ship theWorld.AddShipActive(temp); } // if the ship is dead, remove it if (theWorld.GetShipsActive().Any(item => item.ID == temp.ID) && temp.hp == 0) { // need to tell view to explode theWorld.RemoveShipActive(temp.ID); } // logic for all ships // if the ship is not in the list, add it if (!theWorld.GetShipsAll().Any(item => item.ID == temp.ID)) { theWorld.AddShipAll(temp); } // if the ship is in the list, update it if (theWorld.GetShipsAll().Any(item => item.ID == temp.ID)) { theWorld.RemoveShipAll(temp.ID); theWorld.AddShipAll(temp); } } // if the object is a start if (s.Length >= 4 && s[2] == 's' && s[3] == 't') { tempStar = JsonConvert.DeserializeObject <Star>(s); // if star does not exist if (!theWorld.GetStars().Any(item => item.ID == tempStar.ID)) { // add star theWorld.AddStar(tempStar); } else { theWorld.RemoveStar(tempStar.ID); theWorld.AddStar(tempStar); } } // if the object is a projectile if (s.Length >= 4 && s[2] == 'p') { tempProj = JsonConvert.DeserializeObject <Projectile>(s); // if projectile does not exists if (!theWorld.GetProjectiles().Any(item => item.ID == tempProj.ID)) { // add projectile theWorld.AddProjectile(tempProj.owner, tempProj); } // if projectile exists and is alive if (theWorld.GetProjectiles().Any(item => item.ID == tempProj.ID) && tempProj.alive) { // remove the old projectile theWorld.RemoveProjectile(tempProj.owner, tempProj.ID); // add the new one theWorld.AddProjectile(tempProj.owner, tempProj); } // if projectile is dead if (tempProj.alive == false) { theWorld.RemoveProjectile(tempProj.owner, tempProj.ID); } } }