public static GameObject getNearestEnemyObject(GameObject locationObj) { if (objList.Count == 0) return null; int nr = -1; float length = float.MaxValue; for (int i = 0; i < objList.Count; i++) { if (objList[i] is Ball) { float newDist; Vector2.Distance(ref locationObj.position, ref objList[i].position, out newDist); if (newDist < length) { length = newDist; nr = i; } } } return objList[nr]; }
private void UpdatePerceptions(GameTime gameTime) { nearestObj = null; nearestObj = Game1.getNearestEnemyObject(Game1.controlShip); willCollide = false; if (nearestObj != null) { Vector2.Distance(ref nearestObj.position, ref Game1.controlShip.position, out nearestObjDist); float adjSafetyRadius = safetyRadius + nearestObj.size; if (nearestObjDist <= adjSafetyRadius) willCollide = true; } }
public override bool checkCollision(GameObject o) { float distance = (float)Math.Sqrt(Math.Pow(o.position.X - position.X, 2) + Math.Pow(o.position.Y - position.Y, 2)); return distance < (o.offset.X * o.scale.X) + (m_offset.X * m_scale.X); }
public virtual bool checkCollision(GameObject o) { return false; }
// spawn an explosion based on a source object public void spawnExplosion(GameObject o) { if(o == null) { return; } spawnExplosion(o.position, o.velocity); }
// check if the ship is too close to another object public bool checkExtendedCollision(GameObject o) { float distance = (float)Math.Sqrt(Math.Pow(o.position.X - position.X, 2) + Math.Pow(o.position.Y - position.Y, 2)); return distance < (o.offset.X * o.scale.X) + 125.0f; }
public override bool Collision(GameObject obj) { return base.Collision(obj); }
public void spawnAsteroidCluster(GameObject o) { if(o == null || m_numberOfSmallAsteroids >= m_maxSmallAsteroids) { return; } Random rand = new Random(); // randomly choose how many small asteroids to spawn from a bigger asteroid int clusterSize = 0; int clusterSeed = rand.Next(0, 100); if(clusterSeed < 34) { clusterSize = 0; } // 34% chance no asteroids else if(clusterSeed < 76) { clusterSize = 1; } // 42% chance 1 asteroid else if(clusterSeed < 90) { clusterSize = 2; } // 14% chance 2 asteroids else if(clusterSeed < 96) { clusterSize = 3; } // 6% chance 3 asteroids else if(clusterSeed < 100) { clusterSize = 4; } // 4% chance 4 asteroids // create the corresponding number of mini-asteroids, and set their appropriate locations and directions // by rotating a pair of x/y co-ordinates around the origin of the original asteroid, randomized at equal // intervals and set their velocities so they are moving away from the origin of the original asteroid float angle = (float) (rand.NextDouble() * 359.0f); float angleIncrement = (360.0f / clusterSize); Asteroid newAsteroid; for(int i=0;i<clusterSize;i++) { newAsteroid = new Asteroid(m_asteroidSprites.getSprite(rand.Next(0, 4) + 2), false, m_settings); // calculate the velocities of the new asteroid float minVelocity = (float) (rand.NextDouble() * 0.3f) + 0.007f; float maxVelocity = (float) (rand.NextDouble() * 2.5f) + 0.8f; float xVelocity = (float) ( (((rand.Next(0, 2) % 2) == 0) ? 1 : -1) * (rand.NextDouble() * (maxVelocity - minVelocity) + minVelocity) ); float yVelocity = (float) ( (((rand.Next(0, 2) % 2) == 0) ? 1 : -1) * (rand.NextDouble() * (maxVelocity - minVelocity) + minVelocity) ); // set the velocities and locations of the new asteroid newAsteroid.position = new Vector2((float) (o.position.X + (o.offset.X * Math.Cos(MathHelper.ToRadians(angle)))), (float) (o.position.Y + (o.offset.Y * Math.Sin(MathHelper.ToRadians(angle))))); newAsteroid.velocity = new Vector2((float) (xVelocity * Math.Cos(MathHelper.ToRadians(angle))), (float) (yVelocity * Math.Sin(MathHelper.ToRadians(angle)))); // store the asteroid if(newAsteroid.bigAsteroid) { m_numberOfBigAsteroids++; } else { m_numberOfSmallAsteroids++; } m_asteroids.Add(newAsteroid); // increment to the next projection angle angle += angleIncrement; } }