#include "mapSprite.h"
#include "Display.h"
#include "Sprites.h"
#include "Tamagotchi.h"
#include "FoodSprites.h"
#include "ToySprites.h"
#include "MedicineSprites.h"
#define PIN_BUTTON_DOWN 2
#define PIN_BUTTON_RIGHT 3
#define PIN_BUTTON_UP 4
#define PIN_BUTTON_LEFT 5
uint16_t arrowX = 136;
String userInput;
uint16_t xCoord;
unsigned long systime, timestamp, waitTime;
// 0 = dog, 1 = cat
uint16_t* pet;
Tamagotchi tamagotchi;
Map playerMap = Map(tamagotchiMap);
String name;
void drawSprite(uint16_t *spriteArray, byte width, byte height, int16_t xCoord, int16_t yCoord, byte phase = 0, bool erase = false) {
// If erase parameter is true, draw black box with width/height of the sprite at coordinates
if (erase) {
tft.fillRect(xCoord, yCoord, width, height, ILI9341_BLACK);
} else {
// Skip variable for jumping to a certain point in a sprite array (to have multiple animation phases)
uint16_t skip = phase * width * height;
// Iterate over sprite array and display pixels on the display
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
uint16_t color = spriteArray[x + y * width + skip];
if (color != 0) {
tft.drawPixel(xCoord + x, yCoord + y, color);
}
}
}
}
}
void generateRandomToy() {
// uint8_t randomChoice = random(2);
uint8_t randomChoice = 1;
xCoord = random(220);
switch (randomChoice) {
case 1:
playerMap.toys = new ToyItem[TOY]("Tespey", 15, bonepic_data);
drawSprite(playerMap.toys->getDesign(), 26, 12, xCoord, 180);
break;
case 2:
playerMap.toys = new ToyItem[TOY]("Inoplanet", 10, basketballpic_data);
drawSprite(playerMap.toys->getDesign(), 26, 26, xCoord, 180);
break;
}
}
int readButtons() {
for (int i = 0; i < 4; i++) {
if (!digitalRead(i + 2)) { //liest Knöpfe 2, 3 ,4 ,5 pins
return i;
}
}
return -1;
}
void choosePet(){
tft.fillRect(100, 20, 320, 10, ILI9341_BLACK);
if(arrowX<100) {
drawSprite(dogSprite, 48, 52, 120, 94);
pet = dogSprite;
namePet();
}else if(arrowX>100) {
drawSprite(catSprite, 48, 52, 120, 94);
pet = catSprite;
namePet();
}
}
void namePet(){
tft.setTextColor(ILI9341_RED);
tft.setTextSize(1);
tft.setCursor(100, 20);
tft.print("Menu ");
tft.setTextColor(ILI9341_WHITE);
tft.println("Name your pet");
tft.setCursor(60, 200);
tft.println("Use the serial monitor for input!");
bool chooseName = false;
while(chooseName == false){
if(Serial.available()>0){
userInput = Serial.readStringUntil('\n');
name = userInput;
if(userInput>" "){
tft.fillRect(60, 200, 320, 10, ILI9341_BLACK);
tft.fillRect(50, 190, 320, 10, ILI9341_BLACK);
tft.setCursor(50, 170);
tft.println("Are you sure you want to name your pet ");
tft.setCursor(50, 180);
tft.println(userInput + "?");
tft.setCursor(50, 200);
tft.println("Confirm your choice with the Up-button.");
tft.setCursor(50, 220);
tft.println("Reenter the name with the down-Button.");
//pause zum drücken vom Up-Button einbauen //keine Zeit Up-button zu drücken um funktion startGame() zu beginnen // kehrt wieder an den Anfang der while Schleife zurück
bool confirmed = false;
bool reenterName = false;
while(confirmed == false && reenterName == false){
systime = millis();
if(systime - timestamp >= 50) {
timestamp = systime;
int button = readButtons();
switch (button) {
case DOWN:
tft.fillRect(50, 170, 320, 60, ILI9341_BLACK);
namePet();
chooseName = true;
break;
case RIGHT:
break;
case UP:
chooseName = true;
Serial.println(button);
confirmed = true;
break;
case LEFT:
break;
}
}
}
}
//wenn up button gepresst dann while schleife verlassen
}
}
tamagotchi = Tamagotchi(name,pet, 100, 100, 100);
startGame();
}
void startGame() {
tft.fillRect(0, 200, 320, 210, ILI9341_BLACK);
tft.fillRect(0, 170, 320, 190, ILI9341_BLACK);
tft.setCursor(50, 180);
tft.println("Congrats your Pet's name from now on is "+ userInput);
tft.setCursor(50, 200);
tft.println("Press Up-Button to START the GAME!");
}
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
void setup()
{
Serial.begin(9600);
Serial.println("Welcome to Digital Pet");
randomSeed(analogRead(A0));
tft.begin();
tft.setRotation(1);
pinMode(PIN_BUTTON_UP, INPUT_PULLUP);
pinMode(PIN_BUTTON_DOWN, INPUT_PULLUP);
pinMode(PIN_BUTTON_LEFT, INPUT_PULLUP);
pinMode(PIN_BUTTON_RIGHT, INPUT_PULLUP);
// Draw border and text
tft.drawRect(0, 0, tft.width(), tft.height(), tft.color565(80, 80, 80));
tft.setTextColor(ILI9341_RED);
tft.setTextSize(1);
tft.setCursor(100, 20);
tft.print("Menu");
tft.setTextColor(ILI9341_WHITE);
tft.println(" Choose your pet");
drawSprite(dogSprite, 48, 52, 72, 94);
drawSprite(catSprite, 48, 52, 200, 94);
drawSprite(arrowSprite, 48, 52, arrowX, 160);
bool choosen = false;
while(choosen == false) {
systime = millis();
if (systime - timestamp >= 100) {
timestamp = systime;
int button = readButtons();
if (button > -1) {
// Delete sprite at old coords
drawSprite(arrowSprite, 48, 52, arrowX, 160, 0, true);
// Increase / decrease coords depending on button pressed
switch (button) {
case DOWN:
if(arrowX != 136) {
drawSprite(arrowSprite, 48, 52, 200, 160, 0, true);
drawSprite(arrowSprite, 48, 52, 72, 160, 0, true);
drawSprite(dogSprite, 48, 52, 72, 94, 0, true);
drawSprite(catSprite, 48, 52, 200, 94, 0, true);
choosen = true;
choosePet();
}else{
Serial.println("invalid please choose a pet!");
drawSprite(arrowSprite, 48, 52, arrowX, 160);
}
break;
case RIGHT:
arrowX =200;
drawSprite(arrowSprite, 48, 52, arrowX, 160);
break;
case UP:
if(arrowX != 136) {
drawSprite(arrowSprite, 48, 52, 200, 160, 0, true);
drawSprite(arrowSprite, 48, 52, 72, 160, 0, true);
drawSprite(dogSprite, 48, 52, 72, 94, 0, true);
drawSprite(catSprite, 48, 52, 200, 94, 0, true);
choosen = true;
choosePet();
}else{
Serial.println("invalid please choose a pet!");
drawSprite(arrowSprite, 48, 52, arrowX, 160);
}
break;
case LEFT:
arrowX = 72;
drawSprite(arrowSprite, 48, 52, arrowX, 160);
break;
}
}
}
}
tft.fillScreen(ILI9341_BLACK);
drawEarth();
generateRandomToy();
}
void tamagotchiScore()
{
tft.setTextSize(1);
tft.setCursor(5, 20);
tft.print("Health:");
tft.fillRect(47, 20, 10, 30, ILI9341_BLACK);
tft.setCursor(47, 20);
tft.print(tamagotchi.getHealth());
tft.setCursor(5, 30);
tft.print("Hunger:");
tft.fillRect(47, 30, 10, 30, ILI9341_BLACK);
tft.setCursor(47, 30);
tft.print(tamagotchi.getHunger());
tft.setCursor(5, 40);
tft.print("Happiness:");
tft.fillRect(65, 40, 10, 30, ILI9341_BLACK);
tft.setCursor(65, 40);
tft.print(tamagotchi.getHappiness());
}
int tamagotchiX = 0;
int tamagotchiY = 156;
int tamagotchiSpeed = 5;
bool tamagotchiDirection = 1;
//uint16_t *flipppedDog = flippedDogSprite;
void drawEarth()
{
int i = 0;
int x = 0;
while (i != 20)
{
drawSprite(playerMap.getDesign(), 16, 32, x, 210);
x += 16;
i++;
}
}
bool checkToy(uint8_t tamagochiX, uint8_t toyX)
{
if(tamagochiX < toyX)
{
if(tamagochiX + 48 >= toyX){
Serial.println("Left");
if (playerMap.toys != nullptr)
{
Serial.println("Left-if");
tamagotchi.play(*(playerMap.toys));
tamagotchiScore();
delete playerMap.toys;
playerMap.toys = nullptr;
}
}
}
else if(toyX < tamagochiX)
{
if(toyX + 24 >= tamagochiX){
Serial.println("Right");
if (playerMap.toys != nullptr)
{
Serial.println("Left-if");
tamagotchi.play(*(playerMap.toys));
tamagotchiScore();
delete playerMap.toys;
playerMap.toys = nullptr;
}
}
}
else
{
Serial.println("same coords");
}
}
void loop()
{
systime = millis();
tamagotchiScore();
// generateRandomToy(1, 150);
if (systime - timestamp >= 50) {
timestamp = systime;
drawSprite(tamagotchi.getDesign(), 48, 52, tamagotchiX, tamagotchiY, tamagotchiDirection);
int button = readButtons();
if (button == 1 || button == 3) {
// Delete sprite at old coords
drawSprite(tamagotchi.getDesign(), 48, 52, tamagotchiX, tamagotchiY, 0, true);
// Increase / decrease coords depending on button pressed
// Draw sprite at new coords
switch (button) {
case RIGHT:
if (tamagotchiX > 271)
{
tamagotchiX = 0;
generateRandomToy();
}
tamagotchiDirection = 1;
tamagotchiX += tamagotchiSpeed;
checkToy(tamagotchiX, xCoord);
//drawSprite(tamagotchi.getDesign(), 48, 52, tamagotchiX, tamagotchiY, 1);
break;
case LEFT:
if (tamagotchiX < 0){
tamagotchiX = 271;
generateRandomToy();
}
tamagotchiDirection = 0;
tamagotchiX -= tamagotchiSpeed;
checkToy(tamagotchiX, xCoord);
//drawSprite(tamagotchi.getDesign(), 48, 52, tamagotchiX, tamagotchiY, 0);
break;
}
}
}
}
/*
drawSprite(arrowSprite, 48, 52, arrowX, 160, 0, true);
drawSprite(dogSprite, 48, 52, 72, 94, 0, true);
drawSprite(catSprite, 48, 52, 200, 94, 0, true);
//delete all sprites
//Start actual game
//Choose Pet name
*/