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.

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