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))]
public class BlackHole : MonoBehaviour
{
protected float spentMass;
public float spentMass;
public float Mass {
get {

View file

@ -7,28 +7,26 @@ public class CameraPan : MonoBehaviour
{
public string axisName;
protected new Camera camera;
protected Vector3? lastMousePosition;
private void Start() {
lastMousePosition = null;
camera = GetComponent<Camera>();
}
private void Update() {
bool panIsPressed = Input.GetAxisRaw(axisName) != 0f;
Vector3? currentMousePosition = null;
if(panIsPressed) {
currentMousePosition = camera.ScreenToWorldPoint(Input.mousePosition);
currentMousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
if(lastMousePosition.HasValue) {
Vector3 positionDelta = lastMousePosition.Value - currentMousePosition.Value;
camera.transform.position += positionDelta;
currentMousePosition = camera.ScreenToWorldPoint(Input.mousePosition);
Camera.main.transform.position += positionDelta;
currentMousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
}
}
lastMousePosition = currentMousePosition;
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 float health;
protected float _health;
protected Particle particle;
public float Health {
get {
return _health;
}
}
protected void Awake() {
particle = GetComponent<Particle>();
}
@ -16,13 +22,13 @@ public class Disappear : MonoBehaviour
}
public void ResetTimer() {
health = 1f;
_health = 1f;
}
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);
}
}

View file

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

View file

@ -9,13 +9,9 @@ public class Gravitation : MonoBehaviour
[Header("Config")]
public bool isStatic;
[Header("Forces")]
protected Vector3 appliedForce;
[Header("Internals")]
[HideInInspector]
public int positionInList;
[Header("References")]
protected new Rigidbody2D rigidbody;
protected GameController gameController;
@ -53,7 +49,6 @@ public class Gravitation : MonoBehaviour
private void Start()
{
appliedForce = new Vector3(0f, 0f, 0f);
}
// O(n²)
@ -67,6 +62,5 @@ public class Gravitation : MonoBehaviour
if(!this.isStatic) 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 Material material;
protected Color color;
protected void Awake() {

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -7,15 +7,15 @@ using UnityEngine.UI;
public class TextFromUnspentBlackHoleMass : MonoBehaviour
{
protected Text text;
public BlackHole blackHole;
protected GameController gameController;
protected void Awake() {
text = GetComponent<Text>();
blackHole = GameObject.FindGameObjectWithTag("BlackHole").GetComponent<BlackHole>();
gameController = GameObject.Find("GameController").GetComponent<GameController>();
}
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
{
[Header("Properties")]
public int cost;
public int level;
public UpgradeType type;
[Header("Sprites")]
public Sprite cantBuySprite;
public Sprite canBuySprite;
public Sprite hoveredSprite;
@ -33,9 +35,8 @@ public class UpgradeButton : MonoBehaviour
case UpgradeType.ANTIG: return gameController.LevelAntig >= level;
case UpgradeType.MATTER: return gameController.LevelMatter >= level;
case UpgradeType.FISSION: return gameController.LevelFission >= level;
default: return false;
}
//ok c#
return false;
}
}