Author Topic: 2 Player  (Read 1552 times)

yankscally

  • Newbie
  • *
  • Posts: 22
    • View Profile
Re: 2 Player
« Reply #15 on: May 02, 2017, 11:28:26 pm »
@Diarandor
Ah! I did not notice that tricky math variable.

@yankscally
No need for the math variable.

Your code worked, straight up. Thank you Zefk! A little bit of time writing that code for you would have taken me days to figure out, but I would have got there eventually. I'm from an music/art background so im no wizard but I do know the basics of code, I really appreciate the help! If you need some sounds for your game, I will happily supply them I'm working on some sound packs that are pretty neat, including a very nice sounding dialog menu, I could show you if you are interested!

Zefk

  • Sr. Member
  • ****
  • Posts: 496
  • Just helping Solarus
    • View Profile
    • Zelzec Business
Re: 2 Player
« Reply #16 on: May 02, 2017, 11:32:06 pm »
You are welcome. I updated that post about camera movements and other info. You will still have to set the direction as I mentioned.
http://forum.solarus-games.org/index.php/topic,962.msg5558.html#msg5558

Code: Lua
  1. local entity = ...
  2. local game = entity:get_game()
  3. local map = entity:get_map()
  4. local hero = map:get_hero()
  5. local sprite
  6. local straight = sol.movement.create("straight")
  7. menu = false
  8. local camera = map:get_camera()
  9. camera:start_tracking(entity)
  10.  
  11.  
  12. ---Create sprite
  13. function entity:on_created()
  14.   sprite = entity:create_sprite("hero/tunic3"):set_animation("stopped")
  15.   entity:set_can_traverse("hero", false)
  16.   entity:get_direction(2)
  17.   straight:set_speed(80)
  18. end
  19.  
  20. --CONTROLS
  21. --UP
  22. function player_up()
  23.  straight:set_angle(math.pi / 2)
  24.  straight:start(entity)
  25. end
  26.  
  27. --DOWN
  28. function player_down()
  29.  straight:set_angle(3 * math.pi / 2)
  30.  straight:start(entity)
  31. end
  32.  
  33. --Left
  34. function player_left()
  35.  straight:set_angle(math.pi)
  36.  straight:start(entity)
  37. end
  38.  
  39. --right
  40. function player_right()
  41.  straight:set_angle(0)
  42.  straight:start(entity)
  43. end
  44.  
  45.  
  46. --Key press function
  47. function sol.main:on_key_pressed(key)
  48.  
  49. --right
  50.   if key == "l" and menu == false then
  51.     player_right()
  52.     entity:get_sprite():set_animation("walking")
  53.   end
  54.  
  55. --left
  56.   if key == "j" and menu == false then
  57.     player_left()
  58.     entity:get_sprite():set_animation("walking")
  59.   end
  60.  
  61. --down
  62.   if key == "k" and menu == false then
  63.     player_down()
  64.     entity:get_sprite():set_animation("walking")
  65.  
  66.   end
  67.  
  68. --up
  69.   if key == "i" and menu == false then
  70.     player_up()
  71.     entity:get_sprite():set_animation("walking")
  72.   end
  73. end
  74.  
  75.  
  76. function sol.main:on_key_released(key)
  77.  
  78.    if key == "l" then
  79.        straight:stop()
  80.        entity:get_sprite():set_animation("stopped")
  81.    end
  82.    
  83.    if key == "j" then
  84.        straight:stop()
  85.        entity:get_sprite():set_animation("stopped")
  86.    end
  87.    
  88.    if key == "k" then
  89.        straight:stop()
  90.        entity:get_sprite():set_animation("stopped")
  91.    end
  92.    
  93.    if key == "i" then
  94.        straight:stop()
  95.        entity:get_sprite():set_animation("stopped")
  96.    end
  97.  
  98. end
  99.  
« Last Edit: May 02, 2017, 11:40:02 pm by Zefk »

Zefk

  • Sr. Member
  • ****
  • Posts: 496
  • Just helping Solarus
    • View Profile
    • Zelzec Business
Re: 2 Player
« Reply #17 on: May 03, 2017, 08:20:47 am »
I made a quick library example for the custom entity and attached it to this post. I made it mostly for the book project, but it can be used to shorten the script above. I included documentation and you will need a markdown reader like haroopad.

If you want to use it, then the script would look like this:



Code: Lua
  1. local entity = ...
  2. local game = entity:get_game()
  3. local map = entity:get_map()
  4. local hero = map:get_hero()
  5. local sprite
  6. menu = false
  7.  
  8.  
  9. local camera = map:get_camera()
  10. camera:start_tracking(entity)
  11.  
  12. ---Create sprite
  13. function entity:on_created()
  14.   sprite = entity:create_sprite("hero/tunic1"):set_animation("stopped")
  15.   entity:set_can_traverse("hero", false)
  16.   entity:speed(80)
  17. end
  18.  
  19. --Key press function
  20. function sol.main:on_key_pressed(key)
  21.  
  22. --right
  23.   if key == "l" and menu == false then
  24.     entity:right()
  25.   end
  26.  
  27. --up
  28.   if key == "i" and menu == false then
  29.     entity:up()
  30.   end
  31.  
  32. --left
  33.   if key == "j" and menu == false then
  34.     entity:left()
  35.   end
  36.  
  37. --down
  38.   if key == "k" and menu == false then
  39.     entity:down()
  40.   end
  41. end
  42.  
  43. function sol.main:on_key_released(key)
  44.    --right
  45.    if key == "l" then
  46.        entity:stop()
  47.    end
  48.  
  49.    --up
  50.    if key == "i" then
  51.        entity:stop()
  52.    end
  53.  
  54.    --left
  55.    if key == "j" then
  56.        entity:stop()
  57.    end
  58.  
  59.    --down
  60.    if key == "k" then
  61.        entity:stop()
  62.    end
  63. end
« Last Edit: May 03, 2017, 08:28:46 am by Zefk »

Diarandor

  • Hero Member
  • *****
  • Posts: 935
  • Cats are cool! (ΦωΦ)
    • View Profile
Re: 2 Player
« Reply #18 on: May 03, 2017, 08:51:34 am »
It is really cool to see that working.  :)
The feature of switching heroes would be very useful too for your project.
“If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you.”

Zefk

  • Sr. Member
  • ****
  • Posts: 496
  • Just helping Solarus
    • View Profile
    • Zelzec Business
Re: 2 Player
« Reply #19 on: May 03, 2017, 12:54:27 pm »
@Diarandor

I am not sure about all the functions I will make. I will probably just go with the flow, but I am planning hero:switch() function. I am planning stats and stat penalties (poisoned).

hero:projectile()
hero:hookshot()
hero:boomerang()
hero:bomb()
hero:switch()
hero:bubble()
hero:damaged()
hero:inflict() --poison, burn, frozen, cursed


For entities:

entity:attack()
entity:projectile()
entity:boomerang()
entity:hookshot()
entity:damaged()
entity:hud()
entity:bomb()
entity:mode() --balanced, bomber, sniper, charge, & melee
entity:protect_hero()
entity:jump()
entity:swim()
entity:wander()
entity:encounter()
entity:inflict() --poison, burn, frozen, cursed
entity:jumper()
entity:follow()
ect
« Last Edit: May 03, 2017, 12:58:44 pm by Zefk »