2 Player

Started by yankscally, May 02, 2017, 02:03:26 AM

Previous topic - Next topic
Quote from: Zefk on May 02, 2017, 11:03:05 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!

May 02, 2017, 11:32:06 PM #16 Last Edit: May 02, 2017, 11:40:02 PM by Zefk
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) Select
local entity = ...
local game = entity:get_game()
local map = entity:get_map()
local hero = map:get_hero()
local sprite
local straight = sol.movement.create("straight")
menu = false
local camera = map:get_camera()
camera:start_tracking(entity)


---Create sprite
function entity:on_created()
  sprite = entity:create_sprite("hero/tunic3"):set_animation("stopped")
  entity:set_can_traverse("hero", false)
  entity:get_direction(2)
  straight:set_speed(80)
end

--CONTROLS
--UP
function player_up()
straight:set_angle(math.pi / 2)
straight:start(entity)
end

--DOWN
function player_down()
straight:set_angle(3 * math.pi / 2)
straight:start(entity)
end

--Left
function player_left()
straight:set_angle(math.pi)
straight:start(entity)
end

--right
function player_right()
straight:set_angle(0)
straight:start(entity)
end


--Key press function
function sol.main:on_key_pressed(key)

--right
  if key == "l" and menu == false then
    player_right()
    entity:get_sprite():set_animation("walking")
  end

--left
  if key == "j" and menu == false then
    player_left()
    entity:get_sprite():set_animation("walking")
  end

--down
  if key == "k" and menu == false then
    player_down()
    entity:get_sprite():set_animation("walking")

  end

--up
  if key == "i" and menu == false then
    player_up()
    entity:get_sprite():set_animation("walking")
  end
end


function sol.main:on_key_released(key)

   if key == "l" then
       straight:stop()
       entity:get_sprite():set_animation("stopped")
   end
   
   if key == "j" then
       straight:stop()
       entity:get_sprite():set_animation("stopped")
   end
   
   if key == "k" then
       straight:stop()
       entity:get_sprite():set_animation("stopped")
   end
   
   if key == "i" then
       straight:stop()
       entity:get_sprite():set_animation("stopped")
   end

end


May 03, 2017, 08:20:47 AM #17 Last Edit: May 03, 2017, 08:28:46 AM by Zefk
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) Select
local entity = ...
local game = entity:get_game()
local map = entity:get_map()
local hero = map:get_hero()
local sprite
menu = false


local camera = map:get_camera()
camera:start_tracking(entity)

---Create sprite
function entity:on_created()
  sprite = entity:create_sprite("hero/tunic1"):set_animation("stopped")
  entity:set_can_traverse("hero", false)
  entity:speed(80)
end

--Key press function
function sol.main:on_key_pressed(key)

--right
  if key == "l" and menu == false then
    entity:right()
  end

--up
  if key == "i" and menu == false then
    entity:up()
  end

--left
  if key == "j" and menu == false then
    entity:left()
  end

--down
  if key == "k" and menu == false then
    entity:down()
  end
end

function sol.main:on_key_released(key)
   --right
   if key == "l" then
       entity:stop()
   end

   --up
   if key == "i" then
       entity:stop()
   end

   --left
   if key == "j" then
       entity:stop()
   end

   --down
   if key == "k" then
       entity:stop()
   end
end

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."