DayZ 1.24
Loading...
Searching...
No Matches
ScriptCamera.c
Go to the documentation of this file.
1#ifdef GAME_TEMPLATE
2
3[EditorAttribute("box", "GameLib/Scripted", "Script camera", "-0.25 -0.25 -0.25", "0.25 0.25 0.25", "255 0 0 255")]
4class ScriptCameraClass
5{
6
7}
8
9ScriptCameraClass ScriptCameraSource;
10
11class ScriptCamera: GenericEntity
12{
13 [Attribute("60", "slider", "Field of view", "0 180 1")]
14 float FOV;
15 [Attribute("1", "editbox", "Near plane clip")]
16 float NearPlane;
17 [Attribute("4000", "editbox", "Far plane clip")]
18 float FarPlane;
19
20 [Attribute("1", "combobox", "Projection type", "", ParamEnumArray.FromEnum(CameraType))]
21 int Type;
22 [Attribute("5", "slider", "Camera speed", "0 20 1")]
23 float Speed;
24 [Attribute("1", "combobox", "Free Fly", "", ParamEnumArray.FromEnum(EBool))]
25 bool FreeFly;
26 [Attribute("0", "combobox", "Invert vertical", "", ParamEnumArray.FromEnum(EBool))]
27 bool Inverted;
28 [Attribute("0", "slider", "Camera index", "0 31 1")]
29 int Index;
30 float m_MouseSensitivity = 0.001; // should be somewhere else.
31 float m_GamepadSensitivity = 0.2; // should be somewhere else.
32 int m_GamepadFreeFly;
33
34 // debug variables
35 int m_DbgListSelection = 0;
36 ref array<string> m_DbgOptions = {"Perspective", "Orthographic"};
37
38 void ScriptCamera(IEntitySource src, IEntity parent)
39 {
40 SetFlags(EntityFlags.ACTIVE, false);
41 SetEventMask(EntityEvent.FRAME);
42
43 SetCameraVerticalFOV(Index, FOV);
44 SetCameraFarPlane(Index, FarPlane);
45 SetCameraNearPlane(Index, NearPlane);
46 SetCameraType(Index, Type);
47 m_DbgListSelection = Type - 1;
48 SetCamera(Index, GetOrigin(), GetYawPitchRoll());
49
50 vector camMat[4];
51 GetTransform(camMat);
52 SetCameraEx(Index, camMat);
53 m_GamepadFreeFly = FreeFly;
54 }
55
56 override protected void EOnFrame(IEntity other, float timeSlice) //EntityEvent.FRAME
57 {
58 GetGame().GetInputManager().ActivateContext("ScriptCameraContext");
59
60 if (GetGame().GetInputManager().GetActionTriggered("CamFreeFly"))
61 FreeFly = !FreeFly;
62
63 if (FreeFly)
64 FreeFly(timeSlice);
65 else
66 {
67 vector camMat[4]; // matrix can be set outside the class
68 GetTransform(camMat);
69 SetCameraEx(Index, camMat);
70 }
71
72 if (GameSettings.Debug)
73 DebugInfo();
74 }
75
76 protected void FreeFly(float timeSlice)
77 {
78 vector camPosition = GetOrigin();
79 vector angles = GetYawPitchRoll();
80 vector camMat[4];
81 GetTransform(camMat);
82 InputManager imanager = GetGame().GetInputManager();
83 imanager.ActivateContext("ScriptCameraFreeFlyContext");
84
85 // get input
86 float turnX = imanager.LocalValue("CamTurnRight") * 20.0 * timeSlice;
87 float turnY = imanager.LocalValue("CamTurnUp") * 20.0 * timeSlice;
88 float turnZ = imanager.LocalValue("CamRotate") * 20.0 * timeSlice;
89 float moveForward = imanager.LocalValue("CamForward");
90 float moveRight = imanager.LocalValue("CamRight");
91 float moveAscend = imanager.LocalValue("CamAscend");
92 float speedDelta = imanager.LocalValue("CamSpeedDelta") * timeSlice;
93 bool speedBoostHigh = imanager.GetActionTriggered("CamSpeedBoostHigh");
94 bool speedBoostLow = imanager.GetActionTriggered("CamSpeedBoostLow");
95
96 Speed = Math.Clamp(Speed + speedDelta * Speed * 0.25, 0.1, 1000.0);
97
98 float finalSpeed = Speed;
99 if (speedBoostLow)
100 finalSpeed *= 25;
101 else if (speedBoostHigh)
102 finalSpeed *= 5;
103
104 // rotation
105 angles[0] = turnX + angles[0];
106 if (Inverted)
107 angles[1] = turnY + angles[1];
108 else
109 angles[1] = -turnY + angles[1];
110
111 angles[2] = turnZ + angles[2];
112
113 // movement
114 vector move = vector.Zero;
115 vector forward = camMat[2];
116 vector up = camMat[1];
117 vector side = camMat[0];
118
119 move += forward * moveForward;
120 move += side * moveRight;
121 move += up * moveAscend;
122
123 // ------------
124 camPosition = (move * timeSlice * finalSpeed) + camPosition;
125
126 Math3D.YawPitchRollMatrix(angles, camMat);
127 camMat[3] = camPosition;
128 SetTransform(camMat);
129 SetCameraEx(Index, camMat);
130 }
131
132 protected void DebugInfo()
133 {
134 InputManager imanager = GetGame().GetInputManager();
135 DbgUI.Begin(String("Camera #" + Index.ToString()), 0, Index * 300);
136
137 DbgUI.Text(String("Position : " + GetOrigin().ToString()));
138 DbgUI.Text(String("Orientation (Y, P, R): " + GetYawPitchRoll().ToString()));
139 DbgUI.Text(String("Speed : " + Speed.ToString()));
140 DbgUI.Text(String("Mouse sensitivity : " + (2000 - (1 / m_MouseSensitivity)).ToString()));
141 DbgUI.Check("Select Free fly", FreeFly);
142 DbgUI.List("Camera type", m_DbgListSelection, m_DbgOptions);
143 if (m_DbgListSelection + 1 != Type)
144 {
145 Type = m_DbgListSelection + 1;
146 SetCameraType(Index, Type);
147 }
148
149 float sensitivity = 2000 - (1 / m_MouseSensitivity);
150 DbgUI.SliderFloat("Mouse sensitivity", sensitivity, 1, 1999);
151 m_MouseSensitivity = 1 / (2000 - sensitivity);
152
153 DbgUI.Text("CamTurnRight: " + imanager.LocalValue("CamTurnRight"));
154 DbgUI.Text("CamTurnUp: " + imanager.LocalValue("CamTurnUp"));
155 DbgUI.Text("CamSpeedDelta: " + imanager.LocalValue("CamSpeedDelta"));
156 DbgUI.Text("CamForward: " + imanager.LocalValue("CamForward"));
157 DbgUI.Text("CamRight: " + imanager.LocalValue("CamRight"));
158 DbgUI.Text("CamAscend: " + imanager.LocalValue("CamAscend"));
159 DbgUI.Text("CamSpeedBoostHigh: " + imanager.GetActionTriggered("CamSpeedBoostHigh"));
160 DbgUI.Text("CamSpeedBoostLow:" + imanager.GetActionTriggered("CamSpeedBoostLow"));
161
162 DbgUI.End();
163 }
164}
165
166#endif
EBool
Definition EnConvert.c:17
proto string ToString()
string Type
override void EOnFrame(IEntity other, float timeSlice)
Definition DbgUI.c:60
Definition EnMath.c:7
Result for an object found in CGame.IsBoxCollidingGeometryProxy.
static const vector Zero
Definition EnConvert.c:110
proto native CGame GetGame()
proto native void SetCamera(int cam, vector origin, vector angle)
proto native void SetCameraEx(int cam, const vector mat[4])
Changes camera matrix.
proto native void SetCameraFarPlane(int cam, float farplane)
proto native void SetCameraNearPlane(int cam, float nearplane)
proto native void SetCameraVerticalFOV(int cam, float fovy)
proto native void SetCameraType(int cam, CameraType type)
CameraType
Definition EnWorld.c:39
proto native void SetFlags(ShapeFlags flags)
static proto void List(string label, out int selection, TStringArray elems)
static proto native void End()
static proto native void Begin(string windowTitle, float x=0, float y=0)
static proto native void Text(string label)
static proto void Check(string label, out bool checked)
static proto void SliderFloat(string label, out float value, float min, float max, int pxWidth=150)
string String(string s)
Helper for passing string expression to functions with void parameter. Example: Print(String("Hello "...
Definition EnScript.c:338
EntityEvent
Entity events for event-mask, or throwing event from code.
Definition EnEntity.c:44
EntityFlags
Entity flags.
Definition EnEntity.c:114
void EditorAttribute(string style, string category, string description, vector sizeMin, vector sizeMax, string color, string color2="0 0 0 0", bool visible=true, bool insertable=true, bool dynamicBox=false)
Definition EnEntity.c:851
static proto void YawPitchRollMatrix(vector ang, out vector mat[3])
Creates rotation matrix from angles.
static proto float Clamp(float value, float min, float max)
Clamps 'value' to 'min' if it is lower than 'min', or to 'max' if it is higher than 'max'.
proto native void SetTransform(vector mat[4], bool immedUpdate=true)