Difference between revisions of "IsInRadius (CMS)"

From The Official Visionaire Studio: Adventure Game Engine Wiki
Line 6: Line 6:
 
|}
 
|}
  
This small function allows you to quickly query if the mouse cursor is currently inside of a specified radius based on current position of a character, or center of an object. ''Visionaire Studio 4.1 is required for this function to work.''
+
This not so small function allows you to quickly query if the mouse cursor is currently inside of a specified radius based on current position of a character, or the center coordinate of a playing animation or the static image sprite or animation belonging to a scene object.
  
  
 
== Instructions ==
 
== Instructions ==
 
1. Add the [[#Main_Script|main script]] to the Visionaire Studio Script Editor & set the script as a definition script.<br/>
 
1. Add the [[#Main_Script|main script]] to the Visionaire Studio Script Editor & set the script as a definition script.<br/>
2a. To query if cursor is inside of a specified radius based on a characters position, create an ''execute a script'' containing...
+
2a. Usage Example #1: check if cursor is inside of a 50px radius from current characters position via '''if lua result''' action part.
<syntaxhighlight>
+
<syntaxhighlight lang="lua">
isInRadius(Characters["character_name"], 50) -- this queries if cursor is inside of a radius of 50 pixels from characters current position
+
return isInRadius(game.CurrentCharacter, 50)
 
</syntaxhighlight>
 
</syntaxhighlight>
2b. To query if cursor is inside of a specified radius based on the center of an objects sprite (image), create an ''execute a script'' containing...
+
2b. Usage Example #2: check if cursor is inside of a 50px radius from the center of the image/animation sprite belonging to the specified scene object via '''if lua result''' action part.
<syntaxhighlight>
+
<syntaxhighlight lang="lua">
isInRadius(Objects["object_name"], 100) -- this queries if cursor is inside of a radius of 100 pixels from objects center (center of the image)
+
return isInRadius(game.CurrentScene.Objects["object_name"], 50)
 
</syntaxhighlight>
 
</syntaxhighlight>
3. Here is a quick example of how to use this function...
+
2c. Usage Example #3: check if cursor is inside of a 50px radius from the center of the specified animation via '''if lua result''' action part.
<syntaxhighlight>
+
<syntaxhighlight lang="lua">
if isInRadius(Characters["Tom"], 50) then
+
return isInRadius(ActiveAnimations["animation_name"], 50)
  -- do something
+
</syntaxhighlight>
 +
2d. Usage Example #4: check if cursor is inside of a 50px radius from the the specified characters position inside of a regular if query.
 +
<syntaxhighlight lang="lua">
 +
if isInRadius(game.CurrentCharacter, 50) then
 +
  -- true, add some actions here
 
else
 
else
  -- do something else
+
  -- false, add some actions here
 
end
 
end
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
  
 
== Main Script ==
 
== Main Script ==
<syntaxhighlight>
+
<syntaxhighlight lang="lua">
local x, y -- empty variables which the objects position or center will be stored in
+
local x, y -- empty variables which will be used to store data in later on
 +
local radius = 20 -- specify the radius in pixels (from characters position in scene)
 
local pos = {} -- empty table which we will store the mouse cursor positions in
 
local pos = {} -- empty table which we will store the mouse cursor positions in
  
-- check if cursor is in radius (circle)
+
 
 
function isInRadius(obj, rad)
 
function isInRadius(obj, rad)
-- + cursor position + --
+
  pos.x = game.ScrollPosition.x + getCursorPos().x; pos.y = game.ScrollPosition.y + getCursorPos().y -- store current cursor position in the scene
pos.x = game.ScrollPosition.x + getCursorPos().x; pos.y = game.ScrollPosition.y + getCursorPos().y
+
  if obj:getId().tableId == eObjects then if not obj.Animation:isEmpty() then obj = ActiveAnimations[obj.Animation:getName()] end end -- check if object & contains animation
-- + object position & radius + --
+
  -- + character position & radius + --
if obj:getId().tableId == eCharacters then
+
  if obj:getId().tableId == eCharacters then -- check if character
   x = obj.Position.x; y = obj.Position.y
+
   x = obj.Position.x; y = obj.Position.y -- store characters position
   rad = math.floor(obj.Size * rad) / 100
+
   rad = math.floor(obj.Size * radius) / 100 -- dynamically adjust radius size based on current scale value of character
elseif obj:getId().tableId == eObjects then
+
  -- + object sprite position & radius + --
   x = obj.ObjectSprite.SpriteSprite:getPosition().x + (obj.ObjectSprite.SpriteSprite:getSize().x / 2)
+
  elseif obj:getId().tableId == eObjects then
   y = obj.ObjectSprite.SpriteSprite:getPosition().y + (obj.ObjectSprite.SpriteSprite:getSize().y / 2)
+
   x = obj.Sprite.Sprite:getPosition().x + (obj.Sprite.Sprite:getSize().x / 2) -- calculate center coordinate belonging to object sprite (x)
   rad = math.floor(obj.Scale * rad)
+
   y = obj.Sprite.Sprite:getPosition().y + (obj.Sprite.Sprite:getSize().y / 2) -- calculate center coordinate belonging to object sprite (y)
end
+
   rad = math.floor(obj.Scale * radius) -- dynamically adjust radius size based on current scale value of object
-- + calculate distance from circle center + --
+
  -- + animation sprite position & radius + --
if x > pos.x then x = math.floor(x - pos.x) else x = math.floor(pos.x - x) end
+
  elseif obj:getId().tableId == eActiveAnimations then
if y > pos.y then y = math.floor(y - pos.y) else y = math.floor(pos.y - y) end
+
  x = obj.CurrentPosition.x + (getAnimationSize(obj).x / 2) -- calculate center coordinate belonging to the currently displayed sprite of the animation (x)
if math.floor( math.pow(x, 2) + math.pow(y, 2) ) <= math.pow(rad, 2) then return true end
+
  y = obj.CurrentPosition.y + (getAnimationSize(obj).y / 2) -- calculate center coordinate belonging to the currently displayed sprite of the animation (y)
return false -- if not in radius then it's false
+
  rad = rad = math.floor(obj.Scale * radius) -- dynamically adjust radius size based on current scale value of animation
 +
  end
 +
  -- + calculate distance from circle center + --
 +
  if x > pos.x then x = math.floor(x - pos.x) else x = math.floor(pos.x - x) end -- fallback to invert position calculation in case x is greater than pos.x
 +
  if y > pos.y then y = math.floor(y - pos.y) else y = math.floor(pos.y - y) end -- fallback to invert position calculation in case y is greater than pos.y
 +
  if math.floor( (x ^ 2) + (y ^ 2) ) <= (rad ^ 2) then return true else return false end -- check if in radius & return true or false
 
end
 
end
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
  
 
== Syntax Breakdown ==
 
== Syntax Breakdown ==
Line 59: Line 72:
 
! style="text-align:left" | Name !! style="text-align:left" | Type !! style="text-align:left;width:80%" | Description
 
! style="text-align:left" | Name !! style="text-align:left" | Type !! style="text-align:left;width:80%" | Description
 
|-
 
|-
| obj || link || This should be a link to a scene object or a character. ''The function automatically detects if obj type is a character or object.''
+
| obj || link || This should be a link to a scene object, an active animation (currently playing), or a character. ''The function automatically detects if obj type is a character, active animation, or object.''
 
|-
 
|-
| rad || integer || This should be an integer (number) value containing the radius value of the circle. ''Radius = circle diameter ÷ 2.''
+
| rad || integer || This should be number value containing the radius value of the circle. ''Radius = circle diameter ÷ 2.''
|}
+
|}{{toc}}

Revision as of 22:29, 23 August 2022

Name Type By
isInRadius(obj, rad) Definition AFRLme

This not so small function allows you to quickly query if the mouse cursor is currently inside of a specified radius based on current position of a character, or the center coordinate of a playing animation or the static image sprite or animation belonging to a scene object.


Instructions

1. Add the main script to the Visionaire Studio Script Editor & set the script as a definition script.
2a. Usage Example #1: check if cursor is inside of a 50px radius from current characters position via if lua result action part.

return isInRadius(game.CurrentCharacter, 50)

2b. Usage Example #2: check if cursor is inside of a 50px radius from the center of the image/animation sprite belonging to the specified scene object via if lua result action part.

return isInRadius(game.CurrentScene.Objects["object_name"], 50)

2c. Usage Example #3: check if cursor is inside of a 50px radius from the center of the specified animation via if lua result action part.

return isInRadius(ActiveAnimations["animation_name"], 50)

2d. Usage Example #4: check if cursor is inside of a 50px radius from the the specified characters position inside of a regular if query.

if isInRadius(game.CurrentCharacter, 50) then
 -- true, add some actions here
else
 -- false, add some actions here
end


Main Script

local x, y -- empty variables which will be used to store data in later on
local radius = 20 -- specify the radius in pixels (from characters position in scene)
local pos = {} -- empty table which we will store the mouse cursor positions in


function isInRadius(obj, rad)
  pos.x = game.ScrollPosition.x + getCursorPos().x; pos.y = game.ScrollPosition.y + getCursorPos().y -- store current cursor position in the scene
  if obj:getId().tableId == eObjects then if not obj.Animation:isEmpty() then obj = ActiveAnimations[obj.Animation:getName()] end end -- check if object & contains animation
  -- + character position & radius + --
  if obj:getId().tableId == eCharacters then -- check if character
  x = obj.Position.x; y = obj.Position.y -- store characters position
  rad = math.floor(obj.Size * radius) / 100 -- dynamically adjust radius size based on current scale value of character
  -- + object sprite position & radius + --
  elseif obj:getId().tableId == eObjects then
  x = obj.Sprite.Sprite:getPosition().x + (obj.Sprite.Sprite:getSize().x / 2) -- calculate center coordinate belonging to object sprite (x)
  y = obj.Sprite.Sprite:getPosition().y + (obj.Sprite.Sprite:getSize().y / 2) -- calculate center coordinate belonging to object sprite (y)
  rad = math.floor(obj.Scale * radius) -- dynamically adjust radius size based on current scale value of object
  -- + animation sprite position & radius + --
  elseif obj:getId().tableId == eActiveAnimations then
  x = obj.CurrentPosition.x + (getAnimationSize(obj).x / 2) -- calculate center coordinate belonging to the currently displayed sprite of the animation (x)
  y = obj.CurrentPosition.y + (getAnimationSize(obj).y / 2) -- calculate center coordinate belonging to the currently displayed sprite of the animation (y)
  rad = rad = math.floor(obj.Scale * radius) -- dynamically adjust radius size based on current scale value of animation
  end
  -- + calculate distance from circle center + --
  if x > pos.x then x = math.floor(x - pos.x) else x = math.floor(pos.x - x) end -- fallback to invert position calculation in case x is greater than pos.x
  if y > pos.y then y = math.floor(y - pos.y) else y = math.floor(pos.y - y) end -- fallback to invert position calculation in case y is greater than pos.y
  if math.floor( (x ^ 2) + (y ^ 2) ) <= (rad ^ 2) then return true else return false end -- check if in radius & return true or false
end


Syntax Breakdown

Name Type Description
obj link This should be a link to a scene object, an active animation (currently playing), or a character. The function automatically detects if obj type is a character, active animation, or object.
rad integer This should be number value containing the radius value of the circle. Radius = circle diameter ÷ 2.