-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRpg.cs
393 lines (324 loc) · 11.7 KB
/
Rpg.cs
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
using System;
using System.Collections.Generic;
using System.Text;
namespace Rpg
{
//Maximum information exposing. A principle favors exposing internal parameters as much as it's not causing disfunctionality
[Serializable]
public class EnergyPool
{
public float energy { get; private set; }
public float capasity { get { return bonus.Calculate(coreCapasity); } }
public Bonus bonus { get; private set; }
public float growRatio = 0.01f;
private float coreCapasity;
public EnergyPool(float cap)
{
coreCapasity = cap;
bonus = new Bonus();
energy = capasity;
}
public void Alter(float change)
{
change = Math.Clamp(energy + change, 0, capasity) - energy;
energy += change;
coreCapasity += Math.Max(0f, -change) * growRatio;
energy =(float) Math.Round(energy, 2);
}
public delegate void EnergyPoolAction(float x);
}
[Serializable]
public class Bonus
{
public float modifier { get; private set; } = 0f;
public float multiplier { get; private set; } = 1f;
Dictionary<string, float> modifiers = new Dictionary<string, float>();
Dictionary<string, float> multipliers = new Dictionary<string, float>();
public void SetFactors(string key, float _modifier=1f, float _multiplier=1f)
{
if (!modifiers.ContainsKey(key))
modifiers.Add(key, 0f);
if (!multipliers.ContainsKey(key))
multipliers.Add(key, 1f);
modifiers[key] = _modifier;
multipliers[key] = _multiplier;
modifier = 0f;
foreach (var x in modifiers)
modifier += x.Value;
multiplier = 1f;
foreach (var x in multipliers)
multiplier *= x.Value;
}
public float Calculate(float baseValue)
{
return (float)Math.Round( baseValue * multiplier + modifier,2);
}
}
[Serializable]
public class Atribute
{
public AtributeAction OnLevelUp;
public float level { get { return bonus.Calculate(baseLevel); } }
public float exp { get; private set; }
public float expCap { get; private set; }
public Bonus bonus { get; private set; }
public float growRatio = 0.0f;
int baseLevel;
public Atribute(int startLevel, float expCapofLvl1 = 100f)
{
baseLevel = startLevel;
expCap = expCapofLvl1 * (float)Math.Pow(1f + growRatio, startLevel - 1);
bonus = new Bonus();
exp = 0;
}
public void ChangeExp(float change)
{
exp += change;
while (exp > expCap)
{
exp -= expCap;
expCap += expCap * growRatio;
baseLevel++;
OnLevelUp?.Invoke();
}
while (exp < 0)
{
exp += expCap;
expCap -= expCap * growRatio;
expCap = Math.Max(expCap, 1f);
baseLevel--;
}
exp =(float) Math.Round(exp, 2);
}
public delegate void AtributeAction();
}
public struct Damage
{
public float magnitude;
public Type type;
public object source;
public Damage(float magnitude, Type type, object source = null)
{
this.magnitude =(float)Math.Round( magnitude,2);
this.type = type;
this.source = source;
}
public enum Type { physical, magical }
}
[Serializable]
public class Recipe
{
public List<Item> requirements;
public Item craft;
public bool Craft(Inventory inventory)
{
if (requirements == null || craft == null)
return false;
if (Craftability(inventory))
{
RemoveRequirements(inventory);
inventory.Add(craft);
return true;
}
return false;
}
//checks names
public bool Craftability(Inventory inventory)
{
List<Item> items = inventory.GetVirtualList();
var ingredients = IngredientsDuplicate();
for (int i = 0; i < ingredients.Count; i++)
{
var ingredient = ingredients[i];
foreach (var item in items)//try to diminish ingredient in inventory
{
if (item.name == ingredient.name)
{
var quantity = Math.Min(ingredient.quantity, item.quantity);
ingredient.quantity -= quantity;
if (ingredient.quantity <= 0)
ingredients.RemoveAt(i--);
}
}
}
return ingredients.Count < 1;
}
void RemoveRequirements(Inventory inventory)
{
List<Item> items = inventory.GetVirtualList();
var ingredients = IngredientsDuplicate();
for (int i = 0; i < ingredients.Count; i++)
{
var ingredient = requirements[i];
for (int j = 0; j < items.Count; j++)
{
var item = items[j];
if (item.name == ingredient.name)
{
var quantity = Math.Min(ingredient.quantity, item.quantity);
item.quantity -= quantity;
ingredient.quantity -= quantity;
if (item.quantity == 0)
inventory.Remove(item);
if (ingredient.quantity == 0)
break;
}
}
}
}
List<Item> IngredientsDuplicate()
{
var ingredients = new List<Item>();
foreach (var item in requirements)
{
ingredients.Add(item.Duplicate());
}
return ingredients;
}
}
[Serializable]
public class Stats
{
public EnergyPool health { get; private set; }
public EnergyPool stamina { get; private set; }
public EnergyPool mana { get; private set; }
public Atribute strenght { get; private set; }
public Atribute agility { get; private set; }
public Atribute wisdom { get; private set; }
public Stats(float hp, float sta, float mna, int str, int agi, int wis)
{
health = new EnergyPool(hp);
stamina = new EnergyPool(sta);
mana = new EnergyPool(mna);
strenght = new Atribute(str);
agility = new Atribute(agi);
wisdom = new Atribute(wis);
}
}
[Serializable]
public class Equipments
{
public enum Placement { head, torso, leg, hands, weapon, shield, neck, finger };
public Dictionary<Placement, Equipment> pieces;
public float physicalDefence { get; private set; }
public float magicalDefence { get; private set; }
Character character;
public Equipments(Character character)
{
this.character = character;
pieces = new Dictionary<Placement, Equipment>();
CalculateDefences();
}
public Item EquipAndGetDiscarded(Equipment piece)
{
Equipment itemTobeReturned = null;
if (pieces.ContainsKey(piece.placement))
{
itemTobeReturned = pieces[piece.placement];
itemTobeReturned?.OnDiscard(character);
}
pieces[piece.placement] = piece;
piece.OnEquip(character);
CalculateDefences();
return itemTobeReturned;
}
public float FilterDamage(Damage damage)
{
float defence = 0;
var eqps = pieces.Values;
foreach (Equipment equipment in eqps)
{
defence += equipment.DefencePower(damage);
}
defence += character.stats.agility.level;
float reducedDamage = RelativeDamageReduction(damage.magnitude, defence);
return reducedDamage;
}
static float RelativeDamageReduction(float damage, float defence)
{
//damage is quarter if damage is equal to defence
if (damage > 0f)
{
float DefenceFactor = 1f + Math.Max(0, defence) / damage;
damage /= (float)Math.Pow(DefenceFactor, 2);
return Math.Max(0f, damage);
}
return 0;
}
void CalculateDefences()
{
var eqps = pieces.Values;
physicalDefence = 0f;
magicalDefence = 0f;
foreach (Equipment equipment in eqps)
{
physicalDefence += equipment.DefencePower(new Damage(1f, Damage.Type.physical));
magicalDefence += equipment.DefencePower(new Damage(1f, Damage.Type.magical));
}
physicalDefence += character.stats.agility.level;
magicalDefence += character.stats.agility.level;
}
[Serializable]
public class Equipment : Item
{
public float strFactor = 1f, agiFactor = 1f, wisFactor = 1f;
public float strBonus, agiBonus, wisBonus;
public List<Defence> defences { get; private set; }
public Placement placement { get; private set; }
public Equipment(List<Defence> defs, Placement placement)
{
defences = defs;
this.placement = placement;
}
public void OnEquip(Character character)
{
//attribute bonuses here
character.stats.strenght.bonus.SetFactors(placement.ToString(), strBonus, strFactor);
character.stats.agility.bonus.SetFactors(placement.ToString(), agiBonus, agiFactor);
character.stats.wisdom.bonus.SetFactors(placement.ToString(), wisBonus, wisFactor);
//check compatibility
}
public void OnDiscard(Character character)
{
//attribute bonuses here
character.stats.strenght.bonus.SetFactors(placement.ToString(), 0, 1f);
character.stats.agility.bonus.SetFactors(placement.ToString(), 0, 1f);
character.stats.wisdom.bonus.SetFactors(placement.ToString(), 0, 1f);
//check compatibility
}
public float DefencePower(Damage damage)
{
foreach (Defence d in defences)
{
if (d.type == damage.type)
return d.power;
}
return 0f;
}
[Serializable]
public struct Defence
{
public Damage.Type type;
public float power;
}
}
}
[Serializable]
public class Weapon : Equipments.Equipment
{
public float balance { get; private set; }//well balanced weapons give agility exp
public Weapon(float balance):base(new List<Defence>() { },Equipments.Placement.weapon)
{
this.balance = Math.Clamp(balance, 0f, 1f);
}
public void SetStats(float strF,float strB,float agiF,float agiB,float wisF,float wisB)
{
strFactor = strF;
strBonus = strB;
agiFactor = agiF;
agiBonus = agiB;
wisFactor = wisF;
wisBonus = wisB;
}
}
}