private void OnFixedUpdate(float obj) { if (_fixedTime.FixedTravelTime >= _ttl) { _projectile.Destroy(); } }
private void AfterCollisionEnter() { _remainingCollisions--; if (_remainingCollisions < 0) { _facade.Destroy(); } }
public override void Update(float ms) { if (_state == AlienState.ALIVE) { // check bullet collision // check for collision with bullet IProjectile p = Globals.gameInstance.projectileManager.CheckHit(this); if (p != null) { p.PreDestroy(); p.Destroy(); this._health -= p.GetDamage(); if (this._health <= 0) { this._state = AlienState.DYING; this._aplayer.StartClip(Animation_States.death); Globals.audioManager.PlayGameSound("aliendeath1"); } } if (_livestate == LiveState.ROAMING) { // check distance to target if (Vector3.DistanceSquared(_targetPosition, _position) < 0.3f) { // new target _targetPosition = new Vector3( MathHelper.Clamp( _position.X + (float)Globals.random.NextDouble() * 10 - 5, _section.Index * 32, _section.Index * 32 + 32 ), _position.Y, _position.Z + (float)Globals.random.NextDouble() * 10 - 5 ); } // check player LOS PlayerObject closestPlayer = Globals.gameInstance.GetClosestPlayer(_position); if (Vector3.DistanceSquared(closestPlayer.Position, _position) <= 6 * 6) { //check LOS _livestate = LiveState.CHASING; _targetPlayer = closestPlayer; } } if (_livestate == LiveState.CHASING) { _targetPosition = _targetPlayer.Position; float d = Vector3.DistanceSquared(_targetPosition, _position); if (d > 6 * 6) { _livestate = LiveState.ROAMING; _targetPosition = _position; } else { //check LOS bool LOS = true; if (LOS) { if (d < 2.0f) { _livestate = LiveState.ATTACKING; if (_targetPlayer.alive == true) { _targetPlayer.health = _targetPlayer.health - 10; } _aplayer.StartClip(Animation_States.attacking); } } else { _livestate = LiveState.ROAMING; _targetPosition = _position; } } } if (_livestate == LiveState.ATTACKING) { _targetPosition = _targetPlayer.Position; if (_aplayer.GetLoopCount() > 0) { _livestate = LiveState.CHASING; _aplayer.StartClip(Animation_States.charging); } } if (_targetPosition != _position) { _velocity = _targetPosition - _position; _velocity.Normalize(); if (RotateToFacePosition(_velocity, ms)) { bool collided = false; Vector3 newpos = _position + _velocity * (ms / 180); // FIRST DO TEH X RebuildCollisionRectangle(newpos); if (Globals.gameInstance.cellCollider.RectangleCollides(_collisionRectangle)) { collided = true; } else if (Globals.gameInstance.campaignManager.CollideCurrentEntities(this)) { collided = true; } else if (Globals.gameInstance.CollidesPlayers(this)) { collided = true; } if (!collided) { _position = newpos; } else if (_livestate == LiveState.ROAMING) { _targetPosition = new Vector3( MathHelper.Clamp( _position.X + (float)Globals.random.NextDouble() * 10 - 5, _section.Index * 32, _section.Index * 32 + 32 ), _position.Y, _position.Z + (float)Globals.random.NextDouble() * 10 - 5 ); } else { Vector3 newposX = _position + Vector3.Right * _velocity * (ms / 350); RebuildCollisionRectangle(newposX); if (Globals.gameInstance.cellCollider.RectangleCollides(_collisionRectangle)) { newposX.X = _position.X; } else if (Globals.gameInstance.campaignManager.CollideCurrentEntities(this)) { newposX.X = _position.X; } else if (Globals.gameInstance.CollidesPlayers(this)) { newposX.X = _position.X; } Vector3 newposZ = _position + Vector3.Backward * _velocity * (ms / 350); RebuildCollisionRectangle(newposX); if (Globals.gameInstance.cellCollider.RectangleCollides(_collisionRectangle)) { newposZ.Z = _position.Z; } else if (Globals.gameInstance.campaignManager.CollideCurrentEntities(this)) { newposZ.Z = _position.Z; } else if (Globals.gameInstance.CollidesPlayers(this)) { newposZ.Z = _position.Z; } _position = new Vector3(newposX.X, _position.Y, newposZ.Z); } } } UpdateAnimations(ms * 1.5f); UpdateMajorTransform(); _modelReceipt.graph.Renew(_modelReceipt); } else { UpdateAnimations(ms * 1.5f); // animations are accelerated a bit if (this._aplayer.GetLoopCount() > 0) { this._state = AlienState.DEAD; this._mustBeDeleted = true; this.DestroyReceipt(); } } }
public override void Update(float ms) { if (health <= 0) { health = 0; this.alive = false; if (currentFiringAnimation != Animation_States.death) { currentFiringAnimation = Animation_States.death; upPlayer.StartClip(currentFiringAnimation); lowPlayer.StartClip(currentFiringAnimation); } else if (upPlayer.GetLoopCount() > 1) { this.dead = true; } UpdateAnimation(ms); UpdateMajorTransforms(ms); } else { IProjectile p = Globals.gameInstance.alienProjectileManager.CheckHit(this); if (p != null) { p.PreDestroy(); p.Destroy(); this.health -= p.GetDamage(); //if (this._health <= 0) //{ // this._state = AlienState.DYING; // this._aplayer.StartClip(Animation_States.death); // Globals.audioManager.PlayGameSound("aliendeath1"); //} } double temp = this.oldHealth; this.oldHealth = this.health; if (temp == health && health >= 0 && health < 100) { this.health = this.health + ((ms / 1000) * 0.6); } Vector3 newposition = new Vector3(_position.X, _position.Y, _position.Z); // 1. == Update Movement // movement is done via analog sticks Vector2 vleft = Globals.inputController.getAnalogVector(AnalogStick.Left, playerIndex); Vector2 vright = Globals.inputController.getAnalogVector(AnalogStick.Right, playerIndex); // 1.1 = Update player rotation based on RIGHT analog stick if (vright.LengthSquared() > 0.01f) { // get target angle float targetrotation = (float)Math.Atan2(vright.Y, vright.X) - MathHelper.PiOver2; // get difference float deltarotation = targetrotation - rotation; // sanitise if (deltarotation > Math.PI) { deltarotation -= MathHelper.TwoPi; } if (deltarotation < -Math.PI) { deltarotation += MathHelper.TwoPi; } // add difference rotation += (ms / 300) * deltarotation; // sanitise rotation if (rotation > MathHelper.TwoPi) { rotation -= MathHelper.TwoPi; } if (rotation < -MathHelper.TwoPi) { rotation += MathHelper.TwoPi; } } // 1.2 = Update player movement based on LEFT analog stick if (vleft.LengthSquared() > 0.1f) { Vector3 pdelta = new Vector3(vleft.X, 0, -vleft.Y); pdelta.Normalize(); // modifies the horizantal direction newposition += pdelta * ms / 300; // 1.3 = If no rotation was changed, pull player angle toward forward vector if (vright.LengthSquared() < 0.01f) { // get target angle float targetrotation = (float)Math.Atan2(vleft.Y, vleft.X) - MathHelper.PiOver2; // get difference float deltarotation = targetrotation - rotation; // sanitise if (deltarotation > Math.PI) { deltarotation -= MathHelper.TwoPi; } if (deltarotation < -Math.PI) { deltarotation += MathHelper.TwoPi; } // add difference rotation += (ms / 300) * deltarotation; // sanitise rotation if (rotation > MathHelper.TwoPi) { rotation -= MathHelper.TwoPi; } if (rotation < -MathHelper.TwoPi) { rotation += MathHelper.TwoPi; } } moving = Animation_States.run; } else { moving = Animation_States.idle; } if (Globals.inputController.isTriggerDown(Triggers.Right, playerIndex)) { if (currentWeapon > -1 && weapons[currentWeapon].CanFire()) { weapons[currentWeapon].Fire(rotation + MathHelper.PiOver2); shooting = Animation_States.shoot; } } if (Globals.inputController.isButtonPressed(Microsoft.Xna.Framework.Input.Buttons.Y, playerIndex)) { int nw = (currentWeapon + 1) % 5; SwitchWeapon(nw); } if (Globals.inputController.isButtonPressed(Microsoft.Xna.Framework.Input.Buttons.X, playerIndex)) { // check game section WeaponDepot wd = Globals.gameInstance.campaignManager.GetNearestActiveDepot(_position); if (wd != null) { SwitchWeapon(wd.GetIndex()); wd.Deactivate(); } } // collision stuff if (_position != newposition) { // First test X collision // Xmovement can collide with walls or doors // first check the doors, this is fast collisionRectangle.Left = newposition.X - boundingBoxSize; collisionRectangle.Top = _position.Z - boundingBoxSize; if (Globals.gameInstance.cellCollider.RectangleCollides(collisionRectangle)) { // if it does collide, pull it back newposition.X = _position.X; } if (Globals.gameInstance.campaignManager.RectangleCollidesDoor(collisionRectangle)) { newposition.X = _position.X; } // Then test Z collision collisionRectangle.Left = _position.X - boundingBoxSize; collisionRectangle.Top = newposition.Z - boundingBoxSize; if (Globals.gameInstance.cellCollider.RectangleCollides(collisionRectangle)) { // if it does collide, pull it back newposition.Z = _position.Z; } collisionRectangle.Left = newposition.X - boundingBoxSize; collisionRectangle.Top = newposition.Z - boundingBoxSize; if (Globals.gameInstance.campaignManager.CollideCurrentEntities(this)) { newposition = _position; } //lastly check the distance between players if (Globals.gameInstance.players.Length > 1) { int otherPlayer = Math.Abs((int)playerIndex - 1); if (Math.Abs(Globals.gameInstance.players[otherPlayer]._position.X - newposition.X) > CameraController.MAX_DISTANCE_BETWEEN_PLAYERS) { newposition.X = _position.X; } } // if there is still a new position if (_position != newposition) { _position = newposition; modelReceipt.graph.Renew(modelReceipt); light1receipt.graph.Renew(light1receipt); light2receipt.graph.Renew(light2receipt); light3receipt.graph.Renew(light3receipt); } } UpdateAnimation(ms); UpdateMajorTransforms(ms); if (currentWeapon > -1) { weapons[currentWeapon].SetTransform(upPlayer.GetWorldTransforms()[31], mesh.Transform); weapons[currentWeapon].receipt.graph.Renew(weapons[currentWeapon].receipt); if (weapons[currentWeapon].AnimationComplete()) { shooting = 0; } } // Update Top half of body if (currentFiringAnimation != moving + shooting + weapon) { currentFiringAnimation = moving + shooting + weapon; upPlayer.StartClip(currentFiringAnimation); } //Update Bottom half of body if (currentAnimation != moving + weapon) { currentAnimation = moving + weapon; lowPlayer.StartClip(currentAnimation); } } }