Author Topic: Résolu : Menu de game-over + inventaire en 1.6  (Read 1461 times)

Lucifer

  • Newbie
  • *
  • Posts: 28
    • View Profile
    • Mon site
Re: Menu de game-over + inventaire en 1.6
« Reply #15 on: January 26, 2018, 09:40:14 pm »
voici mon nouveau code :
Code: Lua
  1. local submenu = require("scripts/menus/pause_submenu")
  2. local inventory_submenu = submenu:new()
  3.  
  4. local item_names = {
  5.   "feather",
  6.   "bombs_counter",
  7.   "bow",
  8.   "lamp",
  9.   "pegasus_shoes",
  10.   "lens_of_truth",
  11.   "bottle_1",
  12.  
  13.   "hookshot",
  14.   "boomerang",
  15.   "fire_rod",
  16.   "pains_au_chocolat_counter",
  17.   "croissants_counter",
  18.   "glove",
  19.   "bottle_2",
  20.  
  21.   "cat_food",
  22.   "mail_counter",
  23.   "perfume_counter",
  24.   "banana_skin_counter",
  25.   "vip_card",
  26.   "flippers",
  27.   "bottle_3",
  28. }
  29.  
  30. function inventory_submenu:on_started()
  31.  
  32.   submenu.on_started(self)
  33.  
  34.   self.cursor_sprite = sol.sprite.create("menus/pause_cursor")
  35.   self.sprites = {}
  36.   self.counters = {}
  37.   self.captions = {}
  38.  
  39.   for k = 1, #item_names do
  40.     if item_names[k] ~= "" then
  41.       -- Get the item, its possession state and amount.
  42.       local item = self.game:get_item(item_names[k])
  43.       local variant = item:get_variant()
  44.  
  45.       if variant > 0 then
  46.         if item:has_amount() then
  47.           -- Show a counter in this case.
  48.           local amount = item:get_amount()
  49.           local maximum = item:get_max_amount()
  50.  
  51.           self.counters[k] = sol.text_surface.create{
  52.             horizontal_alignment = "center",
  53.             vertical_alignment = "top",
  54.             text = item:get_amount(),
  55.             font = (amount == maximum) and "green_digits" or "white_digits",
  56.           }
  57.         end
  58.  
  59.         -- Initialize the sprite and the caption string.
  60.         self.sprites[k] = sol.sprite.create("entities/items")
  61.         self.sprites[k]:set_animation(item_names[k])
  62.         self.sprites[k]:set_direction(variant - 1)
  63.       end
  64.     end
  65.   end
  66.  
  67.   -- Initialize the cursor
  68.   local index = self.game:get_value("pause_inventory_last_item_index") or 0
  69.   local row = math.floor(index / 7)
  70.   local column = index % 7
  71.   self:set_cursor_position(row, column)
  72. end
  73.  
  74. function inventory_submenu:on_finished()
  75.  
  76.   if submenu.on_finished then
  77.     submenu.on_finished(self)
  78.   end
  79.  
  80.   if self:is_assigning_item() then
  81.     self:finish_assigning_item()
  82.   end
  83.  
  84.   local hud = self.game:get_hud()
  85.   if hud ~= nil then
  86.     self.game:get_hud():set_item_icon_opacity(1, 255)
  87.     self.game:get_hud():set_item_icon_opacity(2, 255)
  88.   end
  89. end
  90.  
  91. function inventory_submenu:set_cursor_position(row, column)
  92.  
  93.   self.cursor_row = row
  94.   self.cursor_column = column
  95.  
  96.   local index = row * 7 + column
  97.   self.game:set_value("pause_inventory_last_item_index", index)
  98.  
  99.   -- Update the caption text and the action icon.
  100.   local item_name = item_names[index + 1]
  101.   local item = item_name and item_name ~= "" and self.game:get_item(item_name) or nil
  102.   local variant = item and item:get_variant() or 0
  103.  
  104.   local item_icon_opacity = 128
  105.   if variant > 0 then
  106.     self:set_caption("inventory.caption.item." .. item_name .. "." .. variant)
  107.     self.game:set_custom_command_effect("action", "info")
  108.     if item:is_assignable() then
  109.       item_icon_opacity = 255
  110.     end
  111.   else
  112.     self:set_caption(nil)
  113.     self.game:set_custom_command_effect("action", nil)
  114.   end
  115.   self.game:get_hud():set_item_icon_opacity(1, item_icon_opacity)
  116.   self.game:get_hud():set_item_icon_opacity(2, item_icon_opacity)
  117. end
  118.  
  119. function inventory_submenu:get_selected_index()
  120.  
  121.   return self.cursor_row * 7 + self.cursor_column
  122. end
  123.  
  124. function inventory_submenu:is_item_selected()
  125.  
  126.   local item_name = item_names[self:get_selected_index() + 1]
  127.   return self.game:get_item(item_name):get_variant() > 0
  128. end
  129.  
  130. function inventory_submenu:on_command_pressed(command)
  131.  
  132.   local handled = submenu.on_command_pressed(self, command)
  133.  
  134.   if not handled then
  135.  
  136.     if command == "action" then
  137.       if self.game:get_command_effect("action") == nil
  138.             and self.game:get_custom_command_effect("action") == "info" then
  139.         self:show_info_message()
  140.         handled = true
  141.       end
  142.  
  143.     elseif command == "item_1" then
  144.       if self:is_item_selected() then
  145.         self:assign_item(1)
  146.         handled = true
  147.       end
  148.  
  149.     elseif command == "item_2" then
  150.       if self:is_item_selected() then
  151.         self:assign_item(2)
  152.         handled = true
  153.       end
  154.  
  155.     elseif command == "left" then
  156.       if self.cursor_column == 0 then
  157.         self:previous_submenu()
  158.       else
  159.         sol.audio.play_sound("cursor")
  160.         self:set_cursor_position(self.cursor_row, self.cursor_column - 1)
  161.       end
  162.       handled = true
  163.  
  164.     elseif command == "right" then
  165.       if self.cursor_column == 6 then
  166.         self:next_submenu()
  167.       else
  168.         sol.audio.play_sound("cursor")
  169.         self:set_cursor_position(self.cursor_row, self.cursor_column + 1)
  170.       end
  171.       handled = true
  172.  
  173.     elseif command == "up" then
  174.       sol.audio.play_sound("cursor")
  175.       self:set_cursor_position((self.cursor_row + 3) % 4, self.cursor_column)
  176.       handled = true
  177.  
  178.     elseif command == "down" then
  179.       sol.audio.play_sound("cursor")
  180.       self:set_cursor_position((self.cursor_row + 1) % 4, self.cursor_column)
  181.       handled = true
  182.  
  183.     end
  184.   end
  185.  
  186.   return handled
  187. end
  188.  
  189. function inventory_submenu:on_draw(dst_surface)
  190.  
  191.   self:draw_background(dst_surface)
  192.   self:draw_caption(dst_surface)
  193.  
  194.  -- Draw the cursor.
  195.   self.cursor_sprite:draw(dst_surface, 64 + 32 * self.cursor_column, 77 + 32 * self.cursor_row)
  196.  
  197.   -- Draw each inventory item.
  198.   local y = 82
  199.   local k = 0
  200.  
  201.   for i = 0, 3 do
  202.     local x = 64
  203.  
  204.     for j = 0, 6 do
  205.       k = k + 1
  206.       if item_names[k] ~= nil and item_names[k] ~= "" then
  207.         local item = self.game:get_item(item_names[k])
  208.         if item:get_variant() > 0 then
  209.           -- The player has this item: draw it.
  210.           self.sprites[k]:draw(dst_surface, x, y)
  211.           if self.counters[k] ~= nil then
  212.             self.counters[k]:draw(dst_surface, x + 8, y)
  213.           end
  214.         end
  215.       end
  216.       x = x + 32
  217.     end
  218.     y = y + 32
  219.   end
  220.  
  221.   -- Draw the item being assigned if any.
  222.   if self:is_assigning_item() then
  223.     self.item_assigned_sprite:draw(dst_surface)
  224.   end
  225.  
  226.   self:draw_save_dialog_if_any(dst_surface)
  227. end
  228.  
  229. -- Shows a message describing the item currently selected.
  230. -- The player is supposed to have this item.
  231. function inventory_submenu:show_info_message()
  232.  
  233.   local item_name = item_names[self:get_selected_index() + 1]
  234.   local variant = self.game:get_item(item_name):get_variant()
  235.   local map = self.game:get_map()
  236.  
  237.   -- Position of the dialog (top or bottom).
  238.   if self.cursor_row >= 2 then
  239.     self.game:get_dialog_box():set_position("top")  -- Top of the screen.
  240.   else
  241.     self.game:get_dialog_box():set_position("bottom")  -- Bottom of the screen.
  242.   end
  243.  
  244.   self.game:set_custom_command_effect("action", nil)
  245.   self.game:set_custom_command_effect("attack", nil)
  246.   self.game:start_dialog("_item_description." .. item_name .. "." .. variant, function()
  247.     self.game:set_custom_command_effect("action", "info")
  248.     self.game:set_custom_command_effect("attack", "save")
  249.     self.game:get_dialog_box():set_position("auto")  -- Back to automatic position.
  250.   end)
  251.  
  252. end
  253.  
  254. -- Assigns the selected item to a slot (1 or 2).
  255. -- The operation does not take effect immediately: the item picture is thrown to
  256. -- its destination icon, then the assignment is done.
  257. -- Nothing is done if the item is not assignable.
  258. function inventory_submenu:assign_item(slot)
  259.  
  260.   local index = self:get_selected_index() + 1
  261.   local item_name = item_names[index]
  262.   local item = self.game:get_item(item_name)
  263.  
  264.   -- If this item is not assignable, do nothing.
  265.   if not item:is_assignable() then
  266.     return
  267.   end
  268.  
  269.   -- If another item is being assigned, finish it immediately.
  270.   if self:is_assigning_item() then
  271.     self:finish_assigning_item()
  272.   end
  273.  
  274.   -- Memorize this item.
  275.   self.item_assigned = item
  276.   self.item_assigned_sprite = sol.sprite.create("entities/items")
  277.   self.item_assigned_sprite:set_animation(item_name)
  278.   self.item_assigned_sprite:set_direction(item:get_variant() - 1)
  279.   self.item_assigned_destination = slot
  280.  
  281.   -- Play the sound.
  282.   sol.audio.play_sound("throw")
  283.  
  284.   -- Compute the movement.
  285.   local x1 = 60 + 32 * self.cursor_column
  286.   local y1 = 75 + 32 * self.cursor_row
  287.   local x2 = (slot == 1) and 20 or 72
  288.   local y2 = 46
  289.  
  290.   self.item_assigned_sprite:set_xy(x1, y1)
  291.   local movement = sol.movement.create("target")
  292.   movement:set_target(x2, y2)
  293.   movement:set_speed(500)
  294.   movement:start(self.item_assigned_sprite, function()
  295.     self:finish_assigning_item()
  296.   end)
  297. end
  298.  
  299. -- Returns whether an item is currently being thrown to an icon.
  300. function inventory_submenu:is_assigning_item()
  301.   return self.item_assigned_sprite ~= nil
  302. end
  303.  
  304. -- Stops assigning the item right now.
  305. -- This function is called when we want to assign the item without
  306. -- waiting for its throwing movement to end, for example when the inventory submenu
  307. -- is being closed.
  308. function inventory_submenu:finish_assigning_item()
  309.  
  310.   -- If the item to assign is already assigned to the other icon, switch both items.
  311.   local slot = self.item_assigned_destination
  312.   local current_item = self.game:get_item_assigned(slot)
  313.   local other_item = self.game:get_item_assigned(3 - slot)
  314.  
  315.   if other_item == self.item_assigned then
  316.     self.game:set_item_assigned(3 - slot, current_item)
  317.   end
  318.   self.game:set_item_assigned(slot, self.item_assigned)
  319.  
  320.   self.item_assigned_sprite:stop_movement()
  321.   self.item_assigned_sprite = nil
  322.   self.item_assigned = nil
  323. end
  324.  
  325. return inventory_submenu
  326.  
  327.  

et l'erreur : Error: In on_draw: scripts/menus/pause_inventory.lua:195: attempt to perform arithmetic on field 'cursor_column' (a nil value)

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1146
    • View Profile
Re: Menu de game-over + inventaire en 1.6
« Reply #16 on: January 26, 2018, 09:54:14 pm »
Alors vérifie que cursor_column a bien été initialisé (ligne 94).

Lucifer

  • Newbie
  • *
  • Posts: 28
    • View Profile
    • Mon site
Re: Menu de game-over + inventaire en 1.6
« Reply #17 on: January 27, 2018, 08:23:24 am »
Code: Lua
  1. function inventory_submenu:set_cursor_position(row, column)
  2.  
  3.   self.cursor_row = row
  4.   self.cursor_column = column
mais, il y a la même erreur

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1146
    • View Profile
Re: Menu de game-over + inventaire en 1.6
« Reply #18 on: January 27, 2018, 10:54:02 am »
Et donc cursor_column vaut combien ?
Comme expliqué dans le premier tuto, il faut un minimum de bases de programmation pour pouvoir débugguer. Commence par un tuto Lua.

MetalZelda

  • Hero Member
  • *****
  • Posts: 551
    • View Profile
Re: Menu de game-over + inventaire en 1.6
« Reply #19 on: January 27, 2018, 01:28:28 pm »
De ce que je vois soit un item n'existe pas (et donc ne peut pas être placé) soit il y'a une erreur d'initialisation. Je pencherais plus pour la 1ere hypothèse.

Lucifer

  • Newbie
  • *
  • Posts: 28
    • View Profile
    • Mon site
Re: Menu de game-over + inventaire en 1.6
« Reply #20 on: January 29, 2018, 01:04:23 pm »
J'ai regardé, il s'agit d'une erreur d'initialisation...

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1146
    • View Profile
Re: Menu de game-over + inventaire en 1.6
« Reply #21 on: January 29, 2018, 02:02:37 pm »
D'où ce que suggère : vérifier la valeur de cursor_column.

Lucifer

  • Newbie
  • *
  • Posts: 28
    • View Profile
    • Mon site
Re: Menu de game-over + inventaire en 1.6
« Reply #22 on: January 29, 2018, 09:27:49 pm »
 Est-ce bien ceci ?
Code: Lua
  1.   -- Initialize the cursor
  2.   local index = self.game:get_value("pause_inventory_last_item_index") or 0
  3.   local row = math.floor(index / 7)
  4.   local column = index % 7
  5.   self:set_cursor_position(row, column)
  6. end

Lucifer

  • Newbie
  • *
  • Posts: 28
    • View Profile
    • Mon site
Re: Résolu : Menu de game-over + inventaire en 1.6
« Reply #23 on: March 14, 2018, 05:09:04 pm »
Merci beaucoup, j'ai trouvé en prenant les ressources de alttd.