#include <GrafxT3.h>
#include <SPIN.h>
#include "SPI.h"
#include <Bounce.h>
#include "bitmaps.h"
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
#define TFT_DC 9
#define TFT_CS 10
#define TFT_RST 7
#define TFT_SCK 13
#define TFT_MISO 12
#define TFT_MOSI 11
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
uint8_t use_fb = 0;
uint8_t use_clip_rect = 0;
uint8_t use_set_origin = 0;
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
GrafxT3 tft = GrafxT3(TFT_CS, TFT_DC, TFT_RST, TFT_MOSI, TFT_SCK, TFT_MISO, &SPIN);
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
int player_x = 240;
int player_y = 320;
int player_direction = 2;
int x=-0,y=0;
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// Camera offset
int cameraX = -160; /////starting position X 0f camera on tilemap
int cameraY = -120; /////starting position Y of camera on tilemap
// Camera offset boundaries
const int cameraXMin = -2352;
const int cameraXMax = 0;
const int cameraYMin = -2352;
const int cameraYMax = 0;
// Player offset boundaries
const int playerXMin = 64;
const int playerXMax = 274;
const int playerYMin = 64;
const int playerYMax = 180;
///////////////////////////////////////////////////////////////////////////////
///////////////////////////Pixel Color Includes////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
uint16_t palette[16]; // Should probably be 256, but I don't use many colors...
uint16_t pixel_data[2500];
//Extra integers for color palette
int a = 0xa; int b = 0xb; int c = 0xc;
int d = 0xd; int e = 0xe; int f = 0xf;
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////Button assignments//////////////////////////////
/////////////////////////////////////////////////////////////////////////////
//////dpad + select buttons
const int buttonUp = 33; //up button
Bounce ButtonUp = Bounce(buttonUp, 10); // 10 ms debounce
const int buttonDown = 38; //down_button
Bounce ButtonDown = Bounce(buttonDown, 10); // 10 ms debounce
const int buttonLeft = 35; //left button
Bounce ButtonLeft = Bounce(buttonLeft, 10); // 10 ms debounce
const int buttonRight = 17; //right button
Bounce ButtonRight = Bounce(buttonRight, 10); // 10 ms debounce
const int buttonS = 21; //select button
Bounce ButtonS = Bounce(buttonS, 10); // 10 ms debounce
//////action + start buttons
const int buttonX = 32; // X button up
Bounce ButtonX = Bounce(buttonX, 10); // 10 ms debounce
const int buttonY = 26; // Y button left
Bounce ButtonY = Bounce(buttonY, 10); // 10 ms debounce
const int buttonA = 21; // A button right
Bounce ButtonA = Bounce(buttonA, 10); // 10 ms debounce
const int buttonB = 28; // B buttun down
Bounce ButtonB = Bounce(buttonB, 10); // 10 ms debounce
const int buttonT = 4; // Start button
Bounce ButtonT = Bounce(buttonT, 10); // 10 ms debounce
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////Set-up//////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
void setup() {
while (!Serial && (millis() < 4000)) ;
Serial.begin(115200);
tft.begin();
tft.setRotation(1);
tft.fillScreen(BLACK);
//tft.setFrameRate(60);
tft.persistence = false;
pinMode(buttonUp, INPUT_PULLUP);
pinMode(buttonDown, INPUT_PULLUP);
pinMode(buttonLeft, INPUT_PULLUP);
pinMode(buttonRight, INPUT_PULLUP);
pinMode(buttonS, INPUT_PULLUP);
pinMode(buttonX, INPUT_PULLUP);
pinMode(buttonY, INPUT_PULLUP);
pinMode(buttonA, INPUT_PULLUP);
pinMode(buttonB, INPUT_PULLUP);
pinMode(buttonT, INPUT_PULLUP);
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////Loop////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
void loop(void) {
//updates the GameRIot (the display, the sound, the buttons, everyyhing)
//returns true when it's time to render a new frame (20 times/second)
// if(tft.updateAll()){
///////////////////////////////////////////////////////////////////////////////
////////////////////////////////camera controls////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// Clamp cameraX
if(cameraX < cameraXMin)
{
cameraX = cameraXMin;
}
else if(cameraX > cameraXMax)
{
cameraX = cameraXMax;
}
// Clamp cameraY
if(cameraY < cameraYMin)
{
cameraY = cameraYMin;
}
else if(cameraY > cameraYMax)
{
cameraY = cameraYMax;
}
// Check if player is beyond X boundary
if(player_x < playerXMin)
{
++cameraX;
if(cameraX > cameraXMin && cameraX < cameraXMax)
{
player_x = playerXMin;
}
}
else if(player_x > playerXMax)
{
--cameraX;
if(cameraX > cameraXMin && cameraX < cameraXMax)
{
player_x = playerXMax;
}
}
// Check if player is beyond Y boundary
if(player_y < playerYMin)
{
++cameraY;
if(cameraY > cameraYMin && cameraY < cameraYMax)
{
player_y = playerYMin;
}
}
else if(player_y > playerYMax)
{
--cameraY;
if(cameraY > cameraYMin && cameraY < cameraYMax)
{
player_y = playerYMax;
}
}
//////////////////////////////////////////////////////////////////////////////
///////////////////////////////Palette////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
palette[0] = 0;
palette[1] = BLACK;
palette[2] = BLUE;
palette[3] = BROWN;
palette[4] = DARKGREEN;
palette[5] = GREY;
palette[6] = PINK;
palette[7] = RED;
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
palette[8] = LIGHTBROWN;
palette[9] = GREEN;
palette[a]= DARKGREY;
palette[b] = LIGHTGREY;
palette[c] = YELLOW;
palette[d] = PURPLE;
palette[e] = WHITE;
palette[f] = NAVY;
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////Tilemap/////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
tft.drawTilemap(cameraX, cameraY, dune_demo, spritesheet, palette);
///////////////////////////////////////////////////////////////////////////////
///////////////////////////Buttons/////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
if (ButtonUp.update());
if (ButtonDown.update());
if (ButtonLeft.update());
if (ButtonRight.update());
if (ButtonA.update());
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
ButtonUp.rebounce(10);
ButtonDown.rebounce(10);
ButtonLeft.rebounce(10);
ButtonRight.rebounce(10);
ButtonA.rebounce(10);
///////////////////////////////////////////////////////////////////////////////
//////////////////////////Down/////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
if (ButtonUp.fallingEdge()){
tft.writeRectNBPP(player_x, player_y,16,16,4,paul_rearwalk,palette);
player_direction = 1;
player_y = player_y - 5;
if(checkcolision())player_y--;}
if(player_y <= 16){
player_y = 16;}
//////////////////////////////////////////////////////////////////////////////
/////////////////////////////////Up///////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
if (ButtonDown.fallingEdge()){
tft.writeRectNBPP(player_x, player_y,16,16,4,paul_frontwalk,palette);
player_direction = 2;
player_y = player_y + 5;
if(checkcolision())player_y++;}
if(player_y >= 216){
player_y = 216;}
//////////////////////////////////////////////////////////////////////////////
//////////////////////////Left////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
if (ButtonLeft.fallingEdge()){
tft.writeRectNBPP(player_x, player_y,16,16,4,paul_leftwalk,palette);
player_direction = 3;
player_x = player_x - 5;
if(checkcolision())player_x--;}
if(player_x >= 288){
player_x = 288;}
//////////////////////////////////////////////////////////////////////////////
////////////////////////////Right////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
if (ButtonRight.fallingEdge()){
tft.writeRectNBPP(player_x, player_y,16,16,4,paul_rightwalk,palette);
player_direction = 4;
player_x = player_x + 5;
if(checkcolision())player_x++;}
if(player_x <= 16){
player_x = 16;}
///////////////////////////////////////////////////////////////////////////////
//////////////////////////////PLAYER DIRECTION/////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
if (player_direction == 1){
tft.writeRectNBPP(player_x, player_y,16,16,4,paul_rear,palette);
}
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
else if (player_direction == 2){
tft.useFrameBuffer(use_fb);
uint32_t start_time = millis();
tft.writeRectNBPP(player_x, player_y,16,16,4,paul_front,palette);
tft.updateScreen();
use_fb = !use_fb;
}
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
else if (player_direction == 3){
tft.writeRectNBPP(player_x, player_y,16,16,4,paul_left,palette);
}
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
else if (player_direction == 4){
tft.writeRectNBPP(player_x, player_y,16,16,4,paul_right,palette);
}
}
//}
/////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////collision/////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
bool checkcolision() { // Transformed it into a function
uint16_t i;
for(i=0; i < tft.numcolision + 1; i++)
{
if(tft.collideRectRect(player_x, player_y,16,16,tft.solid[i].x,tft.solid[i].y,16,16))
{
if(tft.solid[i].spritecol == blank_tile)return false; //Do nothing because it's floor - this line not needed
else if(tft.solid[i].spritecol == cave)return true;
else if(tft.solid[i].spritecol == grass)return false;
else if(tft.solid[i].spritecol == grassbl)return false;
else if(tft.solid[i].spritecol == grassbr)return false;
else if(tft.solid[i].spritecol == grasstl)return false;
else if(tft.solid[i].spritecol == grasstr)return false;
else if(tft.solid[i].spritecol == rock){tft.popup(F(" ""Rock"" "),1); return true;} //Return True if character have touched the wall
else if(tft.solid[i].spritecol == rockbl)return true;
else if(tft.solid[i].spritecol == rockbr)return true;
else if(tft.solid[i].spritecol == rocktl)return true;
else if(tft.solid[i].spritecol == rocktr)return true;
else if(tft.solid[i].spritecol == sand)return false;
else if(tft.solid[i].spritecol == seitch)return true;
else if(tft.solid[i].spritecol == stairsl)return true;
else if(tft.solid[i].spritecol == stairsr)return true;
else if(tft.solid[i].spritecol == tree)return false;
}
}
return false; // Return false if don't touch anything
}
//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////