This repository has been archived by the owner on Apr 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PixelPlayer.cs
79 lines (66 loc) · 2.5 KB
/
PixelPlayer.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
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;
using Microsoft.Xna.Framework;
namespace PixelItems
{
public class PixelPlayer : ModPlayer
{
public bool hasMirrorShield = false;
public int mirrorShieldCooldownFrames = 0;
private int lastMirrorShieldProc = 0;
public override void ResetEffects ()
{
hasMirrorShield = false;
mirrorShieldCooldownFrames = int.MaxValue;
}
public override bool CanBeHitByProjectile (Projectile proj)
{
if (hasMirrorShield && PixelItems.frameCount - lastMirrorShieldProc > mirrorShieldCooldownFrames && proj.Colliding (player.Hitbox, proj.Hitbox))
{
lastMirrorShieldProc = PixelItems.frameCount;
reflectProjectile (proj);
return false;
}
else
return base.CanBeHitByProjectile (proj);
}
// Using https://gamedev.stackexchange.com/a/25582 to determine which axes to reflect projectile in.
private void reflectProjectile (Projectile projectile)
{
projectile.hostile = false;
projectile.friendly = true;
Vector2 projectileCenter = projectile.Center - projectile.velocity;
Vector2 oldVelocity = projectile.velocity * -1;
Vector2 ourBottomLeft = player.BottomLeft;
float difference = projectileCenter.Y - ourBottomLeft.Y;
bool ab = difference * player.width > (projectileCenter.X - ourBottomLeft.X) * player.height;
bool ad = difference * player.width > (player.TopRight.X - projectileCenter.X) * player.height;
if (ab == ad)
projectile.velocity.Y *= -1;
else
projectile.velocity.X *= -1;
Vector2 predictedPosition = projectile.Center + projectile.velocity;
if (predictedPosition.X > ourBottomLeft.X
&& predictedPosition.X < ourBottomLeft.X + player.width
&& predictedPosition.Y < ourBottomLeft.Y
&& predictedPosition.Y > ourBottomLeft.Y - player.height)
projectile.velocity *= -1;
Main.PlaySound (SoundID.Item, projectile.position, 30);
for (int i = 0; i < 2; ++i)
{
int dustIndex = Dust.NewDust (projectile.Center, 0, 0, 45, oldVelocity.X, oldVelocity.Y, 255, default, Main.rand.Next (20, 26) * 0.1f);
Main.dust [dustIndex].noLight = true;
Main.dust [dustIndex].noGravity = true;
Main.dust [dustIndex].velocity *= 0.5f;
}
for (int i = 0; i < 2; ++i)
{
int dustIndex = Dust.NewDust (projectile.Center, 0, 0, 45, projectile.velocity.X, projectile.velocity.Y, 255, default, Main.rand.Next (20, 26) * 0.1f);
Main.dust [dustIndex].noLight = true;
Main.dust [dustIndex].noGravity = true;
Main.dust [dustIndex].velocity *= 0.5f;
}
}
}
}