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.

777 lines
29 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. blockSpawn spawn = GetComponent<blockSpawn>();
  301. if (collectable != null)
  302. {
  303. //get position from average;
  304. ClientList list = spawn.clientDataList;
  305. float average = 0;
  306. int livePlayerCount = 0;
  307. foreach (ClientData data in list.ConnectedClients)
  308. {
  309. if(data.Lives > 0)
  310. {
  311. average += data.playerCharacter.transform.position.x;
  312. livePlayerCount++;
  313. }
  314. }
  315. average /= livePlayerCount;
  316. float tosend = lives + (transform.position.x - average);
  317. spawn.assignLogicBlock(collectable.gameObject, tosend);
  318. collectable.OnCollect(this);
  319. }
  320. }
  321. /// <summary>
  322. /// Rotates to point in specific direction based on current direction
  323. /// </summary>
  324. /// <param name="direction">Local direction to point</param>
  325. public void Rotate(Direction direction, float speed)
  326. {
  327. StartCoroutine(rotateCoroutine(direction, transform, speed, 0.15f));
  328. //transform.forward = direction.ToVector(transform);
  329. }
  330. public void RotateHalf(Direction direction, float speed)
  331. {
  332. StartCoroutine(rotateHalfCoroutine(direction, transform, speed, 0.15f));
  333. //transform.forward = direction.ToVector(transform);
  334. }
  335. /// <summary>
  336. /// Jumps in specefied direction, picks between Long Jump and Jumping up
  337. /// </summary>
  338. /// <param name="direction">Direction to Jump</param>
  339. public void Jump(Direction direction, float speed)
  340. {
  341. //if there is a block infront JumpUp else LongJump
  342. if (Block.isBlockAtPosition(_currentBlock.position + direction.ToVector(transform) + Vector3.up, 1, ~Ignore))
  343. JumpUp(direction, speed);
  344. else
  345. JumpLong(direction, speed);
  346. }
  347. #endregion Class Implementation
  348. #region Private Functions
  349. /// <summary>
  350. /// Jumps up obstacle of specific height. Jumps as high as possible
  351. /// </summary>
  352. /// <param name="direction">Direction of obstacle</param>
  353. /// <param name="height">max height</param>
  354. private void JumpUp(Direction direction, float speed, int height = 1)
  355. {
  356. //setting up variables
  357. Vector3 position; // position wanted
  358. Block hit; //output of block detection
  359. Block moveTo = _currentBlock; //block we'll actually move to
  360. //Check blocks in going up then move to the heighest walkable one
  361. for (int i = 0; i <= height; i++)
  362. {
  363. //next position up the tower
  364. position = _currentBlock.position + direction.ToVector(transform) + (Vector3.up * i);
  365. //if block is walkable set it to last known position
  366. if (Block.isBlockAtPosition(position, 1, ~Ignore, out hit) && hit.isWalkable(~Ignore))
  367. moveTo = hit;
  368. }
  369. //set current block && move
  370. _currentBlock = moveTo;
  371. StartCoroutine(JumpCoroutine(_currentBlock, transform, speed, 0.5f));
  372. //transform.position = CurrentBlock.VisualPosition;
  373. }
  374. /// <summary>
  375. /// Long jumps forward a specified distance. Can Jump gaps. Stops at obstruction
  376. /// </summary>
  377. /// <param name="direction">Direction to Jump</param>
  378. /// <param name="Distance">Max distance</param>
  379. private void JumpLong(Direction direction, float speed, int Distance = 2)
  380. {
  381. //setting up variables
  382. Vector3 position; // position wanted
  383. Block hit; //output of block detection
  384. Block moveTo = _currentBlock; //block we'll actually move to
  385. //Check blocks in front until we hit an obstruction or went the distance
  386. for (int i = 1; i <= Distance; i++)
  387. {
  388. //Next position to MoveTo
  389. position = _currentBlock.position + (direction.ToVector(transform) * i);
  390. //if jump is obstructed, stop and go to last known block
  391. if (Block.isBlockAtPosition(position + Vector3.up, 1, ~Ignore))
  392. break;
  393. //If block at Position is walkable set it to last known position
  394. if (Block.isBlockAtPosition(position, 1, ~Ignore, out hit) && hit.isWalkable(~Ignore))
  395. moveTo = hit;
  396. //else if block down one is walkable
  397. else if (Block.isBlockAtPosition(position + Vector3.down, 1, ~Ignore, out hit) && hit.isWalkable(~Ignore))
  398. moveTo = hit;
  399. }//end for
  400. //Set Current Block and move
  401. _currentBlock = moveTo;
  402. StartCoroutine(JumpCoroutine(_currentBlock, transform, speed, 0.5f));
  403. //transform.position = CurrentBlock.VisualPosition;
  404. }
  405. #endregion Private Functions
  406. #region Depricated
  407. IEnumerator JumpCoroutine(Block Target, Transform Current, float time, float heightMax)
  408. {
  409. float elapsedTime = 0;
  410. Vector3 startPosition = Current.position;
  411. time *= 0.8f;
  412. characterAnimator.Play("Jump Inplace");
  413. yield return new WaitForSeconds(0.15f);
  414. while (elapsedTime < time)
  415. {
  416. transform.position = Vector3.Lerp(startPosition, Target.VisualPosition, (elapsedTime / time));
  417. //transform.position += yValue((elapsedTime / time), heightMax);
  418. yield return new WaitForEndOfFrame();
  419. elapsedTime += Time.deltaTime;
  420. }
  421. transform.position = Target.VisualPosition;
  422. }
  423. IEnumerator MoveCoroutine(Block Target, Transform Current, float time, float heightMax)
  424. {
  425. float elapsedTime = 0;
  426. Vector3 startPosition = Current.position;
  427. time *= 0.8f;
  428. characterAnimator.Play("Walk");
  429. yield return new WaitForSeconds(0.05f);
  430. while (elapsedTime < time)
  431. {
  432. transform.position = Vector3.Lerp(startPosition, Target.VisualPosition, (elapsedTime / time));
  433. yield return new WaitForEndOfFrame();
  434. elapsedTime += Time.deltaTime;
  435. }
  436. transform.position = Target.VisualPosition;
  437. }
  438. IEnumerator MoveConveyorForwardCoroutine(Block Target, Transform Current, float time, float heightMax)
  439. {
  440. float elapsedTime = 0;
  441. Vector3 startPosition = Current.position;
  442. Vector3 charEndPosition = new Vector3(Current.position.x + 1.0f, Current.position.y, Current.position.z);
  443. time *= 0.8f;
  444. yield return new WaitForSeconds(0.05f);
  445. while (elapsedTime < time)
  446. {
  447. Current.position = Vector3.Lerp(startPosition, charEndPosition, (elapsedTime / time));
  448. yield return new WaitForEndOfFrame();
  449. elapsedTime += Time.deltaTime;
  450. }
  451. Current.position = charEndPosition;
  452. }
  453. IEnumerator MoveConveyorBackwardCoroutine(Block Target, Transform Current, float time, float heightMax)
  454. {
  455. float elapsedTime = 0;
  456. Vector3 startPosition = Current.position;
  457. Vector3 charEndPosition = new Vector3(Current.position.x - 1.0f, Current.position.y, Current.position.z);
  458. time *= 0.8f;
  459. yield return new WaitForSeconds(0.05f);
  460. while (elapsedTime < time)
  461. {
  462. Current.position = Vector3.Lerp(startPosition, charEndPosition, (elapsedTime / time));
  463. yield return new WaitForEndOfFrame();
  464. elapsedTime += Time.deltaTime;
  465. }
  466. Current.position = charEndPosition;
  467. }
  468. IEnumerator MoveConveyorLeftCoroutine(Block Target, Transform Current, float time, float heightMax)
  469. {
  470. float elapsedTime = 0;
  471. Vector3 startPosition = Current.position;
  472. Vector3 charEndPosition = new Vector3(Current.position.x, Current.position.y, Current.position.z + 1.0f);
  473. time *= 0.8f;
  474. yield return new WaitForSeconds(0.05f);
  475. while (elapsedTime < time)
  476. {
  477. Current.position = Vector3.Lerp(startPosition, charEndPosition, (elapsedTime / time));
  478. yield return new WaitForEndOfFrame();
  479. elapsedTime += Time.deltaTime;
  480. }
  481. Current.position = charEndPosition;
  482. }
  483. IEnumerator MoveConveyorRightCoroutine(Block Target, Transform Current, float time, float heightMax)
  484. {
  485. float elapsedTime = 0;
  486. Vector3 startPosition = Current.position;
  487. Vector3 charEndPosition = new Vector3(Current.position.x, Current.position.y, Current.position.z - 1.0f);
  488. time *= 0.8f;
  489. yield return new WaitForSeconds(0.05f);
  490. while (elapsedTime < time)
  491. {
  492. Current.position = Vector3.Lerp(startPosition, charEndPosition, (elapsedTime / time));
  493. yield return new WaitForEndOfFrame();
  494. elapsedTime += Time.deltaTime;
  495. }
  496. Current.position = charEndPosition;
  497. }
  498. IEnumerator PushLeftCoroutine(Transform Current, float time)
  499. {
  500. float elapsedTime = 0;
  501. Vector3 startPosition = Current.transform.position;
  502. Vector3 endPosition = new Vector3(Current.position.x, Current.position.y, Current.position.z + 1.0f);
  503. time *= 0.8f;
  504. yield return new WaitForSeconds(0.05f);
  505. while (elapsedTime < time)
  506. {
  507. Current.position = Vector3.Lerp(startPosition, endPosition, (elapsedTime / time));
  508. yield return new WaitForEndOfFrame();
  509. elapsedTime += Time.deltaTime;
  510. }
  511. Current.position = endPosition;
  512. }
  513. IEnumerator PushRightCoroutine(Transform Current, float time)
  514. {
  515. float elapsedTime = 0;
  516. Vector3 startPosition = Current.transform.position;
  517. Vector3 endPosition = new Vector3(Current.position.x, Current.position.y, Current.position.z - 1.0f);
  518. time *= 0.8f;
  519. yield return new WaitForSeconds(0.05f);
  520. while (elapsedTime < time)
  521. {
  522. Current.position = Vector3.Lerp(startPosition, endPosition, (elapsedTime / time));
  523. yield return new WaitForEndOfFrame();
  524. elapsedTime += Time.deltaTime;
  525. }
  526. Current.position = endPosition;
  527. }
  528. IEnumerator MoveDownCoroutine(Block Target, Transform Current, float time, float heightMax)
  529. {
  530. float elapsedTime = 0;
  531. Vector3 startPosition = Current.position;
  532. time *= 0.8f;
  533. characterAnimator.Play("Walk");
  534. while (elapsedTime < time)
  535. {
  536. transform.position = Vector3.Lerp(startPosition, Target.VisualPosition, (elapsedTime / time));
  537. //transform.position += yValue((elapsedTime / time), heightMax);
  538. yield return new WaitForEndOfFrame();
  539. elapsedTime += Time.deltaTime;
  540. }
  541. transform.position = Target.VisualPosition;
  542. }
  543. IEnumerator rotateCoroutine(Direction direction, Transform Current, float time, float heightMax)
  544. {
  545. float elapsedTime = 0;
  546. time *= 0.8f;
  547. Vector3 endDirection = direction.ToVector(Current);
  548. Vector3 startDirection = Current.forward;
  549. while (elapsedTime < time)
  550. {
  551. characterAnimator.Play("Jump");
  552. transform.forward = Vector3.Slerp(startDirection, endDirection, (elapsedTime / time));
  553. yield return new WaitForEndOfFrame();
  554. elapsedTime += Time.deltaTime;
  555. }
  556. transform.forward = endDirection;
  557. }
  558. IEnumerator rotateHalfCoroutine(Direction direction, Transform Current, float time, float heightMax)
  559. {
  560. float elapsedTime = 0;
  561. time *= 0.8f;
  562. Vector3 endDirection = (direction.ToVector(Current) + Current.forward).normalized;
  563. Vector3 startDirection = Current.forward;
  564. while (elapsedTime < time)
  565. {
  566. characterAnimator.Play("Jump");
  567. transform.forward = Vector3.Slerp(startDirection, endDirection, (elapsedTime / time));
  568. yield return new WaitForEndOfFrame();
  569. elapsedTime += Time.deltaTime;
  570. }
  571. transform.forward = endDirection;
  572. }
  573. /// <summary>
  574. /// Moves one block in specefied direction, Can walk off obstacles
  575. /// </summary>
  576. /// <param name="direction">direction to walk</param>
  577. /// <remarks>Technically is same as JumpLong(1) but kept seperate to avoid confusion</remarks>
  578. public void Move(Direction direction, float speed)
  579. {
  580. //setting up variables
  581. Vector3 position = _currentBlock.position + direction.ToVector(transform); // position wanted
  582. Block hit; //output of block detection
  583. Block moveTo = _currentBlock; //block we'll actually move to
  584. //if move is obstucted no where to move
  585. if (Block.isBlockAtPosition(position + Vector3.up, 1, ~Ignore))
  586. return;
  587. //If block at Position is walkable set it to moveTo
  588. if (Block.isBlockAtPosition(position, 1, ~Ignore, out hit) && hit.isWalkable(~Ignore))
  589. {
  590. moveTo = hit;
  591. _currentBlock = moveTo;
  592. StartCoroutine(MoveCoroutine(_currentBlock, transform, speed, 0.3f));
  593. }
  594. //else if block down one is walkable
  595. else if (Block.isBlockAtPosition(position + Vector3.down, 1, ~Ignore, out hit) && hit.isWalkable(~Ignore))
  596. {
  597. moveTo = hit;
  598. _currentBlock = moveTo;
  599. StartCoroutine(MoveDownCoroutine(_currentBlock, transform, speed, 0.3f));
  600. }
  601. }
  602. public void conveyorMoveForward(Direction direction, float speed)
  603. {
  604. Vector3 position = _currentBlock.position + direction.ToVector(transform); // position wanted
  605. Block hit; //output of block detection
  606. Block moveTo = _currentBlock; //block we'll actually move to
  607. if (Block.isBlockAtPosition(position, 1, ~Ignore, out hit) && hit.isWalkable(~Ignore))
  608. {
  609. moveTo = hit;
  610. _currentBlock = moveTo;
  611. StartCoroutine(MoveConveyorForwardCoroutine(_currentBlock, transform, speed, 0.3f));
  612. }
  613. }
  614. public void conveyorMoveBackward(Direction direction, float speed)
  615. {
  616. Vector3 position = _currentBlock.position + direction.ToVector(transform); // position wanted
  617. Block hit; //output of block detection
  618. Block moveTo = _currentBlock; //block we'll actually move to
  619. if (Block.isBlockAtPosition(position, 1, ~Ignore, out hit) && hit.isWalkable(~Ignore))
  620. {
  621. moveTo = hit;
  622. _currentBlock = moveTo;
  623. StartCoroutine(MoveConveyorBackwardCoroutine(_currentBlock, transform, speed, 0.3f));
  624. }
  625. }
  626. public void conveyorMoveLeft(Direction direction, float speed)
  627. {
  628. Vector3 position = _currentBlock.position + direction.ToVector(transform); // position wanted
  629. Block hit; //output of block detection
  630. Block moveTo = _currentBlock; //block we'll actually move to
  631. if (Block.isBlockAtPosition(position, 1, ~Ignore, out hit) && hit.isWalkable(~Ignore))
  632. {
  633. moveTo = hit;
  634. _currentBlock = moveTo;
  635. StartCoroutine(MoveConveyorLeftCoroutine(_currentBlock, transform, speed, 0.3f));
  636. }
  637. }
  638. public void conveyorMoveRight(Direction direction, float speed)
  639. {
  640. Vector3 position = _currentBlock.position + direction.ToVector(transform); // position wanted
  641. Block hit; //output of block detection
  642. Block moveTo = _currentBlock; //block we'll actually move to
  643. if (Block.isBlockAtPosition(position, 1, ~Ignore, out hit) && hit.isWalkable(~Ignore))
  644. {
  645. moveTo = hit;
  646. _currentBlock = moveTo;
  647. StartCoroutine(MoveConveyorRightCoroutine(_currentBlock, transform, speed, 0.3f));
  648. }
  649. }
  650. public void CannonRMove(float speed)
  651. {
  652. Direction direction = Direction.Right;
  653. Vector3 position = _currentBlock.position + direction.ToVector(transform); // position wanted
  654. Block hit; //output of block detection
  655. Block moveTo = _currentBlock; //block we'll actually move to
  656. if (Block.isBlockAtPosition(position, 1, ~Ignore, out hit) && hit.isWalkable(~Ignore))
  657. {
  658. moveTo = hit;
  659. _currentBlock = moveTo;
  660. StartCoroutine(PushRightCoroutine(transform, speed));
  661. }
  662. }
  663. public void CannonLMove(float speed)
  664. {
  665. Direction direction = Direction.Left;
  666. Vector3 position = _currentBlock.position + direction.ToVector(transform); // position wanted
  667. Block hit; //output of block detection
  668. Block moveTo = _currentBlock; //block we'll actually move to
  669. if (Block.isBlockAtPosition(position, 1, ~Ignore, out hit) && hit.isWalkable(~Ignore))
  670. {
  671. moveTo = hit;
  672. _currentBlock = moveTo;
  673. StartCoroutine(PushLeftCoroutine(transform, speed));
  674. }
  675. }
  676. #endregion Depricated
  677. }