public void CheckCollision(GameObject other) { if (CollisionBox.Intersects(other.CollisionBox)) { OnCollision(other); } }
private void CheckCollision() { foreach (Collider other in GameWorld.Instance.Colliders) { if (other != this) { if (other != null) { if (CollisionBox.Intersects(other.CollisionBox)) { if (!otherColliders.Contains(other)) { otherColliders.Add(this); GameObject.OnCollisionEnter(other); other.GameObject.OnCollisionEnter(this); } } } else { if (otherColliders.Contains(this)) { otherColliders.Remove(this); GameObject.OnCollisionExit(this); } } } } }
public void ResolveCollision(ICollidable collidable) { if (!IsDead) { if (collidable is Bullet) { if (CollisionBox.Intersects(collidable.CollisionBox)) { Die(); } } } }
public void Update(GameTime gameTime) { _cursorPosition = new Vector2(Mouse.GetState().X, Mouse.GetState().Y); _target = (Actor)(JustCombat.TargetingSystem.GetCurrentTarget()); _playerInfoCard.Update(gameTime); if (_target != null) { _targetInfoCard.Update(gameTime); } //CoolDownTimer.Update(gameTime); //MapScrollTransformBounds; _experienceBar.Update(gameTime); foreach (Actor actor in JustCombat.EntityContainer) { int width = (int)(actor.GetWidth()); int height = (int)(actor.GetHeight()); Vector2 oldPosition = new Vector2(actor.GetX(), actor.GetY()); Vector2 newPosition = JustCombat.WorldCamera.WorldToScreen(oldPosition); CollisionBox candidate = new CollisionBox(newPosition.X, newPosition.Y, width, height); if (candidate.Intersects(ViewContainerBounds)) { if (_actorList.Contains(actor)) { continue; } else { _actorList.Add(actor); } } else { _actorList.Remove(actor); } } }
public static bool BeyondMapScrollTransformBounds(int[] positions, int width, int height) { bool result = true; int futureX = positions[0]; int futureY = positions[1]; CollisionBox candidate = new CollisionBox(futureX, futureY, width, height); CollisionBox boundary = UserInterface.ScrollTransformBounds; if (candidate.Intersects(boundary)) { result = false; } return(result); }
//Handle interactions with the player public override void Update(GameTime _gameTime, Map _map, Player _player) { SavePosition.X = Position.X; SavePosition.Y = Position.Y; if (direction == Direction.Right) { Position.X += 1; } if (direction == Direction.Left) { Position.X -= 1; } bool positiveChange = SavePosition.X < Position.X; float newPosition; TileType collisionType; bool collisionH = CollisionDetection.IsColliding(_map, CollisionBox, positiveChange, true, out newPosition, out collisionType); if (collisionH) { Position.X = newPosition; } Position.Y += fallSpeed; positiveChange = SavePosition.Y < Position.Y; TileType collisionTypeVer; bool collisionV = CollisionDetection.IsColliding(_map, CollisionBox, positiveChange, false, out newPosition, out collisionTypeVer); collisionType = (collisionType == TileType.Water ? TileType.Water : collisionTypeVer); if (!collisionV) { isFalling = true; if (collisionType == TileType.Water) { fallSpeed = Math.Max(fallSpeed + (Gravity / 3f), -6f); fallSpeed = Math.Min(fallSpeed + (Gravity / 3f), 2f); } else { fallSpeed = Math.Min(fallSpeed + Gravity, 10f); } } else { Position.Y = newPosition; if (collisionType == TileType.Water) { fallSpeed = Math.Max(fallSpeed + (Gravity / 3f), -6f); fallSpeed = Math.Min(fallSpeed + (Gravity / 3f), 2f); } else { fallSpeed = Gravity; isFalling = false; } } float positionXAhead = Position.X + (direction == Direction.Right ? Width / 2 : -Width / 2); TileType type = TileType.None; float newValue = 0f; if (!isFalling) { if (collisionH || !CollisionDetection.IsColliding(_map, new Rectangle((int)positionXAhead, (int)Position.Y + 1, CollisionBox.Width, CollisionBox.Height), true, false, out newValue, out type)) { //if(collisionH) // { direction = direction == Direction.Right ? Direction.Left : Direction.Right; //} current = animations.GetAnimation($"{direction}_Walk"); } current = animations.GetAnimation($"{direction}_Walk"); } else { current = animations.GetAnimation($"{direction}"); } if (CollisionBox.Intersects(_player.CollisionBox)) { if (CollisionDetection.IsAbove(_player.CollisionBox, CollisionBox)) { IsDead = true; _player.Bounce(); } else { _player.TakeDamage(); } } Update(_gameTime); }