Advertisement
Guest User

Untitled

a guest
Jun 16th, 2014
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.80 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using UMA;
  4.  
  5. public class PlayerHandler : MonoBehaviour
  6. {
  7. private Client myClient = null;
  8. public NetworkPlayer TargetPlayerTradePlayer;
  9. public PlayerHandler TargetPlayerTradeHandler;
  10. public bool AcceptedTrade = false;
  11. public bool CancelledTrade = false;
  12. public ItemData[] TradeItems = new ItemData[5];
  13. public int[] TradeItemSlots = new int[5];
  14. public int currentslot = 0;
  15. public string[] slottypes = new string[5];
  16. public string slottype = "";
  17. bool firstinput = false;
  18. public bool Initialized = false;
  19. public string OWNERPLAYER;
  20. public string OWNERPLAYERGUID;
  21. public string OWNERUSER;
  22. public string VIEWID;
  23. public int TYPE = 0;
  24. private static GameObject myPlayerCameraGO;
  25. public GameObject myPlayerGO;
  26. private GameObject CameraLookAt;
  27. public NetworkPlayer myPlayer;
  28. private NetworkViewID myPlayerViewID;
  29. private NetworkViewID myPlayerCameraViewID;
  30. private Rigidbody myRigidbody;
  31. private static Camera myPlayerCamera;
  32. private Animator myPlayerAnimator;
  33. public int currenticon = 0;
  34. public ItemData currentitem;
  35. public float MouseX = 0f;
  36. public float MouseY = 0f;
  37. public float Distance = 5f;
  38. public float desiredDistance = 0f;
  39. public float distanceSmooth = 0f;
  40. public float preOccludedDistance = 0f;
  41. public float velX = 0f;
  42. public float velY = 0f;
  43. public float velZ = 0f;
  44. public float velDistance = 0f;
  45. public Vector3 MoveVector = Vector3.zero;
  46. public Vector3 position = Vector3.zero;
  47. public Vector3 desiredPosition = Vector3.zero;
  48.  
  49. public float ForwardSpeed = 3f;
  50. public float BackwardSpeed = 1f;
  51. public float StrafingSpeed = 2f;
  52. public float JumpSpeed = 5f;
  53.  
  54. public float TerminalVelocity = 5f;
  55. public float Gravity = -15f;
  56. int Grounded = 0;
  57. bool MouseButton1 = false;
  58. bool MouseButton0 = false;
  59. bool MouseButton2 = false;
  60. bool MouseButton1Up = false;
  61. bool MouseButton0Up = false;
  62. bool MouseButton2Up = false;
  63. bool MouseButton1Down = false;
  64. bool MouseButton0Down = false;
  65. bool MouseButton2Down = false;
  66. bool TabDown = false;
  67. bool Jumping = false;
  68. float HAxis;
  69. float VAxis;
  70. Vector3 MousePosition;
  71. float MouseXAxis;
  72. float MouseYAxis;
  73. float MouseScrollAxis = -1f;
  74.  
  75. private bool PlayerIsCombative = false;
  76.  
  77.  
  78. public void Initialize(int type, NetworkPlayer player, string user, NetworkViewID viewID, NetworkViewID cameraViewID, GameObject camLookAt, Animator animator, Camera camera, GameObject camerago, GameObject playergo, Rigidbody rigidbody)
  79. {
  80. if(Network.player == player || Network.isServer)
  81. enabled=true;
  82. else
  83. enabled=false;
  84. myPlayer = player;
  85. CameraLookAt = camLookAt;
  86. myPlayerViewID = viewID;
  87. myPlayerAnimator = animator;
  88. OWNERPLAYER = myPlayer.ToString();
  89. OWNERPLAYERGUID = myPlayer.guid;
  90. OWNERUSER = user;
  91. VIEWID = myPlayerViewID.ToString();
  92. TYPE = type;
  93. myPlayerCameraGO = camerago;
  94. myPlayerCamera = camera;
  95. myPlayerCameraViewID = cameraViewID;
  96. myRigidbody = rigidbody;
  97. myPlayerGO = playergo;
  98. Initialized = true;
  99. TradeItems = new ItemData[5];
  100. TradeItemSlots = new int[5];
  101. for(int i = 0;i<5;i++)
  102. {
  103. TradeItems[i] = ItemData.EmptyItem();
  104. TradeItemSlots[i]= 0;
  105. slottypes[i] = "";
  106. }
  107.  
  108. }
  109. private void FixedUpdate()
  110. {
  111. if(Network.isServer)
  112. {
  113. Vector3 gravityUp;
  114. gravityUp = myPlayerGO.transform.position - new Vector3(0,0,0);
  115. gravityUp.Normalize();
  116. myRigidbody.AddForce(gravityUp * Gravity * myRigidbody.mass);
  117. if(Grounded >= 1)
  118. myRigidbody.drag = 0.1f;
  119. else
  120. myRigidbody.drag = 1.0f;
  121.  
  122. Quaternion quatern = Quaternion.FromToRotation(myPlayerGO.transform.up, gravityUp);
  123. quatern = quatern * myPlayerGO.transform.rotation;
  124.  
  125. myPlayerGO.transform.rotation = Quaternion.Slerp(myPlayerGO.transform.rotation, quatern, 0.1f);
  126.  
  127.  
  128. DetermineDirection();
  129.  
  130.  
  131. if(myRigidbody.velocity.magnitude < MoveSpeed() && Grounded >= 1)
  132. {
  133. myRigidbody.AddForce(transform.rotation * Vector3.forward * VAxis * MoveSpeed());
  134. myRigidbody.AddForce(transform.rotation * Vector3.right * HAxis * MoveSpeed());
  135. }
  136. if(Jumping == true && Grounded >= 1)
  137. {
  138. myRigidbody.velocity = myRigidbody.velocity + (myRigidbody.transform.up * JumpSpeed);
  139. Jumping = false;
  140. }
  141. if (!PlayerIsCombative)
  142. {
  143. if(MouseButton2)
  144. {
  145. MouseX += MouseXAxis * 5f;
  146. MouseY -= MouseYAxis * 5f;
  147. if (MouseY < -360) MouseY += 360;
  148. if (MouseY > 360) MouseY -= 360;
  149. //MouseY = Mathf.Clamp(MouseY, -40f, 80f);
  150. }
  151. if(MouseScrollAxis < -0.01f || MouseScrollAxis > 0.01f)
  152. {
  153. desiredDistance = Mathf.Clamp(Distance - MouseScrollAxis * 5f,0.5f,5f);
  154. preOccludedDistance = desiredDistance;
  155. distanceSmooth = 0.05f;
  156. }
  157. int count = 0;
  158. do{
  159. if(desiredDistance < preOccludedDistance)
  160. {
  161. Vector3 pos = CalculatePosition(MouseY, MouseX, preOccludedDistance);
  162. float nearestDistance = CheckCameraPoints(CameraLookAt.transform.localPosition, pos);
  163. if(nearestDistance == -1 || nearestDistance > preOccludedDistance)
  164. {
  165. desiredDistance = preOccludedDistance;
  166. }
  167. }
  168. Distance = Mathf.SmoothDamp(Distance, desiredDistance, ref velDistance, distanceSmooth);
  169. desiredPosition = CalculatePosition(MouseY, MouseX, Distance);
  170. count++;
  171. }while(CheckIfOccluded(count));
  172. CheckCameraPoints(CameraLookAt.transform.localPosition, desiredPosition);
  173. float posX = Mathf.SmoothDamp(position.x, desiredPosition.x, ref velX, 0.05f);
  174. float posY = Mathf.SmoothDamp(position.y, desiredPosition.y, ref velY, 0.1f);
  175. float posZ = Mathf.SmoothDamp(position.z, desiredPosition.z, ref velZ, 0.05f);
  176. position = new Vector3(posX, posY, posZ);
  177. myPlayerCameraGO.transform.localPosition = position;
  178.  
  179.  
  180.  
  181. Vector3 relPlayerPosition = CameraLookAt.transform.position - myPlayerCameraGO.transform.position;
  182. myPlayerCameraGO.transform.rotation = Quaternion.LookRotation(relPlayerPosition, myPlayerGO.transform.up);
  183.  
  184. if (HAxis != 0 || VAxis != 0)
  185. {
  186.  
  187. //myPlayerGO.transform.rotation = Quaternion.Euler(0,myPlayerCameraGO.transform.localEulerAngles.y,0);
  188. //myPlayerGO.transform.rotation = Quaternion.Euler(0,myPlayerCameraGO.transform.eulerAngles.y,0);
  189. //myPlayerGO.transform.localRotation = Quaternion.Euler(0,myPlayerCameraGO.transform.localEulerAngles.y,0);
  190. //myPlayerGO.transform.localRotation = Quaternion.Euler(0,myPlayerCameraGO.transform.eulerAngles.y,0);
  191.  
  192.  
  193. //myPlayerGO.transform.rotation = Quaternion.Euler(myPlayerGO.transform.eulerAngles.x,myPlayerCameraGO.transform.eulerAngles.y,myPlayerGO.transform.eulerAngles.z);
  194. //myPlayerGO.transform.rotation = Quaternion.Euler(myPlayerGO.transform.localEulerAngles.x,myPlayerCameraGO.transform.eulerAngles.y,myPlayerGO.transform.localEulerAngles.z);
  195. //myPlayerGO.transform.rotation = Quaternion.Euler(myPlayerGO.transform.eulerAngles.x,myPlayerCameraGO.transform.eulerAngles.y,myPlayerGO.transform.eulerAngles.z);
  196. //myPlayerGO.transform.rotation = Quaternion.Euler(myPlayerGO.transform.localEulerAngles.x,myPlayerCameraGO.transform.localEulerAngles.y,myPlayerGO.transform.localEulerAngles.z);
  197. //myPlayerGO.transform.localRotation = Quaternion.Euler(myPlayerGO.transform.eulerAngles.x,myPlayerCameraGO.transform.eulerAngles.y,myPlayerGO.transform.eulerAngles.z);
  198. //myPlayerGO.transform.localRotation = Quaternion.Euler(myPlayerGO.transform.localEulerAngles.x,myPlayerCameraGO.transform.eulerAngles.y,myPlayerGO.transform.localEulerAngles.z);
  199. //myPlayerGO.transform.localRotation = Quaternion.Euler(myPlayerGO.transform.eulerAngles.x,myPlayerCameraGO.transform.localEulerAngles.y,myPlayerGO.transform.eulerAngles.z);
  200. //myPlayerGO.transform.localRotation = Quaternion.Euler(myPlayerGO.transform.localEulerAngles.x,myPlayerCameraGO.transform.localEulerAngles.y,myPlayerGO.transform.localEulerAngles.z);
  201. }
  202.  
  203.  
  204. }
  205.  
  206. }
  207. if(Network.isClient)
  208. {
  209. HAxis = Input.GetAxis("Horizontal");
  210. VAxis = Input.GetAxis("Vertical");
  211. MousePosition = Input.mousePosition;
  212. MouseXAxis = Input.GetAxis("Mouse X");
  213. MouseYAxis = Input.GetAxis("Mouse Y");
  214. MouseScrollAxis = Input.GetAxis("Mouse ScrollWheel");
  215. Jumping = Input.GetButton("Jump");
  216. if(Input.GetMouseButton(0))MouseButton0 = true;
  217. else MouseButton0 = false;
  218. if(Input.GetMouseButtonUp(0))MouseButton0Up = true;
  219. else MouseButton0Up = false;
  220. if(Input.GetMouseButtonDown(0))MouseButton0Down = true;
  221. else MouseButton0Down = false;
  222. if(Input.GetMouseButton(1))MouseButton1 = true;
  223. else MouseButton1 = false;
  224. if(Input.GetMouseButtonUp(1))MouseButton1Up = true;
  225. else MouseButton1Up = false;
  226. if(Input.GetMouseButtonDown(1))MouseButton1Down = true;
  227. else MouseButton1Down = false;
  228. if(Input.GetMouseButtonDown(2))MouseButton2Down = true;
  229. else MouseButton2Down = false;
  230. if(Input.GetMouseButtonUp(2))MouseButton2Up = true;
  231. else MouseButton2Up = false;
  232. if(Input.GetMouseButton(2))MouseButton2 = true;
  233. else MouseButton2 = false;
  234. if(Input.GetKeyDown(KeyCode.Tab))TabDown = true;
  235. else TabDown = false;
  236. }
  237.  
  238. }
  239. private float MoveSpeed()
  240. {
  241. var moveSpeed = 0f;
  242. switch (MoveDirection)
  243. {
  244. case Direction.Stationary:
  245. moveSpeed = 0;
  246. break;
  247. case Direction.Forward:
  248. moveSpeed = ForwardSpeed;
  249. break;
  250. case Direction.Backward:
  251. moveSpeed = BackwardSpeed;
  252. break;
  253. case Direction.Left:
  254. moveSpeed = StrafingSpeed;
  255. break;
  256. case Direction.Right:
  257. moveSpeed = StrafingSpeed;
  258. break;
  259. case Direction.LeftForward:
  260. moveSpeed = ForwardSpeed;
  261. break;
  262. case Direction.RightForward:
  263. moveSpeed = ForwardSpeed;
  264. break;
  265. case Direction.LeftBackward:
  266. moveSpeed = BackwardSpeed;
  267. break;
  268. case Direction.RightBackward:
  269. moveSpeed = BackwardSpeed;
  270. break;
  271. }
  272. return moveSpeed;
  273. }
  274. private enum Direction
  275. {
  276. Stationary, Forward, Backward, Left, Right, LeftForward, RightForward, LeftBackward, RightBackward
  277. }
  278.  
  279. private Direction MoveDirection {get; set;}
  280. private void DetermineDirection()
  281. {
  282. bool forward = false;
  283. bool backward = false;
  284. bool left = false;
  285. bool right = false;
  286. if(VAxis > 0)
  287. forward = true;
  288. if(VAxis < 0)
  289. backward = true;
  290. if(HAxis > 0)
  291. right = true;
  292. if(HAxis < 0)
  293. left = true;
  294. if(forward)
  295. {
  296. if(left)
  297. MoveDirection = Direction.LeftForward;
  298. else if(right)
  299. MoveDirection = Direction.RightForward;
  300. else
  301. MoveDirection = Direction.Forward;
  302. }
  303. else if(backward)
  304. {
  305. if(left)
  306. MoveDirection = Direction.LeftBackward;
  307. else if(right)
  308. MoveDirection = Direction.RightBackward;
  309. else
  310. MoveDirection = Direction.Backward;
  311. }
  312. else if(left)
  313. MoveDirection = Direction.Left;
  314. else if(right)
  315. MoveDirection = Direction.Right;
  316. else
  317. MoveDirection = Direction.Stationary;
  318. }
  319.  
  320. private void CalculateDesiredPosition()
  321. {
  322. if(desiredDistance < preOccludedDistance)
  323. {
  324. Vector3 pos = CalculatePosition(MouseY, MouseX, preOccludedDistance);
  325. float nearestDistance = CheckCameraPoints(CameraLookAt.transform.localPosition, pos);
  326. if(nearestDistance == -1 || nearestDistance > preOccludedDistance)
  327. {
  328. desiredDistance = preOccludedDistance;
  329. }
  330. }
  331. Distance = Mathf.SmoothDamp(Distance, desiredDistance, ref velDistance, distanceSmooth);
  332. desiredPosition = CalculatePosition(MouseY, MouseX, Distance);
  333.  
  334. }
  335. private Vector3 CalculatePosition(float rotationX, float rotationY, float distance)
  336. {
  337. Vector3 direction = new Vector3(0, 0, -distance);
  338. Quaternion rotation = Quaternion.Euler(rotationX, rotationY, 0);
  339. return CameraLookAt.transform.localPosition + rotation * direction;
  340. }
  341. private bool CheckIfOccluded(int count)
  342. {
  343. bool isOccluded = false;
  344. float nearestDistance = CheckCameraPoints(CameraLookAt.transform.localPosition, desiredPosition);
  345. if(nearestDistance != -1)
  346. {
  347. if(count < 10)
  348. {
  349. isOccluded = true;
  350. Distance -= 0.05f;
  351. if(Distance < 0.25f)
  352. Distance = 0.25f;
  353. }
  354. else
  355.  
  356. Distance = nearestDistance - myPlayerCamera.nearClipPlane;
  357. desiredDistance = Distance;
  358. distanceSmooth = 1f;
  359. }
  360. return isOccluded;
  361. }
  362. private float CheckCameraPoints(Vector3 from, Vector3 to)
  363. {
  364. var nearDistance = -1f;
  365. RaycastHit hitInfo;
  366. ClipPlanePoints clipPlanePoints = ClipPlaneAtNear( to);
  367. if(Physics.Linecast(from, clipPlanePoints.UpperLeft, out hitInfo) && hitInfo.collider.tag != "Player")
  368. nearDistance = hitInfo.distance;
  369. if(Physics.Linecast(from, clipPlanePoints.LowerLeft, out hitInfo) && hitInfo.collider.tag != "Player")
  370. if(hitInfo.distance < nearDistance || nearDistance == -1)
  371. nearDistance = hitInfo.distance;
  372. if(Physics.Linecast(from, clipPlanePoints.UpperRight, out hitInfo) && hitInfo.collider.tag != "Player")
  373. if(hitInfo.distance < nearDistance || nearDistance == -1)
  374. nearDistance = hitInfo.distance;
  375. if(Physics.Linecast(from, clipPlanePoints.LowerRight, out hitInfo) && hitInfo.collider.tag != "Player")
  376. if(hitInfo.distance < nearDistance || nearDistance == -1)
  377. nearDistance = hitInfo.distance;
  378. if(Physics.Linecast(from, to + transform.forward * -myPlayerCamera.nearClipPlane, out hitInfo) && hitInfo.collider.tag != "Player")
  379. if(hitInfo.distance < nearDistance || nearDistance == -1)
  380. nearDistance = hitInfo.distance;
  381. return nearDistance;
  382. }
  383. private static ClipPlanePoints ClipPlaneAtNear(Vector3 pos)
  384. {
  385. ClipPlanePoints clipPlanePoints = new ClipPlanePoints();
  386. Transform transform = myPlayerCameraGO.transform;
  387. float HalfFOV = (myPlayerCamera.fieldOfView / 2) * Mathf.Deg2Rad;
  388. float aspect = myPlayerCamera.aspect;
  389. float distance = myPlayerCamera.nearClipPlane;
  390. float height = distance * Mathf.Tan(HalfFOV);
  391. float width = height * aspect;
  392. clipPlanePoints.LowerRight = pos + transform.right * width;
  393. clipPlanePoints.LowerRight -= transform.up * height;
  394. clipPlanePoints.LowerRight += transform.forward * distance;
  395. clipPlanePoints.LowerLeft = pos - transform.right * width;
  396. clipPlanePoints.LowerLeft -= transform.up * height;
  397. clipPlanePoints.LowerLeft += transform.forward * distance;
  398. clipPlanePoints.UpperRight = pos + transform.right * width;
  399. clipPlanePoints.UpperRight += transform.up * height;
  400. clipPlanePoints.UpperRight += transform.forward * distance;
  401. clipPlanePoints.UpperLeft = pos - transform.right * width;
  402. clipPlanePoints.UpperLeft += transform.up * height;
  403. clipPlanePoints.UpperLeft += transform.forward * distance;
  404. return clipPlanePoints;
  405. }
  406. private struct ClipPlanePoints
  407. {
  408. public Vector3 UpperLeft;
  409. public Vector3 LowerLeft;
  410. public Vector3 UpperRight;
  411. public Vector3 LowerRight;
  412. }
  413. private void OnCollisionEnter(Collision c)
  414. {
  415. if(c.gameObject.tag == "Land")
  416. {
  417. Grounded++;
  418. }
  419. }
  420.  
  421.  
  422. private void OnCollisionExit(Collision c)
  423. {
  424. if(c.gameObject.tag == "Land" && Grounded > 0)
  425. {
  426. Grounded--;
  427. }
  428. }
  429.  
  430.  
  431.  
  432.  
  433.  
  434.  
  435.  
  436. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement