aboutsummaryrefslogtreecommitdiffstats
path: root/code/pawn/PlayerInventory.cs
blob: 55fa3ed67b6ba1f3d4f0e24544ccdbd2bfea5e6d (plain)
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
using Sandbox;
using System;

namespace MurderGame;

public partial class PlayerInventory : EntityComponent<Player> 
{
	const int MIN_SLOT = 1;
	const int MAX_SLOT = 2;

	const int UNARMED_SLOT = 1;
	const int PRIMARY_SLOT = 2;

	[Net]
	public Weapon PrimaryWeapon { get; private set; }

	[Net]
	public int ActiveSlot { get; set; }

	[Net]
	public bool AllowPickup { get; set; } = true;

	public Weapon GetCurrentWeapon()
	{
		return ActiveSlot switch
		{
			PRIMARY_SLOT => PrimaryWeapon,
			_ => null,
		};
	}

	public void SetPrimaryWeapon( Weapon weapon )
	{
		PrimaryWeapon?.OnHolster();
		PrimaryWeapon?.Delete();
		PrimaryWeapon = weapon;
		if (weapon != null)
		{
			weapon.ChangeOwner( Entity );
		}
		if (ActiveSlot == PRIMARY_SLOT)
		{
			weapon?.OnEquip( Entity );
		}
	}

	private void PrevSlot()
	{
		if (ActiveSlot > MIN_SLOT)
		{
			--ActiveSlot;
		}
		else
		{
			ActiveSlot = MAX_SLOT;
		}
	}
	private void NextSlot()
	{
		if (ActiveSlot < MAX_SLOT)
		{
			++ActiveSlot;
		}
		else
		{
			ActiveSlot = MIN_SLOT;
		}
	}

	public void Simulate(IClient cl)
	{
		var currentWeapon = GetCurrentWeapon();
		var currentSlot = ActiveSlot;

		if (Input.Released("SlotPrev"))
		{
			PrevSlot();
		}
		else if (Input.Released("SlotNext"))
		{
			NextSlot();
		}
		else if (Input.Down("Slot1"))
		{
			ActiveSlot = 1;
		}
		else if (Input.Down("Slot2"))
		{
			ActiveSlot = 2;
		}

		if (ActiveSlot != currentSlot)
		{
			currentWeapon?.OnHolster();
			GetCurrentWeapon()?.OnEquip( Entity );
		}
		GetCurrentWeapon()?.Simulate( cl );
	}

	public void Holster()
	{
		Weapon weapon = GetCurrentWeapon();
		weapon?.OnHolster();
	}

	public void Clear()
	{
		Holster();
		SetPrimaryWeapon( null );
	}

	public void SpillContents(Vector3 location, Vector3 velocity)
	{
		Holster();
		if (PrimaryWeapon is not null and Pistol )
		{
			PrimaryWeapon.ChangeOwner( null );
			DroppedWeapon droppedWeapon = new( (Pistol)PrimaryWeapon );
			droppedWeapon.CopyFrom( PrimaryWeapon );
			droppedWeapon.Position = location;
			droppedWeapon.Velocity = velocity;
		}
		Clear();
	}

}