/// <summary> /// This function will be called by the GUI to call for another cycle of the game to be run. This function will /// update all of the states of the Entitys in the GameWorld. /// </summary> public void updateWorld() { List <Entity> entitiesToRemove = new List <Entity>(); // Update all units. foreach (Unit u in gameWorld.getUnits()) { GameEventLogic.processEvents(u, gameWorld); updateEntity(u, entitiesToRemove); } // Update all buildings. foreach (Building b in gameWorld.getBuildings()) { updateEntity(b, entitiesToRemove); } /** Remove any entities **/ foreach (Entity e in entitiesToRemove) { locController.removeEntity(e); } curTick++; }
/// <summary> /// The basic outline of the updateEntity method is as follows: /// 1, Have the entity perform the current action at the top of it's action queue (if any) /// 2, Check for changes in the entity's stats. (ex: check for death.) /// 3, Have the Entity react to any Events that occur within it's visibility range. /// (Events that either occur to the Entity or events that the Entity can "see") /// 4, Check if the Entity should be removed from the game. (When it's primary state is set to Remove.) /// </summary> /// <param name="entity">Entity being updated.</param> /// <param name="entitiesToRemove">List of Entities to be removed.</param> private void updateEntity(Entity entity, List <Entity> entitiesToRemove) { /*** Have entity perform it's current action (if any) ***/ ActionController.Instance.update(entity, locController); Entity.EntityType type = entity.getEntityType(); // Enity's type (Unit, Building, Object or Resource) State state = entity.getState(); // Entity's State. /*** Update stats of Entity ***/ if (type == Entity.EntityType.Unit) { UnitStatsLogic.updateUnit((Unit)entity, curTick); } else { // Only Stats update occurs upon death of any StaticEntity right? if (entity.health <= 0) { state.setPrimaryState(State.PrimaryState.Dead); entity.tickKilled = curTick; } } /*** Have Entity react to any Events ***/ if (type == Entity.EntityType.Unit) // Only Units really need to react to Events. { GameEventLogic.processEvents((Unit)entity, scenario.getGameWorld()); } /*** Remove Entity if it needs to be removed. ***/ if (state.getPrimaryState() == State.PrimaryState.Dead) { entitiesToRemove.Add(entity); } }