I would be so grateful if someone could help me with this code, it seems really hard for me to figure it out.
As you can see, onCastSpell is getting the player position and its direction. It will then teleport the player to 1 square forward.
I do not want to use if statements in movePlayer function because I think it will not be properly structured, there is no need to check what direction the player is facing every time, it can be checked once during the onCastSpell and, there is no need to get the position each time.
So, to improve the code I wanted to use iteration numbers (0 - in if statements, and 1, 2, 3 in addEvent)
The movePlayer meant to push the player into the right position, depending on which addEvent is currently running, as you can see the number is increasing so it will look like the player is walking.
But that is point where I am stuck and completely clueless if it's actually possible to do it without another set of if statements in movePlayer function.
Any help appreciated
Lua:
local combat = Combat()
combat:setParameter(COMBAT_PARAM_AGGRESSIVE, false)
local function movePlayer(cid, pos, iteration)
local player = Player(cid)
player:teleportTo(pos, false) -- pos[iteration]?
return true
end
function onCastSpell(cid, var)
local player = Player(cid)
local moveToPos = player:getPosition()
local playerDirection = player:getDirection()
if playerDirection == SOUTH then
moveToPos.y = moveToPos.y + 0
elseif playerDirection == NORTH then
moveToPos.y = moveToPos.y - 0
elseif playerDirection == WEST then
moveToPos.x = moveToPos.x - 0
elseif playerDirection == EAST then
moveToPos.x = moveToPos.x + 0
end
addEvent(movePlayer, 1000, player:getId(), moveToPos, 1)
addEvent(movePlayer, 2000, player:getId(), moveToPos, 2)
addEvent(movePlayer, 3000, player:getId(), moveToPos, 3)
return combat:execute(cid, var)
end
As you can see, onCastSpell is getting the player position and its direction. It will then teleport the player to 1 square forward.
I do not want to use if statements in movePlayer function because I think it will not be properly structured, there is no need to check what direction the player is facing every time, it can be checked once during the onCastSpell and, there is no need to get the position each time.
So, to improve the code I wanted to use iteration numbers (0 - in if statements, and 1, 2, 3 in addEvent)
The movePlayer meant to push the player into the right position, depending on which addEvent is currently running, as you can see the number is increasing so it will look like the player is walking.
But that is point where I am stuck and completely clueless if it's actually possible to do it without another set of if statements in movePlayer function.
Any help appreciated