Carlos Lopez Infante
Carlos Lopez Infante

Reputation: 107

Y movement in Space Invaders (Unity)

I am making a Space Invaders clone. The enemies start in the left side of the screen, go to right and then down, but i want to know how to stop and go to left, as the original videogames. Thanks!

  #pragma strict

// ESTADOS DEL ESCUADRON

enum TIPOS_MOVIMIENTO{
    ABAJO = 0,
    IZQ = 1,
    DER = 2,
    PARADO = 3
}

var estado : TIPOS_MOVIMIENTO;
private var cacheTransform : Transform;
var limitelateral : int;
var velocidad : float ;
private var vectorMov : Vector3;

function Start () {
    cacheTransform = transform;

}

function Update () {
    ComprobarEstado();
}


function ComprobarEstado(){
    switch(estado){
        case TIPOS_MOVIMIENTO.ABAJO:
            // TODO: mueve abajo el escuadron
            if(cacheTransform.position.x <= limitelateral) {
            vectorMov = Vector3.down * velocidad;
            cacheTransform.Translate(vectorMov * Time.deltaTime);
            } else {
                estado = TIPOS_MOVIMIENTO.IZQ;

            }
        break;
        case TIPOS_MOVIMIENTO.DER:
            // TODO: mueve der el escuadron
            // Si el escuadron esta menor que el limite, mueve derecha
            if(cacheTransform.position.x < limitelateral){
                vectorMov = Vector3.right * velocidad;
                cacheTransform.Translate(vectorMov * Time.deltaTime);
            } else {
                estado = TIPOS_MOVIMIENTO.ABAJO;

            }
        break;
        case TIPOS_MOVIMIENTO.IZQ:
            // TODO: mueve izq el escuadron
            if(cacheTransform.position.x > limitelateral)
            {
                vectorMov = Vector3.left * velocidad;
                cacheTransform.Translate(vectorMov * Time.deltaTime);
            } else {
                estado = TIPOS_MOVIMIENTO.ABAJO;

            }
        break;
        case TIPOS_MOVIMIENTO.PARADO:
            // TODO: detiene el escuadron
            if(cacheTransform.position.y < limitelateral){
                vectorMov = Vector3.down * 0;
                cacheTransform.Translate(vectorMov * Time.deltaTime);
            }
        break;
    }

}

Thanks you very much!

Upvotes: 2

Views: 843

Answers (1)

Aaron Digulla
Aaron Digulla

Reputation: 328790

Split the problem into two: Create a method that can paint all the (surviving) enemies relative to some offset and create an iterator which gives you all the offsets.

Here is some pseudo code for the iterator:

init() {
   x = 0
   y = 0

   width = 5
   maxHeight = 5
}

next() {
    x ++
    if( x >= width ) {
        y ++
        x = 0
    }
    return y < maxHeight
}

getX() {
    return ( y & 1 ) ? width - x : x
}

Call next() until it returns false and getX() to get the correct X position for the current y offset

Upvotes: 1

Related Questions