Author Topic: Diagonal directions for hero  (Read 275 times)

Diarandor

  • Hero Member
  • *****
  • Posts: 957
  • Cats are cool! (ΦωΦ)
    • View Profile
Diagonal directions for hero
« 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. :)

“If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you.”

santiago_itzcoatl

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Diagonal directions for hero
« Reply #1 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.

santiago_itzcoatl

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Diagonal directions for hero
« Reply #2 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

Code: [Select]
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

santiago_itzcoatl

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Diagonal directions for hero
« Reply #3 on: March 10, 2018, 11:49:21 pm »
The same code but for game commands instead of keys

Code: [Select]
    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