Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / activity / role / shop / ShopItemSlot.cs
  1.  
  2. using System;
  3. using Config;
  4. using Godot;
  5.  
  6. public partial class ShopItemSlot : Area2D, IInteractive, IOutline
  7. {
  8. private Label _name;
  9. private Label _price;
  10. private Sprite2D _icon;
  11. private ExcelConfig.ActivityBase _config;
  12.  
  13. private uint _finalPrice;
  14. //是否买得起
  15. private bool _flag = true;
  16.  
  17. public bool ShowOutline { get; set; } = true;
  18. public Color OutlineColor
  19. {
  20. get => _blendShaderMaterial == null ? Colors.Black : _blendShaderMaterial.GetShaderParameter(ShaderParamNames.OutlineColor).AsColor();
  21. set => _blendShaderMaterial?.SetShaderParameter(ShaderParamNames.OutlineColor, value);
  22. }
  23.  
  24. public bool IsDestroyed { get; private set; }
  25.  
  26. private ShaderMaterial _blendShaderMaterial;
  27.  
  28. public void InitItem(ExcelConfig.ActivityBase config)
  29. {
  30. _price = GetNode<Label>("Price");
  31. _name = GetNode<Label>("NameLabel");
  32. _icon = GetNode<Sprite2D>("Icon");
  33. _name.Visible = false;
  34. _blendShaderMaterial = _icon.Material as ShaderMaterial;
  35. _config = config;
  36. _name.Text = config.Name;
  37. _finalPrice = config.Price;
  38. _price.Text = _finalPrice.ToString();
  39. _icon.Texture = ResourceManager.LoadTexture2D(config.Icon);
  40. }
  41.  
  42. public override void _Process(double delta)
  43. {
  44. var player = World.Current.Player;
  45. if (player != null)
  46. {
  47. if (_flag && player.RoleState.Gold < _finalPrice) //买不起
  48. {
  49. _flag = false;
  50. _price.Modulate = Colors.Red;
  51. }
  52. else if (!_flag && player.RoleState.Gold >= _finalPrice) //买得起
  53. {
  54. _flag = true;
  55. _price.Modulate = Colors.White;
  56. }
  57. }
  58. }
  59.  
  60. public CheckInteractiveResult CheckInteractive(ActivityObject master)
  61. {
  62. return new CheckInteractiveResult(this, _config != null && master is Role);
  63. }
  64.  
  65. public void Interactive(ActivityObject master)
  66. {
  67. var role = (Role)master;
  68. if (role.RoleState.Gold < _finalPrice)
  69. {
  70. return;
  71. }
  72. Monitorable = false;
  73. Visible = false;
  74. role.UseGold((int)_finalPrice);
  75.  
  76. var item = ActivityObject.Create(_config);
  77. if (item is Weapon weapon)
  78. {
  79. if (!role.PickUpWeapon(weapon))
  80. {
  81. role.ThrowWeapon();
  82. role.PickUpWeapon(weapon);
  83. }
  84. }
  85. else if (item is ActiveProp activeProp)
  86. {
  87. if (!role.PickUpActiveProp(activeProp))
  88. {
  89. role.ThrowActiveProp();
  90. role.PickUpActiveProp(activeProp);
  91. }
  92. }
  93. else if (item is BuffProp buffProp)
  94. {
  95. role.PickUpBuffProp(buffProp);
  96. }
  97. else
  98. {
  99. throw new Exception("商店中购买到不支持的物体: " + _config.Id);
  100. }
  101.  
  102. _config = null;
  103. }
  104. public virtual void OnTargetEnterd(ActivityObject target)
  105. {
  106. _name.Visible = true;
  107. OutlineColor = Colors.White;
  108. }
  109.  
  110. public virtual void OnTargetExitd(ActivityObject target)
  111. {
  112. _name.Visible = false;
  113. OutlineColor = Colors.Black;
  114. }
  115. public void Destroy()
  116. {
  117. if (IsDestroyed) return;
  118. IsDestroyed = true;
  119. QueueFree();
  120. }
  121. }