-
Notifications
You must be signed in to change notification settings - Fork 0
/
obstacles_manager.c
83 lines (75 loc) · 2.68 KB
/
obstacles_manager.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
** EPITECH PROJECT, 2021
** MyRUNNER
** File description:
** obstacles handler
*/
#include "runner.h"
void manage_obstacles (myrunner_t *myh, sfRenderWindow *window, sfEvent event)
{
myh->live_pos_car_x = sfSprite_getPosition(myh->obs_car).x;
myh->live_pos_taxi_x = sfSprite_getPosition(myh->obs_taxi).x;
myh->live_pos_trash_x = sfSprite_getPosition(myh->obs_trash).x;
sfRenderWindow_drawSprite(window, myh->obs_car, NULL);
sfRenderWindow_drawSprite(window, myh->obs_taxi, NULL);
sfRenderWindow_drawSprite(window, myh->obs_trash, NULL);
}
void checker(myrunner_t *myh) {
if (myh->live_pos_car_x <= -210)
set_obstacles_position(myh, myh->obs_car);
if (myh->live_pos_taxi_x <= -210)
set_obstacles_position(myh, myh->obs_taxi);
if (myh->live_pos_trash_x <= -210)
set_obstacles_position(myh, myh->obs_trash);
}
void reset_obstacles_position (myrunner_t *myh)
{
checker(myh);
if (myh->live_pos_taxi_x > -210 && myh->live_pos_trash_x > -210) {
if (myh->spawning_nb == 2) {
moving_obstacles(myh, myh->obs_car);
}
}
if (myh->live_pos_car_x > -210 && myh->live_pos_trash_x > -210) {
if (myh->spawning_nb == 3 ) {
moving_obstacles(myh, myh->obs_taxi);
}
}
if (myh->live_pos_car_x > -210 && myh->live_pos_taxi_x > -210) {
if (myh->spawning_nb == 4 ) {
moving_obstacles(myh, myh->obs_trash);
}
}
}
void set_obstacles_position(myrunner_t *myh, sfSprite *sprite)
{
sfVector2f reset_pos = {830, 480};
sfSprite_setPosition(sprite, reset_pos);
}
void moving_obstacles (myrunner_t *myh, sfSprite *sprite) {
int rand_speed = (rand() % (7 - 15)) + 7;
rand_speed *= -1;
sfVector2f moving = {rand_speed, 0};
sfSprite_move(sprite, moving);
}
int spawn_random_obstacles (myrunner_t *myh)
{
if (myh->live_pos_taxi_x <= -210 |
myh->live_pos_trash_x <= -210 | myh->live_pos_car_x <= -210) {
myh->spawning_nb = (rand() % (2 - 5)) + 2;
}
}
void hit_obstacles (myrunner_t *myh, sfRenderWindow *window, sfEvent event)
{
myh->character_pos_x = sfSprite_getPosition(myh->game_sprite_p).x;
myh->character_pos_y = sfSprite_getPosition(myh->game_sprite_p).y;
if (myh->live_pos_car_x <= 377 && myh->live_pos_car_x >= 199
&& myh->character_pos_y <= 480 && myh->character_pos_y >= 425)
myh->game_over = 1;
if (myh->live_pos_taxi_x <= 377 && myh->live_pos_taxi_x >= 206
&& myh->character_pos_y <= 480 && myh->character_pos_y >= 425)
myh->game_over = 1;
if (myh->live_pos_trash_x <= 380 && myh->live_pos_trash_x >= 220
&& myh->character_pos_y <= 480 && myh->character_pos_y >= 425)
myh->game_over = 1;
}