/// <summary> /// Creates a unit. /// </summary> /// <param name="playerID">The player ID.</param> /// <param name="serverID">Server ID.</param> /// <param name="type">The type of the unit.</param> public void CreateBuilding(int playerID, int serverID, int type, int byID) { Building building = null; Player p = Game1.GetInstance().GetPlayerByMultiplayerID(playerID); Engineer engineer = (Engineer)((UnitMultiplayerData)MultiplayerDataManager.GetInstance().GetDataByServerID(byID)).unit; switch (type) { case BuildingHeaders.TYPE_BARRACKS: { building = new Barracks(p, p.color); break; } case BuildingHeaders.TYPE_FACTORY: { building = new Factory(p, p.color); break; } case BuildingHeaders.TYPE_FORTRESS: { building = new Fortress(p, p.color); break; } case BuildingHeaders.TYPE_RESOURCES_GATHER: { building = new ResourceGather(p, p.color); break; } } building.constructedBy = engineer; building.multiplayerData.serverID = serverID; }
/// <summary> /// Used for creating units and buildings respectively. /// </summary> /// <param name="me">The MouseEvent to use</param> void MouseClickListener.OnMouseClick(MouseEvent me) { if (me.button == MouseEvent.MOUSE_BUTTON_1) //player.currentSelection != null //player.currentSelection.units.Count == 1 && //player.currentSelection.units.ElementAt(0).type == Unit.UnitType.Engineer &&) { foreach (HUDObject o in objects) { if (o.DefineRectangle().Contains(Mouse.GetState().X, Mouse.GetState().Y)) { player.RemovePreviewBuildings(); Building b; Unit u; switch (o.type) { case HUDObject.Type.Resources: b = new ResourceGather(this.player, this.color); // Was false Game1.GetInstance().IsMouseVisible = true; break; case HUDObject.Type.Barracks: b = new Barracks(this.player, this.color); // Was false Game1.GetInstance().IsMouseVisible = true; break; case HUDObject.Type.Factory: b = new Factory(this.player, this.color); // Was false Game1.GetInstance().IsMouseVisible = true; break; case HUDObject.Type.Fortress: b = new Fortress(this.player, this.color); // Was false Game1.GetInstance().IsMouseVisible = true; break; case HUDObject.Type.Engineer: foreach (Fortress building in player.buildingSelection.buildings) { building.CreateUnit(Unit.Type.Engineer); } break; case HUDObject.Type.Ranged: foreach (Barracks building in player.buildingSelection.buildings) { building.CreateUnit(Unit.Type.Ranged); } break; case HUDObject.Type.Melee: foreach (Barracks building in player.buildingSelection.buildings) { building.CreateUnit(Unit.Type.Melee); } break; case HUDObject.Type.Fast: foreach (Barracks building in player.buildingSelection.buildings) { building.CreateUnit(Unit.Type.Fast); } break; default: break; } } } foreach (HUDCommandObject co in commandObjects) { if (co.DefineRectangle().Contains(Mouse.GetState().X, Mouse.GetState().Y)) { switch (co.type) { case HUDCommandObject.Type.Repair: if (!co.disabled) { player.command = new Command(Game1.GetInstance().Content.Load<Texture2D>("HUD/Commands/HUDRepair"), this.player, Command.Type.Repair, Mouse.GetState().X, Mouse.GetState().Y, new Color(255, 187, 0, 255)); } break; default: break; } } } foreach (Building b in player.buildings) { if (b.state == Building.State.Preview && !this.DefineRectangle().Contains(new Rectangle(me.location.X, me.location.Y, 1, 1)) && Game1.GetInstance().collision.CanPlace(b.DefineRectangle())) { Engineer temp = null; foreach (Unit u in player.currentSelection.units) { if (u.type == Unit.Type.Engineer) { LinkedList<Point> path = u.CalculatePath(new Point(me.location.X, me.location.Y)); // Get the last point of the pathfinding result Point lastPoint = path.ElementAt(path.Count - 1); // Remove that point path.RemoveLast(); // Add a point that is on the circle near the building, not inside the building! Point targetPoint = new Point(0, 0); if (path.Count == 0) targetPoint = new Point((int)u.x, (int)u.y); else targetPoint = path.ElementAt(path.Count - 1); // Move to the point around the circle of the building, but increase the radius a bit // so we're not standing on the exact top of the building path.AddLast( Util.GetPointOnCircle(lastPoint, b.GetCircleRadius() + u.texture.Width / 2, Util.GetHypoteneuseAngleDegrees(lastPoint, targetPoint))); // Now that we know the point where we should move to // Again calculate the path .. but now the proper way, // we only needed to calculate the point. u.MoveToQueue(path.Last.Value); // Set the Engineer to link with the Building so construction won't start without an Engineer // Since only one Engineer is needed, break aftwards if (temp == null) { temp = (Engineer)u; break; } } } b.PlaceBuilding(temp); } } } }
/// <summary> /// Used for creating units and buildings respectively. /// </summary> /// <param name="me">The MouseEvent to use</param> void MouseClickListener.OnMouseClick(MouseEvent me) { MouseState state = Mouse.GetState(); if (me.button == MouseEvent.MOUSE_BUTTON_1) { for (int i = 0; i < this.objects.Count(); i++) { HUDObject o = this.objects.ElementAt(i); if (o.DefineRectangle().Contains(state.X, state.Y)) { player.RemovePreviewBuildings(); Building b; switch (o.type) { case HUDObject.Type.Resources: if (player.resources >= Building.GetCost(Building.Type.Resources)) { b = new ResourceGather(this.player, this.color); // Was false Game1.GetInstance().IsMouseVisible = true; } break; case HUDObject.Type.Barracks: if (player.resources >= Building.GetCost(Building.Type.Barracks)) { b = new Barracks(this.player, this.color); // Was false Game1.GetInstance().IsMouseVisible = true; } break; case HUDObject.Type.Factory: if (player.resources >= Building.GetCost(Building.Type.Factory)) { b = new Factory(this.player, this.color); // Was false Game1.GetInstance().IsMouseVisible = true; } break; case HUDObject.Type.Sentry: if (player.resources >= Building.GetCost(Building.Type.Sentry)) { b = new Sentry(this.player, this.color); // Was false Game1.GetInstance().IsMouseVisible = true; } break; case HUDObject.Type.Fortress: if (player.resources >= Building.GetCost(Building.Type.Fortress)) { b = new Fortress(this.player, this.color); // Was false Game1.GetInstance().IsMouseVisible = true; } break; case HUDObject.Type.Engineer: for (int k = 0; k < player.buildingSelection.buildings.Count(); k++) { Building bu = player.buildingSelection.buildings.ElementAt(k); if (bu is Fortress) { Fortress building = (Fortress)bu; if (Keyboard.GetState().IsKeyDown(Keys.LeftShift) || Keyboard.GetState().IsKeyDown(Keys.RightShift)) { for (int j = 0; j < 5; j++) { if (player.resources >= Unit.GetCost(Unit.Type.Engineer)) { building.CreateUnit(Unit.Type.Engineer); } } } else { if (player.resources >= Unit.GetCost(Unit.Type.Engineer)) { building.CreateUnit(Unit.Type.Engineer); } } } } break; case HUDObject.Type.Ranged: for (int k = 0; k < player.buildingSelection.buildings.Count(); k++) { Building bu = player.buildingSelection.buildings.ElementAt(k); if (bu is Barracks) { Barracks building = (Barracks)bu; if (Keyboard.GetState().IsKeyDown(Keys.LeftShift) || Keyboard.GetState().IsKeyDown(Keys.RightShift)) { for (int j = 0; j < 5; j++) { if (player.resources >= Unit.GetCost(Unit.Type.Ranged)) { building.CreateUnit(Unit.Type.Ranged); } } } else { if (player.resources >= Unit.GetCost(Unit.Type.Ranged)) { building.CreateUnit(Unit.Type.Ranged); } } } } break; case HUDObject.Type.Melee: for (int k = 0; k < player.buildingSelection.buildings.Count(); k++) { Building bu = player.buildingSelection.buildings.ElementAt(k); if (bu is Barracks) { Barracks building = (Barracks)bu; if (Keyboard.GetState().IsKeyDown(Keys.LeftShift) || Keyboard.GetState().IsKeyDown(Keys.RightShift)) { for (int j = 0; j < 5; j++) { if (player.resources >= Unit.GetCost(Unit.Type.Melee)) { building.CreateUnit(Unit.Type.Melee); } } } else { if (player.resources >= Unit.GetCost(Unit.Type.Melee)) { building.CreateUnit(Unit.Type.Melee); } } } } break; case HUDObject.Type.Fast: for (int k = 0; k < player.buildingSelection.buildings.Count(); k++) { Building bu = player.buildingSelection.buildings.ElementAt(k); if (bu is Barracks) { Barracks building = (Barracks)bu; if (Keyboard.GetState().IsKeyDown(Keys.LeftShift) || Keyboard.GetState().IsKeyDown(Keys.RightShift)) { for (int j = 0; j < 5; j++) { if (player.resources >= Unit.GetCost(Unit.Type.Fast)) { building.CreateUnit(Unit.Type.Fast); } } } else { if (player.resources >= Unit.GetCost(Unit.Type.Fast)) { building.CreateUnit(Unit.Type.Fast); } } } } break; case HUDObject.Type.Heavy: for (int k = 0; k < player.buildingSelection.buildings.Count(); k++) { Building bu = player.buildingSelection.buildings.ElementAt(k); if (bu is Factory) { Factory building = (Factory)bu; if (Keyboard.GetState().IsKeyDown(Keys.LeftShift) || Keyboard.GetState().IsKeyDown(Keys.RightShift)) { for (int j = 0; j < 5; j++) { if (player.resources >= Unit.GetCost(Unit.Type.HeavyMelee)) { building.CreateUnit(Unit.Type.HeavyMelee); } } } else { if (player.resources >= Unit.GetCost(Unit.Type.HeavyMelee)) { building.CreateUnit(Unit.Type.HeavyMelee); } } } } break; default: break; } } } for (int i = 0; i < this.commandObjects.Count(); i++) { HUDCommandObject co = this.commandObjects.ElementAt(i); if (co.DefineRectangle().Contains(state.X, state.Y)) { switch (co.type) { case HUDCommandObject.Type.Repair: if (!co.disabled) { player.command = new Command(TextureManager.GetInstance().GetTexture(HUDCommandObject.Type.Repair), this.player, Command.Type.Repair, state.X, state.Y, new Color(255, 187, 0, 255)); } break; case HUDCommandObject.Type.Attack: if (!co.disabled) { player.command = new Command(TextureManager.GetInstance().GetTexture(HUDCommandObject.Type.Attack), this.player, Command.Type.Attack, state.X, state.Y, new Color(255, 0, 12, 255)); } break; case HUDCommandObject.Type.Defend: if (!co.disabled) { player.command = new Command(TextureManager.GetInstance().GetTexture(HUDCommandObject.Type.Defend), this.player, Command.Type.Defend, state.X, state.Y, new Color(255, 125, 0, 255)); } break; case HUDCommandObject.Type.Move: if (!co.disabled) { player.command = new Command(TextureManager.GetInstance().GetTexture(HUDCommandObject.Type.Move), this.player, Command.Type.Move, state.X, state.Y, new Color(0, 100, 255, 255)); } break; case HUDCommandObject.Type.Stop: if (!co.disabled) { if (player.currentSelection != null) { for (int j = 0; j < player.currentSelection.units.Count(); j++) { Unit u = player.currentSelection.units.ElementAt(j); u.unitToDefend = null; u.unitToStalk = null; u.waypoints.Clear(); u.SetJob(Unit.Job.Idle); u.hasToMove = false; u.buildingToDestroy = null; } } } break; default: break; } } } for (int i = 0; i < player.buildings.Count(); i++) { Building b = player.buildings.ElementAt(i); if (b.state == Building.State.Preview && !this.DefineRectangle().Contains(new Rectangle(me.location.X, me.location.Y, 1, 1)) && Game1.GetInstance().map.collisionMap.CanPlace(b.DefineRectangle())) { Engineer temp = null; for (int j = 0; j < player.currentSelection.units.Count(); j++) { Unit u = player.currentSelection.units.ElementAt(j); if (u.type == Unit.Type.Engineer) { Point p = new Point((int)(b.x + (b.texture.Width / 2)), (int)(b.y + (b.texture.Height / 2))); // Add a point that is on the circle near the building, not inside the building! Point targetPoint = new Point(0, 0); if (u.waypoints.Count() == 0) targetPoint = new Point((int)u.x, (int)u.y); else targetPoint = u.waypoints.ElementAt(u.waypoints.Count() - 1); // Move to the point around the circle of the building, but increase the radius a bit // so we're not standing on the exact top of the building u.MoveToQueue( Util.GetPointOnCircle(p, b.GetCircleRadius() + u.texture.Width / 2, Util.GetHypoteneuseAngleDegrees(p, targetPoint))); // Set the Engineer to link with the Building so construction won't start without an Engineer // Since only one Engineer is needed, break aftwards if (temp == null) { temp = (Engineer)u; break; } } } b.PlaceBuilding(temp); } } } }
void MouseClickListener.OnMouseClick(MouseEvent me) { if (me.button == MouseEvent.MOUSE_BUTTON_1 && player.currentSelection != null && //player.currentSelection.units.Count == 1 && //player.currentSelection.units.ElementAt(0).type == Unit.UnitType.Engineer && loadForEngineer) { if (drawResourcesText || drawBarracksText || drawFactoryText || drawFortressText) { Console.Out.WriteLine("Previewing a building!"); player.RemovePreviewBuildings(); Building b; if (drawResourcesText) { b = new ResourceGather(this.player, this.color); } else if (drawBarracksText) { b = new Barracks(this.player, this.color); } else if (drawFactoryText) { b = new Factory(this.player, this.color); } else if (drawFortressText) { b = new Fortress(this.player, this.color); } Console.Out.WriteLine("Creating a building"); // player.buildings.AddLast(b); } foreach (Building b in player.buildings) { if (b.state == Building.BuildState.Preview && !this.DefineRectangle().Contains(new Rectangle(me.location.X, me.location.Y, 1, 1)) && Game1.GetInstance().collision.CanPlace(b.DefineRectangle())) { Engineer temp = null; foreach (Unit u in player.currentSelection.units) { if (u.type == Unit.UnitType.Engineer) { u.MoveToNow(new Point(me.location.X, me.location.Y)); // Get the last point of the pathfinding result Point lastPoint = u.waypoints.ElementAt(u.waypoints.Count - 1); // Remove that point u.waypoints.RemoveLast(); // Add a point that is on the circle near the building, not inside the building! Point targetPoint = new Point(0, 0); if (u.waypoints.Count == 0) targetPoint = new Point((int)u.x, (int)u.y); else targetPoint = u.waypoints.ElementAt(u.waypoints.Count - 1); // Move to the point around the circle of the building, but increase the radius a bit // so we're not standing on the exact top of the building u.waypoints.AddLast( Util.GetPointOnCircle(lastPoint, b.GetCircleRadius() + u.texture.Width / 2, Util.GetHypoteneuseAngleDegrees(lastPoint, targetPoint))); // Set the Engineer to link with the Building // so construction won't start without an Engineer // Since only one Engineer is needed, break aftwards if (temp == null) { temp = (Engineer) u; break; } } } b.PlaceBuilding(temp); } } } }