/Assets/Scripts/Player.cs

https://bitbucket.org/Danvil/ld26_minimal · C# · 193 lines · 161 code · 21 blank · 11 comment · 33 complexity · 04f3969c38cc2f4ad0378c047d406b74 MD5 · raw file

  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Linq;
  4. using System.Collections.Generic;
  5. public class Player : MonoBehaviour {
  6. const float SHOOT_TIMEOUT = 1.0f;
  7. const float PLAYER_RADIUS = 0.2f;
  8. const float PLAYER_VELOCITY = 3.53f;
  9. public int MegaBombCoins = 10;
  10. float shootTimeout = 0;
  11. public Living living { get; private set; }
  12. public int NumCoinsCollected = 0;
  13. public int NumEnemiesKilled = 0;
  14. public int NumCoins = 0;
  15. public AudioClip audioGetMegaBomb;
  16. public GameObject pfBombInventory;
  17. public List<GameObject> inventory = new List<GameObject>();
  18. void Awake()
  19. {
  20. Globals.Player = this;
  21. }
  22. // Use this for initialization
  23. void Start()
  24. {
  25. Globals.BlobManager.AddBlob(gameObject);
  26. living = GetComponent<Living>();
  27. // position player
  28. this.transform.position = new Vector3(
  29. Globals.Level.PlayerStart.x,
  30. Globals.Level.PlayerStart.y,
  31. 0.1f);
  32. this.transform.localScale = new Vector3(Globals.PlayerSize, Globals.PlayerSize, Globals.PlayerSize);
  33. for(int i=0; i<Globals.NumMegaBombs; i++) {
  34. AddMegaBomb();
  35. }
  36. }
  37. void AddMegaBomb()
  38. {
  39. GameObject go = (GameObject)Instantiate(pfBombInventory);
  40. inventory.Add(go);
  41. go.transform.parent = this.transform;
  42. float s = 0.5f*go.transform.localScale.x;
  43. int i = inventory.Count - 1;
  44. float phi = ((float)i) * 40.0f;
  45. go.transform.localPosition = new Vector3(s*Mathf.Cos(phi), s*Mathf.Sin(phi), -0.4f);
  46. }
  47. public void OnPickupCoin()
  48. {
  49. NumCoinsCollected ++;
  50. NumCoins ++;
  51. if(NumCoins >= MegaBombCoins) {
  52. NumCoins -= MegaBombCoins;
  53. Globals.NumMegaBombs ++;
  54. AddMegaBomb();
  55. audio.PlayOneShot(audioGetMegaBomb);
  56. }
  57. }
  58. void Move()
  59. {
  60. // move with keyboard
  61. Vector3 movedir = Vector3.zero;
  62. if(Input.GetKey(KeyCode.A)) {
  63. movedir += new Vector3(-1,0,0);
  64. }
  65. if(Input.GetKey(KeyCode.D)) {
  66. movedir += new Vector3(+1,0,0);
  67. }
  68. if(Input.GetKey(KeyCode.W)) {
  69. movedir += new Vector3(0,+1,0);
  70. }
  71. if(Input.GetKey(KeyCode.S)) {
  72. movedir += new Vector3(0,-1,0);
  73. }
  74. if(movedir.magnitude > 0.0f) {
  75. float r = Mathf.Min(0.45f, PLAYER_RADIUS*this.transform.localScale.x);
  76. movedir = movedir.normalized * PLAYER_VELOCITY * MyTime.deltaTime;
  77. Vector3 newpos = this.transform.position + movedir;
  78. if(!Globals.Level.IsBlocking(newpos, r)) {
  79. this.transform.position = newpos;
  80. }
  81. else {
  82. newpos = this.transform.position + movedir.WithChangedX(0.0f);
  83. if(!Globals.Level.IsBlocking(newpos, r)) {
  84. this.transform.position = newpos;
  85. }
  86. else {
  87. newpos = this.transform.position + movedir.WithChangedY(0.0f);
  88. if(!Globals.Level.IsBlocking(newpos, r)) {
  89. this.transform.position = newpos;
  90. }
  91. }
  92. }
  93. }
  94. this.transform.position = this.transform.position.WithChangedZ(0);
  95. }
  96. void Shoot()
  97. {
  98. shootTimeout -= MyTime.deltaTime;
  99. // shoot and aim with mouse
  100. bool fire1 = Input.GetButton("Fire1");
  101. bool fire2 = Input.GetButton("Fire2");
  102. if((fire1 || fire2) && shootTimeout <= 0) {
  103. shootTimeout = SHOOT_TIMEOUT;
  104. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  105. Vector3 target = ray.GetPoint(- ray.origin.z / ray.direction.z);
  106. Vector3 start = this.transform.position + new Vector3(0,0,-0.5f*this.transform.localScale.x -.3f);
  107. bool usemega = fire2 && Globals.NumMegaBombs > 0;
  108. Globals.BombManager.ThrowBomb(start, target, true, usemega);
  109. if(usemega) {
  110. Globals.NumMegaBombs --;
  111. GameObject go = inventory.Last();
  112. inventory.Remove(go);
  113. Object.Destroy(go);
  114. }
  115. }
  116. }
  117. void UpdateCameraPosition()
  118. {
  119. const float MARG = 2.5f;
  120. float w = (float)(Globals.Level.Cols);
  121. float h = (float)(Globals.Level.Rows);
  122. float px = this.transform.position.x;
  123. float py = this.transform.position.y;
  124. if(px < MARG) px = MARG;
  125. if(px > w - MARG) px = w - MARG;
  126. if(py < MARG) py = MARG;
  127. if(py > h - MARG) py = h - MARG;
  128. Camera.main.transform.position = new Vector3(px, py, Camera.main.transform.position.z);
  129. }
  130. public void EatPotatoe()
  131. {
  132. living.HealthMax += 5.00f;
  133. living.Health += 5.0f;
  134. float growth = 0.20f / this.transform.localScale.x;
  135. this.transform.localScale += new Vector3(growth, growth, growth);
  136. Globals.PlayerSize = this.transform.localScale.x;
  137. foreach(GameObject go in inventory) {
  138. go.transform.localScale = new Vector3(0.5f,0.5f,0.5f);
  139. }
  140. }
  141. float minWinTime = 3.0f;
  142. // Update is called once per frame
  143. void Update () {
  144. minWinTime -= MyTime.deltaTime;
  145. Move();
  146. Shoot();
  147. // stop time if dead
  148. if(living.IsDead) {
  149. Globals.SceneTransition.Loose();
  150. }
  151. // move camera
  152. UpdateCameraPosition();
  153. // check for completion
  154. if(NumCoinsCollected == Globals.Level.NumCoins && NumEnemiesKilled == Globals.Level.NumEnemies) {
  155. // show princess
  156. Globals.Princess.ShowPrincess();
  157. }
  158. if(minWinTime <= 0.0f && Globals.RoomManager != null && Globals.RoomManager.currentRoom.isBoss) {
  159. // test if boss is dead
  160. int bossnum = Globals.BlobManager.GetLifeBehaviours()
  161. .Where(x => !x.IsDead)
  162. .Select(x => x.GetComponent<Enemy>())
  163. .Where(x => x != null)
  164. .Select(x => x.IsBoss)
  165. .Count();
  166. if(bossnum == 0) {
  167. //WIN THE GAME
  168. Globals.SceneTransition.Win();
  169. }
  170. }
  171. }
  172. }