Hi to anyone that is reading this, and thank you for taking the time to do so. I have been trying to get some code figured out and im going out of my mind.
THE GOAL: Have a playerobject (square) and a enemyobject(square) and get a vector between them to determine if they are in line of sight of each other. (there maybe terrain objects, also square in the way). All of these objects are derived from the same "Entity" super class. (I WISH I WAS BETTER AT MATH FOR THIS) Here is my code so far, it just determines if the player is close enough to the enemy for the enemy to pay attention to him.
I think the proper way to do this.. is to create a line between the player and enemy, get all the terrainobjects that are in bounds of the rectangle of possibles, then check the objects to see if any of thier boundries are touching the line between the player and enemy. Sadly this is beyond my skills.. can someone please help me get started. AND/Or tell me if it is possible to make a 360 degree projectile but using graphics.Drawline() with the vector line between the player and enemy.
public void creatureShoot() {
CreatureEntity creature;
int xDistance, yDistance;
int playerX, playerY, creatureX, creatureY, totalDistance;
for (int j = 0; j < currentCreatureObj.size(); j++) {
creature = (CreatureEntity) currentCreatureObj.get(j);
//get coords of the center of the sprites
playerX = (int) (playerEnt.getX() + (playerEnt.getWidth()) / 2);
playerY = (int) (playerEnt.getY() + (playerEnt.getHeight()) / 2);
creatureX = (int) (creature.getX() + (creature.getWidth()) / 2);
creatureY = (int) (creature.getY() + (creature.getHeight()) / 2);
xDistance = Math.abs(playerX - creatureX);
yDistance = Math.abs(playerY - creatureY);
totalDistance = (int) Math.sqrt(Math.pow(xDistance, 2) + Math.pow(yDistance, 2));
if (totalDistance <= SHOOTING_RANGE) {
String ref = "null.jpg";
//NEED TO CHANGE THIS TO 360 DEGREE SHOOTING
int direction = (int) creature.getLastMovementDirection();
if (direction == Entity.NORTH || direction == Entity.SOUTH) { ref = "verticalmissile.gif"; }
else if (direction == Entity.WEST || direction == Entity.EAST) { ref = "horizontalmissile.gif"; }
System.out.println(totalDistance + " creature in range of player, should shoot now");
//CREATE A MISSLE OBJ IN THE LAST DIRECTION THE MONSTER WAS FACING.. NEEDS TO SWITCH TO 360 DEGREE VECTOR TO PLAYER
currentMissileObj.add(new Missile(ref, creature.getX() + creature.getWidth() / 2, creature.getY() + creature.getHeight() / 2, direction));
}
}
}