1
Fork 0
mirror of https://github.com/Steffo99/gravity-fusion.git synced 2024-11-24 01:04:20 +00:00

Code refactor

This commit is contained in:
Steffo 2019-10-07 13:44:26 +02:00
parent 832fdc766f
commit 7e9137f114
16 changed files with 90 additions and 66 deletions

View file

@ -6,7 +6,7 @@ using UnityEngine;
[RequireComponent(typeof(Collider2D))] [RequireComponent(typeof(Collider2D))]
public class BlackHole : MonoBehaviour public class BlackHole : MonoBehaviour
{ {
protected float spentMass; public float spentMass;
public float Mass { public float Mass {
get { get {

View file

@ -7,28 +7,26 @@ public class CameraPan : MonoBehaviour
{ {
public string axisName; public string axisName;
protected new Camera camera;
protected Vector3? lastMousePosition; protected Vector3? lastMousePosition;
private void Start() { private void Start() {
lastMousePosition = null; lastMousePosition = null;
camera = GetComponent<Camera>();
} }
private void Update() { private void Update() {
bool panIsPressed = Input.GetAxisRaw(axisName) != 0f; bool panIsPressed = Input.GetAxisRaw(axisName) != 0f;
Vector3? currentMousePosition = null; Vector3? currentMousePosition = null;
if(panIsPressed) { if(panIsPressed) {
currentMousePosition = camera.ScreenToWorldPoint(Input.mousePosition); currentMousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
if(lastMousePosition.HasValue) { if(lastMousePosition.HasValue) {
Vector3 positionDelta = lastMousePosition.Value - currentMousePosition.Value; Vector3 positionDelta = lastMousePosition.Value - currentMousePosition.Value;
camera.transform.position += positionDelta; Camera.main.transform.position += positionDelta;
currentMousePosition = camera.ScreenToWorldPoint(Input.mousePosition); currentMousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
} }
} }
lastMousePosition = currentMousePosition; lastMousePosition = currentMousePosition;
if(Input.GetAxisRaw("ResetCamera") > 0) { if(Input.GetAxisRaw("ResetCamera") > 0) {
camera.transform.position = new Vector3(0, 0, camera.transform.position.z); Camera.main.transform.position = new Vector3(0, 0, Camera.main.transform.position.z);
} }
} }
} }

View file

@ -4,9 +4,15 @@ using UnityEngine;
public class Disappear : MonoBehaviour public class Disappear : MonoBehaviour
{ {
public float health; protected float _health;
protected Particle particle; protected Particle particle;
public float Health {
get {
return _health;
}
}
protected void Awake() { protected void Awake() {
particle = GetComponent<Particle>(); particle = GetComponent<Particle>();
} }
@ -16,13 +22,13 @@ public class Disappear : MonoBehaviour
} }
public void ResetTimer() { public void ResetTimer() {
health = 1f; _health = 1f;
} }
protected void Update() { protected void Update() {
health -= Mathf.Pow(particle.gameController.particleDurationConstant, particle.gameController.maxTierPresent - particle.Tier - 4) * Time.deltaTime; _health -= Mathf.Pow(particle.gameController.particleDurationConstant, particle.gameController.maxTierPresent - particle.Tier - 4) * Time.deltaTime;
if(health < 0) { if(_health < 0) {
Destroy(this.gameObject); Destroy(this.gameObject);
} }
} }

View file

@ -13,22 +13,19 @@ public class GameController : MonoBehaviour
[Header("Black Hole")] [Header("Black Hole")]
public GameObject blackHolePrefab; public GameObject blackHolePrefab;
public BlackHole blackHole;
[Header("Particles")] [Header("Particles")]
public GameObject particlePrefab; public GameObject particlePrefab;
public Gradient[] tierGradients; public Gradient[] tierGradients;
public RuntimeAnimatorController[] tierAnimation; public RuntimeAnimatorController[] tierAnimation;
public List<Gravitation> simulatedObjects;
public int maxTierPresent;
[Header("Upgrades")] [Header("Upgrade Parameters")]
public float[] upgradePushForce; public float[] upgradePushForce;
public float[] upgradePushRadius; public float[] upgradePushRadius;
public int[] upgradeParticleCount; public int[] upgradeParticleCount;
public int[] upgradeParticleTiers; public int[] upgradeParticleTiers;
[Header("Bought Upgrades")] [Header("Bought Updates")]
public int _levelAntig = 0; public int _levelAntig = 0;
public int _levelMatter = 0; public int _levelMatter = 0;
public int _levelFission = 0; public int _levelFission = 0;
@ -64,13 +61,31 @@ public class GameController : MonoBehaviour
} }
} }
[Header("Simulation Status")]
[HideInInspector]
public List<Gravitation> simulatedObjects;
[HideInInspector]
public int maxTierPresent;
[Header("References")] [Header("References")]
[HideInInspector]
public SpawnOnMouseClick spawner; public SpawnOnMouseClick spawner;
[HideInInspector]
public PushOnMouseClick pusher; public PushOnMouseClick pusher;
[HideInInspector]
public CameraPan panner; public CameraPan panner;
[HideInInspector]
public MusicManager musicManager; public MusicManager musicManager;
[HideInInspector]
public Canvas canvas; public Canvas canvas;
[HideInInspector]
public BlackHole blackHole;
protected void Awake() { protected void Awake() {
spawner = Camera.main.GetComponent<SpawnOnMouseClick>(); spawner = Camera.main.GetComponent<SpawnOnMouseClick>();
pusher = Camera.main.GetComponent<PushOnMouseClick>(); pusher = Camera.main.GetComponent<PushOnMouseClick>();

View file

@ -9,13 +9,9 @@ public class Gravitation : MonoBehaviour
[Header("Config")] [Header("Config")]
public bool isStatic; public bool isStatic;
[Header("Forces")] [HideInInspector]
protected Vector3 appliedForce;
[Header("Internals")]
public int positionInList; public int positionInList;
[Header("References")]
protected new Rigidbody2D rigidbody; protected new Rigidbody2D rigidbody;
protected GameController gameController; protected GameController gameController;
@ -53,7 +49,6 @@ public class Gravitation : MonoBehaviour
private void Start() private void Start()
{ {
appliedForce = new Vector3(0f, 0f, 0f);
} }
// O(n²) // O(n²)
@ -67,6 +62,5 @@ public class Gravitation : MonoBehaviour
if(!this.isStatic) rigidbody.AddForce(direction * force); if(!this.isStatic) rigidbody.AddForce(direction * force);
if(!other.isStatic) other.rigidbody.AddForce(-direction * force); if(!other.isStatic) other.rigidbody.AddForce(-direction * force);
} }
appliedForce = new Vector3(0, 0, 0);
} }
} }

View file

@ -9,7 +9,6 @@ public class Hue : MonoBehaviour
protected Particle particle; protected Particle particle;
protected Material material; protected Material material;
protected Color color; protected Color color;
protected void Awake() { protected void Awake() {

View file

@ -5,13 +5,13 @@ using UnityEngine;
[RequireComponent(typeof(CircleCollider2D))] [RequireComponent(typeof(CircleCollider2D))]
public class Merger : MonoBehaviour public class Merger : MonoBehaviour
{ {
public float mergeAfterSeconds;
protected bool mergeEnabled;
protected List<Merger> mergeCandidates;
[HideInInspector] [HideInInspector]
public Particle particle; public Particle particle;
public float mergeAfterSeconds;
protected bool mergeEnabled;
protected List<Merger> mergeCandidates;
protected Collider2D Collider { protected Collider2D Collider {
get { get {

View file

@ -6,21 +6,22 @@ using UnityEngine;
public class MusicManager : MonoBehaviour public class MusicManager : MonoBehaviour
{ {
public AudioSource baseLayer;
public float changeSpeed = 1f; public float changeSpeed = 1f;
[BeforeStart]
public AudioSource baseLayer;
[BeforeStart] [BeforeStart]
public AudioSource[] layers; public AudioSource[] layers;
protected bool neverStarted;
protected float baseLayerTargetVolume; protected float baseLayerTargetVolume;
protected float[] targetVolume; protected float[] targetVolume;
protected bool neverStarted;
protected void Start() protected void Start()
{ {
foreach(AudioSource audioSource in layers) { foreach(AudioSource audioSource in layers) {
audioSource.volume = 0; audioSource.volume = 0f;
} }
targetVolume = new float[layers.Length]; targetVolume = new float[layers.Length];
for(int i = 0; i < layers.Length; i++) { for(int i = 0; i < layers.Length; i++) {
@ -40,7 +41,9 @@ public class MusicManager : MonoBehaviour
if(neverStarted) { if(neverStarted) {
foreach(AudioSource layer in layers) { foreach(AudioSource layer in layers) {
layer.Play(); if(layer != null) {
layer.Play();
}
} }
neverStarted = false; neverStarted = false;
} }
@ -63,19 +66,23 @@ public class MusicManager : MonoBehaviour
} }
protected void Update() { protected void Update() {
if(baseLayer.volume > baseLayerTargetVolume) { if(baseLayer != null) {
baseLayer.volume = Mathf.Clamp(baseLayer.volume - Time.deltaTime * changeSpeed, baseLayerTargetVolume, float.PositiveInfinity); if(baseLayer.volume > baseLayerTargetVolume) {
} baseLayer.volume = Mathf.Clamp(baseLayer.volume - Time.deltaTime * changeSpeed, baseLayerTargetVolume, float.PositiveInfinity);
else if(baseLayer.volume < baseLayerTargetVolume) { }
baseLayer.volume = Mathf.Clamp(baseLayer.volume + Time.deltaTime * changeSpeed, 0f, baseLayerTargetVolume); else if(baseLayer.volume < baseLayerTargetVolume) {
baseLayer.volume = Mathf.Clamp(baseLayer.volume + Time.deltaTime * changeSpeed, 0f, baseLayerTargetVolume);
}
} }
for(int i = 0; i < layers.Length; i++) { for(int i = 0; i < layers.Length; i++) {
if(layers[i].volume > targetVolume[i]) { if(layers[i] != null) {
layers[i].volume = Mathf.Clamp(layers[i].volume - Time.deltaTime * changeSpeed, targetVolume[i], float.PositiveInfinity); if(layers[i].volume > targetVolume[i]) {
} layers[i].volume = Mathf.Clamp(layers[i].volume - Time.deltaTime * changeSpeed, targetVolume[i], float.PositiveInfinity);
else if(layers[i].volume < targetVolume[i]) { }
layers[i].volume = Mathf.Clamp(layers[i].volume + Time.deltaTime * changeSpeed, 0f, targetVolume[i]); else if(layers[i].volume < targetVolume[i]) {
layers[i].volume = Mathf.Clamp(layers[i].volume + Time.deltaTime * changeSpeed, 0f, targetVolume[i]);
}
} }
} }
} }

View file

@ -34,11 +34,11 @@ public class Particle : MonoBehaviour {
} }
} }
public float Scale { float Scale {
get { get {
return transform.localScale.x; return transform.localScale.x;
} }
set { protected set {
transform.localScale = new Vector3(value, value, 1); transform.localScale = new Vector3(value, value, 1);
} }
} }

View file

@ -4,16 +4,15 @@ using UnityEngine;
public class PushOnMouseClick : MonoBehaviour public class PushOnMouseClick : MonoBehaviour
{ {
[Header("Config")] public int mouseButton = 1;
public int mouseButton; public float pushForce = 0;
public float pushForce; public float pushRadius = 0;
public float pushRadius;
protected Vector3 GetWorldMousePosition() { protected Vector3 GetWorldMousePosition() {
return Camera.main.ScreenToWorldPoint(Input.mousePosition); return Camera.main.ScreenToWorldPoint(Input.mousePosition);
} }
void Update() protected void Update()
{ {
if(Input.GetMouseButton(mouseButton)) { if(Input.GetMouseButton(mouseButton)) {
Vector3 mousePosition = GetWorldMousePosition(); Vector3 mousePosition = GetWorldMousePosition();

View file

@ -5,11 +5,12 @@ using UnityEngine;
public class SpawnOnMouseClick : MonoBehaviour public class SpawnOnMouseClick : MonoBehaviour
{ {
[Header("Config")] [Header("Config")]
public GameController gameController; public int mouseButton = 0;
public int mouseButton; public int spawnedTier = 0;
public int spawnedTier; public int spawnCount = 1;
public int spawnCount; public float appliedForce = 0.1f;
public float appliedForce;
protected GameController gameController;
protected void Awake() { protected void Awake() {
gameController = GameObject.FindGameObjectWithTag("GameController").GetComponent<GameController>(); gameController = GameObject.FindGameObjectWithTag("GameController").GetComponent<GameController>();

View file

@ -6,7 +6,11 @@ public class TempUpgradesToggler : MonoBehaviour
{ {
public GameObject upgrades; public GameObject upgrades;
void Update() protected void Start() {
Debug.LogWarning("TempUpgradesToggler should not be used.");
}
protected void Update()
{ {
if(Input.GetKeyDown(KeyCode.P)) { if(Input.GetKeyDown(KeyCode.P)) {
upgrades.SetActive(!upgrades.activeSelf); upgrades.SetActive(!upgrades.activeSelf);

View file

@ -7,15 +7,15 @@ using UnityEngine.UI;
public class TextFromBlackHoleMass : MonoBehaviour public class TextFromBlackHoleMass : MonoBehaviour
{ {
protected Text text; protected Text text;
public BlackHole blackHole; protected GameController gameController;
protected void Awake() { protected void Awake() {
text = GetComponent<Text>(); text = GetComponent<Text>();
blackHole = GameObject.FindGameObjectWithTag("BlackHole").GetComponent<BlackHole>(); gameController = GameObject.Find("GameController").GetComponent<GameController>();
} }
protected void Update() protected void Update()
{ {
text.text = blackHole.Mass.ToString("0"); text.text = gameController.blackHole.Mass.ToString("0");
} }
} }

View file

@ -12,7 +12,7 @@ public class TextFromCost : MonoBehaviour
} }
public void DisplayCost(float cost) { public void DisplayCost(float cost) {
if(cost > 0) { if(cost >= 0) {
text.text = cost.ToString("0"); text.text = cost.ToString("0");
} }
else { else {

View file

@ -7,15 +7,15 @@ using UnityEngine.UI;
public class TextFromUnspentBlackHoleMass : MonoBehaviour public class TextFromUnspentBlackHoleMass : MonoBehaviour
{ {
protected Text text; protected Text text;
public BlackHole blackHole; protected GameController gameController;
protected void Awake() { protected void Awake() {
text = GetComponent<Text>(); text = GetComponent<Text>();
blackHole = GameObject.FindGameObjectWithTag("BlackHole").GetComponent<BlackHole>(); gameController = GameObject.Find("GameController").GetComponent<GameController>();
} }
protected void Update() protected void Update()
{ {
text.text = blackHole.UnspentMass.ToString("0"); text.text = gameController.blackHole.UnspentMass.ToString("0");
} }
} }

View file

@ -12,10 +12,12 @@ public enum UpgradeType {
public class UpgradeButton : MonoBehaviour public class UpgradeButton : MonoBehaviour
{ {
[Header("Properties")]
public int cost; public int cost;
public int level; public int level;
public UpgradeType type; public UpgradeType type;
[Header("Sprites")]
public Sprite cantBuySprite; public Sprite cantBuySprite;
public Sprite canBuySprite; public Sprite canBuySprite;
public Sprite hoveredSprite; public Sprite hoveredSprite;
@ -33,9 +35,8 @@ public class UpgradeButton : MonoBehaviour
case UpgradeType.ANTIG: return gameController.LevelAntig >= level; case UpgradeType.ANTIG: return gameController.LevelAntig >= level;
case UpgradeType.MATTER: return gameController.LevelMatter >= level; case UpgradeType.MATTER: return gameController.LevelMatter >= level;
case UpgradeType.FISSION: return gameController.LevelFission >= level; case UpgradeType.FISSION: return gameController.LevelFission >= level;
default: return false;
} }
//ok c#
return false;
} }
} }