-
Notifications
You must be signed in to change notification settings - Fork 755
/
ProjectileWeapon.java
441 lines (366 loc) · 15.6 KB
/
ProjectileWeapon.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
package tconstruct.library.weaponry;
import net.minecraft.init.Items;
import net.minecraft.util.StatCollector;
import tconstruct.client.TProxyClient;
import tconstruct.library.TConstructRegistry;
import tconstruct.weaponry.client.CrosshairType;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.EnumAction;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.IIcon;
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.player.ArrowLooseEvent;
import tconstruct.library.tools.AbilityHelper;
import tconstruct.library.tools.ToolCore;
import tconstruct.library.util.TextureHelper;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Weapons that utilize ammo that uses the ammo system to shoot projectiles.
* Bows,...
*/
public abstract class ProjectileWeapon extends ToolCore implements IAccuracy, IWindup {
public ProjectileWeapon(int baseDamage, String name) {
super(baseDamage);
this.setCreativeTab(TConstructRegistry.weaponryTab);
}
@SideOnly(Side.CLIENT)
public CrosshairType getCrosshairType() {
return CrosshairType.SQUARE;
}
@Override
public boolean zoomOnWindup(ItemStack itemStack) {
return false;
}
@Override
public float getZoom(ItemStack itemStack) {
return 1.0f;
}
/**
* Searches the player for ammo to use
* @param player the player
* @return the itemstack found to be ammo
*/
public abstract ItemStack searchForAmmo(EntityPlayer player, ItemStack weapon);
/**
* Creates the projectile to be fired.
* @param ammo The ammo used
* @param world world.
* @param player player.
* @param speed the speed calculated for the projectile
* @param accuracy the accuracy calculated for the projectile
* @return A banana.
*/
protected abstract Entity createProjectile(ItemStack ammo, World world, EntityPlayer player, float speed, float accuracy, float windup);
/* Accuracy */
public abstract float minAccuracy(ItemStack itemStack);
public abstract float maxAccuracy(ItemStack itemStack);
public float getAccuracy(ItemStack itemStack, EntityPlayer player)
{
return getAccuracy(itemStack, getMaxItemUseDuration(itemStack) - player.getItemInUseCount());
}
public float getAccuracy(ItemStack itemStack, int time)
{
float dif = minAccuracy(itemStack) - maxAccuracy(itemStack);
return minAccuracy(itemStack) - dif * getWindupProgress(itemStack, time);
}
/* Windup */
public int getWindupTime(ItemStack itemStack) {
if(!itemStack.hasTagCompound())
return 1;
NBTTagCompound toolTag = itemStack.getTagCompound().getCompoundTag("InfiTool");
return toolTag.getInteger("DrawSpeed");
}
public float getWindupProgress(ItemStack itemStack, EntityPlayer player)
{
// what are you doing!
if(player.inventory.getCurrentItem() != itemStack)
return 0f;
// are we using it?
if(player.getItemInUse() == null)
return 0f;
return getWindupProgress(itemStack, getMaxItemUseDuration(itemStack) - player.getItemInUseCount());
}
public float getWindupProgress(ItemStack itemStack, int timeInUse)
{
float time = (float) timeInUse;
float windup = getWindupTime(itemStack);
if(time > windup)
return 1.0f;
return time/windup;
}
public float getProjectileSpeed(ItemStack itemStack)
{
NBTTagCompound toolTag = itemStack.getTagCompound().getCompoundTag("InfiTool");
return toolTag.getFloat("FlightSpeed");
}
/* Bow usage */
@Override
public EnumAction getItemUseAction(ItemStack par1ItemStack) {
return EnumAction.none;
}
@Override
public int getMaxItemUseDuration(ItemStack par1ItemStack) {
return 72000;
}
@Override
public ItemStack onItemRightClick (ItemStack stack, World world, EntityPlayer player)
{
if(getWindupTime(stack) == 0.0f)
return stack;
// broken tool?
if(stack.hasTagCompound() && stack.getTagCompound().hasKey(getBaseTagName()) && stack.getTagCompound().getCompoundTag(getBaseTagName()).getBoolean("Broken"))
return stack;
// only if ammo is present
if(searchForAmmo(player, stack) != null)
player.setItemInUse(stack, this.getMaxItemUseDuration(stack));
return stack;
}
@Override
public void onPlayerStoppedUsing (ItemStack weapon, World world, EntityPlayer player, int useRemaining)
{
int time = this.getMaxItemUseDuration(weapon) - useRemaining;
// we abuse the arrowLooseEvent for all projectiles
ArrowLooseEvent event = new ArrowLooseEvent(player, weapon, time);
MinecraftForge.EVENT_BUS.post(event);
if (event.isCanceled())
{
return;
}
time = event.charge;
// find ammo
ItemStack ammo = searchForAmmo(player, weapon);
// no ammo found. :(
if(ammo == null)
return;
NBTTagCompound toolTag = weapon.getTagCompound().getCompoundTag("InfiTool");
float projectileSpeed = getProjectileSpeed(weapon);
float windup = getWindupProgress(weapon, time);
float accuracy = getAccuracy(weapon, time);
// needs a minimum windup
if(windup < this.getMinWindupProgress(weapon))
return;
// take windup time into account
projectileSpeed *= windup;
Entity projectile = createProjectile(ammo, world, player, projectileSpeed, accuracy, windup);
int reinforced = 0;
if (toolTag.hasKey("Unbreaking"))
reinforced = toolTag.getInteger("Unbreaking");
if (random.nextInt(10) < 10 - reinforced)
AbilityHelper.damageTool(weapon, 1, player, false);
playFiringSound(world, player, weapon, ammo, projectileSpeed, accuracy);
// use up ammo
if(!player.capabilities.isCreativeMode) {
if (ammo.getItem() instanceof IAmmo)
((IAmmo) ammo.getItem()).consumeAmmo(1, ammo);
else
player.inventory.consumeInventoryItem(ammo.getItem());
}
// FIREEEEEEE
if (!world.isRemote)
world.spawnEntityInWorld(projectile);
}
public abstract void playFiringSound(World world, EntityPlayer player, ItemStack weapon, ItemStack ammo, float speed, float accuracy);
public HashMap<Integer, IIcon[]> animationHeadIcons = new HashMap<Integer, IIcon[]>();
public HashMap<Integer, IIcon[]> animationHandleIcons = new HashMap<Integer, IIcon[]>();
public HashMap<Integer, IIcon[]> animationAccessoryIcons = new HashMap<Integer, IIcon[]>();
public HashMap<Integer, IIcon[]> animationExtraIcons = new HashMap<Integer, IIcon[]>();
public HashMap<Integer, IIcon[]> animationEffectIcons = new HashMap<Integer, IIcon[]>();
/**
* return true if the current renderpass should use animations.
* 0 == handle
* 1 == head
* 2 == accessory
* 3 == extra
*/
protected boolean animateLayer(int renderPass)
{
return false;
}
@Override
public IIcon getIcon(ItemStack stack, int renderPass, EntityPlayer player, ItemStack usingItem, int useRemaining) {
NBTTagCompound tags = stack.getTagCompound().getCompoundTag("InfiTool");
if(tags == null || renderPass > 10)
return super.getIcon(stack, renderPass, player, usingItem, useRemaining);
// are we drawing an effect?
if(renderPass >= getPartAmount()) {
// is the effect animated?
String effect = "Effect" + (1 + renderPass - getPartAmount());
if(tags.hasKey(effect)) {
int index = tags.getInteger(effect);
if(animationEffectIcons.get(index) != null)
return getCorrectAnimationIcon(animationEffectIcons, index, getWindupProgress(usingItem, getMaxItemUseDuration(usingItem) - useRemaining));
else
// non-animated
return effectIcons.get(index);
}
return super.getIcon(stack, renderPass, player, usingItem, useRemaining);
}
// animate?
if(!animateLayer(renderPass))
return super.getIcon(stack, renderPass, player, usingItem, useRemaining);
if(usingItem == null || stack != usingItem || !stack.hasTagCompound())
return super.getIcon(stack, renderPass, player, usingItem, useRemaining);
float progress = getWindupProgress(usingItem, getMaxItemUseDuration(usingItem) - useRemaining);
// get the correct icon
switch (renderPass)
{
case 0: return getCorrectAnimationIcon(animationHandleIcons, tags.getInteger("RenderHandle"), progress);
case 1: return getCorrectAnimationIcon(animationHeadIcons, tags.getInteger("RenderHead"), progress);
case 2: return getCorrectAnimationIcon(animationAccessoryIcons, tags.getInteger("RenderAccessory"), progress);
case 3: return getCorrectAnimationIcon(animationExtraIcons, tags.getInteger("RenderExtra"), progress);
}
return emptyIcon;
}
protected IIcon getCorrectAnimationIcon(Map<Integer, IIcon[]> icons, int id, float progress)
{
// animation count, standard texture as reference
float count = icons.get(-1).length - 1;
int step = Math.round(progress * count);
if(icons.containsKey(id))
return icons.get(id)[step];
// default icon
return icons.get(-1)[step];
}
@Override
@SideOnly(Side.CLIENT)
// exactly the same as the ToolCore one but with broken check for Handle instead of Head
public IIcon getIcon (ItemStack stack, int renderPass)
{
NBTTagCompound tags = stack.getTagCompound();
if (tags != null)
{
tags = stack.getTagCompound().getCompoundTag("InfiTool");
if (renderPass < getPartAmount())
{
// Handle
if (renderPass == 0) {
if (tags.getBoolean("Broken"))
return getCorrectIcon(brokenIcons, tags.getInteger("RenderHandle"));
else
return getCorrectIcon(handleIcons, tags.getInteger("RenderHandle"));
}
// Head
else if (renderPass == 1)
{
return getCorrectIcon(headIcons, tags.getInteger("RenderHead"));
}
// Accessory
else if (renderPass == 2)
return getCorrectIcon(accessoryIcons, tags.getInteger("RenderAccessory"));
// Extra
else if (renderPass == 3)
return getCorrectIcon(extraIcons, tags.getInteger("RenderExtra"));
}
// Effects
else if (renderPass <= 10)
{
String effect = "Effect" + (1 + renderPass - getPartAmount());
if(tags.hasKey(effect))
return effectIcons.get(tags.getInteger(effect));
}
return blankSprite;
}
return emptyIcon;
}
@Override
public void registerIcons(IIconRegister iconRegister) {
super.registerIcons(iconRegister);
addAnimationIcons(headStrings, animationHeadIcons, iconRegister, getIconSuffix(0));
addAnimationIcons(handleStrings, animationHandleIcons, iconRegister, getIconSuffix(2));
addAnimationIcons(accessoryStrings, animationAccessoryIcons, iconRegister, getIconSuffix(3));
addAnimationIcons(extraStrings, animationExtraIcons, iconRegister, getIconSuffix(4));
// animated effects...
// find out the longest animation
int count = 0;
if(animationHeadIcons.get(-1) != null)
count = Math.max(count, animationHeadIcons.get(-1).length);
if(animationHandleIcons.get(-1) != null)
count = Math.max(count, animationHandleIcons.get(-1).length);
if(animationAccessoryIcons.get(-1) != null)
count = Math.max(count, animationAccessoryIcons.get(-1).length);
if(animationExtraIcons.get(-1) != null)
count = Math.max(count, animationExtraIcons.get(-1).length);
for(Map.Entry<Integer, String> entry : effectStrings.entrySet())
{
IIcon[] anims = new IIcon[count];
boolean empty = true;
for(int i = 0; i < count; i++) {
String tex = entry.getValue() + "_" + (i+1);
if (TextureHelper.itemTextureExists(tex)) {
anims[i] = iconRegister.registerIcon(tex);
empty = false;
}
}
if(!empty)
animationEffectIcons.put(entry.getKey(), anims);
}
// default for effects is blank
IIcon[] anims = new IIcon[count];
for(int i = 0; i < count; i++)
anims[i] = blankSprite;
animationEffectIcons.put(-1, anims);
}
private void addAnimationIcons(HashMap<Integer, String> textures, HashMap<Integer, IIcon[]> icons, IIconRegister iconRegister, String standard)
{
icons.clear();
// we use the standard to determine how many animations there are
if(standard == null || standard.isEmpty())
return;
int count = 1;
standard = getDefaultTexturePath() + "/" + standard;
while(TextureHelper.itemTextureExists(standard + "_" + count))
count++;
count--;
// add the standard icons
IIcon[] anims = new IIcon[count];
for(int i = 0; i < count; i++)
anims[i] = iconRegister.registerIcon(standard + "_" + (i+1));
icons.put(-1, anims);
// now do the same for each entry
for(Map.Entry<Integer, String> entry : textures.entrySet())
{
anims = new IIcon[count];
boolean empty = true;
for(int i = 0; i < count; i++) {
String tex = entry.getValue() + "_" + (i+1);
if (TextureHelper.itemTextureExists(tex)) {
anims[i] = iconRegister.registerIcon(tex);
empty = false;
}
}
if(!empty)
icons.put(entry.getKey(), anims);
}
}
@SideOnly(Side.CLIENT)
@Override
public void addInformation(ItemStack stack, EntityPlayer player, List list, boolean par4) {
super.addInformation(stack, player, list, par4);
if(!stack.hasTagCompound())
return;
// also display max damage with current ammo
ItemStack currentAmmo = searchForAmmo(player, stack);
if(currentAmmo == null)
return;
float damage;
if(currentAmmo.getItem() == Items.arrow)
damage = 2;
else if(currentAmmo.getItem() instanceof AmmoItem && currentAmmo.hasTagCompound())
damage = currentAmmo.getTagCompound().getCompoundTag("InfiTool").getInteger("Attack");
else
return;
damage *= getProjectileSpeed(stack);
damage /= 2;
list.remove(list.size()-1); // remove last item (the damage of the bow itself)
list.add(currentAmmo.getDisplayName());
list.add(StatCollector.translateToLocal("attribute.name.ammo.maxAttackDamage") + ": " + TProxyClient.df.format(damage));
}
}