/// <summary> /// Creates a building. /// </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) { if (MultiplayerDataManager.GetInstance().GetDataByServerID(serverID, false) != null) return; Building building = null; Player p = Game1.GetInstance().GetPlayerByMultiplayerID(playerID); Engineer engineer = (Engineer)((UnitMultiplayerData)MultiplayerDataManager.GetInstance().GetDataByServerID(byID, true)).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; } case BuildingHeaders.TYPE_SENTRY: { building = new Sentry(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) { 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); } } } }