The Powers That Be

One of the first actions a player will likely want to do is turn on the main power generators.  There are 4 main power cores, each contributing to the overall ship power.  However, at the start of the game, these will all be off. 

Each core has 4 possible states:  offline, online, damaged or destroyed.  The current state will determine what components are active on the prefab.  When offline, there is no lights, sparks or sound.  Online has a glowing blue light.  Damaged has sparks coming out of it and destroyed will look...destroyed.

3 of the main power cores
To create the power core prefab, I grabbed a free model asset off the Unity Asset Store.  I added a panel and a canvas object so that I could put UI text into the world space.  The lower text changes to display the current status.

The code to interact with the cores (repairing and powering-up) sits in the player script, but the current state is tracked in each power core's script.  Here is the C# code so far in case it's interesting to anyone:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class PowerCoreController : MonoBehaviour {

    public int coreState;       //0 is off, 1 is on, 2 is damaged, 3 is destroyed
    public Light glowLight;
    public bool lightOn;        //false means light is off, true it's on
    public float minGlow;
    public float maxGlow;
    public float curGlow;
    public bool rampUp;
    public ParticleSystem sparks;
    public Text coreStatus;


// Use this for initialization
void Start () {
        minGlow = 5f;
        maxGlow = 10f;
        curGlow = 5f;
        lightOn = false;
        sparks.gameObject.SetActive(false);
        updateCore();
}

    void FixedUpdate ()
    {
        //This just makes the light glow
        if ((lightOn) && (rampUp))
        {
            curGlow += Time.deltaTime + 0.1f;
            glowLight.intensity = curGlow;
            if(curGlow > maxGlow)
            {
                rampUp = false;
            }
        }
        if ((lightOn) && (!rampUp))
        {
            curGlow -= Time.deltaTime + 0.1f;
            glowLight.intensity = curGlow;
            if (curGlow < minGlow)
            {
                rampUp = true;
            }
        }

    }

    public void updateCore()
    {
        switch (coreState)
        {
            case 0:     //core is off
                glowLight.intensity = 0;
                coreStatus.text = "Status:\nOffline";
                break;
            case 1:     //core is on
                curGlow = 5f;
                lightOn = true;
                rampUp = true;
                coreStatus.text = "Status:\nOnline";
                break;
            case 2:     //core is damaged
                glowLight.intensity = 0;
                sparks.gameObject.SetActive(true);
                coreStatus.text = "Status:\nDamaged";
                break;
            case 3:     //core is destroyed (unrepairable)
                glowLight.intensity = 0;
                sparks.gameObject.SetActive(false);
                coreStatus.text = "Status:\nDestroyed";
                break;
        }
    }
}

Comments

Popular Posts