~ndiddy/libremo

f8a18edb21f5332143cbd4908a5e3f5fcdb93437 — Nathan Misner 2 years ago 091f473
made the ship door functional
4 files changed, 139 insertions(+), 65 deletions(-)

M enemy.cpp
M game.cpp
M hud.cpp
M hud.h
M enemy.cpp => enemy.cpp +3 -3
@@ 102,7 102,7 @@ Enemy::Enemy() : SpriteInfo() {

Enemy::Enemy(int door) : SpriteInfo(enemyTexture->raw, &enemyRect, NULL) {
	this->door = door;
	Hud_OpenDoor(door);
	Hud_OpenEnemyDoor(door);
	if (door == LEFT_DOOR) {
		x = HUD_DOOR_LEFTX;
	}


@@ 176,13 176,13 @@ void Enemy::move() {

	switch (state) {
	case ENEMY_STATE_INIT:
		if (Hud_IsDoorOpen(door)) {
		if (Hud_IsEnemyDoorOpen(door)) {
			if (y < HUD_DOOR_YSIZE) {
				y += Speed_Multiplier();
			}
			else {
				state = ENEMY_STATE_NORMAL;
				Hud_CloseDoor(door);
				Hud_CloseEnemyDoor(door);
			}
		}
		break;

M game.cpp => game.cpp +11 -1
@@ 45,6 45,7 @@ typedef enum {
    STATE_GAME_FADELEVEL,
    STATE_GAME_LOSS,
    STATE_GAME_CENTERSHIP,
    STATE_GAME_SHIPDOOROPEN,
    STATE_GAME_FLYUP,
    STATE_GAME_FLYDOWN,
    STATE_GAME_FLYOUT,


@@ 235,6 236,8 @@ void Game_Init(int level, int numLevels) {

    // make sure blocks are opaque
    Block_SetAlpha(255);
    // make sure the ship door is closed
    Hud_CloseShipDoor();
    gameLevel = std::make_unique<Level>(LEFT_WALL, TOP_WALL, gameLevelNum, BLOCK_MODE_NORMAL);

    state = STATE_GAME_FADEIN;


@@ 596,11 599,18 @@ int Game_Run() {
        }

        if (shipSprite.x == SHIP_STARTX) {
            state = STATE_GAME_FLYUP;
            state = STATE_GAME_SHIPDOOROPEN;
        }

        break;

    // door at the top opens to allow the ship to pass through
    case STATE_GAME_SHIPDOOROPEN:
        if (Hud_OpenShipDoor()) {
            state = STATE_GAME_FLYUP;
        }
        break;

    // ship moves up while transforming from a paddle to a ship
    case STATE_GAME_FLYUP:
        if (shipSprite.y > SHIP_FLY1) {

M hud.cpp => hud.cpp +113 -56
@@ 88,24 88,34 @@ static float rollTimer;
#define ROLL_NUMFRAMES (8)
static int rollFrame; // for the rolling thing on the ceiling's animation

// for the door that the enemies go through
static SpriteInfo doorSprite;
#define NUM_DOORS (2)
static int doorState[NUM_DOORS];
// for the ship door
#define SHIP_DOOR_OPENING_X (HUD_WALL_LEFTX + (11 * HUD_WALL_SIZE))
#define SHIP_DOOR_LEFTX (SHIP_DOOR_OPENING_X + (2 * HUD_WALL_SIZE))
#define SHIP_DOOR_RIGHTX (SHIP_DOOR_OPENING_X + (3 * HUD_WALL_SIZE))
#define SHIP_DOOR_OPENING_LEFTX (SHIP_DOOR_OPENING_X)
#define SHIP_DOOR_OPENING_RIGHTX (SHIP_DOOR_OPENING_X + (5 * HUD_WALL_SIZE))
#define SHIP_DOOR_MAX_OPEN (16)
static float shipDoorOpen;

// for the enemy door
static SpriteInfo enemyDoorSprite;
#define NUM_ENEMY_DOORS (2)
static int enemyDoorState[NUM_ENEMY_DOORS];
typedef enum {
    DOOR_NONE = 0,
    DOOR_OPENING,
    DOOR_CLOSING,
} DOOR_STATE;
    ENEMY_DOOR_NONE = 0,
    ENEMY_DOOR_OPENING,
    ENEMY_DOOR_CLOSING,
} ENEMY_DOOR_STATE;

static int doorPos[NUM_DOORS] = { HUD_DOOR_LEFTX, HUD_DOOR_RIGHTX };
static int enemyDoorPos[NUM_ENEMY_DOORS] = { HUD_DOOR_LEFTX, HUD_DOOR_RIGHTX };

#define DOOR_TIMING (8)
static float doorTimer[NUM_DOORS];
#define DOOR_MIN (0)
#define DOOR_MAX (5)
static int doorFrame[NUM_DOORS];
#define ENEMY_DOOR_TIMING (8)
static float enemyDoorTimer[NUM_ENEMY_DOORS];
#define ENEMY_DOOR_MIN (0)
#define ENEMY_DOOR_MAX (5)
static int enemyDoorFrame[NUM_ENEMY_DOORS];

// for printing score
#define MAX_SCORE (9999999)
#define SCORE_DIGITS (7)
#define SCORE_X (hudSprite.x + 341)


@@ 314,12 324,12 @@ const int ceilArr[] = {
    WALL_TBLUE | WALL_HMIRROR,
    WALL_TOP,
    WALL_TOP,
    WALL_DOOR1,
    WALL_DOOR1,
    WALL_DOOR2,
    WALL_DOOR3,
    WALL_DOOR1,
    WALL_DOOR1,
    WALL_NONE,
    WALL_NONE,
    WALL_NONE,
    WALL_NONE,
    WALL_NONE,
    WALL_NONE,
    WALL_TOP,
    WALL_TOP,
    WALL_TBLUE,


@@ 426,51 436,95 @@ static void Hud_DispWall() {
    }
}

void Hud_OpenDoor(int num) {
    doorFrame[num] = DOOR_MIN;
    doorTimer[num] = 0;
    doorState[num] = DOOR_OPENING;
int Hud_OpenShipDoor() {
    shipDoorOpen += Speed_Multiplier();
    if (shipDoorOpen >= SHIP_DOOR_MAX_OPEN) {
        shipDoorOpen = SHIP_DOOR_MAX_OPEN;
        return 1;
    }

    return 0;
}

void Hud_CloseDoor(int num) {
    doorFrame[num] = DOOR_MAX;
    doorTimer[num] = 0;
    doorState[num] = DOOR_CLOSING;
void Hud_CloseShipDoor() {
    shipDoorOpen = 0;
}

int Hud_IsDoorOpen(int num) {
    return (doorState[num] == DOOR_NONE) && (doorFrame[num] == DOOR_MAX);
static void Hud_DispShipDoor() {
    float leftDoorPos = SHIP_DOOR_LEFTX - shipDoorOpen;
    if (leftDoorPos < SHIP_DOOR_OPENING_LEFTX) { leftDoorPos = SHIP_DOOR_OPENING_LEFTX; }
    float rightDoorPos = SHIP_DOOR_RIGHTX + shipDoorOpen;
    if (rightDoorPos > SHIP_DOOR_OPENING_RIGHTX) { rightDoorPos = SHIP_DOOR_OPENING_RIGHTX; }

    // fill in the door
    for (int i = 0; i < (leftDoorPos - SHIP_DOOR_OPENING_LEFTX) / HUD_WALL_SIZE; i++) {
        // left
        wallSprite.x = SHIP_DOOR_OPENING_LEFTX + (HUD_WALL_SIZE * i);
        wallSprite.y = 0;
        wallSprite.source.x = WALL_DOOR1;
        wallSprite.draw();

        // right
        wallSprite.x = SHIP_DOOR_OPENING_RIGHTX - (HUD_WALL_SIZE * i);
        wallSprite.draw();
    }

    // draw left door
    wallSprite.x = leftDoorPos;
    wallSprite.y = 0;
    wallSprite.source.x = WALL_DOOR2;
    wallSprite.draw();
    // draw right door
    wallSprite.x = rightDoorPos;
    wallSprite.source.x = WALL_DOOR3;
    wallSprite.draw();
}

void Hud_OpenEnemyDoor(int num) {
    enemyDoorFrame[num] = ENEMY_DOOR_MIN;
    enemyDoorTimer[num] = 0;
    enemyDoorState[num] = ENEMY_DOOR_OPENING;
}

static void Hud_DispDoor() {
    for (int i = 0; i < NUM_DOORS; i++) {
        switch (doorState[i]) {
        case DOOR_OPENING:
            doorTimer[i] += Speed_Multiplier();
            if (doorTimer[i] >= DOOR_TIMING) {
                doorTimer[i] = 0;
                doorFrame[i]++;
                if (doorFrame[i] >= DOOR_MAX) {
                    doorState[i] = DOOR_NONE;
void Hud_CloseEnemyDoor(int num) {
    enemyDoorFrame[num] = ENEMY_DOOR_MAX;
    enemyDoorTimer[num] = 0;
    enemyDoorState[num] = ENEMY_DOOR_CLOSING;
}

int Hud_IsEnemyDoorOpen(int num) {
    return (enemyDoorState[num] == ENEMY_DOOR_NONE) && (enemyDoorFrame[num] == ENEMY_DOOR_MAX);
}

static void Hud_DispEnemyDoor() {
    for (int i = 0; i < NUM_ENEMY_DOORS; i++) {
        switch (enemyDoorState[i]) {
        case ENEMY_DOOR_OPENING:
            enemyDoorTimer[i] += Speed_Multiplier();
            if (enemyDoorTimer[i] >= ENEMY_DOOR_TIMING) {
                enemyDoorTimer[i] = 0;
                enemyDoorFrame[i]++;
                if (enemyDoorFrame[i] >= ENEMY_DOOR_MAX) {
                    enemyDoorState[i] = ENEMY_DOOR_NONE;
                }
            }
            break;

        case DOOR_CLOSING:
            doorTimer[i] += Speed_Multiplier();
            if (doorTimer[i] >= DOOR_TIMING) {
                doorTimer[i] = 0;
                doorFrame[i]--;
                if (doorFrame[i] <= 0) {
                    doorState[i] = DOOR_NONE;
        case ENEMY_DOOR_CLOSING:
            enemyDoorTimer[i] += Speed_Multiplier();
            if (enemyDoorTimer[i] >= ENEMY_DOOR_TIMING) {
                enemyDoorTimer[i] = 0;
                enemyDoorFrame[i]--;
                if (enemyDoorFrame[i] <= 0) {
                    enemyDoorState[i] = ENEMY_DOOR_NONE;
                }
            }
            break;
        }
        doorSprite.x = (float)doorPos[i];
        doorSprite.y = 0;
        doorSprite.source.y = doorFrame[i] * HUD_DOOR_YSIZE;
        doorSprite.draw();
        enemyDoorSprite.x = (float)enemyDoorPos[i];
        enemyDoorSprite.y = 0;
        enemyDoorSprite.source.y = enemyDoorFrame[i] * HUD_DOOR_YSIZE;
        enemyDoorSprite.draw();
    }
}



@@ 568,10 622,12 @@ void Hud_Init() {
    rollTimer = 0;
    rollFrame = 0;

    doorSprite = SpriteInfo("enemydoor.png");
    doorSprite.source.h = HUD_DOOR_YSIZE;
    doorState[0] = DOOR_NONE;
    doorState[1] = DOOR_NONE;
    shipDoorOpen = 0;

    enemyDoorSprite = SpriteInfo("enemydoor.png");
    enemyDoorSprite.source.h = HUD_DOOR_YSIZE;
    enemyDoorState[0] = ENEMY_DOOR_NONE;
    enemyDoorState[1] = ENEMY_DOOR_NONE;

    blinkTimer = Hud_SetBlinkTimer();
	


@@ 618,7 674,8 @@ void Hud_Disp(int score, int lives, int powerUp, int turbo) {
    starScroll += 3;

    Hud_DispWall();
    Hud_DispDoor();
    Hud_DispShipDoor();
    Hud_DispEnemyDoor();
 	Hud_DispScore(score);
    Hud_DispPowerUp(powerUp);
    Hud_DispLives(lives);

M hud.h => hud.h +12 -5
@@ 47,14 47,21 @@ typedef enum {
// sets the chip animation state machine to a given state
void Hud_ChipSet(int state);

// opens the ship door. Should be run every frame until it returns 1 (meaning
// that the door is fully open)
int Hud_OpenShipDoor();

// fully closes the ship door in one frame
void Hud_CloseShipDoor();

#define HUD_LEFT_DOOR (0)
#define HUD_RIGHT_DOOR (1)

// opens the selected door
void Hud_OpenDoor(int num);
// opens the selected enemy door
void Hud_OpenEnemyDoor(int num);

// closes the selected door
void Hud_CloseDoor(int num);
// closes the selected enemy door
void Hud_CloseEnemyDoor(int num);

// returns 1 if the door is fully open, 0 otherwise
int Hud_IsDoorOpen(int num);
int Hud_IsEnemyDoorOpen(int num);