/// <summary> /// Default Constructor. /// </summary> protected Level(SimulationContext context) { Context = new SimulationContext(context.Resolver, context.Settings); // Clone Settings for Slots slotSettings = new KeyValueStore[MAX_SLOTS]; for (int i = 0; i < MAX_SLOTS; i++) { slotSettings[i] = Settings.Clone(); } // Give the Level Designer the chance to change Faction/Slot Settings. DoSettings(Settings, slotSettings); Mode = LevelMode.Uninit; // Ermitteln des LevelDescriptionAttribute Type level = GetType(); object[] levelDescriptions = level.GetCustomAttributes(typeof(LevelDescriptionAttribute), false); if (levelDescriptions.Length != 1) { Mode = LevelMode.InitFailed; throw new NotSupportedException("No Level Description found"); } LevelDescription = (LevelDescriptionAttribute)levelDescriptions[0]; // Level-Extensions context.Resolver.ResolveLevel(this); }
/// <summary> /// Default Constructor for Type Mapper. /// </summary> /// <param name="context">Simulation Context</param> /// <param name="factoryType">Type of Factory Class</param> /// <param name="level">Reference to the Level</param> protected Faction(SimulationContext context, Type factoryType, Level level) { Context = context; Units = new Dictionary <Item, UnitGroup>(); Level = level; this.factoryType = factoryType; Context.Resolver.ResolveFaction(this); }
/// <summary> /// Default Constructor. /// </summary> /// <param name="context">Current Simulation Context</param> /// <param name="position">First Position of this Item</param> /// <param name="radius">Radius of this Item</param> /// <param name="orientation">First Orientation of this Item</param> public Item(SimulationContext context, Vector2 position, float radius, Angle orientation) { Context = context; Orientation = orientation; Radius = radius; Position = new Vector3(position.X, position.Y, 0); // Resolve Item Properties and Extender Context.Resolver.ResolveItem(this); }
/// <summary> /// Initializes the Faction. /// </summary> public void Init(byte slotIndex, byte teamIndex, string name, PlayerColor color, Random random, Vector2 home) { SlotIndex = slotIndex; TeamIndex = teamIndex; Name = name; PlayerColor = color; Home = home; // Creates the Simulation Context for this Faction Context = new SimulationContext(Context.Resolver, Context.Settings, random); // Factory für Ameisen erzeugen Factory = new FactoryGroup() { Factory = (FactionFactory)Activator.CreateInstance(factoryType), Interop = Context.Resolver.CreateFactoryInterop(this) }; Factory.Factory.Init(Factory.Interop); // TODO: Types checken! OnInit(); }
/// <summary> /// Initializes the Level. /// </summary> /// <param name="randomSeed">Randomizer Seed</param> /// <param name="slots">List of Slots</param> public void Init(int randomSeed, params LevelSlot[] slots) { // Init only allowed in Uninit-Mode. if (Mode != LevelMode.Uninit) { throw new NotSupportedException("Level is not ready for init"); } // Initialize the Randomizer. RandomSeed = (int)DateTime.Now.Ticks; if (randomSeed != 0) { RandomSeed = randomSeed; } // Generate the Simulation Context for this Level. Context = new SimulationContext( Context.Resolver, Context.Settings, new Random(RandomSeed)); // Check for the right number of Slots. if (slots.Length > MAX_SLOTS) { var exception = new ArgumentOutOfRangeException("There are too many Slots"); SetMode(LevelMode.InitFailed, exception); throw exception; } // Generate Engine engine = new Engine(Context.Resolver); // Generate Map and validate. Map map = GetMap(); if (map == null) { var exception = new NotSupportedException("No Map was created"); SetMode(LevelMode.InitFailed, exception); throw exception; } map.CheckMap(); int minPlayer = LevelDescription.MinPlayerCount; int maxPlayer = LevelDescription.MaxPlayerCount; // TODO: Ermitteln der Filter Attribute //object[] levelFilters = GetType().GetCustomAttributes(typeof(FactionFilterAttribute), false); //var filters = new Dictionary<int, List<FactionFilterAttribute>>(); //foreach (FactionFilterAttribute item in levelFilters) //{ // if (!filters.ContainsKey(item.SlotIndex)) // filters.Add(item.SlotIndex, new List<FactionFilterAttribute>()); // filters[item.SlotIndex].Add(item); //} // Check for Color Collisions. var colors = new List <PlayerColor>(); foreach (var slot in slots) { if (slot != null) { if (colors.Contains(slot.Color)) { var exception = new NotSupportedException("There are two Players with the same color"); SetMode(LevelMode.InitFailed, exception); throw exception; } colors.Add(slot.Color); } } // Gegencheck mit Level-Attributen int playerCount = 0; int highestSlot = 0; for (int i = 0; i < slots.Length; i++) { if (slots[i] != null) { // Counter playerCount++; highestSlot = i; // TODO: Filter //if (filters.ContainsKey(i) && filters[i].Count > 0) //{ // if (filters[i].Any(item => item.FactionType == slots[i].GetType())) // continue; // Mode = LevelMode.InitFailed; // Factions = null; // throw new NotSupportedException(string.Format( // "Faction '{0}' is not allowed in Slot {1}", // slots[i].GetType(), i)); //} } } // Faction Counts mit Map- und Level-Requirements gegenchecken if (playerCount < minPlayer) { var exception = new NotSupportedException(string.Format("Not enought player. Requires {0} Player", minPlayer)); SetMode(LevelMode.InitFailed, exception); throw exception; } if (playerCount > maxPlayer) { var exception = new NotSupportedException(string.Format("Too many player. Requires a Maximum of {0} Player", maxPlayer)); SetMode(LevelMode.InitFailed, exception); throw exception; } if (highestSlot > map.GetPlayerCount()) { var exception = new NotSupportedException(string.Format("Too many Slots used. Map has only {0} Slots", map.GetPlayerCount())); SetMode(LevelMode.InitFailed, exception); throw exception; } // Factions erzeugen Factions = new Faction[MAX_SLOTS]; for (int i = 0; i < slots.Length; i++) { if (slots[i] == null) { continue; } SimulationContext factionContext = new SimulationContext(Context.Resolver, slotSettings[i]); // Identify and generate Faction try { Factions[i] = Context.Resolver.CreateFaction(factionContext, slots[i].FactoryType, this); } catch (Exception ex) { SetMode(LevelMode.InitFailed, ex); throw; } // In Case the Faction could not be found... if (Factions[i] == null) { var exception = new Exception(string.Format("Cound not identify Faction for player {0}.", slots[i].Name)); SetMode(LevelMode.InitFailed, exception); throw exception; } } // State erzeugen mapState = new MapState(); mapState.BlockBorder = map.BlockBorder; mapState.Tiles = (MapTile[, ])map.Tiles.Clone(); engine.Init(map); engine.OnInsertItem += engine_OnInsertItem; engine.OnRemoveItem += engine_OnRemoveItem; // Fraktionen ins Spiel einbetten for (byte i = 0; i < Factions.Length; i++) { try { InitFaction(i, slots[i], Factions[i]); } catch (Exception ex) { SetMode(LevelMode.PlayerException, ex, i); Factions = null; throw; } } // Initialisierung des Levels try { OnInit(); } catch (Exception ex) { SetMode(LevelMode.InitFailed, ex); Factions = null; throw; } SetMode(LevelMode.Running); }