-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
WeaponAttributes.java
454 lines (393 loc) · 14 KB
/
WeaponAttributes.java
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
package net.bettercombat.api;
import org.jetbrains.annotations.Nullable;
import java.util.Objects;
/**
* Represents how a weapon behaves when player performs attack with it.
*/
public final class WeaponAttributes {
/**
* The maximal range of any attacks made with this weapon.
*/
private final double attack_range;
/**
* The pose animation to play when idling.
* Applied when weapon is in the main hand.
* Value must be an identifier, formula: "namespace:resource".
* Suggested to be only used for two-handed weapons.
*
* Example values:
* "bettercombat:sword-pose"
* "my-mod-id:my-sword-pose"
*/
@Nullable
private final String pose;
/**
* The pose animation to play when idling.
* Applied when weapon is in the off hand.
* Value must be an identifier, formula: "namespace:resource".
* Suggested to be only used for two-handed weapons.
*
* Example values:
* "bettercombat:sword-pose"
* "my-mod-id:my-sword-pose"
*/
@Nullable
private final String off_hand_pose;
/**
* Determines whether the weapon counts as two-handed or one-handed weapon.
* When the player's selected item is two-handed weapon, the off-hand slot is completely disabled.
* When the player's selected item is one-handed weapon, another one-handed weapon can be placed into
*/
private final Boolean two_handed;
/**
* Specifies the category (aka family) type of the weapon.
* This can be any value. Prefer using lowercase values.
*
* Example values:
* "cutlass"
* "sickle"
*
* Used for conditional combos.
*/
@Nullable
private final String category;
/**
* Specifies the sequence of attacks following each other, when the user is attacking continuously.
* (When last attack of the sequence is reached, it restarts)
* With a sequence of different attacks, you can create combos.
* Check out the member wise documentation of `Attack` (in this file), to see how they can be different.
*
* When using attribute inheritance, the inherited sequence of attacks can be reduced or extended.
* Example reducing (inherited attributes have a sequence of 3 attack):
* "attacks": [ {}, {} ]
* Example of extending (inherited attributes have a sequence of 2 attack):
* "attacks": [ {}, {}, { ... my new fully parsable attack object ... } ]
* The properties of inherited attack objects can be overridden.
*/
private final Attack[] attacks;
public WeaponAttributes(
double attack_range,
@Nullable String pose,
@Nullable String off_hand_pose,
Boolean isTwoHanded,
String category,
Attack[] attacks) {
this.attack_range = attack_range;
this.pose = pose;
this.off_hand_pose = off_hand_pose;
this.two_handed = isTwoHanded;
this.category = category;
this.attacks = attacks;
}
/**
* Represents a single weapon swing.
*/
public static final class Attack {
/**
* Conditions those need to be fulfilled for the attack to be performed,
* otherwise the attack is skipped.
* Conditions are in a logic AND (&&) relation.
*
* For no conditions use `null` or empty array.
*/
private Condition[] conditions;
/**
* Determines the shape of the attack hitbox.
* (This shape is scaled by the `attack_range` property from the embedding object.)
* For accepted values check out `HitBoxShape` (in this file).
*/
private HitBoxShape hitbox;
/**
* Applies damage multiplier to a single attack in the sequence.
* (So different attacks in a combo can do more or less damage compared to each other.)
* Example values:
* for +30% damage, use the value `1.3`
* for -30% damage, use the value `0.7`
*/
private double damage_multiplier = 1;
/**
* Determines the angle (measured in degrees) of the attack's hitbox, centered to the player's look vector.
* Targeted entities outside of this 3D slice are not hit.
* If set to `0`, no angle check is done.
* Example values:
* for an attack with an angle of 90 degrees, use the value `90`
* (targeted entities where position vector compared to player's look vector is bigger than 45, will not be hit)
*/
private double angle = 0;
/**
* Determines the amount of time, after the attack is performed during the attack animation.
* The actual amount of time is relative to the cooldown of the weapon.
* Example values:
* to perform an attack at 60% of the attack cooldown, use the value `0.6`
* (in case of a sword with 1.6 speed (12 ticks attack cooldown), the attack will be performed
* after 7.2 (rounded to 7) ticks of upswing)
*
* Try to align the tipping point of the animation and the time at which the attack is performed
* as close as possible.
* Formula to calculate attack cooldown in ticks: (1 / (4 - ATTACK_SPEED)) * 20
*/
private double upswing = 0;
/**
* The attack animation to play.
* Value must be an identifier, formula: "namespace:resource".
* Example values:
* "bettercombat:sword-slash"
* "my-mod-id:my-sword-swing"
*/
private String animation = null;
/**
* The sound to play upon executing the attack.
* Check out the member wise documentation of `Sound` (in this file).
*/
private Sound swing_sound = null;
/**
* Not working at the moment.
*/
private Sound impact_sound = null;
/**
* This empty initializer is needed for GSON, to support parsing over default values
*/
public Attack() { }
public Attack(
Condition[] conditions,
HitBoxShape hitbox,
double damage_multiplier,
double angle,
double upswing,
String animation,
Sound swing_sound,
Sound impact_sound
) {
this.conditions = conditions;
this.hitbox = hitbox;
this.damage_multiplier = damage_multiplier;
this.angle = angle;
this.upswing = upswing;
this.animation = animation;
this.swing_sound = swing_sound;
this.impact_sound = impact_sound;
}
@Nullable
public Condition[] conditions() {
return conditions;
}
public HitBoxShape hitbox() {
return hitbox;
}
public double damageMultiplier() {
return damage_multiplier;
}
public double angle() {
return angle;
}
public double upswing() {
return upswing;
}
public String animation() {
return animation;
}
public Sound swingSound() {
return swing_sound;
}
public Sound impactSound() {
return impact_sound;
}
@Override
public boolean equals(Object obj) {
if (obj == this) return true;
if (obj == null || obj.getClass() != this.getClass()) return false;
var that = (Attack) obj;
return Objects.equals(this.hitbox, that.hitbox) &&
Double.doubleToLongBits(this.damage_multiplier) == Double.doubleToLongBits(that.damage_multiplier) &&
Double.doubleToLongBits(this.angle) == Double.doubleToLongBits(that.angle) &&
Double.doubleToLongBits(this.upswing) == Double.doubleToLongBits(that.upswing) &&
Objects.equals(this.animation, that.animation) &&
Objects.equals(this.swing_sound, that.swing_sound) &&
Objects.equals(this.impact_sound, that.impact_sound);
}
@Override
public int hashCode() {
return Objects.hash(hitbox, damage_multiplier, angle, upswing, animation, swing_sound, impact_sound);
}
@Override
public String toString() {
return "Attack[" +
"hitbox=" + hitbox + ", " +
"damage_multiplier=" + damage_multiplier + ", " +
"angle=" + angle + ", " +
"upswing=" + upswing + ", " +
"animation=" + animation + ", " +
"swing_sound=" + swing_sound + ", " +
"impact_sound=" + impact_sound + ']';
}
}
public enum HitBoxShape {
FORWARD_BOX,
VERTICAL_PLANE,
HORIZONTAL_PLANE
}
public enum Condition {
/**
* Fulfilled if the player is not dual wielding weapons
*/
NOT_DUAL_WIELDING,
/**
* Fulfilled if the player is dual wielding any weapons
*/
DUAL_WIELDING_ANY,
/**
* Fulfilled if the player is dual wielding items with matching ids
*/
DUAL_WIELDING_SAME,
/**
* Fulfilled if the player is dual wielding items with matching categories
* (Category of an item is specified at `WeaponAttributes.category`)
*/
DUAL_WIELDING_SAME_CATEGORY,
/**
* Fulfilled if the player has not item at all in the off-hand
*/
NO_OFFHAND_ITEM,
/**
* Fulfilled if the player has a shield in the off-hand
*/
OFF_HAND_SHIELD,
/**
* Fulfilled for attacks performed with main-hand only
*/
MAIN_HAND_ONLY,
/**
* Fulfilled for attacks performed with off-hand only
*/
OFF_HAND_ONLY,
/**
* Fulfilled if the player is riding some entity
*/
MOUNTED,
/**
* Fulfilled if the player is not riding any entity
*/
NOT_MOUNTED
}
/**
* Represents a sound to be played with arguments.
*/
public static final class Sound {
/**
* The id of the sound.
* Must be specified with a non-null and non-empty value!
* If using your own sounds, make sure you register in your mod initializer.
*
* Value must be an identifier, formula: "namespace:resource".
* Example values:
* "bettercombat:sword-swing"
* "my-mod-id:my-sword-sound"
*/
private String id = null;
/**
* Volume of the sound
* Has default value, optional to specify.
*/
private float volume = 1;
/**
* Pitch of the sound
* Has default value, optional to specify.
*/
private float pitch = 1;
/**
* This empty initializer is needed for GSON, to support parsing over default values
*/
public Sound() { }
public Sound(String id) {
this.id = id;
}
/**
* Pitch randomness of the sound.
* Has default value, optional to specify.
* Example values:
* for additional pitch within a range of +/- 10%, use the value `0.1`
*/
private float randomness = 0.1F;
public String id() {
return id;
}
public float volume() {
return volume;
}
public float pitch() {
return pitch;
}
public float randomness() {
return randomness;
}
@Override
public boolean equals(Object obj) {
if (obj == this) return true;
if (obj == null || obj.getClass() != this.getClass()) return false;
var that = (Sound) obj;
return Objects.equals(this.id, that.id) &&
Float.floatToIntBits(this.volume) == Float.floatToIntBits(that.volume) &&
Float.floatToIntBits(this.pitch) == Float.floatToIntBits(that.pitch) &&
Float.floatToIntBits(this.randomness) == Float.floatToIntBits(that.randomness);
}
@Override
public int hashCode() {
return Objects.hash(id, volume, pitch, randomness);
}
@Override
public String toString() {
return "SoundV2[" +
"id=" + id + ", " +
"volume=" + volume + ", " +
"pitch=" + pitch + ", " +
"randomness=" + randomness + ']';
}
}
// Helpers
public double attackRange() {
return attack_range;
}
@Nullable
public String pose() {
return pose;
}
@Nullable
public String offHandPose() {
return off_hand_pose;
}
@Nullable
public String category() {
return category;
}
public boolean isTwoHanded() {
return two_handed != null ? two_handed.booleanValue() : false;
}
public Boolean two_handed() {
return two_handed;
}
public Attack[] attacks() {
return attacks;
}
@Override
public boolean equals(Object obj) {
if (obj == this) return true;
if (obj == null || obj.getClass() != this.getClass()) return false;
var that = (WeaponAttributes) obj;
return Double.doubleToLongBits(this.attack_range) == Double.doubleToLongBits(that.attack_range) &&
Objects.equals(this.pose, that.pose) &&
Objects.equals(this.two_handed, that.two_handed) &&
Objects.equals(this.attacks, that.attacks);
}
@Override
public int hashCode() {
return Objects.hash(attack_range, two_handed, attacks);
}
@Override
public String toString() {
return "WeaponAttributes[" +
"attack_range=" + attack_range + ", " +
"pose=" + pose + ", " +
"isTwoHanded=" + two_handed + ", " +
"attacks=" + attacks + ']';
}
}