//Lock's camera to the player sprite public void LockToTarget(AnimatedSprite sprite, int screenWidth, int screenHeight) { //sets camera position to the sprite's position. Middle of sprite, -(screenWidth / 2) puts sprite in center of screen when camera moves this.Position.X = sprite.Position.X + (sprite.CurrentAnimation.CurrentRect.Width / 2) - (screenWidth / 2); this.Position.Y = sprite.Position.Y + (sprite.CurrentAnimation.CurrentRect.Height / 2) - (screenHeight / 2); }
public bool InSpeakingRange(AnimatedSprite sprite) { Vector2 d = Origin - sprite.Origin; return (d.Length() < ReactionRadius); }
public static bool AreColliding(AnimatedSprite a, AnimatedSprite b) { Vector2 d = b.Position - a.Position; return (d.Length() < b.CollisionRadius + a.CollisionRadius); }
private void CheckForUnwalkableTiles(AnimatedSprite sprite) { Point spriteCell = Engine.ConvertPositionToCell(sprite.Center); Point? upLeft = null, up = null, upRight = null, left = null, right = null, downLeft = null, down = null, downRight = null; if (spriteCell.Y > 0) up = new Point(spriteCell.X, spriteCell.Y - 1); if (spriteCell.Y < tileMap.CollisionLayer.Height - 1) down = new Point(spriteCell.X, spriteCell.Y + 1); if (spriteCell.X > 0) left = new Point(spriteCell.X - 1, spriteCell.Y); if (spriteCell.X < tileMap.CollisionLayer.Width - 1) right = new Point(spriteCell.X + 1, spriteCell.Y); if (spriteCell.X > 0 && spriteCell.Y > 0) upLeft = new Point(spriteCell.X - 1, spriteCell.Y - 1); if (spriteCell.X < tileMap.CollisionLayer.Width - 1 && spriteCell.Y > 0) upRight = new Point(spriteCell.X + 1, spriteCell.Y - 1); if (spriteCell.X > 0 && spriteCell.Y < tileMap.CollisionLayer.Height - 1) downLeft = new Point(spriteCell.X - 20, spriteCell.Y + 20); if (spriteCell.X < tileMap.CollisionLayer.Width - 1 && spriteCell.Y < tileMap.CollisionLayer.Height - 1) downRight = new Point(spriteCell.X + 1, spriteCell.Y + 1); if (up != null && tileMap.CollisionLayer.GetCellIndex(up.Value) == 0) { Rectangle cellRect = Engine.CreateRectForCell(up.Value); Rectangle spriteRect = sprite.Bounds; if (cellRect.Intersects(spriteRect)) { sprite.Postition.Y = spriteCell.Y * Engine.TileHeight; } } if (down != null && tileMap.CollisionLayer.GetCellIndex(down.Value) == 0) { Rectangle cellRect = Engine.CreateRectForCell(down.Value); Rectangle spriteRect = sprite.Bounds; if (cellRect.Intersects(spriteRect)) { sprite.Postition.Y = down.Value.Y * Engine.TileHeight - sprite.Bounds.Height; } } if (left != null && tileMap.CollisionLayer.GetCellIndex(left.Value) == 0) { Rectangle cellRect = Engine.CreateRectForCell(left.Value); Rectangle spriteRect = sprite.Bounds; if (cellRect.Intersects(spriteRect)) { sprite.Postition.X = spriteCell.X * Engine.TileWidth; } } if (right != null && tileMap.CollisionLayer.GetCellIndex(right.Value) == 0) { Rectangle cellRect = Engine.CreateRectForCell(right.Value); Rectangle spriteRect = sprite.Bounds; if (cellRect.Intersects(spriteRect)) { sprite.Postition.X = right.Value.X * Engine.TileWidth - sprite.Bounds.Width; } } if (upLeft != null && tileMap.CollisionLayer.GetCellIndex(upLeft.Value) == 0) { Rectangle cellRect = Engine.CreateRectForCell(upLeft.Value); Rectangle spriteRect = sprite.Bounds; if (cellRect.Intersects(spriteRect)) { sprite.Postition.X = spriteCell.X * Engine.TileWidth; sprite.Postition.Y = spriteCell.Y * Engine.TileHeight; } } if (upRight != null && tileMap.CollisionLayer.GetCellIndex(upRight.Value) == 0) { Rectangle cellRect = Engine.CreateRectForCell(upRight.Value); Rectangle spriteRect = sprite.Bounds; if (cellRect.Intersects(spriteRect)) { sprite.Postition.X = right.Value.X * Engine.TileWidth - sprite.Bounds.Width; sprite.Postition.Y = spriteCell.Y * Engine.TileHeight; } } if (downLeft != null && tileMap.CollisionLayer.GetCellIndex(downLeft.Value) == 0) { Rectangle cellRect = Engine.CreateRectForCell(downLeft.Value); Rectangle spriteRect = sprite.Bounds; if (cellRect.Intersects(spriteRect)) { sprite.Postition.X = spriteCell.X * Engine.TileWidth; sprite.Postition.Y = down.Value.Y * Engine.TileHeight - sprite.Bounds.Height; } } if (downRight != null && tileMap.CollisionLayer.GetCellIndex(downRight.Value) == 0) { Rectangle cellRect = Engine.CreateRectForCell(downRight.Value); Rectangle spriteRect = sprite.Bounds; if (cellRect.Intersects(spriteRect)) { sprite.Postition.X = right.Value.X * Engine.TileWidth - sprite.Bounds.Width; sprite.Postition.Y = down.Value.Y * Engine.TileHeight - sprite.Bounds.Height; } } }
private void CheckForTreasureChest(AnimatedSprite sprite) { Point cell = Engine.ConvertPositionToCell(sprite.Origin); int upOne = cell.Y -= 1; Vector2 check; check.X = cell.X; check.Y = upOne; string chest; foreach (TileLayer layer in tileMap.Layers) { if (layer.ContainsChest(check) != null && !dialog.Enabled) { layer.ContainsChest(check).CurrentAnimationName = "Open"; if (layer.ContainsChest(check).Item != null && layer.ContainsChest(check).Quantity == 1 && layer.ContainsChest(check).ItemType != "Gold") chest = "You found a " + layer.ContainsChest(check).Item; else if (layer.ContainsChest(check).Item != null && layer.ContainsChest(check).Quantity > 1 && layer.ContainsChest(check).ItemType != "Gold") chest = "You found " + layer.ContainsChest(check).Item + " x " + layer.ContainsChest(check).Quantity; else if (layer.ContainsChest(check).Item != null && layer.ContainsChest(check).ItemType == "Gold") chest = "You found " + layer.ContainsChest(check).Quantity + " Gold"; else chest = "Empty"; TextToScreen(chest); layer.RemoveChestContents(check); } } }
private bool CheckForDoor(AnimatedSprite sprite) { Point cell = Engine.ConvertPositionToCell(sprite.Origin); int upOne = cell.Y -= 1; int x = cell.X; int downOne = cell.Y += 1; Vector2 check; check.X = x; check.Y = upOne; foreach (TileLayer layer in tileMap.Layers) { if (layer.ContainsDoor(check) != null && !dialog.Enabled) { layer.ContainsDoor(check).CurrentAnimationName = "Open"; int layercount = tileMap.Layers.IndexOf(layer) + 50; tileMap.CollisionLayer.SetCellIndex(x, upOne, layercount); tileMap.CollisionLayer.SetCellIndex(x, downOne, 50); } } return false; }
private Vector2 CheckCollisionForMotion(Vector2 motion, AnimatedSprite sprite) { Point cell = Engine.ConvertPositionToCell(sprite.Origin); int colIndex = tileMap.CollisionLayer.GetCellIndex(cell); if (colIndex == 2) return motion * .2f; return motion; }
static int renderSpriteCompare(AnimatedSprite a, AnimatedSprite b) { return a.Origin.Y.CompareTo(b.Origin.Y); }
/// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); treasureChestTexture = Texture2D.FromStream(GraphicsDevice, new System.IO.StreamReader("content/treasurechest.png").BaseStream); doorTexture = Texture2D.FromStream(GraphicsDevice, new System.IO.StreamReader("content/door.png").BaseStream); tileMap = Content.Load<TileMap>("Maps/OneHouseTest"); sprite = new AnimatedSprite(Content.Load<Texture2D>("Sprites/npc3")); sprite.OriginOffset = new Vector2(16, 30); NPC npc = new NPC(Content.Load<Texture2D>("Sprites/npc3"), dialog, Content.Load<Script>("Scripts/NPC1")); npc.Target = sprite; npcs.Add(npc); npcs.Add(new NPC(Content.Load<Texture2D>("Sprites/amg2"), null, null)); npcs.Add(new NPC(Content.Load<Texture2D>("Sprites/avt1"), null, null)); npcs.Add(new NPC(Content.Load<Texture2D>("Sprites/avt2"), null, null)); npcs.Add(new NPC(Content.Load<Texture2D>("Sprites/npc6"), null, null)); }
public void LockToTarget(AnimatedSprite sprite, int screenWidth, int screenHeight) { Position.X = sprite.Postition.X + (sprite.CurrentAnimation.CurrentRect.Width / 2) - (screenWidth / 2); Position.Y = sprite.Postition.Y + (sprite.CurrentAnimation.CurrentRect.Height / 2) - (screenHeight / 2); }
private Vector2 CheckCollisionForMotion(Vector2 motion, AnimatedSprite sprite) { Point cell = Engine.ConvertPositionToCell(sprite.Origin); cell.X = (int)MathHelper.Clamp(cell.X, 0f, tileMap.GetWidth() - 1); cell.Y = (int)MathHelper.Clamp(cell.Y, 0f, tileMap.GetHeight() - 1); int collIndex; collIndex = tileMap.CollisionLayer.GetCellIndex(cell); //If player is standing on tile index 2 (Mud) if (collIndex == 2) return motion * .2f; return motion; }
/// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); tileMap.layers.Add( TileLayer.FromFile(Content, "Content/Layers/Layer1.layer")); tileMap.CollisionLayer = CollisionLayer.FromFile("Content/Layers/Collision.layer"); sprite = new AnimatedSprite(Content.Load<Texture2D>("Sprites/thf4")); //Because all sprites are 32, 32. 16 is half the sprite width, 32 is the height, where the feet are. sprite.OriginOffset = new Vector2(16, 32); npcs.Add(new AnimatedSprite(Content.Load<Texture2D>("Sprites/amg1"))); npcs.Add(new AnimatedSprite(Content.Load<Texture2D>("Sprites/man1"))); npcs.Add(new AnimatedSprite(Content.Load<Texture2D>("Sprites/npc3"))); npcs.Add(new AnimatedSprite(Content.Load<Texture2D>("Sprites/wmg1"))); npcs.Add(new AnimatedSprite(Content.Load<Texture2D>("Sprites/ygr1"))); // TODO: use this.Content to load your game content here }
public int ShowLayer(AnimatedSprite animSprite, int original) { Point cell = Engine.ConvertPositionToCell(animSprite.Origin); int colIndex = GetCellIndex(cell); if (colIndex < 50) return original; else return colIndex; }
private Vector2 CheckTileForCollision(AnimatedSprite sprite, Vector2 motion) { Point cell = Engine.GetPointFromCell(sprite.Origin); int colIndex = tileMap.CollisionLayer.GetCellIndex(cell); if (colIndex == 2) return motion * .2f; return motion; }
/// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); tileMap.Layers.Add(TileLayer.FromFile(Content, "Layouts/Layer1.layer")); tileMap.Layers.Add(TileLayer.FromFile(Content, "Layouts/Layer3.layer")); tileMap.CollisionLayer = CollisionLayer.FromFile("Content", "Layouts/Collision.layer"); scripts.Add("NPC1", Content.Load<Script>("Scripts/NPC1")); playerSprite = new AnimatedSprite(Content.Load<Texture2D>("Sprites/image (1)_strip"), 0); playerSprite.OriginOffset = new Vector2(16, 30); EntityManager.Instance.RegisterEntity(playerSprite); npc = new NPC(Content.Load<Texture2D>("Sprites/image (1)_strip"), scripts["NPC1"], 1); EntityManager.Instance.RegisterEntity(npc); npcs.Add(npc.Id, npc); npc = new NPC(Content.Load<Texture2D>("Sprites/image (11)_strip"), null, 2); EntityManager.Instance.RegisterEntity(npc); npcs.Add(npc.Id, npc); npc = new NPC(Content.Load<Texture2D>("Sprites/image (2)_strip"), null, 3); EntityManager.Instance.RegisterEntity(npc); npcs.Add(npc.Id, npc); npc = new NPC(Content.Load<Texture2D>("Sprites/image (7)_strip"), null, 4); EntityManager.Instance.RegisterEntity(npc); npcs.Add(npc.Id, npc); }