I'm making 2d Mobile game using JavaScript.
I have method that controls every character (Dumb AI) on battleground. It using recursion to call another action. I want to add debuff system and had little bit difficulty adding taunt (where close enemies are forced to attack specified enemy) and confuse (where enemy is attacking his ally). I want this to be instant. What I managed to do is make this happen after enemy finish his current action, but this behaviour is bad. I need somehow a way to break instantly current call, but I don't have idea how to do it as from my knowledge there isn't anything method that by reference can break JavaScript method, so I could start new one. I had some idea to use memory reference and kill it in memory, but that's where I was having some fun in my teenage years with cheat engine on some games, but this can raise other problem where the game would need access to memory and this would be bad for user and me.
What I can do achieve instant change of action?
Below my method that controls every character:
export const characterControl = async (characterData, closestEnemy, enemyCharacters, allies, characterControlId) => {
// my new attep to introduce a new variable called `characterControlId` where new method call would have new characterControlId and old one would be forced to stop.
if(characterData.battleData.debuff && characterData.battleData.characterControlId !== characterControlId || isPaused || gamePause) {
return;
}
if (checkIfBattleEnded(allies, enemyCharacters)) {
/// code that stop all methods and game loop.
}
if (isBattleEnded) {
return;
}
if (checkIfCharacterDied(closestEnemy)) {
// code that find new enemy and start new action to move to him
}
let direction;
switch (characterData.battleData.action) {
case "attack":
// this handle logic to switch character Spine2d animation to attack and deal dmg to enemy
return attackEnemy(characterData, closestEnemy, enemyCharacters, allies);
case "move-ally":
// testing new approach where this gonna switch Spine2d animation to run character towards his closest ally.
return moveCharacter(characterData, closestEnemy, enemyCharacters, allies, direction, true);
case "attack-ally":
// new approach where this logic gonna switch Spine2d animation to attack and inflict dmg to his ally.
return attackEnemy(characterData, closestEnemy, enemyCharacters, allies, true);
// case "taunted":
// return attackEnemy(characterData, closestEnemy, enemyCharacters, allies);
case "move":
// default approach to switch character spine2d animation to run towards closest enemy
return moveCharacter(characterData, closestEnemy, enemyCharacters, allies, direction);
case "skill":
// when mana is above 100% it gonna switch character animation to his skill.
return useSkillNew(characterData, closestEnemy, enemyCharacters, allies);
default:
// when action of character is undefined it gonna look for possibilities what he should do next. If he should run or can has enemy in range and can attack him.
return setCharacterAction(characterData, closestEnemy, enemyCharacters, allies);
}
}
asyncfunction here. How exactly do you use that function? Do you just call it once and then it takes care of any time delays itself? Or do you call it every frame? Are you using promises? \$\endgroup\$