public void Update(ref List <Projectile> projectiles, GameTime gameTime, ProjectileType primaryProj, User user, bool removeProjectiles) { keyState = Keyboard.GetState(); // Check and handle key presses if (keyState.IsKeyDown(Controls.MoveLeftKey) && !keyState.IsKeyDown(Controls.MoveRightKey) && !Controls.CtrlPressed(keyState) && cannonBottomRect.Left > 0) { cannonBottomRect.X -= Speed; cannonTubeRect.X -= Speed; cannonBgRect.X -= Speed; accuracyBeamRect.X -= Speed; loadedDrawProj.Move(Speed, Direction.Left); } else if (keyState.IsKeyDown(Controls.MoveRightKey) && !keyState.IsKeyDown(Controls.MoveLeftKey) && !Controls.CtrlPressed(keyState) && cannonBottomRect.Right < windowWidth) { cannonBottomRect.X += Speed; cannonTubeRect.X += Speed; cannonBgRect.X += Speed; accuracyBeamRect.X += Speed; loadedDrawProj.Move(Speed, Direction.Right); } // Gets the number of stored projectiles of the current primary type int primaryProjCount = GameInfo.CountOf(GameInfo.ProjListToTypesWithoutFlying(projectiles), primaryProj); if (primaryProjCount <= 0 && GameInfo.ProjListToTypesWithoutFlying(projectiles).Count > 0) { ProjectileType newPrimary = user.Hotbar[0]; changeProj(newPrimary); primaryProj = newPrimary; } if (loadedDrawProj.Type != primaryProj && projectiles.Count > 0 && primaryProj != ProjectileType.None) { hasLoadedProjectile = true; loadedDrawProj = GameInfo.CreateProj(primaryProj); loadedDrawProj.SetPosition(cannonBgRect.X + cannonBgRect.Width / 2, cannonBottomRect.Y + YSPACING); } if (primaryProj == ProjectileType.None) { hasLoadedProjectile = false; } if (keyState.IsKeyDown(Controls.LaunchKey) && !Controls.CtrlPressed(keyState) && projectiles.Count > 0 && !slidingIn && !waitSlide && !animatingDown && !animatingUp && primaryProjCount > 0) { if (!prevKeyState.IsKeyDown(Controls.LaunchKey) || MachineCannon) { foreach (Projectile p in projectiles) { if (p.Type == primaryProj && !p.Flying) { if (p is ExplosiveProjectile) { ExplosiveProjectile e = p as ExplosiveProjectile; if (e.Exploding) { // Makes sure that projectiles that are exploding can't be // fired continue; } } p.X = loadedDrawProj.X; p.Y = cannonTubeRect.Y; p.Damage += cannonSettings.Damage; p.Speed += cannonSettings.Power; if (cannonSettings.EffectAdded != StatusEffect.None) { p.Effects.Add(cannonSettings.EffectAdded); } if (cannonSettings.Freezes) { p.Freezes = true; } p.Launch(); Sound.PlaySound(p.SoundWhenFired); onLaunch?.Invoke(p.Type); break; } } //if (currentProjIndx + 1 >= projectiles.Count) //{ // // Here we make sure that adding 1 to the projectile index won't put it // // out of the range of the list // currentProjIndx--; //} //else //{ // currentProjIndx++; //} animatingDown = true; waitSlide = true; loadedDrawProj.SetPosition(cannonBgRect.X + cannonBgRect.Width / 2, cannonBottomRect.Y + YSPACING); } } else if ((projectiles.Count <= 0 || primaryProjCount <= 0) && keyState.IsKeyDown(Controls.LaunchKey) && !prevKeyState.IsKeyDown(Controls.LaunchKey)) { Sound.PlaySound(Sounds.CannonClunk); } for (int i = 0; i < projectiles.Count; i++) { if (projectiles[i].Flying || (projectiles[i] is ExplosiveProjectile && (projectiles[i] as ExplosiveProjectile).Exploding)) { projectiles[i].Update(gameTime); } if (projectiles[i].Y < 0) { if (removeProjectiles) { projectiles.RemoveAt(i); } projRemoved?.Invoke(); //ResetNextProjectile(); continue; } if (!projectiles[i].Active && removeProjectiles) { //if (i < currentProjIndx) //{ // // The inactive projectile appears before the current projectile // // in the list, which will affect our updating logic. // // Thus, we must decrease the index and reset the projectile // ResetNextProjectile(); //} projectiles.RemoveAt(i); } } if (waitSlide) { slideTimer.Update(gameTime); if (slideTimer.QueryWaitTime(gameTime)) { waitSlide = false; slidingIn = true; loadedDrawProj.SetPosition(cannonBgRect.X + loadedDrawProj.Width / 2, loadedDrawProj.Y); } } if (hasLoadedProjectile) { loadedDrawProj.Update(gameTime); } //if (currentProjIndx < projectiles.Count) //{ // if (!(slidingIn || projectiles[currentProjIndx].Flying)) // { // ResetProjectilePos(projectiles[currentProjIndx]); // } //} //if (!waitSlide && !slidingIn) //{ // ResetProjPos(projectiles[currentProjIndx]); //} if (slidingIn) { if (loadedDrawProj.X < cannonBgRect.X + cannonBgRect.Width / 2) { loadedDrawProj.Move(2, Direction.Right); } else { slidingIn = false; } } if (loadedDrawProj != null) { loadedDrawProj.Y = cannonBottomRect.Y + YSPACING; } if (animatingDown) { cannonTubeRect.Width += ANIMATION_AMOUNT; cannonTubeRect.X -= ANIMATION_AMOUNT / 2; cannonTubeRect.Height -= ANIMATION_AMOUNT; cannonTubeRect.Y += ANIMATION_AMOUNT; if (cannonTubeRect.Width >= 0.5 * cannonBottomRect.Width) { animatingDown = false; animatingUp = true; } } if (animatingUp) { cannonTubeRect.Width -= ANIMATION_AMOUNT; cannonTubeRect.X += ANIMATION_AMOUNT / 2; cannonTubeRect.Height += ANIMATION_AMOUNT; cannonTubeRect.Y -= ANIMATION_AMOUNT; if (cannonTubeRect.Width <= fullWidth * TUBE_EXT_RATIO_WIDTH) { animatingUp = false; } } prevKeyState = keyState; this.projectiles = projectiles; }
public void Update(ref List <Item> items, ref User user, List <Projectile> projectiles, bool needSweep, bool restrictActions, GameTime gameTime) { keyState = Keyboard.GetState(); List <ProjectileType> currentHotbar = user.Hotbar; for (int i = 0; i <= currentHotbar.Count - 1; i++) { if (GameInfo.CountOf(GameInfo.ProjListToTypesWithoutFlying(projectiles), currentHotbar[i]) == 0) { // Resets the hotbar if there are no available projectiles of a certain type List <ProjectileType> availableTypes = GameInfo.ProjListToTypesWithoutFlying(projectiles); // Make sure that we can't include any projectiles that are already on our "hotbar" availableTypes = availableTypes.Except(availableTypes).ToList(); currentHotbar[i] = GameInfo.GetRandomProjType(availableTypes); } projAmountStrings[i] = "x" + AmountOf(currentHotbar[i], projectiles).ToString(); projAmounts[i] = AmountOf(currentHotbar[i], projectiles); projImgs[i] = Utilities.GetIconOf(currentHotbar[i]); } if (user.Hotbar != currentHotbar) { user.SetHotbar(currentHotbar); } projs = user.Hotbar; if (!restrictActions) { Rectangle mouseRect = new Rectangle(Mouse.GetState().X, Mouse.GetState().Y, 1, 1); for (int i = 0; i <= projRects.Count - 1; i++) { if (((mouseRect.Intersects(projRects[i]) && Mouse.GetState().LeftButton == ButtonState.Pressed) || (keyState.IsKeyDown(KeyForHotbarSlot(i + 1)) && prevKeyState.IsKeyUp(KeyForHotbarSlot(i + 1)))) && projAmounts[i] > 0) { PrimaryProj = user.Hotbar[i]; SetHighlightSquare(PrimaryProj); } } if (mouseRect.Intersects(rapidFireButton.Button.DrawRectangle)) { toolTip = Language.Translate(MACH_TOOLTIP); toolTipRect = new Rectangle(mouseRect.X - (int)mediumFont.MeasureString(toolTip).X, mouseRect.Y, (int)mediumFont.MeasureString(toolTip).X + SPACING, (int)mediumFont.MeasureString(toolTip).Y + SPACING); toolTipLoc = new Vector2(toolTipRect.X + SPACING / 2, toolTipRect.Y + SPACING / 2); showingToolTip = true; } else if (mouseRect.Intersects(sweeperButton.DrawRectangle)) { toolTip = Language.Translate(SWEEP_TOOLTIP); toolTipRect = new Rectangle(mouseRect.X - (int)mediumFont.MeasureString(toolTip).X, mouseRect.Y, (int)mediumFont.MeasureString(toolTip).X + SPACING, (int)mediumFont.MeasureString(toolTip).Y + SPACING); toolTipLoc = new Vector2(toolTipRect.X + SPACING / 2, toolTipRect.Y + SPACING / 2); showingToolTip = true; } else { showingToolTip = false; } if (keyState.IsKeyDown(Controls.ToggleRapidFireKey) && prevKeyState.IsKeyUp(Controls.ToggleRapidFireKey)) { rapidFireButton.ClickWithSound(); } rapidFireButton.Update(gameTime); } if (needSweep || !restrictActions) { if (keyState.IsKeyDown(Controls.SweepKey) && prevKeyState.IsKeyUp(Controls.SweepKey) && !Controls.CtrlPressed()) { sweeper.Sweep(); } sweeperButton.Update(gameTime); sweeper.Update(ref items); } prevKeyState = keyState; }