Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
CrossbowWidget.cs
浏览该文件的文档.
1using System.Xml.Linq;
2using Engine;
3
4namespace Game {
5 public class CrossbowWidget : CanvasWidget {
7
8 public int m_slotIndex;
9
10 public float? m_dragStartOffset;
11
13
15
17
18 public Random m_random = new();
19 public static string fName = "CrossbowWidget";
20
21 public CrossbowWidget(IInventory inventory, int slotIndex) {
22 m_inventory = inventory;
23 m_slotIndex = slotIndex;
24 XElement node = ContentManager.Get<XElement>("Widgets/CrossbowWidget");
25 LoadContents(this, node);
26 m_inventoryGrid = Children.Find<GridPanelWidget>("InventoryGrid");
27 m_inventorySlotWidget = Children.Find<InventorySlotWidget>("InventorySlot");
28 m_instructionsLabel = Children.Find<LabelWidget>("InstructionsLabel");
29 for (int i = 0; i < m_inventoryGrid.RowsCount; i++) {
30 for (int j = 0; j < m_inventoryGrid.ColumnsCount; j++) {
31 InventorySlotWidget widget = new();
32 m_inventoryGrid.Children.Add(widget);
33 m_inventoryGrid.SetWidgetCell(widget, new Point2(j, i));
34 }
35 }
36 int num = 10;
37 foreach (Widget child in m_inventoryGrid.Children) {
38 (child as InventorySlotWidget)?.AssignInventorySlot(inventory, num++);
39 }
40 m_inventorySlotWidget.AssignInventorySlot(inventory, slotIndex);
41 m_inventorySlotWidget.CustomViewMatrix = Matrix.CreateLookAt(new Vector3(0f, 1f, 0.2f), new Vector3(0f, 0f, 0.2f), -Vector3.UnitZ);
42 }
43
44 public override void Update() {
45 int slotValue = m_inventory.GetSlotValue(m_slotIndex);
46 int slotCount = m_inventory.GetSlotCount(m_slotIndex);
47 int num = Terrain.ExtractContents(slotValue);
48 int data = Terrain.ExtractData(slotValue);
49 int draw = CrossbowBlock.GetDraw(data);
51 if (num == 200
52 && slotCount > 0) {
53 if (draw < 15) {
54 m_instructionsLabel.Text = LanguageControl.Get(fName, 0);
55 }
56 else {
57 m_instructionsLabel.Text = !arrowType.HasValue ? LanguageControl.Get(fName, 1) : LanguageControl.Get(fName, 2);
58 }
59 if ((draw < 15 || !arrowType.HasValue)
60 && Input.Tap.HasValue
61 && HitTestGlobal(Input.Tap.Value) == m_inventorySlotWidget) {
62 Vector2 vector = m_inventorySlotWidget.ScreenToWidget(Input.Press.Value);
63 float num2 = vector.Y - DrawToPosition(draw);
64 if (MathF.Abs(vector.X - m_inventorySlotWidget.ActualSize.X / 2f) < 25f
65 && MathF.Abs(num2) < 25f) {
66 m_dragStartOffset = num2;
67 }
68 }
69 if (!m_dragStartOffset.HasValue) {
70 return;
71 }
72 if (Input.Press.HasValue) {
73 int num3 = PositionToDraw(m_inventorySlotWidget.ScreenToWidget(Input.Press.Value).Y - m_dragStartOffset.Value);
74 SetDraw(num3);
75 if (draw <= 9
76 && num3 > 9) {
77 AudioManager.PlaySound("Audio/CrossbowDraw", 1f, m_random.Float(-0.2f, 0.2f), 0f);
78 }
79 }
80 else {
81 m_dragStartOffset = null;
82 if (draw == 15) {
83 AudioManager.PlaySound("Audio/UI/ItemMoved", 1f, 0f, 0f);
84 return;
85 }
86 SetDraw(0);
87 AudioManager.PlaySound("Audio/CrossbowBoing", MathUtils.Saturate((draw - 3) / 10f), m_random.Float(-0.1f, 0.1f), 0f);
88 }
89 }
90 else {
91 ParentWidget.Children.Remove(this);
92 }
93 }
94
95 public void SetDraw(int draw) {
96 int data = Terrain.ExtractData(m_inventory.GetSlotValue(m_slotIndex));
97 int value = Terrain.MakeBlockValue(200, 0, CrossbowBlock.SetDraw(data, draw));
98 m_inventory.RemoveSlotItems(m_slotIndex, 1);
99 m_inventory.AddSlotItems(m_slotIndex, value, 1);
100 }
101
102 public static float DrawToPosition(int draw) => draw * 5.4f + 85f;
103
104 public static int PositionToDraw(float position) => (int)Math.Clamp(MathF.Round((position - 85f) / 5.4f), 0f, 15f);
105 }
106}
Engine.Vector3 Vector3
static float Saturate(float x)
static void PlaySound(string name, float volume, float pitch, float pan)
readonly WidgetsList Children
static object Get(Type type, string name)
static ArrowBlock.? ArrowType GetArrowType(int data)
static int GetDraw(int data)
static int SetDraw(int data, int draw)
static int PositionToDraw(float position)
CrossbowWidget(IInventory inventory, int slotIndex)
static float DrawToPosition(int draw)
GridPanelWidget m_inventoryGrid
InventorySlotWidget m_inventorySlotWidget
static string Get(string className, int key)
获取在当前语言类名键对应的字符串
static int ExtractContents(int value)
static int MakeBlockValue(int contents)
static int ExtractData(int value)
virtual Widget HitTestGlobal(Vector2 point, Func< Widget, bool > predicate=null)
WidgetInput Input
ContainerWidget ParentWidget
virtual void LoadContents(object eventsTarget, XElement node)
static Matrix CreateLookAt(Vector3 position, Vector3 target, Vector3 up)
static readonly Vector3 UnitZ