You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

775 lines
28 KiB

5 years ago
5 years ago
5 years ago
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.SceneManagement;
  6. using Networking.Server;
  7. public class Character : MonoBehaviour
  8. {
  9. public enum Animation { Walk, Run, Jump, Sit, Attack, Hit }
  10. public string nextScene;
  11. Animator characterAnimator;
  12. public bool isTuteLevel = false;
  13. public bool inWater = false; //Am I in the water?
  14. public bool inPit = false; //Did I fall into a pit?
  15. public bool stuck = false; //Am I still stuck?
  16. public bool justMoved = false; //Was the logic block I just executed a move command?
  17. Vector3 death = new Vector3(-50, 0, 0);
  18. #region Inspector Fields
  19. [SerializeField]
  20. [Tooltip("Will move to this block at start, else will try and find a block below")]
  21. private Block _currentBlock;
  22. [SerializeField]
  23. [Tooltip("Layers to ignore when checking for blocks")]
  24. public LayerMask Ignore;
  25. [Tooltip("Current Inventory of the player")]
  26. public Inventory Inventory;
  27. public bool CloneInventoryOnStart = false;
  28. [Tooltip("How many lives to start out with")]
  29. public int lives = 3;
  30. [SerializeField]
  31. [Tooltip("Character to display")]
  32. private string CharacterModel = "Bear";
  33. [SerializeField]
  34. public ClientData ClientLink;
  35. [SerializeField]
  36. private TMPro.TextMeshPro BlockTitlePrefab;
  37. #endregion Inspector Fields
  38. #region Read Only
  39. public Block CurrentBlock { get { return _currentBlock; } }
  40. #endregion Read Only
  41. #region Unity Functions
  42. private void Start()
  43. {
  44. if (Inventory != null && CloneInventoryOnStart)
  45. Inventory = Inventory.Clone(Inventory);
  46. //If no starting block find one below it
  47. if (_currentBlock == null)
  48. Block.isBlockAtPosition(transform.position + Vector3.down / 2, 1, ~Ignore, out _currentBlock);
  49. //move to starting block
  50. transform.position = _currentBlock.VisualPosition;
  51. //get character string from player replace from "Bear"
  52. GameObject prefab = Resources.Load(CharacterModel) as GameObject;
  53. GameObject animal = Instantiate(prefab, this.gameObject.transform);
  54. characterAnimator = GetComponentInChildren<Animator>();
  55. }
  56. private void Update()
  57. {
  58. if(lives < 1)
  59. {
  60. this.transform.position = death;
  61. //this.enabled = false;
  62. //gameObject.SetActive(false);
  63. }
  64. }
  65. #endregion Unity Functions
  66. #region Class Implementation
  67. public void Initialise(Block startingBlock, Inventory inventory, string Character)
  68. {
  69. _currentBlock = startingBlock;
  70. Inventory = inventory;
  71. CharacterModel = Character;
  72. }
  73. public void DisplayBlock(LogicBlock block)
  74. {
  75. if (isTuteLevel == false)
  76. {
  77. if (BlockTitlePrefab == null)
  78. return;
  79. TMPro.TextMeshPro temp = Instantiate(BlockTitlePrefab.gameObject).GetComponent<TMPro.TextMeshPro>();
  80. temp.text = block.DisplayName;
  81. temp.color = ClientLink.Color;
  82. temp.transform.position = transform.position + (Vector3.one * 0.25f);
  83. temp.transform.rotation = Quaternion.LookRotation(temp.transform.position - Camera.main.transform.position);
  84. }
  85. }
  86. public IEnumerator MoveToBlock(Block target, Animation animation, float time)
  87. {
  88. float startTime = Time.time;
  89. Vector3 moveDirection = Vector3.ProjectOnPlane(target.position - _currentBlock.position, Vector3.up).normalized;
  90. _currentBlock.OnLeftByPlayer(this);
  91. System.Func<float, float> yFunction = null;
  92. if (animation == Animation.Jump)
  93. yFunction = (t) => Mathf.Sin((Mathf.PI * t));
  94. StartAnimation(animation, time);
  95. yield return StartCoroutine(LerpToBlock(target.VisualPosition, time * 0.8f, yFunction));
  96. _currentBlock= target;
  97. yield return StartCoroutine (_currentBlock.OnWalkedOnByPlayer(this,moveDirection));
  98. yield return new WaitForSeconds(time - (Time.time - startTime));
  99. StopAnimation(animation);
  100. }
  101. public IEnumerator RotateInDirection(Direction direction,float angles, Animation animation, float time)
  102. {
  103. System.Func<float, float> yFunction = null;
  104. if (animation == Animation.Jump)
  105. yFunction = (t) => Mathf.Sin((Mathf.PI * t));
  106. StartAnimation(animation, time);
  107. Debug.Log("Rotating by: " + angles);
  108. yield return StartCoroutine(Rotate(direction,angles, time * 0.8f, yFunction));
  109. StopAnimation(animation);
  110. }
  111. private IEnumerator LerpToBlock(Vector3 target, float time, System.Func<float, float> heightOffset = null)
  112. {
  113. Vector3 _startPos = transform.position;
  114. Vector3 _endPos = target;
  115. Vector3 _newPos;
  116. float elapsedTime = 0;
  117. while (elapsedTime / time < 1)
  118. {
  119. _newPos = Vector3.Lerp(_startPos, _endPos, (elapsedTime / time));
  120. if (heightOffset != null)
  121. _newPos.y += heightOffset(elapsedTime / time);
  122. transform.position = _newPos;
  123. yield return new WaitForEndOfFrame();
  124. elapsedTime += Time.deltaTime;
  125. }
  126. _newPos = _endPos;
  127. if (heightOffset != null)
  128. _newPos.y += heightOffset(1);
  129. transform.position = _newPos;
  130. }
  131. private IEnumerator Rotate(Direction direction, float angles, float time, System.Func<float, float> heightOffset = null)
  132. {
  133. int RotationDir = 0;
  134. switch (direction)
  135. {
  136. case Direction.Forward:
  137. RotationDir = 0;
  138. break;
  139. case Direction.Left:
  140. RotationDir = -1;
  141. break;
  142. case Direction.Right:
  143. RotationDir = 1;
  144. break;
  145. case Direction.Back:
  146. RotationDir = 2;
  147. break;
  148. }
  149. float elapsedTime = 0;
  150. float anglePerSecond = (angles * RotationDir) / time;
  151. Vector3 _startPos = transform.position;
  152. Vector3 startDirection = transform.forward;
  153. while (elapsedTime < time)
  154. {
  155. transform.Rotate(Vector3.up, anglePerSecond * Time.deltaTime);
  156. if (heightOffset != null)
  157. transform.position = _startPos + Vector3.up * heightOffset(elapsedTime / time);
  158. yield return new WaitForEndOfFrame();
  159. elapsedTime += Time.deltaTime;
  160. }
  161. transform.forward = Quaternion.AngleAxis(angles * RotationDir, Vector3.up) * startDirection;
  162. if (heightOffset != null)
  163. transform.position = _startPos + Vector3.up * heightOffset(1);
  164. }
  165. public IEnumerator AnimateToPosition(Vector3 position, Animation animation, float time)
  166. {
  167. System.Func<float, float> yFunction = null;
  168. if (animation == Animation.Jump)
  169. yFunction = (t) => Mathf.Sin((Mathf.PI * t));
  170. StartAnimation(animation, time);
  171. yield return StartCoroutine(LerpToBlock(position, time * 0.8f, yFunction));
  172. StopAnimation(animation);
  173. }
  174. public void StartAnimation(Animation animation,float speed = 1)
  175. {
  176. characterAnimator.SetFloat("AnimationSpeed", (1 / speed));
  177. switch (animation)
  178. {
  179. case Animation.Walk:
  180. characterAnimator.SetBool("isWalking", true);
  181. break;
  182. case Animation.Run:
  183. characterAnimator.SetBool("isRunning", true);
  184. break;
  185. case Animation.Sit:
  186. characterAnimator.SetBool("isSitting", true);
  187. break;
  188. case Animation.Jump:
  189. characterAnimator.SetTrigger("Jump");
  190. break;
  191. case Animation.Attack:
  192. characterAnimator.SetTrigger("Attack");
  193. break;
  194. case Animation.Hit:
  195. characterAnimator.SetTrigger("Hit");
  196. break;
  197. default:
  198. break;
  199. }
  200. }
  201. public void StopAnimation(Animation animation)
  202. {
  203. characterAnimator.SetFloat("AnimationSpeed", 1f);
  204. switch (animation)
  205. {
  206. case Animation.Walk:
  207. characterAnimator.SetBool("isWalking", false);
  208. break;
  209. case Animation.Run:
  210. characterAnimator.SetBool("isRunning", false);
  211. break;
  212. case Animation.Sit:
  213. characterAnimator.SetBool("isSitting", false);
  214. break;
  215. }
  216. }
  217. public void respawnCharacter()
  218. {
  219. /* Will introduce more complex criteria for choosing where to respawn the player in future
  220. * For now: if the square one back (x =- 1) from the pit is walkable and unoccupied, then put them there
  221. * Otherwise, try the next square back, etc, until we find an available one
  222. */
  223. Block currentBlock = null;
  224. //We start from the position of our pit, at ground level
  225. Vector3 currentPos = new Vector3(this.transform.position.x, this.transform.position.y, this.transform.position.z);
  226. Debug.Log("Commencing respawn");
  227. //Hardcoding the number of iterations for now for simplicity, should change this later
  228. for (int i = 0; i < 100; i++)
  229. {
  230. //First we check one back
  231. currentPos.x -= 1;
  232. Debug.Log("currentPos = " + currentPos.x + ", " + currentPos.y + ", " + currentPos.z);
  233. if (Block.isBlockAtPosition(currentPos, 1, ~Ignore, out currentBlock)) //Does a block exist here?
  234. {
  235. Debug.Log("Block exists");
  236. if (currentBlock.isWalkable(~Ignore)) //If so, can we walk on it?
  237. {
  238. Debug.Log("Block is walkable");
  239. //Don't yet have a check for whether it's occupied
  240. break; //If it is, we stop here
  241. }
  242. }
  243. //If the block one back isn't an option, we check to the left and right
  244. currentPos.z += 1;
  245. Debug.Log("currentPos = " + currentPos.x + ", " + currentPos.y + ", " + currentPos.z);
  246. if (Block.isBlockAtPosition(currentPos, 1, ~Ignore, out currentBlock)) //Does a block exist here?
  247. {
  248. Debug.Log("Block exists");
  249. if (currentBlock.isWalkable(~Ignore)) //If so, can we walk on it?
  250. {
  251. Debug.Log("Block is walkable");
  252. //Don't yet have a check for whether it's occupied
  253. break; //If it is, we stop here
  254. }
  255. }
  256. currentPos.z -= 2;
  257. Debug.Log("currentPos = " + currentPos.x + ", " + currentPos.y + ", " + currentPos.z);
  258. if (Block.isBlockAtPosition(currentPos, 1, ~Ignore, out currentBlock)) //Does a block exist here?
  259. {
  260. Debug.Log("Block exists");
  261. if (currentBlock.isWalkable(~Ignore)) //If so, can we walk on it?
  262. {
  263. Debug.Log("Block is walkable");
  264. //Don't yet have a check for whether it's occupied
  265. break; //If it is, we stop here
  266. }
  267. }
  268. //If we've gotten this far and haven't found an available spot, we move back a row and try again
  269. currentPos.z += 1;
  270. }
  271. //Having found our target block, we move the character there
  272. if (currentBlock != null)
  273. {
  274. this.transform.position = currentBlock.VisualPosition;
  275. this.inPit = false;
  276. this._currentBlock = currentBlock;
  277. Debug.Log("Moved " + this.name + " to "
  278. + this.transform.position.x + ", "
  279. + this.transform.position.y + ", "
  280. + this.transform.position.z + ", "
  281. + " inPit = " + inPit);
  282. }
  283. else
  284. {
  285. Debug.Log("Failed to find anywhere to put " + this.name);
  286. }
  287. }
  288. /// <summary>
  289. /// Upon collision with a floating block, collect its
  290. /// Upon collision with the end portal, end of level
  291. /// </summary>
  292. /// <param name="other">name of collided object</param>
  293. void OnTriggerEnter(Collider other)
  294. {
  295. Collectable collectable = other.GetComponentInChildren<Collectable>();
  296. if (collectable != null)
  297. {
  298. collectable.OnCollect(this);
  299. }
  300. if (other.gameObject.name == "collect_sphere")
  301. {
  302. other.gameObject.SetActive(false);
  303. //player.collected +=1;
  304. }
  305. if (other.gameObject.name == "End Portal")
  306. {
  307. other.GetComponent<Collider>().enabled = false;
  308. SceneManager.LoadScene(nextScene);
  309. }
  310. }
  311. /// <summary>
  312. /// Rotates to point in specific direction based on current direction
  313. /// </summary>
  314. /// <param name="direction">Local direction to point</param>
  315. public void Rotate(Direction direction, float speed)
  316. {
  317. StartCoroutine(rotateCoroutine(direction, transform, speed, 0.15f));
  318. //transform.forward = direction.ToVector(transform);
  319. }
  320. public void RotateHalf(Direction direction, float speed)
  321. {
  322. StartCoroutine(rotateHalfCoroutine(direction, transform, speed, 0.15f));
  323. //transform.forward = direction.ToVector(transform);
  324. }
  325. /// <summary>
  326. /// Jumps in specefied direction, picks between Long Jump and Jumping up
  327. /// </summary>
  328. /// <param name="direction">Direction to Jump</param>
  329. public void Jump(Direction direction, float speed)
  330. {
  331. //if there is a block infront JumpUp else LongJump
  332. if (Block.isBlockAtPosition(_currentBlock.position + direction.ToVector(transform) + Vector3.up, 1, ~Ignore))
  333. JumpUp(direction, speed);
  334. else
  335. JumpLong(direction, speed);
  336. }
  337. #endregion Class Implementation
  338. #region Private Functions
  339. /// <summary>
  340. /// Jumps up obstacle of specific height. Jumps as high as possible
  341. /// </summary>
  342. /// <param name="direction">Direction of obstacle</param>
  343. /// <param name="height">max height</param>
  344. private void JumpUp(Direction direction, float speed, int height = 1)
  345. {
  346. //setting up variables
  347. Vector3 position; // position wanted
  348. Block hit; //output of block detection
  349. Block moveTo = _currentBlock; //block we'll actually move to
  350. //Check blocks in going up then move to the heighest walkable one
  351. for (int i = 0; i <= height; i++)
  352. {
  353. //next position up the tower
  354. position = _currentBlock.position + direction.ToVector(transform) + (Vector3.up * i);
  355. //if block is walkable set it to last known position
  356. if (Block.isBlockAtPosition(position, 1, ~Ignore, out hit) && hit.isWalkable(~Ignore))
  357. moveTo = hit;
  358. }
  359. //set current block && move
  360. _currentBlock = moveTo;
  361. StartCoroutine(JumpCoroutine(_currentBlock, transform, speed, 0.5f));
  362. //transform.position = CurrentBlock.VisualPosition;
  363. }
  364. /// <summary>
  365. /// Long jumps forward a specified distance. Can Jump gaps. Stops at obstruction
  366. /// </summary>
  367. /// <param name="direction">Direction to Jump</param>
  368. /// <param name="Distance">Max distance</param>
  369. private void JumpLong(Direction direction, float speed, int Distance = 2)
  370. {
  371. //setting up variables
  372. Vector3 position; // position wanted
  373. Block hit; //output of block detection
  374. Block moveTo = _currentBlock; //block we'll actually move to
  375. //Check blocks in front until we hit an obstruction or went the distance
  376. for (int i = 1; i <= Distance; i++)
  377. {
  378. //Next position to MoveTo
  379. position = _currentBlock.position + (direction.ToVector(transform) * i);
  380. //if jump is obstructed, stop and go to last known block
  381. if (Block.isBlockAtPosition(position + Vector3.up, 1, ~Ignore))
  382. break;
  383. //If block at Position is walkable set it to last known position
  384. if (Block.isBlockAtPosition(position, 1, ~Ignore, out hit) && hit.isWalkable(~Ignore))
  385. moveTo = hit;
  386. //else if block down one is walkable
  387. else if (Block.isBlockAtPosition(position + Vector3.down, 1, ~Ignore, out hit) && hit.isWalkable(~Ignore))
  388. moveTo = hit;
  389. }//end for
  390. //Set Current Block and move
  391. _currentBlock = moveTo;
  392. StartCoroutine(JumpCoroutine(_currentBlock, transform, speed, 0.5f));
  393. //transform.position = CurrentBlock.VisualPosition;
  394. }
  395. #endregion Private Functions
  396. #region Depricated
  397. IEnumerator JumpCoroutine(Block Target, Transform Current, float time, float heightMax)
  398. {
  399. float elapsedTime = 0;
  400. Vector3 startPosition = Current.position;
  401. time *= 0.8f;
  402. characterAnimator.Play("Jump Inplace");
  403. yield return new WaitForSeconds(0.15f);
  404. while (elapsedTime < time)
  405. {
  406. transform.position = Vector3.Lerp(startPosition, Target.VisualPosition, (elapsedTime / time));
  407. //transform.position += yValue((elapsedTime / time), heightMax);
  408. yield return new WaitForEndOfFrame();
  409. elapsedTime += Time.deltaTime;
  410. }
  411. transform.position = Target.VisualPosition;
  412. }
  413. IEnumerator MoveCoroutine(Block Target, Transform Current, float time, float heightMax)
  414. {
  415. float elapsedTime = 0;
  416. Vector3 startPosition = Current.position;
  417. time *= 0.8f;
  418. characterAnimator.Play("Walk");
  419. yield return new WaitForSeconds(0.05f);
  420. while (elapsedTime < time)
  421. {
  422. transform.position = Vector3.Lerp(startPosition, Target.VisualPosition, (elapsedTime / time));
  423. yield return new WaitForEndOfFrame();
  424. elapsedTime += Time.deltaTime;
  425. }
  426. transform.position = Target.VisualPosition;
  427. }
  428. IEnumerator MoveConveyorForwardCoroutine(Block Target, Transform Current, float time, float heightMax)
  429. {
  430. float elapsedTime = 0;
  431. Vector3 startPosition = Current.position;
  432. Vector3 charEndPosition = new Vector3(Current.position.x + 1.0f, Current.position.y, Current.position.z);
  433. time *= 0.8f;
  434. yield return new WaitForSeconds(0.05f);
  435. while (elapsedTime < time)
  436. {
  437. Current.position = Vector3.Lerp(startPosition, charEndPosition, (elapsedTime / time));
  438. yield return new WaitForEndOfFrame();
  439. elapsedTime += Time.deltaTime;
  440. }
  441. Current.position = charEndPosition;
  442. }
  443. IEnumerator MoveConveyorBackwardCoroutine(Block Target, Transform Current, float time, float heightMax)
  444. {
  445. float elapsedTime = 0;
  446. Vector3 startPosition = Current.position;
  447. Vector3 charEndPosition = new Vector3(Current.position.x - 1.0f, Current.position.y, Current.position.z);
  448. time *= 0.8f;
  449. yield return new WaitForSeconds(0.05f);
  450. while (elapsedTime < time)
  451. {
  452. Current.position = Vector3.Lerp(startPosition, charEndPosition, (elapsedTime / time));
  453. yield return new WaitForEndOfFrame();
  454. elapsedTime += Time.deltaTime;
  455. }
  456. Current.position = charEndPosition;
  457. }
  458. IEnumerator MoveConveyorLeftCoroutine(Block Target, Transform Current, float time, float heightMax)
  459. {
  460. float elapsedTime = 0;
  461. Vector3 startPosition = Current.position;
  462. Vector3 charEndPosition = new Vector3(Current.position.x, Current.position.y, Current.position.z + 1.0f);
  463. time *= 0.8f;
  464. yield return new WaitForSeconds(0.05f);
  465. while (elapsedTime < time)
  466. {
  467. Current.position = Vector3.Lerp(startPosition, charEndPosition, (elapsedTime / time));
  468. yield return new WaitForEndOfFrame();
  469. elapsedTime += Time.deltaTime;
  470. }
  471. Current.position = charEndPosition;
  472. }
  473. IEnumerator MoveConveyorRightCoroutine(Block Target, Transform Current, float time, float heightMax)
  474. {
  475. float elapsedTime = 0;
  476. Vector3 startPosition = Current.position;
  477. Vector3 charEndPosition = new Vector3(Current.position.x, Current.position.y, Current.position.z - 1.0f);
  478. time *= 0.8f;
  479. yield return new WaitForSeconds(0.05f);
  480. while (elapsedTime < time)
  481. {
  482. Current.position = Vector3.Lerp(startPosition, charEndPosition, (elapsedTime / time));
  483. yield return new WaitForEndOfFrame();
  484. elapsedTime += Time.deltaTime;
  485. }
  486. Current.position = charEndPosition;
  487. }
  488. IEnumerator PushLeftCoroutine(Transform Current, float time)
  489. {
  490. float elapsedTime = 0;
  491. Vector3 startPosition = Current.transform.position;
  492. Vector3 endPosition = new Vector3(Current.position.x, Current.position.y, Current.position.z + 1.0f);
  493. time *= 0.8f;
  494. yield return new WaitForSeconds(0.05f);
  495. while (elapsedTime < time)
  496. {
  497. Current.position = Vector3.Lerp(startPosition, endPosition, (elapsedTime / time));
  498. yield return new WaitForEndOfFrame();
  499. elapsedTime += Time.deltaTime;
  500. }
  501. Current.position = endPosition;
  502. }
  503. IEnumerator PushRightCoroutine(Transform Current, float time)
  504. {
  505. float elapsedTime = 0;
  506. Vector3 startPosition = Current.transform.position;
  507. Vector3 endPosition = new Vector3(Current.position.x, Current.position.y, Current.position.z - 1.0f);
  508. time *= 0.8f;
  509. yield return new WaitForSeconds(0.05f);
  510. while (elapsedTime < time)
  511. {
  512. Current.position = Vector3.Lerp(startPosition, endPosition, (elapsedTime / time));
  513. yield return new WaitForEndOfFrame();
  514. elapsedTime += Time.deltaTime;
  515. }
  516. Current.position = endPosition;
  517. }
  518. IEnumerator MoveDownCoroutine(Block Target, Transform Current, float time, float heightMax)
  519. {
  520. float elapsedTime = 0;
  521. Vector3 startPosition = Current.position;
  522. time *= 0.8f;
  523. characterAnimator.Play("Walk");
  524. while (elapsedTime < time)
  525. {
  526. transform.position = Vector3.Lerp(startPosition, Target.VisualPosition, (elapsedTime / time));
  527. //transform.position += yValue((elapsedTime / time), heightMax);
  528. yield return new WaitForEndOfFrame();
  529. elapsedTime += Time.deltaTime;
  530. }
  531. transform.position = Target.VisualPosition;
  532. }
  533. IEnumerator rotateCoroutine(Direction direction, Transform Current, float time, float heightMax)
  534. {
  535. float elapsedTime = 0;
  536. time *= 0.8f;
  537. Vector3 endDirection = direction.ToVector(Current);
  538. Vector3 startDirection = Current.forward;
  539. while (elapsedTime < time)
  540. {
  541. characterAnimator.Play("Jump");
  542. transform.forward = Vector3.Slerp(startDirection, endDirection, (elapsedTime / time));
  543. yield return new WaitForEndOfFrame();
  544. elapsedTime += Time.deltaTime;
  545. }
  546. transform.forward = endDirection;
  547. }
  548. IEnumerator rotateHalfCoroutine(Direction direction, Transform Current, float time, float heightMax)
  549. {
  550. float elapsedTime = 0;
  551. time *= 0.8f;
  552. Vector3 endDirection = (direction.ToVector(Current) + Current.forward).normalized;
  553. Vector3 startDirection = Current.forward;
  554. while (elapsedTime < time)
  555. {
  556. characterAnimator.Play("Jump");
  557. transform.forward = Vector3.Slerp(startDirection, endDirection, (elapsedTime / time));
  558. yield return new WaitForEndOfFrame();
  559. elapsedTime += Time.deltaTime;
  560. }
  561. transform.forward = endDirection;
  562. }
  563. /// <summary>
  564. /// Moves one block in specefied direction, Can walk off obstacles
  565. /// </summary>
  566. /// <param name="direction">direction to walk</param>
  567. /// <remarks>Technically is same as JumpLong(1) but kept seperate to avoid confusion</remarks>
  568. public void Move(Direction direction, float speed)
  569. {
  570. //setting up variables
  571. Vector3 position = _currentBlock.position + direction.ToVector(transform); // position wanted
  572. Block hit; //output of block detection
  573. Block moveTo = _currentBlock; //block we'll actually move to
  574. //if move is obstucted no where to move
  575. if (Block.isBlockAtPosition(position + Vector3.up, 1, ~Ignore))
  576. return;
  577. //If block at Position is walkable set it to moveTo
  578. if (Block.isBlockAtPosition(position, 1, ~Ignore, out hit) && hit.isWalkable(~Ignore))
  579. {
  580. moveTo = hit;
  581. _currentBlock = moveTo;
  582. StartCoroutine(MoveCoroutine(_currentBlock, transform, speed, 0.3f));
  583. }
  584. //else if block down one is walkable
  585. else if (Block.isBlockAtPosition(position + Vector3.down, 1, ~Ignore, out hit) && hit.isWalkable(~Ignore))
  586. {
  587. moveTo = hit;
  588. _currentBlock = moveTo;
  589. StartCoroutine(MoveDownCoroutine(_currentBlock, transform, speed, 0.3f));
  590. }
  591. }
  592. public void conveyorMoveForward(Direction direction, float speed)
  593. {
  594. Vector3 position = _currentBlock.position + direction.ToVector(transform); // position wanted
  595. Block hit; //output of block detection
  596. Block moveTo = _currentBlock; //block we'll actually move to
  597. if (Block.isBlockAtPosition(position, 1, ~Ignore, out hit) && hit.isWalkable(~Ignore))
  598. {
  599. moveTo = hit;
  600. _currentBlock = moveTo;
  601. StartCoroutine(MoveConveyorForwardCoroutine(_currentBlock, transform, speed, 0.3f));
  602. }
  603. }
  604. public void conveyorMoveBackward(Direction direction, float speed)
  605. {
  606. Vector3 position = _currentBlock.position + direction.ToVector(transform); // position wanted
  607. Block hit; //output of block detection
  608. Block moveTo = _currentBlock; //block we'll actually move to
  609. if (Block.isBlockAtPosition(position, 1, ~Ignore, out hit) && hit.isWalkable(~Ignore))
  610. {
  611. moveTo = hit;
  612. _currentBlock = moveTo;
  613. StartCoroutine(MoveConveyorBackwardCoroutine(_currentBlock, transform, speed, 0.3f));
  614. }
  615. }
  616. public void conveyorMoveLeft(Direction direction, float speed)
  617. {
  618. Vector3 position = _currentBlock.position + direction.ToVector(transform); // position wanted
  619. Block hit; //output of block detection
  620. Block moveTo = _currentBlock; //block we'll actually move to
  621. if (Block.isBlockAtPosition(position, 1, ~Ignore, out hit) && hit.isWalkable(~Ignore))
  622. {
  623. moveTo = hit;
  624. _currentBlock = moveTo;
  625. StartCoroutine(MoveConveyorLeftCoroutine(_currentBlock, transform, speed, 0.3f));
  626. }
  627. }
  628. public void conveyorMoveRight(Direction direction, float speed)
  629. {
  630. Vector3 position = _currentBlock.position + direction.ToVector(transform); // position wanted
  631. Block hit; //output of block detection
  632. Block moveTo = _currentBlock; //block we'll actually move to
  633. if (Block.isBlockAtPosition(position, 1, ~Ignore, out hit) && hit.isWalkable(~Ignore))
  634. {
  635. moveTo = hit;
  636. _currentBlock = moveTo;
  637. StartCoroutine(MoveConveyorRightCoroutine(_currentBlock, transform, speed, 0.3f));
  638. }
  639. }
  640. public void CannonRMove(float speed)
  641. {
  642. Direction direction = Direction.Right;
  643. Vector3 position = _currentBlock.position + direction.ToVector(transform); // position wanted
  644. Block hit; //output of block detection
  645. Block moveTo = _currentBlock; //block we'll actually move to
  646. if (Block.isBlockAtPosition(position, 1, ~Ignore, out hit) && hit.isWalkable(~Ignore))
  647. {
  648. moveTo = hit;
  649. _currentBlock = moveTo;
  650. StartCoroutine(PushRightCoroutine(transform, speed));
  651. }
  652. }
  653. public void CannonLMove(float speed)
  654. {
  655. Direction direction = Direction.Left;
  656. Vector3 position = _currentBlock.position + direction.ToVector(transform); // position wanted
  657. Block hit; //output of block detection
  658. Block moveTo = _currentBlock; //block we'll actually move to
  659. if (Block.isBlockAtPosition(position, 1, ~Ignore, out hit) && hit.isWalkable(~Ignore))
  660. {
  661. moveTo = hit;
  662. _currentBlock = moveTo;
  663. StartCoroutine(PushLeftCoroutine(transform, speed));
  664. }
  665. }
  666. #endregion Depricated
  667. }