Author Topic: custom entity hero movement  (Read 274 times)

Porksteak

  • Newbie
  • *
  • Posts: 8
    • View Profile
custom entity hero movement
« on: March 22, 2017, 03:46:21 am »
Hi, I was trying to make a custom ice entity that can be used for sliding ice puzzles, but after the hero slides on one and stops he is able to walk for a bit before he is forced into the slide again. Is there a certain collision test that can be done or something?

Code: Lua
  1. local entity = ...
  2. local map = entity:get_map()
  3. local pushing = false
  4. local block_on_switch = false
  5. local lava_crust, ice_patch
  6. local hero = entity:get_map():get_entity("hero")
  7.  
  8. -- Ice block: special block made of ice that can fill an
  9. -- ice pit, turn lava solid, and freeze water.
  10.  
  11. function entity:on_created()
  12.   self:set_size(16, 16)
  13.   self:snap_to_grid()
  14.   self:set_modified_ground("traversable")
  15.   self:set_traversable_by("hero", true)
  16.   self:create_sprite("entities/icepatch")
  17.  
  18.   self:add_collision_test("facing", function(self, other)
  19.     if other:get_type() == "hero" and not pushing then
  20.       pushing = true
  21.       local m = sol.movement.create("path")
  22.       m:set_ignore_obstacles(false)
  23.       m:set_snap_to_grid(true)
  24.       m:set_speed(120)
  25.  
  26.       hero:freeze()        
  27.  
  28.       local sx, sy, sl = self:get_position()
  29.       if other:get_direction() == 0 then
  30.         if map:get_ground(sx+8,sy,sl) ~= "wall" and map:get_ground(sx+16,sy,sl) ~= "wall" then m:set_path({0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}) end
  31.       elseif other:get_direction() == 1 then
  32.         if map:get_ground(sx,sy+8,sl) ~= "wall" and map:get_ground(sx,sy+16,sl) ~= "wall" then m:set_path({2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}) end
  33.       elseif other:get_direction() == 2 then
  34.         if map:get_ground(sx-8,sy,sl) ~= "wall" and map:get_ground(sx-16,sy,sl) ~= "wall" then m:set_path({4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4}) end
  35.       elseif other:get_direction() == 3 then
  36.         if map:get_ground(sx,sy-8,sl) ~= "wall" and map:get_ground(sx,sy-16,sl) ~= "wall" then m:set_path({6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6}) end
  37.       end
  38.       m:start(other, function() pushing = false hero:unfreeze() end)
  39.     end
  40.   end)
  41.  
  42.   self:add_collision_test("overlapping", function(self, other)
  43.     if other:get_type() == "switch" then
  44.       block_on_switch = true
  45.       other:set_activated(true)
  46.       if other:on_activated() ~= nil and not other.active then
  47.         other:on_activated()
  48.         other.active = true
  49.       end
  50.       sol.timer.start(map, 1000, function()
  51.         if block_on_switch then
  52.           return true
  53.         else
  54.           block_on_switch = false
  55.           other:set_activated(false)
  56.           if other:on_inactivated() ~= nil and other.active then
  57.             other:on_inactivated()
  58.             other.active = false
  59.           end
  60.         end
  61.       end)
  62.     elseif other:get_type() == "hole" then
  63.       sol.audio.play_sound("hero_falls")
  64.       self:remove()
  65.     elseif other:get_type() == "fire" then
  66.       sol.audio.play_sound("ice_melt")
  67.       self:remove()
  68.     elseif other:get_type() == "explosion" then
  69.       sol.audio.play_sound("ice_melt")
  70.       self:remove()
  71.     else
  72.       block_on_switch = false
  73.     end
  74.   end)
  75.  
  76.   local sx, sy, sl = self:get_position()
  77.   self:on_position_changed(sx, sy, sl)
  78. end
  79.  
  80. function entity:on_position_changed(x, y, layer)
  81.   for e in map:get_entities("") do
  82.     if e:get_type() == "dynamic_tile" then
  83.       if self:overlaps(e) then --if block overlaps dynamic tile
  84.         if map:get_ground(e:get_position()) == "lava" then
  85.           self:remove()
  86.           if map:get_hero():get_direction() == 0 then
  87.             lava_crust = map:create_custom_entity({ x=x+8, y=y-16, layer=layer, width=32, height=32, direction=0 })
  88.           elseif map:get_hero():get_direction() == 1 then
  89.             lava_crust = map:create_custom_entity({ x=x-16, y=y-48, layer=layer, width=32, height=32, direction=0 })
  90.           elseif map:get_hero():get_direction() == 2 then
  91.             lava_crust = map:create_custom_entity({ x=x-40, y=y-16, layer=layer, width=32, height=32, direction=0 })
  92.           elseif map:get_hero():get_direction() == 3 then
  93.             lava_crust = map:create_custom_entity({ x=x-16, y=y, layer=layer, width=32, height=32, direction=0 })
  94.           end
  95.           lava_crust = map:create_custom_entity({ x = x, y = y, layer = layer, width = 32, height = 32, direction = 0 })
  96.           lava_crust:snap_to_grid()
  97.           sol.audio.play_sound("freeze")
  98.           lava_crust:create_sprite("entities/lava")
  99.           lava_crust:set_modified_ground("traversable")
  100.           lava_crust:set_traversable_by("hero", true)
  101.           lava_crust:set_traversable_by("enemy", true)
  102.           lava_crust:set_traversable_by("block", true)
  103.           sol.timer.start(map, 15000, function() lava_crust:remove() end)
  104.         elseif map:get_ground(e:get_position()) == "deep_water" then
  105.           self:remove()
  106.           if map:get_hero():get_direction() == 0 then
  107.             ice_patch = map:create_custom_entity({ x=x+8, y=y-16, layer=layer, width=32, height=32, direction=0 })
  108.           elseif map:get_hero():get_direction() == 1 then
  109.             ice_patch = map:create_custom_entity({ x=x-16, y=y-48, layer=layer, width=32, height=32, direction=0 })
  110.           elseif map:get_hero():get_direction() == 2 then
  111.             ice_patch = map:create_custom_entity({ x=x-40, y=y-16, layer=layer, width=32, height=32, direction=0 })
  112.           elseif map:get_hero():get_direction() == 3 then
  113.             ice_patch = map:create_custom_entity({ x=x-16, y=y, layer=layer, width=32, height=32, direction=0 })
  114.           end
  115.           sol.audio.play_sound("freeze")
  116.           ice_patch:create_sprite("entities/ice")
  117.           ice_patch:set_modified_ground("ice")
  118.           ice_patch:set_traversable_by("hero", true)
  119.           ice_patch:set_traversable_by("enemy", true)
  120.           ice_patch:set_traversable_by("block", true)
  121.           sol.timer.start(map, 15000, function() ice_patch:remove() end)
  122.         end
  123.       end
  124.     elseif e:get_type() == "wall" or e:get_type() == "hole" then
  125.       hero:unfreeze()
  126.       e:stop_movement()
  127.     end
  128.   end
  129. end
  130.  
  131. function entity:on_removed()
  132.   self:get_sprite():set_animation("destroy")
  133. end

Diarandor

  • Hero Member
  • *****
  • Posts: 781
  • Cats are cool! (ΦωΦ)
    • View Profile
Re: custom entity hero movement
« Reply #1 on: March 22, 2017, 07:38:32 am »
Someone already made a script for that (maybe Nate-Devv or Christopho? I can't remember), but I have not read the code, so I don't know how it works. Link here:
https://github.com/Nate-Devv/libsolarus-mudora/tree/f06406cc5fe45f17bafdf4a44b3f60462b215ebf