/// <summary> /// Returns the index of the first (ie lowest numbered) /// sprite it colides with -1 if no collision /// the self parameter is there to allow the avoidance of collision texting against yourself /// </summary> /// <param name="s">the sprite to test the list against</param> /// <param name="self">the number of a single sprite to avoid testing</param> /// <returns></returns> public int collisionAA(Sprite3 s, int self) { if (s == null) { return(-1); } if (!s.getVisible()) { return(-1); } if (!s.getActive()) { return(-1); } for (int i = 0; i < highUsed + 1; i++) { if (sprite[i] != null && sprite[i].active) { if (sprite[i].getVisible() && i != self && s.collision(sprite[i])) { return(i); } } } return(-1); }
public override void Update(GameTime gameTime) { base.Update(gameTime); float deltaTime = (float)gameTime.ElapsedGameTime.TotalSeconds; //MOVEMENT //ALL CHECKS AND CHANGES BEFORE THE POSITION SET. //PLAYER SLICE COOLDOWN if (canSlice == false) { sliceTimer += deltaTime; } if (sliceTimer > sliceCooldown) { sliceTimer = 0; canSlice = true; iconSlice.isReady = true; } //DASH MECHANIC...no gravity for a duration //if (justDashed) //{ // acceleration.Y = 0; // noGravityTimer += deltaTime; // if(noGravityTimer > noGravityLimit) // { // noGravityTimer = 0; // justDashed = false; // } //} velocity += acceleration * deltaTime; if (velocity.X > velocityHorizontalMovespeedCap) { velocity.X -= accelerationDecay.X * deltaTime; if (velocity.X < velocityHorizontalMovespeedCap) { velocity.X = velocityHorizontalMovespeedCap; } acceleration.X = 0; } else if (velocity.X < -velocityHorizontalMovespeedCap) { velocity.X += accelerationDecay.X * deltaTime; if (velocity.X > -velocityHorizontalMovespeedCap) { velocity.X = -velocityHorizontalMovespeedCap; } acceleration.X = 0; } //FINALLY SET POSITION //set x pos. setPosX(getPosX() + velocity.X * deltaTime); //check horizontal collisions //COLLISION TEST WITH PLATFORMS for (int i = 0; i < platformList.count(); i++) { Sprite3 currentPlatform = platformList.getSprite(i); if (this.collision(currentPlatform)) { if (velocity.X > 0) { velocity.X = 0; this.setPosX(currentPlatform.getPosX() - this.getWidth()); } else if (velocity.X < 0) { velocity.X = 0; this.setPosX(currentPlatform.getPosX() + currentPlatform.getWidth()); } } } setPosY(getPosY() + velocity.Y * deltaTime); //COLLISION TEST WITH PLATFORMS for (int i = 0; i < platformList.count(); i++) { Sprite3 currentPlatform = platformList.getSprite(i); if (this.collision(currentPlatform)) { if (velocity.Y >= 0 && this.getPosY() <= currentPlatform.getPosY()) { this.setPos(getPosX(), currentPlatform.getPosY() - this.getHeight()); velocity.Y = 0; IsOnGround = true; } else if (velocity.Y < 0 && this.getPosY() >= currentPlatform.getPosY()) { this.setPos(getPosX(), currentPlatform.getPosY() + currentPlatform.getHeight()); velocity.Y = 0; } } } //If no input movement, slow down player if (canPlayerMove) { if (InputManager.Instance.KeyUp(Keys.Left) && InputManager.Instance.KeyUp(Keys.Right) && GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.X == 0) { acceleration.X = 0; if (velocity.X != 0) { if (velocity.X > 0) { velocity.X -= deltaTime * velocityDecay.X; if (velocity.X < 0) { velocity.X = 0; } } else if (velocity.X < 0) { velocity.X += deltaTime * velocityDecay.X; if (velocity.X > 0) { velocity.X = 0; } } } } else { //If travelling right and player moves left, increase the acceleration to opposite if (InputManager.Instance.KeyDown(Keys.Left)) { if (acceleration.X > -HorizontalMoveSpeed) { acceleration.X = -HorizontalMoveSpeed; } //If player velocity is more than the cap, slowly decay to the cap. if (velocity.X > 0) { velocity.X -= deltaTime * velocityDecay.X; if (velocity.X < 0) { velocity.X = 0; } } } if (GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.X != 0) { acceleration.X = HorizontalMoveSpeed * GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.X; if (GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.X > 0) { if (velocity.X < 0) { velocity.X += deltaTime * velocityDecay.X; if (velocity.X > 0) { velocity.X = 0; } } } else if (GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.X < 0) { if (velocity.X > 0) { velocity.X -= deltaTime * velocityDecay.X; if (velocity.X < 0) { velocity.X = 0; } } } } if (InputManager.Instance.KeyDown(Keys.Right)) { if (acceleration.X < HorizontalMoveSpeed) { acceleration.X = HorizontalMoveSpeed; } if (velocity.X < 0) { velocity.X += deltaTime * velocityDecay.X; if (velocity.X > 0) { velocity.X = 0; } } } //If both left and right pressed down, 0 acceleration if (InputManager.Instance.KeyDown(Keys.Right) && InputManager.Instance.KeyDown(Keys.Left)) { acceleration.X = 0; } } } //Is the player currently on a platform? bool isOnPlatform = false; for (int i = 0; i < platformList.count(); i++) { Sprite3 currentPlatform = platformList.getSprite(i); if (currentPlatform.getActive()) { if (currentPlatform.getBoundingBoxAA().Intersects(rectPlatformCheck)) { isOnPlatform = true; } } } if (isOnPlatform == false) { IsOnGround = false; } //PLAYER JUMP. if (canPlayerMove) { if (InputManager.Instance.KeyPressed(Keys.Z) || InputManager.Instance.ButtonPressed(Buttons.A)) { if (IsOnGround) { velocity.Y = -jumpVelocity; IsOnGround = false; soundJump.Play(); } else { if (canDoubleJump == true) { velocity.Y = -doubleJumpVelocity; canDoubleJump = false; iconDoubleJump.isReady = false; soundDoubleJump.Play(); } } } } //If player holds the jump button, gets maximum jump, otherwise fall faster. if (canPlayerMove) { if (InputManager.Instance.KeyDown(Keys.Z) || InputManager.Instance.ButtonDown(Buttons.A) && IsOnGround == false) { acceleration.Y = accelerationPlayerFall.Y; } else { acceleration.Y = accelerationDefault.Y; } } //PLAYER DASH if (canPlayerMove) { if (InputManager.Instance.KeyPressed(Keys.C) || InputManager.Instance.ButtonPressed(Buttons.B)) { if (canDash) { if (InputManager.Instance.KeyDown(Keys.Right) || GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.X > 0) { velocity.X = horizontalDashVelocity; velocity.Y = -20; canDash = false; iconDash.isReady = false; noGravityTimer = 0; justDashed = true; soundDash.Play(); } else if (InputManager.Instance.KeyDown(Keys.Left) || GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.X < 0) { velocity.X = -horizontalDashVelocity; velocity.Y = -20; canDash = false; iconDash.isReady = false; noGravityTimer = 0; justDashed = true; soundDash.Play(); } } } } if (IsOnGround == false) { if (velocity.Y > 0) { //check for platforms isCheckGroundPlatforms = true; } } else { isCheckGroundPlatforms = false; } rectPlatformCheck = new Rectangle((int)this.getPosX(), (int)this.getPosY() + (int)this.getHeight(), (int)this.getWidth(), 5); }