diff --git a/code/source/game.c b/code/source/game.c index 98a1225..9461bcf 100644 --- a/code/source/game.c +++ b/code/source/game.c @@ -8,6 +8,8 @@ #include <../include/maxmod.h> #include <../build/soundbank.h> #include <../build/soundbank_bin.h> +#include <../include/gba-link-connection-c/link_connection.h> + void Game_renderPlayer(Paddle *p) { @@ -79,12 +81,12 @@ void Game_removePauseText() tte_erase_rect((SCREEN_WIDTH/2)-40, (SCREEN_HEIGHT/2)-5, (SCREEN_WIDTH/2)+40, (SCREEN_HEIGHT/2)+6); } -void Game_gameLoop() +void Game_gameLoop(LinkConnection *conn) { int _frame = 0; int *frame = &_frame; Scene_showTitlescreen(frame); - Scene_showGamescreen(frame); + Scene_showGamescreen(frame, conn); Scene_showLosingscreen(frame); } diff --git a/code/source/game.h b/code/source/game.h index 86f8e72..a738fb2 100644 --- a/code/source/game.h +++ b/code/source/game.h @@ -3,6 +3,7 @@ typedef struct paddle Paddle; typedef struct ball Ball; +typedef struct LinkConnection LinkConnection; #define BG_COLOR 1 @@ -19,7 +20,7 @@ typedef struct game } Game; -void Game_gameLoop(); +void Game_gameLoop(LinkConnection *conn); void Game_renderPlayer(Paddle *p); void Game_renderBall(Ball *ball); void Game_updateScore(const Paddle *p1, const Paddle *p2); diff --git a/code/source/main.c b/code/source/main.c index 51357ec..003be69 100644 --- a/code/source/main.c +++ b/code/source/main.c @@ -2,26 +2,52 @@ #include <../include/maxmod.h> #include "../build/soundbank_bin.h" +#include <../include/gba-link-connection-c/link_connection.h> #include "game.h" -void on_vblank() { - mmVBlank(); - mmFrame(); +LinkConnection conn; + +void onVBlank() { + mmVBlank(); + mmFrame(); + lc_on_vblank(&conn); +} +void onSerial() { + lc_on_serial(&conn); } +void onTimer() { + lc_on_timer(&conn); +} + int main(void) { + LinkConnectionSettings settings = { + .baud_rate = BAUD_RATE_1, + .timeout = 3, + .remote_timeout = 5, + .buffer_len = 30, + .interval = 50, + .send_timer_id = 3, + }; + + conn = lc_init(settings); + irq_init(NULL); irq_enable(II_VBLANK); - irq_add(II_VBLANK, on_vblank); + irq_add(II_VBLANK, onVBlank); + irq_add(II_SERIAL, onSerial); + irq_add(II_TIMER3, onTimer); mmInitDefault((mm_addr)soundbank_bin, 8); REG_DISPCNT = DCNT_MODE4 | DCNT_PAGE | DCNT_BG2; REG_DISPCNT ^= DCNT_PAGE; - Game_gameLoop(); + Game_gameLoop(&conn); + + lc_destroy(&conn); return 1; } diff --git a/code/source/scene.c b/code/source/scene.c index a3cac0e..dcfde8f 100644 --- a/code/source/scene.c +++ b/code/source/scene.c @@ -18,12 +18,15 @@ #include <../build/soundbank.h> #include <../build/soundbank_bin.h> -#define TOP -1 -#define BOTTOM 1 +#include <../include/gba-link-connection-c/link_connection.h> -void _runGame(Game *self, int *frame, int *scoreP1, int *scoreP2, int randomNuber); + +#define TOP 1 +#define BOTTOM 3 + +void _runGame(Game *self, int *frame, int *scoreP1, int *scoreP2, int randomNuber, LinkConnection *conn); void _move_paddle_to(int direction, Paddle *paddle); -void _ai_decision(Game *self, int *last_enemy_move, int correct_move_chance); +void _ai_decision(Game *self, int *last_enemy_move, int correct_move_chance, LinkConnection *conn, u8 opponent_player_id); void _renderGame(Game *self); void Scene_showTitlescreen(int *frame) @@ -103,13 +106,17 @@ void Scene_showLosingscreen(int *frame) } } -void Scene_showGamescreen(int *frame) +void Scene_showGamescreen(int *frame, LinkConnection *conn) { - - int scoreP1 = 0; int scoreP2 = 0; + lc_activate(conn); + + //u16 keys = ~REG_KEYS & KEY_ANY; + //u16 message = keys + 1; + //lc_send(conn, message); + // main loop while(true) { @@ -162,11 +169,11 @@ void Scene_showGamescreen(int *frame) Game *self = &_game; - _runGame(self, frame, &scoreP1, &scoreP2, randDir); + _runGame(self, frame, &scoreP1, &scoreP2, randDir, conn); } } -void _runGame(Game *self, int *frame, int *scoreP1, int *scoreP2, int randomNuber) +void _runGame(Game *self, int *frame, int *scoreP1, int *scoreP2, int randomNuber, LinkConnection *conn) { int status = 0; int last_enemy_move = (*frame % 2) -1; @@ -177,9 +184,26 @@ void _runGame(Game *self, int *frame, int *scoreP1, int *scoreP2, int randomNube Game_updateScore(self->p1, self->p2); + u8 this_player_id = conn->state.current_player_id; + u8 opponent_player_id = 5; + + while (!lc_is_connected(conn)) {} + + if (lc_is_connected(conn)) { + lc_send(conn, this_player_id + 5); + for (int id = 0; id < conn->state.player_count; id++) { + if (lc_has_message(conn, id)) { + opponent_player_id = lc_read_message(conn, id) - 5; + } + } + } + if (key_is_down(KEY_DOWN)) { if (NO_COLLISION_BOTTOM(self->p1)) { + if (lc_is_connected(conn)) { + lc_send(conn, BOTTOM); + } _move_paddle_to(BOTTOM, self->p1); } } @@ -187,14 +211,16 @@ void _runGame(Game *self, int *frame, int *scoreP1, int *scoreP2, int randomNube if (key_is_down(KEY_UP)) { if (NO_COLLISION_TOP(self->p1)) { + if (lc_is_connected(conn)) { + lc_send(conn, TOP); + } _move_paddle_to(TOP, self->p1); } } - // Player 2 is AI controlled // Add some randomness to the AI's decision-making - _ai_decision(self, &last_enemy_move, (*frame % 10) < 1); + _ai_decision(self, &last_enemy_move, (*frame % 10) < 1, conn, opponent_player_id); if (self->isRunning) { status = Ball_moveAndCollide(self); @@ -202,6 +228,9 @@ void _runGame(Game *self, int *frame, int *scoreP1, int *scoreP2, int randomNube _renderGame(self); + self->isRunning = true; + + /* // pause the game after a player lost and wait for user input if (!self->isRunning) { @@ -216,7 +245,7 @@ void _runGame(Game *self, int *frame, int *scoreP1, int *scoreP2, int randomNube Game_removePauseText(); self->isRunning = true; - } + }*/ if (status != 0) { if (status == 1) (*scoreP2)++; @@ -235,9 +264,16 @@ void _renderGame(Game *self) { Draw_rectXYHW(0, 0, SCREEN_HEIGHT - 1, SCREEN_WIDTH, 24); // grey border around the screen } -void _ai_decision(Game *self, int *last_enemy_move, int correct_move_chance) { - if (correct_move_chance) { - if (self->p2->x + self->p2->h/2 < self->ball->x + self->ball->h/2 - 5) { +void _ai_decision(Game *self, int *last_enemy_move, int correct_move_chance, LinkConnection *conn, u8 opponent_player_id) { + u16 data = 3; + + if ((lc_is_connected(conn) && lc_has_message(conn, opponent_player_id)) || correct_move_chance) + { + if ((lc_is_connected(conn) && lc_has_message(conn, opponent_player_id))) { + data = lc_read_message(conn, opponent_player_id); + } + + if (data == BOTTOM || self->p2->x + self->p2->h/2 < self->ball->x + self->ball->h/2 - 5) { if (NO_COLLISION_BOTTOM(self->p2)) { _move_paddle_to(BOTTOM, self->p2); @@ -245,7 +281,7 @@ void _ai_decision(Game *self, int *last_enemy_move, int correct_move_chance) { } } - if (self->p2->x + self->p2->h/2 > self->ball->x + self->ball->h/2 + 5) { + if (data == TOP || self->p2->x + self->p2->h/2 > self->ball->x + self->ball->h/2 + 5) { if (NO_COLLISION_TOP(self->p2)) { _move_paddle_to(TOP, self->p2); @@ -258,5 +294,5 @@ void _ai_decision(Game *self, int *last_enemy_move, int correct_move_chance) { } void _move_paddle_to(int direction, Paddle *paddle) { - paddle->x += paddle->speed * direction; + paddle->x += paddle->speed * (direction-2); } diff --git a/code/source/scene.h b/code/source/scene.h index 3d4e780..042d55d 100644 --- a/code/source/scene.h +++ b/code/source/scene.h @@ -3,6 +3,6 @@ void Scene_showTitlescreen(int *frame); void Scene_showLosingscreen(int *frame); -void Scene_showGamescreen(int *frame); +void Scene_showGamescreen(int *frame, LinkConnection *conn); #endif //PONG_SCREEN_H