Skip to content

Commit

Permalink
Split code in modules
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasPietrzak committed May 1, 2017
1 parent 6deea2f commit fbb97a1
Show file tree
Hide file tree
Showing 9 changed files with 563 additions and 302 deletions.
92 changes: 92 additions & 0 deletions ball.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#include "ball.h"

#include <Gamebuino.h>
extern Gamebuino gb;

#include "line.h"

uint8_t Ball::ballsize = 2;
uint8_t Ball::maxballs = 15;

Ball::Ball(uint8_t x, uint8_t y)
: _x(x), _y(y)
{
if (random(2))
_vx = 1;
else
_vx = -1;
if (random(2))
_vy = 1;
else
_vy = -1;
}

void Ball::move(uint8_t gamex, uint8_t gamey, uint8_t gamew, uint8_t gameh)
{
if ((_x == gamex + gamew - ballsize && _vx > 0) || (_x == gamex && _vx < 0))
{
_vx = -_vx;
gb.sound.playTick();
}
else
_x += _vx;

if ((_y == gamey + gameh - ballsize && _vy > 0) || (_y == gamey && _vy < 0))
{
_vy = -_vy;
gb.sound.playTick();
}
else
_y += _vy;
}

void Ball::collide(Ball &ball)
{
if (!gb.collideRectRect(_x, _y, ballsize, ballsize, ball._x, ball._y, ballsize, ballsize))
return;
//ball on the right
if (_vx > 0 && ball._x == _x + ballsize - 1)
{
_vx = -1;
ball._vx = 1;
}
//ball on the left
else if (_vx < 0 && _x == ball._x + ballsize - 1)
{
_vx = 1;
ball._vx = -1;
}
//ball on the top
if (_vy > 0 && ball._y == _y + ballsize - 1)
{
_vy = -1;
ball._vy = 1;
}
//ball on the bottom
else if (_vy < 0 && _y == ball._y + ballsize - 1)
{
_vy = 1;
ball._vy = -1;
}
gb.sound.playTick();
}

bool Ball::collide(Line &line)
{
if (!line.collision(*this))
return false;

if (line.getH())
_vy *= -1;
else
_vx *= -1;
gb.sound.playTick();
return true;
}


void Ball::draw() const
{
gb.display.fillRect(_x, _y, ballsize, ballsize);
}

28 changes: 28 additions & 0 deletions ball.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once

#include <Arduino.h>

class Line;

class Ball {
public:
Ball(uint8_t x, uint8_t y);

void draw() const;

void move(uint8_t gamex, uint8_t gamey, uint8_t gamew, uint8_t gameh);

void collide(Ball &ball);
bool collide(Line &ball);

inline uint8_t getX() { return _x; }
inline uint8_t getY() { return _y; }

static uint8_t ballsize;
static uint8_t maxballs;

private:
uint8_t _x, _y;
int8_t _vx, _vy;
};

159 changes: 159 additions & 0 deletions board.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
#include "board.h"

#include <Gamebuino.h>
extern Gamebuino gb;

extern unsigned int level;
extern unsigned int score;
extern unsigned int levelscore;

extern int nbboards;
extern Board **boards;

uint8_t Board::totalballs = 0;
Ball ** Board::ballsarray = NULL;

Board::Board(uint8_t x, uint8_t y, uint8_t w, uint8_t h)
:_x(x), _y(y), _w(w), _h(h), _nbballs(0), _balls(NULL)
{
}

void Board::initBalls(uint8_t nb)
{
if (ballsarray)
clearBalls();

totalballs = nb;
ballsarray = (Ball **)malloc(totalballs * sizeof(Ball *));
_nbballs = nb;
_balls = ballsarray;
memset(ballsarray, 0, sizeof(ballsarray));
for (uint8_t i = 0 ; i < totalballs ; i++)
ballsarray[i] = new Ball(random(_w - Ball::ballsize), random(_h - Ball::ballsize));
}

void Board::clearBalls()
{
if (!ballsarray)
return;
for (uint8_t i = 0 ; i < totalballs ; i++)
delete ballsarray[i];
free(ballsarray);
ballsarray = NULL;
totalballs = 0;
}

void Board::moveBalls()
{
for (uint8_t i = 0 ; i < _nbballs ; i++)
{
_balls[i]->move(_x, _y, _w, _h);

//bounce balls against each other
for (uint8_t j = i + 1 ; j < _nbballs ; j++)
_balls[i]->collide(*_balls[j]);
}
}

Board *Board::split(Line &line)
{
uint8_t tempnbballs = _nbballs;

if (!line.finished())
return;

if (line.getH())
{
boards[nbboards] = new Board(_x, line.getY() + 1, _w, _h + _y - line.getY() - 1);
boards[nbboards]->_balls = _balls + _nbballs;
_h = line.getY() - _y;
}
else
{
boards[nbboards] = new Board(line.getX() + 1, _y, _w + _x - line.getX() - 1, _h);
boards[nbboards]->_balls = _balls + _nbballs;
_w = line.getX() - _x;
}
_nbballs = 0;

while (_nbballs + boards[nbboards]->_nbballs < tempnbballs)
{
if (gb.collideRectRect(_x, _y, _w, _h, _balls[_nbballs]->getX(), _balls[_nbballs]->getY(), Ball::ballsize, Ball::ballsize))
_nbballs++;
else
{
Ball *temp = _balls[_nbballs];
_balls[_nbballs] = _balls[tempnbballs - boards[nbboards]->_nbballs - 1];
_balls[tempnbballs - boards[nbboards]->_nbballs - 1] = temp;
boards[nbboards]->_balls--;
boards[nbboards]->_nbballs++;
}
}
//Check for empty boards
if (boards[nbboards]->_nbballs == 0)
{
if (line.getH())
{
uint16_t s = boards[nbboards]->_w * (boards[nbboards]->_h + 1);
score += s;
levelscore += s;
}
else
{
uint16_t s = (boards[nbboards]->_w + 1) * boards[nbboards]->_h;
score += s;
levelscore += s;
}
boards[nbboards]->_balls = NULL;
delete boards[nbboards];
}
else if (_nbballs == 0)
{
if (line.getH())
{
uint16_t s = _w * (_h + 1);
score += s;
levelscore += s;
}
else
{
uint16_t s = (_w + 1) * _h;
score += s;
levelscore += s;
}
_x = boards[nbboards]->_x;
_y = boards[nbboards]->_y;
_w = boards[nbboards]->_w;
_h = boards[nbboards]->_h;
_balls = boards[nbboards]->_balls;
_nbballs = boards[nbboards]->_nbballs;
boards[nbboards]->_balls = NULL;
delete boards[nbboards];
}
else
{
if (line.getH())
{
score += _w;
levelscore += _w;
}
else
{
score += _h;
levelscore += _h;
}
nbboards++;
}
}

void Board::draw() const
{
gb.display.fillRect(_x, _y, _w, _h);
}

void Board::drawBalls() const
{
for (uint8_t i = 0 ; i < _nbballs ; i++)
_balls[i]->draw();
}

37 changes: 37 additions & 0 deletions board.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#pragma once

#include <Arduino.h>

#include "ball.h"
#include "line.h"

class Board {
public:
Board(uint8_t x, uint8_t y, uint8_t w, uint8_t h);

inline uint8_t getX() { return _x; }
inline uint8_t getY() { return _y; }
inline uint8_t getW() { return _w; }
inline uint8_t getH() { return _h; }
inline uint8_t getNbBalls() { return _nbballs; }
inline Ball * getBall(uint8_t i) { return _balls[i]; }

void initBalls(uint8_t nb);
static void clearBalls();

void moveBalls();

Board *split(Line &line);

void draw() const;
void drawBalls() const;

private:
uint8_t _x, _y, _w, _h;
Ball **_balls;
uint8_t _nbballs;

static Ball ** ballsarray;
static uint8_t totalballs;
};

22 changes: 22 additions & 0 deletions cursor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "cursor.h"

#include <Gamebuino.h>
extern Gamebuino gb;

uint8_t Cursor::cursorsize = 3;
uint8_t Cursor::gamew = LCDWIDTH - 22;
uint8_t Cursor::gameh = LCDHEIGHT;

Cursor::Cursor()
:_x(gamew/2 - cursorsize + 1), _y(gameh/2), _horizontal(true)
{
}

void Cursor::draw() const
{
if (_horizontal)
gb.display.drawFastHLine(_x - (cursorsize/2), _y, cursorsize);
else
gb.display.drawFastVLine(_x, _y - (cursorsize/2), cursorsize);
}

28 changes: 28 additions & 0 deletions cursor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once

#include <Arduino.h>

class Cursor {
public:
Cursor();

void draw() const;

uint8_t getX() const { return _x; }
uint8_t getY() const { return _y; }
bool getH() const { return _horizontal; }

inline void up() { _y = constrain(_y - 1, 0, gameh - 1); }
inline void down() { _y = constrain(_y + 1, 0, gameh - 1); }
inline void right() { _x = constrain(_x + 1, 0, gamew - 1); }
inline void left() { _x = constrain(_x - 1, 0, gamew - 1); }
inline void rotate() { _horizontal = !_horizontal; }

private:
uint8_t _x, _y;
bool _horizontal;

static uint8_t gamew, gameh;
static uint8_t cursorsize;
};

Loading

0 comments on commit fbb97a1

Please sign in to comment.