// ignores targeted commands, which is currently only rally point commands public int CountScheduledStructureCommands(Structure structure) { int count = 0; foreach (ScheduledAction action in ScheduledActions) { ScheduledStructureCommand structureCommand = action as ScheduledStructureCommand; if (structureCommand != null && !(structureCommand is ScheduledStructureTargetedCommand) && structureCommand.Structure == structure) count++; } return count; }
public void CheckForResetCommandCardWhenStructureCompletes(Structure s) { if (SelectedUnits.Count == 1 && SelectedUnits.Contains(s)) resetCommandCard(); foreach (StructureCogWheel cog in CogWheels) { if (cog.Structure == s) { CogWheels.Remove(cog); break; } } }
public ReturnCargoCommand(Unit unit, Structure targetStructure, Resource source) : base(unit, targetStructure.CenterPoint) { TargetStructure = targetStructure; Source = source; }
// uses starting point (for rally points) /*public PathNode FindNearestPathNode(int startY, int startX, Vector2 startingPoint) { startX = (int)MathHelper.Clamp(startX, 0, Map.Width - 1); startY = (int)MathHelper.Clamp(startY, 0, Map.Height - 1); if (IsNodeWalkable(startY, startX, unit)) return PathNodes[startY, startX]; List<PathNode> walkableNodes = new List<PathNode>(); for (int i = 1; ; i++) { for (int x = startX - i; x <= startX + i; x += i*2) { if (x < 0 || x > Map.Width - 1) continue; for (int y = startY - i; y <= startY + i; y += i*2) { if (y < 0 || y > Map.Height - 1) continue; if (IsNodeWalkable(y, x, unit)) //return PathNodes[y, x]; walkableNodes.Add(PathNodes[y, x]); } } if (walkableNodes.Count > 0) break; } // return discovered path node that is closest to unit float shortestDistanceToUnit = float.MaxValue; int shortestIndex = 0; for (int i = 0; i < walkableNodes.Count; i++) { float distance = Vector2.Distance(walkableNodes[i].Tile.CenterPoint, unit.CenterPoint); if (distance < shortestDistanceToUnit) { shortestDistanceToUnit = distance; shortestIndex = i; } }*/ public bool IsStructureInLineOfSight(Unit unit, Structure structure) { float lerpAmountIncrement = 1f / ((int)(Vector2.Distance(unit.CenterPoint, structure.CenterPoint) / unit.Diameter) + 1); for (float l = lerpAmountIncrement; l < 1f; l += lerpAmountIncrement) //for (float l = 1f - lerpAmountIncrement; l > 0; l -= lerpAmountIncrement) { Vector2 intermediatePoint = Vector2.Lerp(unit.CenterPoint, structure.CenterPoint, l); //if (!IsPointWalkable(intermediatePoint, unit)) // return false; int y = (int)(intermediatePoint.Y / Map.TileSize); int x = (int)(intermediatePoint.X / Map.TileSize); //return IsTileWalkable((int)(point.Y / Map.TileSize), (int)(point.X / Map.TileSize), unit); if (y < 0 || y >= Map.Height || x < 0 || x >= Map.Width)// || !IsNodeWalkable(PathNodes[y, x], unit)) return false; if (!PathNodes[y, x].Tile.Walkable || (PathNodes[y, x].Blocked && PathNodes[y, x].Blocker != structure)) return false; } return true; }
public ScheduledStructureCommand(float scheduledTime, Structure structure, CommandButtonType commandType, short id) : base(scheduledTime) { Structure = structure; CommandType = commandType; ID = id; }
public ScheduledStructureTargetedCommand(float scheduledTime, Structure structure, CommandButtonType commandType, BaseObject target, Vector2 point, bool queued) : base(scheduledTime, structure, commandType) { Target = target; Point = point; Queued = queued; }
bool buildStructure(BuildStructureCommand command) { bool allowBuild = Rts.pathFinder.Tools.CanStructureBePlaced(command.StructureLocation, command.StructureType.Size, this, command.StructureType.CutCorners); if (allowBuild) { //if (command.StructureType == StructureType.Barracks) // new Barracks(command.StructureLocation, this, Team); Structure structure; if (command.StructureType == StructureType.TownHall) structure = new TownHall(command.StructureLocation, this, Team); else structure = new Structure(command.StructureType, command.StructureLocation, this, Team); CenterPoint = structure.CenterPoint; //IgnoringCollision = true; Targetable = false; Busy = true; Building = true; Unit.RemoveUnit(this); } return allowBuild; }
public StructureCogWheel(Structure structure, int size) { Structure = structure; //Rectangle = new Rectangle((int)(structure.CenterPointX - size / 2), (int)(structure.CenterPointY - size / 2), size, size); Rectangle = new Rectangle((int)(structure.CenterPointX), (int)(structure.CenterPointY), size, size); }
static void AddStructure(Structure s) { structures.Add(s); lock (RtsObject.RtsObjects) { RtsObject.AddObject(s); } }
public static void RemoveStructure(Structure s) { structures.Remove(s); RtsObject.RemoveObject(s); }
void checkForLeftClick(GameTime gameTime) { // check if clicked on unit portrait if (SelectedUnits.Count == 1) { if (mouseState.LeftButton == ButtonState.Pressed && unitPictureRectangle.Contains(mouseState.X, mouseState.Y)) { centerCameraOnSelectedUnits(); return; } } // check if clicked on a unit box foreach (UnitButton box in unitButtons) { if (box.Triggered) { // holding shift if (usingShift) { SelectedUnits.Remove(box.Unit); } // not holding shift else { SelectedUnits.Clear(); SelectedUnits.Add(box.Unit); } selectedUnitsChanged = true; return; } } //check if clicked on structure queue item if (QueuedItems != null) { if (mouseState.LeftButton == ButtonState.Released) { allowRemoveFromQueue = true; } else if (allowRemoveFromQueue) { allowRemoveFromQueue = false; //foreach (SimpleButton item in QueuedItems) for (int i = 0; i < QueuedItems.Length; i++) { Structure s = SelectedUnits[0] as Structure; if (mouseState.LeftButton == ButtonState.Pressed && QueuedItems[i].Contains(mouseState.X, mouseState.Y) && s.BuildQueue.Count > i) //if (QueuedItems[i].Triggered) { //s.BuildQueue.RemoveAt(i); s.RemoveFromBuildQueue(i); ScheduledStructureCommand cancelCommand = new ScheduledStructureCommand(currentScheduleTime, s, CommandButtonType.Cancel); cancelCommand.Index = (short)i; Player.Me.ScheduledActions.Add(cancelCommand); TransmitStructureCommand(s, cancelCommand.CommandType, short.MinValue, (short)i); } } } } if ((usingTargetedCommand && mouseState.LeftButton == ButtonState.Pressed) || selecting) { allowMiniMapClick = false; } else if (mouseState.LeftButton == ButtonState.Released) { allowMiniMapClick = true; } // clicked on bottom ui if (mouseState.Y > worldViewport.Height) { if (!selecting) { SelectBox.Enabled = false; //SelectBox.Clear(); //SelectingUnits.Clear(); } if (mouseState.LeftButton == ButtonState.Pressed) { if (minimap.Contains(mouseState.X, mouseState.Y)) { if (allowMiniMapClick) { //Vector2 mousePosition = Vector2.Transform(new Vector2((mouseState.X - minimapPosX) / minimapToMapRatioX, (mouseState.Y - minimapPosY) / minimapToMapRatioY), camera.get_transformation(worldViewport)); Vector2 mousePosition = new Vector2(mouseState.X, mouseState.Y); Vector2 minimapCenterPoint = new Vector2(minimap.X + minimap.Width / 2f, minimap.Y + minimap.Height / 2f); float distance = Vector2.Distance(mousePosition, minimapCenterPoint); float angle = (float)Math.Atan2(mousePosition.Y - minimapCenterPoint.Y, mousePosition.X - minimapCenterPoint.X); mousePosition = new Vector2(minimapCenterPoint.X + distance * (float)Math.Cos(angle - camera.Rotation), minimapCenterPoint.Y + distance * (float)Math.Sin(angle - camera.Rotation)); camera.Pos = new Vector2((mousePosition.X - minimapPosX) / minimapToMapRatioX, (mousePosition.Y - minimapPosY) / minimapToMapRatioY); //Matrix transform = camera.get_minimap_transformation(minimapViewport); //Vector2 mousePosition = Vector2.Transform(new Vector2(mouseState.X - minimapPosX, mouseState.Y - minimapPosY), transform); //camera.Pos = new Vector2((mouseState.X - minimapPosX) / minimapToMapRatioX, (mouseState.Y - minimapPosY) / minimapToMapRatioY); //float asdf = minimapToMapRatioX - minimapToMapRatioY; //float smallest = minimapToMapRatioY; //camera.Pos = new Vector2(mousePosition.X / minimapToMapRatioX, mousePosition.Y / minimapToMapRatioY); //camera.Pos = mousePosition; stopTargetedCommands(); } } else { stopTargetedCommands(); //SimpleButton.PressingHotkey = false; } } } // clicked somewhere above bottom ui else { if (mouseState.LeftButton == ButtonState.Released) { SelectBox.Enabled = true; } } if (usingTargetedCommand) { if (mouseState.LeftButton == ButtonState.Released) { allowTargetedCommand = true; } else if (allowTargetedCommand && mouseState.LeftButton == ButtonState.Pressed) { allowTargetedCommand = false; allowSelect = false; SelectBox.Enabled = false; Vector2 mousePosition = new Vector2(mouseState.X, mouseState.Y); if (minimap.Contains(mouseState.X, mouseState.Y)) { mousePosition = new Vector2((mousePosition.X - minimapPosX) / minimapToMapRatioX, (mousePosition.Y - minimapPosY) / minimapToMapRatioY); } else { mousePosition = Vector2.Transform(mousePosition, Matrix.Invert(camera.get_transformation(worldViewport))); } if (usingAttackCommand) { giveAttackCommand(mousePosition); } else if (usingRallyPointCommand) { setRallyPoint(mousePosition); } if (usingShift) { queueingTargetedCommand = true; } else { stopTargetedCommands(); } } } else if (placingStructure) { if (mouseState.LeftButton == ButtonState.Released) { allowClickToPlaceStructure = true; } else if (allowClickToPlaceStructure && mouseState.LeftButton == ButtonState.Pressed) { allowClickToPlaceStructure = false; allowSelect = false; SelectBox.Enabled = false; if (allowPlacingStructure) { //new Barracks(placingStructureLocation, myTeam); giveBuildCommand(); if (usingShift) { queueingPlacingStructure = true; } else { placingStructure = false; } } else { playErrorSound(); } } } else { if (mouseState.LeftButton == ButtonState.Released) { allowSelect = true; SelectBox.Enabled = true; } if (allowSelect) { SelectUnits(gameTime); } } }
void SelectUnits(GameTime gameTime) { //SelectBox.Box.CalculateCorners(); bool structureInPreviousSelection = false; foreach (RtsObject o in SelectedUnits) { if (o is Structure) { structureInPreviousSelection = true; } } int selectingUnitsCount = SelectingUnits.Count; SelectingUnits.Clear(); timeSinceLastSimpleClick += (int)gameTime.ElapsedGameTime.TotalMilliseconds; bool simpleClick = (SelectBox.Box.GreaterOfWidthAndHeight <= simpleClickSize); if (SelectBox.IsSelecting) { selecting = true; unitsSelected = false; /*foreach (Unit unit in Unit.Units) * { * if ((simpleClick && unit.Contains(Vector2.Transform(new Vector2(mouseState.X, mouseState.Y), Matrix.Invert(camera.get_transformation(worldViewport))))) || * (!simpleClick && SelectBox.Box.Rectangle.Intersects(unit.Rectangle))) * { * if (SelectingUnits.Count < MAXSELECTIONSIZE) * SelectingUnits.Add(unit); * } * } * foreach (Structure structure in Structure.Structures) * { * if ((simpleClick && structure.Contains(Vector2.Transform(new Vector2(mouseState.X, mouseState.Y), Matrix.Invert(camera.get_transformation(worldViewport))))) || * (!simpleClick && SelectBox.Box.Rectangle.Intersects(structure.Rectangle))) * { * if (SelectingUnits.Count < MAXSELECTIONSIZE) * SelectingUnits.Add(structure); * } * }*/ foreach (RtsObject o in RtsObject.RtsObjects) { if ((simpleClick && o.Contains(Vector2.Transform(new Vector2(mouseState.X, mouseState.Y), Matrix.Invert(camera.get_transformation(worldViewport))))) || (!simpleClick && SelectBox.Box.Rectangle.Intersects(o.Rectangle))) { if (!simpleClick && o.Team != Player.Me.Team) { continue; } if (SelectingUnits.Count < MAXSELECTIONSIZE && o.Visible) { SelectingUnits.Add(o); } } } /*for (int i = 0; i < SelectingUnits.Count; ) * { * RtsObject o = SelectingUnits[i]; * * if (!o.Visible) * SelectingUnits.Remove(o); * else * i++; * }*/ } else if (unitsSelected == false) { selecting = false; unitsSelected = true; selectedUnitsChanged = true; unitInSelection = false; newUnitInSelection = false; myTeamInSelection = false; bool objectClicked = false; // holding shift if (usingShift) { // dont do if enemy unit selected if (SelectedUnits.Count == 0 || SelectedUnits[0].Team == Player.Me.Team) { foreach (RtsObject o in RtsObject.RtsObjects) { if (!o.Visible)// || (SelectedUnits.Count > 0 && o.Team != SelectedUnits[0].Team)) { continue; } if ((simpleClick && o.Contains(Vector2.Transform(new Vector2(mouseState.X, mouseState.Y), Matrix.Invert(camera.get_transformation(worldViewport))))) || (!simpleClick && SelectBox.Box.Rectangle.Intersects(o.Rectangle))) { // holding ctrl or double click if ((simpleClick && lastUnitClicked == o && timeSinceLastSimpleClick <= doubleClickDelay) || (simpleClick && keyboardState.IsKeyDown(Keys.LeftControl))) { timeSinceLastSimpleClick = 0; Unit unit = o as Unit; if (unit != null) { foreach (Unit u in Unit.Units) { if (u.Type == unit.Type && u.Team == unit.Team && !u.IsOffScreen(worldViewport, camera)) { if (SelectedUnits.Count >= MAXSELECTIONSIZE) { break; } if (!SelectedUnits.Contains(u)) { SelectedUnits.Add(u); newUnitInSelection = true; } } } } Structure structure = o as Structure; if (structure != null) { foreach (Structure s in Structure.Structures) { if (s.Type == structure.Type && s.Team == structure.Team && !s.IsOffScreen(worldViewport, camera)) { if (SelectedUnits.Count >= MAXSELECTIONSIZE) { break; } if (!SelectedUnits.Contains(s)) { SelectedUnits.Add(s); } } } } } // not holding ctrl or double click else { if (!SelectedUnits.Contains(o)) { if (SelectedUnits.Count < MAXSELECTIONSIZE) { SelectedUnits.Add(o); if (o is Unit) { newUnitInSelection = true; } } //selectedUnitsChanged = true; } else if (simpleClick) { SelectedUnits.Remove(o); //selectedUnitsChanged = true; } } lastUnitClicked = o; objectClicked = true; } } } } // not holding shift else { SelectedUnits.Clear(); //selectedUnitsChanged = true; foreach (RtsObject o in RtsObject.RtsObjects) { if (!o.Visible) { continue; } if ((simpleClick && o.Contains(Vector2.Transform(new Vector2(mouseState.X, mouseState.Y), Matrix.Invert(camera.get_transformation(worldViewport))))) || (!simpleClick && SelectBox.Box.Rectangle.Intersects(o.Rectangle))) { // holding ctrl or double click if ((simpleClick && lastUnitClicked == o && timeSinceLastSimpleClick <= doubleClickDelay) || (simpleClick && keyboardState.IsKeyDown(Keys.LeftControl))) { timeSinceLastSimpleClick = 0; //if (o.Team != Player.Me.Team) // continue; Unit unit = o as Unit; if (unit != null) { foreach (Unit u in Unit.Units) { if (u.Type == unit.Type && u.Team == unit.Team && !u.IsOffScreen(worldViewport, camera)) { if (SelectedUnits.Count >= MAXSELECTIONSIZE) { break; } if (!SelectedUnits.Contains(u)) { SelectedUnits.Add(u); newUnitInSelection = true; } } } } Structure structure = o as Structure; if (structure != null) { foreach (Structure s in Structure.Structures) { if (s.Type == structure.Type && s.Team == structure.Team && !s.IsOffScreen(worldViewport, camera)) { if (SelectedUnits.Count >= MAXSELECTIONSIZE) { break; } if (!SelectedUnits.Contains(s)) { SelectedUnits.Add(s); } } } } } // not holding ctrl or double click else { if (SelectedUnits.Count < MAXSELECTIONSIZE && !SelectedUnits.Contains(o)) { //if (SelectedUnits.Count == 0 || SelectedUnits[0].Team == Player.Me.Team) SelectedUnits.Add(o); if (o is Unit) { newUnitInSelection = true; } } } lastUnitClicked = o; objectClicked = true; } } SelectedUnits.SetActiveTypeToMostPopulousType(); } if (simpleClick) { timeSinceLastSimpleClick = 0; } foreach (RtsObject o in SelectedUnits) { if (o.Team == Player.Me.Team) { myTeamInSelection = true; break; } } if (myTeamInSelection) { for (int i = 0; i < SelectedUnits.Count;) { RtsObject o = SelectedUnits[i]; if (o.Team != Player.Me.Team) { SelectedUnits.Remove(o); } else { i++; } } } foreach (RtsObject o in SelectedUnits) { if (o is Unit) { unitInSelection = true; break; } } if (unitInSelection) { if (!usingShift || (!structureInPreviousSelection && newUnitInSelection)) { for (int i = 0; i < SelectedUnits.Count;) { RtsObject o = SelectedUnits[i]; if (o is Structure) { SelectedUnits.Remove(o); } else { i++; } } } } if (!objectClicked) { lastUnitClicked = null; } } }