Arma Reforger Explorer 1.7.0.54
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Loading...
Searching...
No Matches
SCR_EntitySpawnerPlugin.c
Go to the documentation of this file.
1#ifdef WORKBENCH
2[WorkbenchPluginAttribute(name: "Playmode Entity Spawner", wbModules: { "WorldEditor" }, shortcut: "Ctrl+Alt+E", awesomeFontCode: 0xF055)]
3class SCR_EntitySpawnerPlugin : WorkbenchPlugin
4{
5 [Attribute(desc: "Prefab of the entity to be spawned.", uiwidget: UIWidgets.ResourcePickerThumbnail, params: "et")]
6 protected ResourceName m_sPrefab;
7
8 [Attribute(desc: "When enabled, previously spawned entity will be deleted upon creation of the new one.")]
9 protected bool m_bReplacePrevious;
10
11 [Attribute(desc: "When enabled, the entity will be created on camera position and shot towards cursor.")]
12 protected bool m_bShootFromCamera;
13
14 [Attribute(desc: "When enabled, this window won't be shown again when you activate the plugin.\nYou can still access it Plugins > Settings > Entity Spawner.")]
15 protected bool m_bDontShowThisAgain;
16
17 protected IEntity m_Entity;
18
19 protected static const string DESCRIPTION = "Spawn prefabs during play mode for easy debugging.\nWorks only when playing in viewport, not in full-screen.";
20
21 //------------------------------------------------------------------------------------------------
22 override void Run()
23 {
25 {
26 Print("Cannot spawn entity in editor mode, only in play mode.", LogLevel.WARNING);
27 return;
28 }
29
30 if (Replication.IsClient())
31 {
32 Print("Cannot spawn entity on client.", LogLevel.WARNING);
33 return;
34 }
35
36 //--- Delete previous entity
37 if (m_bReplacePrevious && m_Entity)
38 SCR_EntityHelper.DeleteEntityAndChildren(m_Entity);
39
40 //--- Find position
41 InputManager inputManager = GetGame().GetInputManager();
42 WorkspaceWidget workspace = GetGame().GetWorkspace();
43 BaseWorld world = GetGame().GetWorld();
44
45 float cursorX, cursorY;
46 if (inputManager.IsContextActive("MenuContext"))
47 {
48 //--- Cursor shown, use pointer position
49 cursorX = inputManager.GetActionValue("MouseX");
50 cursorY = inputManager.GetActionValue("MouseY");
51 }
52 else
53 {
54 //--- Cursor not shown, use center of the screen
55 cursorX = workspace.GetWidth() * 0.5;
56 cursorY = workspace.GetHeight() * 0.5;
57 }
58
59 //--- Trace position under cursor
60 vector outDir;
61 vector startPos = workspace.ProjScreenToWorld(workspace.DPIUnscale(cursorX), workspace.DPIUnscale(cursorY), outDir, world, -1);
62 outDir *= 1000;
63
64 TraceParam trace = new TraceParam();
65 trace.Start = startPos;
66 trace.End = startPos + outDir;
67 trace.Flags = TraceFlags.WORLD | TraceFlags.ENTS;
68 trace.LayerMask = TRACE_LAYER_CAMERA;
69
70 if (startPos[1] > world.GetOceanBaseHeight())
71 trace.Flags = trace.Flags | TraceFlags.OCEAN;
72
73 float traceDis = world.TraceMove(trace, null);
74 if (traceDis == 1 && !m_bShootFromCamera)
75 {
76 Print("Cannot spawn entity, cursor is not pointing at surface.", LogLevel.WARNING);
77 return;
78 }
79
80 //--- Show configuration dialog
81 if (!m_bDontShowThisAgain && !Workbench.ScriptDialog("Configure 'Playmode Entity Spawner' plugin", DESCRIPTION, this))
82 {
83 Print("User-cancelled", LogLevel.NORMAL);
84 return;
85 }
86
87 if (!m_sPrefab)
88 {
89 Print("Cannot spawn entity, prefab not defined. Configure it in Plugins > Settings > Entity Spawner.", LogLevel.WARNING);
90 return;
91 }
92
93 //--- Spawn entity
94 EntitySpawnParams spawnParams = new EntitySpawnParams();
95 Math3D.AnglesToMatrix(Vector(outDir.VectorToAngles()[0], 0, 0), spawnParams.Transform);
96 if (m_bShootFromCamera)
97 spawnParams.Transform[3] = startPos;
98 else
99 spawnParams.Transform[3] = startPos + outDir * traceDis;
100
101 m_Entity = GetGame().SpawnEntityPrefab(Resource.Load(m_sPrefab), world, spawnParams);
102
103 //--- Shoot!
104 if (m_bShootFromCamera)
105 {
106 Physics physics = m_Entity.GetPhysics();
107 if (physics)
108 physics.SetVelocity(outDir * 0.1);
109 }
110
111 if (m_Entity)
112 Print(string.Format("Entity @\"%1\" spawned at %2", m_sPrefab.GetPath(), spawnParams.Transform[3]), LogLevel.NORMAL);
113 else
114 Print(string.Format("Error when spawning entity @\"%1\" at %2", m_sPrefab.GetPath(), spawnParams.Transform[3]), LogLevel.WARNING);
115 }
116
117 //------------------------------------------------------------------------------------------------
118 override void Configure()
119 {
120 Workbench.ScriptDialog("Configure 'Playmode Entity Spawner' plugin", DESCRIPTION, this);
121 }
122
123 //------------------------------------------------------------------------------------------------
124 [ButtonAttribute("OK", true)]
125 protected bool ButtonOK()
126 {
127 return true;
128 }
129
130 //------------------------------------------------------------------------------------------------
131 [ButtonAttribute("Cancel")]
132 protected bool ButtonCancel()
133 {
134 return false;
135 }
136}
137#endif // WORKBENCH
GenerateFlowMaps WorkbenchPlugin WorkbenchPluginAttribute("Regenerate river flow-maps", "Generate and save/overwrite river flow-maps", "", "", {"WorldEditor"}, "", 0xf773)
Definition FlowmapTool.c:59
const int TRACE_LAYER_CAMERA
Definition Constants.c:14
ArmaReforgerScripted GetGame()
Definition game.c:1398
enum EAITargetInfoCategory m_Entity
override void Run()
bool ButtonCancel()
bool ButtonOK()
UI Textures DeployMenu Briefing conflict_HintBanner_1_UI desc
override void Configure()
class WorkbenchDialog_AbortRetryIgnore ButtonAttribute("OK", true)
Input management system for user interactions.
Main replication API.
Definition Replication.c:14
Object holding reference to resource. In destructor release the resource.
Definition Resource.c:25
static bool IsEditMode()
Definition Functions.c:1566
void EntitySpawnParams()
Definition gameLib.c:130
proto void Print(void var, LogLevel level=LogLevel.NORMAL)
Prints content of variable to console/log.
LogLevel
Enum with severity of the logging message.
Definition LogLevel.c:14
SCR_FieldOfViewSettings Attribute
proto native vector Vector(float x, float y, float z)
TraceFlags
Definition TraceFlags.c:13