-- Smooth Character Turning Test/Demonstation Script
-- (c) Simon Scheckel 2014 - Visionaire Studio Engine
smt_animation = {Rufus = {135, 150, 180, 225, 270, 315, 0, 25, 45, 90, 110}}
smt_direction = game.CurrentCharacter.Direction
smt_state = game.CurrentCharacter.AnimState
smt_dest = game.CurrentCharacter.Position
smt_destobj = game.CurrentCharacter.DestinationObject
smt_actionc = game.CurrentCharacter.ActionCharacter
smt_anim = 0
function angleDist(a,b)
return math.min(math.min( math.abs(a + 360 - b), math.abs(a - 360 - b)), math.abs(a - b))
end
function smoothTurningMainLoop()
local newstate = game.CurrentCharacter.AnimState
local newdir = game.CurrentCharacter.Direction
local c = game.CurrentCharacter
local animTable = smt_animation[c:getName()]
if smt_anim ~= 0 and smt_anim:getId().id==-1 then
smt_anim = 0
c.DestinationObject = smt_destobj
c.ActionCharacter = smt_actionc
--c.Position = {x=smt_dest.x-1,y=smt_dest.y-1}
c.Destination = smt_dest
end
if smt_direction~=newdir then
local turn = false
if newstate == eWalkAnim and smt_state ~= eWalkAnim then
smt_dest = c.Destination
smt_destobj = c.DestinationObject
smt_actionc = c.ActionCharacter
c.Destination = c.Position
turn = true
end
if newstate ~= eWalkAnim and smt_state == eWalkAnim then
turn = true
end
if turn then
smt_anim = startAnimation(getObject("Game.GameCurrentCharacter.CharacterCurrentOutfit.OutfitCharacterAnimations[character_turn]"))
local bestspos = 1
local bestdist = 360
for i = 1, #animTable do
local dist = angleDist(animTable[i], smt_direction)
if dist < bestdist then
bestdist = dist
bestpos = i
end
end
local bestlpos = 1
bestdist = 360
for i = 1, #animTable do
local dist = angleDist(animTable[i], newdir)
if dist < bestdist then
bestdist = dist
bestlpos = i
end
end
print(bestspos.." "..bestlpos)
smt_anim.AnimationFirstFrame = bestspos
smt_anim.AnimationLastFrame = bestlpos
smt_anim.PlayOppositeDirection = bestspos > bestlpos
if bestspos > bestlpos then
smt_anim.AnimationFirstFrame = bestlpos
smt_anim.AnimationLastFrame = bestspos
end
end
end
smt_direction = newdir
smt_state = newstarte
end
registerEventHandler("mainLoop", "smoothTurningMainLoop")