/// <summary> /// Constructs a new base fromBuilding /// </summary> /// <param name="name"></param> /// <param name="posX"></param> /// <param name="posY"></param> /// <param name="owner"></param> public BaseBuilding(String name, int posX, int posY,Player owner,LinkedList<Tile> controlZone) : base(name, posX, posY, BASE_BUILDING_HEALTH,owner,Globals.BuildingTypes.Base , null,controlZone) { this.type = Globals.BuildingTypes.Base; childBuildings = new LinkedList<Building>(); baseBuilding = this; this.rateOfProduction = BASE_PRODUCTION; }
public void init() { this.p = new Player(); this.bb = new BaseBuilding("test", 0, 0, new Player(), new LinkedList<Tile>()); this.g = new Graph(bb); this.ua = new UnitAccountant(p); this.p.AddGraph(g); }
/// <summary> /// Constructs a new ResourceBuilding /// </summary> /// <param name="name"></param> /// <param name="posX"></param> /// <param name="posY"></param> /// <param name="maxHealth"></param> /// <param name="owner"></param> /// <param name="baseBuilding"></param> public ResourceBuilding(String name, int posX, int posY, Player owner, BaseBuilding baseBuilding, LinkedList<Tile> controlZone) : base(name, posX, posY, RESOURCE_BUILDING_HEALTH, owner, Globals.BuildingTypes.Resource, baseBuilding,controlZone) { this.rateOfProduction = controlZone.First().GetTerrainType().getResourceModifier() + DEFAULT_PRODUCTION; if (baseBuilding != null) { baseBuilding.RateOfProduction += this.rateOfProduction; } }
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>()); }
// Create public WorldController(Player p, World theWorld) { //Debugging finished = false; myLogger = LoggerFactory.GetLogger(); myLogger.SetThreshold(LogLevel.TRACE); this.playerInControll = p; this.theWorld = theWorld; createGUIRegionGridAndScrollZone(); }
/// <summary> /// Constructs a new BarrierBuilding /// </summary> /// <param name="name"></param> /// <param name="posX"></param> /// <param name="posY"></param> /// <param name="owner"></param> /// <param name="baseBuilding"></param> public BarrierBuilding(String name, int posX, int posY, Player owner, BaseBuilding baseBuilding, LinkedList<Tile> controlZone) : base(name, posX, posY, BARRIER_BUILDING_HEALTH, owner, Globals.BuildingTypes.Barrier, baseBuilding, controlZone) { foreach(Tile t in controlZone) { t.unitsChanged += BarrierBuilding_unitsChanged; foreach(Unit u in t.GetUnits(owner)) { u.Buff = powerBonus; } } }
/// <summary> /// Constructs a new AgressiveBuilding /// </summary> /// <param name="name"></param> /// <param name="posX"></param> /// <param name="posY"></param> /// <param name="maxHealth"></param> /// <param name="owner"></param> /// <param name="baseBuilding"></param> public AggressiveBuilding(String name, int posX, int posY, Player owner, BaseBuilding baseBuilding, LinkedList<Tile> controlZone) : base(name, posX, posY, AGGRESSIVE_BUILDING_HEALTH, owner, Globals.BuildingTypes.Aggressive, baseBuilding,controlZone) { currentTargets = new List<Unit>(); /*for(int i = 0; i < controlZone.Count; i++) { controlZone.ElementAt(i).unitsChanged += AggressiveBuilding_unitsChanged; }*/ foreach (Tile t in controlZone) { t.unitsChanged += AggressiveBuilding_unitsChanged; } }
private bool CreateGameObjects(int seed) { myLogger.Info("Generating world."); theWorld = WorldGenerator.GenerateWorld(seed); myLogger.Info("Done."); // Let all units belong to the world! Unit.SetWorld(theWorld); new SoundsController(theWorld); Random randomer = new Random(seed); myLogger.Info("Adding players."); Player human = new Player(Color.Blue, "John"); theWorld.AddPlayer(human); List<Player> temp2 = new List<Player>(); temp2.Add(human); AIPlayer ai = new AIPlayer(new AIView(theWorld),Color.Red); theWorld.AddPlayer(ai); human.Enemy = ai; ai.Enemy = human; myLogger.Info("Creating spawnpoints."); SpawnPoints(theWorld.players, theWorld.map.width, theWorld.map.height, randomer); myLogger.Info("Spawning units."); foreach(Player p in theWorld.players) { // We want 50 units! for(int i = 0; i < 10; i++) { p.unitAcc.ProduceUnits(); } } int xOffset = (Recellection.viewPort.Width/Globals.TILE_SIZE)/2; int yOffset = (Recellection.viewPort.Height/Globals.TILE_SIZE)/2; theWorld.LookingAt = new Point( (int)(theWorld.players[0].GetGraphs()[0].baseBuilding.position.X-xOffset), (int)(theWorld.players[0].GetGraphs()[0].baseBuilding.position.Y-yOffset)); myLogger.Info("Setting lookingAt to X: " + theWorld.LookingAt.X + " y: " + theWorld.LookingAt.Y); return true; }
/// <summary> /// Creates a unit. /// </summary> /// <param name="position">Position of unit.</param> /// <param name="owner">Owner of this unit.</param> public Unit(Player owner, Vector2 position, Entity baseEntity) : base(position, owner) { this.BaseEntity = baseEntity; this.returnToBase = (baseEntity != null); this.DisperseDistance = 1.5f; this.position = position; this.angle = 0; this.isDead = false; this.owner = owner; this.rand = new Random(id++); this.IsAggressive = true; world.GetMap().GetTile((int)position.X, (int)position.Y).AddUnit(this); world.AddUnit(this); }
/// <summary> /// Creates a fromBuilding with specified parameters, the unit list will /// be initiated but empty and the current health will be set at maxHealth. /// Regarding the controlZone the first tile should be the /// tile the fromBuilding is standing on. /// </summary> /// <param name="name">The name for the fromBuilding TODO Decide if this is /// needded</param> /// <param name="posX">The x tile coordinate</param> /// <param name="posY">The y tile coordinate</param> /// <param name="maxHealth">The max health of this fromBuilding</param> /// <param name="owner">The player that owns the fromBuilding</param> /// <param name="type">The </param> /// <param name="baseBuilding">The Base Building this fromBuilding belongs /// <param name="controlZone">The nine tiles around the fromBuilding /// and the tile the fromBuilding is on.</param> /// to</param> public Building(String name, int posX, int posY, int maxHealth, Player owner, Globals.BuildingTypes type, BaseBuilding baseBuilding, LinkedList<Tile> controlZone) : base(new Vector2(((float)posX) + 0.5f, ((float)posY) + 0.5f), owner) { if (maxHealth <= 0) { throw new ArgumentOutOfRangeException("maxHealth", "The max of health may not be zero or less"); } logger.Trace("Constructing new Building with choosen values"); this.name = name; this.maxHealth = maxHealth; this.currentHealth = maxHealth; this.units = new List<Unit>(); this.incomingUnits = new List<Unit>(); this.type = type; this.IsAggressive = true; this.baseBuilding = baseBuilding; if (baseBuilding != null) { Accept(baseBuilding); } this.controlZone = controlZone; foreach (Tile t in controlZone) { t.unitsChanged += UpdateAggressiveness; } }
/// <summary> /// Creates a unit. /// </summary> /// <param name="posX">Unit x-coordinate.</param> /// <param name="posY">Unit y-coordinate.</param> public Unit(Player owner, float posX, float posY) : this(owner, new Vector2(posX, posY)) { }
private Entity targetEntity = null; // Target entity #endregion Fields #region Constructors /// <summary> /// Creates a "default unit". /// </summary> public Unit(Player owner) : this(owner, new Vector2(0, 0)) { }
private void updateFogOfWar(Player player) { lock (world) { foreach (Tile t in world.GetMap().map) { if (t.GetBuilding() == null) continue; for (int x = -2; x <= 2; x++) { for (int y = -2; y <= 2; y++) { if (! world.isWithinMap(x + (int)t.position.X, y + (int)t.position.Y)) continue; //Get the tile and check if it is visible already else set it to visible. if (world.GetMap().GetTile(x + (int)t.position.X, y + (int)t.position.Y).IsVisible(t.GetBuilding().owner)) continue; // TODO: Do stuff. } } } } }
/// <summary> /// Creates a new BaseBuilding at the specified location and /// add that fromBuilding to a new Graph and then add that graph /// to the player. /// </summary> /// <param name="xCoord">The x-coordinate to spawn the BaseBuilding on /// </param> /// <param name="yCoord">The Y-coordinate to spawn the BaseBuilding on /// </param> /// <param name="owner">The player which this method will create a new /// graph.</param> private void SpawnGraph(int xCoord, int yCoord, Player owner) { /*BaseBuilding baseBuilding = new BaseBuilding("base", xCoord, yCoord, owner); theWorld.map.GetTile(xCoord, yCoord).SetBuilding(baseBuilding);*/ myLogger.Info("Creating graph for player: "+ owner +" at: "+xCoord+","+yCoord); //owner.AddGraph(new Graph(baseBuilding)); BuildingController.AddBuilding(Globals.BuildingTypes.Base, null, new Vector2(xCoord, yCoord), theWorld,owner); }
/// <summary> /// Counts how many graphs a player has, if it is zero /// the player has lost. /// /// Condition: when a graph is empty it is /// deleted or a empty graph is not counted. /// </summary> /// <param name="player">The player which might have lost</param> /// <returns>True if the player has no graphs false other vice</returns> private Boolean HasLost(Player player) { if (player.GetGraphs().Count == 0) { return true; } else { return false; } }
/// <summary> /// Add a player to the game world. Invokes the PlayerEvent event. /// </summary> /// <param name="p">The player to be added to the world</param> public void AddPlayer(Player p) { players.Add(p); if (PlayerEvent != null) { PlayerEvent(this, new Event<Player>(p, EventType.ADD)); } }
/// <summary> /// Add a unit to this Tile. /// </summary> /// <param name="units">Units to be added to this Tile.</param> public void AddUnit(Player p, Unit u) { lock (units) { lock (allUnits) { if (!this.units.ContainsKey(p)) { this.units.Add(p, new HashSet<Unit>()); } this.units[p].Add(u); this.allUnits.Add(u); if (unitsChanged != null) { //I'm sorry for this ugly hax - John List<Unit> temp = new List<Unit>(); temp.Add(u); unitsChanged(this, new Event<IEnumerable<Unit>>(temp, EventType.ADD)); } } } }
/// <summary> /// Make a player able to see this tile. /// </summary> /// <param name="p">Player to add.</param> public void MakeVisibleTo(Player p) { this.visibleTo.Add(p); if (visionChanged != null) { visionChanged(this, new Event<IEnumerable<Player>>(visibleTo,EventType.ADD)); } }
/// <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; }
/// <summary> /// Make a player unable to see this tile. /// </summary> /// <param name="p">Player to remove.</param> public void MakeInvisibleTo(Player p) { this.visibleTo.Remove(p); if (visionChanged != null) { visionChanged(this, new Event<IEnumerable<Player>>(visibleTo,EventType.REMOVE)); } }
/// <summary> /// Checks if this tile is visible to player 'p'. /// </summary> /// <param name="p">Player to check against.</param> public bool IsVisible(Player p) { if (visibleTo.Contains(p)) { return true; } else { return false; } }
/// <summary> /// Get the list of units on this tile. /// </summary> /// <param name="p">Owner of the units to be returned.</param> /// <returns>HashSet of units in this tile.</returns> public HashSet<Unit> GetUnits(Player p) { lock (units) { if (this.units.ContainsKey(p)) { return this.units[p]; } else { return new HashSet<Unit>(); } } }
/// <summary> /// Creates a unit. /// </summary> /// <param name="position">Position of unit.</param> /// <param name="owner">Owner of this unit.</param> public Unit(Player owner, Vector2 position) : this(owner, position, null) { }
/// <summary> /// Constructs an UnitAccountant. /// </summary> /// <param name="owner">This UnitAccountant will belong to the player 'owner'.</param> public UnitAccountant(Player owner) { this.owner = owner; }
/// <summary> /// /// </summary> /// <param name="p"></param> /// <param name="units"></param> public void RemoveUnit(Player p, List<Unit> units) { lock (units) { lock (allUnits) { foreach (Unit u in units) { this.units[p].Remove(u); this.allUnits.Remove(u); } if (unitsChanged != null) { unitsChanged(this, new Event<IEnumerable<Unit>>(units, EventType.REMOVE)); } } } }
/// <summary> /// Adds a list of units to this Tile. /// </summary> /// <param name="p">Owner of the units</param> /// <param name="units">Units to be added to this Tile.</param> public void AddUnit(Player p, List<Unit> units) { lock (units) { lock (allUnits) { if (!this.units.ContainsKey(p)) { this.units.Add(p, new HashSet<Unit>()); } foreach (Unit u in units) { this.units[p].Add(u); this.allUnits.Add(u); } if (unitsChanged != null) { unitsChanged(this, new Event<IEnumerable<Unit>>(units, EventType.ADD)); } } } }
/// <summary> /// Remove a player from the game world. Invokes the PlayerEvent event. /// </summary> /// <param name="p">The player to be removed</param> public void RemovePlayer(Player p) { players.Remove(p); if (PlayerEvent != null) { PlayerEvent(this, new Event<Player>(p, EventType.REMOVE)); } }
/// <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 void RemoveUnit(Player p, Unit u) { lock (units) { lock (allUnits) { this.units[p].Remove(u); this.allUnits.Remove(u); if (unitsChanged != null) { //I'm sorry for this ugly hax - John List<Unit> temp = new List<Unit>(); temp.Add(u); unitsChanged(this, new Event<IEnumerable<Unit>>(temp, EventType.REMOVE)); } } } }
private void EndGame(Player winner) { if (backgroundSound.IsPlaying) { backgroundSound.Pause(); } humanControl.Stop(); // Build menu List<MenuIcon> options = new List<MenuIcon>(1); MenuIcon cancel = new MenuIcon(""); cancel.region = new GUIRegion(Recellection.windowHandle, new System.Windows.Rect(0, Globals.VIEWPORT_HEIGHT - 100, Globals.VIEWPORT_WIDTH, 100)); options.Add(cancel); Menu menu = new Menu(options); MenuController.LoadMenu(menu); Recellection.CurrentState = new EndGameView(! (winner is AIPlayer)); MenuController.GetInput(); MenuController.UnloadMenu(); }