I'm creating a code to detect hotspots and characters. The hotspot detection is working like I want it to with the character standing on the hotspot and facing it's origin. This should work like 3D Lucasarts Adventure games. Any ideas?
function repeatedly_execute() // Every game loop
{
standingCharacter = Character.GetAtScreenXY(player.x , player.y);
standingHotspot = Hotspot.GetAtScreenXY(player.x, player.y); //The hotspot the player is standing on
if(player.IsFacingLocation(standingHotspot.GetProperty("InteractionX"), standingHotspot.GetProperty("InteractionY"))) // If the player is facing the hotspot he is standing on
{
// Set the facing hotspot and set description Gui
facingHotspot = standingHotspot;
lHotspot.Text = facingHotspot.Name;
}
else
{
facingHotspot = hotspot[0];
lHotspot.Text = "";
}
}
What is the purpose of the standingCharacter pointer? It looks as though you're just trying to get the player.
Also, your post seems to say that this is working. If it is working...is there a question here?
Oh, and you might want to post the Character.IsFacingLocation function you've used, as it's definitely not built-in.
The question is, how to detect if the player is facing another character.
Some time ago I've coded a pretty extensive "NoMouse" module that among other things calculates the "feet coordinates" of hotspots and objects, then uses angle and direction to evaluate what the player is currently facing.
There's no other way I can see.
But yeah, please do post the .IsFacingLocation function.
//Variables Declared
Hotspot *facingHotspot;
Hotspot *standingHotspot;
Character *standingCharacter;
Character *facingCharacter;
function IsFacingLocation(this Character*, int x, int y) // Returns if the player is facing coordinates
{
int checkLoop;
int RelativeX = x + GetViewportX() - this.x;
int RelativeY = y + GetViewportY() - this.y;
if (RelativeX > 0 && AbsInt(RelativeX) > AbsInt(RelativeY)) checkLoop = 2;
else if (RelativeX < 0 && AbsInt(RelativeX) > AbsInt(RelativeY)) checkLoop = 1;
else if (RelativeY < 0 && AbsInt(RelativeX) < AbsInt(RelativeY)) checkLoop = 3;
else if (RelativeY > 0 && AbsInt(RelativeX) < AbsInt(RelativeY)) checkLoop = 0;
return (this.Loop == checkLoop);
}
Sorry about that. I'm trying to be clear in what I would like. Yes, hotspot detection works because the player can stand on it and face a certain direction. I need a way to detect weather a character is within a certain distance, and is facing the characters location.
Since the x and y parameter for this function are static values gathered from the Custom Properties of a hotspot, they are room coordinates, not screen coordinates. So are the player's, thus the calculation of RelativeX/Y shouldn't need GetViewportX/Y.
So what exactly is the problem?
Calculating the angle or distance?
Distance is the big problem, I think. Here's something new I've tried.
I found this distance function on the forums.
function repeatedly_execute() // Every game loop
{
//Get's a character less than 50 pixels away
if((Distance(player, character[numChar]) < 50) && (player.Room == character[numChar].Room)){
standingCharacter = character[numChar];
}
else{
standingCharacter = null;
if(numChar < Game.CharacterCount - 1)
numChar++;
else
numChar = 1;
}
standingHotspot = Hotspot.GetAtScreenXY(player.x, player.y); //The hotspot the player is standing on
if(standingHotspot == hotspot[0])
standingHotspot = null;
if((standingHotspot!= null) && (player.IsFacingLocation(standingHotspot.GetProperty("InteractionX"), standingHotspot.GetProperty("InteractionY")))) // If the player is facing the hotspot he is standing on
{
// Set the facing hotspot and set description Gui
facingHotspot = standingHotspot;
lHotspot.Text = facingHotspot.Name;
}
else if((standingCharacter != null) && (player.IsFacingLocation(standingCharacter.x, standingCharacter.y)))
{
facingCharacter = standingCharacter;
lHotspot.Text = facingCharacter.Name;
}
else
{
facingHotspot = null;
facingCharacter = null;
lHotspot.Text = "";
}
}
[EDIT]Whoops, just a simple logical error with my new code. There might be a problem later since the standing character will stay the same even if another character is closer and faced if they are both in range, but this isn't a big issue.
I hope I'm making sense.