// returns true if a special command is being used protected override bool DoSpecialCommands(UnitCommand command, GameTime gameTime) { BuildStructureCommand buildStructureCommand = command as BuildStructureCommand; if (buildStructureCommand != null) { moveToBuildLocation(buildStructureCommand, gameTime); return(true); } HarvestCommand harvestCommand = command as HarvestCommand; if (harvestCommand != null) { moveToHarvestLocation(harvestCommand, gameTime); return(true); } ReturnCargoCommand returnCargoCommand = command as ReturnCargoCommand; if (returnCargoCommand != null) { moveToReturnCargo(returnCargoCommand, gameTime); return(true); } return(false); }
public ScheduledUnitCommand(float scheduledTime, UnitCommand unitCommand, bool queued) : base(scheduledTime) { UnitCommand = unitCommand; Queued = queued; Team = unitCommand.Unit.Team; MoveCommand moveCommand = UnitCommand as MoveCommand; if (moveCommand != null) { // add new command to pathfinding queue Rts.pathFinder.AddPathFindRequest(moveCommand, queued, false, false); } }
void nextCommand() { UnitCommand lastCommand = Commands[0]; lastCommand.Active = false; Commands.RemoveAt(0); MoveCommand lastMoveCommand = lastCommand as MoveCommand; if (lastMoveCommand != null) { lastMoveDestination = lastWayPoint; if (Commands.Count > 0 && Commands[0] is MoveCommand) { MoveCommand newMoveCommand = (MoveCommand)Commands[0]; PathFinder.AddHighPriorityPathFindRequest(this, newMoveCommand, CurrentPathNode, (int)Vector2.DistanceSquared(centerPoint, newMoveCommand.Destination), false); //newMoveCommand.WayPoints = PathFinder.FindPath(CurrentPathNode, newMoveCommand.Destination, false); } } timeSinceLastRecalculatePath = 0; attackStarted = false; }
void processUnitStatusUpdate(NetIncomingMessage msg) { short unitID = msg.ReadInt16(); short team = msg.ReadInt16(); short hp = msg.ReadInt16(); Vector2 position = new Vector2(msg.ReadFloat(), msg.ReadFloat()); float rotation = msg.ReadFloat(); bool idle = msg.ReadBoolean(); Unit unit = Player.Players[team].UnitArray[unitID]; if (unit != null) { if (hp < unit.Hp && !unit.IsDead) { unit.Hp = hp; if (hp <= 0) { unit.Die(); } return; } //if (unit.IsIdle) if (idle) { if (unit.IsIdle) { unit.CenterPoint = position; } if (!unit.IsIdle) { unit.NextCommand(); } } else { Vector2 expectedPosition = new Vector2(position.X + unit.Speed * connection.AverageRoundtripTime / 2 * (float)Math.Cos(rotation), position.Y + unit.Speed * connection.AverageRoundtripTime / 2 * (float)Math.Sin(rotation)); if (Vector2.Distance(expectedPosition, unit.CenterPoint) > unit.Radius) { //unit.CenterPoint -= new Vector2((unit.CenterPoint.X - expectedPosition.X), (unit.CenterPoint.Y - expectedPosition.Y)); unit.CenterPoint = expectedPosition; } } // read current command ID int commandID = msg.ReadInt16(); // if its not the same as our current command, look for it in queue if (commandID != -1 && unit.Commands.Count > 0 && commandID != unit.Commands[0].ID) { for (int i = 1; i < unit.Commands.Count; i++) { UnitCommand command = unit.Commands[i]; if (command.ID == commandID) { // do NextCommand enough times to catch up for (int s = 0; s < i; s++) { unit.NextCommand(); } } } } // read cargoAmount at end if worker WorkerNublet worker = unit as WorkerNublet; if (worker != null) { short cargoAmount = msg.ReadInt16(); worker.CargoAmount = cargoAmount; } } }
public void QueueCommand(UnitCommand command) { Commands.Add(command); }
public void GiveCommand(UnitCommand command) { if (Busy) { if (Commands.Count > 1) { //for (int i = 1; i < Commands.Count - 1; i++) // Player.Players[Team].UnitCommands.Remove(Commands[i]); Commands.RemoveRange(1, Commands.Count - 1); } return; } IgnoringCollision = (command is HarvestCommand || command is ReturnCargoCommand); deactivateAllCommands(); clearCommands(); Commands.Add(command); }
protected virtual bool DoSpecialCommands(UnitCommand command, GameTime gameTime) { return false; }
public ScheduledUnitTargetedCommand(float scheduledTime, UnitCommand unitCommand, BaseObject target, bool queued) : base(scheduledTime, unitCommand, queued) { Target = target; }
public void GiveCommand(UnitCommand command) { deactivateAllCommands(); Commands.Clear(); Commands.Add(command); }
// returns true if a special command is being used protected override bool DoSpecialCommands(UnitCommand command, GameTime gameTime) { BuildStructureCommand buildStructureCommand = command as BuildStructureCommand; if (buildStructureCommand != null) { moveToBuildLocation(buildStructureCommand, gameTime); return true; } HarvestCommand harvestCommand = command as HarvestCommand; if (harvestCommand != null) { moveToHarvestLocation(harvestCommand, gameTime); return true; } ReturnCargoCommand returnCargoCommand = command as ReturnCargoCommand; if (returnCargoCommand != null) { moveToReturnCargo(returnCargoCommand, gameTime); return true; } return false; }
public void Update(GameTime gameTime) { int elapsedMilliseconds = (int)gameTime.ElapsedGameTime.TotalMilliseconds; timeSinceLastRecalculatePath += elapsedMilliseconds; timeSinceLastAttack += elapsedMilliseconds; updateCurrentPathNode(); if (Commands.Count == 0) { return; } UnitCommand command = Commands[0]; if (command is AttackCommand) { AttackCommand attackCommand = (AttackCommand)command; if (attackCommand.Target.IsDead) { nextCommand(); } else { Attack(attackCommand, gameTime); performAttackIfStarted(attackCommand); } } else if (command is MoveCommand) { MoveCommand moveCommand = (MoveCommand)command; /*if (timeSinceLastRecalculatePath >= recalculatePathDelay && moveCommand.Calculated) * { * timeSinceLastRecalculatePath = 0; * PathFinder.AddLowPriorityPathFindRequest(this, moveCommand, CurrentPathNode, (int)Vector2.Distance(centerPoint, moveCommand.Destination), false); * }*/ //if (instanceFrameCount % reCalculatePathFrameDelay == 1) // PathFinder.SmoothPath(moveCommand.WayPoints, this); Move(moveCommand, gameTime); } // update attack command destinations for (int i = 0; i < Commands.Count; i++) { AttackCommand c = Commands[i] as AttackCommand; if (c != null) { if (c.Target.IsDead) { Commands.Remove(c); i--; } else { c.Destination = c.Target.CenterPoint; } } } // update queued command starting points for (int i = 1; i < Commands.Count; i++) { MoveCommand c = Commands[i] as MoveCommand; MoveCommand previous = Commands[i - 1] as MoveCommand; if (c != null && previous != null) { c.WayPoints[0] = previous.Destination; } } // recalculate queued paths /*if (command is MoveCommand && instanceFrameCount % (reCalculatePathFrameDelay) == 0) * { * MoveCommand moveCommand = (MoveCommand)command; * for (int i = 1; i < Commands.Count; i++) * { * MoveCommand c = Commands[i] as MoveCommand; * MoveCommand previousCommand = Commands[i - 1] as MoveCommand; * if (c != null && previousCommand != null) * { * int y = (int)MathHelper.Clamp(previousCommand.Destination.Y / Map.TileSize, 0, Map.Height - 1); * int x = (int)MathHelper.Clamp(previousCommand.Destination.X / Map.TileSize, 0, Map.Width - 1); * * PathNode node = PathFinder.PathNodes[y, x]; * if (!node.Tile.Walkable) * node = PathFinder.FindNearestPathNode(y, x); * * c.WayPoints = PathFinder.FindPath(node, c.Destination, false); * } * } * }*/ }