private void InitializeCollision() { solidCollision = TestLevel.Collisions.FirstOrDefault(item => item.Name == "Tiles"); roadCollision = TestLevel.Collisions.FirstOrDefault(item => item.Name == "RoadTiles"); foreach (var collision in TestLevel.Collisions) { collision.Visible = true; } var relationship = CollisionManager.Self.CreateTileRelationship(TankList, solidCollision); relationship.SetMoveCollision(0, 1); var bulletVsTank = CollisionManager.Self.CreateRelationship(TankList, BulletList); bulletVsTank.CollisionOccurred = HandleBulletVsTankCollision; roadVsTankRelationship = CollisionManager.Self.CreateTileRelationship(TankList, roadCollision); roadVsTankRelationship.IsActive = false; }
private void InitializeCollision() { solidCollision = new TileShapeCollection(); solidCollision.Visible = false; var layer = currentLevel.MapLayers.First(item => item.Name == "ForegroundLevel"); solidCollision.AddMergedCollisionFromLayer(layer, currentLevel, (propertyList) => { return(propertyList.Any(item => item.Name == "SolidCollision" && (string)item.Value == "true")); }); #if DEBUG if (DebuggingVariables.ShowTerrainCollision) { solidCollision.Visible = true; AboveWaterCollision.Visible = true; } #endif }
public void HandleCollision(TileShapeCollection solidCollision, TileShapeCollection cloudCollision) { var wasOnGround = IsOnGround; CollideAgainst(() => solidCollision.CollideAgainstSolid(this), false); if (!IsFallingThroughClouds) { CollideAgainst(() => cloudCollision.CollideAgainstSolid(this), true); } else if (!cloudCollision.CollideAgainst(this)) { IsFallingThroughClouds = false; } if (!wasOnGround && IsOnGround && LandedAction != null) { LandedAction(); } }
private bool ApplyDesiredDirectionToMovement(Direction desiredDirection, TileShapeCollection collision, PositionedObjectList <Character> characters) { bool movedNewDirection = false; if (isMovingToTile == false && desiredDirection != Direction.None) { float desiredX = this.X; float desiredY = this.Y; bool isBlocked; MoveInDirection(desiredDirection, ref desiredX, ref desiredY); this.ForwardCollision.X = desiredX; this.ForwardCollision.Y = desiredY; isBlocked = GetIfIsBlocked(collision, characters); if (isBlocked) { // move the collision back so it occupies the character's tile this.ForwardCollision.Position = this.Position; } else { float timeToTake = tileSize / MovementSpeed; InstructionManager.MoveToAccurate(this, desiredX, desiredY, this.Z, timeToTake); isMovingToTile = true; this.Call(() => { this.isMovingToTile = false; BackwardCollision.Position = this.Position; UpdateActionCollision(); }).After(timeToTake); movedNewDirection = true; } } return(movedNewDirection); }
public Stage(LayeredTileMap map) { sCollision = new TileShapeCollection(); cCollision = new TileShapeCollection(); var tilesWithCollision = map.TileProperties .Where(item => item.Value.Any(property => property.Name == "HasCollision" && String.Equals((string)property.Value, "true", StringComparison.OrdinalIgnoreCase))) .Where(item => item.Value.Any(property => property.Name == "Is Cloud" && !String.Equals((string)property.Value, "true", StringComparison.OrdinalIgnoreCase) || item.Value.All(property2 => property2.Name != "Is Cloud"))) .Select(item => item.Key).ToList(); var tilesWithCloudCollision = map.TileProperties .Where(item => item.Value.Any(property => property.Name == "HasCollision" && String.Equals((string)property.Value, "true", StringComparison.OrdinalIgnoreCase))) .Where(item => item.Value.Any(property => property.Name == "Is Cloud" && String.Equals((string)property.Value, "true", StringComparison.OrdinalIgnoreCase))) .Select(item => item.Key).ToList(); sCollision.AddCollisionFrom(map, tilesWithCollision); cCollision.AddCollisionFrom(map, tilesWithCloudCollision); sCollision.Visible = false; cCollision.Visible = false; }
private bool GetIfIsBlocked(TileShapeCollection collision, PositionedObjectList <Character> characters) { var isBlocked = collision.CollideAgainst(ForwardCollision); if (!isBlocked) { // If not blocked, check against NPCs foreach (var npc in characters) { // If the NPC is standing still - tests to see if where 'this' is trying to go to // is already occupied by the NPC // If the NPC is walking - tests to see if where 'this' is trying to go is where // the NPC is already going. if (npc != this && ForwardCollision.CollideAgainst(npc.ForwardCollision)) { isBlocked = true; break; } } } return(isBlocked); }
private void AddCollisions() { collisionCollection = new TileShapeCollection(); collisionCollection.Visible = true; collisionCollection.AddCollisionFrom(PlatformerLevel1, "Grass1"); collisionCollection.AddCollisionFrom(PlatformerLevel1, "Grass2"); collisionCollection.AddCollisionFrom(PlatformerLevel1, "Grass3"); collisionCollection.AddCollisionFrom(PlatformerLevel1, "Grass4"); collisionCollection.AddCollisionFrom(PlatformerLevel1, "Grass5"); collisionCollection.AddCollisionFrom(PlatformerLevel1, "Grass6"); collisionCollection.AddCollisionFrom(PlatformerLevel1, "Grass7"); collisionCollection.AddCollisionFrom(PlatformerLevel1, "Grass8"); collisionCollection.AddCollisionFrom(PlatformerLevel1, "Grass9"); collisionCollection.AddCollisionFrom(PlatformerLevel1, "Grass10"); collisionCollection.AddCollisionFrom(PlatformerLevel1, "Grass11"); collisionCollection.AddCollisionFrom(PlatformerLevel1, "Grass12"); collisionCollection.AddCollisionFrom(PlatformerLevel1, "Rock1"); collisionCollection.AddCollisionFrom(PlatformerLevel1, "Rock2"); collisionCollection.AddCollisionFrom(PlatformerLevel1, "Rock3"); collisionCollection.AddCollisionFrom(PlatformerLevel1, "Rock4"); collisionCollection.AddCollisionFrom(PlatformerLevel1, "Rock5"); collisionCollection.AddCollisionFrom(PlatformerLevel1, "Rock6"); collisionCollection.AddCollisionFrom(PlatformerLevel1, "Rock7"); collisionCollection.AddCollisionFrom(PlatformerLevel1, "Rock8"); collisionCollection.AddCollisionFrom(PlatformerLevel1, "WoodPlank1"); collisionCollection.AddCollisionFrom(PlatformerLevel1, "WoodPlank2"); collisionCollection.AddCollisionFrom(PlatformerLevel1, "WoodPlank3"); collisionCollection.AddCollisionFrom(PlatformerLevel1, "WoodPlank4"); collisionCollection.AddCollisionFrom(PlatformerLevel1, "WoodPlank5"); collisionCollection.AddCollisionFrom(PlatformerLevel1, "WoodPlank6"); collisionCollection.AddCollisionFrom(PlatformerLevel1, "Tree1"); collisionCollection.AddCollisionFrom(PlatformerLevel1, "Tree2"); collisionCollection.AddCollisionFrom(PlatformerLevel1, "Tree3"); collisionCollection.AddCollisionFrom(PlatformerLevel1, "Tree4"); collisionCollection.AddCollisionFrom(PlatformerLevel1, "Tree5"); collisionCollection.AddCollisionFrom(PlatformerLevel1, "Tree6"); collisionCollection.AddCollisionFrom(PlatformerLevel1, "Tree7"); collisionCollection.AddCollisionFrom(PlatformerLevel1, "Tree8"); collisionCollection.AddCollisionFrom(PlatformerLevel1, "Tree9"); collisionCollection.AddCollisionFrom(PlatformerLevel1, "Tree10"); collisionCollection.AddCollisionFrom(PlatformerLevel1, "Tree11"); collisionCollection.AddCollisionFrom(PlatformerLevel1, "Tree12"); collisionCollection.AddCollisionFrom(PlatformerLevel1, "Tree13"); collisionCollection.AddCollisionFrom(PlatformerLevel1, "Tree14"); collisionCollection.AddCollisionFrom(PlatformerLevel1, "Tree15"); collisionCollection.AddCollisionFrom(PlatformerLevel1, "Tree16"); collisionCollection.AddCollisionFrom(PlatformerLevel1, "Tree17"); collisionCollection.AddCollisionFrom(PlatformerLevel1, "Tree18"); collisionCollection.AddCollisionFrom(PlatformerLevel1, "Tree19"); collisionCollection.AddCollisionFrom(PlatformerLevel1, "Tree20"); collisionCollection.AddCollisionFrom(PlatformerLevel1, "Tree21"); collisionCollection.AddCollisionFrom(PlatformerLevel1, "Tree22"); collisionCollection.AddCollisionFrom(PlatformerLevel1, "Tree23"); collisionCollection.AddCollisionFrom(PlatformerLevel1, "Tree24"); collisionCollection.AddCollisionFrom(PlatformerLevel1, "Tree25"); collisionCollection.AddCollisionFrom(PlatformerLevel1, "Tree26"); collisionCollection.AddCollisionFrom(PlatformerLevel1, "Tree27"); collisionCollection.AddCollisionFrom(PlatformerLevel1, "Tree28"); climbableCollection = new TileShapeCollection(); climbableCollection.Visible = true; climbableCollection.AddCollisionFrom(PlatformerLevel1, "Vine1"); climbableCollection.AddCollisionFrom(PlatformerLevel1, "Vine2"); climbableCollection.AddCollisionFrom(PlatformerLevel1, "Vine3"); climbableCollection.AddCollisionFrom(PlatformerLevel1, "Vine4"); climbableCollection.AddCollisionFrom(PlatformerLevel1, "Vine5"); climbableCollection.AddCollisionFrom(PlatformerLevel1, "Vine6"); climbableCollection.AddCollisionFrom(PlatformerLevel1, "Vine7"); climbableCollection.AddCollisionFrom(PlatformerLevel1, "Vine8"); climbableCollection.AddCollisionFrom(PlatformerLevel1, "Vine9"); climbableCollection.AddCollisionFrom(PlatformerLevel1, "Vine10"); climbableCollection.AddCollisionFrom(PlatformerLevel1, "Vine11"); climbableCollection.AddCollisionFrom(PlatformerLevel1, "Vine12"); climbableCollection.AddCollisionFrom(PlatformerLevel1, "Vine13"); climbableCollection.AddCollisionFrom(PlatformerLevel1, "Vine14"); climbableCollection.AddCollisionFrom(PlatformerLevel1, "Vine15"); climbableCollection.AddCollisionFrom(PlatformerLevel1, "Vine16"); climbableCollection.AddCollisionFrom(PlatformerLevel1, "Vine17"); climbableCollection.AddCollisionFrom(PlatformerLevel1, "Vine18"); climbableCollection.AddCollisionFrom(PlatformerLevel1, "Vine19"); climbableCollection.AddCollisionFrom(PlatformerLevel1, "Vine20"); climbableCollection.AddCollisionFrom(PlatformerLevel1, "Vine21"); climbableCollection.AddCollisionFrom(PlatformerLevel1, "Vine22"); climbableCollection.AddCollisionFrom(PlatformerLevel1, "Vine23"); climbableCollection.AddCollisionFrom(PlatformerLevel1, "Vine24"); damagingCollection = new TileShapeCollection(); damagingCollection.AddCollisionFrom(PlatformerLevel1, "Spike1"); damagingCollection.AddCollisionFrom(PlatformerLevel1, "Spike2"); damagingCollection.AddCollisionFrom(PlatformerLevel1, "Spike3"); damagingCollection.AddCollisionFrom(PlatformerLevel1, "Spike4"); damagingCollection.AddCollisionFrom(PlatformerLevel1, "Spike5"); damagingCollection.AddCollisionFrom(PlatformerLevel1, "Spike6"); damagingCollection.AddCollisionFrom(PlatformerLevel1, "Spike7"); damagingCollection.AddCollisionFrom(PlatformerLevel1, "Spike8"); }
public CollidableListVsTileShapeCollectionRelationship(PositionedObjectList <FirstCollidableT> list, TileShapeCollection tileShapeCollection) { data = new CollidableVsTileShapeCollectionData <FirstCollidableT>(tileShapeCollection); this.list = list; }
public CollidableVsTileShapeCollectionRelationship(FirstCollidableT singleObject, TileShapeCollection tileShapeCollection) { data = new CollidableVsTileShapeCollectionData <FirstCollidableT>(tileShapeCollection); this.singleObject = singleObject; }
private void BulletCollidedWithTerrain(Bullet b, TileShapeCollection Tiles) { b.HitTheTerrain(); }
private void HandlePlayerCollisionWall(Player player, TileShapeCollection tileShapeCollection) { FlatRedBall.Debugging.Debugger.Write("Collided with wall..."); }
public CollidableVsTileShapeCollectionData(TileShapeCollection tileShapeCollection) { this.tileShapeCollection = tileShapeCollection; }
private bool DoFirstCollisionLineVsShapeCollection(Line line, TileShapeCollection tileShapeCollection) { line.LastCollisionPoint = new Point(double.NaN, double.NaN); Segment a = line.AsSegment(); if (tileShapeCollection.SortAxis == Axis.X) { var leftmost = (float)System.Math.Min(line.AbsolutePoint1.X, line.AbsolutePoint2.X); var rightmost = (float)System.Math.Max(line.AbsolutePoint1.X, line.AbsolutePoint2.X); float clampedPosition = line.Position.X; bool isPositionOnEnd = false; if (clampedPosition <= leftmost) { clampedPosition = leftmost; isPositionOnEnd = true; } else if (clampedPosition >= rightmost) { clampedPosition = rightmost; isPositionOnEnd = true; } // only support rectangles for now (maybe forever) var rectangles = tileShapeCollection.Rectangles; var firstIndex = rectangles.GetFirstAfter(leftmost - tileShapeCollection.GridSize, Axis.X, 0, rectangles.Count); var lastIndex = rectangles.GetFirstAfter(rightmost + tileShapeCollection.GridSize, Axis.X, firstIndex, rectangles.Count); if (isPositionOnEnd) { FlatRedBall.Math.Geometry.AxisAlignedRectangle collidedRectangle = null; Point?intersectionPoint = null; if (clampedPosition < rightmost) { // start at the beginning of the list, go up for (int i = firstIndex; i < lastIndex; i++) { var rectangle = tileShapeCollection.Rectangles[i]; if (collidedRectangle != null) { if (rectangle.X > collidedRectangle.X) { break; } if (rectangle.Y > collidedRectangle.Y && collidedRectangle.Y > line.Position.Y) { break; } if (rectangle.Y < collidedRectangle.Y && collidedRectangle.Y < line.Position.Y) { break; } } Point tl = new Point( rectangle.Position.X - rectangle.ScaleX, rectangle.Position.Y + rectangle.ScaleY); Point tr = new Point( rectangle.Position.X + rectangle.ScaleX, rectangle.Position.Y + rectangle.ScaleY); Point bl = new Point( rectangle.Position.X - rectangle.ScaleX, rectangle.Position.Y - rectangle.ScaleY); Point br = new Point( rectangle.Position.X + rectangle.ScaleX, rectangle.Position.Y - rectangle.ScaleY); Point tempPoint; // left gets priority // left var intersects = a.Intersects(new Segment(tl, bl), out tempPoint); if (rectangle.Y > line.Y) { // bottom gets priority over top if (!intersects) { // bottom intersects = a.Intersects(new Segment(bl, br), out tempPoint); } if (!intersects) { // top intersects = a.Intersects(new Segment(tl, tr), out tempPoint); } } else { // top gets priority over top if (!intersects) { // top intersects = a.Intersects(new Segment(tl, tr), out tempPoint); } if (!intersects) { // bottom intersects = a.Intersects(new Segment(bl, br), out tempPoint); } } if (!intersects) { // right intersects = a.Intersects(new Segment(tr, br), out tempPoint); } if (intersects) { intersectionPoint = tempPoint; collidedRectangle = rectangle; } } } else { // start at the end of the list, go down for (int i = lastIndex - 1; i >= firstIndex; i--) { var rectangle = tileShapeCollection.Rectangles[i]; if (collidedRectangle != null) { if (rectangle.X < collidedRectangle.X) { break; } if (rectangle.Y > collidedRectangle.Y && collidedRectangle.Y > line.Position.Y) { break; } if (rectangle.Y < collidedRectangle.Y && collidedRectangle.Y < line.Position.Y) { break; } } Point tl = new Point( rectangle.Position.X - rectangle.ScaleX, rectangle.Position.Y + rectangle.ScaleY); Point tr = new Point( rectangle.Position.X + rectangle.ScaleX, rectangle.Position.Y + rectangle.ScaleY); Point bl = new Point( rectangle.Position.X - rectangle.ScaleX, rectangle.Position.Y - rectangle.ScaleY); Point br = new Point( rectangle.Position.X + rectangle.ScaleX, rectangle.Position.Y - rectangle.ScaleY); Point tempPoint; // right gets priority // right var intersects = a.Intersects(new Segment(tr, br), out tempPoint); if (rectangle.Y > line.Y) { // bottom gets priority over top if (!intersects) { // bottom intersects = a.Intersects(new Segment(bl, br), out tempPoint); } if (!intersects) { // top intersects = a.Intersects(new Segment(tl, tr), out tempPoint); } } else { // top gets priority over top if (!intersects) { // top intersects = a.Intersects(new Segment(tl, tr), out tempPoint); } if (!intersects) { // bottom intersects = a.Intersects(new Segment(bl, br), out tempPoint); } } if (!intersects) { // left intersects = a.Intersects(new Segment(tl, bl), out tempPoint); } if (intersects) { intersectionPoint = tempPoint; collidedRectangle = rectangle; } } } if (collidedRectangle != null) { line.LastCollisionPoint = intersectionPoint ?? new Point(double.NaN, double.NaN); } return(collidedRectangle != null); } else { throw new NotImplementedException("Complain to Vic about this!"); } } else if (tileShapeCollection.SortAxis == Axis.Y) { throw new NotImplementedException("Bug Vic to do Y. Currently just X is done"); } return(false); }
private void HandleLightningVsSolidCollision(Player player, TileShapeCollection tileMap) { player.LightningWeaponManager.HandleCollisionVsSolid(player); }
private void RefreshPath(NodeNetwork nodeNetwork, PositionedObject target, TileShapeCollection solidCollisions, TileShapeCollection pitCollision) { var ai = InputDevice as TopDown.TopDownAiInput <Enemy>; // We are going to pathfind to a location // that is not exactly on the target but on // a point on a line perpendicular to the enemy. // This is a quicky way to add some randomness and // spread out the enemies so they come from different directions var pathfindingTarget = target.Position; var lineToTarget = target.Position - this.Position; var perpendicular = new Vector3(-lineToTarget.Y, lineToTarget.X, 0); if (perpendicular.Length() != 0) { perpendicular.Normalize(); var distanceFromTarget = lineToTarget.Length(); const float distanceToPerpendicularLengthRatio = 1 / 2f; pathfindingTarget = target.Position + perpendicular * perpendicularLengthRatio * distanceToPerpendicularLengthRatio * distanceFromTarget; } if (DebuggingVariables.ShowEnemyAiShapes) { thisToPerpendicularTarget.SetFromAbsoluteEndpoints(this.Position, pathfindingTarget); } var path = nodeNetwork.GetPathOrClosest(ref Position, ref pathfindingTarget); ai.Path.Clear(); var points = path.Select(item => item.Position).ToList(); while (points.Count > 0) { var length = (points[0] - Position).Length(); lineOfSightPathFindingPolygon.SetPoint(0, length / 2.0f, NavigationCollider.Radius); lineOfSightPathFindingPolygon.SetPoint(1, length / 2.0f, -NavigationCollider.Radius); lineOfSightPathFindingPolygon.SetPoint(2, -length / 2.0f, -NavigationCollider.Radius); lineOfSightPathFindingPolygon.SetPoint(3, -length / 2.0f, NavigationCollider.Radius); lineOfSightPathFindingPolygon.SetPoint(4, length / 2.0f, NavigationCollider.Radius); lineOfSightPathFindingPolygon.X = (points[0].X + Position.X) / 2.0f; lineOfSightPathFindingPolygon.Y = (points[0].Y + Position.Y) / 2.0f; var angle = (float)System.Math.Atan2(points[0].Y - Position.Y, points[0].X - Position.X); lineOfSightPathFindingPolygon.RotationZ = angle; var hasClearPath = !solidCollisions.CollideAgainst(lineOfSightPathFindingPolygon) && !pitCollision.CollideAgainst(lineOfSightPathFindingPolygon); if (hasClearPath && points.Count > 1) { points.RemoveAt(0); } else { break; } } ai.Path.AddRange(points); ai.Target = ai.Path.FirstOrDefault(); }
public void DoAiActivity(bool refreshPath, NodeNetwork nodeNetwork, PositionedObjectList <Player> players, PlayerBase playerBase, TileShapeCollection solidCollisions, TileShapeCollection pitCollision) { if (CurrentHP > 0) { DoTargetDecision(players, playerBase); if (refreshPath) { // enemies always move towards player, but really slowly when shooting RefreshPath(nodeNetwork, target, solidCollisions, pitCollision); } UpdateAimingBehavior(target); UpdatePrimaryAction(); UpdateCurrentBehavior(nodeNetwork, target); UpdateCurrentMovementValues(); } }
private void BallGoalCollisionOccurred(Ball ball, TileShapeCollection map) { ball.Destroy(); GoalDisplay.Visible = true; }
/// <summary> /// Performs collision between the argument Line and TileShapeCollection, returning whether collision occurred. The Line's /// LastCollisionPoint will be set to the closest point where collision occurs between the line and the tile shape collection. /// If collision does not occur, LastCollisionPoint will be set to (NaN, NaN) /// </summary> /// <param name="line">The line to perform collision. The "closest point" is the closest point to the line's Position.</param> /// <param name="tileShapeCollection">The TileShapeCollection to collide against.</param> /// <returns></returns> public static bool DoFirstCollisionLineVsShapeCollection(Line line, TileShapeCollection tileShapeCollection) { return(tileShapeCollection.CollideAgainstClosest(line)); }