/// <summary> /// Loads a whole Ihn, all of the entities and systems /// </summary> /// <param name="ihn">Ihn to load over</param> /// <param name="saveFile">Path to file to load from</param> public static void Load(Ihn ihn, string saveFile) { Stream stream = File.Open(saveFile, FileMode.Open); BinaryFormatter bin = new BinaryFormatter(); ihn.ClearEntities(); ihn.ClearSystems(); while (true) { try { var a = bin.Deserialize(stream); if (a.GetType() == typeof(Entity)) { dynamic b = a; ihn.AddEntity(b); } else { dynamic b = a; ihn.AddSystem(b); } } catch { break; } } stream.Close(); }
/// <summary> /// Updates the entity by reducing its velocity /// </summary> /// <param name="ihn">Ihn the entity is contained in</param> /// <param name="entity">Entity to slow</param> public void Update(Ihn ihn, Entity entity) { var velocity = entity.GetComp <ComponentVelocity>(); velocity.X *= Multiplier / HorizontalMultiplier; velocity.Y *= Multiplier / VerticalMultiplier; }
/// <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> /// 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); }
/// <summary> /// Chases after the entity using a 2D pathmap /// </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 ai = entity.GetComp <ComponentZombieAI>(); if (ai.SwipeTime > 0) { ai.SwipeTime--; } if (ai.ChaseX != Following.GetComp <ComponentPosition>().X / ai.TileSize || ai.ChaseY != Following.GetComp <ComponentPosition>().Y / ai.TileSize) { ai.ShouldRefreshPath = true; ai.ChaseX = (int)Following.GetComp <ComponentPosition>().X / ai.TileSize; ai.ChaseY = (int)Following.GetComp <ComponentPosition>().Y / ai.TileSize; } if (ai.ShouldRefreshPath) { ai.ShouldRefreshPath = false; //ai.Path = Pathfinding.FindPath(Map.SolidityMap, new Vector2(pos.X / ai.TileSize, pos.Y / ai.TileSize), new Vector2(ai.ChaseX, ai.ChaseY)); } Vector2 nextPos; if (ai.Path.Count > 1) { var vector = ai.Path[ai.PathStep + 1].ToVector() * ai.TileSize + new Position(ai.TileSize / 2, ai.TileSize / 2).ToVector(); nextPos = vector; } else { nextPos = new Vector2(Following.GetComp <ComponentPosition>().X, Following.GetComp <ComponentPosition>().Y); } Vector2 momentum = new Vector2(0, 0); if (nextPos.X > pos.X) { pos.X += ai.Speed; momentum.X++; } if (nextPos.Y < pos.Y) { pos.Y -= ai.Speed; momentum.Y--; } if (nextPos.X < pos.X) { pos.X -= ai.Speed; momentum.X--; } if (nextPos.Y > pos.Y) { pos.Y += ai.Speed; momentum.Y++; } if (entity.HasComp <ComponentDirection>()) { entity.GetComp <ComponentDirection>().Dir = DirectionHelper.VectorToDirection(momentum); } }
/// <summary> /// Checks if an entities HP is lower than or equal to 0 and removes it if so /// </summary> /// <param name="ihn">Ihn calling the function</param> /// <param name="entity">Entity called on</param> public void Update(Ihn ihn, Entity entity) { var health = entity.GetComp <ComponentHealth>(); if (health.Hp <= 0) { ihn.RemoveEntity(entity); } }
/// <summary> /// Updates the Y velocity of an entity /// </summary> /// <param name="ihn">Ihn entity is contained in</param> /// <param name="entity">Entity to update</param> public void Update(Ihn ihn, Entity entity) { if (entity.HasComp <ComponentSize>() || entity.HasComp <ComponentSprite>()) { var pos = entity.GetComp <ComponentPosition>(); var gravity = entity.GetComp <ComponentGravity>(); var velocity = entity.GetComp <ComponentVelocity>(); velocity.Y += gravity.Multiplier * ComponentGravity.GlobalMultiplier; } }
/// <summary> /// Instantiates a new Ihn. Only call this once /// </summary> /// <param name="width">Width of the window</param> /// <param name="height">Height of the window</param> /// <param name="vsync">Vertical Retrace</param> /// <param name="fullscreen">Guess</param> public Ihn(int width, int height, bool vsync, bool fullscreen = false) { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = Directory.GetCurrentDirectory().Substring(0, 3); Instance = this; SBatch = new SpriteBatch(graphics.GraphicsDevice); this.vsync = vsync; this.height = height; this.width = width; this.fullscreen = fullscreen; }
/// <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()); } } } }
static void MoveY(Ihn ihn, Entity entity, ComponentPosition pos, ComponentVelocity velocity) { pos.Y += velocity.Y; if (CollisionHelper.Colliding(ihn, entity)) { pos.Y -= velocity.Y; int runs = 0; while (!CollisionHelper.Colliding(ihn, entity) && runs < Math.Abs(velocity.Y) + 2) { runs++; pos.Y += velocity.Y > 0 ? 1 : -1; } pos.Y -= velocity.Y > 0 ? 1 : -1; } }
/// <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 tilemap /// </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 tm = entity.GetComp <ComponentTilemap>(); for (int i = (int)Math.Min(tm.Map.GetLength(0), Math.Max(0, ihn.CameraPos.X / 32)); i < (int)Math.Min(tm.Map.GetLength(0), Math.Max(0, (ihn.CameraPos.X + 1000) / 32)); i++) { for (int j = (int)Math.Min(tm.Map.GetLength(1), Math.Max(0, ihn.CameraPos.Y / 32)); j < (int)Math.Min(tm.Map.GetLength(1), Math.Max(0, (ihn.CameraPos.Y + 1000) / 32)); j++) { PreRenderTile(ihn, spriteBatch, tm, i, j); } } }
/// <summary> /// Checks if a tile needs to be recut and cuts it /// </summary> /// <param name="ihn"></param> /// <param name="spriteBatch"></param> /// <param name="tm"></param> /// <param name="i"></param> /// <param name="j"></param> /// <param name="rx"></param> /// <param name="ry"></param> private void PreRenderTile(Ihn ihn, SpriteBatch spriteBatch, ComponentTilemap tm, int i, int j, int rx = -1, int ry = -1) { var tile = tm.Map[i, j]; var seed = tm.Seeds[i, j]; var solid = tm.MapSolids[i, j]; if (tm.Textures == null) { tm.Textures = new Texture2D[tm.Map.GetLength(0), tm.Map.GetLength(1)]; } var tex = tm.Textures[i, j]; if (tile.RootTexture != "" && tile.RootTexture != null) { _r = new Random(seed); if (tex == null || tm.ForceTextureBuilds.Contains(new Vector2(i, j))) { _r = new Random(seed + (Spaz ? (int)(DateTime.UtcNow.Ticks * seed) : 0)); if (tm.ForceTextureBuilds == null) { tm.ForceTextureBuilds = new List <Vector2>(); for (int x = 0; x < tm.Map.GetLength(0); x++) { for (int y = 0; y < tm.Map.GetLength(1); y++) { tm.ForceTextureBuilds.Add(new Vector2(x, y)); } } } while (tm.ForceTextureBuilds.Contains(new Vector2(i, j))) { tm.ForceTextureBuilds.Remove(new Vector2(i, j)); } RebuildTileTexture(tm, i, j, ref tile, solid, ref tex); } var drawRoot = new Vector2(i * tex.Width - ihn.CameraPos.X, j * tex.Height - ihn.CameraPos.Y); if (rx != -1 || ry != -1) { drawRoot.X = rx; drawRoot.Y = ry; } RenderTile(spriteBatch, tm, tile, solid, drawRoot, _r, tex); } }
/// <summary> /// Moves the entity contained in Ihn /// </summary> /// <param name="ihn">Ihn entity is contained in</param> /// <param name="entity">Entity to accelerate</param> public void Update(Ihn ihn, Entity entity) { var pos = entity.GetComp <ComponentPosition>(); var velocity = entity.GetComp <ComponentVelocity>(); if ((velocity.X != 0 || velocity.Y != 0)) { if (Math.Abs(velocity.X) > Math.Abs(velocity.Y)) { MoveX(ihn, entity, pos, velocity); MoveY(ihn, entity, pos, velocity); } else { MoveY(ihn, entity, pos, velocity); MoveX(ihn, entity, pos, velocity); } } }
/// <summary> /// Updates the size of this component /// </summary> /// <param name="ihn">Ihn calling this function</param> public override void Update(Ihn ihn) { base.Update(ihn); List <string> toRender = new List <string>(); var newText = Text; while (newText.Contains("\n")) { toRender.Add(newText.Substring(0, newText.IndexOf("\n"))); newText = newText.Substring(newText.IndexOf("\n") + 1); } toRender.Add(newText); int _y = 0; for (int i = 0; i < toRender.Count; i++) { _y += TextSize; } this.Size.Y = _y; }//TODO: Store toRender
/// <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)); } } }
}//TODO: Store toRender /// <summary> /// Renders the text /// </summary> /// <param name="ihn">Ihn calling this function</param> /// <param name="spriteBatch">Spritebatch to render with</param> public override void Render(Ihn ihn, SpriteBatch spriteBatch) { List <string> toRender = new List <string>(); var newText = Text; while (newText.Contains("\n")) { toRender.Add(newText.Substring(0, newText.IndexOf("\n"))); newText = newText.Substring(newText.IndexOf("\n") + 1); } toRender.Add(newText); int _y = 0; for (int i = 0; i < toRender.Count; i++) { Font.Draw(spriteBatch, (int)Root.X, (int)Root.Y + _y, toRender[i], Col, TextSize); _y += TextSize; } this.Size.Y = _y; base.Render(ihn, spriteBatch); }
/// <summary> /// Saves an entire Ihn, all of the systems and entities anyways /// </summary> /// <param name="ihn">Ihn to save</param> /// <param name="saveFile">Path saved to</param> public static void Save(Ihn ihn, string saveFile) { List <string> dat = new List <string>(); File.Delete(saveFile); Stream stream = File.Open(saveFile, FileMode.Create); for (int i = 0; i < ihn.EntityCount; i++) { var ent = ihn.GetEntityAt(i); BinaryFormatter bin = new BinaryFormatter(); bin.Serialize(stream, ent); } for (int i = 0; i < ihn.SystemCount; i++) { var sys = ihn.GetSystemAt(i); BinaryFormatter bin = new BinaryFormatter(); bin.Serialize(stream, sys); } stream.Close(); }
/// <summary> /// Snaps the entity to its follow /// </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 follow = entity.GetComp <ComponentFollow>(); if (follow.ToFollow.HasComp <ComponentPosition>()) { pos.X = follow.ToFollow.GetComp <ComponentPosition>().X + follow.Offset.X; pos.Y = follow.ToFollow.GetComp <ComponentPosition>().Y + follow.Offset.Y; } if (follow.FollowRotation) { var rot = 0f; if (follow.ToFollow.HasComp <ComponentMultiSprite>()) { rot = follow.ToFollow.GetComp <ComponentMultiSprite>().Rotation; } else if (follow.ToFollow.HasComp <ComponentSprite>()) { rot = follow.ToFollow.GetComp <ComponentSprite>().Rotation; } if (entity.HasComp <ComponentMultiSprite>()) { entity.GetComp <ComponentMultiSprite>().Rotation = rot; } if (entity.HasComp <ComponentSprite>()) { entity.GetComp <ComponentSprite>().Rotation = rot; } } if (follow.FollowDestroy) { if (!ihn.ContainsEntity(follow.ToFollow)) { Ihn.Instance.RemoveEntity(entity); } } }
/// <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> /// Updates status of gui control /// </summary> /// <param name="ihn">Ihn that triggered the update</param> public virtual void Update(Ihn ihn) { for (int i = 0; i < Children.Count; i++) { if (Children[i].Enabled) { Children[i].Update(ihn); } } if (Enabled) { var shouldInvokeMouseEvents = true; for (int i = 0; i < Children.Count; i++) { if (Children[i].Enabled) { if (new Rectangle(MouseHelper.X, MouseHelper.Y, 1, 1).Intersects(new FloatRect(Children[i].Root.X, Children[i].Root.Y, Children[i].Size.X, Children[i].Size.Y).ToRect())) { shouldInvokeMouseEvents = false; if (_mouseOver) { _mouseOver = false; if (MouseExit != null) { MouseExit.Invoke(this); } } break; } } } if (shouldInvokeMouseEvents) { InvokeMouseEvents(); } } }
/// <summary> /// Renders the tiletype /// </summary> /// <param name="ihn">Ihn calling the render</param> /// <param name="spriteBatch">Spritebatch to render with</param> public override void Render(Ihn ihn, SpriteBatch spriteBatch) { SystemTilemap.RenderTile(spriteBatch, ihn.GetEntitiesWith <ComponentTilemap>()[0].GetComp <ComponentTilemap>(), (TileType)this.Tag, new List <Direction>(), Root); base.Render(ihn, spriteBatch); }
/// <summary> /// This system does not render /// </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) { }
/// <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> /// This system does not update /// </summary> /// <param name="ihn">Ihn entity is contained in</param> /// <param name="entity">Entity to update</param> public void Update(Ihn ihn, Entity entity) { }
/// <summary> /// Moves the entity /// </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 move = entity.GetComp <ComponentKeyboardMovement>(); ComponentVelocity velocity = null; if (entity.HasComp <ComponentVelocity>()) { velocity = entity.GetComp <ComponentVelocity>(); } bool n; bool e; bool s; bool w; n = KeyHelper.KeyDown(Keys.W); e = KeyHelper.KeyDown(Keys.D); s = KeyHelper.KeyDown(Keys.S); w = KeyHelper.KeyDown(Keys.A); Vector2 momentum = new Vector2(); if (velocity == null) { pos.X += e ? move.Speed * move.HorizontalModifier : 0; momentum.X += e ? move.Speed * move.HorizontalModifier : 0; if (CollisionHelper.Colliding(ihn, entity)) { pos.X -= e ? move.Speed * move.HorizontalModifier : 0; } pos.X += w ? -move.Speed * move.HorizontalModifier : 0; momentum.X += w ? -move.Speed * move.HorizontalModifier : 0; if (CollisionHelper.Colliding(ihn, entity)) { pos.X -= w ? -move.Speed * move.HorizontalModifier : 0; } pos.Y += s ? move.Speed * move.VerticalModifier : 0; momentum.Y += s ? move.Speed * move.VerticalModifier : 0; if (CollisionHelper.Colliding(ihn, entity)) { pos.Y -= s ? move.Speed * move.VerticalModifier : 0; } pos.Y += n ? -move.Speed * move.VerticalModifier : 0; momentum.Y += n ? -move.Speed * move.VerticalModifier : 0; if (CollisionHelper.Colliding(ihn, entity)) { pos.Y -= n ? -move.Speed * move.VerticalModifier : 0; } } else { velocity.X += e ? move.Speed * move.HorizontalModifier : 0; momentum.X += e ? move.Speed * move.HorizontalModifier : 0; velocity.X += w ? -move.Speed * move.HorizontalModifier : 0; momentum.X += w ? -move.Speed * move.HorizontalModifier : 0; velocity.Y += s ? move.Speed * move.VerticalModifier : 0; momentum.Y += s ? move.Speed * move.VerticalModifier : 0; velocity.Y += n ? -move.Speed * move.VerticalModifier : 0; momentum.Y += n ? -move.Speed * move.VerticalModifier : 0; } if (entity.HasComp <ComponentDirection>() && (momentum.X != 0 || momentum.Y != 0)) { var dir = entity.GetComp <ComponentDirection>(); dir.Dir = DirectionHelper.VectorToDirection(momentum); } }
/// <summary> /// Renders this control /// </summary> /// <param name="ihn">Ihn calling the render</param> /// <param name="spriteBatch">Spritebatch to render with</param> public virtual void Render(Ihn ihn, SpriteBatch spriteBatch) { }