Hello.
I'm studying the generic_soldier.lua code of zelda solarus dx. I have created a new model of enemy and I integrate, step by step, some functions of generic_soldier. I notice the use of 2 variables "going_hero" and "being_pushed" and in order to understand their function I don't use them in my code but they seems to be necessary because there is a behavior that I can't explain.
In my code, I have integrated, for the moment, the "target hero", the "change direction of sprite" and the "parrage with the sword" functions. I have also try an "audacious" : "movement:start(self,self:restart())" at line 83.
my code:
-- Lua script of enemy mon_enemie.
-- This script is executed every time an enemy with this model is created.
-- Feel free to modify the code below.
-- You can add more events and remove the ones you don't need.
-- See the Solarus Lua API documentation for the full specification
-- of types, events and methods:
-- http://www.solarus-games.org/doc/latest
local enemy = ...
local game = enemy:get_game()
local map = enemy:get_map()
local hero = map:get_hero()
-- sprite du corps de l'enemie
local body_sprite
-- sprite de l'épée de l'enemie
local sword_sprite
-- mouvement de l'enemie
local movement
-- Event called when the enemy is initialized.
function enemy:on_created()
-- On crée le sprite du corps de l'enemie en fonction de la race de l'enemie (nommage formalisé)
body_sprite = enemy:create_sprite("enemies/" .. enemy:get_breed())
-- On crée le sprite de l'épée de l'enemie en fonction de la race de l'enemie (nommage formalisé)
sword_sprite = enemy:create_sprite("enemies/" .. enemy:get_breed().."_sword")
-- On parametre les points de vie à l'enemie
enemy:set_life(5)
-- On parametre les dégats en pts de vie qu'inflige l'enemie
enemy:set_damage(1)
-- On parametre que l'enemie est repoussé quand il touché
enemy:set_pushed_back_when_hurt(true)
-- On parametre que le sprite épée est invincible
self:set_invincible_sprite(sword_sprite)
-- On parametre que le sprite épée quand il recoit une attaque de type "sword" déclenche l'evenement enemy:on_custom_attack_received
self:set_attack_consequence_sprite(sword_sprite, "sword", "custom")
end
-- Event called when the enemy should start or restart its movements.
-- This is called for example after the enemy is created or after
-- it was hurt or immobilized.
function enemy:on_restarted()
print("restarted")
-- On crée un mouvement de type "target"
movement = sol.movement.create("target")
-- On param que la cible de ce mouvement est le héro
movement:set_target(hero)
-- On param la vitesse du mouvement enemie
movement:set_speed(10)
-- On affecte le mouvement à l'enemie et on le lance
movement:start(enemy)
end
-- LORSQUE L'ENEMIE CHANGE DE DIRECTION
function enemy:on_movement_changed(movement)
print("change direction")
--if not being_pushed then
-- On recupere la direction du mouvement
local direction4 = movement:get_direction4()
-- On represente la nouvelle direction en changeant la direction du sprite du corps de l'enemie
body_sprite:set_direction(direction4)
-- On represente la nouvelle direction en changeant la direction du sprite de l'épée de l'enemie
sword_sprite:set_direction(direction4)
--end
end
-- LORSQUE L'ENEMIE RECOIT UNE ATTAQUE QUE L'ON VEUT GERER SOIT MEME
function enemy:on_custom_attack_received(attack, sprite)
-- Si l'attaque et de type sword et qu'elle touche l'epée enemie
if attack == "sword" and sprite == sword_sprite then
-- On joue le son de choc d'épées
sol.audio.play_sound("sword_tapping")
--being_pushed = true
local x, y = self:get_position()
local angle = self:get_angle(self:get_map():get_entity("hero")) + math.pi
local movement = sol.movement.create("straight")
movement:set_speed(128)
movement:set_angle(angle)
movement:set_max_distance(32)
movement:set_smooth(true)
movement:start(self,self:restart())
end
end
The strange behavior : When the hero hit the enemy's sword, as expected, he is pushed away and the sprite change direction but after this, he walks but stay motionless. Yet there is a "restarted" in the console that prove the function "on_restarted" is call after the pushing movement but the "target" movement seems not to work. If I hit him again in his body, he's hurt and he moves again toward the hero.
Thanks