Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
mini projet
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
LE TENOUX--RACHIDI Isham
mini projet
Commits
7d592e90
Commit
7d592e90
authored
Oct 30, 2020
by
LE TENOUX--RACHIDI Isham
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Jeu modifié
parent
739698e0
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
734 additions
and
0 deletions
+734
-0
asteroidWithDangerousAlien.c
asteroidWithDangerousAlien.c
+734
-0
No files found.
asteroidWithDangerousAlien.c
0 → 100644
View file @
7d592e90
/*******************************************************************************************
*
* 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>
#include <stdbool.h>
#if defined(PLATFORM_WEB)
#include <emscripten/emscripten.h>
#endif
//----------------------------------------------------------------------------------
// Some Defines
//----------------------------------------------------------------------------------
#define PLAYER_BASE_SIZE 20.0f
#define PLAYER_SPEED 6.0f
#define PLAYER_MAX_SHOOTS 10
#define METEORS_SPEED 2
#define MAX_BIG_METEORS 4
#define MAX_MEDIUM_METEORS 6
#define MAX_SMALL_METEORS 16
#define MAX_SUIVEURS 40
#define MIN_SPEED_SUIVEURS 3.0f
#define MAX_SPEED_SUIVEURS 10.0f
//----------------------------------------------------------------------------------
// Types and Structures Definition
//----------------------------------------------------------------------------------
typedef
struct
Player
{
Vector2
position
;
Vector2
speed
;
float
acceleration
;
float
rotation
;
Vector3
collider
;
Color
color
;
float
max_speed
;
}
Player
;
typedef
struct
Suiveur
{
Vector2
position
;
Vector2
speed
;
float
acceleration
;
float
rotation
;
Vector3
collider
;
Color
color
;
float
max_speed
;
float
radius
;
}
Suiveur
;
typedef
struct
Shoot
{
Vector2
position
;
Vector2
speed
;
float
radius
;
float
rotation
;
int
lifeSpawn
;
bool
active
;
Color
color
;
}
Shoot
;
typedef
struct
Meteor
{
Vector2
position
;
Vector2
speed
;
float
radius
;
bool
active
;
Color
color
;
}
Meteor
;
//------------------------------------------------------------------------------------
// Global Variables Declaration
//------------------------------------------------------------------------------------
static
const
int
screenWidth
=
1280
;
static
const
int
screenHeight
=
720
;
static
bool
gameOver
=
false
;
static
bool
pause
=
false
;
static
bool
victory
=
false
;
// NOTE: Defined triangle is isosceles with common angles of 70 degrees.
static
float
shipHeight
=
0
.
0
f
;
static
Player
player
=
{
0
};
static
Suiveur
suiveurs
[
MAX_SUIVEURS
]
=
{
0
};
static
Shoot
shoot
[
PLAYER_MAX_SHOOTS
]
=
{
0
};
static
Meteor
bigMeteor
[
MAX_BIG_METEORS
]
=
{
0
};
static
Meteor
mediumMeteor
[
MAX_MEDIUM_METEORS
]
=
{
0
};
static
Meteor
smallMeteor
[
MAX_SMALL_METEORS
]
=
{
0
};
static
int
midMeteorsCount
=
0
;
static
int
smallMeteorsCount
=
0
;
static
int
destroyedMeteorsCount
=
0
;
//------------------------------------------------------------------------------------
// 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
//------------------------------------------------------------------------------------
int
main
(
void
)
{
// Initialization (Note windowTitle is unused on Android)
//---------------------------------------------------------
InitWindow
(
screenWidth
,
screenHeight
,
"sample game: FlockThis"
);
InitGame
();
#if defined(PLATFORM_WEB)
emscripten_set_main_loop
(
UpdateDrawFrame
,
60
,
1
);
#else
SetTargetFPS
(
60
);
//--------------------------------------------------------------------------------------
// Main game loop
while
(
!
WindowShouldClose
())
// Detect window close button or ESC key
{
// Update and Draw
//----------------------------------------------------------------------------------
UpdateDrawFrame
();
//----------------------------------------------------------------------------------
}
#endif
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadGame
();
// Unload loaded data (textures, sounds, models...)
CloseWindow
();
// Close window and OpenGL context
//--------------------------------------------------------------------------------------
return
0
;
}
//------------------------------------------------------------------------------------
// Module Functions Definitions (local)
//------------------------------------------------------------------------------------
// Initialize game variables
void
InitGame
(
void
)
{
int
posx
,
posy
;
int
velx
,
vely
;
bool
correctRange
=
false
;
victory
=
false
;
pause
=
false
;
shipHeight
=
(
PLAYER_BASE_SIZE
/
2
)
/
tanf
(
20
*
DEG2RAD
);
// 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
.
5
f
),
player
.
position
.
y
-
cos
(
player
.
rotation
*
DEG2RAD
)
*
(
shipHeight
/
2
.
5
f
),
12
};
player
.
color
=
BLUE
;
player
.
max_speed
=
PLAYER_SPEED
;
//Initialisation suiveurs
for
(
int
i
=
0
;
i
<
MAX_SUIVEURS
;
i
++
)
{
suiveurs
[
i
].
position
=
(
Vector2
){
GetRandomValue
(
-
15
,
15
),
GetRandomValue
(
-
15
,
15
)};
suiveurs
[
i
].
speed
=
(
Vector2
){
GetRandomValue
(
-
0
.
001
,
0
.
001
),
GetRandomValue
(
-
0
.
001
,
0
.
001
)};
suiveurs
[
i
].
acceleration
=
1
;
suiveurs
[
i
].
rotation
=
0
;
suiveurs
[
i
].
collider
=
(
Vector3
){
player
.
position
.
x
+
sin
(
player
.
rotation
*
DEG2RAD
)
*
(
shipHeight
/
2
.
5
f
),
player
.
position
.
y
-
cos
(
player
.
rotation
*
DEG2RAD
)
*
(
shipHeight
/
2
.
5
f
),
12
};
suiveurs
[
i
].
color
=
RED
;
suiveurs
[
i
].
max_speed
=
GetRandomValue
(
MIN_SPEED_SUIVEURS
,
MAX_SPEED_SUIVEURS
);
suiveurs
[
i
].
radius
=
1
.
0
*
GetRandomValue
(
6
,
20
);
}
destroyedMeteorsCount
=
0
;
// Initialization shoot
for
(
int
i
=
0
;
i
<
PLAYER_MAX_SHOOTS
;
i
++
)
{
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
;
}
for
(
int
i
=
0
;
i
<
MAX_BIG_METEORS
;
i
++
)
{
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
;
}
for
(
int
i
=
0
;
i
<
MAX_MEDIUM_METEORS
;
i
++
)
{
mediumMeteor
[
i
].
position
=
(
Vector2
){
-
100
,
-
100
};
mediumMeteor
[
i
].
speed
=
(
Vector2
){
0
,
0
};
mediumMeteor
[
i
].
radius
=
20
;
mediumMeteor
[
i
].
active
=
false
;
mediumMeteor
[
i
].
color
=
BLUE
;
}
for
(
int
i
=
0
;
i
<
MAX_SMALL_METEORS
;
i
++
)
{
smallMeteor
[
i
].
position
=
(
Vector2
){
-
100
,
-
100
};
smallMeteor
[
i
].
speed
=
(
Vector2
){
0
,
0
};
smallMeteor
[
i
].
radius
=
10
;
smallMeteor
[
i
].
active
=
false
;
smallMeteor
[
i
].
color
=
BLUE
;
}
midMeteorsCount
=
0
;
smallMeteorsCount
=
0
;
}
bool
notEqual
(
Suiveur
suiveur1
,
Suiveur
suiveur2
)
{
return
suiveur1
.
position
.
x
!=
suiveur2
.
position
.
x
&&
suiveur1
.
position
.
y
!=
suiveur2
.
position
.
y
;
}
float
norm
(
Vector2
vecteur
)
{
return
sqrt
(
vecteur
.
x
*
vecteur
.
x
+
vecteur
.
y
*
vecteur
.
y
);
}
float
distance
(
Vector2
a
,
Vector2
b
)
{
Vector2
d
=
{
a
.
x
-
b
.
x
,
a
.
y
-
b
.
y
};
return
norm
(
d
);
}
void
limitVitesseSuiveurs
(
Suiveur
*
suiveurs
)
{
//int vitesse = norm(suiveur.speed);
//Il faut que le suiveur ralentisse lorsqu'il se rapproche du leader
//pour éviter le phénomène d'oscillation
//calcul de la distance entre le suiveur et le leader
//float d = distance(player.position, suiveur.position);
for
(
int
i
=
0
;
i
<
MAX_SUIVEURS
;
i
++
)
{
int
vitesse
=
norm
(
suiveurs
[
i
].
speed
);
if
(
vitesse
>
MAX_SPEED_SUIVEURS
)
{
suiveurs
[
i
].
speed
.
x
*=
suiveurs
[
i
].
max_speed
/
vitesse
;
suiveurs
[
i
].
speed
.
y
*=
suiveurs
[
i
].
max_speed
/
vitesse
;
}
}
}
Vector2
regle1
(
Suiveur
suiveur
)
{
Vector2
posMoyenne
=
{
0
,
0
};
Vector2
cohesionForce
=
{
0
,
0
};
for
(
int
i
=
0
;
i
<
MAX_SUIVEURS
;
i
++
)
{
if
(
notEqual
(
suiveurs
[
i
],
suiveur
))
{
//Si deux suiveurs sont trop proches
posMoyenne
.
x
+=
suiveurs
[
i
].
position
.
x
;
posMoyenne
.
y
+=
suiveurs
[
i
].
position
.
y
;
}
}
//suivi de la position du leader
posMoyenne
.
x
+=
player
.
position
.
x
;
posMoyenne
.
y
+=
player
.
position
.
y
;
//calcul position moyenne
posMoyenne
.
x
/=
MAX_SUIVEURS
;
posMoyenne
.
y
/=
MAX_SUIVEURS
;
cohesionForce
.
x
=
posMoyenne
.
x
-
suiveur
.
position
.
x
;
cohesionForce
.
y
=
posMoyenne
.
y
-
suiveur
.
position
.
y
;
cohesionForce
.
x
/=
norm
(
cohesionForce
);
cohesionForce
.
y
/=
norm
(
cohesionForce
);
return
cohesionForce
;
}
Vector2
regle2
(
Suiveur
suiveur
)
{
Vector2
repulsionForce
=
{
0
,
0
};
int
radius
=
15
;
for
(
int
i
=
0
;
i
<
MAX_SUIVEURS
;
i
++
)
{
if
(
notEqual
(
suiveurs
[
i
],
suiveur
))
{
//Si deux suiveurs sont trop proches
if
(
distance
(
suiveurs
[
i
].
position
,
suiveur
.
position
)
<
radius
)
{
repulsionForce
.
x
+=
suiveur
.
position
.
x
-
suiveurs
[
i
].
position
.
x
;
repulsionForce
.
y
+=
suiveur
.
position
.
y
-
suiveurs
[
i
].
position
.
y
;
}
}
}
//Si le suiveur est trop près du leader
if
(
distance
(
player
.
position
,
suiveur
.
position
)
<
radius
)
{
repulsionForce
.
x
=
suiveur
.
position
.
x
-
player
.
position
.
x
;
repulsionForce
.
y
=
suiveur
.
position
.
y
-
player
.
position
.
y
;
}
if
(
norm
(
repulsionForce
)
!=
0
)
{
repulsionForce
.
x
*=
2
/
norm
(
repulsionForce
);
repulsionForce
.
y
*=
2
/
norm
(
repulsionForce
);
}
return
repulsionForce
;
}
float
orientationMoyenne
(
Suiveur
suiveur
)
{
//uniformisation de l'orientation des suiveurs
float
rotationMoyenne
=
0
;
for
(
int
i
=
0
;
i
<
MAX_SUIVEURS
;
i
++
)
{
if
(
notEqual
(
suiveurs
[
i
],
suiveur
))
{
rotationMoyenne
+=
suiveurs
[
i
].
rotation
;
}
}
rotationMoyenne
+=
player
.
rotation
;
rotationMoyenne
/=
MAX_SUIVEURS
;
return
rotationMoyenne
;
}
float
orientationAvecDirection
(
Suiveur
suiveur
)
{
float
angle
=
0
;
angle
=
atan
(
suiveur
.
speed
.
y
/
suiveur
.
speed
.
x
);
return
angle
*
(
180
.
0
/
PI
);
}
// Update game (one frame)
void
UpdateGame
(
void
)
{
if
(
!
gameOver
)
{
if
(
IsKeyPressed
(
'P'
))
pause
=
!
pause
;
if
(
!
pause
)
{
// Player logic: rotation
if
(
IsKeyDown
(
KEY_LEFT
))
player
.
rotation
-=
5
;
if
(
IsKeyDown
(
KEY_RIGHT
))
player
.
rotation
+=
5
;
// Player logic: speed
player
.
speed
.
x
=
sin
(
player
.
rotation
*
DEG2RAD
)
*
PLAYER_SPEED
;
player
.
speed
.
y
=
cos
(
player
.
rotation
*
DEG2RAD
)
*
PLAYER_SPEED
;
// Player logic: acceleration
if
(
IsKeyDown
(
KEY_UP
))
{
if
(
player
.
acceleration
<
1
)
player
.
acceleration
+=
0
.
04
f
;
}
else
{
if
(
player
.
acceleration
>
0
)
player
.
acceleration
-=
0
.
02
f
;
else
if
(
player
.
acceleration
<
0
)
player
.
acceleration
=
0
;
}
if
(
IsKeyDown
(
KEY_DOWN
))
{
if
(
player
.
acceleration
>
0
)
player
.
acceleration
-=
0
.
04
f
;
else
if
(
player
.
acceleration
<
0
)
player
.
acceleration
=
0
;
}
// Player logic: movement
player
.
position
.
x
+=
(
player
.
speed
.
x
*
player
.
acceleration
);
player
.
position
.
y
-=
(
player
.
speed
.
y
*
player
.
acceleration
);
//flocking suiveurs
//cohesion
for
(
int
i
=
0
;
i
<
MAX_SUIVEURS
;
i
++
)
{
Vector2
v1
=
regle1
(
suiveurs
[
i
]);
Vector2
v2
=
regle2
(
suiveurs
[
i
]);
suiveurs
[
i
].
speed
.
x
+=
v1
.
x
+
v2
.
x
;
suiveurs
[
i
].
speed
.
y
+=
v1
.
y
+
v2
.
y
;
suiveurs
[
i
].
acceleration
=
0
.
3
f
;
}
limitVitesseSuiveurs
(
suiveurs
);
// Mouvement suiveurs
for
(
int
i
=
0
;
i
<
MAX_SUIVEURS
;
i
++
)
{
suiveurs
[
i
].
position
.
x
+=
(
suiveurs
[
i
].
speed
.
x
*
suiveurs
[
i
].
acceleration
);
suiveurs
[
i
].
position
.
y
+=
(
suiveurs
[
i
].
speed
.
y
*
suiveurs
[
i
].
acceleration
);
}
// Collision logic: player vs walls
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
;
// Player shoot logic
if
(
IsKeyPressed
(
KEY_SPACE
))
{
for
(
int
i
=
0
;
i
<
PLAYER_MAX_SHOOTS
;
i
++
)
{
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
;
shoot
[
i
].
speed
.
x
=
1
.
5
*
sin
(
player
.
rotation
*
DEG2RAD
)
*
PLAYER_SPEED
;
shoot
[
i
].
speed
.
y
=
1
.
5
*
cos
(
player
.
rotation
*
DEG2RAD
)
*
PLAYER_SPEED
;
shoot
[
i
].
rotation
=
player
.
rotation
;
break
;
}
}
}
// Shoot life timer
for
(
int
i
=
0
;
i
<
PLAYER_MAX_SHOOTS
;
i
++
)
{
if
(
shoot
[
i
].
active
)
shoot
[
i
].
lifeSpawn
++
;
}
// Shot logic
for
(
int
i
=
0
;
i
<
PLAYER_MAX_SHOOTS
;
i
++
)
{
if
(
shoot
[
i
].
active
)
{
// Movement
shoot
[
i
].
position
.
x
+=
shoot
[
i
].
speed
.
x
;
shoot
[
i
].
position
.
y
-=
shoot
[
i
].
speed
.
y
;
// Collision logic: shoot vs walls
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
;
}
}
}
// Collision logic: player vs meteors
player
.
collider
=
(
Vector3
){
player
.
position
.
x
+
sin
(
player
.
rotation
*
DEG2RAD
)
*
(
shipHeight
/
2
.
5
f
),
player
.
position
.
y
-
cos
(
player
.
rotation
*
DEG2RAD
)
*
(
shipHeight
/
2
.
5
f
),
12
};
for
(
int
a
=
0
;
a
<
MAX_BIG_METEORS
;
a
++
)
{
if
(
CheckCollisionCircles
((
Vector2
){
player
.
collider
.
x
,
player
.
collider
.
y
},
player
.
collider
.
z
,
bigMeteor
[
a
].
position
,
bigMeteor
[
a
].
radius
)
&&
bigMeteor
[
a
].
active
)
gameOver
=
true
;
}
for
(
int
a
=
0
;
a
<
MAX_MEDIUM_METEORS
;
a
++
)
{
if
(
CheckCollisionCircles
((
Vector2
){
player
.
collider
.
x
,
player
.
collider
.
y
},
player
.
collider
.
z
,
mediumMeteor
[
a
].
position
,
mediumMeteor
[
a
].
radius
)
&&
mediumMeteor
[
a
].
active
)
gameOver
=
true
;
}
for
(
int
a
=
0
;
a
<
MAX_SMALL_METEORS
;
a
++
)
{
if
(
CheckCollisionCircles
((
Vector2
){
player
.
collider
.
x
,
player
.
collider
.
y
},
player
.
collider
.
z
,
smallMeteor
[
a
].
position
,
smallMeteor
[
a
].
radius
)
&&
smallMeteor
[
a
].
active
)
gameOver
=
true
;
}
//collision suiveurs-player
for
(
int
a
=
0
;
a
<
MAX_SUIVEURS
;
a
++
)
{
if
(
CheckCollisionCircles
((
Vector2
){
player
.
collider
.
x
,
player
.
collider
.
y
},
player
.
collider
.
z
,
suiveurs
[
a
].
position
,
suiveurs
[
a
].
radius
))
gameOver
=
true
;
}
// Meteors logic: big meteors
for
(
int
i
=
0
;
i
<
MAX_BIG_METEORS
;
i
++
)
{
if
(
bigMeteor
[
i
].
active
)
{
// Movement
bigMeteor
[
i
].
position
.
x
+=
bigMeteor
[
i
].
speed
.
x
;
bigMeteor
[
i
].
position
.
y
+=
bigMeteor
[
i
].
speed
.
y
;
// Collision logic: meteor vs wall
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
;
}
}
// Meteors logic: medium meteors
for
(
int
i
=
0
;
i
<
MAX_MEDIUM_METEORS
;
i
++
)
{
if
(
mediumMeteor
[
i
].
active
)
{
// Movement
mediumMeteor
[
i
].
position
.
x
+=
mediumMeteor
[
i
].
speed
.
x
;
mediumMeteor
[
i
].
position
.
y
+=
mediumMeteor
[
i
].
speed
.
y
;
// Collision logic: meteor vs wall
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
;
}
}
// Meteors logic: small meteors
for
(
int
i
=
0
;
i
<
MAX_SMALL_METEORS
;
i
++
)
{
if
(
smallMeteor
[
i
].
active
)
{
// Movement
smallMeteor
[
i
].
position
.
x
+=
smallMeteor
[
i
].
speed
.
x
;
smallMeteor
[
i
].
position
.
y
+=
smallMeteor
[
i
].
speed
.
y
;
// Collision logic: meteor vs wall
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
;
}
}
// Collision logic: player-shoots vs meteors
for
(
int
i
=
0
;
i
<
PLAYER_MAX_SHOOTS
;
i
++
)
{
if
((
shoot
[
i
].
active
))
{
for
(
int
a
=
0
;
a
<
MAX_BIG_METEORS
;
a
++
)
{
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
;
destroyedMeteorsCount
++
;
for
(
int
j
=
0
;
j
<
2
;
j
++
)
{
if
(
midMeteorsCount
%
2
==
0
)
{
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
};
}
else
{
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
};
}
mediumMeteor
[
midMeteorsCount
].
active
=
true
;
midMeteorsCount
++
;
}
//bigMeteor[a].position = (Vector2){-100, -100};
bigMeteor
[
a
].
color
=
RED
;
a
=
MAX_BIG_METEORS
;
}
}
for
(
int
b
=
0
;
b
<
MAX_MEDIUM_METEORS
;
b
++
)
{
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
;
destroyedMeteorsCount
++
;
for
(
int
j
=
0
;
j
<
2
;
j
++
)
{
if
(
smallMeteorsCount
%
2
==
0
)
{
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
};
}
else
{
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
};
}
smallMeteor
[
smallMeteorsCount
].
active
=
true
;
smallMeteorsCount
++
;
}
//mediumMeteor[b].position = (Vector2){-100, -100};
mediumMeteor
[
b
].
color
=
GREEN
;
b
=
MAX_MEDIUM_METEORS
;
}
}
for
(
int
c
=
0
;
c
<
MAX_SMALL_METEORS
;
c
++
)
{
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
;
destroyedMeteorsCount
++
;
smallMeteor
[
c
].
color
=
YELLOW
;
// smallMeteor[c].position = (Vector2){-100, -100};
c
=
MAX_SMALL_METEORS
;
}
}
}
}
}
if
(
destroyedMeteorsCount
==
MAX_BIG_METEORS
+
MAX_MEDIUM_METEORS
+
MAX_SMALL_METEORS
)
victory
=
true
;
}
else
{
if
(
IsKeyPressed
(
KEY_ENTER
))
{
InitGame
();
gameOver
=
false
;
}
}
}
// Draw game (one frame)
void
DrawGame
(
void
)
{
BeginDrawing
();
ClearBackground
(
RAYWHITE
);
if
(
!
gameOver
)
{
// Draw spaceship
Vector2
v1
=
{
player
.
position
.
x
+
sinf
(
player
.
rotation
*
DEG2RAD
)
*
(
shipHeight
),
player
.
position
.
y
-
cosf
(
player
.
rotation
*
DEG2RAD
)
*
(
shipHeight
)
};
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
,
player
.
color
);
// Draw suiveurs
for
(
int
i
=
0
;
i
<
MAX_SUIVEURS
;
i
++
)
{
/*Vector2 v1 = { suiveurs[i].position.x + sinf(suiveurs[i].rotation*DEG2RAD)*(shipHeight), suiveurs[i].position.y - cosf(suiveurs[i].rotation*DEG2RAD)*(shipHeight) };
Vector2 v2 = { suiveurs[i].position.x - cosf(suiveurs[i].rotation*DEG2RAD)*(PLAYER_BASE_SIZE/2), suiveurs[i].position.y - sinf(suiveurs[i].rotation*DEG2RAD)*(PLAYER_BASE_SIZE/2) };
Vector2 v3 = { suiveurs[i].position.x + cosf(suiveurs[i].rotation*DEG2RAD)*(PLAYER_BASE_SIZE/2), suiveurs[i].position.y + sinf(suiveurs[i].rotation*DEG2RAD)*(PLAYER_BASE_SIZE/2) };
DrawTriangle(v1, v2, v3, suiveurs[i].color);*/
DrawCircleV
(
suiveurs
[
i
].
position
,
suiveurs
[
i
].
radius
,
suiveurs
[
i
].
color
);
}
// Draw meteors
for
(
int
i
=
0
;
i
<
MAX_BIG_METEORS
;
i
++
)
{
if
(
bigMeteor
[
i
].
active
)
DrawCircleV
(
bigMeteor
[
i
].
position
,
bigMeteor
[
i
].
radius
,
DARKGRAY
);
else
DrawCircleV
(
bigMeteor
[
i
].
position
,
bigMeteor
[
i
].
radius
,
Fade
(
LIGHTGRAY
,
0
.
3
f
));
}
for
(
int
i
=
0
;
i
<
MAX_MEDIUM_METEORS
;
i
++
)
{
if
(
mediumMeteor
[
i
].
active
)
DrawCircleV
(
mediumMeteor
[
i
].
position
,
mediumMeteor
[
i
].
radius
,
GRAY
);
else
DrawCircleV
(
mediumMeteor
[
i
].
position
,
mediumMeteor
[
i
].
radius
,
Fade
(
LIGHTGRAY
,
0
.
3
f
));
}
for
(
int
i
=
0
;
i
<
MAX_SMALL_METEORS
;
i
++
)
{
if
(
smallMeteor
[
i
].
active
)
DrawCircleV
(
smallMeteor
[
i
].
position
,
smallMeteor
[
i
].
radius
,
GRAY
);
else
DrawCircleV
(
smallMeteor
[
i
].
position
,
smallMeteor
[
i
].
radius
,
Fade
(
LIGHTGRAY
,
0
.
3
f
));
}
// Draw shoot
for
(
int
i
=
0
;
i
<
PLAYER_MAX_SHOOTS
;
i
++
)
{
if
(
shoot
[
i
].
active
)
DrawCircleV
(
shoot
[
i
].
position
,
shoot
[
i
].
radius
,
BLACK
);
}
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
();
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment