si quelqu'un aurais du temps pour m'expliquer, ça serais vraiment gentil
je me permet de vous expliquer en detail le système d'inventaire...
Description :
Donc j'ai mon inventaire complet avec une barre toujours activée.
Lorsque je prend un objet, celui ce place directement dans le slot 1 puis dans le 2 si le 1 est pris etc....
Ensuite, lorsque j'appuie sur "I", l'inventaire principale s'ouvre :
Ici, on peut voir que j'ai une épé. Lorsque je double click dessu, elle va sur le slot Weapon (slot ou seul les armes peuvent aller)
Voila j'ai aussi le dernier slot qui permet de supprimer l'item de l'inventaire.
Donc maintenant les scripts :
Donc j'ai un script InventoryScreen qui contient tout mes slots
ensuite, chaque slot à le script ItemSlot
De meme pour chaque slot "Spécial" (weapon, helmet,...)
Les scripts :
WeaponController :
Code : Tout sélectionner
using UnityEngine;
public class WeaponController : MonoBehaviour
{
public GameObject weaponHand;
private ItemWeapon weapon;
private Animator animator;
public GameObject weaponModel;
public GameScreen screen;
public InventoryScreen inventScreen;
public InventoryController invControl;
public bool StaffWhiteIsEquip = false;
public bool StaffGreenIsEquip = false;
public int healthStaffBlue = 5;
void Start ()
{
}
void Awake()
{
animator = GetComponent<Animator>();
}
void Update()
{
if (GameController.Instance.GameState != GameStates.Game) return;
animator.SetBool("IsAttack", InputController.Weapon);
if (weapon == null)
{
StaffWhiteIsEquip = false;
StaffGreenIsEquip = false;
}
else
{
// StaffWhiteIsEquip = true;
// StaffGreenIsEquip = true;
}
if (healthStaffBlue <= 0)
DeleteWeapon ();
}
public void DeleteWeapon (int slotId) // En test
{
// Item weaponDelete = UIController.Instance.InventoryScreen.weaponSlot.ItemInSlot;
//
// int index = ItemsController.Instance.Items.FindIndex(x => x != null && x.id == weaponDelete.id);
//
// if (index >= 0)
// {
// //InventoryController.Instance.SetWeapon (null);
// //ItemsController.Instance.Items [index] = null;
//
// }
InventoryController.items[slotId] = null;
InventoryController.Instance.SetWeapon (null);
}
// by the animation event
public void OnAttack()
{
if (weapon == null) return;
PlayerController player = PlayerController.Instance;
if (weapon.itemType == Item.ItemTypes.Staff)
{
ItemWeaponStaff staff = (ItemWeaponStaff)weapon;
if (player.Mana >= staff.mana && healthStaffBlue > 0) // Ajout de && healthStaffBlue > 0
{
Instantiate(staff.bullet, player.mainCamera.transform.position, player.mainCamera.transform.rotation);
player.Mana -= staff.mana;
//Pour supprimer staffBlue (Arme) quand elle n'a plus de vie
if (weapon.model.name == "StaffBlue")
{
healthStaffBlue--;
}
}
else
{
UIController.Instance.ShowError("Not enought mana");
}
}
else
{
RaycastHit hit;
if (Physics.Raycast(player.mainCamera.transform.position, player.mainCamera.transform.TransformDirection(Vector3.forward), out hit, 4))
{
IHitable hitable = hit.collider.gameObject.GetComponent(typeof(IHitable)) as IHitable;
if (hitable != null)
{
DamageConfig damageWithStreanght = ((ItemWeaponMelee)weapon).damage.Copy();
damageWithStreanght.minPhysical += (int)(damageWithStreanght.minPhysical * 0.05f * PlayerController.Instance.experience.ActualStrenght);
damageWithStreanght.maxPhysical += (int)(damageWithStreanght.maxPhysical * 0.05f * PlayerController.Instance.experience.ActualStrenght);
hitable.Hit(new HitInfo(damageWithStreanght, hit.point, true, HitInfo.HitSources.Player));
}
}
}
}
public void OnSetWeapon(ItemWeapon weapon)
{
if (weapon.model.name == "StaffBlue" && healthStaffBlue <= 0)
weapon = null;
this.weapon = weapon;
animator.SetBool("IsAttack", false);
if (weaponModel != null) Destroy(weaponModel);
if (weapon == null) // ajout de || healthStaffBlue ==0
{
animator.SetInteger("WeaponType", -1);
}
else
{
weaponModel = (GameObject)Instantiate(weapon.model);
weaponModel.transform.parent = weaponHand.transform;
weaponModel.transform.localPosition = Vector3.zero;
weaponModel.transform.localRotation = Quaternion.identity;
//weaponModel.transform.localScale = Vector3.one;
animator.SetInteger("WeaponType", ItemWeapon.GetAnimationId(weapon));
}
}
}
Code : Tout sélectionner
using System.Collections.Generic;
using UnityEngine;
public class InventoryController : Singleton<InventoryController>
{
public event System.Action<ItemWeapon> onSetWeapon;
public event System.Action<ItemWearable> onSetWearable;
public static List<Item> items;
public int slotsCount = 23;
public List<Item> Items { get { return items; } }
public ItemWeapon Weapon { get; private set; }
public ItemWearable Armor { get; private set; }
public ItemWearable Helmet { get; private set; }
public ItemWearable Amulet { get; private set; }
public ItemWearable Ring { get; private set; }
WeaponController weaponControl;
void Awake()
{
items = new List<Item>(new Item[slotsCount]);
}
void Update()
{
if (GameController.Instance.GameState == GameStates.Game)
{
if (InputController.Item1) UseItem(0);
if (InputController.Item2) UseItem(1);
if (InputController.Item3) UseItem(2);
if (InputController.Item4) UseItem(3);
if (InputController.Item5) UseItem(4);
if (InputController.Item6) UseItem(5);
}
}
public bool AddItem(Item item)
{
int index = items.FindIndex(i => i == null);
if (index == -1) return false;
items[index] = item;
return true;
}
public void SetWeapon(ItemWeapon weapon)
{
if (Weapon != null) Weapon.Deactivate();
Weapon = weapon;
if (onSetWeapon != null) onSetWeapon(weapon);
}
public void SetArmor(ItemWearable item)
{
if (item != null && item.wearableType != ItemWearable.WearableTypes.Armor) return;
if (Armor != null) Armor.Deactivate();
Armor = item;
if (onSetWearable != null) onSetWearable(item);
}
public void SetHelmet(ItemWearable item)
{
if (item != null && item.wearableType != ItemWearable.WearableTypes.Helmet) return;
if (Helmet != null) Helmet.Deactivate();
Helmet = item;
if (onSetWearable != null) onSetWearable(item);
}
public void SetAmulet(ItemWearable item)
{
if (item != null && item.wearableType != ItemWearable.WearableTypes.Amulet) return;
if (Amulet != null) Amulet.Deactivate();
Amulet = item;
if (onSetWearable != null) onSetWearable(item);
}
public void SetRing(ItemWearable item)
{
if (item != null && item.wearableType != ItemWearable.WearableTypes.Ring) return;
if (Ring != null) Ring.Deactivate();
Ring = item;
if (onSetWearable != null) onSetWearable(item);
}
public void SetWearable(ItemWearable item)
{
if (item.wearableType == ItemWearable.WearableTypes.Armor) SetArmor(item);
if (item.wearableType == ItemWearable.WearableTypes.Helmet) SetHelmet(item);
if (item.wearableType == ItemWearable.WearableTypes.Amulet) SetAmulet(item);
if (item.wearableType == ItemWearable.WearableTypes.Ring) SetRing(item);
}
public void UseItem(int slotId)
{
if (slotId < 0 || slotId > items.Count - 1) return;
Item item = items[slotId];
if (item == null || item.itemType == Item.ItemTypes.Quest) return;
items[slotId] = null;
item.Activate();
}
public bool FindAndUseItem(string itemId)
{
int index = Items.FindIndex(i => i != null && i.id == itemId);
if (index >= 0)
{
Items[index] = null;
return true;
}
return false;
}
public void Move(int firstSlot, int secondSlot)
{
if (firstSlot >= Items.Count || secondSlot >= Items.Count || firstSlot < 0 || secondSlot < 0) return;
Item item = items[firstSlot];
items[firstSlot] = items[secondSlot];
items[secondSlot] = item;
}
public void Drop(int slotId)
{
if (slotId >= Items.Count || slotId < 0) return;
Item item = items[slotId];
PlayerController.Instance.DropItem(item.id);
items[slotId] = null;
}
}
Code : Tout sélectionner
using UnityEngine;
using UnityEngine.UI;
public class InventoryScreen : Form
{
public ItemSlot[] slots;
public ItemSlot weaponSlot;
public ItemSlot armorSlot;
public ItemSlot helmetSlot;
public ItemSlot amuletSlot;
public ItemSlot ringSlot;
public ItemSlot dropSlot;
public Text levelText;
public Text pointsText;
public Slider strenghtSlider;
public Slider staminaSlider;
public Slider mindSlider;
public Slider willpowerSlider;
public Text strenghtText;
public Text staminaText;
public Text mindText;
public Text willpowerText;
public Button addStrenghtButton;
public Button addStaminaButton;
public Button addMindButton;
public Button addWillpowerButton;
public Text protectionPhysicalText;
public Text protectionFireText;
public Text protectionIceText;
public Text protectionElectroText;
public Text hintText;
public Image dragIcon;
public Color defaultTextColor = Color.grey;
public Color highlightTextColor = Color.green;
private ItemSlot hoveredSlot;
WeaponController weaponControl;
Item item;
public GameObject StaffWhite;
public Transform PlayerHand;
void Start()
{
strenghtSlider.maxValue = ExperienceController.maxSkillLevel;
staminaSlider.maxValue = ExperienceController.maxSkillLevel;
mindSlider.maxValue = ExperienceController.maxSkillLevel;
willpowerSlider.maxValue = ExperienceController.maxSkillLevel;
addStrenghtButton.onClick.AddListener(OnAddStrenghtClick);
addStaminaButton.onClick.AddListener(OnAddStaminaClick);
addMindButton.onClick.AddListener(OnAddMindClick);
addWillpowerButton.onClick.AddListener(OnAddWillpowerClick);
hintText.text = "";
weaponSlot.onDblClick += OnSlotDblClick;
armorSlot.onDblClick += OnSlotDblClick;
helmetSlot.onDblClick += OnSlotDblClick;
amuletSlot.onDblClick += OnSlotDblClick;
ringSlot.onDblClick += OnSlotDblClick;
weaponSlot.onHover += OnSlotHover;
armorSlot.onHover += OnSlotHover;
helmetSlot.onHover += OnSlotHover;
amuletSlot.onHover += OnSlotHover;
ringSlot.onHover += OnSlotHover;
dropSlot.onHover += OnSlotHover;
foreach (ItemSlot itemSlot in slots)
{
itemSlot.onDblClick += OnSlotDblClick;
itemSlot.onHover += OnSlotHover;
itemSlot.onDrag += OnSlotDrag;
}
foreach (ItemSlot itemSlot in UIController.Instance.gameScreen.slots)
{
itemSlot.onDblClick += OnSlotDblClick;
itemSlot.onHover += OnSlotHover;
itemSlot.onDrag += OnSlotDrag;
}
}
void Update()
{
if (IsShown == false) return;
PlayerController player = PlayerController.Instance;
levelText.text = "Level: " + player.experience.Level;
pointsText.text = "Points: " + player.experience.Points;
Vector2 localPoint;
RectTransformUtility.ScreenPointToLocalPointInRectangle(rootPanel.GetComponent<RectTransform>(), Input.mousePosition, null, out localPoint);
if (dragIcon.gameObject.activeSelf == true)
{
dragIcon.transform.parent.GetComponent<RectTransform>().anchoredPosition = localPoint;
}
strenghtSlider.value = player.experience.Strenght;
staminaSlider.value = player.experience.Stamina;
mindSlider.value = player.experience.Mind;
willpowerSlider.value = player.experience.Willpower;
strenghtText.text = player.experience.ActualStrenght.ToString();
staminaText.text = player.experience.ActualStamina.ToString();
mindText.text = player.experience.ActualMind.ToString();
willpowerText.text = player.experience.ActualWillpower.ToString();
strenghtText.color = player.experience.AdditionalStrenght > 0 ? highlightTextColor : defaultTextColor;
staminaText.color = player.experience.AdditionalStamina > 0 ? highlightTextColor : defaultTextColor;
mindText.color = player.experience.AdditionalMind > 0 ? highlightTextColor : defaultTextColor;
willpowerText.color = player.experience.AdditionalWillpower > 0 ? highlightTextColor : defaultTextColor;
addStrenghtButton.interactable = player.experience.Points > 0 && player.experience.Strenght < ExperienceController.maxSkillLevel;
addStaminaButton.interactable = player.experience.Points > 0 && player.experience.Stamina < ExperienceController.maxSkillLevel;
addMindButton.interactable = player.experience.Points > 0 && player.experience.Mind < ExperienceController.maxSkillLevel;
addWillpowerButton.interactable = player.experience.Points > 0 && player.experience.Willpower < ExperienceController.maxSkillLevel;
protectionPhysicalText.text = "Physical: " + (Mathf.RoundToInt(player.defence.physical * 100));
protectionFireText.text = "Fire: " + (Mathf.RoundToInt(player.defence.fire * 100));
protectionIceText.text = "Ice: " + (Mathf.RoundToInt(player.defence.ice * 100));
protectionElectroText.text = "Electro: " + (Mathf.RoundToInt(player.defence.electro * 100));
weaponSlot.SetItem(player.inventory.Weapon);
armorSlot.SetItem(player.inventory.Armor);
helmetSlot.SetItem(player.inventory.Helmet);
amuletSlot.SetItem(player.inventory.Amulet);
ringSlot.SetItem(player.inventory.Ring);
foreach (ItemSlot slot in slots)
{
slot.SetItem(InventoryController.Instance.Items[slot.id]);
}
}
public override void Show()
{
base.Show();
hintText.text = "";
dragIcon.gameObject.SetActive(false);
}
private void OnAddStrenghtClick()
{
PlayerController.Instance.experience.AddStrenght(1, false);
}
private void OnAddStaminaClick()
{
PlayerController.Instance.experience.AddStamina(1, false);
}
private void OnAddMindClick()
{
PlayerController.Instance.experience.AddMind(1, false);
}
private void OnAddWillpowerClick()
{
PlayerController.Instance.experience.AddWillpower(1, false);
}
private void OnSlotDblClick(ItemSlot slot)
{
if (slot == weaponSlot) { InventoryController.Instance.SetWeapon(null); return; }
if (slot == armorSlot) { InventoryController.Instance.SetArmor(null); return; }
if (slot == helmetSlot) { InventoryController.Instance.SetHelmet(null); return; }
if (slot == amuletSlot) { InventoryController.Instance.SetAmulet(null); return; }
if (slot == ringSlot) { InventoryController.Instance.SetRing(null); return; }
InventoryController.Instance.UseItem(slot.id);
}
private void OnSlotHover(ItemSlot slot, bool state)
{
if (state == true)
{
hoveredSlot = slot;
if (slot.ItemInSlot != null) hintText.text = slot.ItemInSlot.name + slot.ItemInSlot.description;
}
else
{
hoveredSlot = null;
hintText.text = "";
}
}
private void OnSlotDrag(ItemSlot slot, bool state)
{
if (state == true)
{
if (slot.ItemInSlot != null)
{
dragIcon.gameObject.SetActive(true);
dragIcon.sprite = slot.ItemInSlot.icon;
//Debug.Log ("Tu déplace un objet dans l'inventaire!"); // Okay
}
}
else
{
if (slot.ItemInSlot != null && hoveredSlot != null)
{
if (hoveredSlot.slotType == ItemSlot.SlotTypes.Drop )
{
InventoryController.Instance.Drop(slot.id);
Debug.Log (slot.ItemInSlot.name); // Name of the object thrown (If I slip the icon of the object in a specific slot, I get the name of this object)
//Instantiate objects in scene
Instantiate (Resources.Load ("Objets/" + slot.ItemInSlot.name) as GameObject, PlayerHand.position, Quaternion.identity);
}
if (hoveredSlot.slotType == ItemSlot.SlotTypes.Backpack)
{
InventoryController.Instance.Move(slot.id, hoveredSlot.id);
//Debug.Log ("Objet posé dans une case de l'inventaire"); //fonctionne
}
}
dragIcon.gameObject.SetActive(false);
}
}
}
ItemSlot :
Code : Tout sélectionner
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class ItemSlot : MonoBehaviour, IPointerClickHandler, IPointerEnterHandler, IPointerExitHandler, IBeginDragHandler, IEndDragHandler, IDragHandler
{
public enum SlotTypes
{
Backpack = 0,
Equipment = 1,
Drop = 2
}
public System.Action<ItemSlot> onDblClick;
public System.Action<ItemSlot, bool> onHover;
public System.Action<ItemSlot, bool> onDrag;
public Image icon;
public Text numText;
public int id = 0;
public bool showHotkeyLabel = false;
public SlotTypes slotType = SlotTypes.Backpack;
public Item ItemInSlot { get; private set; }
public WeaponController weaponControll;
void Start()
{
if (showHotkeyLabel == true)
{
numText.gameObject.SetActive(true);
numText.text = (id + 1).ToString();
}
else
{
numText.gameObject.SetActive(false);
}
}
// void Update ()
// {
// if (weaponControll.healthStaffBlue == 0)
// icon.gameObject.SetActive(false);
//
// }
public void SetItem(Item item)
{
ItemInSlot = item;
if (item == null || item.id == "")
{
icon.gameObject.SetActive(false);
}
else
{
icon.gameObject.SetActive(true);
icon.sprite = item.icon;
}
}
public void OnPointerClick(PointerEventData eventData)
{
if (eventData.clickCount == 2 && onDblClick != null)
{
onDblClick(this);
}
}
public void OnPointerEnter(PointerEventData eventData)
{
if (onHover != null)
{
onHover(this, true);
}
}
public void OnPointerExit(PointerEventData eventData)
{
if (onHover != null )
{
onHover(this, false);
}
}
public void OnBeginDrag(PointerEventData eventData)
{
if (onDrag != null)
{
onDrag(this, true);
}
}
public void OnEndDrag(PointerEventData eventData )
{
if (onDrag != null )
{
onDrag(this, false);
}
}
public void OnDrag(PointerEventData eventData)
{
}
}