Author Topic: Shooting fire  (Read 3889 times)

Dragon_noir

  • Newbie
  • *
  • Posts: 26
    • View Profile
Shooting fire
« on: May 25, 2015, 12:58:24 am »
Is it possible to make an item shoot fire (like the fire wand from alttp)?

YoshiMario2000

  • Jr. Member
  • **
  • Posts: 85
    • View Profile
Re: Shooting fire
« Reply #1 on: May 25, 2015, 02:44:38 am »
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?

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1182
    • View Profile
Re: Shooting fire
« Reply #2 on: May 25, 2015, 08:51:59 am »
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.

Dragon_noir

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Shooting fire
« Reply #3 on: May 26, 2015, 07:09:20 am »
So i was trying to create a fire wand and this is the code i have so far:

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

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1182
    • View Profile
Re: Shooting fire
« Reply #4 on: May 26, 2015, 09:44:25 am »
Check the animations of sprite "entities/fire", and make sure that there is no obstacle that stops the fire too soon.

Dragon_noir

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Shooting fire
« Reply #5 on: May 26, 2015, 02:31:42 pm »
"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.

Dragon_noir

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Shooting fire
« Reply #6 on: May 26, 2015, 02:34:57 pm »
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 :(

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1182
    • View Profile
Re: Shooting fire
« Reply #7 on: May 26, 2015, 02:56:50 pm »
Then you should probably try with a custom entity instead. You will have full control of the sprite, movement and collisions with other entities.

Dragon_noir

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Shooting fire
« Reply #8 on: May 26, 2015, 03:21:19 pm »
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()):
 
Code: [Select]
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

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1182
    • View Profile
Re: Shooting fire
« Reply #9 on: May 26, 2015, 03:29:48 pm »
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

Dragon_noir

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Shooting fire
« Reply #10 on: May 26, 2015, 03:51:11 pm »
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)
« Last Edit: May 26, 2015, 03:56:41 pm by Dragon_noir »

Dragon_noir

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Shooting fire
« Reply #11 on: May 26, 2015, 04:17:47 pm »
Where can i find examples of custom entities scripts?

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1182
    • View Profile
Re: Shooting fire
« Reply #12 on: May 26, 2015, 05:03:25 pm »
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.

Dragon_noir

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Shooting fire
« Reply #13 on: May 26, 2015, 06:32:43 pm »
Thanks those were helpfull.

It's kind of working now.

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

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1182
    • View Profile
Re: Shooting fire
« Reply #14 on: May 26, 2015, 06:40:51 pm »
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.