Shooting fire

Started by Dragon_noir, May 25, 2015, 12:58:24 AM

Previous topic - Next topic
Is it possible to make an item shoot fire (like the fire wand from alttp)?

Potentially, I'd analyze the bow and change the sprites and change some of it's settings.
This signature was way too long before, but now it's short!
Also, I am Still Alive!
On ad Off I go!

Do you ever get the feeling that the fandom of a product(s) ruin the potential that you could have had to enjoy the product?

This might be possible but it was not tested. You can try map:create_fire() or use a custom entity for more control.
But in both cases, you need to be familiar with the scripting API.

So i was trying to create a fire wand and this is the code i have so far:


local item = ...
item.fire = nil

function item:on_created()

  item:set_savegame_variable("possesion_fire_wand")
  self:set_assignable(true)

end

function item:on_using()

  local movement = sol.movement.create("straight")
  local game = self:get_game()
  local map = self:get_map()
  local hero = item:get_map():get_entity("hero")
  local x, y, layer = hero:get_position()
  local direction = hero:get_direction()

  if direction == 0 then
    x = x + 16
    movement:set_angle(0)
  elseif direction == 1 then
    y = y - 16
    movement:set_angle(math.pi / 2)
  elseif direction == 2 then
    x = x - 16
    movement:set_angle(math.pi)
  elseif direction == 3 then
    y = y + 16
    movement:set_angle(3 * math.pi / 2)
  end

  if item.fire == nil or not self.fire:exists() then

      --sol.audio.play_sound("")
      --sol.audio.play_sound("magic_bar")
      --game:remove_magic()

      -- Create the fire.
      item.fire = map:create_fire{
name = "fire",
x = x,
y = y,
layer = layer,
      }
      self.created = true

  end
  movement:set_speed(120)
  movement:set_max_distance(160)
  self.fire:set_position(x, y, layer)
  movement:start(item.fire)
  item:set_finished()
end


The problem i seem to have, is that the animation of the fire is only 3 frames and disappears too fast.

Check the animations of sprite "entities/fire", and make sure that there is no obstacle that stops the fire too soon.

"Check the animations of sprite "entities/fire""

If i change the animation, it would also change it for other items using it, like the lamp, wouldn't it?

"and make sure that there is no obstacle that stops the fire too soon."

I tried it on an empty map and it only moves 32 pixels.

It just ends  the animation after it travels 32 pixels.

I also tried to loop the animation.

It now goes the distance (160) but the animation doesn't end.

And it messes up the lamp :(

Then you should probably try with a custom entity instead. You will have full control of the sprite, movement and collisions with other entities.

I don't really know how to code custom entities, and don't really understand the documentation :/

i tried this (in the fire wand script, instead of map:create fire()):

item.fire = map:create_custom_entity{
name = "fire",
x = x,
y = y,
layer = layer,
          direction(0)       
          sprite = "entities/fire2"
      }
      self.created = true



but i get an error:

Error: Failed to load script 'items/fire_wand': [string "items/fire_wand.lua"]:60: '}' expected (to close '{' at line 54) near 'sprite'
Error: In items/fire_wand: attempt to call a string value
Error: In on_started: [string "scripts/menus/pause_inventory.lua"]:41: Item 'fire_wand' is not saved
Error: In on_draw: [string "scripts/menus/pause_inventory.lua"]:203: Item 'fire_wand' is not saved

You have syntax errors in your Lua code. Again, you need to learn Lua before trying to write scripts. I cannot write them all for everyone :P
http://www.lua.org/pil/contents.html

May 26, 2015, 03:51:11 PM #10 Last Edit: May 26, 2015, 03:56:41 PM by Dragon_noir
I've already been to the site but i can't learn by just reading about it. I learn by doing.

I know you can't write all the scripts for everyone :p.

But i did what the documentation said.

"direction (number): Direction of the custom entity, between 0 (East) and 3 (South). This direction will be applied to the entity's sprites if possible."

I know the error lies with the direction.

I tried : -direction(0)
             -direction = 0
             -direction = hero:get_direction()

As i said before i've only been trying to code for a coupe of months (March) I mostly used C#. I only know the basics of programming i just fail to use them correctly :(

edit: Nevermind i found the error. (stupid comma :p)

Where can i find examples of custom entities scripts?

I only made a few for now, they are here: https://github.com/christopho/zelda_mercuris_chest/tree/master/data/entities
Maybe there are other examples from users on this forum.

Thanks those were helpfull.

It's kind of working now.

local item = ...
item_fire = nil

function item:on_created()

  item:set_savegame_variable("possesion_fire_wand")
  self:set_assignable(true)

end

function item:on_using()

  local movement = sol.movement.create("straight")
  local game = self:get_game()
  local map = self:get_map()
  local hero = item:get_map():get_entity("hero")
  local x, y, layer = hero:get_position()
  local direction = hero:get_direction()

  --hero:set_animation("ice_rod")

  if direction == 0 then
    x = x + 16
    movement:set_angle(0)
  elseif direction == 1 then
    y = y - 16
    movement:set_angle(math.pi / 2)
  elseif direction == 2 then
    x = x - 16
    movement:set_angle(math.pi)
  elseif direction == 3 then
    y = y + 16
    movement:set_angle(3 * math.pi / 2)
  end

  if item.fire == nil or not item.fire:exists() then

      --sol.audio.play_sound("")
      --sol.audio.play_sound("magic_bar")
      --game:remove_magic()

  item_fire = map:create_custom_entity{
name = "fire",
x = x,
y = y,
layer = layer,
    direction = direction,       
    sprite = "entities/fire2"
    }
      self.created = true

  end

  movement:set_speed(160)
  --movement:set_max_distance(320)
  item_fire:set_position(x, y, layer)
  movement:start(item_fire)

  item_fire:add_collision_test("sprite", function(item_fire, other)

    if other:get_type() == "enemy" then

      other:hurt(8)
  item_fire:remove()
 
    end
  end)
   

 
    sol.timer.start(1000, function()
  item_fire:remove()
    end)

  item:set_finished()
end


just a few bugs:

- Sometimes the fire doesn't disappear.
- The fire continues to go against diagonal walls (cliffs)
- This: movement:set_max_distance(), doesn't seem to work (makes no difference if the line of code is in the file or not.)
- I can't seem to be able to add an animation to the hero when i use the fire wand (even though it's in the assets).

The second problem is because the straight movement is smooth by default (is slides on diagonal walls). Call movement:set_smooth(false) to change that.