Ejemplo n.º 1
0
        //Check for collisions between the character and enemies/enemies and attacks
        private void CollisionChecking()
        {
            foreach (Enemy en in room1Enemies.Concat(room2Enemies).Concat(room3Enemies))
            {
                if (en != null)
                {
                    if (heroCharacter.CollisionCheck(en.ColSphere))
                    {
                        float xDiff = en.Position.X - heroCharacter.Position.X;
                        float yDiff = en.Position.Y - heroCharacter.Position.Y;
                        if (!en.IsBoss)
                        {
                            //push the enemy back from the player
                            en.Position += new Vector3(((en.Speed) * xDiff * 12), ((en.Speed) * yDiff * 12), 0f);
                        }
                        heroCharacter.Health -= 10.0f;
                    }

                    //Loop through all boss attacks to see if they have collided with the player
                    if (enemyBoss != null)
                    {
                        foreach (BossAttack bAttack in enemyBoss.OnScreenBossAttacks)
                        {
                            if (bAttack != null)
                            {
                                if (heroCharacter.CollisionCheck(bAttack.ColSphere))
                                {
                                    //Boss attack has collided with the player
                                    //Boss attack seems to do three times it's own power
                                    //Likely due to how long its in contact (issue)
                                    heroCharacter.Health -= (int)((GameConstants.bossAttackPower / 3));
                                    bAttack.IsActive      = false;
                                }
                            }
                        }
                    }

                    //Check if any of the hero's attacks have hit an enemy
                    foreach (HeroAttack hAttack in heroCharacter.OnScreenAttacks)
                    {
                        if (hAttack != null)
                        {
                            if (en.CollisionCheck(hAttack.ColSphere))
                            {
                                //If enemy has enough health to take a hit
                                if (en.Health > GameConstants.heroAttackPower)
                                {
                                    en.Health -= GameConstants.heroAttackPower;
                                    //Only push back if the enemy isnt the final boss
                                    if (!en.IsBoss)
                                    {
                                        en.Position += new Vector3((GameConstants.heroAttackSpeed * ((float)Math.Cos(hAttack.Rotation.Z))) * 10, (GameConstants.heroAttackSpeed * ((float)Math.Sin(hAttack.Rotation.Z))) * 10, 0);
                                    }
                                    hAttack.IsActive = false;
                                }
                                else
                                {
                                    //Enemy is dead
                                    en.Health -= GameConstants.heroAttackPower;
                                    if (!en.IsBoss)
                                    {
                                        en.Position += new Vector3((GameConstants.heroAttackSpeed * ((float)Math.Cos(hAttack.Rotation.Z))) * 10, (GameConstants.heroAttackSpeed * ((float)Math.Sin(hAttack.Rotation.Z))) * 10, 0);
                                    }
                                    hAttack.IsActive = false;

                                    en.IsAlive = false;

                                    //Increase the game score
                                    Random rndNum    = new Random();
                                    int    randomNum = rndNum.Next(1, 3);
                                    gameScore += (int)en.InitHealth * randomNum;
                                }
                            }
                        }
                    }
                }
            }
        }