public override void HandleCollision(Sprite collidedWith, Rectangle overlap) { if (collidedWith.GetType().BaseType == typeof(Gusto.Models.Animated.HandHeld)) { HandHeld handHeld = (HandHeld)collidedWith; showHealthBar = true; health -= handHeld.damage; } else if (collidedWith.bbKey.Equals("landTile") || collidedWith.bbKey.Equals("interiorTile")) { TilePiece tp = (TilePiece)collidedWith; colliding = false; mapCordPoint = tp.mapCordPoint; // narrow the collision to just the feet (appears more realistic) Rectangle footSpace = new Rectangle(GetBoundingBox().Left, GetBoundingBox().Bottom - (GetBoundingBox().Height / 3), GetBoundingBox().Width, GetBoundingBox().Height / 3); if (footSpace.Intersects(collidedWith.GetBoundingBox())) { swimming = false; } } else if (collidedWith is IGroundObject) { colliding = false; } else if (collidedWith.bbKey.Equals("interiorTileWall")) { colliding = false; } else if (collidedWith is IWalks || collidedWith is IShip || collidedWith is IPlaceable || collidedWith is IInventoryItem || collidedWith is IStructure) { colliding = false; } else if (collidedWith is IAmmo) { showHealthBar = true; Ammo ball = (Ammo)collidedWith; // TODO: bug NPC gets hit here by its own ships cannonballs if (!ball.exploded && ball.teamType != teamType) { health -= ball.groundDamage; } return; } }
public override void HandleCollision(Sprite collidedWith, Rectangle overlap) { Rectangle footSpace = new Rectangle(GetBoundingBox().Left, GetBoundingBox().Bottom - (GetBoundingBox().Height / 3), GetBoundingBox().Width, GetBoundingBox().Height / 3); if (collidedWith.bbKey.Equals("landTile")) { colliding = false; // narrow the collision to just the feet (appears more realistic) if (footSpace.Intersects(collidedWith.GetBoundingBox())) { swimming = false; } // can bury item? TilePiece tp = (TilePiece)collidedWith; if (tp.canFillHole) { foreach (var item in inventory) { if (item == null) { continue; } if (item.placeableVersion != null && item.placeableVersion is IStorage) { buryTile = tp; canBury = true; break; } } } } else if (collidedWith.bbKey.Equals("interiorTile")) { colliding = false; swimming = false; } else if (collidedWith.bbKey.Equals("interiorTileWall")) { if (footSpace.Intersects(collidedWith.GetBoundingBox())) { colliding = true; } } else if (collidedWith is IShip) { colliding = false; if (!onShip) { nearShip = true; playerOnShip = (Ship)collidedWith; if (playerOnShip.sinking) { playerOnShip = null; } } } else if (collidedWith.GetType().BaseType == typeof(Gusto.Models.Animated.Npc)) { Npc enemy = (Npc)collidedWith; colliding = false; if (enemy.inCombat) { showHealthBar = true; health -= enemy.damage; } } else if (collidedWith is IWalks || collidedWith is IGroundObject || collidedWith is IPlaceable) { colliding = false; } else if (collidedWith is IStructure) { colliding = false; playerNearStructure = (Structure)collidedWith; } else if (collidedWith is IAmmo) { Ammo ball = (Ammo)collidedWith; showHealthBar = true; if (!ball.exploded) { health -= ball.groundDamage; } return; } }
public void Update(KeyboardState kstate, GameTime gameTime, Camera camera, List <InventoryItem> actionInventory) { timeSinceLastExpClean += gameTime.ElapsedGameTime.Milliseconds; // clean shots foreach (var shot in Shots) { shot.Update(gameTime); } if (timeSinceLastExpClean > millisecondsExplosionLasts) { // remove exploded shots for (int i = 0; i < Shots.Count; i++) { if (Shots[i].exploded || Shots[i].outOfRange) { Shots.RemoveAt(i); } } timeSinceLastExpClean = 0; } // lighting items if (emittingLight != null) { // toggle light if (kstate.IsKeyDown(Keys.T)) { msToggleButtonHit += gameTime.ElapsedGameTime.Milliseconds; if (msToggleButtonHit > 500) // toggle time 500ms { emittingLight.lit = !emittingLight.lit; msToggleButtonHit = 0; } } emittingLight.Update(kstate, gameTime, GetBoundingBox().Center.ToVector2()); } aiming = false; // aiming if (Mouse.GetState().LeftButton == ButtonState.Pressed) { timeSinceLastShot += gameTime.ElapsedGameTime.Milliseconds; float percentReloaded = timeSinceLastShot / millisecondsNewShot; aiming = true; startAimLine = GetBoundingBox().Center.ToVector2(); Vector2 mousePos = new Vector2(Mouse.GetState().X, Mouse.GetState().Y); Vector2 clickPos = mousePos - new Vector2(GameOptions.PrefferedBackBufferWidth / 2, GameOptions.PrefferedBackBufferHeight / 2) + camera.Position; Vector2 reloadSpot = new Vector2(((1 - percentReloaded) * startAimLine.X + (percentReloaded * clickPos.X)), ((1 - percentReloaded) * startAimLine.Y + (percentReloaded * clickPos.Y))); var lineDistanceFull = PhysicsUtility.VectorMagnitude(clickPos.X, startAimLine.X, clickPos.Y, startAimLine.Y); var lineDistanceReload = PhysicsUtility.VectorMagnitude(reloadSpot.X, startAimLine.X, reloadSpot.Y, startAimLine.Y); // max range float disRatio = shotRange / lineDistanceFull; Vector2 maxPos = new Vector2(((1 - disRatio) * startAimLine.X + (disRatio * clickPos.X)), ((1 - disRatio) * startAimLine.Y + (disRatio * clickPos.Y))); // shot offset from mount float shotOffsetRatio = 30 / lineDistanceFull; shotOffsetPos = new Vector2(((1 - shotOffsetRatio) * startAimLine.X + (shotOffsetRatio * clickPos.X)), ((1 - shotOffsetRatio) * startAimLine.Y + (shotOffsetRatio * clickPos.Y))); // restrict aiming by shotRange if (lineDistanceFull > shotRange) { endAimLineFull = maxPos; } else { endAimLineFull = clickPos; } if (lineDistanceReload > lineDistanceFull || lineDistanceReload > shotRange) { endAimLineReload = endAimLineFull; } else { endAimLineReload = reloadSpot; } // rotate the mount float angleFull = (float)Math.Atan2(edgeFull.Y, edgeFull.X); rotation = angleFull + ((float)Math.PI / 2); } else { aiming = false; } // shooting if (aiming && kstate.IsKeyDown(Keys.Space) && timeSinceLastShot > millisecondsNewShot) { animateShot = true; // loading ammo if (ammoLoaded == null || actionInventory[ammoLoadedIndex] == null || actionInventory[ammoLoadedIndex].bbKey != ammoLoaded.bbKey) // ran out of ammo, or switched ammo type. Reload { for (int i = 0; i < actionInventory.Count; i++) { var item = actionInventory[i]; if (item != null && item is IShipAmmoItem && ammoItemType == item.GetType()) // TODO: selects the first item in ship action inv to shoot { if (item.amountStacked > 0) { ammoLoaded = item; ammoLoadedIndex = i; IShipAmmoItem sai = (IShipAmmoItem)item; firedAmmoKey = sai.GetFiredAmmoKey(); } break; } } } else { Ammo shot = (Ammo)ItemUtility.CreateItem(firedAmmoKey, teamType, regionKey, shotOffsetPos, _content, _graphics); float angleFull = (float)Math.Atan2(edgeFull.Y, edgeFull.X); shot.rotation = angleFull + ((float)Math.PI / 2); shot.SetFireAtDirection(endAimLineFull, RandomEvents.rand.Next(10, 25), 0); shot.moving = true; Shots.Add(shot); timeSinceLastShot = 0; ammoLoaded.amountStacked -= 1; if (ammoLoaded.amountStacked <= 0) { ammoLoaded = null; } } } if (animateShot) { msAnimateShot += gameTime.ElapsedGameTime.Milliseconds; if (msAnimateShot > 70) { currColumnFrame++; msAnimateShot = 0; } if (currColumnFrame >= nColumns) { currColumnFrame = 0; animateShot = false; } } }
public void Update(KeyboardState kstate, GameTime gameTime, Camera camera) { timeSinceLastExpClean += gameTime.ElapsedGameTime.Milliseconds; // clean shots foreach (var shot in Shots) { shot.Update(gameTime); } if (timeSinceLastExpClean > millisecondsExplosionLasts) { // remove exploded shots for (int i = 0; i < Shots.Count; i++) { if (Shots[i].exploded || Shots[i].outOfRange) { Shots.RemoveAt(i); } } timeSinceLastExpClean = 0; } // lighting items if (emittingLight != null) { // toggle light if (kstate.IsKeyDown(Keys.T)) { msToggleButtonHit += gameTime.ElapsedGameTime.Milliseconds; if (msToggleButtonHit > 500) // toggle time 500ms { emittingLight.lit = !emittingLight.lit; msToggleButtonHit = 0; } } emittingLight.Update(kstate, gameTime, GetBoundingBox().Center.ToVector2()); } usingItem = false; if (Mouse.GetState().LeftButton == ButtonState.Pressed) { currColumnFrame = 0; // aiming if (this is IRanged) { currColumnFrame = 1; timeSinceLastShot += gameTime.ElapsedGameTime.Milliseconds; Vector2?shotDirection = null; int shotOffsetX = 0; int shotOffsetY = 0; bool shootHorz = false; if (currRowFrame == 1 || currRowFrame == 2) { shootHorz = true; } // diagnoal aim Vector2 mousePos = new Vector2(Mouse.GetState().X, Mouse.GetState().Y); Vector2 clickPos = mousePos - new Vector2(GameOptions.PrefferedBackBufferWidth / 2, GameOptions.PrefferedBackBufferHeight / 2) + camera.Position; int shootMiddleHoverRangeHalf = 40; bool shootLeft = false; // these are not player directional, but used for setting the ammo shot dir with respect to the rotation of gun bool shootRight = false; bool shootUp = false; bool shootDown = false; if (shootHorz) { if (clickPos.Y < GetBoundingBox().Center.ToVector2().Y - shootMiddleHoverRangeHalf) { if (currRowFrame == 1) // right { rotation = -0.5f; } else { rotation = 0.5f; } shootUp = true; } else if (clickPos.Y > GetBoundingBox().Center.ToVector2().Y + shootMiddleHoverRangeHalf) { if (currRowFrame == 1) { rotation = 0.5f; } else { rotation = -0.5f; } shootDown = true; } else { rotation = 0; } } else { if (clickPos.X < GetBoundingBox().Center.ToVector2().X - shootMiddleHoverRangeHalf) { if (currRowFrame == 0) // down { rotation = 0.5f; } else { rotation = -0.7f; } shootLeft = true; } else if (clickPos.X > GetBoundingBox().Center.ToVector2().X + shootMiddleHoverRangeHalf) { if (currRowFrame == 0) { rotation = -0.7f; } else { rotation = 0.7f; } shootRight = true; } else { rotation = 0; } } // shooting if (kstate.IsKeyDown(Keys.Space) && timeSinceLastShot > millisecondsNewShot) { if (ammoLoaded != null) { Vector2 shotStart = new Vector2(GetBoundingBox().Center.ToVector2().X + shotOffsetX, GetBoundingBox().Center.ToVector2().Y + shotOffsetY); Ammo shot = (Ammo)ItemUtility.CreateItem(ammoTypeKey, TeamType.Player, regionKey, shotStart, _content, _graphics); if (shootHorz) { // aiming straight if (!shootUp && !shootDown) { if (currRowFrame == 1) // right { shot.rotation = (shot is IDirectionalAmmo) ? 1.5f : 0; shotDirection = new Vector2((int)shot.GetBoundingBox().Center.ToVector2().X + shotRange, (int)shot.GetBoundingBox().Center.ToVector2().Y); } else { shot.rotation = (shot is IDirectionalAmmo) ? -1.5f : 0; shotDirection = new Vector2((int)shot.GetBoundingBox().Center.ToVector2().X - shotRange, (int)shot.GetBoundingBox().Center.ToVector2().Y); } } else { // angled if (shootUp) { if (currRowFrame == 1) { shot.rotation = (shot is IDirectionalAmmo) ? 0.75f : 0; shotDirection = new Vector2((int)(shot.GetBoundingBox().Center.ToVector2().X + (shotRange * PhysicsUtility.sin45deg)), (int)(shot.GetBoundingBox().Center.ToVector2().Y - (shotRange * PhysicsUtility.sin45deg))); } else { shot.rotation = (shot is IDirectionalAmmo) ? -0.75f : 0; shotDirection = new Vector2((int)(shot.GetBoundingBox().Center.ToVector2().X - (shotRange * PhysicsUtility.sin45deg)), (int)(shot.GetBoundingBox().Center.ToVector2().Y - (shotRange * PhysicsUtility.sin45deg))); } } else { if (currRowFrame == 1) { shot.rotation = (shot is IDirectionalAmmo) ? 2.25f : 0; shotDirection = new Vector2((int)(shot.GetBoundingBox().Center.ToVector2().X + (shotRange * PhysicsUtility.sin45deg)), (int)(shot.GetBoundingBox().Center.ToVector2().Y + (shotRange * PhysicsUtility.sin45deg))); } else { shot.rotation = (shot is IDirectionalAmmo) ? -2.25f : 0; shotDirection = new Vector2((int)(shot.GetBoundingBox().Center.ToVector2().X - (shotRange * PhysicsUtility.sin45deg)), (int)(shot.GetBoundingBox().Center.ToVector2().Y + (shotRange * PhysicsUtility.sin45deg))); } } } } else { // aiming straight if (!shootLeft && !shootRight) { if (currRowFrame == 0) // down { shot.rotation = (shot is IDirectionalAmmo) ? 3.1f : 0; shotDirection = new Vector2((int)shot.GetBoundingBox().Center.ToVector2().X, (int)shot.GetBoundingBox().Center.ToVector2().Y + shotRange); } else { shotDirection = new Vector2((int)shot.GetBoundingBox().Center.ToVector2().X, (int)shot.GetBoundingBox().Center.ToVector2().Y - shotRange); } } else { // angled if (shootLeft) { if (currRowFrame == 0) { shot.rotation = (shot is IDirectionalAmmo) ? 3.8f : 0; shotDirection = new Vector2((int)(shot.GetBoundingBox().Center.ToVector2().X - (shotRange * PhysicsUtility.sin45deg)), (int)(shot.GetBoundingBox().Center.ToVector2().Y + (shotRange * PhysicsUtility.sin45deg))); } else { shot.rotation = (shot is IDirectionalAmmo) ? -0.75f : 0; shotDirection = new Vector2((int)(shot.GetBoundingBox().Center.ToVector2().X - (shotRange * PhysicsUtility.sin45deg)), (int)(shot.GetBoundingBox().Center.ToVector2().Y - (shotRange * PhysicsUtility.sin45deg))); } } else { if (currRowFrame == 0) { shot.rotation = (shot is IDirectionalAmmo) ? 2.25f : 0; shotDirection = new Vector2((int)(shot.GetBoundingBox().Center.ToVector2().X + (shotRange * PhysicsUtility.sin45deg)), (int)(shot.GetBoundingBox().Center.ToVector2().Y + (shotRange * PhysicsUtility.sin45deg))); } else { shot.rotation = (shot is IDirectionalAmmo) ? 0.75f : 0; shotDirection = new Vector2((int)(shot.GetBoundingBox().Center.ToVector2().X + (shotRange * PhysicsUtility.sin45deg)), (int)(shot.GetBoundingBox().Center.ToVector2().Y - (shotRange * PhysicsUtility.sin45deg))); } } } } shot.SetFireAtDirection(shotDirection.Value, RandomEvents.rand.Next(10, 25), 0); shot.moving = true; Shots.Add(shot); ammoLoaded.amountStacked -= 1; if (ammoLoaded.amountStacked == 0) { ammoLoaded = null; } timeSinceLastShot = 0; } } } // lighting on any handheld elements if (emittingLight != null) { emittingLight.scaleSize = emittingLight.baseSize * 1.4f; } } else if (inCombat) { if (emittingLight != null) { emittingLight.scaleSize = emittingLight.baseSize; } usingItem = true; if (nextFrame) { currColumnFrame++; if (currColumnFrame == nColumns) { currColumnFrame = 0; } } if (this is IRanged) { currColumnFrame = 1; } } nextFrame = false; SpatialBounding.SetQuad(GetBase()); }