protected override void Start() { base.Start(); // Utility AI setup // ****** VALUES ****** UAIV_AgentHealth agentHealth = new UAIV_AgentHealth(this, _AgentController._AgentStats.HealthPoints); UAIV_AgentFood agentFood = new UAIV_AgentFood(this, _AgentController._AgentStats.FoodPoints); UAIV_AgentEnergy agentEnergy = new UAIV_AgentEnergy(this, _AgentController._AgentStats.EnergyPoints); UAIV_LumberyardPlaced lumberyardPlaced = new UAIV_LumberyardPlaced(this, 1); UAIV_ResourceCount treeCount = new UAIV_ResourceCount(GameCache._Cache.GetData("Tree").tag, this, 4); UAIV_InventorySize inventorySize = new UAIV_InventorySize(this, _AgentController._Inventory._MaxInventorySize); UAIV_SoldierEnemyCount enemySoldierCount = new UAIV_SoldierEnemyCount(this, 3.0f); // ****** SCORERS ****** UtilityScorer scorer_AgentHealth = new UtilityScorer(agentHealth, _HealthCurve); UtilityScorer scorer_AgentFood = new UtilityScorer(agentFood, _FoodCurve); UtilityScorer scorer_AgentEnergy = new UtilityScorer(agentEnergy, _EnergyCurve); UtilityScorer scorer_LumberyardBoolCheck = new UtilityScorer(lumberyardPlaced, _LumberyardPlacedCurve); UtilityScorer scorer_TreeCount = new UtilityScorer(treeCount, _TreeCountCurve); UtilityScorer scorer_InventorySize = new UtilityScorer(inventorySize, _InventorySizeCurve); UtilityScorer scorer_enemySoldierThreatLevel = new UtilityScorer(enemySoldierCount, _EnemySoldierThreatCurve); // ****** ACTIONS ****** RoamAround roamAction_SearchTrees = new RoamAround(this, 1.0f); roamAction_SearchTrees.AddScorer(scorer_TreeCount); roamAction_SearchTrees.AddScorer(scorer_LumberyardBoolCheck); ChopTree chopTreeAction = new ChopTree(this, 0.5f); DeliverResources deliverResourceAction = new DeliverResources(GameCache._Cache.GetData("Lumberyard").tag, this, 0.0f); deliverResourceAction.AddScorer(scorer_InventorySize); EatFood eatFoodAction = new EatFood(this, 0.0f); eatFoodAction.AddScorer(scorer_AgentFood); eatFoodAction.SetWeight(2); SleepAndRest sleepRestAction = new SleepAndRest("Lumberyard", this, 0.0f); sleepRestAction.AddScorer(scorer_AgentEnergy); Hide hideAction = new Hide("Headquarters", this, 0.0f); hideAction.AddScorer(scorer_enemySoldierThreatLevel); hideAction.SetWeight(3); // ****** REGISTER ACTIONS ****** _AgentActions.Add(roamAction_SearchTrees); _AgentActions.Add(chopTreeAction); _AgentActions.Add(deliverResourceAction); _AgentActions.Add(eatFoodAction); _AgentActions.Add(sleepRestAction); _AgentActions.Add(hideAction); }
/// <summary> /// Handle the various events that may have occurred. /// </summary> public override void handleEvents() { Vector2 newVelocity = Vector2.Zero; Vector2 newImpulse = Vector2.Zero; if (gameWorld.gravity.Y != 0 && !my_Body.noGravity) { newVelocity = my_Body.GetLinearVelocity(); my_Body.SetLinearDamping(1f); } else { my_Body.SetLinearDamping(0f); } GUI.GUIManager.getGUIManager(game, game.Content).Inventory = inventory; while (eventList.Count > 0) { Event e = eventList[0]; eventList.RemoveAt(0); switch (e.type) { case ("Set_Player_Color"): { if ((e._value as String).Equals("Blue")) { color = Color.Blue; } if ((e._value as String).Equals("Red")) { color = Color.Red; } if ((e._value as String).Equals("Green")) { color = Color.Green; } break; } case (EventList.DisplayInventory): { myGUIManager.toggleDisplayInventory(); myGUIManager.setInventoryData(inventory, EntityManager.inventoryImages, "Player"); break; } case (EventList.DisplayQuestLog): { myGUIManager.displayQuestLog(); break; } case (EventList.TogglePlayerGUI): { myGUIManager.PlayersGUI.IsHidden = !myGUIManager.PlayersGUI.IsHidden; break; } case (EventList.RotateCannonCCW): { myCannon = gameWorld.cannon; if (myCannon != null) { myCannon.addEvent(new Event(EventList.RotateCannonCCW, null)); } break; } case (EventList.RotateCannonCW): { myCannon = gameWorld.cannon; if (myCannon != null) { myCannon.addEvent(new Event(EventList.RotateCannonCW, null)); } break; } case (EventList.playerCannonBallLaunch): { myCannon = gameWorld.cannon; if (myCannon != null) { myCannon.addEvent(new Event(EventList.playerCannonBallLaunch, null)); } break; } case (EventList.PlayerDown): { if (gameWorld.gravity.Y == 0 || my_Body.noGravity) { int value = (int)e._value; newVelocity.Y = value; } break; } case (EventList.PlayerUp): { if (gameWorld.gravity.Y == 0 || my_Body.noGravity) { int value = (int)e._value; newVelocity.Y = -value; } else { if (jumpcount < 2 && canJump) { int value = (int)e._value; float mass = my_Body.GetMass(); newImpulse = new Vector2(0, -value * mass); canJump = false; jumpcount++; newImpulse /= jumpcount; } } break; } case ("NotUp"): { canJump = true; break; } case (EventList.PlayerLeft): { int value = (int)e._value; newVelocity.X = -value; break; } case (EventList.PlayerRight): { int value = (int)e._value; newVelocity.X = value; break; } case (EventList.FireVaccine): { if (inventory.ContainsKey("DartGun") && inventory.takeItems("Vaccine", 1)) { EntityManager entityManager = EntityManager.getEntityManager(game); Entity newEntity = new Projectile(gameWorld, entityManager.getType("SyringeBlock"), 1000, new Event("Vaccinate", "PlaciboVax")); Vector2 position = my_Body.Position * 64; Vector2 vel = my_Body.GetLinearVelocity(); if (animation.Contains("Right")) { position.X += type.images[0].Width + newEntity.type.images[0].Width + 5; position.Y += (type.images[0].Height / 2) - (newEntity.type.images[0].Height / 2); newEntity.animation = "3"; vel.X += 10; } if (animation.Contains("Left")) { position.X -= newEntity.type.images[0].Width - 5; position.Y += (type.images[0].Height / 2) - (newEntity.type.images[0].Height / 2); newEntity.animation = "2"; vel.X -= 10; } if (animation.Contains("Up")) { position.Y -= newEntity.type.images[0].Height - 5; position.X += (type.images[0].Width / 2) - (newEntity.type.images[0].Width / 2); newEntity.animation = "_"; vel.Y -= 10; } if (animation.Contains("Down")) { position.Y += type.images[0].Height + newEntity.type.images[0].Height + 5; position.X += (type.images[0].Width / 2) - (newEntity.type.images[0].Width / 2); newEntity.animation = "1"; vel.Y += 10; } newEntity.color = Color.White; newEntity.type.type = TypeOfThing.PROJECTILE; newEntity.my_Body = gameWorld.AddBody(position / 64, newEntity); newEntity.my_Body.SetLinearVelocity(vel); gameWorld.addEntity(newEntity, null); } break; } case (EventList.PlaceBuilding): { String str = e._value as String; if (str.Equals("Bar") && ai != null) { (ai as BarPlacer).placeBar(); } if (str.Equals("SmlResBuilding")) { EntityManager entityManager = EntityManager.getEntityManager(game); Entity newEntity = new Building(gameWorld, entityManager.getType("SmlResBuilding")); Vector2 position = my_Body.Position * 64; if (animation.Contains("Right")) { position.X += type.images[0].Width + 5; } if (animation.Contains("Left")) { position.X -= newEntity.type.images[0].Width - 5; } if (animation.Contains("Up")) { position.Y -= newEntity.type.images[0].Height - 5; } if (animation.Contains("Down")) { position.Y += type.images[0].Height + 5; } newEntity.color = Color.Moccasin; newEntity.type.type = TypeOfThing.BUILDING; newEntity.animation = "_"; gameWorld.navStuff.addRectangle(new Rectangle((int)position.X, (int)position.Y, (int)newEntity.type.size.X, (int)newEntity.type.size.Y)); newEntity.my_Body = gameWorld.AddBody(position / 64, newEntity); gameWorld.addEntity(newEntity, null); } break; } case (EventList.AbsorbObjects): { collectItems(); break; } case (EventList.IncreaseVelocity): { newVelocity.Y -= myGlidingAcceleration; newVelocity.X += (myGlidingAcceleration / 5.0f); break; } case (EventList.DecreaseVelocity): { newVelocity.Y += myGlidingAcceleration; newVelocity.X += (myGlidingAcceleration / 5.0f); break; } case (EventList.ExitShopItems): { if (myGUIManager.isShopMenuShowing()) { myGUIManager.displayShopMenu(); } break; } case (EventList.PlantTree): { EntityManager entityManager = EntityManager.getEntityManager(game); Entity newEntity = new ChopTree(gameWorld, entityManager.getType("ChopTree")); Vector2 position = my_Body.Position * 64; newEntity.color = Color.White; newEntity.type.type = TypeOfThing.CHOPTREE; newEntity.animation = "baby"; gameWorld.navStuff.addRectangle(new Rectangle((int)position.X, (int)position.Y, (int)newEntity.type.size.X, (int)newEntity.type.size.Y)); newEntity.my_Body = gameWorld.AddBody(position / 64, newEntity); gameWorld.addEntity(newEntity, null); break; } case (EventList.GoToPoint): { try { my_Body.Position = (Vector2)e._value; } catch (InvalidCastException ice) { String[] strs = (e._value as String).Split(','); Vector2 point = new Vector2(float.Parse(strs[0]), float.Parse(strs[1])); my_Body.Position = point; } my_Body.SetLinearVelocity(Vector2.Zero); enteringWorld = 20; break; } case (EventList.GrabIRemainingtems): { EntityManager em = EntityManager.getEntityManager(game); String worldName = em.getCurrentGameWorld().name; List <Entity> ents = e._value as List <Entity>; int i = 0; foreach (Entity itemEnt in ents) { if (itemEnt.type.type == TypeOfThing.ITEM2) { if (itemEnt.my_Body.Position.Y < 15) { i++; inventory.AddItems((itemEnt as Item).itemType, 1); } } } if (activeQuest != null) { if (activeQuest.completedCallback != null) { activeQuest.completedCallback(i); } } switchBody("Main"); em.reloadGameWorld(worldName); break; } case (EventList.SwitchWorld): { String[] strs = (e._value as String).Split(','); switchBody(strs[0]); EntityManager.getEntityManager(game).setCurrentGameWorld(strs[0]); copyInventory(); if (strs.Length > 1) { Vector2 pos = new Vector2(float.Parse(strs[1]), float.Parse(strs[2])); my_Body.Position = pos; } break; } case (EventList.QuestCompletedReturn): { EntityManager em = EntityManager.getEntityManager(game); String worldName = em.getCurrentGameWorld().name; String worldToReturnTo = e._value as String; if (activeQuest != null) { if (activeQuest.completedCallback != null) { activeQuest.completedCallback(null); } } if (worldName != worldToReturnTo) { switchBody(worldToReturnTo); em.reloadGameWorld(worldName); } // ai.nextConversation(); //SoundManager.createSoundManager(game.Content).addEvent(new Event(EventList.PlaySound, "pickup")); break; } case (EventList.ResetWorld): { String[] strs = (e._value as String).Split(','); Vector2 pos = new Vector2(float.Parse(strs[0]), float.Parse(strs[1])); EntityManager em = EntityManager.getEntityManager(game); String worldName = em.getCurrentGameWorld().name; switchBody("Main"); em.reloadGameWorld(worldName); switchBody(worldName); my_Body.Position = pos; break; } case (EventList.ResetWorld_Checkpoint): { Vector2 pos = gameWorld.lastCP; EntityManager em = EntityManager.getEntityManager(game); String worldName = em.getCurrentGameWorld().name; switchBody("Main"); em.reloadGameWorld(worldName); switchBody(worldName); my_Body.Position = pos; resetInventory(); break; } default: { //throw new Exception("Unknown Event: " + e.type); break; } } } if (enteringWorld <= 0) { if (!gameWorld.name.Contains("Cannon Game")) { if (newVelocity.Y < -4) { newVelocity.Y = -4; } } else { if (newVelocity.X < 0) { newVelocity.X = 0; } } if (gameWorld.name.Contains("Doctor3") && newVelocity.Y > 3) { newVelocity.Y = 3; } my_Body.SetLinearVelocity(newVelocity); my_Body.ApplyLinearImpulse(newImpulse, my_Body.GetWorldCenter()); } else { newVelocity.X = 0; my_Body.SetLinearVelocity(newVelocity); enteringWorld--; } if (gameWorld.gravity == Vector2.Zero) { if (newVelocity.Equals(Vector2.Zero)) { if (animation.Contains("Standing")) { } else if (animation.Contains("Walking")) { animation = animation.Replace("Walking", "Standing"); } else { throw new Exception("Animation State not recognized"); } } else if (newVelocity.X > 0) { animation = "Walking Right"; } else if (newVelocity.X < 0) { animation = "Walking Left"; } else if (newVelocity.Y > 0) { animation = "Walking Down"; } else if (newVelocity.Y < 0) { animation = "Walking Up"; } } else { if (newVelocity.Equals(Vector2.Zero)) { if (animation.Contains("Standing")) { } else if (animation.Contains("Walking")) { animation = animation.Replace("Walking", "Standing"); } else { throw new Exception("Animation State not recognized"); } } if (newVelocity.X > .5) { animation = "Walking Right"; } else if (newVelocity.X < -.5) { animation = "Walking Left"; } else { animation = animation.Replace("Walking", "Standing"); } } }