Friday, June 5, 2015

P2D5

Made an invisible spawner that spawns enemies at a regular interval.  Enemies now walk to the right side of the screen and pause periodically (also regular interval.)

Next steps: make enemies walk in a circle around the outer edge of the screen; have enemies fire towards player when they pause; make enemy movement directions/pause intervals/spawn intervals more random; display player score

Note: I couldn't figure out the proper grammar when checking for a false bool with another condition (&&), so I ended up making the pause-checker an int instead.  I'm sure this is bad in some way.  I will look into how to properly do this with bool instead.

Codes:
Added enemyControl for movement.  Using 2 timestamps to calculate pause interval and pause time.  Seems kind of clunky?
public class enemyControl : MonoBehaviour {
    private float timeStamp5;
    public float enemySpeed = 1f;
    public float pauseRate = 2f;
    private float timeStamp6;
    private int ispaused;
    public float pauseTime = 0.8f;

    // Use this for initialization
    void Start () {
        timeStamp5 = Time.time;
        timeStamp6 = Time.time;
        GetComponent<Rigidbody2D> ().velocity = new Vector2 (enemySpeed, 0);
        ispaused = 0;
    }
   
    // Update is called once per frame
    void Update () {

        if ((ispaused == 0) && Time.time - timeStamp6 >= pauseTime)
        {
            timeStamp5 = Time.time;
            GetComponent<Rigidbody2D> ().velocity = new Vector2 (enemySpeed, 0);
            ispaused = 1;
        }
        if ((ispaused == 1) && Time.time - timeStamp5 >= pauseRate) {
            GetComponent<Rigidbody2D> ().velocity = new Vector2 (0, 0);
            timeStamp6 = Time.time;
            ispaused = 0;
        }
   
    }

    void OnBecameInvisible()
    {
        DestroyObject(this.gameObject);
    }
}


Made spawner script that has an initial pause at game start, then spawn things at a fixed rate:

public class enemyspawn : MonoBehaviour {
    private float timeStamp3;
    public float spawnDelay;
    private bool firstSpawn;
    public GameObject enemy1;
    public float firstDelay = 0.8f;

    // Use this for initialization
    void Start () {
        timeStamp3 = Time.time;
        firstSpawn = true;
        //Mathf.Clamp (1, 10);  //how to define min/max value
   
    }
   
    // Update is called once per frame
    void Update () {
        if (firstSpawn && Time.time - timeStamp3 >= firstDelay)
        {
            Instantiate (enemy1, transform.position, Quaternion.identity);
            firstSpawn = false;
            Debug.Log (firstSpawn);
            timeStamp3 = Time.time;
        }

        else if (Time.time - timeStamp3 >= spawnDelay)
        {
            Instantiate (enemy1, transform.position, Quaternion.identity);
            timeStamp3 = Time.time;
        }

    }
}


Bonus learning: how to clamp a value between 1-10 (untested)

Mathf.Clamp (1, 10);

No comments:

Post a Comment