Repository navigation
Expand file tree
/
Copy pathDynamicObject.cs
More file actions
135 lines (115 loc) · 5.48 KB
/
Copy pathDynamicObject.cs
File metadata and controls
135 lines (115 loc) · 5.48 KB
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
using System.Numerics;
using SampSharp.Entities.SAMP;
using OpenRP.OpenMp.Streamer.Interop;
namespace OpenRP.OpenMp.Streamer;
/// <summary>Streamer-managed world object (PAWN-native: CreateDynamicObject).</summary>
public sealed class DynamicObject : DynamicEntity
{
internal DynamicObject(int id, int modelId, Vector3 position, Vector3 rotation)
{
Id = id;
ModelId = modelId;
_position = position;
_rotation = rotation;
}
public int ModelId { get; }
private Vector3 _position;
private Vector3 _rotation;
public Vector3 Position
{
get
{
unsafe
{
float x = 0, y = 0, z = 0;
if (StreamerInterop.Streamer_Object_GetPos(Id, &x, &y, &z))
_position = new Vector3(x, y, z);
}
return _position;
}
set
{
_position = value;
StreamerInterop.Streamer_Object_SetPos(Id, value.X, value.Y, value.Z);
}
}
public Vector3 Rotation
{
get
{
unsafe
{
float x = 0, y = 0, z = 0;
if (StreamerInterop.Streamer_Object_GetRot(Id, &x, &y, &z))
_rotation = new Vector3(x, y, z);
}
return _rotation;
}
set
{
_rotation = value;
StreamerInterop.Streamer_Object_SetRot(Id, value.X, value.Y, value.Z);
}
}
public override bool IsAlive => StreamerInterop.Streamer_Object_IsValid(Id);
public bool IsMoving => StreamerInterop.Streamer_Object_IsMoving(Id);
protected override void DestroyNative() => StreamerInterop.Streamer_Object_Destroy(Id);
/// <summary>
/// Starts moving the object towards the given target. Returns the move
/// duration in milliseconds, or 0 if the object is attached or speed is 0.
/// </summary>
public int Move(Vector3 target, float speed, Vector3 rotation = default)
=> StreamerInterop.Streamer_Object_Move(Id, target.X, target.Y, target.Z, speed,
rotation.X, rotation.Y, rotation.Z);
public bool Stop() => StreamerInterop.Streamer_Object_Stop(Id);
/// <summary>Opens the per-player edit mode for this dynamic object on the given player.</summary>
public bool Edit(Player player)
=> player != null && StreamerInterop.Streamer_Object_Edit(player.Id, Id);
public bool AttachTo(DynamicObject parent, Vector3 offset, Vector3 rotation, bool syncRotation = true)
=> StreamerInterop.Streamer_Object_AttachToObject(Id, parent?.Id ?? -1,
offset.X, offset.Y, offset.Z, rotation.X, rotation.Y, rotation.Z, syncRotation);
public bool AttachTo(Player player, Vector3 offset, Vector3 rotation)
=> player != null
&& StreamerInterop.Streamer_Object_AttachToPlayer(Id, player.Id,
offset.X, offset.Y, offset.Z, rotation.X, rotation.Y, rotation.Z);
public bool AttachTo(Vehicle vehicle, Vector3 offset, Vector3 rotation)
=> vehicle != null
&& StreamerInterop.Streamer_Object_AttachToVehicle(Id, vehicle.Id,
offset.X, offset.Y, offset.Z, rotation.X, rotation.Y, rotation.Z);
public bool SetMaterial(int materialIndex, int modelId, string txdName, string textureName, uint materialColor = 0)
=> StreamerInterop.Streamer_Object_SetMaterial(Id, materialIndex, modelId,
txdName ?? string.Empty, textureName ?? string.Empty, materialColor);
// Materials on objects use SAMP's "ARGB" packing (alpha in the high byte —
// 0xAARRGGBB), which is the OPPOSITE of the streamer's 3D-text-label color
// packing (RGBA, 0xRRGGBBAA). SetObjectMaterial / SetObjectMaterialText pass
// the value through to the SA:MP RPC stream, which the 0.3.7 client decodes
// assuming alpha-high. Mixing the two packings gives blue text on a yellow
// backdrop (Color.Black RGBA = 0x000000FF, read as ARGB = α=0/B=255 = invisible
// blue) — see VSRP Blackboards.cs.
public bool SetMaterial(int materialIndex, int modelId, string txdName, string textureName, Color materialColor)
=> SetMaterial(materialIndex, modelId, txdName, textureName, ToArgb(materialColor));
public unsafe bool SetMaterialText(int materialIndex, string text,
int materialSize = 0, string fontFace = "", int fontSize = 24, bool bold = true,
uint fontColor = 0xFFFFFFFFu, uint backColor = 0, int alignment = 1)
{
var enc = SampSharp.OpenMp.Core.Std.StringViewMarshaller.Encoding;
var s = text ?? string.Empty;
var byteCount = enc.GetByteCount(s);
var buffer = new byte[byteCount + 1];
enc.GetBytes(s, buffer);
buffer[byteCount] = 0;
fixed (byte* ptr = buffer)
{
return StreamerInterop.Streamer_Object_SetMaterialText(Id, materialIndex,
ptr, materialSize, fontFace ?? string.Empty, fontSize, bold,
fontColor, backColor, alignment);
}
}
public bool SetMaterialText(int materialIndex, string text,
int materialSize, string fontFace, int fontSize, bool bold,
Color fontColor, Color backColor, int alignment = 1)
=> SetMaterialText(materialIndex, text, materialSize, fontFace, fontSize, bold,
ToArgb(fontColor), ToArgb(backColor), alignment);
/// <summary>SAMP material color packing: 0xAARRGGBB (alpha in high byte).</summary>
private static uint ToArgb(Color c) => ((uint)c.A << 24) | ((uint)c.R << 16) | ((uint)c.G << 8) | c.B;
}