2016-01-29 18:26:06 +03:00
|
|
|
/*******************************************************************************************
|
|
|
|
*
|
|
|
|
* raylib - sample game: asteroids
|
|
|
|
*
|
|
|
|
* Sample game developed by Ian Eito, Albert Martos and Ramon Santamaria
|
|
|
|
*
|
|
|
|
* This game has been created using raylib v1.3 (www.raylib.com)
|
|
|
|
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2015 Ramon Santamaria (@raysan5)
|
|
|
|
*
|
|
|
|
********************************************************************************************/
|
|
|
|
|
|
|
|
#include "raylib.h"
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
|
2017-10-22 12:12:10 +03:00
|
|
|
#if defined(PLATFORM_ANDROID)
|
|
|
|
#include "android_native_app_glue.h"
|
|
|
|
#endif
|
|
|
|
|
2016-01-29 18:26:06 +03:00
|
|
|
#if defined(PLATFORM_WEB)
|
|
|
|
#include <emscripten/emscripten.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Some Defines
|
|
|
|
//----------------------------------------------------------------------------------
|
2016-02-07 13:23:12 +03:00
|
|
|
#define PLAYER_BASE_SIZE 20.0f
|
|
|
|
#define PLAYER_SPEED 6.0f
|
|
|
|
#define PLAYER_MAX_SHOOTS 10
|
|
|
|
|
2016-01-29 18:26:06 +03:00
|
|
|
#define METEORS_SPEED 2
|
2016-02-07 13:23:12 +03:00
|
|
|
#define MAX_BIG_METEORS 4
|
|
|
|
#define MAX_MEDIUM_METEORS 8
|
|
|
|
#define MAX_SMALL_METEORS 16
|
2016-01-29 18:26:06 +03:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Types and Structures Definition
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
typedef struct Player {
|
|
|
|
Vector2 position;
|
|
|
|
Vector2 speed;
|
|
|
|
float acceleration;
|
|
|
|
float rotation;
|
|
|
|
Vector3 collider;
|
|
|
|
Color color;
|
|
|
|
} Player;
|
|
|
|
|
|
|
|
typedef struct Shoot {
|
|
|
|
Vector2 position;
|
|
|
|
Vector2 speed;
|
|
|
|
float radius;
|
|
|
|
float rotation;
|
|
|
|
int lifeSpawn;
|
|
|
|
bool active;
|
|
|
|
Color color;
|
|
|
|
} Shoot;
|
|
|
|
|
2016-02-07 13:23:12 +03:00
|
|
|
typedef struct Meteor {
|
2016-01-29 18:26:06 +03:00
|
|
|
Vector2 position;
|
|
|
|
Vector2 speed;
|
|
|
|
float radius;
|
|
|
|
bool active;
|
|
|
|
Color color;
|
2016-02-07 13:23:12 +03:00
|
|
|
} Meteor;
|
2016-01-29 18:26:06 +03:00
|
|
|
|
|
|
|
//------------------------------------------------------------------------------------
|
|
|
|
// Global Variables Declaration
|
|
|
|
//------------------------------------------------------------------------------------
|
|
|
|
static int screenWidth = 800;
|
|
|
|
static int screenHeight = 450;
|
|
|
|
|
|
|
|
static bool gameOver;
|
|
|
|
static bool pause;
|
|
|
|
static bool victory;
|
|
|
|
|
|
|
|
// NOTE: Defined triangle is isosceles with common angles of 70 degrees.
|
|
|
|
static float shipHeight;
|
|
|
|
|
|
|
|
static Player player;
|
2016-02-07 13:23:12 +03:00
|
|
|
static Shoot shoot[PLAYER_MAX_SHOOTS];
|
|
|
|
static Meteor bigMeteor[MAX_BIG_METEORS];
|
|
|
|
static Meteor mediumMeteor[MAX_MEDIUM_METEORS];
|
|
|
|
static Meteor smallMeteor[MAX_SMALL_METEORS];
|
2016-01-29 18:26:06 +03:00
|
|
|
|
2017-05-15 19:20:27 +03:00
|
|
|
static int midMeteorsCount;
|
|
|
|
static int smallMeteorsCount;
|
|
|
|
static int destroyedMeteorsCount;
|
2016-01-29 18:26:06 +03:00
|
|
|
|
|
|
|
//------------------------------------------------------------------------------------
|
|
|
|
// Module Functions Declaration (local)
|
|
|
|
//------------------------------------------------------------------------------------
|
|
|
|
static void InitGame(void); // Initialize game
|
|
|
|
static void UpdateGame(void); // Update game (one frame)
|
|
|
|
static void DrawGame(void); // Draw game (one frame)
|
|
|
|
static void UnloadGame(void); // Unload game
|
|
|
|
static void UpdateDrawFrame(void); // Update and Draw (one frame)
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------------
|
|
|
|
// Program main entry point
|
|
|
|
//------------------------------------------------------------------------------------
|
2017-10-22 12:12:10 +03:00
|
|
|
#if defined(PLATFORM_ANDROID)
|
|
|
|
void android_main(struct android_app *app)
|
|
|
|
#else
|
|
|
|
int main(void)
|
|
|
|
#endif
|
2016-01-29 18:26:06 +03:00
|
|
|
{
|
|
|
|
// Initialization
|
2017-10-22 12:12:10 +03:00
|
|
|
//---------------------------------------------------------
|
|
|
|
#if defined(PLATFORM_ANDROID)
|
|
|
|
InitWindow(screenWidth, screenHeight, app);
|
|
|
|
#else
|
2016-01-29 18:26:06 +03:00
|
|
|
InitWindow(screenWidth, screenHeight, "sample game: asteroids");
|
2017-10-22 12:12:10 +03:00
|
|
|
#endif
|
2016-01-29 18:26:06 +03:00
|
|
|
|
|
|
|
InitGame();
|
|
|
|
|
|
|
|
#if defined(PLATFORM_WEB)
|
|
|
|
emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
|
|
|
|
#else
|
|
|
|
|
|
|
|
SetTargetFPS(60);
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Main game loop
|
|
|
|
while (!WindowShouldClose()) // Detect window close button or ESC key
|
|
|
|
{
|
2017-10-22 12:12:10 +03:00
|
|
|
// Update and Draw
|
2016-01-29 18:26:06 +03:00
|
|
|
//----------------------------------------------------------------------------------
|
2017-10-22 12:12:10 +03:00
|
|
|
UpdateDrawFrame();
|
2016-01-29 18:26:06 +03:00
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// De-Initialization
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
UnloadGame(); // Unload loaded data (textures, sounds, models...)
|
|
|
|
|
|
|
|
CloseWindow(); // Close window and OpenGL context
|
|
|
|
//--------------------------------------------------------------------------------------
|
2017-10-22 12:12:10 +03:00
|
|
|
#if !defined(PLATFORM_ANDROID)
|
2016-01-29 18:26:06 +03:00
|
|
|
return 0;
|
2017-10-22 12:12:10 +03:00
|
|
|
#endif
|
2016-01-29 18:26:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------------
|
|
|
|
// Module Functions Definitions (local)
|
|
|
|
//------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Initialize game variables
|
|
|
|
void InitGame(void)
|
|
|
|
{
|
|
|
|
int posx, posy;
|
|
|
|
int velx, vely;
|
|
|
|
bool correctRange = false;
|
|
|
|
victory = false;
|
|
|
|
pause = false;
|
|
|
|
|
2016-02-07 13:23:12 +03:00
|
|
|
shipHeight = (PLAYER_BASE_SIZE/2)/tanf(20*DEG2RAD);
|
2016-01-29 18:26:06 +03:00
|
|
|
|
|
|
|
// Initialization player
|
|
|
|
player.position = (Vector2){screenWidth/2, screenHeight/2 - shipHeight/2};
|
|
|
|
player.speed = (Vector2){0, 0};
|
|
|
|
player.acceleration = 0;
|
|
|
|
player.rotation = 0;
|
|
|
|
player.collider = (Vector3){player.position.x + sin(player.rotation*DEG2RAD)*(shipHeight/2.5f), player.position.y - cos(player.rotation*DEG2RAD)*(shipHeight/2.5f), 12};
|
|
|
|
player.color = LIGHTGRAY;
|
|
|
|
|
2017-05-15 19:20:27 +03:00
|
|
|
destroyedMeteorsCount = 0;
|
2016-01-29 18:26:06 +03:00
|
|
|
|
|
|
|
// Initialization shoot
|
2016-02-07 13:23:12 +03:00
|
|
|
for (int i = 0; i < PLAYER_MAX_SHOOTS; i++)
|
2016-01-29 18:26:06 +03:00
|
|
|
{
|
|
|
|
shoot[i].position = (Vector2){0, 0};
|
|
|
|
shoot[i].speed = (Vector2){0, 0};
|
|
|
|
shoot[i].radius = 2;
|
|
|
|
shoot[i].active = false;
|
|
|
|
shoot[i].lifeSpawn = 0;
|
|
|
|
shoot[i].color = WHITE;
|
|
|
|
}
|
|
|
|
|
2016-02-07 13:23:12 +03:00
|
|
|
for (int i = 0; i < MAX_BIG_METEORS; i++)
|
2016-01-29 18:26:06 +03:00
|
|
|
{
|
|
|
|
posx = GetRandomValue(0, screenWidth);
|
|
|
|
|
|
|
|
while(!correctRange)
|
|
|
|
{
|
|
|
|
if (posx > screenWidth/2 - 150 && posx < screenWidth/2 + 150) posx = GetRandomValue(0, screenWidth);
|
|
|
|
else correctRange = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
correctRange = false;
|
|
|
|
|
|
|
|
posy = GetRandomValue(0, screenHeight);
|
|
|
|
|
|
|
|
while(!correctRange)
|
|
|
|
{
|
|
|
|
if (posy > screenHeight/2 - 150 && posy < screenHeight/2 + 150) posy = GetRandomValue(0, screenHeight);
|
|
|
|
else correctRange = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bigMeteor[i].position = (Vector2){posx, posy};
|
|
|
|
|
|
|
|
correctRange = false;
|
|
|
|
velx = GetRandomValue(-METEORS_SPEED, METEORS_SPEED);
|
|
|
|
vely = GetRandomValue(-METEORS_SPEED, METEORS_SPEED);
|
|
|
|
|
|
|
|
while(!correctRange)
|
|
|
|
{
|
|
|
|
if (velx == 0 && vely == 0)
|
|
|
|
{
|
|
|
|
velx = GetRandomValue(-METEORS_SPEED, METEORS_SPEED);
|
|
|
|
vely = GetRandomValue(-METEORS_SPEED, METEORS_SPEED);
|
|
|
|
}
|
|
|
|
else correctRange = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bigMeteor[i].speed = (Vector2){velx, vely};
|
|
|
|
bigMeteor[i].radius = 40;
|
|
|
|
bigMeteor[i].active = true;
|
|
|
|
bigMeteor[i].color = BLUE;
|
|
|
|
}
|
|
|
|
|
2016-02-07 13:23:12 +03:00
|
|
|
for (int i = 0; i < MAX_MEDIUM_METEORS; i++)
|
2016-01-29 18:26:06 +03:00
|
|
|
{
|
|
|
|
mediumMeteor[i].position = (Vector2){-100, -100};
|
|
|
|
mediumMeteor[i].speed = (Vector2){0,0};
|
|
|
|
mediumMeteor[i].radius = 20;
|
|
|
|
mediumMeteor[i].active = false;
|
|
|
|
mediumMeteor[i].color = BLUE;
|
|
|
|
}
|
|
|
|
|
2016-02-07 13:23:12 +03:00
|
|
|
for (int i = 0; i < MAX_SMALL_METEORS; i++)
|
2016-01-29 18:26:06 +03:00
|
|
|
{
|
|
|
|
smallMeteor[i].position = (Vector2){-100, -100};
|
|
|
|
smallMeteor[i].speed = (Vector2){0,0};
|
|
|
|
smallMeteor[i].radius = 10;
|
|
|
|
smallMeteor[i].active = false;
|
|
|
|
smallMeteor[i].color = BLUE;
|
|
|
|
}
|
|
|
|
|
2017-05-15 19:20:27 +03:00
|
|
|
midMeteorsCount = 0;
|
|
|
|
smallMeteorsCount = 0;
|
2016-01-29 18:26:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update game (one frame)
|
|
|
|
void UpdateGame(void)
|
|
|
|
{
|
|
|
|
if (!gameOver)
|
|
|
|
{
|
|
|
|
if (IsKeyPressed('P')) pause = !pause;
|
|
|
|
|
|
|
|
if (!pause)
|
|
|
|
{
|
2017-05-15 19:20:27 +03:00
|
|
|
// Player logic: rotation
|
2016-01-29 18:26:06 +03:00
|
|
|
if (IsKeyDown(KEY_LEFT)) player.rotation -= 5;
|
|
|
|
if (IsKeyDown(KEY_RIGHT)) player.rotation += 5;
|
|
|
|
|
2017-05-15 19:20:27 +03:00
|
|
|
// Player logic: speed
|
2016-02-07 13:23:12 +03:00
|
|
|
player.speed.x = sin(player.rotation*DEG2RAD)*PLAYER_SPEED;
|
|
|
|
player.speed.y = cos(player.rotation*DEG2RAD)*PLAYER_SPEED;
|
2016-01-29 18:26:06 +03:00
|
|
|
|
2017-05-15 19:20:27 +03:00
|
|
|
// Player logic: acceleration
|
2016-01-29 18:26:06 +03:00
|
|
|
if (IsKeyDown(KEY_UP))
|
|
|
|
{
|
|
|
|
if (player.acceleration < 1) player.acceleration += 0.04f;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (player.acceleration > 0) player.acceleration -= 0.02f;
|
|
|
|
else if (player.acceleration < 0) player.acceleration = 0;
|
|
|
|
}
|
|
|
|
if (IsKeyDown(KEY_DOWN))
|
|
|
|
{
|
|
|
|
if (player.acceleration > 0) player.acceleration -= 0.04f;
|
|
|
|
else if (player.acceleration < 0) player.acceleration = 0;
|
|
|
|
}
|
|
|
|
|
2017-05-15 19:20:27 +03:00
|
|
|
// Player logic: movement
|
2016-01-29 18:26:06 +03:00
|
|
|
player.position.x += (player.speed.x*player.acceleration);
|
|
|
|
player.position.y -= (player.speed.y*player.acceleration);
|
|
|
|
|
2017-05-15 19:20:27 +03:00
|
|
|
// Collision logic: player vs walls
|
2016-01-29 18:26:06 +03:00
|
|
|
if (player.position.x > screenWidth + shipHeight) player.position.x = -(shipHeight);
|
|
|
|
else if (player.position.x < -(shipHeight)) player.position.x = screenWidth + shipHeight;
|
|
|
|
if (player.position.y > (screenHeight + shipHeight)) player.position.y = -(shipHeight);
|
|
|
|
else if (player.position.y < -(shipHeight)) player.position.y = screenHeight + shipHeight;
|
|
|
|
|
2017-05-15 19:20:27 +03:00
|
|
|
// Player shoot logic
|
2016-01-29 18:26:06 +03:00
|
|
|
if (IsKeyPressed(KEY_SPACE))
|
|
|
|
{
|
2016-02-07 13:23:12 +03:00
|
|
|
for (int i = 0; i < PLAYER_MAX_SHOOTS; i++)
|
2016-01-29 18:26:06 +03:00
|
|
|
{
|
|
|
|
if (!shoot[i].active)
|
|
|
|
{
|
|
|
|
shoot[i].position = (Vector2){ player.position.x + sin(player.rotation*DEG2RAD)*(shipHeight), player.position.y - cos(player.rotation*DEG2RAD)*(shipHeight) };
|
|
|
|
shoot[i].active = true;
|
2016-02-07 13:23:12 +03:00
|
|
|
shoot[i].speed.x = 1.5*sin(player.rotation*DEG2RAD)*PLAYER_SPEED;
|
|
|
|
shoot[i].speed.y = 1.5*cos(player.rotation*DEG2RAD)*PLAYER_SPEED;
|
2016-01-29 18:26:06 +03:00
|
|
|
shoot[i].rotation = player.rotation;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shoot life timer
|
2016-02-07 13:23:12 +03:00
|
|
|
for (int i = 0; i < PLAYER_MAX_SHOOTS; i++)
|
2016-01-29 18:26:06 +03:00
|
|
|
{
|
|
|
|
if (shoot[i].active) shoot[i].lifeSpawn++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shot logic
|
2016-02-07 13:23:12 +03:00
|
|
|
for (int i = 0; i < PLAYER_MAX_SHOOTS; i++)
|
2016-01-29 18:26:06 +03:00
|
|
|
{
|
|
|
|
if (shoot[i].active)
|
|
|
|
{
|
|
|
|
// Movement
|
|
|
|
shoot[i].position.x += shoot[i].speed.x;
|
|
|
|
shoot[i].position.y -= shoot[i].speed.y;
|
|
|
|
|
2017-05-15 19:20:27 +03:00
|
|
|
// Collision logic: shoot vs walls
|
2016-01-29 18:26:06 +03:00
|
|
|
if (shoot[i].position.x > screenWidth + shoot[i].radius)
|
|
|
|
{
|
|
|
|
shoot[i].active = false;
|
|
|
|
shoot[i].lifeSpawn = 0;
|
|
|
|
}
|
|
|
|
else if (shoot[i].position.x < 0 - shoot[i].radius)
|
|
|
|
{
|
|
|
|
shoot[i].active = false;
|
|
|
|
shoot[i].lifeSpawn = 0;
|
|
|
|
}
|
|
|
|
if (shoot[i].position.y > screenHeight + shoot[i].radius)
|
|
|
|
{
|
|
|
|
shoot[i].active = false;
|
|
|
|
shoot[i].lifeSpawn = 0;
|
|
|
|
}
|
|
|
|
else if (shoot[i].position.y < 0 - shoot[i].radius)
|
|
|
|
{
|
|
|
|
shoot[i].active = false;
|
|
|
|
shoot[i].lifeSpawn = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Life of shoot
|
|
|
|
if (shoot[i].lifeSpawn >= 60)
|
|
|
|
{
|
|
|
|
shoot[i].position = (Vector2){0, 0};
|
|
|
|
shoot[i].speed = (Vector2){0, 0};
|
|
|
|
shoot[i].lifeSpawn = 0;
|
|
|
|
shoot[i].active = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-15 19:20:27 +03:00
|
|
|
// Collision logic: player vs meteors
|
2016-01-29 18:26:06 +03:00
|
|
|
player.collider = (Vector3){player.position.x + sin(player.rotation*DEG2RAD)*(shipHeight/2.5f), player.position.y - cos(player.rotation*DEG2RAD)*(shipHeight/2.5f), 12};
|
|
|
|
|
2016-02-07 13:23:12 +03:00
|
|
|
for (int a = 0; a < MAX_BIG_METEORS; a++)
|
2016-01-29 18:26:06 +03:00
|
|
|
{
|
|
|
|
if (CheckCollisionCircles((Vector2){player.collider.x, player.collider.y}, player.collider.z, bigMeteor[a].position, bigMeteor[a].radius) && bigMeteor[a].active) gameOver = true;
|
|
|
|
}
|
|
|
|
|
2016-02-07 13:23:12 +03:00
|
|
|
for (int a = 0; a < MAX_MEDIUM_METEORS; a++)
|
2016-01-29 18:26:06 +03:00
|
|
|
{
|
|
|
|
if (CheckCollisionCircles((Vector2){player.collider.x, player.collider.y}, player.collider.z, mediumMeteor[a].position, mediumMeteor[a].radius) && mediumMeteor[a].active) gameOver = true;
|
|
|
|
}
|
|
|
|
|
2016-02-07 13:23:12 +03:00
|
|
|
for (int a = 0; a < MAX_SMALL_METEORS; a++)
|
2016-01-29 18:26:06 +03:00
|
|
|
{
|
|
|
|
if (CheckCollisionCircles((Vector2){player.collider.x, player.collider.y}, player.collider.z, smallMeteor[a].position, smallMeteor[a].radius) && smallMeteor[a].active) gameOver = true;
|
|
|
|
}
|
|
|
|
|
2017-05-15 19:20:27 +03:00
|
|
|
// Meteors logic: big meteors
|
2016-02-07 13:23:12 +03:00
|
|
|
for (int i = 0; i < MAX_BIG_METEORS; i++)
|
2016-01-29 18:26:06 +03:00
|
|
|
{
|
|
|
|
if (bigMeteor[i].active)
|
|
|
|
{
|
2017-05-15 19:20:27 +03:00
|
|
|
// Movement
|
2016-01-29 18:26:06 +03:00
|
|
|
bigMeteor[i].position.x += bigMeteor[i].speed.x;
|
|
|
|
bigMeteor[i].position.y += bigMeteor[i].speed.y;
|
|
|
|
|
2017-05-15 19:20:27 +03:00
|
|
|
// Collision logic: meteor vs wall
|
2016-01-29 18:26:06 +03:00
|
|
|
if (bigMeteor[i].position.x > screenWidth + bigMeteor[i].radius) bigMeteor[i].position.x = -(bigMeteor[i].radius);
|
|
|
|
else if (bigMeteor[i].position.x < 0 - bigMeteor[i].radius) bigMeteor[i].position.x = screenWidth + bigMeteor[i].radius;
|
|
|
|
if (bigMeteor[i].position.y > screenHeight + bigMeteor[i].radius) bigMeteor[i].position.y = -(bigMeteor[i].radius);
|
|
|
|
else if (bigMeteor[i].position.y < 0 - bigMeteor[i].radius) bigMeteor[i].position.y = screenHeight + bigMeteor[i].radius;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-15 19:20:27 +03:00
|
|
|
// Meteors logic: medium meteors
|
2016-02-07 13:23:12 +03:00
|
|
|
for (int i = 0; i < MAX_MEDIUM_METEORS; i++)
|
2016-01-29 18:26:06 +03:00
|
|
|
{
|
|
|
|
if (mediumMeteor[i].active)
|
|
|
|
{
|
2017-05-15 19:20:27 +03:00
|
|
|
// Movement
|
2016-01-29 18:26:06 +03:00
|
|
|
mediumMeteor[i].position.x += mediumMeteor[i].speed.x;
|
|
|
|
mediumMeteor[i].position.y += mediumMeteor[i].speed.y;
|
|
|
|
|
2017-05-15 19:20:27 +03:00
|
|
|
// Collision logic: meteor vs wall
|
2016-01-29 18:26:06 +03:00
|
|
|
if (mediumMeteor[i].position.x > screenWidth + mediumMeteor[i].radius) mediumMeteor[i].position.x = -(mediumMeteor[i].radius);
|
|
|
|
else if (mediumMeteor[i].position.x < 0 - mediumMeteor[i].radius) mediumMeteor[i].position.x = screenWidth + mediumMeteor[i].radius;
|
|
|
|
if (mediumMeteor[i].position.y > screenHeight + mediumMeteor[i].radius) mediumMeteor[i].position.y = -(mediumMeteor[i].radius);
|
|
|
|
else if (mediumMeteor[i].position.y < 0 - mediumMeteor[i].radius) mediumMeteor[i].position.y = screenHeight + mediumMeteor[i].radius;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-15 19:20:27 +03:00
|
|
|
// Meteors logic: small meteors
|
2016-02-07 13:23:12 +03:00
|
|
|
for (int i = 0; i < MAX_SMALL_METEORS; i++)
|
2016-01-29 18:26:06 +03:00
|
|
|
{
|
|
|
|
if (smallMeteor[i].active)
|
|
|
|
{
|
2017-05-15 19:20:27 +03:00
|
|
|
// Movement
|
2016-01-29 18:26:06 +03:00
|
|
|
smallMeteor[i].position.x += smallMeteor[i].speed.x;
|
|
|
|
smallMeteor[i].position.y += smallMeteor[i].speed.y;
|
|
|
|
|
2017-05-15 19:20:27 +03:00
|
|
|
// Collision logic: meteor vs wall
|
2016-01-29 18:26:06 +03:00
|
|
|
if (smallMeteor[i].position.x > screenWidth + smallMeteor[i].radius) smallMeteor[i].position.x = -(smallMeteor[i].radius);
|
|
|
|
else if (smallMeteor[i].position.x < 0 - smallMeteor[i].radius) smallMeteor[i].position.x = screenWidth + smallMeteor[i].radius;
|
|
|
|
if (smallMeteor[i].position.y > screenHeight + smallMeteor[i].radius) smallMeteor[i].position.y = -(smallMeteor[i].radius);
|
|
|
|
else if (smallMeteor[i].position.y < 0 - smallMeteor[i].radius) smallMeteor[i].position.y = screenHeight + smallMeteor[i].radius;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-15 19:20:27 +03:00
|
|
|
// Collision logic: player-shoots vs meteors
|
2016-02-07 13:23:12 +03:00
|
|
|
for (int i = 0; i < PLAYER_MAX_SHOOTS; i++)
|
2016-01-29 18:26:06 +03:00
|
|
|
{
|
|
|
|
if ((shoot[i].active))
|
|
|
|
{
|
2016-02-07 13:23:12 +03:00
|
|
|
for (int a = 0; a < MAX_BIG_METEORS; a++)
|
2016-01-29 18:26:06 +03:00
|
|
|
{
|
|
|
|
if (bigMeteor[a].active && CheckCollisionCircles(shoot[i].position, shoot[i].radius, bigMeteor[a].position, bigMeteor[a].radius))
|
|
|
|
{
|
|
|
|
shoot[i].active = false;
|
|
|
|
shoot[i].lifeSpawn = 0;
|
|
|
|
bigMeteor[a].active = false;
|
2017-05-15 19:20:27 +03:00
|
|
|
destroyedMeteorsCount++;
|
|
|
|
|
2016-01-29 18:26:06 +03:00
|
|
|
for (int j = 0; j < 2; j ++)
|
|
|
|
{
|
2017-05-15 19:20:27 +03:00
|
|
|
if (midMeteorsCount%2 == 0)
|
2016-01-29 18:26:06 +03:00
|
|
|
{
|
2017-05-15 19:20:27 +03:00
|
|
|
mediumMeteor[midMeteorsCount].position = (Vector2){bigMeteor[a].position.x, bigMeteor[a].position.y};
|
|
|
|
mediumMeteor[midMeteorsCount].speed = (Vector2){cos(shoot[i].rotation*DEG2RAD)*METEORS_SPEED*-1, sin(shoot[i].rotation*DEG2RAD)*METEORS_SPEED*-1};
|
2016-01-29 18:26:06 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-05-15 19:20:27 +03:00
|
|
|
mediumMeteor[midMeteorsCount].position = (Vector2){bigMeteor[a].position.x, bigMeteor[a].position.y};
|
|
|
|
mediumMeteor[midMeteorsCount].speed = (Vector2){cos(shoot[i].rotation*DEG2RAD)*METEORS_SPEED, sin(shoot[i].rotation*DEG2RAD)*METEORS_SPEED};
|
2016-01-29 18:26:06 +03:00
|
|
|
}
|
|
|
|
|
2017-05-15 19:20:27 +03:00
|
|
|
mediumMeteor[midMeteorsCount].active = true;
|
|
|
|
midMeteorsCount ++;
|
2016-01-29 18:26:06 +03:00
|
|
|
}
|
|
|
|
//bigMeteor[a].position = (Vector2){-100, -100};
|
|
|
|
bigMeteor[a].color = RED;
|
2016-02-07 13:23:12 +03:00
|
|
|
a = MAX_BIG_METEORS;
|
2016-01-29 18:26:06 +03:00
|
|
|
}
|
|
|
|
}
|
2017-05-15 19:20:27 +03:00
|
|
|
|
2016-02-07 13:23:12 +03:00
|
|
|
for (int b = 0; b < MAX_MEDIUM_METEORS; b++)
|
2016-01-29 18:26:06 +03:00
|
|
|
{
|
|
|
|
if (mediumMeteor[b].active && CheckCollisionCircles(shoot[i].position, shoot[i].radius, mediumMeteor[b].position, mediumMeteor[b].radius))
|
|
|
|
{
|
|
|
|
shoot[i].active = false;
|
|
|
|
shoot[i].lifeSpawn = 0;
|
|
|
|
mediumMeteor[b].active = false;
|
2017-05-15 19:20:27 +03:00
|
|
|
destroyedMeteorsCount++;
|
|
|
|
|
2016-01-29 18:26:06 +03:00
|
|
|
for (int j = 0; j < 2; j ++)
|
|
|
|
{
|
2017-05-15 19:20:27 +03:00
|
|
|
if (smallMeteorsCount%2 == 0)
|
2016-01-29 18:26:06 +03:00
|
|
|
{
|
2017-05-15 19:20:27 +03:00
|
|
|
smallMeteor[smallMeteorsCount].position = (Vector2){mediumMeteor[b].position.x, mediumMeteor[b].position.y};
|
|
|
|
smallMeteor[smallMeteorsCount].speed = (Vector2){cos(shoot[i].rotation*DEG2RAD)*METEORS_SPEED*-1, sin(shoot[i].rotation*DEG2RAD)*METEORS_SPEED*-1};
|
2016-01-29 18:26:06 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-05-15 19:20:27 +03:00
|
|
|
smallMeteor[smallMeteorsCount].position = (Vector2){mediumMeteor[b].position.x, mediumMeteor[b].position.y};
|
|
|
|
smallMeteor[smallMeteorsCount].speed = (Vector2){cos(shoot[i].rotation*DEG2RAD)*METEORS_SPEED, sin(shoot[i].rotation*DEG2RAD)*METEORS_SPEED};
|
2016-01-29 18:26:06 +03:00
|
|
|
}
|
|
|
|
|
2017-05-15 19:20:27 +03:00
|
|
|
smallMeteor[smallMeteorsCount].active = true;
|
|
|
|
smallMeteorsCount ++;
|
2016-01-29 18:26:06 +03:00
|
|
|
}
|
|
|
|
//mediumMeteor[b].position = (Vector2){-100, -100};
|
|
|
|
mediumMeteor[b].color = GREEN;
|
2016-02-07 13:23:12 +03:00
|
|
|
b = MAX_MEDIUM_METEORS;
|
2016-01-29 18:26:06 +03:00
|
|
|
}
|
|
|
|
}
|
2017-05-15 19:20:27 +03:00
|
|
|
|
2016-02-07 13:23:12 +03:00
|
|
|
for (int c = 0; c < MAX_SMALL_METEORS; c++)
|
2016-01-29 18:26:06 +03:00
|
|
|
{
|
|
|
|
if (smallMeteor[c].active && CheckCollisionCircles(shoot[i].position, shoot[i].radius, smallMeteor[c].position, smallMeteor[c].radius))
|
|
|
|
{
|
|
|
|
shoot[i].active = false;
|
|
|
|
shoot[i].lifeSpawn = 0;
|
|
|
|
smallMeteor[c].active = false;
|
2017-05-15 19:20:27 +03:00
|
|
|
destroyedMeteorsCount++;
|
2016-01-29 18:26:06 +03:00
|
|
|
smallMeteor[c].color = YELLOW;
|
|
|
|
// smallMeteor[c].position = (Vector2){-100, -100};
|
2016-02-07 13:23:12 +03:00
|
|
|
c = MAX_SMALL_METEORS;
|
2016-01-29 18:26:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-15 19:20:27 +03:00
|
|
|
if (destroyedMeteorsCount == MAX_BIG_METEORS + MAX_MEDIUM_METEORS + MAX_SMALL_METEORS) victory = true;
|
2016-01-29 18:26:06 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (IsKeyPressed(KEY_ENTER))
|
|
|
|
{
|
|
|
|
InitGame();
|
|
|
|
gameOver = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw game (one frame)
|
|
|
|
void DrawGame(void)
|
|
|
|
{
|
|
|
|
BeginDrawing();
|
|
|
|
|
2016-02-07 13:23:12 +03:00
|
|
|
ClearBackground(RAYWHITE);
|
2016-01-29 18:26:06 +03:00
|
|
|
|
|
|
|
if (!gameOver)
|
|
|
|
{
|
|
|
|
// Draw spaceship
|
|
|
|
Vector2 v1 = { player.position.x + sinf(player.rotation*DEG2RAD)*(shipHeight), player.position.y - cosf(player.rotation*DEG2RAD)*(shipHeight) };
|
2016-02-07 13:23:12 +03:00
|
|
|
Vector2 v2 = { player.position.x - cosf(player.rotation*DEG2RAD)*(PLAYER_BASE_SIZE/2), player.position.y - sinf(player.rotation*DEG2RAD)*(PLAYER_BASE_SIZE/2) };
|
|
|
|
Vector2 v3 = { player.position.x + cosf(player.rotation*DEG2RAD)*(PLAYER_BASE_SIZE/2), player.position.y + sinf(player.rotation*DEG2RAD)*(PLAYER_BASE_SIZE/2) };
|
|
|
|
DrawTriangle(v1, v2, v3, MAROON);
|
2016-01-29 18:26:06 +03:00
|
|
|
|
|
|
|
// Draw meteors
|
2016-02-07 13:23:12 +03:00
|
|
|
for (int i = 0; i < MAX_BIG_METEORS; i++)
|
2016-01-29 18:26:06 +03:00
|
|
|
{
|
2016-02-07 13:23:12 +03:00
|
|
|
if (bigMeteor[i].active) DrawCircleV(bigMeteor[i].position, bigMeteor[i].radius, DARKGRAY);
|
|
|
|
else DrawCircleV(bigMeteor[i].position, bigMeteor[i].radius, Fade(LIGHTGRAY, 0.3f));
|
2016-01-29 18:26:06 +03:00
|
|
|
}
|
|
|
|
|
2016-02-07 13:23:12 +03:00
|
|
|
for (int i = 0; i < MAX_MEDIUM_METEORS; i++)
|
2016-01-29 18:26:06 +03:00
|
|
|
{
|
2016-02-07 13:23:12 +03:00
|
|
|
if (mediumMeteor[i].active) DrawCircleV(mediumMeteor[i].position, mediumMeteor[i].radius, GRAY);
|
|
|
|
else DrawCircleV(mediumMeteor[i].position, mediumMeteor[i].radius, Fade(LIGHTGRAY, 0.3f));
|
2016-01-29 18:26:06 +03:00
|
|
|
}
|
|
|
|
|
2016-02-07 13:23:12 +03:00
|
|
|
for (int i = 0; i < MAX_SMALL_METEORS; i++)
|
2016-01-29 18:26:06 +03:00
|
|
|
{
|
2016-02-07 13:23:12 +03:00
|
|
|
if (smallMeteor[i].active) DrawCircleV(smallMeteor[i].position, smallMeteor[i].radius, GRAY);
|
|
|
|
else DrawCircleV(smallMeteor[i].position, smallMeteor[i].radius, Fade(LIGHTGRAY, 0.3f));
|
2016-01-29 18:26:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Draw shoot
|
2016-02-07 13:23:12 +03:00
|
|
|
for (int i = 0; i < PLAYER_MAX_SHOOTS; i++)
|
2016-01-29 18:26:06 +03:00
|
|
|
{
|
2016-02-07 13:23:12 +03:00
|
|
|
if (shoot[i].active) DrawCircleV(shoot[i].position, shoot[i].radius, BLACK);
|
2016-01-29 18:26:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (victory) DrawText("VICTORY", screenWidth/2 - MeasureText("VICTORY", 20)/2, screenHeight/2, 20, LIGHTGRAY);
|
|
|
|
|
|
|
|
if (pause) DrawText("GAME PAUSED", screenWidth/2 - MeasureText("GAME PAUSED", 40)/2, screenHeight/2 - 40, 40, GRAY);
|
|
|
|
}
|
|
|
|
else DrawText("PRESS [ENTER] TO PLAY AGAIN", GetScreenWidth()/2 - MeasureText("PRESS [ENTER] TO PLAY AGAIN", 20)/2, GetScreenHeight()/2 - 50, 20, GRAY);
|
|
|
|
|
|
|
|
EndDrawing();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unload game variables
|
|
|
|
void UnloadGame(void)
|
|
|
|
{
|
|
|
|
// TODO: Unload all dynamic loaded data (textures, sounds, models...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update and Draw (one frame)
|
|
|
|
void UpdateDrawFrame(void)
|
|
|
|
{
|
|
|
|
UpdateGame();
|
|
|
|
DrawGame();
|
|
|
|
}
|