public void Initialize() { if (levelFile == null) { return; } MovableElements = new List <IMoveable>(); updateableElements = new List <IUpdateableObject>(); drawableElements = new List <IDrawableObject>(); List <string> row = new List <string>(levelFile[0].Split(',')); int width = row.Count; int height = levelFile.Count; Tiles = new ICollisionable[width, height]; for (int j = 0; j < height; j++) { row = new List <string>(levelFile[j].Split(',')); for (int i = 0; i < width; i++) { Vector2 position = CalculatePositionWithCell(i, j); Entity entity; EntityType entityType = EntityType.BlockCenter; if (i != 0 && j != 0 && i != width - 1 && j != height - 1) { if (i < row.Count) { if (!resources.StringType.ContainsKey(row[i])) { continue; } entityType = resources.StringType[row[i]]; } } entity = resources.EntityTypeCreator[entityType].FactoryMethod(resources, position); if (entity is IUpdateableObject) { updateableElements.Add((IUpdateableObject)entity); } if (entity is IDrawableObject) { drawableElements.Add((IDrawableObject)entity); } if (entity is IMoveable) { MovableElements.Add((IMoveable)entity); } if (!(entity is IMoveable) && entity is ICollisionable) { Tiles[i, j] = (ICollisionable)entity; } if (entity is Player) { Player = (Player)entity; } } } }
public override void OnCollision(ICollisionable collisionableObject) { if (collisionableObject is IDamageable) { ((IDamageable)collisionableObject).LoseLife(Damage); } }
public override void OnCollision(ICollisionable collisionableObject) { if (collisionableObject is Player) { ((Player)collisionableObject).CurrentEntityStates.Add(EntityStates.Win); } }
public void OnCollision(ICollisionable source) { animationWatch = animationWatch ?? Stopwatch.StartNew(); animationWatch.Reset(); animationWatch.Start(); _sprite = SpriteDuck; _size = new Vector2(SpriteDuck.Width / 2f, SpriteDuck.Height / 2f); }
public bool Collide(ICollisionable other) { if (other is CollisionBox box) { if (Y > box.Y && Y < box.Bottom || Bottom > box.Y && Bottom < box.Bottom) //Collision on the X-Axis { if (X == box.Right || Right == box.X) { return(true); } } else if (X > box.X && X < box.Right || Right > box.X && Right < box.Right) //Collision on the Y-Axis { if (Y == box.Bottom || Bottom == box.Y) { return(true); } } } else if (other is CollisionCircle circle) { if (circle.Collide(LeftTop)) { return(true); } if (circle.Collide(RightTop)) { return(true); } if (circle.Collide(LeftBottom)) { return(true); } if (circle.Collide(RightBottom)) { return(true); } if (circle.Position.X > X && circle.Position.X < Right) { return(Collide(circle.Position + new Vector2(0, circle.Radius)) || Collide(circle.Position - new Vector2(0, circle.Radius))); } if (circle.Position.Y > Y && circle.Position.Y < Bottom) { return(Collide(circle.Position + new Vector2(circle.Radius, 0)) || Collide(circle.Position - new Vector2(circle.Radius, 0))); } return(false); } else { throw new NotSupportedException("Unknown Type " + other.GetType().Name); } return(false); }
protected GameObject(GameObject clone) { this.collision = clone.collision; this.Position = clone.Position; this.Size = clone.Size; this.Velocity = clone.Velocity; this.Acceleration = clone.Acceleration; this.friction = clone.friction; }
public override void OnCollision(ICollisionable collisionableObject) { if (Position.Y == range.Max) { return; } if (collisionableObject is IDamageable) { ((IDamageable)collisionableObject).LoseLife(Damage); } }
public bool EstEnCollisionAvecUnObjet(ICollisionable objet) { bool valRetour = false; int i = 0; while (!valRetour && i < Components.Count) { valRetour = objet.EstEnCollision(Components[i]); ++i; } return(valRetour); }
public override void OnCollision(ICollisionable collisionableObject) { if (!Active) { return; } if (collisionableObject is IKeyOwner) { CollisionSound.Play(); ((IKeyOwner)collisionableObject).Keys++; } Active = false; }
public override void OnCollision(ICollisionable collisionableObject) { if (!Active) { return; } if (collisionableObject is IScore) { CollisionSound.Play(); ((IScore)collisionableObject).Score += Score; Active = false; } }
protected Dictionary <CollisionTypes, List <ICollisionable> > GetPlayerBorderCollisions(Player player, Rectangle playerBorderRectangle, Map map) { Dictionary <CollisionTypes, List <ICollisionable> > collisions = new Dictionary <CollisionTypes, List <ICollisionable> >(); int firstXTileToCheck = playerBorderRectangle.X / map.TileSide; firstXTileToCheck = (int)MathHelper.Clamp(firstXTileToCheck, 0, map.Tiles.GetLength(0) - 1); int lastXTileToCheck = (playerBorderRectangle.X + playerBorderRectangle.Width - 1) / map.TileSide; lastXTileToCheck = (int)MathHelper.Clamp(lastXTileToCheck, 0, map.Tiles.GetLength(0) - 1); int firstYTileToCheck = playerBorderRectangle.Y / map.TileSide; firstYTileToCheck = (int)MathHelper.Clamp(firstYTileToCheck, 0, map.Tiles.GetLength(1) - 1); int lastYTileToCheck = (playerBorderRectangle.Y + playerBorderRectangle.Height - 1) / map.TileSide; lastYTileToCheck = (int)MathHelper.Clamp(lastYTileToCheck, 0, map.Tiles.GetLength(1) - 1); for (int i = firstXTileToCheck; i <= lastXTileToCheck; i++) { for (int j = firstYTileToCheck; j <= lastYTileToCheck; j++) { ICollisionable tile = map.Tiles[i, j]; if (tile == null) { continue; } if (playerBorderRectangle.Intersects(tile.HitBox)) { if (tile.CollisionType == CollisionTypes.Block) { if (!collisions.ContainsKey(CollisionTypes.Block)) { collisions.Add(CollisionTypes.Block, new List <ICollisionable>()); } collisions[CollisionTypes.Block].Add(tile); } if (tile.CollisionType == CollisionTypes.None) { if (!collisions.ContainsKey(CollisionTypes.None)) { collisions.Add(CollisionTypes.None, new List <ICollisionable>()); } collisions[CollisionTypes.None].Add(tile); } } } } return(collisions); }
public Projectile(Game game, ICollisionable source, ProjectileOptions options) { _game = game; _source = source; _options = options; Position = new Position { Current = options.StartPosition, Destination = options.StartPosition }; _size = new Vector2(options.Sprite.Width / 2f, options.Sprite.Height / 2f); _tweenMovement = new TweenMovement(this); if (options.Collision.Type == ProjectileCollisionType.SpecificTarget) { _tweenMovement.SetPosition(options.Collision.SpecificTarget, options.MovementSpeed); } }
public bool Intersects(ICollisionable other) { if (other is CollisionCircle circle) { Vector2 dif = circle.Position - Position; return(dif.LengthSquared() < Radius * Radius + circle.Radius * Radius); } else if (other is CollisionBox box) { if (box.Intersects(Position)) { return(true); } if (Intersects(box.LeftTop)) { return(true); } if (Intersects(box.RightTop)) { return(true); } if (Intersects(box.LeftBottom)) { return(true); } if (Intersects(box.RightBottom)) { return(true); } if (Position.X > box.X && Position.X < box.Right) { return(box.Intersects(Position + new Vector2(0, Radius)) || box.Intersects(Position - new Vector2(0, Radius))); } if (Position.Y > box.Y && Position.Y < box.Bottom) { return(box.Intersects(Position + new Vector2(Radius, 0)) || box.Intersects(Position - new Vector2(Radius, 0))); } return(false); } else { throw new NotImplementedException("Unknown Type: " + other.GetType().Name); } }
public List <Point> GetEntityCoordinatesOnMap(ICollisionable entity) { int firstX = entity.HitBox.X / TileSide; int firstY = entity.HitBox.Y / TileSide; int lastX = (entity.HitBox.X + entity.HitBox.Width - 1) / TileSide; int lastY = (entity.HitBox.Y + entity.HitBox.Height - 1) / TileSide; List <Point> points = new List <Point>(); for (int i = firstX; i <= lastX; i++) { for (int j = firstY; j <= lastY; j++) { points.Add(new Point(i, j)); } } return(points); }
public bool CheckCollision() { ICollisionable result = null; if (_options.Collision.Type == ProjectileCollisionType.SpecificTarget) { var target = _options.Collision.SpecificTarget; if (target == null) { Remove(); return(false); } if (target.Position.Current != Position.Current) { return(false); } result = target; } if (_options.Collision.Type == ProjectileCollisionType.FirstHit) { foreach (var entity in _game.Collisionables.Keys.Where(w => w != this && w != _source)) { var bounds = entity.GetBounds(); if (!Collisions.IsColliding(GetBounds(), bounds)) { continue; } result = entity; } } if (result == null) { return(false); } CollidingWith = result; result.OnCollision(this); OnCollision(result); Remove(); return(true); }
private bool CheckPlayerTilesCollisions(Rectangle playerRectangle) { int firstXTileToCheck = playerRectangle.X / TileSide; firstXTileToCheck = (int)MathHelper.Clamp(firstXTileToCheck, 0, Tiles.GetLength(0) - 1); int lastXTileToCheck = (playerRectangle.X + playerRectangle.Width) / TileSide; lastXTileToCheck = (int)MathHelper.Clamp(lastXTileToCheck, 0, Tiles.GetLength(0) - 1); int firstYTileToCheck = playerRectangle.Y / TileSide; firstYTileToCheck = (int)MathHelper.Clamp(firstYTileToCheck, 0, Tiles.GetLength(1) - 1); int lastYTileToCheck = (playerRectangle.Y + playerRectangle.Height) / TileSide; lastYTileToCheck = (int)MathHelper.Clamp(lastYTileToCheck, 0, Tiles.GetLength(1) - 1); for (int i = firstXTileToCheck; i <= lastXTileToCheck; i++) { for (int j = firstYTileToCheck; j <= lastYTileToCheck; j++) { if (Tiles[i, j] == null) { continue; } ICollisionable tile = Tiles[i, j]; if (tile.CollisionType == CollisionTypes.Block) { if (playerRectangle.Intersects(tile.HitBox)) { return(true); } } if (tile.CollisionType == CollisionTypes.Top) { int playerBottomY = Player.HitBox.Y + Player.HitBox.Height - 1; if ((playerBottomY < tile.HitBox.Y) && playerRectangle.Intersects(tile.HitBox)) { return(true); } } } } return(false); }
protected void AddMovableElementsForCollision(Map map, Player player, Vector2 oldPosition, ref List <ICollisionable>[,] collisions) { Rectangle rectangle = Rectangle.Union(player.BoundingBox.BoxInPosition(oldPosition), player.BoundingBox.BoxInPosition(player.Position)); for (int k = 0; k < map.MovableElements.Count; k++) { IMoveable movable = map.MovableElements[k]; if (!(movable is ICollisionable)) { continue; } if (movable == player) { continue; } ICollisionable collisionableObject = movable as ICollisionable; if (collisionableObject.HitBox.Intersects(rectangle)) { List <Point> coordinates = map.GetEntityCoordinatesOnMap(collisionableObject); for (int l = 0; l < coordinates.Count; l++) { int i = coordinates[l].X; int j = coordinates[l].Y; if (i > collisions.GetLength(0) - 1 || j > collisions.GetLength(1) - 1) { continue; } if (collisions[i, j] == null) { collisions[i, j] = new List <ICollisionable>(); } collisions[i, j].Add(collisionableObject); } } } }
public override void Move(ICollisionable entity, Vector2 nextPosition, Map map) { Player player = entity as Player; bool xBlocked = false; Rectangle playerHitBoxInNextPosition = player.BoundingBox.BoxInPosition(nextPosition); int firstXTileToCheck = player.HitBox.X / map.TileSide; firstXTileToCheck = (int)MathHelper.Clamp(firstXTileToCheck, 0, map.Tiles.GetLength(0) - 1); int lastXTileToCheck = (playerHitBoxInNextPosition.X + playerHitBoxInNextPosition.Width - 1) / map.TileSide; lastXTileToCheck = (int)MathHelper.Clamp(lastXTileToCheck, 0, map.Tiles.GetLength(0) - 1); int firstYTileToCheck = player.HitBox.Y / map.TileSide; firstYTileToCheck = (int)MathHelper.Clamp(firstYTileToCheck, 0, map.Tiles.GetLength(1) - 1); int lastYTileToCheck = (player.HitBox.Y + player.HitBox.Height - 1) / map.TileSide; lastYTileToCheck = (int)MathHelper.Clamp(lastYTileToCheck, 0, map.Tiles.GetLength(1) - 1); float nextPlayerPositionX = nextPosition.X; int playerNextRightX = playerHitBoxInNextPosition.X + playerHitBoxInNextPosition.Width; List <ICollisionable>[,] collisions = new List <ICollisionable> [lastXTileToCheck + 1, lastYTileToCheck + 1]; for (int i = firstXTileToCheck; i <= lastXTileToCheck; i++) { for (int j = firstYTileToCheck; j <= lastYTileToCheck; j++) { ICollisionable tile = map.Tiles[i, j]; if (tile == null) { continue; } if (playerNextRightX <= tile.HitBox.X) { continue; } if (tile.CollisionType != CollisionTypes.Top) { if (collisions[i, j] == null) { collisions[i, j] = new List <ICollisionable>(); } collisions[i, j].Add(tile); } if (tile.CollisionType == CollisionTypes.Block) { nextPlayerPositionX = tile.HitBox.X - player.BoundingBox.OffSetRight; lastXTileToCheck = i; xBlocked = true; } } } Vector2 oldPosition = player.Position; player.Position = Vector2.UnitX * nextPlayerPositionX + Vector2.UnitY * player.Position; AddMovableElementsForCollision(map, player, oldPosition, ref collisions); CheckCollisionsRight(firstXTileToCheck, firstYTileToCheck, lastXTileToCheck, lastYTileToCheck, xBlocked, map, collisions, player); }
public static bool Intersects(ICollisionable collisionAble1, ICollisionable collisionAble2, bool coll1flipped, bool coll2flipped, out int highY, out int lowY) { return(IntersectPixels(collisionAble1.CollisionArea, collisionAble1.Mask, collisionAble2.CollisionArea, collisionAble2.Mask, coll1flipped, coll2flipped, out highY, out lowY)); }
public abstract void Move(ICollisionable player, Vector2 nextPosition, Map map);
private void CheckCollisions(ICollisionable proxyA, ICollisionable proxyB) { // ToDo }
public void OnCollision(ICollisionable source) { throw new NotImplementedException(); }
public void OnCollision(ICollisionable collisionableObject) { }
public abstract void OnCollide(ICollisionable other);
public void EnCollisionAvec(ICollisionable i) { }
public override void Move(ICollisionable entity, Vector2 nextPosition, Map map) { Player player = entity as Player; bool xBlocked = false; bool yBlocked = false; Rectangle playerHitBoxInNextPosition = player.BoundingBox.BoxInPosition(nextPosition); int firstXTileToCheck = player.HitBox.X / map.TileSide; firstXTileToCheck = (int)MathHelper.Clamp(firstXTileToCheck, 0, map.Tiles.GetLength(0) - 1); int lastXTileToCheck = (playerHitBoxInNextPosition.X + playerHitBoxInNextPosition.Width) / map.TileSide; lastXTileToCheck = (int)MathHelper.Clamp(lastXTileToCheck, 0, map.Tiles.GetLength(0) - 1); int firstYTileToCheck = player.HitBox.Y / map.TileSide; firstYTileToCheck = (int)MathHelper.Clamp(firstYTileToCheck, 0, map.Tiles.GetLength(1) - 1); int lastYTileToCheck = (playerHitBoxInNextPosition.Y + playerHitBoxInNextPosition.Height) / map.TileSide; lastYTileToCheck = (int)MathHelper.Clamp(lastYTileToCheck, 0, map.Tiles.GetLength(1) - 1); Vector2 nextPlayerPosition = player.Position; int playerRightX = player.HitBox.X + player.HitBox.Width; int playerBottomY = player.HitBox.Y + player.HitBox.Height; int playerNextBottomY = playerHitBoxInNextPosition.Y + playerHitBoxInNextPosition.Height; List <ICollisionable>[,] collisions = new List <ICollisionable> [lastXTileToCheck + 1, lastYTileToCheck + 1]; for (int i = firstXTileToCheck; i <= lastXTileToCheck; i++) { for (int j = firstYTileToCheck; j <= lastYTileToCheck; j++) { ICollisionable tile = map.Tiles[i, j]; if (tile == null) { continue; } Vector2 playerIntermediatePosition; Rectangle hitBoxInNextPositionToCheck; Rectangle borderHitBox; if (playerNextBottomY >= tile.HitBox.Y && playerBottomY <= tile.HitBox.Y) { float intermediatePlayerPositionYToCheck = tile.HitBox.Y - player.BoundingBox.OffSetBottom; playerIntermediatePosition = player.GetIntermediatePositionWithY(nextPosition, intermediatePlayerPositionYToCheck); hitBoxInNextPositionToCheck = player.BoundingBox.BoxInPosition(playerIntermediatePosition); borderHitBox = new Rectangle(hitBoxInNextPositionToCheck.X, hitBoxInNextPositionToCheck.Y + hitBoxInNextPositionToCheck.Height, hitBoxInNextPositionToCheck.Width, 1); if (tile.HitBox.Intersects(borderHitBox)) { if (tile.CollisionType == CollisionTypes.None) { if (collisions[i, j] == null) { collisions[i, j] = new List <ICollisionable>(); } collisions[i, j].Add(tile); } if (tile.CollisionType == CollisionTypes.Block || tile.CollisionType == CollisionTypes.Top) { nextPlayerPosition = playerIntermediatePosition; lastXTileToCheck = (hitBoxInNextPositionToCheck.X + hitBoxInNextPositionToCheck.Width - 1) / map.TileSide; lastYTileToCheck = (hitBoxInNextPositionToCheck.Y + hitBoxInNextPositionToCheck.Height - 1) / map.TileSide; yBlocked = true; } } } if (playerRightX <= tile.HitBox.X) { float intermediatePlayerPositionXToCheck = tile.HitBox.X - player.BoundingBox.OffSetRight; playerIntermediatePosition = player.GetIntermediatePositionWithX(nextPosition, intermediatePlayerPositionXToCheck); hitBoxInNextPositionToCheck = player.BoundingBox.BoxInPosition(playerIntermediatePosition); borderHitBox = new Rectangle(hitBoxInNextPositionToCheck.X + hitBoxInNextPositionToCheck.Width, hitBoxInNextPositionToCheck.Y, 1, hitBoxInNextPositionToCheck.Height); if (tile.HitBox.Intersects(borderHitBox)) { if (tile.CollisionType == CollisionTypes.None) { if (collisions[i, j] == null) { collisions[i, j] = new List <ICollisionable>(); } collisions[i, j].Add(tile); } if (tile.CollisionType == CollisionTypes.Block) { nextPlayerPosition = playerIntermediatePosition; lastXTileToCheck = (hitBoxInNextPositionToCheck.X + hitBoxInNextPositionToCheck.Width - 1) / map.TileSide; lastYTileToCheck = (hitBoxInNextPositionToCheck.Y + hitBoxInNextPositionToCheck.Height - 1) / map.TileSide; xBlocked = true; } } } if (!xBlocked && !yBlocked) { float intermediatePlayerPositionXToCheck = tile.HitBox.X - player.BoundingBox.OffSetRight; playerIntermediatePosition = player.GetIntermediatePositionWithX(nextPosition, intermediatePlayerPositionXToCheck); hitBoxInNextPositionToCheck = player.BoundingBox.BoxInPosition(playerIntermediatePosition); borderHitBox = new Rectangle(hitBoxInNextPositionToCheck.X + hitBoxInNextPositionToCheck.Width, hitBoxInNextPositionToCheck.Y + hitBoxInNextPositionToCheck.Height, 1, 1); if (tile.HitBox.Intersects(borderHitBox)) { if (tile.CollisionType == CollisionTypes.None) { if (collisions[i, j] == null) { collisions[i, j] = new List <ICollisionable>(); } collisions[i, j].Add(tile); } if (tile.CollisionType == CollisionTypes.Block) { nextPlayerPosition = playerIntermediatePosition; lastXTileToCheck = (hitBoxInNextPositionToCheck.X + hitBoxInNextPositionToCheck.Width - 1) / map.TileSide; lastYTileToCheck = (hitBoxInNextPositionToCheck.Y + hitBoxInNextPositionToCheck.Height - 1) / map.TileSide; xBlocked = true; } } } } } if (!xBlocked && !yBlocked) { Vector2 oldPosition = player.Position; player.Position = nextPosition; AddMovableElementsForCollision(map, player, oldPosition, ref collisions); CheckCollisions(firstXTileToCheck, firstYTileToCheck, map, collisions, player); } else { Vector2 oldPosition = player.Position; player.Position = Vector2.UnitX * nextPlayerPosition + Vector2.UnitY * nextPlayerPosition; AddMovableElementsForCollision(map, player, oldPosition, ref collisions); if (xBlocked) { Rectangle playerRightSide = new Rectangle(player.HitBox.X + player.HitBox.Width, player.HitBox.Y, 1, player.HitBox.Height); Dictionary <CollisionTypes, List <ICollisionable> > borderCollisions = GetPlayerBorderCollisions(player, playerRightSide, map); CheckCollisions(firstXTileToCheck, firstYTileToCheck, map, collisions, player, borderCollisions); player.Speed *= Vector2.UnitY; MovePlayerDown(player, nextPosition, map); } else { Rectangle playerBottomSide = new Rectangle(player.HitBox.X, player.HitBox.Y + player.HitBox.Height, player.HitBox.Width, 1); Dictionary <CollisionTypes, List <ICollisionable> > borderCollisions = GetPlayerBorderCollisions(player, playerBottomSide, map); CheckCollisions(firstXTileToCheck, firstYTileToCheck, map, collisions, player, borderCollisions); MovePlayerRight(player, nextPosition, map); } } }
public override void OnCollide(ICollisionable other) { // do some damage and movement stuff now }
public static bool Intersects(ICollisionable collisionAble1, ICollisionable collisionAble2, bool coll1flipped = false, bool coll2flipped = false) { return(IntersectPixels(collisionAble1.CollisionArea, collisionAble1.Mask, collisionAble2.CollisionArea, collisionAble2.Mask, coll1flipped, coll2flipped)); }
public bool CollidedWith(ICollisionable component) { return BoundingBox.Intersects(component.BoundingBox); }
public void OnCollision(ICollisionable source) { _options.Collision.OnCollision?.Invoke(source); Remove(); }
public void EnCollisionAvec(ICollisionable i) { EntitésEnCollision.Add(i); }
public bool CollidedWith(ICollisionable component) { return(BoundingBox.Intersects(component.BoundingBox)); }