Solarus-Games English Forum

Solarus => Development => Topic started by: Diarandor on March 02, 2018, 10:38:27 AM

Title: Diagonal directions for hero
Post by: Diarandor on March 02, 2018, 10:38:27 AM
Some devs may be interested in this. I think that it is possible (not tested yet) and easy to code directly from Lua scripts the feature of allowing animations for the movement of the hero in 8 directions (that is, including diagonal animations). The idea is as follows:

1) Use the event "hero:on_movement_changed(movement)" to get the movement, and calculate its direction from its angle.
2) Use a modification of my "direction fix" script to include all directions.

This feature could be used with all animations/actions/weapons of the hero. This includes the animations for walking/stopped, swimming, sword attacks, etc. Our hero Eldran does not have art for diagonal animations yet (so we cannot use this feature), but that may change someday. :)

Title: Re: Diagonal directions for hero
Post by: santiago_itzcoatl on March 03, 2018, 06:52:54 AM
Nice topic!

I started looking for that just today, after drawing a nice dummie sprite with 8 directions (for several days).
I'll look forward for any advice or tip, while I'll try to come with an approach by myself.
Title: Re: Diagonal directions for hero
Post by: santiago_itzcoatl on March 10, 2018, 07:31:21 PM
 :D

I came out with a simple solution, one that at least works pretty fine for me.
Still, I have not tried it with other animations, just with normal walking/stopped animations but I guess it should work.

It uses 2 sprites, one with the normal directions, and another one with the additional 4 diagonal directions,
then I switch the sprites and assigned the directions as needed.
I had trouble because there was a feedback from the key events, some simple logic worked as a fix.

There should be a more elegant solution, but as I said it works nicely for me right now.

This goes inside the hero.lua file


local function initialize_hero_features(game)

    local hero = game:get_hero()
    hero:set_tunic_sprite_id("her/her-normal")
   
    function hero:on_position_changed(x, y, layer)
       
        -- Set diagonal movements
       
        if (sol.input.is_key_pressed("right") and not sol.input.is_key_pressed("up") and not sol.input.is_key_pressed("down")) then
            hero:set_tunic_sprite_id("her/her-normal")
        end
       
        if (sol.input.is_key_pressed("left") and not sol.input.is_key_pressed("up") and not sol.input.is_key_pressed("down")) then
            hero:set_tunic_sprite_id("her/her-normal")
        end
       
        if (sol.input.is_key_pressed("up") and not sol.input.is_key_pressed("right") and not sol.input.is_key_pressed("left")) then
            hero:set_tunic_sprite_id("her/her-normal")
        end
       
        if (sol.input.is_key_pressed("down") and not sol.input.is_key_pressed("right") and not sol.input.is_key_pressed("left")) then
            hero:set_tunic_sprite_id("her/her-normal")
        end
       
        if (sol.input.is_key_pressed("left") and sol.input.is_key_pressed("up") ) then
            hero:set_tunic_sprite_id("her/her-diagonal")
            hero:set_direction(1)
        end
       
        if (sol.input.is_key_pressed("left") and sol.input.is_key_pressed("down") ) then
            hero:set_tunic_sprite_id("her/her-diagonal")
            hero:set_direction(2)
        end
       
        if (sol.input.is_key_pressed("right") and sol.input.is_key_pressed("up") ) then
            hero:set_tunic_sprite_id("her/her-diagonal")
            hero:set_direction(0)
        end
       
        if (sol.input.is_key_pressed("right") and sol.input.is_key_pressed("down") ) then
            hero:set_tunic_sprite_id("her/her-diagonal")
            hero:set_direction(3)
        end
       
    end
   
end
Title: Re: Diagonal directions for hero
Post by: santiago_itzcoatl on March 10, 2018, 11:49:21 PM
The same code but for game commands instead of keys


    function hero:on_position_changed(x, y, layer)
       
        -- Set diagonal movements for key
       
        if (game:is_command_pressed("right") and not game:is_command_pressed("up") and not game:is_command_pressed("down")) then
            hero:set_tunic_sprite_id("her/her-normal")
        end
       
        if (game:is_command_pressed("left") and not game:is_command_pressed("up") and not game:is_command_pressed("down")) then
            hero:set_tunic_sprite_id("her/her-normal")
        end
       
        if (game:is_command_pressed("up") and not game:is_command_pressed("right") and not game:is_command_pressed("left")) then
            hero:set_tunic_sprite_id("her/her-normal")
        end
       
        if (game:is_command_pressed("down") and not game:is_command_pressed("right") and not game:is_command_pressed("left")) then
            hero:set_tunic_sprite_id("her/her-normal")
        end
       
        if (game:is_command_pressed("left") and game:is_command_pressed("up") ) then
            hero:set_tunic_sprite_id("her/her-diagonal")
            hero:set_direction(1)
        end
       
        if (game:is_command_pressed("left") and game:is_command_pressed("down") ) then
            hero:set_tunic_sprite_id("her/her-diagonal")
            hero:set_direction(2)
        end
       
        if (game:is_command_pressed("right") and game:is_command_pressed("up") ) then
            hero:set_tunic_sprite_id("her/her-diagonal")
            hero:set_direction(0)
        end
       
        if (game:is_command_pressed("right") and game:is_command_pressed("down") ) then
            hero:set_tunic_sprite_id("her/her-diagonal")
            hero:set_direction(3)
        end
       
    end