public void OnTileClick(StageTile tile) { // move the player to that tile, if possible var player = GetComponentInChildren <Player>(); if (player == null || player.TilePos == tile.TilePos) { return; } if (Input.GetKey(KeyCode.LeftShift)) { var spellDir = player.TilePos.GetApproximateDirectionTo(tile.TilePos); if (spellDir.HasValue) { var spell = Spell.Invoke(this, player.TilePos, spellDir.Value); StartCoroutine(AnimateSpellRoutine(spell)); } } else { var space = this[tile.TilePos]; Debug.AssertFormat(space.Tile == tile, "Tile mismatch at {0}", tile.TilePos); if (space.CanPlayerOccupy) { StartCoroutine(player.MoveTo(tile.TilePosTriplet)); } } }
public void InitializeBridge(ContentManager content, Point coordinates, int BridgeLength) { StageTile bridgestart = CurrentStage.getStageTileByWorldPosition(coordinates.X, coordinates.Y); //for (int i = 0; i < currentStage.; i++) for (int i = 0; i < BridgeLength; i++) { bridgepieces.Add(CurrentStage.getStageTileByGridPosition(bridgestart.X + i, bridgestart.Y)); } _explosionSound = content.Load <SoundEffect>("Sounds/Explosion2"); _explosionAnimationStrip = content.Load <Texture2D>("Sprites/Explosion2"); }
public Rectangle?getStairTopBoundsByGridPosition(int x, int y) { StageTile stageTile = null; stageTile = this.getStageTileByGridPosition(x, y); Rectangle?returnValue = null; if (stageTile != null) { if (stageTile.CollisionType == StageTile.TileCollisionType.StairsLeft) { // stairs going up to the left returnValue = new Rectangle(x * TileWidth, (y * TileHeight) + iTilePlatformDropOffset, TileWidth / 2, 4); } else if (stageTile.CollisionType == StageTile.TileCollisionType.StairsRight) { // stairs going up to the right returnValue = new Rectangle((x * TileWidth) + (TileWidth / 2), (y * TileHeight) + iTilePlatformDropOffset, TileWidth / 2, 4); } } return(returnValue); }
public virtual void HandleCollisions(CVGameTime gameTime) { if (this.WorldPosition.Y > Game.iScreenModelWidth && IsDying == false) { Die(gameTime); } Rectangle actorbounds = this.BoundingBox(); bool wasonground = this.IsOnGround; // get nearest tile below player. this.IsOnGround = false; int leftTile = (int)Math.Floor((float)actorbounds.Left / CurrentStage.TileWidth); int rightTile = (int)Math.Ceiling(((float)actorbounds.Right / CurrentStage.TileWidth)) - 1; int topTile = (int)Math.Floor((float)actorbounds.Top / CurrentStage.TileHeight); int bottomTile = (int)Math.Ceiling(((float)actorbounds.Bottom / CurrentStage.TileHeight)) - 1; // For each potentially colliding platform tile, for (int y = topTile; y <= bottomTile; ++y) { for (int x = leftTile; x <= rightTile; ++x) { StageTile stageTile = CurrentStage.getStageTileByGridPosition(x, y); if (stageTile != null) { if (stageTile.IsImpassable()) { Rectangle tilebounds = CurrentStage.getTileBoundsByGridPosition(x, y); Vector2 depth = RectangleExtensions.GetIntersectionDepth(actorbounds, tilebounds); if (actorbounds.Intersects(tilebounds)) { WorldPosition = new Vector2(WorldPosition.X + depth.X, WorldPosition.Y); actorbounds = this.BoundingBox(); } } else if (stageTile.IsPlatform() && y == bottomTile) { List <Platform> tileboundsList = CurrentStage.getTilePlatformBoundsByGridPosition(x, bottomTile); foreach (Platform platformbounds in tileboundsList) { Rectangle tilebounds = platformbounds.PlatformBounds; Vector2 depth = RectangleExtensions.GetIntersectionDepth(actorbounds, tilebounds); if (this.PreviousBottom <= tilebounds.Top && Velocity.Y >= 0 && actorbounds.Intersects(tilebounds)) //if (Velocity.Y >= 0 && (depth.Y < 0)) // || this.IgnoreNextPlatform)) { this.IsOnGround = true; this.JumpInProgress = false; //this.Velocity.X = 0f; this.WorldPosition.Y += depth.Y; // perform further collisions with the new bounds actorbounds = this.BoundingBox(); } } } } } } if (wasonground && !IsOnGround) { Velocity.Y = 0; } this.PreviousBottom = actorbounds.Bottom; }
public void Draw(SpriteBatch spriteBatch, Vector2 cameraPosition) { //Debug.WriteLine("Stage Draw"); for (int i = 0; i < StageTiles.Count; i++) { StageTile t = StageTiles[i]; if (t.X >= ((int)cameraPosition.X / TileWidth) && (t.X <= ((int)cameraPosition.X / TileWidth) + _screenTileWidth)) { int x, y; if (t.Status == StageTile.TileStatus.Active || t.DestructionLayer1GID == 0) { x = (((int)(t.BackgroundGID) - 1) * TileWidth) % _backgroundTileSource[0].Width; y = ((((int)(t.BackgroundGID) - 1) * TileWidth) / _backgroundTileSource[0].Width) * TileHeight; spriteBatch.Draw(_backgroundTileSource[_tileSourceCurrentFrame], new Rectangle((t.X * TileWidth) - (int)cameraPosition.X, t.Y * TileHeight, TileHeight, TileWidth), new Rectangle(x, y, TileWidth, TileHeight), Color.White); } } } foreach (StageObject s in uniqueBackgroundObjects) { if (s.VisibleRectangle().Intersects(this.ScreenCoordinates())) { s.Draw(spriteBatch); } } for (int i = 0; i < StageTiles.Count; i++) { StageTile t = StageTiles[i]; if (t.X >= ((int)cameraPosition.X / TileWidth) && (t.X <= ((int)cameraPosition.X / TileWidth) + _screenTileWidth)) { int x, y; if (t.Status == StageTile.TileStatus.Destroyed) { x = (((int)(t.DestructionLayer1GID) - _destroyedTileSourceBaseGID) * TileWidth) % _destroyedTileSource.Width; y = ((((int)(t.DestructionLayer1GID) - _destroyedTileSourceBaseGID) * TileWidth) / _destroyedTileSource.Width) * TileHeight; spriteBatch.Draw(_destroyedTileSource, new Rectangle((t.X * TileWidth) - (int)cameraPosition.X, t.Y * TileHeight, TileHeight, TileWidth), new Rectangle(x, y, TileWidth, TileHeight), Color.White); } } } for (int i = 0; i < ActiveEnemies.Count; i++) { ActiveEnemies[i].Draw(spriteBatch); } foreach (StageObject s in uniqueForegroundObjects) { if (s.VisibleRectangle().Intersects(this.ScreenCoordinates())) { s.Draw(spriteBatch); } } // Draw the explosions for (int i = 0; i < _explosions.Count; i++) { _explosions[i].Draw(spriteBatch, Player.PlayerDirection.Right, 1f); } const int leftOffset = 5; for (int i = 0; i < Players.Count; i++) { if (Players[i].LifeCount > 1) { Texture2D currentPlayerLifeIcon = currentPlayerLifeIcon = TextureHelper.SwapPlayerColor(_playerLifeIcon, i + 1); for (int j = 1; j <= Math.Min(Players[i].LifeCount, 5) - 1; j++) { spriteBatch.Draw(currentPlayerLifeIcon, new Rectangle(leftOffset + (i * 65) + ((j - 1) * 12), 10, currentPlayerLifeIcon.Width, currentPlayerLifeIcon.Height), Color.White); } } } }
public List <Platform> getTilePlatformBoundsByGridPosition(int x, int y) { ///int iTileID = this.getTileIDByGridPosition(x, y, "Meta"); //bool bHalfDrop = false; StageTile stageTile = null; stageTile = getStageTileByGridPosition(x, y); //for (int i = 0; i < StageTiles.Count; i++) //{ // if ((StageTiles[i].X == x) && (StageTiles[i].Y == y)) // { // stageTile = StageTiles[i]; // break; // //if (StageTiles[i].CollisionType == StageTile.TileCollisionType.PlatformHalfDrop) // // bHalfDrop = true; // } //} List <Platform> returnValue = new List <Platform>(); if (stageTile != null) { if (stageTile.CollisionType == StageTile.TileCollisionType.PlatformHalfDrop) { returnValue.Add( new Platform( new Rectangle(x * TileWidth, (y * TileHeight) + (TileHeight / 2) + iTilePlatformDropOffset, TileWidth, 1), Platform.PlatformTypes.Normal)); } else if (stageTile.CollisionType == StageTile.TileCollisionType.StairsBottomLeft) { returnValue.Add( new Platform( new Rectangle( x * TileWidth, (y * TileHeight) + iTilePlatformDropOffset, TileWidth / 2, 4), Platform.PlatformTypes.StairsBottom) ); returnValue.Add(new Platform(new Rectangle( x * TileWidth, (y * TileHeight) + iTilePlatformDropOffset, TileWidth, 1), Platform.PlatformTypes.Normal)); } else if (stageTile.CollisionType == StageTile.TileCollisionType.StairsBottomRight) { returnValue.Add( new Platform( new Rectangle( (x * TileWidth) + (TileWidth / 2), (y * TileHeight) + iTilePlatformDropOffset, TileWidth / 2, 4), Platform.PlatformTypes.StairsBottom)); returnValue.Add(new Platform(new Rectangle( x * TileWidth, (y * TileHeight) + iTilePlatformDropOffset, TileWidth, 1), Platform.PlatformTypes.Normal)); } else if (stageTile.CollisionType == StageTile.TileCollisionType.StairsLeft) { // stairs going up to the left // upper left returnValue.Add(new Platform(new Rectangle( x * TileWidth, (y * TileHeight) + iTilePlatformDropOffset, TileWidth / 2, 4), Platform.PlatformTypes.Stairs)); // bottom right returnValue.Add(new Platform(new Rectangle( (x * TileWidth) + (TileWidth / 2), (y * TileHeight) + iTilePlatformDropOffset + (TileHeight / 2), TileWidth / 2, 4), Platform.PlatformTypes.Stairs)); } else if (stageTile.CollisionType == StageTile.TileCollisionType.StairsRight) { // stairs going up to the right // bottom left returnValue.Add(new Platform(new Rectangle( x * TileWidth, (y * TileHeight) + iTilePlatformDropOffset + (TileHeight / 2), TileWidth / 2, 4), Platform.PlatformTypes.Stairs)); // upper right returnValue.Add(new Platform(new Rectangle( (x * TileWidth) + (TileWidth / 2), (y * TileHeight) + iTilePlatformDropOffset, TileWidth / 2, 4), Platform.PlatformTypes.Stairs)); } else { returnValue.Add(new Platform(new Rectangle( x * TileWidth, (y * TileHeight) + iTilePlatformDropOffset, TileWidth, 1), Platform.PlatformTypes.Normal)); } } return(returnValue); }
private void InitializeStageTiles(TiledSharp.TmxMap tmx) { MapHeight = tmx.Height; MapWidth = tmx.Width; for (int i = 0; i < tmx.Layers[0].Tiles.Count; i++) { StageTile st = new StageTile(); st.X = tmx.Layers[0].Tiles[i].X; st.Y = tmx.Layers[0].Tiles[i].Y; st.BackgroundGID = tmx.Layers["Background"].Tiles[i].Gid; if (Game.CurrentGame == Game.GameType.Contra) { st.DestructionLayer1GID = tmx.Layers["Destruction1"].Tiles[i].Gid; //st.DestructionLayer1GID = tmx.Layers["Destruction2"].Tiles[i].Gid; } st.MetaGID = tmx.Layers["Meta"].Tiles[i].Gid; st.Status = StageTile.TileStatus.Active; TiledSharp.TmxTilesetTile t = tmx.GetTmxTilesetTileByGID(tmx.Layers["Meta"].Tiles[i].Gid); if (t != null) { if (t != null & t.Properties != null & t.Properties.ContainsKey("Collision")) { if (t.Properties["Collision"] == "PlatformHalfdrop") { st.CollisionType = StageTile.TileCollisionType.PlatformHalfDrop; } if (t.Properties["Collision"] == "Impassable") { st.CollisionType = StageTile.TileCollisionType.Impassable; } if (t.Properties["Collision"] == "Platform") { st.CollisionType = StageTile.TileCollisionType.Platform; } if (t.Properties["Collision"] == "StairsLeft") { st.CollisionType = StageTile.TileCollisionType.StairsLeft; } if (t.Properties["Collision"] == "StairsRight") { st.CollisionType = StageTile.TileCollisionType.StairsRight; } if (t.Properties["Collision"] == "StairsBottomLeft") { st.CollisionType = StageTile.TileCollisionType.StairsBottomLeft; } if (t.Properties["Collision"] == "StairsBottomRight") { st.CollisionType = StageTile.TileCollisionType.StairsBottomRight; } if ((st.CollisionType == StageTile.TileCollisionType.Impassable || st.CollisionType == StageTile.TileCollisionType.Platform) && st.X > 0 && st.Y > 0 && st.X + 1 < this.MapWidth) { if (this.getStageTileByGridPosition(st.X - 1, st.Y - 1).CollisionType == StageTile.TileCollisionType.StairsLeft) { st.CollisionType = StageTile.TileCollisionType.StairsBottomLeft; } if (this.getStageTileByGridPosition(st.X + 1, st.Y - 1).CollisionType == StageTile.TileCollisionType.StairsRight) { st.CollisionType = StageTile.TileCollisionType.StairsBottomRight; } } } if (t.Properties.ContainsKey("Collision") && t.Properties.ContainsKey("WaterTile")) { if (t.Properties["WaterTile"] == "Yes") { st.CollisionType = StageTile.TileCollisionType.PlatformWater; } } } t = tmx.GetTmxTilesetTileByGID(tmx.Layers["Background"].Tiles[i].Gid); if (t != null) { if (t.Properties.ContainsKey("Portal")) { t.Properties.TryGetValue("Portal", out st.PortalID); } } StageTiles.Add(st); } }
public override void Move(CVGameTime gameTime) { Vector2 proposedPosition; if (Velocity.X > 0) { proposedPosition = new Vector2(WorldPosition.X + BoundingBox().Width * 0.9f, WorldPosition.Y); } else if (Velocity.X < 0) { proposedPosition = new Vector2(WorldPosition.X - BoundingBox().Width * 0.9f, WorldPosition.Y); } else { proposedPosition = WorldPosition; } // simulate gravity to determine if proposed position would collide with platform tile or not. // otherwise proposedPosition.Y += 1; Rectangle proposedbounds = this.BoundingBox(proposedPosition); bool bChangeDirection = false; bool bWouldBeOnGround = false; int leftTile = (int)Math.Floor((float)proposedbounds.Left / CurrentStage.TileWidth); int rightTile = (int)Math.Ceiling(((float)proposedbounds.Right / CurrentStage.TileWidth)) - 1; int topTile = (int)Math.Floor((float)proposedbounds.Top / CurrentStage.TileHeight); int bottomTile = (int)Math.Ceiling(((float)proposedbounds.Bottom / CurrentStage.TileHeight)) - 1; // For each potentially colliding platform tile, for (int y = topTile; y <= bottomTile; ++y) { for (int x = leftTile; x <= rightTile; ++x) { StageTile stageTile = CurrentStage.getStageTileByGridPosition(x, y); if (stageTile != null) { if (stageTile.IsImpassable()) { // bump into wall- reverse direction Rectangle tilebounds = CurrentStage.getTileBoundsByGridPosition(x, y); if (proposedbounds.Intersects(tilebounds)) { bChangeDirection = true; } } if (stageTile.IsPlatform() && y == bottomTile) { List <Platform> tileboundsList = CurrentStage.getTilePlatformBoundsByGridPosition(x, bottomTile); foreach (Platform platformbounds in tileboundsList) { Rectangle tilebounds = platformbounds.PlatformBounds; if (proposedbounds.Bottom >= tilebounds.Top && proposedbounds.Intersects(tilebounds)) { bWouldBeOnGround = true; } } } } } } if (bChangeDirection) { ChangeDirection(); } else if (bWouldBeOnGround == false) { ChangeDirection(); } else if (this.direction == Player.PlayerDirection.Left) { Velocity.X = -EnemyMoveSpeed; } else if (this.direction == Player.PlayerDirection.Right) { Velocity.X = EnemyMoveSpeed; } }