So, I've been working on a game for awhile, and I've run into a snag. I have no idea how to go about this. Here's my problem:
I have ship, with forward-facing railguns and two missiles. The railguns can only fire in a straight line in front of the ship, but the missiles can fire if a ship is anywhere in a cone in front of the ship. The problem, is that the ship is rotating and moving about the screen. For the railguns, I need to check if there's a ship anywhere in front of the ship on the screen. For the missiles, I need to check if there's a ship within 250px in front of the ship, but no more than 45 degrees to either side.
Any help you guys could give would be appreciated. Thank you!
For the railguns you could do somthing like this:
public delegate void inRange();
public event inRange shipInRange;
public void checkRange() {
if(enemyShip.position.x < ship.position.x + 250 ||
enemyShip.position.x > ship.position.x + 250)
shipInRange();
}
Although I haven't tested it yet, you should check if enemyship
is 250 units behind or ahead of ship
. This assumes that you are only moving your boats on the x axis. Although it would be easy to add the y axis, I also recommend a check for which direction you are facing, then put a event for each direction and listen to the one you are facing. That would probably be the easiest way.
Then all you have to do is to listen for that event, and do what you need to do when it happens. Same thing for the missile, but check the angle as well.
Also if you had some example code, I might be able to provide you with a better example.