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.

776 lines
28 KiB

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