Skip to content

Commit f14feab

Browse files
committed
Players work now so thats cool
1 parent ecc29b4 commit f14feab

File tree

5 files changed

+68
-14
lines changed

5 files changed

+68
-14
lines changed

src/collide.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,12 @@ void collideballs(ball_data* ball1, ball_data* ball2) {
2929

3030
}
3131

32-
int num_stripes = 7;
33-
int num_solids = 7;
3432

35-
void check_pockets(ball_data* ball) {
3633

37-
static const int pocket_x[] = {15, LCD_WIDTH / 2, LCD_WIDTH - 15, 15, LCD_WIDTH / 2, LCD_WIDTH - 15};
38-
static const int pocket_y[] = {15, 10, 15, TABLE_HEIGHT - 15, TABLE_HEIGHT - 10, TABLE_HEIGHT - 15};
34+
void check_pockets(ball_data* ball, bool* next_turn, int* num_solids, int* num_stripes, bool is_player_1_turn, gfx_sprite_t** player_1_type) {
35+
36+
static const int pocket_x[] = {14, LCD_WIDTH / 2, LCD_WIDTH - 14, 14, LCD_WIDTH / 2, LCD_WIDTH - 14};
37+
static const int pocket_y[] = {14, 10, 14, TABLE_HEIGHT - 14, TABLE_HEIGHT - 10, TABLE_HEIGHT - 14};
3938
static int next_pocketed_x = 8;
4039

4140
for (int i = 0; i < 6; i++) {
@@ -58,7 +57,13 @@ void check_pockets(ball_data* ball) {
5857

5958
} else {
6059

61-
(ball->sprite == stripe) ? num_stripes-- : num_solids-- ;
60+
if (*player_1_type == NULL)
61+
*player_1_type = (is_player_1_turn) ? ball->sprite : (ball->sprite == solid) ? stripe : solid;
62+
63+
if ((*player_1_type == ball->sprite && is_player_1_turn) || (*player_1_type != ball->sprite && !is_player_1_turn))
64+
*next_turn = true;
65+
66+
(ball->sprite == stripe) ? *num_stripes-- : *num_solids-- ;
6267

6368
ball->x = next_pocketed_x;
6469
ball->y = TABLE_HEIGHT + 8;

src/collide.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ typedef struct cue_data {
3333
void collideballs(ball_data* ball1, ball_data* ball2);
3434
void collidewalls(ball_data* ball);
3535

36-
void check_pockets(ball_data* ball);
36+
void check_pockets(ball_data* ball, bool* next_turn, int* num_solids, int* num_stripes, bool is_player_1_turn, gfx_sprite_t** player_1_type);
3737

3838
float time_of_collision(ball_data *ball1, ball_data *ball2);
3939

src/draw.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,22 @@ float closest_pos(float x, float y, float o_x, float o_y, float min_distance, fl
9393

9494
return min_distance;
9595

96+
}
97+
98+
void draw_players(gfx_sprite_t* player_1_type, bool is_player_1_turn) {
99+
gfx_SetColor(3);
100+
gfx_SetTextFGColor(3);
101+
gfx_PrintStringXY("Player 1", 8, TABLE_HEIGHT + 20);
102+
gfx_PrintStringXY("Player 2", LCD_WIDTH - 60, TABLE_HEIGHT + 20);
103+
104+
if (player_1_type != NULL) {
105+
gfx_TransparentSprite_NoClip(player_1_type, 66, TABLE_HEIGHT + 19);
106+
gfx_TransparentSprite_NoClip((player_1_type == solid) ? stripe : solid, LCD_WIDTH - 72, TABLE_HEIGHT + 19);
107+
}
108+
109+
if (is_player_1_turn)
110+
gfx_Line_NoClip(8, TABLE_HEIGHT + 30, 60, TABLE_HEIGHT + 30);
111+
else
112+
gfx_Line_NoClip(LCD_WIDTH - 60, TABLE_HEIGHT + 30, LCD_WIDTH - 8, TABLE_HEIGHT + 30);
113+
96114
}

src/draw.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ float closest_pos(float x, float y, float o_x, float o_y, float min_distance, fl
1616

1717
void draw_setup(ball_data balls[16], cue_data* queue);
1818

19+
void draw_players(gfx_sprite_t* player_1_type, bool is_player_1_turn);
20+
1921
#if __cplusplus
2022
}
2123
#endif

src/main.c

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ ball_data balls[16];
2626
enum gamestates{start, setup, animate, run};
2727
int gamestate = setup;
2828

29+
bool start_of_setup = false;
30+
31+
// player turns
32+
bool is_player_1_turn = true;
33+
bool extra_turn = false;
34+
gfx_sprite_t* player_1_type = NULL;
35+
36+
int num_stripes = 7;
37+
int num_solids = 7;
38+
2939
// frame for animation of cue
3040
int frame = 0;
3141

@@ -89,13 +99,26 @@ bool step(void) {
8999

90100
if (gamestate == setup) {
91101

92-
// scratch
93-
if (balls[15].pocketed) {
94-
balls[15].x = 67;
95-
balls[15].y = 81;
96-
balls[15].pocketed = false;
102+
if (start_of_setup) {
103+
104+
if (balls[15].pocketed) { // scratch
105+
balls[15].x = 67;
106+
balls[15].y = 81;
107+
balls[15].pocketed = false;
108+
extra_turn = false;
109+
}
110+
111+
// if player sunk own ball, dont switch
112+
if (!extra_turn)
113+
is_player_1_turn = !is_player_1_turn;
114+
else
115+
extra_turn = false;
116+
117+
start_of_setup = false;
97118
}
98119

120+
121+
99122
// speed multiplier for power and angle
100123
if (kb_Data[1] & kb_2nd) {
101124
speedmult = 4;
@@ -160,7 +183,7 @@ bool step(void) {
160183
balls[i].y -= balls[i].vy;
161184
}
162185

163-
check_pockets(&balls[i]);
186+
check_pockets(&balls[i], &extra_turn, &num_solids, &num_stripes, is_player_1_turn, &player_1_type);
164187

165188
collidewalls(&balls[i]); // this jawn worked well enough without multiple per frame
166189

@@ -179,8 +202,10 @@ bool step(void) {
179202
}
180203

181204
// if the counter it equal to the number of balls, end the run state
182-
if (num_stopped == 16)
205+
if (num_stopped == 16) {
183206
gamestate = setup;
207+
start_of_setup = true;
208+
}
184209

185210
}
186211

@@ -206,6 +231,10 @@ void draw(void) {
206231
// for (int i = 0; i < 6; i++) {
207232
// gfx_Circle(pocket_x[i], pocket_y[i], 8);
208233
// }
234+
235+
236+
// draw player stuff
237+
draw_players(player_1_type, is_player_1_turn);
209238

210239
// draw balls
211240
for (int i = 0; i < 16; i++) {

0 commit comments

Comments
 (0)