// This could be smarter. If dest is too close & perpendicular, then the tank // can end up circling around. public void MoveTowards(DVector2 dest) { ComSat.Trace(this, "MoveTowards"); var dir = dest - entity.position; // also vector to dest. var targetAngle = DVector2.ToAngle(dir); var baseAngle = Utility.CalculateNewAngle(entity.rotation, targetAngle, DReal.Radians(turnSpeed)); entity.rotation = baseAngle; // Move along current heading. Ramp speed up as the angle gets closer. // Augh. // [-pi,pi] => [0,2pi] if (targetAngle < 0) { targetAngle += DReal.TwoPI; } // Get targetAngle within +/- pi of baseAngle. if (targetAngle < baseAngle - DReal.PI) { targetAngle += DReal.TwoPI; } else if (targetAngle > baseAngle + DReal.PI) { targetAngle -= DReal.TwoPI; } var diff = DReal.Abs(baseAngle - targetAngle); if (canMoveWithoutTurning || diff < maxMoveAngle) { var distance = dir.magnitude; //print("Distance: " + distance + " speed is: " + tickSpeed); var speed = minSpeed + (maxSpeed - minSpeed) * (1 - (diff / DReal.PI)); if (distance < speed) { speed = DReal.Max(minSpeed, distance); } entity.velocity = canMoveWithoutTurning ? dir.normalized * speed : DVector2.FromAngle(baseAngle) * speed; } else { Stop(); } }
void TickUpdate() { ComSat.Trace(this, "TickUpdate"); if (combatVehicle.mode == CombatVehicle.Mode.IDLE) { combatVehicle.mode = CombatVehicle.Mode.MOVE; combatVehicle.destination = entity.position; } if (ComSat.EntityExists(combatVehicle.target)) { var dist = combatVehicle.target.position - entity.position; var sqrDist = dist.sqrMagnitude; var targetAngle = DReal.Mod(DVector2.ToAngle(dist), DReal.TwoPI); // Get targetAngle within +/- pi of entity.rotation. if (targetAngle < entity.rotation - DReal.PI) { targetAngle += DReal.TwoPI; } else if (targetAngle > entity.rotation + DReal.PI) { targetAngle -= DReal.TwoPI; } if (sqrDist < missileRange * missileRange) { FireMissile(); } if (sqrDist < gunRange * gunRange && DReal.Abs(entity.rotation - targetAngle) < 1) { FireGun(); if (!gunFireSound.isPlaying) { gunFireSound.Play(); } } else { gunFireSound.Stop(); } } else { gunFireSound.Stop(); } if (missilesLoaded < maxMissiles) { if (missileReloadTime <= 0) { missileReloadTime = missileReloadDelay; } else { missileReloadTime -= ComSat.tickRate; if (missileReloadTime <= 0) { missilesLoaded += 1; } } } if (missileRecycleDelay > 0) { missileRecycleDelay -= ComSat.tickRate; } if (gunRecycleDelay > 0) { gunRecycleDelay -= ComSat.tickRate; } }