#include <Backlight.h>
#include <Battery.h>
#include <Buttons.h>
#include <Display.h>
#include <Gamebuino.h>
#include <Sound.h>
#include <SPI.h>
#include <math.h>
//#include <string>
Gamebuino gb;
//Todo:
//-Add more boncy physics
const byte Logo[] PROGMEM = {64,30,0xAD,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0xB5,0x0,0x0,0x0,0x77,0x70,0x0,0x0,0xAD,0x0,0x0,0x0,0x52,0x40,0x0,0x0,0xB5,0x0,0x0,0x0,0x62,0x50,0x0,0x0,0xAD,0x0,0x0,0x0,0x52,0x50,0x0,0x0,0xB5,0x0,0x0,0x0,0x77,0x70,0x0,0x0,0xAD,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0xB5,0x0,0x0,0x0,0x74,0x27,0x50,0x0,0xAD,0x0,0x0,0x0,0x54,0x54,0x50,0x0,0xB5,0x0,0x0,0x0,0x64,0x74,0x60,0x0,0xAD,0x0,0x0,0x0,0x54,0x54,0x50,0x0,0xB5,0x0,0x0,0x0,0x77,0x57,0x50,0x0,0xAD,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0xB5,0x0,0x0,0x0,0x77,0x50,0x0,0x0,0xAD,0x0,0xF,0xF0,0x55,0x50,0x0,0x0,0xB5,0x0,0xF,0xF0,0x65,0x20,0x20,0x0,0xAD,0x0,0xD,0xD0,0x55,0x50,0x0,0x0,0xB5,0x0,0xD,0xD0,0x77,0x50,0x40,0x0,0xAD,0x0,0xF,0xF0,0x0,0x0,0x20,0x0,0xB5,0x0,0xF,0xF0,0xFF,0x0,0xF0,0x0,0xAD,0x0,0xF,0xF0,0x81,0x0,0x60,0x0,0xB5,0x0,0xF,0xF0,0xBD,0x0,0x60,0x0,0xFF,0xFF,0xFF,0xFF,0xA5,0x0,0xF0,0x0,0xC3,0xA6,0x66,0x65,0xA5,0x1,0xB8,0x0,0xA5,0xB9,0x99,0x9D,0xA5,0x3,0x9C,0x0,0x99,0xA6,0x66,0x65,0xA5,0x3,0xC,0x0,0x99,0xB9,0x99,0x9D,0xA5,0x1,0xF8,0x0,0xA5,0xA6,0x66,0x65,0xA5,0x0,0x0,0x0,0xC3,0xB9,0x99,0x9D,0xA5,0x0,0x0,0x0,0xFF,0xA6,0x66,0x65,0xE7,0x0,0x0,0x0,};
const PROGMEM byte Empty[] = //ID: 0
{
8,8,
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,
};
const PROGMEM byte GroundLeft[] = //ID: 10
{
8,8,
B11111111,
B10111001,
B10100110,
B10111001,
B10100110,
B10111001,
B10100110,
B10111001,
};
const PROGMEM byte GroundRight[] = //ID: 11
{
8,8,
B11111111,
B10011101,
B01100101,
B10011101,
B01100101,
B10011101,
B01100101,
B10011101,
};
const PROGMEM byte GroundMiddle[] = //ID: 12
{
8,8,
B11111111,
B01100110,
B10011001,
B01100110,
B10011001,
B01100110,
B10011001,
B01100110,
};
const PROGMEM byte Brick[] = //ID: 1
{
8,8,
B11111111,
B00100001,
B11111111,
B10001000,
B11111111,
B00100001,
B11111111,
B10001000,
};
const PROGMEM byte SharpBrick[]= //ID: 2
{
8,8,
B11111111,
B11000011,
B10100101,
B10011001,
B10011001,
B10100101,
B11000011,
B11111111,
};
const PROGMEM byte TowerBrick[]= //ID: 3
{
8,8,
B10101101,
B10110101,
B10101101,
B10110101,
B10101101,
B10110101,
B10101101,
B10110101,
};
const PROGMEM byte Piston[]= //ID: 4
{
8,8,
B11111111,
B10000001,
B11011011,
B11011011,
B10111101,
B11100111,
B10000001,
B11111111,
};
const PROGMEM byte Holder[]= //ID: 5
{
8,8,
B11111111,
B10100101,
B11111111,
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,
};
const PROGMEM byte Teleporter1[]= //ID: 6
{
8,8,
B11111111,
B10101011,
B11010110,
B10101100,
B10101100,
B11010110,
B10101011,
B11111111,
};
const PROGMEM byte Teleporter0[]= //ID: 7
{
8,8,
B11111111,
B11010101,
B01101011,
B00110101,
B00110101,
B01101011,
B11010101,
B11111111,
};
const PROGMEM byte Wall[]= //ID: 8
{
8,8,
B11110000,
B10010000,
B10010000,
B10010000,
B10010000,
B10010000,
B10010000,
B11110000,
};
const PROGMEM byte Ennemie[]= //ID: 9
{
8,8,
B11111111,
B10000001,
B10010011,
B10010011,
B10000001,
B10000001,
B10000001,
B11111111,
};
const byte Map[] PROGMEM = {
0,0,0,0,0,0,0,0,0,0,0,0,2,
2,1,2,5,5,8,0,0,0,0,0,0,6,
3,0,0,0,0,8,0,0,0,0,0,0,2,
3,0,0,0,0,0,4,5,2,0,0,0,3,
2,0,2,0,0,0,0,0,0,0,0,0,3,
3,0,3,0,0,0,0,0,0,0,0,0,3,
3,0,3,0,0,0,2,5,5,0,5,5,2,
3,0,3,0,9,0,3,0,9,0,0,0,6,
2,0,2,1,1,1,2,1,1,1,1,1,2,
};
#define LCDWidth 84
#define LCDHeight 48
#define BouncyMath1 0.0322580//6452
#define BouncyMath2 -0.0483870f//9677
#define BouncyMath3 -0.0393700f//7874
#define BouncyMath4 0.0236220f//4724
byte MapHeigth = 9;
byte MapWidth = 13;
byte SpawnX = 9;
byte SpawnY = 2;
boolean GoingRight = true;
boolean GroundedDown = false;
boolean GroundedRight = false;
boolean GroundedLeft = false;
const byte* sprites[10] = {
Empty,
Brick,
SharpBrick,
TowerBrick,
Piston,
Holder,
Teleporter0,
Teleporter1,
Wall,
Ennemie
};
const byte* coltype[10] = { //0 = empty 1 = block
0,
1,
1,
1,
1,
2, //2
3,
4,
5, //5
0
};
byte getTile(byte x, byte y){
return pgm_read_byte(Map + (x+y*MapWidth));
} //< Need to be improved (Not adapted for multiples maps)
int CPosX = 0; //Cam Position
int CPosY = 0;
float PPosX = 0; //Player Position
float PPosY = 0;
byte SquisheVertical = 8; //Player squishness :)
byte SquisheHorizontal = 8;
char VelocityX; //Velocity (used for physics)
char VelocityY;
void setup() {
// put your setup code here, to run once:
gb.begin();
gb.titleScreen(F("Developpement"),Logo);
gb.battery.show = false;
PPosX = -(8*SpawnX)-4; //Convert Spawnpos to Worldpos
PPosY = -(8*SpawnY)-8;
//VelocityY = 45;
}
void DrawPlayer () { //Draw player based on squisness
if(GoingRight == true) {
gb.display.fillRect(CPosX - (PPosX + SquisheHorizontal/2.0) + 8, CPosY - (PPosY + SquisheVertical/2.0) + 8, SquisheHorizontal, SquisheVertical);
gb.display.setColor(WHITE,WHITE);
gb.display.drawFastVLine(CPosX - (PPosX + SquisheHorizontal/2.0) + 6 + SquisheHorizontal, CPosY - (PPosY + SquisheVertical/2.0) + 10, 2);
gb.display.drawFastVLine(CPosX - (PPosX + SquisheHorizontal/2.0) + 3 + SquisheHorizontal, CPosY - (PPosY + SquisheVertical/2.0) + 10, 2);
gb.display.setColor(BLACK,WHITE);
} else {
gb.display.fillRect(CPosX - (PPosX + SquisheHorizontal/2.0) + 8, CPosY - (PPosY + SquisheVertical/2.0) + 8, SquisheHorizontal, SquisheVertical);
gb.display.setColor(WHITE,WHITE);
gb.display.drawFastVLine(CPosX - (PPosX + SquisheHorizontal/2.0) + 9, CPosY - (PPosY + SquisheVertical/2.0) + 10, 2);
gb.display.drawFastVLine(CPosX - (PPosX + SquisheHorizontal/2.0) + 12, CPosY - (PPosY + SquisheVertical/2.0) + 10, 2);
gb.display.setColor(BLACK,WHITE);
}
}
void ClampCamera () { //Clamp the camera in the world
CPosX = ClampInt(-(8*MapWidth-LCDWidth),0,PPosX+(LCDWIDTH/2)-8);
CPosY = ClampInt(-(8*MapHeigth-LCDHeight),-8,PPosY+(LCDHEIGHT/2)-8);
}
int ClampInt (int minv, int maxv, int value) {
if(value < minv)
return minv;
else if(value >= maxv)
return maxv;
else
return value;
}
int PixelInCollider (byte PIMX, byte PIMY, byte PICX, byte PICY) { //PIM = Pos in map (0-255), PIC = Pos in cube (0-8)
byte GlobalColiderType = coltype[getTile(PIMX,PIMY)];
byte x1;
byte y1;
byte x2;
byte y2;
byte Value = 0;
switch (GlobalColiderType) {
case 0:
Value = 1;
break;
case 1:
Value = 2;
break;
case 2:
x1 = 0;
y1 = 0;
x2 = 7;
y2 = 3;
break;
case 3:
Value = 3;
break;
case 4:
Value = 4;
case 5:
x1 = 0;
y1 = 0;
x2 = 4;
y2 = 7;
break;
default:
Value = 1;
}
if(Value == 0) {
if(PICY == 0) {
gb.popup(F("Code: 0"), 20);
} else if(PICY == 1) {
gb.popup(F("Code: 1"), 20);
} else if(PICY == 2) {
gb.popup(F("Code: 2"), 20);
} else if(PICY == 3) {
gb.popup(F("Code: 3"), 20);
} else if(PICY == 4) {
gb.popup(F("Code: 4"), 20);
} else if(PICY == 5) {
gb.popup(F("Code: 5"), 20);
} else if(PICY == 6) {
gb.popup(F("Code: 6"), 20);
} else if(PICY == 7) {
gb.popup(F("Code: 7"), 20);
} else if(PICY == 8) {
gb.popup(F("Code: 8"), 20);
} else if(PICY == 9) {
gb.popup(F("Code: 9"), 20);
} else if(PICY == 10) {
gb.popup(F("Code: 10"), 20);
} else {
if(PICY > 255) {
gb.popup(F("else > 255"), 20);
} else {
gb.popup(F("else < 255"), 20);
}
}
/*if(PICY >= 0) {
gb.popup(F("True"), 20);
} else {
gb.popup(F("False"), 20);
}*/
if(-PICX>= x1 && -PICX <= x2 && -PICY>= y1 && -PICY<= y2) {
Value = 2;
} else {
Value = 1;
}
}
return Value - 1;
}
void CheckForCollider () { //Based on current velocity //Need improvement to support BigWhiteBox
if(VelocityX != 0) {
byte ColX[8];
if(VelocityX > 0) {
for(int xd; xd < 8; xd++) { //yd = Y Down
ColX[xd] = PixelInCollider((-(floor((PPosX + 5 + 0) / 8.0))), (-(floor((PPosY - 3 + xd) / 8.0))), -(PPosX + 5 + 0) - (-(floor((PPosX + 5 + 0) / 8.0)))*8, -(PPosY - 3 + xd) - (-(floor((PPosY - 3 + xd) / 8.0)))*8);
}
for(int xd; xd < 8; xd++) {
if(ColX[xd] == 1) {
//gb.popup(F("A wall has been detected"), 20);
VelocityX = 0;
GroundedLeft = true;
}
}
}
if(VelocityX < 0) {
for(int xd; xd < 8; xd++) { //yd = Y Down
ColX[xd] = PixelInCollider((-(floor((PPosX - 4 + 0) / 8.0))), (-(floor((PPosY - 3 + xd) / 8.0))), -(PPosX - 4 + 0) - (-(floor((PPosX - 4 + 0) / 8.0)))*8, -(PPosY - 3 + xd) - (-(floor((PPosY - 3 + xd) / 8.0)))*8);
}
}
for(int xd; xd < 8; xd++) {
if(ColX[xd] == 1) {
//gb.popup(F("A wall has been detected"), 20);
VelocityX = 0;
GroundedRight = true;
}
}
}
//working stuff V
if(VelocityY != 0) {
byte ColY[8];
if(VelocityY > 0) {
for(int yd; yd < 8; yd++) { //yd = Y Down
ColY[yd] = PixelInCollider((-(floor((PPosX - 3 + yd) / 8.0))), (-(floor((PPosY + 5 + 0) / 8.0))), -(PPosX - 3 + yd) - (-(floor((PPosX - 3 + yd) / 8.0)))*8, -(PPosY + 5 + 0) - (-(floor((PPosY + 5 + 0) / 8.0)))*8);
}
for(int yd; yd < 8; yd++) {
if(ColY[yd] == 1) {
VelocityY = 0;
}
}
}
if(VelocityY < 0) {
for(int yd; yd < 8; yd++) { //yd = Y Down
ColY[yd] = PixelInCollider((-(floor((PPosX - 3 + yd) / 8.0))), (-(floor((PPosY - 4 + 0) / 8.0))), -(PPosX - 3 + yd) - (-(floor((PPosX - 3 + yd) / 8.0)))*8, -(PPosY - 4 + 0) - (-(floor((PPosY - 4 + 0) / 8.0)))*8);
}
for(int yd; yd < 8; yd++) {
if(ColY[yd] == 1) {
VelocityY = 0;
GroundedDown = true;
}
}
}
}
}
void loop() {
// put your main code here, to run repeatedly:
if(gb.update()) {
GroundedDown = false;
GroundedRight = false;
GroundedLeft = false;
VelocityY = VelocityY - 3;
VelocityX = VelocityX * 0.6f;
if(gb.buttons.timeHeld(BTN_RIGHT) > 0) {
if(VelocityX - 8 > -50) {
VelocityX -= 8;
}
GoingRight = true;
}
if(gb.buttons.timeHeld(BTN_LEFT) > 0) {
if(VelocityX + 8 < 50) {
VelocityX += 8;
}
GoingRight = false;
}
CheckForCollider();
if(gb.buttons.pressed(BTN_A) && GroundedDown)
VelocityY = 66;
if(gb.buttons.pressed(BTN_A) && GroundedRight) {
VelocityX += 60;
VelocityY = 56;
}
if(gb.buttons.pressed(BTN_A) && GroundedLeft) {
VelocityX -= 60;
VelocityY = 56;
}
PPosX += (float)VelocityX / (float)127 * 3;
PPosY += (float)VelocityY / (float)127 * 3;
if(VelocityY > 0) {
//ex: 62 to 0 is flat to normal
if(VelocityY > 62) {
SquisheVertical = 5;
SquisheHorizontal = 10;
} else {
SquisheVertical = 8+(VelocityY*BouncyMath2);
SquisheHorizontal = 8+(VelocityY*BouncyMath1);
}
}
if(VelocityY < 0) {
//ex: 0 to -127 is normal to verticaly flat
SquisheVertical = 8+(VelocityY*BouncyMath3);
SquisheHorizontal = 8+(VelocityY*BouncyMath4);
}
if(VelocityY == 0) {
SquisheVertical = 8;
SquisheHorizontal = 8;
}
DrawPlayer();
ClampCamera();
for(byte x = 0; x < MapWidth; x++) { //Draw maps
for(byte y = 0; y < MapHeigth; y++) { //< GetTile sould get the map based the current one loaded and not the first
gb.display.drawBitmap(CPosX + x*8, CPosY + y*8, sprites[getTile(x,y)]);
}
}
}
}
if(VelocityY != 0) {
byte ColY[8];
if(VelocityY > 0) {
for(int yd; yd < 8; yd++) { //yd = Y Down
ColY[yd] = PixelInCollider((-(floor((PPosX - 3 + yd) / 8.0))) , (-(floor((PPosY + 5 + 0) / 8.0))) , -(-(PPosY - 3 + yd) - (-(floor((PPosX - 3 + yd) / 8.0)))*8) , -(-(PPosY + 5 + 0) - (-(floor((PPosY + 5 + 0) / 8.0)))*8));
}
for(int yd; yd < 8; yd++) {
if(ColY[yd] == 1) {
VelocityY = 0;
}
}
}
if(VelocityY < 0) {
for(int yd; yd < 8; yd++) { //yd = Y Down
ColY[yd] = PixelInCollider((-(floor((PPosX - 3 + yd) / 8.0))) , (-(floor((PPosY - 4 + 0) / 8.0))) , -(-(PPosY - 3 + 0) - (-(floor((PPosX - 3 + yd) / 8.0)))*8) , -(-(PPosY - 4 + 0) - (-(floor((PPosY - 4 + 0) / 8.0)))*8));
}
for(int yd; yd < 8; yd++) {
if(ColY[yd] == 1) {
VelocityY = 0;
GroundedDown = true;
}
}
}
}
if(VelocityX > 0) {
for(int xd; xd < 8; xd++) { //yd = Y Down
ColX[xd] = PixelInCollider((-(floor((PPosX + 5 + 0) / 8.0))) , (-(floor((PPosY - 3 + xd) / 8.0))) , [u]-(-(PPosX + 5 + 0) - (-(floor((PPosX + 5 + 0) / 8.0)))*8) , -(-(PPosY - 3 + xd) - (-(floor((PPosY - 3 + xd) / 8.0)))*8)[/u]);
}
for(int xd; xd < 8; xd++) {
if(ColX[xd] == 1) {
//gb.popup(F("A wall has been detected"), 20);
VelocityX = 0;
GroundedLeft = true;
}
}
}
if(VelocityX < 0) {
for(int xd; xd < 8; xd++) { //yd = Y Down
ColX[xd] = PixelInCollider((-(floor((PPosX - 4 + 0) / 8.0))) , (-(floor((PPosY - 3 + xd) / 8.0))) , [u]-(-(PPosX - 4 + 0) - (-(floor((PPosX - 4 + 0) / 8.0)))*8) , -(-(PPosY - 3 + xd) - (-(floor((PPosY - 3 + xd) / 8.0)))*8)[/u]);
}
}
for(int xd; xd < 8; xd++) {
if(ColX[xd] == 1) {
//gb.popup(F("A wall has been detected"), 20);
VelocityX = 0;
GroundedRight = true;
}
}
}
#include <Backlight.h>
#include <Battery.h>
#include <Buttons.h>
#include <Display.h>
#include <Gamebuino.h>
#include <Sound.h>
#include <SPI.h>
#include <math.h>
//#include <string>
Gamebuino gb;
//Todo:
//-Add more boncy physics
const byte Logo[] PROGMEM = {64,30,0xAD,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0xB5,0x0,0x0,0x0,0x77,0x70,0x0,0x0,0xAD,0x0,0x0,0x0,0x52,0x40,0x0,0x0,0xB5,0x0,0x0,0x0,0x62,0x50,0x0,0x0,0xAD,0x0,0x0,0x0,0x52,0x50,0x0,0x0,0xB5,0x0,0x0,0x0,0x77,0x70,0x0,0x0,0xAD,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0xB5,0x0,0x0,0x0,0x74,0x27,0x50,0x0,0xAD,0x0,0x0,0x0,0x54,0x54,0x50,0x0,0xB5,0x0,0x0,0x0,0x64,0x74,0x60,0x0,0xAD,0x0,0x0,0x0,0x54,0x54,0x50,0x0,0xB5,0x0,0x0,0x0,0x77,0x57,0x50,0x0,0xAD,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0xB5,0x0,0x0,0x0,0x77,0x50,0x0,0x0,0xAD,0x0,0xF,0xF0,0x55,0x50,0x0,0x0,0xB5,0x0,0xF,0xF0,0x65,0x20,0x20,0x0,0xAD,0x0,0xD,0xD0,0x55,0x50,0x0,0x0,0xB5,0x0,0xD,0xD0,0x77,0x50,0x40,0x0,0xAD,0x0,0xF,0xF0,0x0,0x0,0x20,0x0,0xB5,0x0,0xF,0xF0,0xFF,0x0,0xF0,0x0,0xAD,0x0,0xF,0xF0,0x81,0x0,0x60,0x0,0xB5,0x0,0xF,0xF0,0xBD,0x0,0x60,0x0,0xFF,0xFF,0xFF,0xFF,0xA5,0x0,0xF0,0x0,0xC3,0xA6,0x66,0x65,0xA5,0x1,0xB8,0x0,0xA5,0xB9,0x99,0x9D,0xA5,0x3,0x9C,0x0,0x99,0xA6,0x66,0x65,0xA5,0x3,0xC,0x0,0x99,0xB9,0x99,0x9D,0xA5,0x1,0xF8,0x0,0xA5,0xA6,0x66,0x65,0xA5,0x0,0x0,0x0,0xC3,0xB9,0x99,0x9D,0xA5,0x0,0x0,0x0,0xFF,0xA6,0x66,0x65,0xE7,0x0,0x0,0x0,};
const PROGMEM byte Empty[] = //ID: 0
{
8,8,
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,
};
const PROGMEM byte GroundLeft[] = //ID: 10
{
8,8,
B11111111,
B10111001,
B10100110,
B10111001,
B10100110,
B10111001,
B10100110,
B10111001,
};
const PROGMEM byte GroundRight[] = //ID: 11
{
8,8,
B11111111,
B10011101,
B01100101,
B10011101,
B01100101,
B10011101,
B01100101,
B10011101,
};
const PROGMEM byte GroundMiddle[] = //ID: 12
{
8,8,
B11111111,
B01100110,
B10011001,
B01100110,
B10011001,
B01100110,
B10011001,
B01100110,
};
const PROGMEM byte Brick[] = //ID: 1
{
8,8,
B11111111,
B00100001,
B11111111,
B10001000,
B11111111,
B00100001,
B11111111,
B10001000,
};
const PROGMEM byte SharpBrick[]= //ID: 2
{
8,8,
B11111111,
B11000011,
B10100101,
B10011001,
B10011001,
B10100101,
B11000011,
B11111111,
};
const PROGMEM byte TowerBrick[]= //ID: 3
{
8,8,
B10101101,
B10110101,
B10101101,
B10110101,
B10101101,
B10110101,
B10101101,
B10110101,
};
const PROGMEM byte Piston[]= //ID: 4
{
8,8,
B11111111,
B10000001,
B11011011,
B11011011,
B10111101,
B11100111,
B10000001,
B11111111,
};
const PROGMEM byte Holder[]= //ID: 5
{
8,8,
B11111111,
B10100101,
B11111111,
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,
};
const PROGMEM byte Teleporter1[]= //ID: 6
{
8,8,
B11111111,
B10101011,
B11010110,
B10101100,
B10101100,
B11010110,
B10101011,
B11111111,
};
const PROGMEM byte Teleporter0[]= //ID: 7
{
8,8,
B11111111,
B11010101,
B01101011,
B00110101,
B00110101,
B01101011,
B11010101,
B11111111,
};
const PROGMEM byte Wall[]= //ID: 8
{
8,8,
B11110000,
B10010000,
B10010000,
B10010000,
B10010000,
B10010000,
B10010000,
B11110000,
};
const PROGMEM byte Ennemie[]= //ID: 9
{
8,8,
B11111111,
B10000001,
B10010011,
B10010011,
B10000001,
B10000001,
B10000001,
B11111111,
};
const byte Map[] PROGMEM = {
0,0,0,0,0,0,0,0,0,0,0,0,2,
2,1,2,5,5,8,0,0,0,0,0,0,6,
3,0,0,0,0,8,0,0,0,0,0,0,2,
3,0,0,0,0,0,4,5,2,0,0,0,3,
2,0,2,0,0,0,0,0,0,0,0,0,3,
3,0,3,0,0,0,0,0,0,0,0,0,3,
3,0,3,0,0,0,2,5,5,0,5,5,2,
3,0,3,0,9,0,3,0,9,0,0,0,6,
2,0,2,1,1,1,2,1,1,1,1,1,2,
};
#define LCDWidth 84
#define LCDHeight 48
#define BouncyMath1 0.0322580//6452
#define BouncyMath2 -0.0483870f//9677
#define BouncyMath3 -0.0393700f//7874
#define BouncyMath4 0.0236220f//4724
byte MapHeigth = 9;
byte MapWidth = 13;
byte SpawnX = 9;
byte SpawnY = 2;
boolean GoingRight = true;
boolean GroundedDown = false;
boolean GroundedRight = false;
boolean GroundedLeft = false;
const byte* sprites[10] = {
Empty,
Brick,
SharpBrick,
TowerBrick,
Piston,
Holder,
Teleporter0,
Teleporter1,
Wall,
Ennemie
};
const byte* coltype[10] = { //0 = empty 1 = block
0,
1,
1,
1,
1,
2, //2
3,
4,
5, //5
0
};
byte getTile(byte x, byte y){
return pgm_read_byte(Map + (x+y*MapWidth));
} //< Need to be improved (Not adapted for multiples maps)
int CPosX = 0; //Cam Position
int CPosY = 0;
float PPosX = 0; //Player Position
float PPosY = 0;
byte SquisheVertical = 8; //Player squishness :)
byte SquisheHorizontal = 8;
char VelocityX; //Velocity (used for physics)
char VelocityY;
void setup() {
// put your setup code here, to run once:
gb.begin();
gb.titleScreen(F("Developpement"),Logo);
gb.battery.show = false;
PPosX = -(8*SpawnX)-4; //Convert Spawnpos to Worldpos
PPosY = -(8*SpawnY)-8;
//VelocityY = 45;
}
void DrawPlayer () { //Draw player based on squisness
if(GoingRight == true) {
gb.display.fillRect(CPosX - (PPosX + SquisheHorizontal/2.0) + 8, CPosY - (PPosY + SquisheVertical/2.0) + 8, SquisheHorizontal, SquisheVertical);
gb.display.setColor(WHITE,WHITE);
gb.display.drawFastVLine(CPosX - (PPosX + SquisheHorizontal/2.0) + 6 + SquisheHorizontal, CPosY - (PPosY + SquisheVertical/2.0) + 10, 2);
gb.display.drawFastVLine(CPosX - (PPosX + SquisheHorizontal/2.0) + 3 + SquisheHorizontal, CPosY - (PPosY + SquisheVertical/2.0) + 10, 2);
gb.display.setColor(BLACK,WHITE);
} else {
gb.display.fillRect(CPosX - (PPosX + SquisheHorizontal/2.0) + 8, CPosY - (PPosY + SquisheVertical/2.0) + 8, SquisheHorizontal, SquisheVertical);
gb.display.setColor(WHITE,WHITE);
gb.display.drawFastVLine(CPosX - (PPosX + SquisheHorizontal/2.0) + 9, CPosY - (PPosY + SquisheVertical/2.0) + 10, 2);
gb.display.drawFastVLine(CPosX - (PPosX + SquisheHorizontal/2.0) + 12, CPosY - (PPosY + SquisheVertical/2.0) + 10, 2);
gb.display.setColor(BLACK,WHITE);
}
}
void ClampCamera () { //Clamp the camera in the world
CPosX = ClampInt(-(8*MapWidth-LCDWidth),0,PPosX+(LCDWIDTH/2)-8);
CPosY = ClampInt(-(8*MapHeigth-LCDHeight),-8,PPosY+(LCDHEIGHT/2)-8);
}
int ClampInt (int minv, int maxv, int value) {
if(value < minv)
return minv;
else if(value >= maxv)
return maxv;
else
return value;
}
int PixelInCollider (byte PIMX, byte PIMY, byte PICX, byte PICY) { //PIM = Pos in map (0-255), PIC = Pos in cube (0-8)
byte GlobalColiderType = coltype[getTile(PIMX,PIMY)];
byte x1;
byte y1;
byte x2;
byte y2;
byte Value = 0;
switch (GlobalColiderType) {
case 0:
Value = 1;
break;
case 1:
Value = 2;
break;
case 2:
x1 = 0;
y1 = 0;
x2 = 7;
y2 = 3;
break;
case 3:
Value = 3;
break;
case 4:
Value = 4;
case 5:
x1 = 0;
y1 = 0;
x2 = 4;
y2 = 7;
break;
default:
Value = 1;
}
if(Value == 0) {
if(PICX == 0) {
gb.popup(F("Code: 0"), 20);
} else if(PICX == 1) {
gb.popup(F("Code: 1"), 20);
} else if(PICX == 2) {
gb.popup(F("Code: 2"), 20);
} else if(PICX == 3) {
gb.popup(F("Code: 3"), 20);
} else if(PICX == 4) {
gb.popup(F("Code: 4"), 20);
} else if(PICX == 5) {
gb.popup(F("Code: 5"), 20);
} else if(PICX == 6) {
gb.popup(F("Code: 6"), 20);
} else if(PICX == 7) {
gb.popup(F("Code: 7"), 20);
} else if(PICX == 8) {
gb.popup(F("Code: 8"), 20);
} else if(PICX == 9) {
gb.popup(F("Code: 9"), 20);
} else if(PICX == 10) {
gb.popup(F("Code: 10"), 20);
} else {
if(PICY > 254) {
gb.popup(F("else > 254"), 20);
} else {
gb.popup(F("else < 254"), 20);
}
}
/*if(PICY >= 0) {
gb.popup(F("True"), 20);
} else {
gb.popup(F("False"), 20);
}*/
if(-PICX>= x1 && -PICX <= x2 && -PICY>= y1 && -PICY<= y2) {
Value = 2;
} else {
Value = 1;
}
}
return Value - 1;
}
void CheckForCollider () { //Based on current velocity //Need improvement to support BigWhiteBox
if(VelocityX != 0) {
byte ColX[8];
if(VelocityX > 0) {
for(int xd; xd < 8; xd++) { //yd = Y Down
ColX[xd] = PixelInCollider((-(floor((PPosX + 5 + 0) / 8.0))) , (-(floor((PPosY - 3 + xd) / 8.0))) , -(-(PPosX + 5 + 0) - (-(floor((PPosX + 5 + 0) / 8.0)))*8) , -(-(PPosY - 3 + xd) - (-(floor((PPosY - 3 + xd) / 8.0)))*8));
}
for(int xd; xd < 8; xd++) {
if(ColX[xd] == 1) {
//gb.popup(F("A wall has been detected"), 20);
VelocityX = 0;
GroundedLeft = true;
}
}
}
if(VelocityX < 0) {
for(int xd; xd < 8; xd++) { //yd = Y Down
ColX[xd] = PixelInCollider((-(floor((PPosX - 4 + 0) / 8.0))) , (-(floor((PPosY - 3 + xd) / 8.0))) , -(-(PPosX - 4 + 0) - (-(floor((PPosX - 4 + 0) / 8.0)))*8) , -(-(PPosY - 3 + xd) - (-(floor((PPosY - 3 + xd) / 8.0)))*8));
}
}
for(int xd; xd < 8; xd++) {
if(ColX[xd] == 1) {
//gb.popup(F("A wall has been detected"), 20);
VelocityX = 0;
GroundedRight = true;
}
}
}
//working stuff V
if(VelocityY != 0) {
byte ColY[8];
if(VelocityY > 0) {
for(int yd; yd < 8; yd++) { //yd = Y Down
ColY[yd] = PixelInCollider((-(floor((PPosX - 3 + yd) / 8.0))) , (-(floor((PPosY + 5 + 0) / 8.0))) , -(-(PPosY - 3 + yd) - (-(floor((PPosX - 3 + yd) / 8.0)))*8) , -(-(PPosY + 5 + 0) - (-(floor((PPosY + 5 + 0) / 8.0)))*8));
}
for(int yd; yd < 8; yd++) {
if(ColY[yd] == 1) {
VelocityY = 0;
}
}
}
if(VelocityY < 0) {
for(int yd; yd < 8; yd++) { //yd = Y Down
ColY[yd] = PixelInCollider((-(floor((PPosX - 3 + yd) / 8.0))) , (-(floor((PPosY - 4 + 0) / 8.0))) , -(-(PPosY - 3 + 0) - (-(floor((PPosX - 3 + yd) / 8.0)))*8) , -(-(PPosY - 4 + 0) - (-(floor((PPosY - 4 + 0) / 8.0)))*8));
}
for(int yd; yd < 8; yd++) {
if(ColY[yd] == 1) {
VelocityY = 0;
GroundedDown = true;
}
}
}
}
}
void loop() {
// put your main code here, to run repeatedly:
if(gb.update()) {
GroundedDown = false;
GroundedRight = false;
GroundedLeft = false;
VelocityY = VelocityY - 3;
VelocityX = VelocityX * 0.6f;
if(gb.buttons.timeHeld(BTN_RIGHT) > 0) {
if(VelocityX - 8 > -50) {
VelocityX -= 8;
}
GoingRight = true;
}
if(gb.buttons.timeHeld(BTN_LEFT) > 0) {
if(VelocityX + 8 < 50) {
VelocityX += 8;
}
GoingRight = false;
}
CheckForCollider();
if(gb.buttons.pressed(BTN_A) && GroundedDown)
VelocityY = 66;
if(gb.buttons.pressed(BTN_A) && GroundedRight) {
VelocityX += 60;
VelocityY = 56;
}
if(gb.buttons.pressed(BTN_A) && GroundedLeft) {
VelocityX -= 60;
VelocityY = 56;
}
PPosX += (float)VelocityX / (float)127 * 3;
PPosY += (float)VelocityY / (float)127 * 3;
if(VelocityY > 0) {
//ex: 62 to 0 is flat to normal
if(VelocityY > 62) {
SquisheVertical = 5;
SquisheHorizontal = 10;
} else {
SquisheVertical = 8+(VelocityY*BouncyMath2);
SquisheHorizontal = 8+(VelocityY*BouncyMath1);
}
}
if(VelocityY < 0) {
//ex: 0 to -127 is normal to verticaly flat
SquisheVertical = 8+(VelocityY*BouncyMath3);
SquisheHorizontal = 8+(VelocityY*BouncyMath4);
}
if(VelocityY == 0) {
SquisheVertical = 8;
SquisheHorizontal = 8;
}
DrawPlayer();
ClampCamera();
for(byte x = 0; x < MapWidth; x++) { //Draw maps
for(byte y = 0; y < MapHeigth; y++) { //< GetTile sould get the map based the current one loaded and not the first
gb.display.drawBitmap(CPosX + x*8, CPosY + y*8, sprites[getTile(x,y)]);
}
}
}
}
Return to Project Guidance & Game development
Users browsing this forum: No registered users and 6 guests