/// <summary> /// Creates a Tile of Membrane (default) type. /// </summary> /// <param name="x">Tile x-coordinate.</param> /// <param name="y">Tile y-coordinate.</param> public Tile(int x, int y) : base(new Vector2((float)(x+0.5), (float)(y+0.5)), null) { this.type = new TerrainType(); this.visibleTo = new HashSet<Player>(); this.units = new Dictionary<Player, HashSet<Unit>>(); this.allUnits = new List<Unit>(); this.building = null; this.lineDrawPoints = new List<Vector2>(); }
/// <summary> /// Creates a Tile of the type 'type'. /// </summary> /// <param name="type">Enum of the terrain type.</param> /// <param name="x">Tile x-coordinate.</param> /// <param name="y">Tile y-coordinate.</param> public Tile(int x, int y, Globals.TerrainTypes type) : base(new Vector2(x, y), null) { this.type = new TerrainType(type); this.visibleTo = new HashSet<Player>(); this.units = new Dictionary<Player, HashSet<Unit>>(); this.allUnits = new List<Unit>(); this.position = new Vector2(x, y); this.building = null; this.lineDrawPoints = new List<Vector2>(); }
public void init() { t1 = new Tile(0,0); t2 = new Tile(1, 1, Globals.TerrainTypes.Slow); p = new Player(); u1 = new Unit(p, Vector2.Zero); u2 = new Unit(p, Vector2.Zero); u3 = new Unit(p, Vector2.Zero); b1 = new BaseBuilding("test", 0, 0, new Player(), new LinkedList<Tile>()); b2 = new BarrierBuilding("TestBuilding1", 1, 1, p, b1, new LinkedList<Tile>()); }
/// <summary> /// Adds a fromBuilding to the graph. /// A fromBuilding that has already been added will be ignored. /// </summary> /// <param name="fromBuilding">The fromBuilding to add.</param> /// <exception cref="ArgumentException">If the fromBuilding is a base fromBuilding.</exception> public void Add(Building building) { if (buildings.ContainsKey(building)) { logger.Debug("Can not add building to graph. The building '" + building + "' already exists."); throw new ArgumentException("The building '" + building + "' already exists in a graph."); } lock(buildings) { buildings.Add(building, defaultWeight); TotalWeight += defaultWeight; } Publish(building, defaultWeight, EventType.ADD); }
/// <summary> /// Attempt to set a fromBuilding in this tile. /// </summary> /// <param name="fromBuilding">Building to place here.</param> /// <returns>True iff fromBuilding was placed, False if this Tile already is occupied.</returns> public bool SetBuilding(Building building) { if (this.building != null || building == null) { // Already occupied tile. return false; } else { // Building placed. this.building = building; if (buildingChanged != null) { buildingChanged(this, new Event<Building>(this.building, EventType.ADD)); } return true; } }
/// <summary> /// Removes fromBuilding in this tile. /// </summary> public void RemoveBuilding() { this.building = null; if (buildingChanged != null) { buildingChanged(this, new Event<Building>(this.building, EventType.REMOVE)); } }
/// <summary> /// Add a fromBuilding to the source buildings owners graph, /// the source fromBuilding will be used to find the correct graph. /// </summary> /// <param name="buildingType">The type of fromBuilding to build.</param> /// <param name="sourceBuilding">The fromBuilding used to build this fromBuilding.</param> /// <param name="targetCoordinate">The tile coordinates where the fromBuilding will be built.</param> /// <param name="world">The world to build the fromBuilding in.</param> public static bool AddBuilding(Globals.BuildingTypes buildingType, Building sourceBuilding, Vector2 targetCoordinate, World world, Player owner) { if (sourceBuilding != null && buildingType != Globals.BuildingTypes.Base && (Math.Abs(((int)sourceBuilding.position.X) - (int)targetCoordinate.X) > MAX_BUILDING_RANGE || (Math.Abs(((int)sourceBuilding.position.Y) - (int)targetCoordinate.Y) > MAX_BUILDING_RANGE))) { logger.Debug("Building position out of range"); throw new BuildingOutOfRangeException(); } uint price = owner.unitAcc.CalculateBuildingCostInflation(buildingType); if (sourceBuilding != null && (uint)sourceBuilding.CountUnits() < price) { logger.Debug("Building too expensive"); return false; } logger.Info("Building a building at position "+targetCoordinate+" of "+buildingType+"."); lock (owner.GetGraphs()) { LinkedList<Tile> controlZone = CreateControlZone(targetCoordinate, world); //The Base building is handled in another way due to it's nature. if (buildingType == Globals.BuildingTypes.Base) { logger.Trace("Adding a Base Building and also constructing a new graph"); BaseBuilding baseBuilding = new BaseBuilding("Base Buidling", (int)targetCoordinate.X, (int)targetCoordinate.Y, owner, controlZone); world.map.GetTile((int)targetCoordinate.X, (int)targetCoordinate.Y).SetBuilding(baseBuilding); owner.AddGraph(GraphController.Instance.AddBaseBuilding(baseBuilding, sourceBuilding)); } else { //The other buildings constructs in similiar ways but they are constructed //as the specified type. Building newBuilding = null; switch (buildingType) { case Globals.BuildingTypes.Aggressive: logger.Trace("Building a new Aggressive building"); newBuilding = new AggressiveBuilding("Aggresive Building", (int)targetCoordinate.X, (int)targetCoordinate.Y, owner, GraphController.Instance.GetGraph(sourceBuilding).baseBuilding, controlZone); break; case Globals.BuildingTypes.Barrier: logger.Trace("Building a new Barrier building"); newBuilding = new BarrierBuilding("Barrier Building", (int)targetCoordinate.X, (int)targetCoordinate.Y, owner, GraphController.Instance.GetGraph(sourceBuilding).baseBuilding, controlZone); break; case Globals.BuildingTypes.Resource: logger.Trace("Building a new Resource building"); newBuilding = new ResourceBuilding("Resource Building", (int)targetCoordinate.X, (int)targetCoordinate.Y, owner, GraphController.Instance.GetGraph(sourceBuilding).baseBuilding, controlZone); break; } world.map.GetTile((int)targetCoordinate.X, (int)targetCoordinate.Y).SetBuilding(newBuilding); newBuilding.Parent = sourceBuilding; GraphController.Instance.AddBuilding(sourceBuilding, newBuilding); } if (sourceBuilding != null && world.map.GetTile((int)targetCoordinate.X, (int)targetCoordinate.Y).GetBuilding() != null) { logger.Info("The building has " + sourceBuilding.CountUnits() + " and the building costs " + price); owner.unitAcc.DestroyUnits(sourceBuilding.units, (int)price); logger.Info("The source building only got " + sourceBuilding.CountUnits() + " units left."); } else if (world.map.GetTile((int)targetCoordinate.X, (int)targetCoordinate.Y).GetBuilding() == null) { throw new Exception("A building was not placed on the tile even though it should have been."); } SoundsController.playSound("buildingPlacement"); } // Let's update the fog of war! /* for (int i = -3; i <= 3; i++) { for (int j = -3; j <= 3; j++) { try { world.map.GetTile((int)targetCoordinate.X + j, (int)targetCoordinate.Y + i).MakeVisibleTo(owner); } catch(IndexOutOfRangeException e) { } } } */ return true; }
/// <param name="fromBuilding">The fromBuilding to get weight for.</param> /// <returns>the weight of the fromBuilding.</returns> /// <exception cref="ArgumentException">if the fromBuilding is not a part of the graph.</exception> public int GetWeight(Building building) { int weight; if (! buildings.TryGetValue(building, out weight)) { throw new ArgumentException("That building does not exist in this graph."); } return weight; }
/// <summary> /// Called by fromBuilding, adds units to a fromBuilding. /// </summary> /// <param name="b">The fromBuilding to add units to.</param> /// <param name="units">A list of units.</param> public void AddUnits(Building b, List<Unit> units) { b.AddUnits(units); }
/// <summary> /// A fromBuilding may remove itself with itself as identifier /// </summary> /// <param name="fromBuilding"></param> public bool RemoveBuilding(Building building) { if (building.type == Globals.BuildingTypes.Resource) { ResourceBuilding rb = (ResourceBuilding)building; this.rateOfProduction -= rb.RateOfProduction; } return childBuildings.Remove(building); }
/// <summary> /// Publishes an event of change to all subscribers. /// </summary> /// <param name="fromBuilding">The fromBuilding that has changed.</param> /// <param name="weight">The weight of that fromBuilding.</param> /// <param name="t">Type of event.</param> private void Publish(Building building, int weight, EventType t) { if (weightChanged != null) { weightChanged(this, new GraphEvent(building, weight, t)); } }
/// <summary> /// Sets the weight of a fromBuilding node in the graph. /// </summary> /// <param name="fromBuilding">The fromBuilding to set weight for.</param> /// <param name="weight">The new weight.</param> /// <exception cref="GraphLessBuildingException">If the fromBuilding does not exist in the graph.</exception> public void SetWeight(Building building, int weight) { if (! buildings.ContainsKey(building)) { throw new GraphLessBuildingException(); } lock(buildings) { TotalWeight -= buildings[building]; buildings[building] = weight; TotalWeight += weight; } Publish(building, weight, EventType.ALTER); }
/// <summary> /// Removes a fromBuilding from the graph. /// </summary> /// <param name="fromBuilding">The fromBuilding to remove.</param> public void Remove(Building building) { lock(buildings) { TotalWeight -= buildings[building]; buildings.Remove(building); } Publish(building, 0, EventType.REMOVE); }
/// <summary> /// Checks if a fromBuilding exists in this graph. /// </summary> /// <param name="b">The fromBuilding to check existance for.</param> /// <returns>True if the fromBuilding exists, false if not.</returns> public bool HasBuilding(Building b) { return buildings.ContainsKey(b); }
/// <summary> /// Returns the weight factor for a fromBuilding. /// It' pretty much weight / total weight. /// </summary> /// <param name="fromBuilding"></param> /// <returns></returns> public double GetWeightFactor(Building building) { double weight = GetWeight(building); return weight / (double)TotalWeight; }
/// <summary> /// Allows any fromBuilding except a BaseBuilding to add itself to this basebuildings list of buildings /// </summary> /// <param name="fromBuilding"></param> public void Visit(Building building) { childBuildings.AddLast(building); if (buildingsChanged != null) { buildingsChanged(this, new BuildingAddedEvent(building, EventType.ADD)); } }
/// <summary> /// /// </summary> /// <param name="player"></param> public static void ConstructBuilding(Player player, Tile constructTile, Building sourceBuilding, World theWorld) { logger.Trace("Constructing a building for a player"); //TODO Somehow present a menu to the player, and then //use the information to ADD (not the document) the fromBuilding. MenuIcon baseCell = new MenuIcon(Language.Instance.GetString("BaseCell") + " (" + player.unitAcc.CalculateBuildingCostInflation(Globals.BuildingTypes.Base)+")", Recellection.textureMap.GetTexture(Globals.TextureTypes.BaseBuilding), Color.Black); MenuIcon resourceCell = new MenuIcon(Language.Instance.GetString("ResourceCell") + " (" + player.unitAcc.CalculateBuildingCostInflation(Globals.BuildingTypes.Resource) + ")", Recellection.textureMap.GetTexture(Globals.TextureTypes.ResourceBuilding), Color.Black); MenuIcon defensiveCell = new MenuIcon(Language.Instance.GetString("DefensiveCell") + " (" + player.unitAcc.CalculateBuildingCostInflation(Globals.BuildingTypes.Barrier) + ")", Recellection.textureMap.GetTexture(Globals.TextureTypes.BarrierBuilding), Color.Black); MenuIcon aggressiveCell = new MenuIcon(Language.Instance.GetString("AggressiveCell") + " (" + player.unitAcc.CalculateBuildingCostInflation(Globals.BuildingTypes.Aggressive) + ")", Recellection.textureMap.GetTexture(Globals.TextureTypes.AggressiveBuilding), Color.Black); MenuIcon cancel = new MenuIcon(Language.Instance.GetString("Cancel"), Recellection.textureMap.GetTexture(Globals.TextureTypes.No)); List<MenuIcon> menuIcons = new List<MenuIcon>(); menuIcons.Add(baseCell); menuIcons.Add(resourceCell); menuIcons.Add(defensiveCell); menuIcons.Add(aggressiveCell); menuIcons.Add(cancel); Menu ConstructBuildingMenu = new Menu(Globals.MenuLayout.NineMatrix, menuIcons, Language.Instance.GetString("ChooseBuilding"), Color.Black); MenuController.LoadMenu(ConstructBuildingMenu); Recellection.CurrentState = MenuView.Instance; Globals.BuildingTypes building; MenuIcon choosenMenu = MenuController.GetInput(); Recellection.CurrentState = WorldView.Instance; MenuController.UnloadMenu(); if (choosenMenu.Equals(baseCell)) { building = Globals.BuildingTypes.Base; } else if (choosenMenu.Equals(resourceCell)) { building = Globals.BuildingTypes.Resource; } else if (choosenMenu.Equals(defensiveCell)) { building = Globals.BuildingTypes.Barrier; } else if (choosenMenu.Equals(aggressiveCell)) { building = Globals.BuildingTypes.Aggressive; } else { return; } // If we have selected a tile, and we can place a building at the selected tile... try { if (!AddBuilding(building, sourceBuilding, constructTile.position, theWorld, player)) { //SoundsController.playSound("Denied"); } } catch (BuildingOutOfRangeException bore) { throw bore; } }
public bool PayAndUpgradeSpeed(Building building) { if (building.units.Count < GetUpgradeCost() || (owner.PowerLevel+owner.SpeedLevel) >= 0.6f) { return false; } DestroyUnits(building.units, GetUpgradeCost()); owner.SpeedLevel += 0.1f; return true; }
public static void HurtBuilding(Building toHurt) { toHurt.Damage(1); if (!toHurt.IsAlive()) { RemoveBuilding(toHurt); } }
/// <summary> /// Called when a new fromBuilding should be created. Creates a fromBuilding of a given type at the /// given point from the given sourceBuilding. Returns false if it failed. /// </summary> /// <param name="point"></param> /// <param name="baseBuilding"></param> /// <param name="buildingType"></param> /// <returns></returns> private bool IssueBuildOrder(Vector2 point, Building sourceBuilding, Globals.BuildingTypes buildingType) { bool created = BuildingController.AddBuilding(buildingType, sourceBuilding, point, m_view.world, this); if (created) m_view.BuildingAddedAt(point); return created; }
/// <summary> /// Removes a fromBuilding from the graph containing it. /// </summary> /// <param name="b">The buiding to remove.</param> public static void RemoveBuilding(Building b) { lock (b) { if (b is ResourceBuilding && GraphController.Instance.GetGraph(b).baseBuilding != null) { GraphController.Instance.GetGraph(b).baseBuilding.RateOfProduction -= ((ResourceBuilding)b).RateOfProduction; } lock (b.controlZone) { b.controlZone.First().RemoveBuilding(); GraphController.Instance.RemoveBuilding(b); // How about I exchange this: //b.Damage(Math.Max(0, b.currentHealth+1)); // Kill it! // // With this: b.Kill(); SoundsController.playSound("buildingDeath",b.position); } } }