Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added basic link connection #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions code/source/game.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
}
3 changes: 2 additions & 1 deletion code/source/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

typedef struct paddle Paddle;
typedef struct ball Ball;
typedef struct LinkConnection LinkConnection;

#define BG_COLOR 1

Expand All @@ -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);
Expand Down
36 changes: 31 additions & 5 deletions code/source/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
70 changes: 53 additions & 17 deletions code/source/scene.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
Expand All @@ -177,31 +184,53 @@ 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);
}
}

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);
}

_renderGame(self);

self->isRunning = true;

/*
// pause the game after a player lost and wait for user input
if (!self->isRunning)
{
Expand All @@ -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)++;
Expand All @@ -235,17 +264,24 @@ 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);
*last_enemy_move = BOTTOM;
}
}

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);
Expand All @@ -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);
}
2 changes: 1 addition & 1 deletion code/source/scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading