/// <summary> /// Renders the panel /// </summary> /// <param name="ihn">Ihn to render with</param> /// <param name="spriteBatch">Spritebatch to render with</param> public override void Render(Ihn ihn, SpriteBatch spriteBatch) { for (int i = (int)Root.X + 32; i < Size.X + Root.X - 32; i += 32) { for (int j = (int)Root.Y + 32; j < Size.Y + Root.Y - 32; j += 32) { spriteBatch.Draw(Rsc.Load <Texture2D>(Img), new FloatRect(i, j, 32, 32).ToRect(), new Rectangle(32, 32, 32, 32), Color.White, 0f, new Vector2(0, 0), SpriteEffects.None, 0); } } for (int i = (int)Root.X + 32; i < Size.X + Root.X - 32; i += 32) { spriteBatch.Draw(Rsc.Load <Texture2D>(Img), new FloatRect(i, Root.Y, 32, 32).ToRect(), new Rectangle(32, 0, 32, 32), Color.White, 0f, new Vector2(0, 0), SpriteEffects.None, 0); spriteBatch.Draw(Rsc.Load <Texture2D>(Img), new FloatRect(i, Root.Y + Size.Y - 32, 32, 32).ToRect(), new Rectangle(32, 64, 32, 32), Color.White, 0f, new Vector2(0, 0), SpriteEffects.None, 0); } for (int i = (int)Root.Y + 32; i < Size.Y + Root.Y - 32; i += 32) { spriteBatch.Draw(Rsc.Load <Texture2D>(Img), new FloatRect(Root.X, i, 32, 32).ToRect(), new Rectangle(0, 32, 32, 32), Color.White, 0f, new Vector2(0, 0), SpriteEffects.None, 0); spriteBatch.Draw(Rsc.Load <Texture2D>(Img), new FloatRect(Root.X + Size.X - 32, i, 32, 32).ToRect(), new Rectangle(64, 32, 32, 32), Color.White, 0f, new Vector2(0, 0), SpriteEffects.None, 0); } spriteBatch.Draw(Rsc.Load <Texture2D>(Img), new FloatRect(Root.X, Root.Y, 32, 32).ToRect(), new Rectangle(0, 0, 32, 32), Color.White, 0f, new Vector2(0, 0), SpriteEffects.None, 0); spriteBatch.Draw(Rsc.Load <Texture2D>(Img), new FloatRect(Root.X + Size.X - 32, Root.Y, 32, 32).ToRect(), new Rectangle(64, 0, 32, 32), Color.White, 0f, new Vector2(0, 0), SpriteEffects.None, 0); spriteBatch.Draw(Rsc.Load <Texture2D>(Img), new FloatRect(Root.X, Root.Y + Size.Y - 32, 32, 32).ToRect(), new Rectangle(0, 64, 32, 32), Color.White, 0f, new Vector2(0, 0), SpriteEffects.None, 0); spriteBatch.Draw(Rsc.Load <Texture2D>(Img), new FloatRect(Root.X + Size.X - 32, Root.Y + Size.Y - 32, 32, 32).ToRect(), new Rectangle(64, 64, 32, 32), Color.White, 0f, new Vector2(0, 0), SpriteEffects.None, 0); base.Render(ihn, spriteBatch); }
/// <summary> /// Renders the 8-directional sprite /// </summary> /// <param name="ihn">Ihn entity is contained in</param> /// <param name="spriteBatch">Spritebatch to draw with</param> /// <param name="entity">Entity to draw</param> public void Render(Ihn ihn, SpriteBatch spriteBatch, Entity entity) { var sprite = entity.GetComp <ComponentTopDownEightDirSprite>(); var pos = entity.GetComp <ComponentPosition>(); var dir = entity.GetComp <ComponentDirection>(); int sizex = Rsc.Load <Texture2D>(sprite.Texture).Width / 4; int sizey = Rsc.Load <Texture2D>(sprite.Texture).Height / 2; if (entity.HasComp <ComponentSize>()) { var size = entity.GetComp <ComponentSize>(); sizex = size.Width; sizey = size.Height; } float Rotation = DirectionHelper.ToAngle(dir.Dir); spriteBatch.Draw(Rsc.Load <Texture2D>(sprite.Texture), new Rectangle((int)pos.X - (int)ihn.CameraPos.X, (int)pos.Y - (int)ihn.CameraPos.Y, sizex, sizey), new Rectangle(DirectionHelper.IsDiagonal(dir.Dir) ? Rsc.Load <Texture2D>(sprite.Texture).Width / 2 : 0, 0, Rsc.Load <Texture2D>(sprite.Texture).Width / 2, Rsc.Load <Texture2D>(sprite.Texture).Height), Color.White, MathHelper.ToRadians(Rotation - (DirectionHelper.IsDiagonal(dir.Dir) ? 45 : 0)), new Vector2(Rsc.Load <Texture2D>(sprite.Texture).Width / 4, Rsc.Load <Texture2D>(sprite.Texture).Height / 2), SpriteEffects.None, 0); }
/// <summary> /// Creates a new font /// </summary> /// <param name="path">Path to the font</param> public DrawableFont(string path) { Font = IhnLib.Font.Load(path); Img = path.Substring(0, path.Length - 4); //Images sometimes have _1 at the end, increase the graphic size in this case Img += "_0.png"; fr = new FontRenderer(Font, Rsc.Load <Texture2D>(Img)); }
/// <summary> /// Renders the sprite /// </summary> /// <param name="ihn">Ihn the control is contained in</param> /// <param name="spriteBatch">Spritebatch to draw with</param> public override void Render(Ihn ihn, SpriteBatch spriteBatch) { spriteBatch.Draw(Rsc.Load <Texture2D>(Img), new FloatRect(Root.X, Root.Y, Size.X, Size.Y).ToRect(), new Rectangle(0, 0, (int)Size.X, (int)Size.Y), Color.White, 0, new Vector2(0, 0), Mirror, 0); base.Render(ihn, spriteBatch); }
//TODO: Render tile to a rendertarget, save, and render that instead of generating flairs at runtime /// <summary> /// Renders a tile at drawRoot /// </summary> /// <param name="spriteBatch">Spritebatch to render with</param> /// <param name="tm">Tilemap to render from</param> /// <param name="tile">TileType to render</param> /// <param name="solid">List of directions that the tile considers solid</param> /// <param name="drawRoot">Literal x and y to draw at</param> /// <param name="_r">Random to generate with</param> /// <param name="tex">Prebuilt texture, will build if null</param> public static void RenderTile(SpriteBatch spriteBatch, ComponentTilemap tm, TileType tile, List <Direction> solid, Vector2 drawRoot, Random _r = null, Texture2D tex = null) { tex = tex != null ? tex : Rsc.Load <Texture2D>(tile.RootTexture); spriteBatch.Draw(tex, new Rectangle((int)drawRoot.X, (int)drawRoot.Y, tex.Width, tex.Height), null, Color.White, 0, new Vector2(0, 0), SpriteEffects.None, 0); _r = _r != null ? _r : new Random(100); for (int k = 0; k < tile.Flairs.Count; k++) { var flair = tile.Flairs[k]; if (!solid.Contains(Direction.North) && flair.North) { for (int l = 0; l < (int)((float)flair.Coverage / 100f * ((float)tex.Width)); l++) { var flairtex = Rsc.Load <Texture2D>(flair.Texture); spriteBatch.Draw(flairtex, new Rectangle( (int)(drawRoot.X + _r.Next(-flairtex.Width / 2, tex.Width + 2 - flairtex.Width / 2 - 1)), (int)(drawRoot.Y + -_r.Next((int)flairtex.Height / 2, flairtex.Height) + tile.CutIn + tex.Height * _r.Next(flair.MinDepth, flair.MaxDepth) / 100f), flairtex.Width, flairtex.Height), null, Color.White, 0, new Vector2(0, 0), SpriteEffects.None, 0); } } if (!solid.Contains(Direction.South) && flair.South) { for (int l = 0; l < (int)((float)flair.Coverage / 100f * ((float)tex.Width)); l++) { var flairtex = Rsc.Load <Texture2D>(flair.Texture); spriteBatch.Draw(flairtex, new Rectangle( (int)(drawRoot.X + _r.Next(-flairtex.Width / 2, tex.Width + 2 - flairtex.Width / 2 - 1)), (int)(drawRoot.Y + tex.Height + -flairtex.Height + _r.Next((int)flairtex.Height / 2, flairtex.Height) - tile.CutIn - tex.Height * _r.Next(flair.MinDepth, flair.MaxDepth) / 100f), flairtex.Width, flairtex.Height), null, Color.White, 0, new Vector2(0, 0), SpriteEffects.None, 0); } } } }
/// <summary> /// Checks for a click action and runs the action if the entity is clicked /// </summary> /// <param name="ihn">Ihn entity is contained in</param> /// <param name="entity">Entity to update</param> public void Update(Ihn ihn, Entity entity) { var pos = entity.GetComp <ComponentPosition>(); var x = pos.X; var y = pos.Y; var w = 0; var h = 0; if (entity.HasComp <ComponentAABB>()) { var aabb = entity.GetComp <ComponentAABB>(); x = aabb.X; y = aabb.Y; w = aabb.Width; h = aabb.Height; } else if (entity.HasComp <ComponentTopDownEightDirSprite>()) { var spr = entity.GetComp <ComponentTopDownEightDirSprite>(); x -= Rsc.Load <Texture2D>(spr.Texture).Width / 8; y -= Rsc.Load <Texture2D>(spr.Texture).Height / 4; w = Rsc.Load <Texture2D>(spr.Texture).Width / 4; h = Rsc.Load <Texture2D>(spr.Texture).Height / 2; } else if (entity.HasComp <ComponentSize>()) { w = entity.GetComp <ComponentSize>().Width; h = entity.GetComp <ComponentSize>().Height; } else if (entity.HasComp <ComponentSprite>()) { var spr = entity.GetComp <ComponentSprite>(); x -= spr.Origin.X; y -= spr.Origin.Y; w = Rsc.Load <Texture2D>(spr.Texture).Width; h = Rsc.Load <Texture2D>(spr.Texture).Height; } var onclick = entity.GetComp <ComponentOnClickedDo>(); if (MouseHelper.MouseLeftPressed()) { if (MouseHelper.X > x && MouseHelper.X < x + w) { if (MouseHelper.Y > y && MouseHelper.Y < y + h) { onclick.Todo.Invoke(this, new EventArgs()); } } } }
/// <summary> /// Renders the sprite /// </summary> /// <param name="ihn">Ihn entity is contained in</param> /// <param name="spriteBatch">Spritebatch to draw with</param> /// <param name="entity">Entity to draw</param> public void Render(Ihn ihn, SpriteBatch spriteBatch, Entity entity) { var pos = entity.GetComp <ComponentPosition>(); var sprite = entity.GetComp <ComponentSprite>(); if (entity.HasComp <ComponentSize>()) { var size = entity.GetComp <ComponentSize>(); spriteBatch.Draw(Rsc.Load <Texture2D>(sprite.Texture), new Rectangle((int)pos.X - (int)ihn.CameraPos.X, (int)pos.Y - (int)ihn.CameraPos.Y, size.Width, size.Height), sprite.Source.ToRect(), Color.White, MathHelper.ToRadians(sprite.Rotation), sprite.Origin.ToVector(), sprite.Mirror, 0); } else { spriteBatch.Draw(Rsc.Load <Texture2D>(sprite.Texture), new Rectangle((int)pos.X - (int)ihn.CameraPos.X, (int)pos.Y - (int)ihn.CameraPos.Y, (int)sprite.Source.Width, (int)sprite.Source.Height), sprite.Source.ToRect(), Color.White, MathHelper.ToRadians(sprite.Rotation), sprite.Origin.ToVector(), sprite.Mirror, 0); } }
/// <summary> /// Renders the control and the item in it /// </summary> /// <param name="ihn">Ihn the control is rendered in</param> /// <param name="spriteBatch">Spritebatch to render with</param> public override void Render(Ihn ihn, SpriteBatch spriteBatch) { if (Item == null && DefaultImg != "") { var img = Rsc.Load <Texture2D>(Img); spriteBatch.Draw(img, new Rectangle((int)Root.X, (int)Root.Y, img.Width, img.Height), null, Color.White, 0f, new Vector2(0, 0), SpriteEffects.None, 0); } else { var img = Rsc.Load <Texture2D>(DefaultImg); spriteBatch.Draw(img, new Rectangle((int)Root.X, (int)Root.Y, img.Width, img.Height), null, Color.White, 0f, new Vector2(0, 0), SpriteEffects.None, 0); } if (Item != null) { if (Item.HasComp <ComponentInventorySprite>()) { Item.GetComp <ComponentInventorySprite>().Renderer.Invoke(ihn, Item, spriteBatch, (int)(Root.X + 2), (int)(Root.Y + 2)); } } }
private void Render(object sender, EventArgs e) { if (RootControl != null && RootControl.Enabled) { var renders = RootControl.GetRenders(); for (int i = 0; i < renders.Count; i++) { for (int j = 0; j < renders[i].Count; j++) { renders[i][j].Render(Ihn.Instance, Ihn.Instance.SBatch); } } } if (ItemMoving != null && ItemMoving.HasComp <ComponentInventorySprite>()) { ItemMoving.GetComp <ComponentInventorySprite>().Renderer.Invoke(Ihn.Instance, ItemMoving, Ihn.Instance.SBatch, MouseHelper.X - 16, MouseHelper.Y - 16); } else if (!Ihn.Instance.IsMouseVisible) { Ihn.Instance.SBatch.Draw(Rsc.Load <Texture2D>(CursorImg), new Rectangle((int)(MouseHelper.X - 8), (int)(MouseHelper.Y - 8), 16, 16), null, Color.White, 0f, new Vector2(0, 0), SpriteEffects.None, (int)0); } }
/// <summary> /// Updates the tilemap /// </summary> /// <param name="ihn">Ihn entity is contained in</param> /// <param name="entity">Entity to update</param> public void Update(Ihn ihn, Entity entity) { var tm = entity.GetComp <ComponentTilemap>(); if (KeyHelper.KeyPressed(tm.EditModeToggleKey) && tm.AllowEditModeToggle) { tm.EditMode = !tm.EditMode; } if (MouseHelper.MouseLeftDown() && tm.EditMode && tm.SelectedTile != default(TileType)) { var tex = Rsc.Load <Texture2D>(tm.SelectedTile.RootTexture); tm.PlaceTile(tm.SelectedTile, Math.Max(0, (int)((MouseHelper.X + Ihn.Instance.CameraPos.X) / tex.Width)), Math.Max(0, (int)((MouseHelper.Y + Ihn.Instance.CameraPos.Y) / tex.Height))); } if (MouseHelper.MouseRightDown() && tm.EditMode) { var tex = Rsc.Load <Texture2D>(tm.SelectedTile.RootTexture); tm.PlaceTile(new TileType(), Math.Max(0, (int)((MouseHelper.X + Ihn.Instance.CameraPos.X) / tex.Width)), Math.Max(0, (int)((MouseHelper.Y + Ihn.Instance.CameraPos.Y) / tex.Height))); } }
/// <summary> /// Gets the boundries of an entity /// </summary> /// <param name="entity">Entity to get bounds for</param> /// <param name="pos">Position of the entity</param> /// <param name="x">outs the x coord to this</param> /// <param name="y">outs the y coord to this</param> /// <param name="w">outs the width to this</param> /// <param name="h">outs the height to this</param> public static void GetBounds(Entity entity, ComponentPosition pos, out float x, out float y, out int w, out int h) { x = pos.X; y = pos.Y; w = 0; h = 0; if (entity.HasComp <ComponentAABB>()) { var aabb = entity.GetComp <ComponentAABB>(); x = aabb.X; y = aabb.Y; w = aabb.Width; h = aabb.Height; } else if (entity.HasComp <ComponentTopDownEightDirSprite>()) { var spr = entity.GetComp <ComponentTopDownEightDirSprite>(); x -= Rsc.Load <Texture2D>(spr.Texture).Width / 8; y -= Rsc.Load <Texture2D>(spr.Texture).Height / 4; w = Rsc.Load <Texture2D>(spr.Texture).Width / 4; h = Rsc.Load <Texture2D>(spr.Texture).Height / 2; } else if (entity.HasComp <ComponentSize>()) { w = entity.GetComp <ComponentSize>().Width; h = entity.GetComp <ComponentSize>().Height; } else if (entity.HasComp <ComponentSprite>()) { var spr = entity.GetComp <ComponentSprite>(); x -= spr.Origin.X; y -= spr.Origin.Y; w = Rsc.Load <Texture2D>(spr.Texture).Width; h = Rsc.Load <Texture2D>(spr.Texture).Height; } }
//Cuts into a rectangle private void RebuildTileTexture(ComponentTilemap tm, int i, int j, ref TileType tile, List <Direction> solid, ref Texture2D tex) { tex = Rsc.Load <Texture2D>(tile.RootTexture); //TODO: GetData broken on linux/mac, implement /* * if(!solid.Contains(Direction.North)) { * tex = tex.FillRectangle(new Rectangle(0, 0, tex.Width, tile.CutIn), Color.Transparent); * var rects = new List<Rectangle>(); * for(int k = 0; k < _r.Next((int)(tex.Width / 2f), (int)(tex.Width / 1.2f)); k++) { * rects.Add(new Rectangle(_r.Next(0, tex.Width - 1), tile.CutIn, _r.Next(1, 3), _r.Next(1, 3))); * } * tex = tex.FillRectangles(rects, Color.Transparent); * } * if(!solid.Contains(Direction.South)) { * var rects = new List<Rectangle>(); * for(int k = 0; k < _r.Next((int)(tex.Width / 2f), (int)(tex.Width / 1.2f)); k++) { * rects.Add(new Rectangle(_r.Next(0, tex.Width - 1), tex.Height - tile.CutIn + 1, _r.Next(1, 3), _r.Next(1, 3))); * } * tex = tex.FillRectangles(rects, Color.Transparent); * } */ tm.Textures[i, j] = tex; }
/// <summary> /// Creates a new component with textures at origin with rotation in layer /// </summary> /// <param name="textures">List of paths to textures</param> /// <param name="origin">Position origin</param> /// <param name="rotation">Angle to draw the sprite at (In degrees)</param> /// <param name="layer">Layer to render at</param> public ComponentMultiSprite(List <string> textures, Position origin, float rotation, int layer) : this(textures, new FloatRect(0, 0, Rsc.Load <Texture2D>(textures[0]).Width, Rsc.Load <Texture2D>(textures[0]).Height), origin, rotation, layer) { }
/// <summary> /// Simple 1 size fits all collision test /// </summary> /// <param name="ihn">Ihn to compare with</param> /// <param name="entity">Entity to compare</param> /// <returns>Whether the entity collides with any solid in Ihn</returns> public static bool Colliding(Ihn ihn, Entity entity) { if (!entity.HasComp <ComponentSolid>()) { return(false); } var pos = entity.GetComp <ComponentPosition>(); float x; float y; int w; int h; GetBounds(entity, pos, out x, out y, out w, out h); //TODO: Optimize tilemap collision var tilemaps = ihn.GetEntitiesWith <ComponentTilemap>(); if (tilemaps.Count > 0) { var tilemap = tilemaps[0].GetComp <ComponentTilemap>(); for (int i = (int)Math.Max(0, (x - 250) / 32); i < (int)Math.Min(tilemap.Map.GetLength(0), (x + 250) / 32); i++) { for (int j = (int)Math.Max(0, (y - 250) / 32); j < (int)Math.Min(tilemap.Map.GetLength(1), (y + 250) / 32); j++) { if (tilemap.Map[i, j].Solid) { var tw = Rsc.Load <Texture2D>(tilemap.Map[i, j].RootTexture).Width; var th = Rsc.Load <Texture2D>(tilemap.Map[i, j].RootTexture).Height; var rect = new Rectangle(i * tw, j * th, tw, th); if (rect.Intersects(new Rectangle((int)x, (int)y, w, h))) { return(true); } } } } } if (entity.HasComp <ComponentVelocity>() && !entity.HasComp <ComponentTransient>()) { var velocity = entity.GetComp <ComponentVelocity>(); var bounds = new Rectangle((int)x, (int)y, (int)w, (int)h); var entities = ihn.GetEntitiesWith <ComponentSolid, ComponentPosition>(); for (int i = 0; i < entities.Count; i++) { if (entities[i] != entity && !entities[i].HasComp <ComponentTransient>()) { var entsize = new Vector2(1, 1); if (!entities[i].HasComp <ComponentPosition>()) { continue; } if (entities[i].HasComp <ComponentSize>()) { entsize = new Vector2(entities[i].GetComp <ComponentSize>().Width, entities[i].GetComp <ComponentSize>().Height); } else if (entities[i].HasComp <ComponentSprite>()) { entsize = new Vector2(Rsc.Load <Texture2D>(entities[i].GetComp <ComponentSprite>().Texture).Width, Rsc.Load <Texture2D>(entities[i].GetComp <ComponentSprite>().Texture).Height); } var entbounds = new Rectangle((int)entities[i].GetComp <ComponentPosition>().X, (int)entities[i].GetComp <ComponentPosition>().Y, (int)entsize.X, (int)entsize.Y); if (entbounds.Intersects(bounds)) { return(true); } } } } return(false); }
/// <summary> /// Default render function /// </summary> /// <param name="ihn">Ihn instance to use</param> /// <param name="entity">Entity rendered</param> /// <param name="spriteBatch">Spritebatch to render with</param> /// <param name="x">XCoord</param> /// <param name="y">YCoord</param> /// <param name="mirrorx">Mirror on Y axis?</param> public static void Render(Ihn ihn, Entity entity, SpriteBatch spriteBatch, int x, int y, bool mirrorx = false) { var sprite = entity.GetComp <ComponentInventorySprite>(); spriteBatch.Draw(Rsc.Load <Texture2D>(sprite.Texture), new Rectangle(x, y, Rsc.Load <Texture2D>(sprite.Texture).Width, Rsc.Load <Texture2D>(sprite.Texture).Height), null, Color.White, 0f, new Vector2(0, 0), mirrorx ? SpriteEffects.FlipHorizontally : SpriteEffects.None, 0f); }
/// <summary> /// Creates a new sprite /// </summary> /// <param name="texture">Path to texture</param> /// <param name="origin">Image origin</param> /// <param name="rotation">Angle to render image at (In degrees)</param> /// <param name="layer">Layer to render in</param> public ComponentSprite(string texture, Position origin, float rotation, int layer) : this(texture, new FloatRect(0, 0, Rsc.Load <Texture2D>(texture).Width, Rsc.Load <Texture2D>(texture).Height), origin, rotation, layer) { }
/// <summary> /// Instantiates a new ControlSprite with parent and a string reference to an image /// </summary> /// <param name="parent">Control to be a child of</param> /// <param name="img">Path to image</param> public ControlSprite(Control parent, string img) : base(parent, new Vector2(Rsc.Load <Texture2D>(img).Width, Rsc.Load <Texture2D>(img).Height)) { Img = img; }