Guest User

ezrdxfcghvbjnkm,

a guest
Sep 1st, 2013
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.55 KB | None | 0 0
  1. //-------------------------------------------------
  2. //
  3. // This is an example of using the AttachCameraToObject function
  4. // to create a no-clip flying camera.
  5. //
  6. // h02 2012
  7. //
  8. // SA-MP 0.3e and above
  9. //
  10. //-------------------------------------------------
  11.  
  12. #include <a_samp>
  13.  
  14. // Players Move Speed
  15. #define MOVE_SPEED 100.0
  16. #define ACCEL_RATE 0.03
  17.  
  18. // Players Mode
  19. #define CAMERA_MODE_NONE 0
  20. #define CAMERA_MODE_FLY 1
  21.  
  22. // Key state definitions
  23. #define MOVE_FORWARD 1
  24. #define MOVE_BACK 2
  25. #define MOVE_LEFT 3
  26. #define MOVE_RIGHT 4
  27. #define MOVE_FORWARD_LEFT 5
  28. #define MOVE_FORWARD_RIGHT 6
  29. #define MOVE_BACK_LEFT 7
  30. #define MOVE_BACK_RIGHT 8
  31.  
  32. // Enumeration for storing data about the player
  33. enum noclipenum
  34. {
  35. cameramode,
  36. flyobject,
  37. mode,
  38. lrold,
  39. udold,
  40. lastmove,
  41. Float:accelmul
  42. }
  43. new noclipdata[MAX_PLAYERS][noclipenum];
  44.  
  45. //--------------------------------------------------
  46.  
  47. public OnFilterScriptExit()
  48. {
  49. // If any players are still in edit mode, boot them out before the filterscript unloads
  50. for(new x; x<MAX_PLAYERS; x++)
  51. {
  52. if(noclipdata[x][cameramode] == CAMERA_MODE_FLY) CancelFlyMode(x);
  53. }
  54. return 1;
  55. }
  56.  
  57. //--------------------------------------------------
  58.  
  59. public OnPlayerConnect(playerid)
  60. {
  61. // Reset the data belonging to this player slot
  62. noclipdata[playerid][cameramode] = CAMERA_MODE_NONE;
  63. noclipdata[playerid][lrold] = 0;
  64. noclipdata[playerid][udold] = 0;
  65. noclipdata[playerid][mode] = 0;
  66. noclipdata[playerid][lastmove] = 0;
  67. noclipdata[playerid][accelmul] = 0.0;
  68. return 1;
  69. }
  70.  
  71. //--------------------------------------------------
  72.  
  73. public OnPlayerCommandText(playerid, cmdtext[])
  74. {
  75. if(!strcmp(cmdtext, "/flymode", true))
  76. {
  77. // Place the player in and out of edit mode
  78. if(GetPVarType(playerid, "FlyMode")) CancelFlyMode(playerid);
  79. else FlyMode(playerid);
  80. return 1;
  81. }
  82. return 0;
  83. }
  84.  
  85. //--------------------------------------------------
  86.  
  87. public OnPlayerUpdate(playerid)
  88. {
  89. if(noclipdata[playerid][cameramode] == CAMERA_MODE_FLY)
  90. {
  91. new keys,ud,lr;
  92. GetPlayerKeys(playerid,keys,ud,lr);
  93.  
  94. if(noclipdata[playerid][mode] && (GetTickCount() - noclipdata[playerid][lastmove] > 100))
  95. {
  96. // If the last move was > 100ms ago, process moving the object the players camera is attached to
  97. MoveCamera(playerid);
  98. }
  99.  
  100. // Is the players current key state different than their last keystate?
  101. if(noclipdata[playerid][udold] != ud || noclipdata[playerid][lrold] != lr)
  102. {
  103. if((noclipdata[playerid][udold] != 0 || noclipdata[playerid][lrold] != 0) && ud == 0 && lr == 0)
  104. { // All keys have been released, stop the object the camera is attached to and reset the acceleration multiplier
  105. StopPlayerObject(playerid, noclipdata[playerid][flyobject]);
  106. noclipdata[playerid][mode] = 0;
  107. noclipdata[playerid][accelmul] = 0.0;
  108. }
  109. else
  110. { // Indicates a new key has been pressed
  111.  
  112. // Get the direction the player wants to move as indicated by the keys
  113. noclipdata[playerid][mode] = GetMoveDirectionFromKeys(ud, lr);
  114.  
  115. // Process moving the object the players camera is attached to
  116. MoveCamera(playerid);
  117. }
  118. }
  119. noclipdata[playerid][udold] = ud; noclipdata[playerid][lrold] = lr; // Store current keys pressed for comparison next update
  120. return 0;
  121. }
  122. return 1;
  123. }
  124.  
  125. //--------------------------------------------------
  126.  
  127. stock GetMoveDirectionFromKeys(ud, lr)
  128. {
  129. new direction = 0;
  130.  
  131. if(lr < 0)
  132. {
  133. if(ud < 0) direction = MOVE_FORWARD_LEFT; // Up & Left key pressed
  134. else if(ud > 0) direction = MOVE_BACK_LEFT; // Back & Left key pressed
  135. else direction = MOVE_LEFT; // Left key pressed
  136. }
  137. else if(lr > 0) // Right pressed
  138. {
  139. if(ud < 0) direction = MOVE_FORWARD_RIGHT; // Up & Right key pressed
  140. else if(ud > 0) direction = MOVE_BACK_RIGHT; // Back & Right key pressed
  141. else direction = MOVE_RIGHT; // Right key pressed
  142. }
  143. else if(ud < 0) direction = MOVE_FORWARD; // Up key pressed
  144. else if(ud > 0) direction = MOVE_BACK; // Down key pressed
  145.  
  146. return direction;
  147. }
  148.  
  149. //--------------------------------------------------
  150.  
  151. stock MoveCamera(playerid)
  152. {
  153. new Float:FV[3], Float:CP[3];
  154. GetPlayerCameraPos(playerid, CP[0], CP[1], CP[2]); // Cameras position in space
  155. GetPlayerCameraFrontVector(playerid, FV[0], FV[1], FV[2]); // Where the camera is looking at
  156.  
  157. // Increases the acceleration multiplier the longer the key is held
  158. if(noclipdata[playerid][accelmul] <= 1) noclipdata[playerid][accelmul] += ACCEL_RATE;
  159.  
  160. // Determine the speed to move the camera based on the acceleration multiplier
  161. new Float:speed = MOVE_SPEED * noclipdata[playerid][accelmul];
  162.  
  163. // Calculate the cameras next position based on their current position and the direction their camera is facing
  164. new Float:X, Float:Y, Float:Z;
  165. GetNextCameraPosition(noclipdata[playerid][mode], CP, FV, X, Y, Z);
  166. MovePlayerObject(playerid, noclipdata[playerid][flyobject], X, Y, Z, speed);
  167.  
  168. // Store the last time the camera was moved as now
  169. noclipdata[playerid][lastmove] = GetTickCount();
  170. return 1;
  171. }
  172.  
  173. //--------------------------------------------------
  174.  
  175. stock GetNextCameraPosition(move_mode, Float:CP[3], Float:FV[3], &Float:X, &Float:Y, &Float:Z)
  176. {
  177. // Calculate the cameras next position based on their current position and the direction their camera is facing
  178. #define OFFSET_X (FV[0]*6000.0)
  179. #define OFFSET_Y (FV[1]*6000.0)
  180. #define OFFSET_Z (FV[2]*6000.0)
  181. switch(move_mode)
  182. {
  183. case MOVE_FORWARD:
  184. {
  185. X = CP[0]+OFFSET_X;
  186. Y = CP[1]+OFFSET_Y;
  187. Z = CP[2]+OFFSET_Z;
  188. }
  189. case MOVE_BACK:
  190. {
  191. X = CP[0]-OFFSET_X;
  192. Y = CP[1]-OFFSET_Y;
  193. Z = CP[2]-OFFSET_Z;
  194. }
  195. case MOVE_LEFT:
  196. {
  197. X = CP[0]-OFFSET_Y;
  198. Y = CP[1]+OFFSET_X;
  199. Z = CP[2];
  200. }
  201. case MOVE_RIGHT:
  202. {
  203. X = CP[0]+OFFSET_Y;
  204. Y = CP[1]-OFFSET_X;
  205. Z = CP[2];
  206. }
  207. case MOVE_BACK_LEFT:
  208. {
  209. X = CP[0]+(-OFFSET_X - OFFSET_Y);
  210. Y = CP[1]+(-OFFSET_Y + OFFSET_X);
  211. Z = CP[2]-OFFSET_Z;
  212. }
  213. case MOVE_BACK_RIGHT:
  214. {
  215. X = CP[0]+(-OFFSET_X + OFFSET_Y);
  216. Y = CP[1]+(-OFFSET_Y - OFFSET_X);
  217. Z = CP[2]-OFFSET_Z;
  218. }
  219. case MOVE_FORWARD_LEFT:
  220. {
  221. X = CP[0]+(OFFSET_X - OFFSET_Y);
  222. Y = CP[1]+(OFFSET_Y + OFFSET_X);
  223. Z = CP[2]+OFFSET_Z;
  224. }
  225. case MOVE_FORWARD_RIGHT:
  226. {
  227. X = CP[0]+(OFFSET_X + OFFSET_Y);
  228. Y = CP[1]+(OFFSET_Y - OFFSET_X);
  229. Z = CP[2]+OFFSET_Z;
  230. }
  231. }
  232. }
  233. //--------------------------------------------------
  234.  
  235. stock CancelFlyMode(playerid)
  236. {
  237. DeletePVar(playerid, "FlyMode");
  238. CancelEdit(playerid);
  239. TogglePlayerSpectating(playerid, false);
  240.  
  241. DestroyPlayerObject(playerid, noclipdata[playerid][flyobject]);
  242. noclipdata[playerid][cameramode] = CAMERA_MODE_NONE;
  243. return 1;
  244. }
  245.  
  246. //--------------------------------------------------
  247.  
  248. stock FlyMode(playerid)
  249. {
  250. // Create an invisible object for the players camera to be attached to
  251. new Float:X, Float:Y, Float:Z;
  252. GetPlayerPos(playerid, X, Y, Z);
  253. noclipdata[playerid][flyobject] = CreatePlayerObject(playerid, 19300, X, Y, Z, 0.0, 0.0, 0.0);
  254.  
  255. // Place the player in spectating mode so objects will be streamed based on camera location
  256. TogglePlayerSpectating(playerid, true);
  257. // Attach the players camera to the created object
  258. AttachCameraToPlayerObject(playerid, noclipdata[playerid][flyobject]);
  259.  
  260. SetPVarInt(playerid, "FlyMode", 1);
  261. noclipdata[playerid][cameramode] = CAMERA_MODE_FLY;
  262. return 1;
  263. }
  264.  
  265. //--------------------------------------------------
Add Comment
Please, Sign In to add comment