forked from space-wizards/space-station-14
/
BallisticMagazineWeaponComponent.cs
289 lines (246 loc) · 9.64 KB
/
BallisticMagazineWeaponComponent.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
using System;
using System.Collections.Generic;
using System.Linq;
using Content.Server.GameObjects.Components.Sound;
using Content.Server.GameObjects.EntitySystems;
using Content.Shared.GameObjects;
using Content.Shared.GameObjects.Components.Weapons.Ranged;
using Content.Shared.Interfaces;
using Robust.Server.GameObjects;
using Robust.Server.GameObjects.Components.Container;
using Robust.Server.Interfaces.GameObjects;
using Robust.Shared.Audio;
using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Random;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Maths;
using Robust.Shared.Random;
using Robust.Shared.Serialization;
using Robust.Shared.Utility;
using Robust.Shared.ViewVariables;
namespace Content.Server.GameObjects.Components.Weapon.Ranged.Projectile
{
[RegisterComponent]
public class BallisticMagazineWeaponComponent : BallisticWeaponComponent, IUse, IAttackBy, IMapInit
{
public override string Name => "BallisticMagazineWeapon";
[ViewVariables]
private string _defaultMagazine;
[ViewVariables]
private ContainerSlot _magazineSlot;
private List<BallisticMagazineType> _magazineTypes;
[ViewVariables]
public List<BallisticMagazineType> MagazineTypes => _magazineTypes;
[ViewVariables]
private IEntity Magazine => _magazineSlot.ContainedEntity;
#pragma warning disable 649
[Dependency] private readonly IRobustRandom _bulletDropRandom;
#pragma warning restore 649
[ViewVariables]
private string _magInSound;
[ViewVariables]
private string _magOutSound;
[ViewVariables]
private string _autoEjectSound;
[ViewVariables]
private bool _autoEjectMagazine;
[ViewVariables]
private AppearanceComponent _appearance;
private static readonly Direction[] _randomBulletDirs =
{
Direction.North,
Direction.East,
Direction.South,
Direction.West
};
protected override int ChamberCount => 1;
public override void ExposeData(ObjectSerializer serializer)
{
base.ExposeData(serializer);
serializer.DataField(ref _magazineTypes, "magazines",
new List<BallisticMagazineType>{BallisticMagazineType.Unspecified});
serializer.DataField(ref _defaultMagazine, "default_magazine", null);
serializer.DataField(ref _autoEjectMagazine, "auto_eject_magazine", false);
serializer.DataField(ref _autoEjectSound, "sound_auto_eject", null);
serializer.DataField(ref _magInSound, "sound_magazine_in", null);
serializer.DataField(ref _magOutSound, "sound_magazine_out", null);
}
public override void Initialize()
{
base.Initialize();
_appearance = Owner.GetComponent<AppearanceComponent>();
}
public override void Startup()
{
base.Startup();
_magazineSlot = ContainerManagerComponent.Ensure<ContainerSlot>("ballistic_gun_magazine", Owner);
if (Magazine != null)
{
// Already got magazine from loading a container.
Magazine.GetComponent<BallisticMagazineComponent>().OnAmmoCountChanged += _magazineAmmoCountChanged;
}
_updateAppearance();
}
public bool InsertMagazine(IEntity magazine, bool playSound = true)
{
if (!magazine.TryGetComponent(out BallisticMagazineComponent component))
{
throw new ArgumentException("Not a magazine", nameof(magazine));
}
if (!MagazineTypes.Contains(component.MagazineType))
{
throw new ArgumentException("Wrong magazine type", nameof(magazine));
}
if (component.Caliber != Caliber)
{
throw new ArgumentException("Wrong caliber", nameof(magazine));
}
if (!_magazineSlot.Insert(magazine))
{
return false;
}
if (_magInSound != null)
{
Owner.GetComponent<SoundComponent>().Play(_magInSound);
}
component.OnAmmoCountChanged += _magazineAmmoCountChanged;
if (GetChambered(0) == null)
{
// No bullet in chamber, load one from magazine.
var bullet = component.TakeBullet();
if (bullet != null)
{
LoadIntoChamber(0, bullet);
}
}
_updateAppearance();
return true;
}
public bool EjectMagazine(bool playSound = true)
{
var entity = Magazine;
if (entity == null)
{
return false;
}
if (_magazineSlot.Remove(entity))
{
entity.Transform.GridPosition = Owner.Transform.GridPosition;
if (_magOutSound != null)
{
Owner.GetComponent<SoundComponent>().Play(_magOutSound);
}
_updateAppearance();
entity.GetComponent<BallisticMagazineComponent>().OnAmmoCountChanged -= _magazineAmmoCountChanged;
return true;
}
_updateAppearance();
return false;
}
protected override void CycleChamberedBullet(int chamber)
{
DebugTools.Assert(chamber == 0);
// Eject chambered bullet.
var entity = RemoveFromChamber(chamber);
entity.Transform.GridPosition = Owner.Transform.GridPosition;
entity.Transform.LocalRotation = _bulletDropRandom.Pick(_randomBulletDirs).ToAngle();
var effect = $"/Audio/Guns/Casings/casingfall{_bulletDropRandom.Next(1, 4)}.ogg";
Owner.GetComponent<SoundComponent>().Play(effect, AudioParams.Default.WithVolume(-3));
if (Magazine != null)
{
var magComponent = Magazine.GetComponent<BallisticMagazineComponent>();
var bullet = magComponent.TakeBullet();
if (bullet != null)
{
LoadIntoChamber(0, bullet);
}
if (magComponent.CountLoaded == 0 && _autoEjectMagazine)
{
EjectMagazine();
if (_autoEjectSound != null)
{
Owner.GetComponent<SoundComponent>().Play(_autoEjectSound, AudioParams.Default.WithVolume(-5));
}
}
}
_updateAppearance();
}
public bool UseEntity(UseEntityEventArgs eventArgs)
{
var ret = EjectMagazine();
if (ret)
{
Owner.PopupMessage(eventArgs.User, "Magazine ejected");
}
else
{
Owner.PopupMessage(eventArgs.User, "No magazine");
}
return true;
}
public bool AttackBy(AttackByEventArgs eventArgs)
{
if (!eventArgs.AttackWith.TryGetComponent(out BallisticMagazineComponent component))
{
return false;
}
if (Magazine != null)
{
Owner.PopupMessage(eventArgs.User, "Already got a magazine.");
return false;
}
if (!MagazineTypes.Contains(component.MagazineType) || component.Caliber != Caliber)
{
Owner.PopupMessage(eventArgs.User, "Magazine doesn't fit.");
return false;
}
return InsertMagazine(eventArgs.AttackWith);
}
private void _magazineAmmoCountChanged()
{
_updateAppearance();
}
private void _updateAppearance()
{
if (Magazine != null)
{
var comp = Magazine.GetComponent<BallisticMagazineComponent>();
_appearance.SetData(BallisticMagazineWeaponVisuals.AmmoLeft, comp.CountLoaded);
_appearance.SetData(BallisticMagazineWeaponVisuals.AmmoCapacity, comp.Capacity);
_appearance.SetData(BallisticMagazineWeaponVisuals.MagazineLoaded, true);
}
else
{
_appearance.SetData(BallisticMagazineWeaponVisuals.AmmoLeft, 0);
_appearance.SetData(BallisticMagazineWeaponVisuals.AmmoLeft, 0);
_appearance.SetData(BallisticMagazineWeaponVisuals.MagazineLoaded, false);
}
}
[Verb]
public sealed class EjectMagazineVerb : Verb<BallisticMagazineWeaponComponent>
{
protected override string GetText(IEntity user, BallisticMagazineWeaponComponent component)
{
return component.Magazine == null ? "Eject magazine (magazine missing)" : "Eject magazine";
}
protected override VerbVisibility GetVisibility(IEntity user, BallisticMagazineWeaponComponent component)
{
return component.Magazine == null ? VerbVisibility.Disabled : VerbVisibility.Visible;
}
protected override void Activate(IEntity user, BallisticMagazineWeaponComponent component)
{
component.EjectMagazine();
}
}
void IMapInit.MapInit()
{
if (_defaultMagazine != null)
{
var magazine = Owner.EntityManager.SpawnEntity(_defaultMagazine);
InsertMagazine(magazine, false);
}
}
}
}