Advertisement
Guest User

Untitled

a guest
Feb 21st, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.79 KB | None | 0 0
  1. public class GameManager : MonoBehaviour {
  2.  
  3. // reference to other scripts
  4. public Controller2D controller2D;
  5.  
  6. public float screenPositionX;
  7. public float screenPositionY;
  8. int sw = Screen.width;
  9. int sh = Screen.height;
  10. public float iconSizeX = 25;
  11. public float iconSizeY = 25;
  12.  
  13. static public int bulletDamage = 1;
  14.  
  15. static public int curHealth;
  16. public int maxHealth;
  17. public int curExp = 0;
  18. int maxExp = 50;
  19. int level = 1;
  20. public int curCoins = 0;
  21.  
  22. bool pauseMenu;
  23.  
  24. private void Awake() {
  25. print("AWAKE1() bulletDamage: " + bulletDamage); // 1
  26. curExp = PlayerPrefs.GetInt("Player Exp");
  27. level = PlayerPrefs.GetInt("Player Level");
  28. curCoins = PlayerPrefs.GetInt("Player Coins");
  29. bulletDamage = PlayerPrefs.GetInt("Player Damage");
  30. print("AWAKE2() bulletDamage: " + bulletDamage); // 0
  31. // ...
  32. }
  33.  
  34. private void Update() {
  35. // ...
  36. }
  37.  
  38. void OnGUI() {
  39. if(pauseMenu) {
  40. if(GUI.Button(new Rect(sw * 0.25f, sh * 0.3f, sw * 0.5f, sh * 0.1f), "Save")) {
  41. SaveGame();
  42. }
  43. // ...
  44. }
  45. }
  46.  
  47. public void SaveGame() {
  48. PlayerPrefs.SetInt("Player Level", level);
  49. PlayerPrefs.SetInt("Player Exp", curExp);
  50. PlayerPrefs.SetInt("Player Coins", curCoins);
  51. print("SAVEGAME() bd: " + bulletDamage); // 0
  52. PlayerPrefs.SetInt("Player Damage", bulletDamage);
  53. }
  54.  
  55. void PlayerDamaged(int damage) {
  56. if(curHealth > 0) {
  57. curHealth -= damage;
  58. }
  59. if(curHealth <= 0) {
  60. curHealth = 0;
  61. RestartScene();
  62. }
  63. }
  64. }
  65.  
  66. public class Bullet : MonoBehaviour {
  67.  
  68. private void OnTriggerEnter(Collider other) {
  69. if(other.gameObject.tag == "Enemy") {
  70. Destroy(gameObject);
  71. other.gameObject.SendMessage("EnemyDamaged", GameManager.bulletDamage, SendMessageOptions.DontRequireReceiver);
  72. other.gameObject.SendMessage("TakenDamage", SendMessageOptions.DontRequireReceiver);
  73. }
  74. if(other.gameObject.tag == "Wall") {
  75. Destroy(gameObject);
  76. }
  77. }
  78.  
  79. private void FixedUpdate() {
  80. Destroy(gameObject, 1.25f);
  81. }
  82. }
  83.  
  84. public class Controller2D : MonoBehaviour {
  85.  
  86. // references to other scripts
  87. CharacterController characterController;
  88. public GameManager gameManager;
  89.  
  90. Renderer rend;
  91.  
  92. public float gravity = 10;
  93. public float walkSpeed = 5;
  94. public float jumpHeight = 5;
  95.  
  96. float takenDamage = 0.2f;
  97.  
  98. Vector3 moveDirection = Vector3.zero;
  99. float horizontal = 0;
  100.  
  101. public Rigidbody bulletPrefab;
  102. float attackRate = 0.2f;
  103. float coolDown;
  104.  
  105. bool lookRight = true;
  106.  
  107. void Start() {
  108. characterController = GetComponent<CharacterController>();
  109. rend = GetComponent<Renderer>();
  110. }
  111.  
  112. void Update() {
  113. characterController.Move(moveDirection * Time.deltaTime);
  114. horizontal = Input.GetAxis("Horizontal");
  115. moveDirection.y -= gravity * Time.deltaTime;
  116. if(horizontal == 0) {
  117. moveDirection.x = horizontal;
  118. }
  119. if(horizontal > 0.01f) {
  120. lookRight = true;
  121. moveDirection.x = horizontal * walkSpeed;
  122. }
  123. if(horizontal < -0.01f) {
  124. lookRight = false;
  125. moveDirection.x = horizontal * walkSpeed;
  126. }
  127. if(characterController.isGrounded) {
  128. if(Input.GetKeyDown(KeyCode.Space)) {
  129. moveDirection.y = jumpHeight;
  130. }
  131. }
  132. if(Time.time >= coolDown) {
  133. if(Input.GetKeyDown(KeyCode.F)) {
  134. BulletAttack();
  135. }
  136. }
  137. }
  138.  
  139. void BulletAttack() {
  140. if(lookRight) {
  141. Rigidbody bPrefab = Instantiate(bulletPrefab, transform.position, Quaternion.identity) as Rigidbody;
  142. bPrefab.AddForce(Vector3.right * 500);
  143. coolDown = Time.time + attackRate;
  144. }
  145. else {
  146. Rigidbody bPrefab = Instantiate(bulletPrefab, transform.position, Quaternion.identity) as Rigidbody;
  147. bPrefab.AddForce(-Vector3.right * 500);
  148. coolDown = Time.time + attackRate;
  149. }
  150. }
  151.  
  152. public IEnumerator TakenDamage() {
  153. rend.enabled = false;
  154. yield return new WaitForSeconds(takenDamage);
  155. rend.enabled = true;
  156. yield return new WaitForSeconds(takenDamage);
  157. rend.enabled = false;
  158. yield return new WaitForSeconds(takenDamage);
  159. rend.enabled = true;
  160. yield return new WaitForSeconds(takenDamage);
  161. rend.enabled = false;
  162. yield return new WaitForSeconds(takenDamage);
  163. rend.enabled = true;
  164. yield return new WaitForSeconds(takenDamage);
  165. }
  166. }
  167.  
  168. public class Enemy2D : MonoBehaviour {
  169.  
  170. // references to other scripts
  171. public GameManager gameManager;
  172.  
  173. Renderer rend;
  174.  
  175. Rigidbody rb;
  176.  
  177. float startingPos;
  178. float endPos;
  179.  
  180. public int unitsToMove = 5;
  181. public int moveSpeed = 2;
  182. bool moveRight = true;
  183. int damageValue = 1;
  184. int enemyHealth = 1;
  185. public bool basicEnemy = true;
  186. public bool advancedEnemy;
  187. float takenDamage = 0.2f;
  188.  
  189. private void Start() {
  190. rb = GetComponent<Rigidbody>();
  191. rend = GetComponent<Renderer>();
  192. }
  193.  
  194. private void Update() {
  195. if(moveRight) {
  196. rb.position += Vector3.right * moveSpeed * Time.deltaTime;
  197. }
  198. if(rb.position.x >= endPos) {
  199. moveRight = false;
  200. }
  201. if(!moveRight) {
  202. rb.position -= Vector3.right * moveSpeed * Time.deltaTime;
  203. }
  204. if(rb.position.x <= startingPos) {
  205. moveRight = true;
  206. }
  207. }
  208.  
  209. private void Awake() {
  210. startingPos = transform.position.x;
  211. endPos = startingPos + unitsToMove;
  212. if(basicEnemy) {
  213. enemyHealth = 3;
  214. }
  215. if(advancedEnemy) {
  216. enemyHealth = 6;
  217. }
  218. }
  219.  
  220. void OnTriggerEnter(Collider col) {
  221. if(col.gameObject.tag == "Player") {
  222. gameManager.SendMessage("PlayerDamaged", damageValue, SendMessageOptions.DontRequireReceiver);
  223. gameManager.controller2D.SendMessage("TakenDamage", SendMessageOptions.DontRequireReceiver);
  224. }
  225. }
  226.  
  227. void EnemyDamaged(int damage) {
  228. if(enemyHealth > 0) {
  229. enemyHealth -= damage;
  230. }
  231. if(enemyHealth <= 0) {
  232. enemyHealth = 0;
  233. Destroy(gameObject);
  234. if(basicEnemy) {
  235. gameManager.curExp += 5;
  236. gameManager.curCoins += 5;
  237. }
  238. if(advancedEnemy) {
  239. gameManager.curExp += 10;
  240. gameManager.curCoins += 10;
  241. }
  242. }
  243. }
  244.  
  245. public IEnumerator TakenDamage() {
  246. rend.enabled = false;
  247. yield return new WaitForSeconds(takenDamage);
  248. rend.enabled = true;
  249. yield return new WaitForSeconds(takenDamage);
  250. rend.enabled = false;
  251. yield return new WaitForSeconds(takenDamage);
  252. rend.enabled = true;
  253. yield return new WaitForSeconds(takenDamage);
  254. rend.enabled = false;
  255. yield return new WaitForSeconds(takenDamage);
  256. rend.enabled = true;
  257. yield return new WaitForSeconds(takenDamage);
  258. }
  259. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement