void Move(MoveCommand command, GameTime gameTime) { timeSinceLastSmoothPath += (int)gameTime.ElapsedGameTime.TotalMilliseconds; if (timeSinceLastSmoothPath >= smoothPathDelay) { timeSinceLastSmoothPath = 0; PathFinder.SmoothImmediatePath(command.WayPoints, this); } clearPushStatus(); clearHitWallStatus(); Vector2 wayPoint = command.WayPoints[0]; float moveX = Util.ScaleWithGameTime(speed.X, gameTime); float moveY = Util.ScaleWithGameTime(speed.Y, gameTime); if (command.WayPoints.Count > 1) { if (Contains(wayPoint)) { lastWayPoint = wayPoint; command.NextWayPoint(this, PathFinder); return; } } else { Vector2 difference = wayPoint - centerPoint; if (Math.Abs(difference.X) < moveX && Math.Abs(difference.Y) < moveY) { this.CenterPoint = wayPoint; lastWayPoint = wayPoint; //command.NextWayPoint(this, PathFinder); //if (command.WayPoints.Count == 0) nextCommand(); return; } } float angle = (float)Math.Atan2(wayPoint.Y - CenterPoint.Y, wayPoint.X - CenterPoint.X); moveX *= (float)Math.Cos(angle); moveY *= (float)Math.Sin(angle); lastMove.X = moveX; lastMove.Y = moveY; PrecisePosition += lastMove; checkForWallHit(); if (checkForPush(command)) { return; } turnTowards(wayPoint, 120 / Radius, gameTime); }
// for move command. returns true if command ended protected bool checkForPush(MoveCommand command) { //lock (PotentialCollisions) { foreach (Unit unit in PotentialCollisions) { if (!unit.IgnoringCollision && Intersects(unit)) { if (timeSinceLastRecalculatePath >= recalculatePathDelay && command.Calculated) { timeSinceLastRecalculatePath = 0; Rts.pathFinder.AddPathFindRequest(command, false, true, false); } float angle = (float)Math.Atan2(unit.centerPoint.Y - centerPoint.Y, unit.centerPoint.X - centerPoint.X); float distance = Radius + unit.Radius; float force = distance - Vector2.Distance(unit.centerPoint, centerPoint); if (unit.Team != Team) { PushSimple(angle + (float)Math.PI, force); } else if (unit.IsIdle && (unit.lastMoveDestination == command.Destination || unit.Contains(command.Destination))) { PushSimple(angle + (float)Math.PI, force); lastWayPoint = command.WayPoints[0]; if (!(command is BuildStructureCommand)) { command.NextWayPoint(this, Rts.pathFinder); if (command.WayPoints.Count == 0) { NextCommand(); return true; } } } else if (unit.lastMoveDestination == command.Destination && unit.Contains(command.Destination)) { PushSimple(angle + (float)Math.PI, force); lastWayPoint = command.WayPoints[0]; if (!(command is BuildStructureCommand)) { command.NextWayPoint(this, Rts.pathFinder); if (command.WayPoints.Count == 0) { NextCommand(); return true; } } command.NextWayPoint(this, Rts.pathFinder); if (command.WayPoints.Count == 0 && !(command is BuildStructureCommand)) { NextCommand(); return true; } } else if (Contains(command.Destination)) { PushSimple(angle + (float)Math.PI, force); lastWayPoint = command.WayPoints[0]; if (!(command is BuildStructureCommand)) { command.NextWayPoint(this, Rts.pathFinder); if (command.WayPoints.Count == 0) { NextCommand(); return true; } } } //if (unit.isFollowing && unit.followTarget == this) //{ // unit.Push(this, angle, force); //} //else if (isFollowing && unit == followTarget) //{ // PushSimple(angle + (float)Math.PI, force); //} // else { //pushCount++; float sizeRatio = this.Diameter / unit.Diameter; //float pushForce = force * (.05f * sizeRatio); float pushForce; if (unit.IsMoving) pushForce = force * (.05f * sizeRatio); else pushForce = force * (.3f * sizeRatio); unit.Push(this, angle, pushForce); PushSimple(angle + (float)Math.PI, force - pushForce); //PushSimple(angle + (float)Math.PI, force * .90f); //unit.Push(this, angle, force * .1f); //PushSimple(angle + (float)Math.PI, force * .9f); } } } } /*foreach (PathNode pathNode in OccupiedPathNodes) { foreach (Unit unit in pathNode.UnitsContained) { if (unit != this && Intersects(unit)) { if (timeSinceLastRecalculatePath >= recalculatePathDelay && command.Calculated) { timeSinceLastRecalculatePath = 0; PathFinder.AddLowPriorityPathFindRequest(this, command, CurrentPathNode, (int)Vector2.DistanceSquared(centerPoint, command.Destination), false); } float angle = (float)Math.Atan2(unit.centerPoint.Y - centerPoint.Y, unit.centerPoint.X - centerPoint.X); float distance = Radius + unit.Radius; float force = distance - Vector2.Distance(unit.centerPoint, centerPoint); if (unit.IsIdle && (unit.lastMoveDestination == command.Destination || unit.Contains(command.Destination))) { PushSimple(angle + (float)Math.PI, force); lastWayPoint = command.WayPoints[0]; command.NextWayPoint(this, PathFinder); if (command.WayPoints.Count == 0) { nextCommand(); return true; } } else if (unit.lastMoveDestination == command.Destination && unit.Contains(command.Destination)) { PushSimple(angle + (float)Math.PI, force); lastWayPoint = command.WayPoints[0]; command.NextWayPoint(this, PathFinder); if (command.WayPoints.Count == 0) { nextCommand(); return true; } } else if (Contains(command.Destination)) { PushSimple(angle + (float)Math.PI, force); lastWayPoint = command.WayPoints[0]; command.NextWayPoint(this, PathFinder); if (command.WayPoints.Count == 0) { nextCommand(); return true; } } //if (unit.isFollowing && unit.followTarget == this) //{ // unit.Push(this, angle, force); //} //else if (isFollowing && unit == followTarget) //{ // PushSimple(angle + (float)Math.PI, force); //} // else { //pushCount++; float sizeRatio = this.Diameter / unit.Diameter; float pushForce = force * (.1f * sizeRatio); unit.Push(this, angle, pushForce); PushSimple(angle + (float)Math.PI, force - pushForce); //unit.Push(this, angle, force * .1f); //PushSimple(angle + (float)Math.PI, force * .9f); } } } }*/ return false; }
/*void Move(Vector2 target, GameTime gameTime) { clearPushStatus(); float moveX = Util.ScaleWithGameTime(speed.X, gameTime); float moveY = Util.ScaleWithGameTime(speed.Y, gameTime); Vector2 difference = target - centerPoint; if (Math.Abs(difference.X) < moveX && Math.Abs(difference.Y) < moveY) { this.CenterPoint = target; nextWayPoint(); return; } float angle = (float)Math.Atan2((double)(target.Y - CenterPoint.Y), (double)(target.X - CenterPoint.X)); moveX *= (float)Math.Cos(angle); moveY *= (float)Math.Sin(angle); lastMove.X = moveX; lastMove.Y = moveY; PrecisePosition += lastMove; checkForWallHit(); //foreach (Unit unit in Units) foreach (Unit unit in PotentialCollisions) { if (unit != this && Intersects(unit)) { if (isMoving) { if (unit.isIdle && unit.lastWayPoint == target) { nextWayPoint(); //Stop(); } else if (Contains(MoveTarget))// || unit.Contains(moveTarget)) { nextWayPoint(); //Stop(); } } angle = (float)Math.Atan2(unit.centerPoint.Y - centerPoint.Y, unit.centerPoint.X - centerPoint.X); float distance = Radius + unit.Radius; float force = distance - Vector2.Distance(unit.centerPoint, centerPoint); if (unit.isFollowing && unit.followTarget == this) { unit.Push(this, angle, force); } else if (isFollowing && unit == followTarget) { //Push(angle + (float)Math.PI, force); PushSimple(angle + (float)Math.PI, force); } else { isPushing = true; unit.Push(this, angle, force * .1f); PushSimple(angle + (float)Math.PI, force * .9f); //Push(angle + (float)Math.PI, force * .5f); //unit.Push(angle, force); } } } //if (instanceFrameCount % 6 == 0) // checkIfCurrentPathNodeChanged(); checkIfCloserToNextWayPointThanCurrentWayPoint(); //if (instanceFrameCount % 120 == 0) // checkIfCloserToGoalThanCurrentWayPoint(); turnTowards(target, 100 / radius, gameTime); }*/ void Move(MoveCommand command, GameTime gameTime) { checkForSmoothPath(command, gameTime); clearPushStatus(); clearHitWallStatus(); Vector2 wayPoint = command.WayPoints[0]; //float moveX = Util.ScaleWithGameTime(speed.X, gameTime); //float moveY = Util.ScaleWithGameTime(speed.Y, gameTime); Speed = MathHelper.Min(Speed + Util.ScaleWithGameTime(acceleration, gameTime), MaxSpeed); float actualSpeed = Util.ScaleWithGameTime(Speed, gameTime); //float moveX = Util.ScaleWithGameTime(Speed, gameTime); //float moveY = moveX; if (command.WayPoints.Count > 1) { if (Contains(wayPoint)) { lastWayPoint = wayPoint; command.NextWayPoint(this, Rts.pathFinder); return; } } else { //Vector2 difference = wayPoint - centerPoint; float distance = Vector2.Distance(wayPoint, centerPoint); if (distance < actualSpeed) { this.CenterPoint = wayPoint; HasMoved = true; lastWayPoint = wayPoint; //command.NextWayPoint(this, PathFinder); //if (command.WayPoints.Count == 0) NextCommand(); return; } } //float angle = (float)Math.Atan2(wayPoint.Y - CenterPoint.Y, wayPoint.X - CenterPoint.X); //moveX *= (float)Math.Cos(angle); //moveY *= (float)Math.Sin(angle); //lastMove.X = moveX; //lastMove.Y = moveY; lastMove = new Vector2(wayPoint.X - CenterPoint.X, wayPoint.Y - CenterPoint.Y); //lastMove.Normalize(); //lastMove *= actualSpeed; lastMove *= (actualSpeed / lastMove.Length()); PrecisePosition += lastMove; HasMoved = true; if (checkForWallHit(command) && Vector2.Distance(centerPoint, command.Destination) < Radius) { NextCommand(); return; } if (checkForPush(command)) return; if (!turnTowards(wayPoint, 120 / Radius, gameTime)) { Speed = MathHelper.Max(Speed - Util.ScaleWithGameTime(acceleration, gameTime), 0); } }
void Move(MoveCommand command, GameTime gameTime) { timeSinceLastSmoothPath += (int)gameTime.ElapsedGameTime.TotalMilliseconds; if (timeSinceLastSmoothPath >= smoothPathDelay) { timeSinceLastSmoothPath = 0; PathFinder.SmoothImmediatePath(command.WayPoints, this); } clearPushStatus(); clearHitWallStatus(); Vector2 wayPoint = command.WayPoints[0]; float moveX = Util.ScaleWithGameTime(speed.X, gameTime); float moveY = Util.ScaleWithGameTime(speed.Y, gameTime); if (command.WayPoints.Count > 1) { if (Contains(wayPoint)) { lastWayPoint = wayPoint; command.NextWayPoint(this, PathFinder); return; } } else { Vector2 difference = wayPoint - centerPoint; if (Math.Abs(difference.X) < moveX && Math.Abs(difference.Y) < moveY) { this.CenterPoint = wayPoint; lastWayPoint = wayPoint; //command.NextWayPoint(this, PathFinder); //if (command.WayPoints.Count == 0) nextCommand(); return; } } float angle = (float)Math.Atan2(wayPoint.Y - CenterPoint.Y, wayPoint.X - CenterPoint.X); moveX *= (float)Math.Cos(angle); moveY *= (float)Math.Sin(angle); lastMove.X = moveX; lastMove.Y = moveY; PrecisePosition += lastMove; checkForWallHit(); if (checkForPush(command)) return; turnTowards(wayPoint, 120 / Radius, gameTime); }
// for move command. returns true if command ended bool checkForPush(MoveCommand command) { lock (PotentialCollisions) { foreach (Unit unit in PotentialCollisions) { if (Intersects(unit)) { if (timeSinceLastRecalculatePath >= recalculatePathDelay && command.Calculated) { timeSinceLastRecalculatePath = 0; PathFinder.AddLowPriorityPathFindRequest(this, command, CurrentPathNode, (int)Vector2.DistanceSquared(centerPoint, command.Destination), false); } float angle = (float)Math.Atan2(unit.centerPoint.Y - centerPoint.Y, unit.centerPoint.X - centerPoint.X); float distance = Radius + unit.Radius; float force = distance - Vector2.Distance(unit.centerPoint, centerPoint); if (unit.IsIdle && (unit.lastMoveDestination == command.Destination || unit.Contains(command.Destination))) { PushSimple(angle + (float)Math.PI, force); lastWayPoint = command.WayPoints[0]; command.NextWayPoint(this, PathFinder); if (command.WayPoints.Count == 0) { nextCommand(); return true; } } else if (unit.lastMoveDestination == command.Destination && unit.Contains(command.Destination)) { PushSimple(angle + (float)Math.PI, force); lastWayPoint = command.WayPoints[0]; command.NextWayPoint(this, PathFinder); if (command.WayPoints.Count == 0) { nextCommand(); return true; } } else if (Contains(command.Destination)) { PushSimple(angle + (float)Math.PI, force); lastWayPoint = command.WayPoints[0]; command.NextWayPoint(this, PathFinder); if (command.WayPoints.Count == 0) { nextCommand(); return true; } } /*if (unit.isFollowing && unit.followTarget == this) { unit.Push(this, angle, force); } else if (isFollowing && unit == followTarget) { PushSimple(angle + (float)Math.PI, force); } else*/ { //pushCount++; float sizeRatio = this.Diameter / unit.Diameter; float pushForce = force * (.1f * sizeRatio); unit.Push(this, angle, pushForce); PushSimple(angle + (float)Math.PI, force - pushForce); //unit.Push(this, angle, force * .1f); //PushSimple(angle + (float)Math.PI, force * .9f); } } } } return false; }
// for move command. returns true if command ended bool checkForPush(MoveCommand command) { lock (PotentialCollisions) { foreach (Unit unit in PotentialCollisions) { if (Intersects(unit)) { if (timeSinceLastRecalculatePath >= recalculatePathDelay && command.Calculated) { timeSinceLastRecalculatePath = 0; PathFinder.AddLowPriorityPathFindRequest(this, command, CurrentPathNode, (int)Vector2.DistanceSquared(centerPoint, command.Destination), false); } float angle = (float)Math.Atan2(unit.centerPoint.Y - centerPoint.Y, unit.centerPoint.X - centerPoint.X); float distance = Radius + unit.Radius; float force = distance - Vector2.Distance(unit.centerPoint, centerPoint); if (unit.IsIdle && (unit.lastMoveDestination == command.Destination || unit.Contains(command.Destination))) { PushSimple(angle + (float)Math.PI, force); lastWayPoint = command.WayPoints[0]; command.NextWayPoint(this, PathFinder); if (command.WayPoints.Count == 0) { nextCommand(); return(true); } } else if (unit.lastMoveDestination == command.Destination && unit.Contains(command.Destination)) { PushSimple(angle + (float)Math.PI, force); lastWayPoint = command.WayPoints[0]; command.NextWayPoint(this, PathFinder); if (command.WayPoints.Count == 0) { nextCommand(); return(true); } } else if (Contains(command.Destination)) { PushSimple(angle + (float)Math.PI, force); lastWayPoint = command.WayPoints[0]; command.NextWayPoint(this, PathFinder); if (command.WayPoints.Count == 0) { nextCommand(); return(true); } } /*if (unit.isFollowing && unit.followTarget == this) * { * unit.Push(this, angle, force); * } * else if (isFollowing && unit == followTarget) * { * PushSimple(angle + (float)Math.PI, force); * } * else*/ { //pushCount++; float sizeRatio = this.Diameter / unit.Diameter; float pushForce = force * (.1f * sizeRatio); unit.Push(this, angle, pushForce); PushSimple(angle + (float)Math.PI, force - pushForce); //unit.Push(this, angle, force * .1f); //PushSimple(angle + (float)Math.PI, force * .9f); } } } } return(false); }