IsInRadius (CMS)

From The Official Visionaire Studio: Adventure Game Engine Wiki
Revision as of 13:56, 19 November 2014 by AFRLme (talk) (Created page with "{| class="ts" style="width:100%" |- ! style="text-align:left" | Name !! style="text-align:left" | Type !! style="text-align:left" | By |- | isInRadius(obj, rad) || Definition...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Name Type By
isInRadius(obj, rad) Definition AFRLme

This small function allows you to quickly align a character to another character.

Additional Info
Alignment is based on character center which is usually set near the feet.
There is no simple method for obtaining the top (y) pixel coordinate of the currently active character animation.

Instructions

1. Add the main script to the Visionaire Studio Script Editor & set the script as a definition script.
2a. To query if cursor is inside of a specified radius based on a characters position, create an execute a script containing...

isInRadius(Characters["character_name"], 50) -- this queries if cursor is inside of a radius of 50 pixels from characters current position

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...

isInRadius(Objects["object_name"], 100) -- this queries if cursor is inside of a radius of 100 pixels from objects center (center of the image)

3. Here is a quick example of how to use this function...

if isInRadius(Characters["Tom"], 50) then
 -- do something
else
 -- do something else
end

Main Script

local x, y -- empty variables which the objects position or center will be stored 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)
 -- + cursor position + --
 pos.x = game.ScrollPosition.x + getCursorPos().x; pos.y = game.ScrollPosition.y + getCursorPos().y
 -- + object position & radius + --
 if obj:getId().tableId == eCharacters then
  x = obj.Position.x; y = obj.Position.y
  rad = math.floor(obj.Size * rad) / 100
 elseif obj:getId().tableId == eObjects then
  x = obj.ObjectSprite.SpriteSprite:getPosition().x + (obj.ObjectSprite.SpriteSprite:getSize().x / 2)
  y = obj.ObjectSprite.SpriteSprite:getPosition().y + (obj.ObjectSprite.SpriteSprite:getSize().y / 2)
  rad = math.floor(obj.Scale * rad)  
 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
 if y > pos.y then y = math.floor(y - pos.y) else y = math.floor(pos.y - y) end
 if  math.floor( math.pow(x, 2) + math.pow(y, 2) ) <= math.pow(rad, 2) then return true end
 return false -- if not in radius then it's false
end

Syntax Breakdown

Name Type 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.
rad integer This should be an integer (number) value containing the radius value of the circle. Radius = circle diameter ÷ 2.