Advertisement
Guest User

Untitled

a guest
Feb 21st, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.19 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Player : Character
  6. {
  7. public byte playerNum;
  8. public byte charNum;
  9. public float maxspeed, speedcount, speedrate, turnrate; //speed floats
  10. public float jumpheld, jumpreset; //jump floats
  11. public float MercyTime, defaultMercyTime, launcherDist;
  12. public byte downstep, upstep, leftstep, rightstep, punchcount;
  13. public float isFacing, moveDelay, moveThisMuch, stunTime, leftBuffer, downBuffer, upBuffer, rightBuffer; //various other floats
  14. public bool isOnFloor, solidfloor, isAttack, landed, leftPress, rightPress, downPress, upPress; //various bools
  15. public string lastmove;
  16. public string Horizontal, Vertical, A, B, C;
  17. public byte canAirAttack;
  18.  
  19.  
  20.  
  21. // Use this for initialization
  22. public override void Start()
  23. {
  24. health = 1;// playerNum == 1 ? GameManager.Lives1 : GameManager.Lives2;
  25. Horizontal = playerNum == 1 ? Controls.H1 : Controls.H2;
  26. Vertical = playerNum == 1 ? Controls.V1 : Controls.V2;
  27. A = playerNum == 1 ? Controls.A1 : Controls.A2;
  28. B = playerNum == 1 ? Controls.B1 : Controls.B2;
  29. C = playerNum == 1 ? Controls.C1 : Controls.C2;
  30. gravity = maxgravity;
  31. facing = 1;
  32. state = "alive";
  33. base.Start();
  34. }
  35.  
  36. // Update is called once per frame
  37. public override void Update()
  38. {
  39. base.Update();
  40. if (!isOnFloor)
  41. {
  42. solidfloor = false;
  43. }
  44. DirPresses();
  45. if (health < 0)
  46. { //if the players health is less than or equal to 0
  47. state = "dead"; //they are dead
  48. }
  49. if (state == "alive")
  50. { //if the player is alive
  51. stunTime = 0.5f;
  52. if (MercyTime > 0f)
  53. {
  54. MercyTime -= Time.deltaTime;
  55. }
  56.  
  57. Movement(); //execute movement commands
  58. Animations(); //execute attack commands
  59. Attacks(); //players can only attack when not in landing animation
  60. } else if (state == "stun")
  61. {
  62. Stun();
  63. } else if (state == "dead")
  64. {
  65. Dead();
  66. }
  67.  
  68. }
  69.  
  70. public virtual void Movement()
  71. {
  72. if (!isAttack)
  73. {
  74. if (Controls.GetAxis(Horizontal) != 0)
  75. {
  76. speed = Mathf.Clamp(speed + (speedrate * Time.deltaTime), 0, maxspeed);
  77. transform.rotation = Quaternion.AngleAxis(180 * (Controls.GetAxis(Horizontal) < 0).CompareTo(false), Vector3.up);
  78. facing = Controls.GetAxis(Horizontal);
  79. }
  80. else
  81. {
  82. speed = Mathf.Clamp(speed - (speedrate * Time.deltaTime), 0, maxspeed);
  83. }
  84. if (Controls.GetAxis(Horizontal) > 0)
  85. {
  86. isFacing = Mathf.Min(isFacing + (turnrate * Time.deltaTime), 1);
  87. }
  88. if (Controls.GetAxis(Horizontal) < 0)
  89. {
  90. isFacing = Mathf.Max(isFacing - (turnrate * Time.deltaTime), -1);
  91. }
  92. } else
  93. {
  94. speed = 0;
  95. }
  96. rb.velocity = new Vector3((speed * isFacing), rb.velocity.y);
  97. Jumping();
  98. }
  99.  
  100. public virtual void Jumping()
  101. {
  102. if (Controls.GetButtonDown(A) && isOnFloor && Controls.GetAxis(Vertical) >= 0 && (notPlaying("uu", "dd", "aa", "adb")))
  103. {
  104. if (lastmove == "")
  105. lastmove = anim.CurrentClip.name;
  106. isAttack = false;
  107. jump = maxjump;
  108. downstep = 0;
  109. upstep = 0;
  110. }
  111. if (!isAttack)
  112. {
  113. if (!isOnFloor && jump > jumprate*-1.5)
  114. {
  115. jump -= jumprate * Time.deltaTime;
  116. }
  117.  
  118. if ((Controls.GetButtonUp(A) && jump > 0) || (isOnFloor && jump < 0))
  119. {
  120. jump = 0;
  121. }
  122. } else
  123. {
  124. jump = 0;
  125. }
  126. rb.velocity = new Vector3(rb.velocity.x, jump);
  127.  
  128. }
  129.  
  130. public virtual void Animations()
  131. {
  132. if (!isAttack)
  133. {
  134. if (!isOnFloor)
  135. {
  136. if (jump > 0)
  137. {
  138. playThis("jump");
  139. }
  140. if (jump < 0)
  141. {
  142. playThis("fall");
  143. }
  144. }
  145. else
  146. {
  147. if (Controls.GetAxis(Horizontal) == 0)
  148. {
  149. playThis("idle");
  150. }
  151. else
  152. {
  153. playThis("run");
  154. }
  155. }
  156. }
  157.  
  158. }
  159.  
  160. public virtual void Attacks()
  161. {
  162. if (moveDelay > 0f)
  163. {
  164. moveDelay -= Time.deltaTime;
  165. }
  166. if (moveDelay <= 0f)
  167. {
  168.  
  169. lastmove = "";
  170. }
  171. if (isOnFloor)
  172. {
  173. canAirAttack = 0;
  174. }
  175. if (Controls.GetButtonDown(B) && Controls.GetAxis(Vertical) == 0 && notPlaying("rb", "da", "dd", "adb", "ub", "aa") && punchcount < 3 && lastmove != "b" && lastmove != "bb" && (canAirAttack & 1) == 0)
  176. {
  177. isAttack = true;
  178. punchcount++;
  179. if (anim.IsPlaying("uu"))
  180. {
  181. canAirAttack |= 32;
  182. }
  183. if (anim.IsPlaying("aa"))
  184. {
  185. canAirAttack |= 128;
  186. }
  187. if (punchcount % 2 == 1)
  188. {
  189. anim.Play("b");
  190. } else
  191. {
  192. anim.Play("bb");
  193. }
  194. }
  195. if (endOfAnim("b"))
  196. {
  197. lastmove = "b";
  198. canAirAttack |= 1;
  199. moveDelay = 0.4f + (0.0f * (punchcount >= 4).CompareTo(false));
  200. isAttack = false;
  201. punchcount = 0;
  202. }
  203. if (endOfAnim("bb"))
  204. {
  205. lastmove = "bb";
  206. canAirAttack |= 1;
  207. moveDelay = 0.4f;
  208. isAttack = false;
  209. punchcount = 0;
  210. }
  211. if (!Controls.GetButton(B) && lastmove != "dd" && (Controls.GetButtonDown(A) || (Controls.GetButton(A) && (anim.CurrentClip.name[0] == 'a' || anim.CurrentClip.name[0] == 'j') && anim.CurrentFrame < 1)) && Controls.GetAxis(Vertical) < 0 && notPlaying("da", "aa", "dd", "adb", "ub") && lastmove != "da" && (canAirAttack & 2) == 0 && (!anim.IsPlaying("rb") || (anim.IsPlaying("rb") && Controls.GetAxis(Horizontal) == -facing)))
  212. {
  213. if ((anim.IsPlaying("rb") && Controls.GetAxis(Horizontal) == -facing)){
  214. transform.rotation = Quaternion.AngleAxis(180 * (Controls.GetAxis(Horizontal) < 0).CompareTo(false), Vector3.up);
  215. facing = Controls.GetAxis(Horizontal);
  216. }
  217. isAttack = true;
  218. playThis("da");
  219. }
  220.  
  221. if (endOfAnim("da"))
  222. {
  223. lastmove = "da";
  224. canAirAttack |= 34;
  225. moveDelay = 0.4f;
  226. isAttack = false;
  227. punchcount = 0;
  228. }
  229.  
  230. if (!Controls.GetButton(B) && !Controls.GetButton(A) && ((rightstep >= 2 && leftstep == 0) || (leftstep >= 2 && rightstep == 0)) && notPlaying("rb", "da", "dd", "ub", "adb", "aa") && lastmove != "rb" && (canAirAttack & 4) == 0)
  231. {
  232. if (anim.IsPlaying("uu"))
  233. {
  234. canAirAttack |= 32;
  235. }
  236. isAttack = true;
  237. anim.Play("rb");
  238. }
  239. if (anim.IsPlaying("rb"))
  240. {
  241. rb.velocity = new Vector3(maxspeed * 3f/2f * facing, 0);
  242. }
  243. if (endOfAnim("rb"))
  244. {
  245. lastmove = "rb";
  246. canAirAttack |= 36;
  247. moveDelay = 0.4f;
  248. isAttack = false;
  249. punchcount = 0;
  250. }
  251. if (Controls.GetButton(B) && lastmove != "dd" && !Controls.GetButton(A) && Controls.GetAxis(Vertical) > 0 && notPlaying("rb", "da", "dd", "uu", "adb", "aa", "ub") && lastmove != "ub" && (canAirAttack & 8) == 0)
  252. {
  253.  
  254. isAttack = true;
  255. playThis("ub");
  256. }
  257.  
  258. if (endOfAnim("ub"))
  259. {
  260. lastmove = "ub";
  261. canAirAttack |= 40;
  262. moveDelay = 0.3f;
  263. isAttack = false;
  264. punchcount = 0;
  265. }
  266. if (Controls.GetButton(B) && !Controls.GetButton(A) && lastmove != "dd" && lastmove != "ub" && lastmove != "da" && Controls.GetAxis(Vertical) < 0 && !isOnFloor && notPlaying("rb", "da", "dd", "ub", "aa") && lastmove != "adb" && (canAirAttack & 16) == 0)
  267. {
  268. if (anim.IsPlaying("uu"))
  269. {
  270. canAirAttack |= 32;
  271. }
  272. if (anim.IsPlaying("aa"))
  273. {
  274. canAirAttack |= 128;
  275. }
  276. isAttack = true;
  277. playThis("adb");
  278. }
  279.  
  280. if (endOfAnim("adb"))
  281. {
  282. lastmove = "adb";
  283. canAirAttack |= 176;
  284. moveDelay = 0.4f;
  285. isAttack = false;
  286. punchcount = 0;
  287. }
  288. if (downstep >= 2 && upstep == 0 && !Controls.GetButton(B) && !Controls.GetButton(A) && !solidfloor && (!anim.IsPlaying("jump") || (anim.IsPlaying("jump") && anim.CurrentFrame > 0)) && notPlaying("rb", "aa", "uu", "adb", "ub", "da") && lastmove != "dd")
  289. {
  290. if (isOnFloor)
  291. {
  292. boxcol.isTrigger = true;
  293. }
  294. rb.velocity = Vector3.zero;
  295. isAttack = true;
  296. anim.Play("dd");
  297. }
  298. if (anim.IsPlaying("dd") && !isOnFloor)
  299. {
  300. rb.velocity = new Vector3(0, -maxspeed * 3f);
  301. }
  302.  
  303.  
  304. }
  305.  
  306.  
  307.  
  308. public virtual void Stun()
  309. {
  310.  
  311.  
  312. }
  313.  
  314. public virtual void Dead()
  315. {
  316.  
  317. }
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327. public virtual void Sounds()
  328. {
  329.  
  330. }
  331.  
  332. public void DirPresses() //this determines what direction is tapped
  333. {
  334. if (leftBuffer > 0f)
  335. {
  336. leftBuffer -= Time.deltaTime;
  337. }
  338. if (leftBuffer <= 0f)
  339. {
  340. leftstep = 0;
  341. }
  342. if (rightBuffer > 0f)
  343. {
  344. rightBuffer -= Time.deltaTime;
  345. }
  346. if (rightBuffer <= 0f)
  347. {
  348. rightstep = 0;
  349. }
  350. if (downBuffer > 0f)
  351. {
  352. downBuffer -= Time.deltaTime;
  353. }
  354. if (downBuffer <= 0f)
  355. {
  356. downstep = 0;
  357. }
  358. if (upBuffer > 0f)
  359. {
  360. upBuffer -= Time.deltaTime;
  361. }
  362. if (upBuffer <= 0f)
  363. {
  364. upstep = 0;
  365. }
  366. if (Controls.GetAxis(Vertical) == 0)
  367. {
  368. downPress = true;
  369. upPress = true;
  370. }
  371. else
  372. {
  373. if (downPress)
  374. {
  375. if (Controls.GetAxis(Vertical) < 0)
  376. {
  377. downstep += 1;
  378. downBuffer = 0.33f;
  379. }
  380. downPress = false;
  381. }
  382. if (upPress)
  383. {
  384. if (Controls.GetAxis(Vertical) > 0)
  385. {
  386. upstep += 1;
  387. upBuffer = 0.33f;
  388. }
  389. upPress = false;
  390. }
  391. }
  392. if (Controls.GetAxis(Horizontal) == 0)
  393. {
  394. leftPress = true;
  395. rightPress = true;
  396. }
  397. else
  398. {
  399. if (leftPress)
  400. {
  401. if (Controls.GetAxis(Horizontal) < 0)
  402. {
  403. leftstep += 1;
  404. leftBuffer = 0.33f;
  405. }
  406. leftPress = false;
  407. }
  408. if (rightPress)
  409. {
  410. if (Controls.GetAxis(Horizontal) > 0)
  411. {
  412. rightstep += 1;
  413. rightBuffer = 0.33f;
  414. }
  415. rightPress = false;
  416. }
  417. }
  418. }
  419.  
  420. public bool isFloorCollision(Collision c)
  421. {
  422. foreach (ContactPoint col in c)
  423. {
  424.  
  425. if (transform.position.y > col.point.y && Mathf.Abs((transform.position.y + boxcol.center.y) - col.point.y) >= boxcol.size.y / 2f)
  426. {
  427. //Debug.Log("floor");
  428. return true;
  429. }
  430. }
  431. return false;
  432. }
  433. public bool isCeilingCollision(Collision c)
  434. {
  435. foreach (ContactPoint col in c)
  436. {
  437. if (transform.position.y < col.point.y && Mathf.Abs((transform.position.y + boxcol.center.y) - col.point.y) >= boxcol.size.y / 2f)
  438. {
  439. //Debug.Log("ceiling");
  440. return true;
  441. }
  442. }
  443. return false;
  444. }
  445. public bool isWallCollision(Collision c)
  446. {
  447. foreach (ContactPoint col in c)
  448. {
  449. if (Mathf.Abs(transform.position.y - col.point.y) < boxcol.size.y / 2f && Mathf.Abs(transform.position.x - col.point.x) >= boxcol.size.x/2f)
  450. {
  451. //Debug.Log("wall");
  452. return true;
  453. }
  454. }
  455. return false;
  456. }
  457.  
  458. public void OnCollisionEnter(Collision col)
  459. {
  460. if (col.gameObject.tag.Contains("Floor") && anim.IsPlaying("dd"))
  461. {
  462. lastmove = "dd";
  463. moveDelay = 0.8f;
  464. isAttack = false;
  465. punchcount = 0;
  466. }
  467. if (col.gameObject.tag.Contains("Floor"))
  468. {
  469.  
  470. //Debug.Log("AAAAAAAAAAA");
  471. if (isFloorCollision(col))
  472. {
  473. if (col.gameObject.tag.Contains("Solid"))
  474. {
  475. solidfloor = true;
  476. }
  477. isOnFloor = true;
  478. boxcol.isTrigger = false;
  479. }
  480. if (isCeilingCollision(col) || (isWallCollision(col) && transform.position.y < col.gameObject.transform.position.y))
  481. {
  482.  
  483. boxcol.isTrigger = true;
  484. }
  485. }
  486.  
  487. }
  488.  
  489. public void OnCollisionStay(Collision col)
  490. {
  491. if (col.gameObject.tag == "Wall")
  492. {
  493. if (isWallCollision(col) && facing == Mathf.Sign(col.transform.position.x - transform.position.x))
  494. {
  495. speed = 0;
  496. }
  497. }
  498. }
  499.  
  500. public void OnCollisionExit(Collision col)
  501. {
  502.  
  503. if (col.gameObject.tag.Contains("Floor"))
  504. {
  505. isOnFloor = false;
  506. }
  507. }
  508.  
  509. public void OnTriggerEnter(Collider col)
  510. {
  511. if (col.gameObject.tag.Contains("Floor") && !anim.IsPlaying("dd"))
  512. {
  513. transform.position += new Vector3(0, 32, 0);
  514. }
  515. }
  516. public void OnTriggerStay(Collider col)
  517. {
  518. if (col.gameObject.tag.Contains("Floor") && !anim.IsPlaying("dd"))
  519. {
  520. transform.position += new Vector3(0, 32, 0);
  521. }
  522. }
  523. public void OnTriggerExit(Collider col)
  524. {
  525. if (col.gameObject.tag.Contains("Floor"))
  526. {
  527. boxcol.isTrigger = false;
  528. }
  529. }
  530. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement