public void InitializePlayers() { int healthBarNO = 1; int hungerBarNO = 1; for (int i = 0; i < numPlayers.Count; i++) { Player player = new Player(numPlayers[i].Item1, numPlayers[i].Item2, engine.GraphicsDevice.Viewport.Width / 2 + (80 * (i + 1)), engine.GraphicsDevice.Viewport.Height / 2, 72, 62, 0, new BoundingBox(new Vector3(engine.GraphicsDevice.Viewport.Width / 2 - (72 / 4), engine.GraphicsDevice.Viewport.Height / 2 - (62 / 4), 0), new Vector3(engine.GraphicsDevice.Viewport.Width / 2 + (72 / 4), engine.GraphicsDevice.Viewport.Height / 2 + (62 / 4), 0)), 1, null, true); HealthBar healthBar = new HealthBar("healthBar" + healthBarNO, player.X, player.Y - 50, player.Health, 10, 0, new BoundingBox(new Vector3(player.X - 100, player.Y - 100, 0), new Vector3(player.X - 100, player.Y - 100, 0)), 0, null, false, player); HungerBar hungerBar = new HungerBar("hungerBar" + hungerBarNO, player.X, player.Y - 70, player.Hunger, 10, 0, new BoundingBox(new Vector3(player.X - 100, player.Y - 100, 0), new Vector3(player.X - 100, player.Y - 100, 0)), 0, null, false, player); engine.AddEntity(healthBar); engine.AddEntity(hungerBar); players.Add(player); healthBarNO++; hungerBarNO++; } }
/* TODO: Make this class more dynamic. Either change name of the class to PlayerObserver, * or make it able to change in all entities not only in player entities. * */ //Called by engine. In this method all the changes to the player is made //Hint: Not fully working yet, needs to be more dynamic. The collision management only works at certain key input public void OnNext(GameTime gameTime) { if (MainGame.currentState == GameState.Game) { for (int i = 0; i < engine.Entities.Count; i++) { if (engine.Entities [i].GetType() == typeof(Player)) { HandlePlayer(engine.Entities [i]); } else if (engine.Entities[i].GetType() == typeof(Wolf)) { Wolf wolf = (Wolf)engine.Entities[i]; wolf.UpdateWolf(engine.Entities); } else if (engine.Entities [i].GetType() == typeof(HealthBar)) { HealthBar hb = (HealthBar)engine.Entities [i]; Player player = (Player)engine.Entities.Find(e => e.ID.Equals(hb.ConectedTo.ID)); hb.updatePosition(player.Health); } else if (engine.Entities [i].GetType() == typeof(HungerBar)) { HungerBar hunb = (HungerBar)engine.Entities [i]; Player player = (Player)engine.Entities.Find(e => e.ID.Equals(hunb.ConectedTo.ID)); hunb.updatePosition(player.Hunger); } } } else if (MainGame.currentState == GameState.InGameMenu || MainGame.currentState == GameState.OptionMenu || MainGame.currentState == GameState.PlayGameMenu || MainGame.currentState == GameState.StartMenu) { if (engine.Entities.Exists(e => e.GetType().IsSubclassOf(typeof(MenuComponent)))) { TimeSpan time = gameTime.TotalGameTime; if (oldtimespan.Ticks == 0 || time.TotalMilliseconds - oldtimespan.TotalMilliseconds > 200) { HandleMenuComponent(time); } } compSet = false; } bool temp = timeCheck(gameTime); for (int i = 0; i < engine.Entities.Count; i++) { if (engine.Entities [i].GetType() == typeof(Player)) { Player player = (Player)engine.Entities [i]; if (temp) { player.Hunger = player.Hunger - 2; } } } }
//Defines which content should be ed for the specified entity /*TODO iteration3: First of all add more content to the game engine, but it should also * be able to content not only for a player but also other kinds of objects */ public void OnNext(List <Texture2D> value) { for (int i = 0; i < engine.Entities.Count; i++) { if (engine.Entities [i].GetType() == typeof(Player)) { HandlePlayerContent(value, i); } else if (engine.Entities [i].GetType() == typeof(Tile)) { Tile tile = (Tile)engine.Entities [i]; if (tile.Texture == null) { Random r = new Random(int.Parse(Guid.NewGuid().ToString().Substring(0, 8), System.Globalization.NumberStyles.HexNumber)); int randInt = r.Next(1, nTileTypes + 1); tile.Texture = value.Find(x => x.Name.Equals("tile_" + randInt)); } engine.Entities [i] = tile; } if (engine.Entities [i].GetType() == typeof(HealthBar)) { HealthBar healthBar = (HealthBar)engine.Entities [i]; if (healthBar.Texture == null) { engine.AddTextureOnEntity("health_Bar", healthBar.ID); } } if (engine.Entities [i].GetType() == typeof(HungerBar)) { HungerBar hungerBar = (HungerBar)engine.Entities [i]; if (hungerBar.Texture == null) { engine.AddTextureOnEntity("hunger_Bar", hungerBar.ID); } } if (engine.Entities[i].GetType() == typeof(Bush)) { Bush bush = (Bush)engine.Entities [i]; if (bush.Texture == null) { bush.Texture = value.Find(x => x.Name.Equals("bush_2")); } //engine.Entities[i] = bush; if (bush.IsUsed) { engine.AddTextureOnEntity("bush_1", bush.ID); } } if (engine.Entities[i].GetType() == typeof(Wolf)) { HandleWolfContent(value, i); } else if (engine.Entities [i].GetType() == typeof(Button)) { Button button = (Button)engine.Entities [i]; HandleButtonTexture(button); } else if (engine.Entities [i].GetType() == typeof(CheckBox)) { CheckBox checkBox = (CheckBox)engine.Entities [i]; if (checkBox.Texture == null || !checkBox.IsHighlighted) { if (!checkBox.IsChecked) { engine.AddTextureOnEntity("CheckBox", checkBox.ID); } else { engine.AddTextureOnEntity("CheckBox_C", checkBox.ID); } } else if (checkBox.IsHighlighted) { if (!checkBox.IsChecked) { engine.AddTextureOnEntity("CheckBox_Y", checkBox.ID); } else { engine.AddTextureOnEntity("CheckBox_C_Y", checkBox.ID); } } } else if (engine.Entities [i].GetType() == typeof(RenderedEntity)) { RenderedEntity rendEnt = (RenderedEntity)engine.Entities [i]; if (rendEnt.Texture == null) { engine.AddTextureOnEntity("Menu", rendEnt.ID); } } else if (engine.Entities [i].GetType() == typeof(OptionBar)) { OptionBar optionBar = (OptionBar)engine.Entities [i]; HandleOptionBar(optionBar); } } }