/// <summary> /// Iterates through the current Map's lists and checks their BoundingBox's against /// the MainPlayer's to determine if a Collision has occurred. Begins or handles /// collision processes as they occur. /// </summary> public void HandleCollisions(object sender, EventArgs eventArgs) { foreach (MapAreaDefinition _area in Areas) { if (MainPlayer.BoundingBox.Intersects(_area.BoundingBox) == true) { if (_area.Name != ActiveAreaName) { PreviousArea = new Rectangle(ActiveArea.X, ActiveArea.Y, ActiveArea.Width, ActiveArea.Height); ActiveAreaName = _area.Name; SetAreaBoundaries(_area.Name); AreaTransitionCollisionRectangle = Rectangle.Intersect(MainPlayer.BoundingBox, ActiveArea); IsAreaTransitionActive = true; AreaTransitionTimer = 0f; return; } } } foreach (MapCollisionSolidStatic _collisionObject in CollisionObjects) { if (MainPlayer.SolidBoundingBox.Intersects(_collisionObject.BoundingBox)) { switch (_collisionObject.Type) { case "MapCollisionStatic": { Rectangle collisionRectangle = Rectangle.Intersect(MainPlayer.SolidBoundingBox, _collisionObject.BoundingBox); MainPlayer.HasCollided(collisionRectangle); break; } case "MapCollisionWater": { break; } case "MapCollisionPitTrap": { break; } } } } foreach (MapObject _mapTransition in MapObjects) { if (_mapTransition.Type == "mapTransition") { MapTransitionHandler transitionObject = (MapTransitionHandler)_mapTransition; if (transitionObject.BoundingBox.Intersects(MainPlayer.SolidBoundingBox)) { MapTransition = transitionObject; transitionObject.InitializeTransition(MapCurrent, (Rectangle)MapCamera.BoundingRectangle, TransitionSound); IsTransitionActive = true; } } } foreach (MapEntity _mapEntity in Entities) { if (MainPlayer.BoundingBox.Intersects(_mapEntity.BoundingBox) && _mapEntity.IsSolid == true) { // Run Uncollide Code in Player Rectangle collisionRectangle = Rectangle.Intersect(MainPlayer.BoundingBox, _mapEntity.BoundingBox); MainPlayer.HasCollided(collisionRectangle); } } }
/// <summary> /// Loads MapObjects for the Active Map, iterates through them, and instantiates objects /// based on the information found in the Map ObjectLayers /// </summary> private void LoadMapObjectLayers() { TiledMapObjectLayer _mapEntityObjectLayer = MapCurrent.GetLayer <TiledMapObjectLayer>("Entity Layer"); TiledMapObjectLayer _mapCollisionLayer = MapCurrent.GetLayer <TiledMapObjectLayer>("Collision Layer"); TiledMapObjectLayer _mapAreaDefinitionLayer = MapCurrent.GetLayer <TiledMapObjectLayer>("Area Definitions"); foreach (TiledMapObject _entityObject in _mapEntityObjectLayer.Objects) { switch (_entityObject.Name) { case "mapPlayerSpawn": { // Run Player Spawn Code Here PlayerSpawnX = Convert.ToInt32(_entityObject.Position.X); PlayerSpawnY = Convert.ToInt32(_entityObject.Position.Y); IsPlayerSpawn = true; break; } case "mapTransition": { // Create Transition Objects float destinationX, destinationY; destinationX = Convert.ToInt32(_entityObject.Properties["mapDestinationX"]); destinationY = Convert.ToInt32(_entityObject.Properties["mapDestinationY"]); MapTransitionHandler mapTransition = new MapTransitionHandler(contentManager, _entityObject.Properties["mapDestination"], new Vector2((float)destinationX, (float)destinationY), new Rectangle((int)_entityObject.Position.X, (int)_entityObject.Position.Y, (int)_entityObject.Size.Width, (int)_entityObject.Size.Height), _entityObject.Properties["mapDestinationArea"], _entityObject.Properties["mapDestinationFacing"]); MapObjects.Add(mapTransition); break; } case "mapEntitySpawn": { // Get AssetManager Data AnimationLibrary _animationLibrary = AssetManager.GetAnimationLibrary(_entityObject.Properties["AnimationLibraryName"]); // Spawn the Entity bool isSolid = Convert.ToBoolean(_entityObject.Properties["IsSolid"]); MapEntityStatic _mapEntity = new MapEntityStatic(isSolid, new Vector2(_entityObject.Position.X, _entityObject.Position.Y), (int)_entityObject.Size.Width, (int)_entityObject.Size.Height, _entityObject.Properties["AnimationLibraryName"]); _mapEntity.ConstructAnimationLibrary(_animationLibrary.Name, _entityObject.Properties["CurrentAnimation"]); Entities.Add(_mapEntity); break; } } } foreach (TiledMapObject _collisionObject in _mapCollisionLayer.Objects) { switch (_collisionObject.Name) { case "solidStatic": { MapCollisionSolidStatic solid = new MapCollisionSolidStatic(_collisionObject.Position, (int)_collisionObject.Size.Width, (int)_collisionObject.Size.Height); CollisionObjects.Add(solid); break; } } } foreach (TiledMapObject _areaDefintion in _mapAreaDefinitionLayer.Objects) { MapAreaDefinition area = new MapAreaDefinition(_areaDefintion.Name, new Vector2((int)_areaDefintion.Position.X, (int)_areaDefintion.Position.Y), new Rectangle((int)_areaDefintion.Position.X, (int)_areaDefintion.Position.Y, (int)_areaDefintion.Size.Width, (int)_areaDefintion.Size.Height)); Areas.Add(area); } }