public override void Update() { if (_memory.Count >= 5) { _memory.RemoveAt(0); } _memory.Add(CurrentTile); VisibleTiles.Add(CurrentTile); LookForFood(); LookForDanger(); if (_targetTile == null && CurrentHunger > 5 && _foodSource != null) { // is hungry and has a known food source with no other targets _targetTile = _foodSource; } MoveToTarget(); TryToEat(); }
public WorldModel(short tileSize) { TileSize = tileSize; int biomes = Enum.GetNames(typeof(Biome)).Length; for (short x = 0; x < TilesHeight; x++) { for (short y = 0; y < TilesWidth; y++) { Tiles[x, y] = new Tile(x, y, (Biome)MathHelper.Random.Next(0, biomes), TileSize, TileSize); if (Tiles[x, y].Biome == Biome.Forest1 && MathHelper.Random.Next(1, 100) > 75) { Tiles[x, y].AddContent(new Grass()); } if (Tiles[x, y].Biome == Biome.Forest3 && MathHelper.Random.Next(1, 100) > 95) { Tiles[x, y].AddContent(new Tree()); } if (MathHelper.Random.Next(1, 100) > 80) { Tiles[x, y].AddContent(new Rock()); } if (MathHelper.Random.Next(1, 1000) > 998) { Tiles[x, y].AddContent(new Flint()); } } } // critters, ai is still broken, figure out movespeed first //for (short i = 0; i < 1000; i++) //{ // Tiles[MathHelper.Random.Next(1, TilesWidth - 1), MathHelper.Random.Next(1, TilesHeight - 1)].AddContent(new Magentaur()); //} Player = new Player(); Tiles[MathHelper.Random.Next(1, TilesWidth - 1), MathHelper.Random.Next(1, TilesHeight - 1)].AddContent(Player); // Blueriors are immobile, useful for testing combat //Tiles[Player.CurrentTile.X, Player.CurrentTile.Y - 1].AddContent(new Bluerior()); //Tiles[Player.CurrentTile.X, Player.CurrentTile.Y - 1].AddContent(new Bluerior()); //Tiles[Player.CurrentTile.X, Player.CurrentTile.Y - 1].AddContent(new Bluerior()); ViewOffset = new Vector2(Player.CurrentTile.X * TileSize - 8 * tileSize, Player.CurrentTile.Y * TileSize - 8 * TileSize); }
public void Use(Tile currentTile) { List<Tile> tiles = new List<Tile> { currentTile }; IlluminationHelper.Illuminate(this, tiles, IlluminatedTiles, true); }
private void LookForDanger() { Tile dangerTile = null; foreach (Tile t in VisibleTiles.Where(t => t.TileContents.Count > 0)) { foreach (IEntity e in t.TileContents.Where(f => f is IFeeder)) { IAnimate animate = e as IAnimate; IFeeder feeder = e as IFeeder; if (feeder.DesiredFood.HasFlag(ProvidesFoodType) && animate != null && animate.Damage > Damage) { // this thing eats and what it eats is me dangerTile = t; break; } } } if (dangerTile != null) { // run away this thing potentially wants to eat me int maxDist = 0; foreach (Tile visTile in VisibleTiles) { if (visTile == dangerTile) continue; int tileDistance = Math.Abs((visTile.X - CurrentTile.X)) + Math.Abs((visTile.Y - CurrentTile.Y)); if (tileDistance > maxDist) { maxDist = tileDistance; _targetTile = visTile; } } } if (_foodSource != null && _targetTile != _foodSource) { // no danger, amble around _targetTile = null; } }
private void TryToEat() { if (CurrentHunger > -10) { for (int i = 0; i < CurrentTile.TileContents.Count; i++) { IEntity e = CurrentTile.TileContents[i]; IEdible edible = e as IEdible; if (edible != null && edible.ProvidesFoodType != 0 && DesiredFood.HasFlag(edible.ProvidesFoodType)) { IAnimate animate = e as IAnimate; if (animate != null) { if (animate.Health > 0) { // attack animate.Health -= Damage; Health -= animate.Damage; } else { CurrentHunger -= edible.GetEaten(); } } } } } else { // full if (_targetTile == _foodSource) _targetTile = null; } }
private void LookForFood() { if (_foodSource == null || CurrentHunger > 20) { // search for a tile with food on it Tile closest = null; int distance = int.MaxValue; foreach (Tile t in VisibleTiles.Where(t => t.TileContents.Count > 0)) { foreach (IEntity e in t.TileContents) { if (e == this || e.GetType() == GetType()) { continue; } IEdible edible = e as IEdible; if (edible == null) { continue; } if (edible.ProvidesFoodType != 0 && DesiredFood.HasFlag(edible.ProvidesFoodType)) { if (closest == null) { closest = t; distance = Math.Abs(CurrentTile.X - t.X) + Math.Abs(CurrentTile.Y - t.Y); } else { int newDistance = Math.Abs(CurrentTile.X - t.X) + Math.Abs(CurrentTile.Y - t.Y); if (newDistance < distance) { closest = t; distance = newDistance; } } } } } if (closest != null) _foodSource = closest; } }
public bool Move(Tile targetTile) { // always 0, 1 or 2 int distance = Math.Abs((CurrentTile.X - targetTile.X)) + Math.Abs((CurrentTile.Y - targetTile.Y)); if (distance <= 2) { CurrentTile.Move(this, targetTile); return true; } return false; }
private static Texture2D GetTexture(Tile tile, GraphicsDevice device) { string name = "Tile:" + tile.Biome; if (!CachedTextures.ContainsKey(name)) { Texture2D texture = new Texture2D(device, tile.Width, tile.Height); Color color; switch (tile.Biome) { case Biome.Forest1: color = Color.Green; break; case Biome.Forest2: color = Color.DarkGreen; break; case Biome.Forest3: color = Color.ForestGreen; break; default: color = Color.White; break; } Color[] texData = new Color[tile.Width * tile.Height]; bool toggle = true; for (int i = 0; i < texData.Length; i++) { if (i % tile.Width == 0 || i >= texData.Length - tile.Height) { if (toggle) { texData[i] = color; toggle = false; } else { texData[i] = Color.Black; toggle = true; } } else { texData[i] = color; } } texture.SetData(texData); CachedTextures.Add(name, texture); } return CachedTextures[name]; }
internal void Move(IEntity entity, Tile tile) { if (!tile.Passable(entity)) return; tile.AddContent(entity); TileContents.Remove(entity); }
private static void UpdateTileStatics(Tile tile, WorldModel worldModel) { for (int i = 0; i < tile.TileContents.Count; i++) { LivingEntity e = tile.TileContents[i] as LivingEntity; if (e != null) { IAnimate animate = e as IAnimate; if (animate != null) { if (animate.Health <= 0) { // unit is dead continue; } } ISighted sighted = e as ISighted; if (sighted != null) { sighted.VisibleTiles = worldModel.GetVisibleTiles(tile.X, tile.Y, sighted); } IMobile mobile = e as IMobile; if (mobile != null) { mobile.AdjacentTiles = worldModel.GetAdjacentTiles(tile.X, tile.Y); } IFeeder feeder = e as IFeeder; if (feeder != null) { feeder.CurrentHunger += feeder.HungerRate; ILiving living = e as ILiving; if (feeder.CurrentHunger > 50 && living != null) { living.Health -= feeder.HungerRate; } } IIlluminator iluminator = e as IIlluminator; if (iluminator != null) { iluminator.Illuminate(); } } } }
private static void UpdateTileDynamics(Tile tile, List<IEntity> processedEntities, WorldModel worldModel) { for (int i = 0; i < tile.TileContents.Count; i++) { LivingEntity e = tile.TileContents[i] as LivingEntity; if (e != null && !processedEntities.Contains(e)) { IAnimate animate = e as IAnimate; if (animate != null) { if (animate.Health <= 0) { // unit is dead processedEntities.Add(e); continue; } } e.Update(); // entity moved or died if (e.CurrentTile != tile) { i--; } processedEntities.Add(e); } } }
public Color GetAmbientColor(Tile tile) { if (tile.Illuminators.Count > 0 || tile.TileContents.Any(entity => entity.Illuminated)) { return Color.White; } UpdateAmbient(); return _ambientColor; }
public void AddTile(List<Tile> tiles, Tile tile) { if (tile == null) return; if (!tiles.Contains(tile)) tiles.Add(tile); }