-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerSkillbar.cs
More file actions
163 lines (147 loc) · 5.85 KB
/
PlayerSkillbar.cs
File metadata and controls
163 lines (147 loc) · 5.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using Mirror;
namespace uMMORPG
{
[Serializable]
public struct SkillbarEntry
{
public string reference;
public KeyCode hotKey;
}
[RequireComponent(typeof(PlayerEquipment))]
[RequireComponent(typeof(PlayerInventory))]
[RequireComponent(typeof(PlayerSkills))]
public class PlayerSkillbar : NetworkBehaviour
{
[Header("Components")]
public PlayerEquipment equipment;
public PlayerInventory inventory;
public PlayerSkills skills;
[Header("Skillbar")]
public SkillbarEntry[] slots =
{
new SkillbarEntry{reference="", hotKey=KeyCode.Alpha1},
new SkillbarEntry{reference="", hotKey=KeyCode.Alpha2},
new SkillbarEntry{reference="", hotKey=KeyCode.Alpha3},
new SkillbarEntry{reference="", hotKey=KeyCode.Alpha4},
new SkillbarEntry{reference="", hotKey=KeyCode.Alpha5},
new SkillbarEntry{reference="", hotKey=KeyCode.Alpha6},
new SkillbarEntry{reference="", hotKey=KeyCode.Alpha7},
new SkillbarEntry{reference="", hotKey=KeyCode.Alpha8},
new SkillbarEntry{reference="", hotKey=KeyCode.Alpha9},
new SkillbarEntry{reference="", hotKey=KeyCode.Alpha0},
};
public override void OnStartLocalPlayer()
{
// load skillbar after player data was loaded
Load();
}
public override void OnStopClient()
{
if (isLocalPlayer)
Save();
}
// skillbar ////////////////////////////////////////////////////////////////
//[Client] <- disabled while UNET OnDestroy isLocalPlayer bug exists
//void Save()
//{
// save skillbar to player prefs (based on player name, so that
// each character can have a different skillbar)
// for (int i = 0; i < slots.Length; ++i)
// PlayerPrefs.SetString(name + "_skillbar_" + i, slots[i].reference);
// force saving playerprefs, otherwise they aren't saved for some reason
// PlayerPrefs.Save();
//}
void Save()
{
// slot 0 is dynamic (weapon skill) and must NOT be saved
for (int i = 1; i < slots.Length; ++i)
PlayerPrefs.SetString(name + "_skillbar_" + i, slots[i].reference);
PlayerPrefs.Save();
}
/*
[Client]
void Load()
{
Debug.Log("loading skillbar for " + name);
List<Skill> learned = skills.skills.Where(skill => skill.level > 0).ToList();
for (int i = 0; i < slots.Length; ++i)
{
// try loading an existing entry
if (PlayerPrefs.HasKey(name + "_skillbar_" + i))
{
string entry = PlayerPrefs.GetString(name + "_skillbar_" + i, "");
// is this a valid item/equipment/learned skill?
// (might be an old character's playerprefs)
// => only allow learned skills (in case it's an old character's
// skill that we also have, but haven't learned yet)
if (skills.HasLearned(entry) ||
inventory.GetItemIndexByName(entry) != -1 ||
equipment.GetItemIndexByName(entry) != -1)
{
slots[i].reference = entry;
}
}
// otherwise fill with default skills for a better first impression
else if (i < learned.Count)
{
slots[i].reference = learned[i].name;
}
}
}*/
[Client]
void Load()
{
List<Skill> learned = skills.skills.Where(skill => skill.level > 0).ToList();
// slot 0 intentionally skipped (weapon skill)
for (int i = 1; i < slots.Length; ++i)
{
if (PlayerPrefs.HasKey(name + "_skillbar_" + i))
{
string entry = PlayerPrefs.GetString(name + "_skillbar_" + i, "");
if (skills.HasLearned(entry) ||
inventory.GetItemIndexByName(entry) != -1 ||
equipment.GetItemIndexByName(entry) != -1)
{
slots[i].reference = entry;
}
}
else if (i - 1 < learned.Count)
{
slots[i].reference = learned[i - 1].name;
}
}
}
// drag & drop /////////////////////////////////////////////////////////////
void OnDragAndDrop_InventorySlot_SkillbarSlot(int[] slotIndices)
{
// slotIndices[0] = slotFrom; slotIndices[1] = slotTo
slots[slotIndices[1]].reference = inventory.slots[slotIndices[0]].item.name; // just save it clientsided
}
void OnDragAndDrop_EquipmentSlot_SkillbarSlot(int[] slotIndices)
{
// slotIndices[0] = slotFrom; slotIndices[1] = slotTo
slots[slotIndices[1]].reference = equipment.slots[slotIndices[0]].item.name; // just save it clientsided
}
void OnDragAndDrop_SkillsSlot_SkillbarSlot(int[] slotIndices)
{
// slotIndices[0] = slotFrom; slotIndices[1] = slotTo
slots[slotIndices[1]].reference = skills.skills[slotIndices[0]].name; // just save it clientsided
}
void OnDragAndDrop_SkillbarSlot_SkillbarSlot(int[] slotIndices)
{
// slotIndices[0] = slotFrom; slotIndices[1] = slotTo
// just swap them clientsided
string temp = slots[slotIndices[0]].reference;
slots[slotIndices[0]].reference = slots[slotIndices[1]].reference;
slots[slotIndices[1]].reference = temp;
}
void OnDragAndClear_SkillbarSlot(int slotIndex)
{
slots[slotIndex].reference = "";
}
}
}