forked from Tr1ll10ns/Godot_FPS_Tutorial_CSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rifle.cs
103 lines (83 loc) · 2.94 KB
/
Rifle.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
using Godot;
using System;
public class Rifle : Weapon
{
public const int AMMO_IN_MAG = 50;
public override void _Ready()
{
Damage = 4;
SpareAmmo = 125;
AmmoInWeapon = 25;
Reloadable = true; // CAN_RELOAD
Refillable = true; // CAN_REFILL
IdleAnimationState = AnimationLoader.AnimationState.Rifle_idle;
FireAnimationState = AnimationLoader.AnimationState.Rifle_fire;
ReloadAnimationState = AnimationLoader.AnimationState.Rifle_reload;
WeaponEnabled = false;
PlayerNode = null;
}
public override void FireWeapon()
{
RayCast ray = GetNode<RayCast>("Ray_Cast");
ray.ForceRaycastUpdate();
AmmoInWeapon--;
if (ray.IsColliding()) {
var body = ray.GetCollider();
if (body == PlayerNode) {
}
else if (body is HittableByBullets hittableByBullets)
{
hittableByBullets.BulletHit(Damage, ray.GlobalTransform);
}
}
PlayerNode.CreateSound(FireSound, this.GlobalTransform.origin);
}
public override bool ReloadWeapon() {
bool canReload = false;
if (PlayerNode.AnimationPlayer.currentState.ToString() == IdleAnimationState.ToString())
canReload = true;
if (SpareAmmo <= 0 || AmmoInWeapon == AMMO_IN_MAG)
canReload = false;
if (canReload)
{
var ammoNeeded = AMMO_IN_MAG - AmmoInWeapon;
if (SpareAmmo >= ammoNeeded) {
SpareAmmo -= ammoNeeded;
AmmoInWeapon = AMMO_IN_MAG;
} else {
AmmoInWeapon += SpareAmmo;
SpareAmmo = 0;
}
PlayerNode.CreateSound(ReloadSound, this.GlobalTransform.origin);
PlayerNode.AnimationPlayer.SetAnimation(AnimationLoader.AnimationState.Rifle_reload);
return true;
}
return false;
}
public override bool EquipWeapon()
{
if (PlayerNode.AnimationPlayer.currentState.ToString() == IdleAnimationState.ToString()) {
WeaponEnabled = true;
return true;
}
if (PlayerNode.AnimationPlayer.currentState.ToString() == "Idle_unarmed") {
PlayerNode.AnimationPlayer.SetAnimation(AnimationLoader.AnimationState.Rifle_equip);
}
return false;
}
public override bool UnequipWeapon()
{
if (PlayerNode.AnimationPlayer.currentState.ToString() == IdleAnimationState.ToString())
if (PlayerNode.AnimationPlayer.currentState.ToString() != "Rifle_unequip")
PlayerNode.AnimationPlayer.SetAnimation(AnimationLoader.AnimationState.Rifle_unequip);
if (PlayerNode.AnimationPlayer.currentState.ToString() == "Idle_unarmed")
{
WeaponEnabled = false;
return true;
}
else
{
return false;
}
}
}