Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using UnityEngine.SceneManagement;
- public class GameLogic : MonoBehaviour
- {
- [SerializeField] public static int health = 10;
- string healthtextstring;
- [SerializeField] Text healthcomponent;
- public static int bullets = 0;
- string bullettextstring;
- [SerializeField] Text bulletcomponent;
- [SerializeField] Text textComponent;
- [SerializeField] State startingState;
- [SerializeField] State gameoverState;
- public State currentstate;
- AudioSource audiosource;
- // Start is called before the first frame update
- void Start()
- {
- currentstate = startingState;
- textComponent.text = currentstate.GetStateStory();
- audiosource = GetComponent<AudioSource>();
- }
- // Update is called once per frame
- void Update()
- {
- Debug.Log(currentstate.optionsounds);
- ManageHealth();
- ManageState();
- ManageBullets();
- if(Input.GetKeyDown(KeyCode.Q))
- {
- SceneManager.LoadScene(0);
- }
- }
- private void ManageHealth()
- {
- //Always sets the health string to the health integer(in case it is changed elseware), and the health component
- //sets the UI element to the string
- healthtextstring = health.ToString();
- healthcomponent.text = healthtextstring;
- if (health <= 0)
- {
- health = 0;
- currentstate = gameoverState;
- }
- }
- private void ManageBullets()
- {
- bullettextstring = bullets.ToString();
- bulletcomponent.text = bullettextstring;
- if (bullets < 0)
- {
- bullets = 0;
- }
- }
- private void ManageState()
- {
- var nextStates = currentstate.GetNextStates();
- for(int index = 0; index < nextStates.Length; index++)
- {
- if (Input.GetKeyDown(KeyCode.Alpha1 + index))
- {
- if (currentstate.optionsammocost.Length>0)
- {
- if (bullets >= currentstate.optionsammocost[index])
- {
- indextvariablechanges(index);
- currentstate = nextStates[index];
- }
- }
- else
- {
- indextvariablechanges(index);
- currentstate = nextStates[index];
- }
- }
- }
- textComponent.text = currentstate.GetStateStory();
- }
- private void indextvariablechanges(int index)
- {
- if (index == 0)
- {
- RemovehealthOnOption(0);
- RemovebulletsOnOption(0);
- PlaysoundOnOption(0);
- }
- if (index == 1)
- {
- RemovehealthOnOption(1);
- RemovebulletsOnOption(1);
- PlaysoundOnOption(1);
- }
- if (index == 2)
- {
- RemovehealthOnOption(2);
- RemovebulletsOnOption(2);
- PlaysoundOnOption(2);
- }
- }
- private void RemovehealthOnOption(int option)
- {
- if (currentstate.optionsdamage.Length > option)
- {
- health = health - currentstate.optionsdamage[option];
- }
- }
- private void RemovebulletsOnOption(int option)
- {
- if (currentstate.optionsammocost.Length > option)
- {
- bullets = bullets - currentstate.optionsammocost[option];
- }
- }
- //this is the thing you want
- private void PlaysoundOnOption(int option)
- {
- if(currentstate.optionsounds.Length > option)
- {
- if (currentstate.optionsounds[option])
- {
- audiosource.PlayOneShot(currentstate.optionsounds[option]);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement