private static void setObjectPosition(Entity obj, Vector position, Vector scale, Tile tile, float layernum, string contentName) { Componets.Transform transform = obj.GetComponent <Componets.Transform>(); Sprite sprite = obj.GetComponent <Sprite>(); obj.Name = tile.Properties.ContainsKey("Name") ? tile.Properties["Name"] : "Unnamed"; transform.Position = position; transform.Scale = scale; sprite.ContentName = contentName; sprite.SpriteLocation = tile.Source; sprite.Layer = layernum; }
public void HandlePhysics() { foreach (Entity entity in Entities) { Componets.Transform transform = entity.GetComponent <Componets.Transform>(); Physics physics = entity.GetComponent <Physics>(); transform.Position += physics.Velocity; // Check Collision if (entity.HasComponent <BoxCollision>()) { BoxCollision boxCollision = entity.GetComponent <BoxCollision>(); IBox collidedBox = null; foreach (IBox box in boxCollision.Boxes) { BoxData data = (BoxData)box.Data; var move = box.Move(transform.Position.X + data.Position.X * transform.Scale.X, transform.Position.Y + data.Position.Y * transform.Scale.Y, (collision) => { if (!collision.Other.HasTag(data.Layer)) { if (!data.TriggerOnly) { collidedBox = box; return(CollisionResponses.Slide); } else { // Trigger Collision return(CollisionResponses.Cross); } } return(CollisionResponses.None); }); } if (collidedBox != null) { BoxData boxData = (BoxData)collidedBox.Data; transform.Position = new Vector(collidedBox.X - transform.Scale.X * boxData.Position.X, collidedBox.Y - transform.Scale.Y * boxData.Position.Y); } } } }
public void Draw(SpriteBatch spriteBatch, PenumbraComponent penumbra) { foreach (Entity entity in Entities) { Componets.Transform transform = entity.GetComponent <Componets.Transform>(); // Sprite Draw if (entity.HasComponent <Sprite>() && entity.GetComponent <Sprite>().Enabled) { Sprite sprite = entity.GetComponent <Sprite>(); if (sprite.SpriteLocation == Rectangle.Empty) { sprite.SpriteLocation = new Rectangle(0, 0, textures[sprite.ContentName].Width, textures[sprite.ContentName].Height); } Rectangle spriteLocation = new Rectangle(sprite.SpriteLocation.X, sprite.SpriteLocation.Y, (int)(sprite.SpriteLocation.Width * sprite.Fill), sprite.SpriteLocation.Height); if (textures.ContainsKey(sprite.ContentName)) { spriteBatch.Draw(textures[sprite.ContentName], new Rectangle((int)Math.Floor(transform.Position.X), (int)Math.Floor(transform.Position.Y), (int)Math.Floor(transform.Scale.X * sprite.Fill), (int)Math.Floor(transform.Scale.Y)), spriteLocation, sprite.Color, transform.Rotation, new Vector2(0, 0), sprite.SpriteEffects, sprite.Layer); } else { Debug.WriteLine($"Content '{sprite.ContentName}' does not exist"); } } // Debug if (WindowManager.ShowCollisionsDetails) { if (entity.HasComponent <BoxCollision>()) { BoxCollision boxCollision = entity.GetComponent <BoxCollision>(); foreach (IBox box in boxCollision.Boxes) { Color color = new Color(Color.White, .5f); spriteBatch.Draw(textures["Pixel"], new Rectangle((int)box.X, (int)box.Y, (int)box.Width, (int)box.Height), color); } } } } }
private void setCurrentCollisions(Animation animation, Entity entity, AnimationTracker current) { animation.CurrentCollisions = new Dictionary <string, BoxCollision>(); foreach (KeyValuePair <string, BoxCollision> pair in current.Frames[current.FrameNumber].Colliders) { if (pair.Key == "Collision") { BoxCollision boxCollision = entity.GetComponent <BoxCollision>(); Componets.Transform transform = entity.GetComponent <Componets.Transform>(); List <IBox> newBoxes = new List <IBox>(); for (int i = 0; i < pair.Value.Boxes.Count; i++) { BoxData boxData = (BoxData)pair.Value.Boxes[i].Data; IBox newIbox = boxCollision.World.Create(transform.Position.X + transform.Scale.X * boxData.Position.X, transform.Position.Y + transform.Scale.Y * boxData.Position.Y, transform.Scale.X * boxData.Scale.X, transform.Scale.Y * boxData.Scale.Y).AddTags(boxData.Layer); newIbox.Data = boxData; if (boxCollision.Boxes.Count > i) { boxCollision.World.Remove(boxCollision.Boxes[i]); boxCollision.Boxes.Remove(boxCollision.Boxes[i]); } newBoxes.Add(newIbox); } boxCollision.Boxes = newBoxes; } else { animation.CurrentCollisions.Add(pair.Key, pair.Value); } } }