public CGameObject CollisionCircle(string instanceType) { CGameObject collidedInstance = null; CGameObject otherInstance = null; //getting the object to collide with //looping through the object list for (int i = 0; i < CObjectManager.Instance.m_iGameObjects; i++) { //if the other object type is the one we're looking for if ((CObjectManager.Instance.m_pGameObjectList[i] != null) && instanceType.Equals(CObjectManager.Instance.m_pGameObjectList[i].m_sInstanceType)) { //getting the reference otherInstance = CObjectManager.Instance.m_pGameObjectList[i]; //measuring the distance double dist = (float)Math.Sqrt(Math.Pow(this.x - otherInstance.x, 2) + Math.Pow(this.y - otherInstance.y, 2)); //if distance is smaller than the collision radiuses if (dist <= (double)(this.m_iCollisionRadius + otherInstance.m_iCollisionRadius)) { collidedInstance = otherInstance; } } } return(collidedInstance); }
public override void Update() { m_fRotation += 10; //capping the rotation if (m_fRotation >= 360) { m_fRotation -= 360; } if (m_fRotation < 0) { m_fRotation += 360; } //movement m_fHorSpeed = (float)(distDirX(m_fVelocity, degToRad(m_fDirection))); m_fVerSpeed = (float)(distDirY(m_fVelocity, degToRad(m_fDirection))); x += m_fHorSpeed; y += m_fVerSpeed; //destroying the bullet outside the bounds if ((x > 848) || (x < 0) || (y > 480) || (y < 0)) { CObjectManager.Instance.DestroyInstance(m_iIndex); } //collisions CGameObject colTest = CollisionCircle("objAsteroid"); if (colTest != null) { CObjectManager.Instance.DestroyInstance(colTest.m_iIndex); CObjectManager.Instance.DestroyInstance(m_iIndex); } }
public override void Update() { if (!m_bDead && CObjectManager.Instance.m_bHasStarted) { //input KeyboardState keyboardState = Keyboard.GetState(); //turning if (keyboardState.IsKeyDown(Keys.Left)) { m_fDirection += 2; if (m_iSprite > 0) { m_iSprite -= 0.25F; } } else if (keyboardState.IsKeyDown(Keys.Right)) { m_fDirection -= 2; if (m_iSprite < 4) { m_iSprite += 0.25F; } } else { if (m_iSprite < 2) { m_iSprite += 0.25F; } else if (m_iSprite > 2) { m_iSprite -= 0.25F; } } //movement forwards if (keyboardState.IsKeyDown(Keys.Up)) { if (m_iImageIndex < 3) { m_iImageIndex += 0.5F; } else { m_iImageIndex = 0; } m_fVelocity = 0.25F; m_iSmokeCreationTick--; if (m_iSmokeCreationTick <= 0) { CObjectManager.Instance.CreateInstance("objSmoke", x + (float)distDirX(10, degToRad(m_fDirection + 180)), y + (float)distDirY(10, degToRad(m_fDirection + 180))); m_iSmokeCreationTick = 3; } } else { m_iImageIndex = 0; m_fVelocity = 0; } //firing boolets if (keyboardState.IsKeyDown(Keys.LeftControl) && m_iReload <= 0) { CObjectManager.Instance.CreateInstance("objBullet", x, y); CObjectManager.Instance.m_pGameObjectList[CObjectManager.Instance.m_iGameObjects - 1].m_fDirection = this.m_fDirection; if (m_bMultiShot) { CObjectManager.Instance.CreateInstance("objBullet", x, y); CObjectManager.Instance.m_pGameObjectList[CObjectManager.Instance.m_iGameObjects - 1].m_fDirection = this.m_fDirection - 22.5f; CObjectManager.Instance.CreateInstance("objBullet", x, y); CObjectManager.Instance.m_pGameObjectList[CObjectManager.Instance.m_iGameObjects - 1].m_fDirection = this.m_fDirection + 22.5f; } if (m_bRapidFire) { CMusicPlayer.Instance.PlaySound(CMusicPlayer.SFX_RAPIDSHOT); } else { CMusicPlayer.Instance.PlaySound(CMusicPlayer.SFX_BLASTERSHOT); } m_iReload = m_iReloadMax; } } if (m_bRapidFire) { m_iReloadMax = 5; } else { m_iReloadMax = 15; } if (m_iReload > 0) { m_iReload--; } //capping the rotation if (m_fDirection >= 360) { m_fDirection -= 360; } if (m_fDirection < 0) { m_fDirection += 360; } //the sprite wobble thing /*if (m_iSprite < 4.5F) * m_iSprite += 0.25F; * else * m_iSprite = 0;*/ //acceleration m_fHorSpeed += (float)(distDirX(m_fVelocity, degToRad(m_fDirection))); m_fVerSpeed += (float)(distDirY(m_fVelocity, degToRad(m_fDirection))); //capping the speed float m_fMaxSpeed = 5; if (m_fHorSpeed > m_fMaxSpeed) { m_fHorSpeed = m_fMaxSpeed; } else if (m_fHorSpeed < -m_fMaxSpeed) { m_fHorSpeed = -m_fMaxSpeed; } if (m_fVerSpeed > m_fMaxSpeed) { m_fVerSpeed = m_fMaxSpeed; } else if (m_fVerSpeed < -m_fMaxSpeed) { m_fVerSpeed = -m_fMaxSpeed; } //deacceleration float deAccSpeed = 0.025f; if (m_fHorSpeed < deAccSpeed && m_fHorSpeed > -deAccSpeed) { m_fHorSpeed = 0; } else if (m_fHorSpeed > 0) { m_fHorSpeed -= deAccSpeed; } else if (m_fHorSpeed < 0) { m_fHorSpeed += deAccSpeed; } if (m_fVerSpeed < deAccSpeed && m_fVerSpeed > deAccSpeed) { m_fVerSpeed = 0; } else if (m_fVerSpeed > 0) { m_fVerSpeed -= deAccSpeed; } else if (m_fVerSpeed < 0) { m_fVerSpeed += deAccSpeed; } //moving it loel x += m_fHorSpeed; y += m_fVerSpeed; //wrapping the object if (x > 832) { x = -32; } else if (x < -32) { x = 832; } if (y > 512) { y = -32; } else if (y < -32) { y = 512; } //collisions if (!m_bInvincible && !m_bDead && CObjectManager.Instance.m_bHasStarted) { CGameObject colTest = CollisionCircle("objAsteroid"); if (colTest != null) { m_bDead = true; m_iRespawnTimer = 120; CMusicPlayer.Instance.PlaySound(CMusicPlayer.SFX_PLAYERDEATH); CObjectManager.Instance.CreateInstance("objExplosion", x - 16 + myRandom.Next(32), y - 16 + myRandom.Next(32)); CObjectManager.Instance.CreateInstance("objExplosion", x - 16 + myRandom.Next(32), y - 16 + myRandom.Next(32)); CObjectManager.Instance.CreateInstance("objExplosion", x - 16 + myRandom.Next(32), y - 16 + myRandom.Next(32)); CObjectManager.Instance.m_iOneUps--; } } //powerup if (!m_bDead && CObjectManager.Instance.m_bHasStarted) { CGameObject colTest = CollisionCircle("objPowerUp"); if (colTest != null) { CMusicPlayer.Instance.PlaySound(CMusicPlayer.SFX_POWERUP); if (colTest.m_iSprInd == 12) { m_bMultiShot = true; m_iMultiShotTimer = 600; CMusicPlayer.Instance.PlaySound(CMusicPlayer.SFX_MULTISHOT); CObjectManager.Instance.m_bHasMultiShot = true; } else if (colTest.m_iSprInd == 13) { m_bRapidFire = true; m_iRapidFireTimer = 600; CMusicPlayer.Instance.PlaySound(CMusicPlayer.SFX_RAPIDFIRE); CObjectManager.Instance.m_bHasRapidFire = true; } CObjectManager.Instance.DestroyInstance(colTest.m_iIndex); CObjectManager.Instance.m_iScore += 100; } } if (m_iRespawnTimer <= 0 && m_bDead && CObjectManager.Instance.m_iOneUps > 0) { m_bInvincible = true; m_iInvincibilityTimer = 240; m_bDead = false; x = 400; y = 240; m_fHorSpeed = 0; m_fVerSpeed = 0; m_fVelocity = 0; m_iMultiShotTimer = 0; m_bMultiShot = false; CObjectManager.Instance.m_bHasMultiShot = false; m_iRapidFireTimer = 0; m_bRapidFire = false; CObjectManager.Instance.m_bHasRapidFire = false; } else { m_iRespawnTimer--; } if (m_iInvincibilityTimer <= 0 && m_bInvincible) { m_bInvincible = false; } else { m_iInvincibilityTimer--; } if (m_iMultiShotTimer <= 90) { CObjectManager.Instance.m_bMultiShotEnding = true; } else { CObjectManager.Instance.m_bMultiShotEnding = false; } if (m_iMultiShotTimer <= 0 && m_bMultiShot) { m_bMultiShot = false; CObjectManager.Instance.m_bHasMultiShot = false; } else { m_iMultiShotTimer--; } if (m_iRapidFireTimer <= 90) { CObjectManager.Instance.m_bRapidFireEnding = true; } else { CObjectManager.Instance.m_bRapidFireEnding = false; } if (m_iRapidFireTimer <= 0 && m_bRapidFire) { m_bRapidFire = false; CObjectManager.Instance.m_bHasRapidFire = false; } else { m_iRapidFireTimer--; } }