aboutsummaryrefslogtreecommitdiffstats
path: root/code/entity/DroppedWeapon.cs
blob: 46cf67d02dc2030146b873da2a8377f26333c847 (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
using System;
using MurderGame;
using Sandbox;
using Sandbox.Component;
using Player = MurderGame.Player;

public class DroppedWeapon : AnimatedEntity, IUse
{
	public DroppedWeapon( Weapon weapon ) : this()
	{
		Ammo = weapon.Ammo;
		WeaponType = weapon.GetType();
	}

	public DroppedWeapon()
	{
		Tags.Add( "droppedweapon" );

		var glow = Components.GetOrCreate<Glow>();
		glow.Enabled = true;
		glow.Width = 0.25f;
		glow.Color = new Color( 4f, 50.0f, 70.0f );
		glow.ObscuredColor = new Color( 0, 0, 0, 0 );

		PhysicsEnabled = true;
		UsePhysicsCollision = true;
		EnableSelfCollisions = true;
		EnableSolidCollisions = true;
	}

	private int Ammo { get; }
	private Type WeaponType { get; }

	public bool OnUse( Entity user )
	{
		if ( user is not Player player )
		{
			return false;
		}

		Pickup( player );
		return false;
	}

	public bool IsUsable( Entity user )
	{
		return user is Player { Inventory: { PrimaryWeapon: null, AllowPickup: true } };
	}

	public override void StartTouch( Entity other )
	{
		if ( !Game.IsServer )
		{
			return;
		}

		if ( !other.Tags.Has( "livingplayer" ) )
		{
			return;
		}

		var player = (Player)other;
		if ( IsUsable( player ) )
		{
			Pickup( player );
		}
	}

	public void Pickup( Player player )
	{
		var instance = TypeLibrary.Create<Weapon>( WeaponType );
		instance.Ammo = Ammo;
		player.Inventory.SetPrimaryWeapon( instance );
		Delete();
	}
}