game.c 2.22 KB
Newer Older
Max Kobelt's avatar
Max Kobelt committed
1
#include "game.h"
Max Kobelt's avatar
Max Kobelt committed
2
#include "projectile.h"
Max Kobelt's avatar
Max Kobelt committed
3
#include "alien.h"
Max Kobelt's avatar
Max Kobelt committed
4

5 6 7
#include "raylib.h"

#include <stddef.h>
Max Kobelt's avatar
Max Kobelt committed
8 9
#include <stdlib.h>
#include <stdbool.h>
10 11

extern const int GAME_SCREEN_WIDTH, GAME_SCREEN_HEIGHT, FRAME_RATE;
Max Kobelt's avatar
Max Kobelt committed
12
const int LASER_CANON_SPEED = 2;
13

Max Kobelt's avatar
Max Kobelt committed
14
Projectile *playerProj;
Max Kobelt's avatar
Max Kobelt committed
15

Max Kobelt's avatar
Max Kobelt committed
16
Aliens* aliens;
Valentin Bartschies's avatar
Valentin Bartschies committed
17
Texture2D ALIEN_UP, ALIEN_DOWN, ALIEN_SHOOT, CANON_TEXTURE;
Max Kobelt's avatar
Max Kobelt committed
18

19 20 21 22 23
Rectangle laserCanon;
Rectangle gameBorder;
Vector2 laserCanonPos;

void initGame() {
Max Kobelt's avatar
Max Kobelt committed
24
    aliens = spawnAliens();
Max Kobelt's avatar
Max Kobelt committed
25 26 27
    ALIEN_UP = LoadTexture("img/alien_up.png");
    ALIEN_DOWN = LoadTexture("img/alien_down.png");
    ALIEN_SHOOT = LoadTexture("img/alien_shoot.png");
Valentin Bartschies's avatar
Valentin Bartschies committed
28
    CANON_TEXTURE = LoadTexture("img/canon.png");
Max Kobelt's avatar
Max Kobelt committed
29

Valentin Bartschies's avatar
Valentin Bartschies committed
30 31
    laserCanonPos = (Vector2) {(float) GAME_SCREEN_WIDTH / 2, (float) GAME_SCREEN_HEIGHT - (CANON_TEXTURE.width * 2)};
    laserCanon = (Rectangle) {(float) laserCanonPos.x, (float) laserCanonPos.y, CANON_TEXTURE.width, CANON_TEXTURE.height};
Valentin Bartschies's avatar
Valentin Bartschies committed
32
    gameBorder = (Rectangle) {0.0, 0.0, (float) GAME_SCREEN_WIDTH, (float) GAME_SCREEN_HEIGHT};
33 34 35
}

void calcGame() {
Max Kobelt's avatar
Max Kobelt committed
36 37 38 39 40
    if (IsKeyDown(KEY_LEFT)) {
       if (laserCanonPos.x > 0) {
           laserCanonPos.x -= LASER_CANON_SPEED;
       }
    } else if (IsKeyDown(KEY_RIGHT)) {
Valentin Bartschies's avatar
Valentin Bartschies committed
41
        if (laserCanonPos.x < (GAME_SCREEN_WIDTH - 1) - CANON_TEXTURE.width) {
Max Kobelt's avatar
Max Kobelt committed
42
            laserCanonPos.x += LASER_CANON_SPEED;
43
        }
Max Kobelt's avatar
Max Kobelt committed
44
    }
Max Kobelt's avatar
Max Kobelt committed
45

Max Kobelt's avatar
Max Kobelt committed
46 47 48 49
    if (playerProj) {
        playerProj = projectileTick(playerProj);
    } else if (IsKeyPressed(KEY_SPACE)) {
        spawnPlayerProjectile();
50 51
    }

52 53
    laserCanon.x = laserCanonPos.x;
    laserCanon.y = laserCanonPos.y;
Max Kobelt's avatar
Max Kobelt committed
54 55

    aliensTick(aliens);
56 57 58
}

void drawGame() {
Valentin Bartschies's avatar
Valentin Bartschies committed
59
    DrawTexture(CANON_TEXTURE, (int) laserCanonPos.x, (int) laserCanonPos.y, WHITE);
Valentin Bartschies's avatar
Valentin Bartschies committed
60
    DrawRectangleLines((int) gameBorder.x, (int) gameBorder.y, (int) gameBorder.width, (int) gameBorder.height, WHITE);
Max Kobelt's avatar
Max Kobelt committed
61 62

    if (playerProj) {
Max Kobelt's avatar
Max Kobelt committed
63
        DrawRectangleRec((Rectangle) { playerProj->pos.x, playerProj->pos.y, PROJECTILE_WIDTH, PROJECTILE_HEIGHT }, WHITE);
Max Kobelt's avatar
Max Kobelt committed
64
    }
Max Kobelt's avatar
Max Kobelt committed
65
    drawAliens(aliens);
Max Kobelt's avatar
Max Kobelt committed
66 67
}

68 69 70 71
void destructGame() {
    destructAliens(aliens);
}

Max Kobelt's avatar
Max Kobelt committed
72 73 74
void spawnPlayerProjectile() {
    playerProj = malloc(sizeof *playerProj);
    playerProj->movingUp = true;
Max Kobelt's avatar
Max Kobelt committed
75
    playerProj->pos = (Vector2) { laserCanonPos.x + laserCanon.width / 2, laserCanonPos.y - PROJECTILE_HEIGHT };
Max Kobelt's avatar
Max Kobelt committed
76
}