IsInRadius (CMS)

From The Official Visionaire Studio: Adventure Game Engine Wiki
Revision as of 22:29, 23 August 2022 by AFRLme (talk | contribs)
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.