#include <Wire.h>
#include <SD.h>
#include <LiquidCrystal_I2C.h>
#include <FS.h>
#include <ArduinoJson.h>
//{ Pines
#define velocityY 34
#define button 13
//#define touch 15
#define button2 33
#define snd 16
#define CS 5
#define MOSI 23
#define MISO 19
#define SCK 18
//#define DOUT 27
//#define BCLK 26
//#define LRC 25
//}
//{ Texturas
unsigned char arrow[] =
{
B10000,
B11000,
B11100,
B11110,
B11100,
B11000,
B10000,
B00000
};
unsigned char arrowUp[] = {
B00000,
B00000,
B00000,
B00000,
B00100,
B01110,
B11111,
B11111
};
unsigned char arrowDown[] = {
B11111,
B11111,
B01110,
B00100,
B00000,
B00000,
B00000,
B00000
};
unsigned char player1[] =
{
B01110,
B11011,
B10101,
B01010,
B01101,
B11010,
B10101,
B01110
};
unsigned char player2[] =
{
B00000,
B00000,
B11111,
B00000,
B00000,
B11111,
B00000,
B00000
};
unsigned char player3[] =
{
B00000,
B00000,
B11110,
B01001,
B01001,
B11110,
B00000,
B00000
};
unsigned char interrog[] =
{
B00000,
B00100,
B00000,
B00100,
B01000,
B10000,
B10001,
B01110
};
unsigned char gun[] =
{
B00000,
B00000,
B00000,
B11111,
B11111,
B00000,
B00000,
B00000
};
unsigned char player4[] =
{
B01110,
B11011,
B10101,
B01010,
B10110,
B01011,
B10101,
B01110
};
unsigned char player5[] =
{
B00000,
B00000,
B01111,
B10010,
B10010,
B01111,
B00000,
B00000
};
unsigned char enemy[] =
{
B01000,
B00100,
B00110,
B01110,
B01110,
B11111,
B11111,
B01110
};
unsigned char femi[] = {
B01110,
B10001,
B10001,
B01110,
B00100,
B01110,
B00100,
B00000
};
unsigned char ino[] = {
B01110,
B01010,
B01110,
B01110,
B10101,
B00100,
B01010,
B01010
};
unsigned char opr[] = {
B00000,
B00111,
B00011,
B00101,
B01100,
B10010,
B10010,
B01100
};
unsigned char inv[] = {
B00100,
B01010,
B01110,
B01010,
B01010,
B10001,
B10101,
B01010
};
unsigned char tet[] = {
B00000,
B01010,
B10001,
B10001,
B10101,
B10001,
B01010,
B00000
};
unsigned char nz[] = {
B00000,
B10111,
B10100,
B11111,
B00101,
B11101,
B00000,
B00000
};
unsigned char nz1[] = {
B00000,
B00000,
B00000,
B00000,
B00000,
B11111,
B11111,
B11111
};
unsigned char nz2[] = {
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111
};
unsigned char nz3[] = {
B11111,
B11111,
B11111,
B00000,
B00000,
B00000,
B00000,
B00000
};
//}
//{ Inicialización
LiquidCrystal_I2C LCD(0x27, 20, 4);
JsonDocument doc;
String json;
int option = 0;
int timer;
int timer2;
int positionX = 0;
int positionY = 0;
int useTime = 0;
bool useButton = false;
bool useButton2 = false;
int playerLife = 100;
int joyDelay = 0;
int playerDmg = 5;
int activeCount = 0;
const int maxNPCs = 25;
int npcType[maxNPCs];
int npcDmg[maxNPCs];
int npcDrop[maxNPCs];
int npcLife[maxNPCs];
int npcPositionX[maxNPCs];
int npcPositionY[maxNPCs];
bool npcActive[maxNPCs];
const int maxProjs = 40;
int projDamage[maxProjs];
int projAlt[maxNPCs];
bool projHostile[maxProjs];
int projPositionX[maxProjs];
int projPositionY[maxProjs];
bool projActive[maxProjs];
int score;
int posLead;
int namePos = 0;
char charPos = 65;
char newName[9] = " ";
int mode = 6;
int timerIntro = 0;
int nivel = 1;
bool mode2 = false;
bool pauseGame = false;
//}
void setup()
{
Serial.begin(9600);
pinMode(velocityY, INPUT);
pinMode(button, INPUT);
pinMode(CS, OUTPUT);
digitalWrite(CS, HIGH);
SPI.begin(SCK, MISO, MOSI);
Serial.print("Iniciando SD... ");
while (!SD.begin(CS))
{
}
doc["name"][0] = "Vacio";
doc["score"][0] = 0;
doc["name"][1] = "Vacio";
doc["score"][1] = 0;
doc["name"][2] = "Vacio";
doc["score"][2] = 0;
doc["name"][3] = "Vacio";
doc["score"][3] = 0;
doc["money"] = 0;
doc["lvHp"] = 0;
doc["lvDmg"] = 0;
Serial.println("Terminado");
deserializeJson(doc, readFile());
LCD.init();
LCD.backlight();
}
void loop()
{
if (analogRead(button) <= 256)
{
useButton = false;
noTone(snd);
}
else
{
tone(snd, 125);
}
if (mode == 6)
{
if (timerIntro == 0)
{
LCD.setCursor(8, 1);
LCD.print(F("Por:"));
LCD.setCursor(2, 2);
LCD.print(F("Carlos y Rodrigo"));
}
else if (timerIntro == 2500)
{
LCD.clear();
}
else if (timerIntro > 2500 && timerIntro < 19500)
{
if (timerIntro == 3500)
{
LCD.setCursor(0, 0);
LCD.print(F("En el siglo XXI, los"));
}
else if (timerIntro == 4500)
{
LCD.setCursor(0, 1);
LCD.print(F("mostruos sexuales"));
}
else if (timerIntro == 5500)
{
LCD.setCursor(0, 2);
LCD.print(F("emergieron para hun-"));
}
else if (timerIntro == 6500)
{
LCD.setCursor(0, 3);
LCD.print(F("dir al mundo en caos"));
}
else if (timerIntro == 11000)
{
LCD.clear();
}
else if (timerIntro == 12000)
{
LCD.setCursor(0, 0);
LCD.print(F("Pero, de un lugar"));
}
else if (timerIntro == 13000)
{
LCD.setCursor(0, 1);
LCD.print(F("remoto, una polla se"));
}
else if (timerIntro == 14000)
{
LCD.setCursor(0, 2);
LCD.print(F("levantara para traer"));
}
else if (timerIntro == 15000)
{
LCD.setCursor(0, 3);
LCD.print(F("la paz de nuevo"));
}
}
else if (timerIntro > 2500 && timerIntro < 24500)
{
if (timerIntro == 19500)
{
LCD.clear();
}
LCD.createChar(0, player1);
LCD.createChar(1, player2);
LCD.createChar(2, player3);
LCD.createChar(3, player4);
LCD.createChar(4, player5);
for (int i = 0; i < 4; i++)
{
if (i < 2)
{
LCD.setCursor(i * 5, 0);
LCD.write(0);
LCD.setCursor(i * 5 + 1, 0);
LCD.write(1);
LCD.setCursor(i * 5 + 2, 0);
LCD.write(2);
LCD.setCursor(i * 5, 3);
LCD.write(0);
LCD.setCursor(i * 5 + 1, 3);
LCD.write(1);
LCD.setCursor(i * 5 + 2, 3);
LCD.write(2);
}
else
{
LCD.setCursor(i * 5 + 2, 0);
LCD.write(4);
LCD.setCursor(i * 5 + 3, 0);
LCD.write(1);
LCD.setCursor(i * 5 + 4, 0);
LCD.write(3);
LCD.setCursor(i * 5 + 2, 3);
LCD.write(4);
LCD.setCursor(i * 5 + 3, 3);
LCD.write(1);
LCD.setCursor(i * 5 + 4, 3);
LCD.write(3);
}
}
LCD.setCursor(8, 1);
LCD.print(F("SEXO"));
LCD.setCursor(6, 2);
LCD.print(F("el juego"));
}
timerIntro++;
delay(1);
if (analogRead(button) > 256 && !useButton)
{
LCD.clear();
LCD.createChar(0, arrow);
LCD.setCursor(0, 0);
LCD.write(0);
mode = 0;
useButton = true;
}
}
else if (mode == 0)
{
LCD.setCursor(1, 0);
LCD.print(F("Jugar"));
LCD.setCursor(1, 1);
LCD.print(F("Mejoras"));
LCD.setCursor(1, 2);
LCD.print(F("Clasificacion"));
LCD.setCursor(1, 3);
LCD.print(F("Reiniciar"));
if (joyDelay <= 0)
{
if (analogRead(velocityY) >= 3072 && option > 0)
{
LCD.setCursor(0, option);
LCD.print(' ');
option--;
LCD.setCursor(0, option);
LCD.write(0);
joyDelay = 4;
}
else if (analogRead(velocityY) < 1024 && option < 3)
{
LCD.setCursor(0, option);
LCD.print(' ');
option++;
LCD.setCursor(0, option);
LCD.write(0);
joyDelay = 4;
}
}
else if (analogRead(velocityY) < 768 && analogRead(velocityY) >= 256)
{
joyDelay = 0;
}
else
{
joyDelay--;
}
if (analogRead(button) > 256 && !useButton)
{
if (option == 0)
{
mode = 7;
LCD.clear();
LCD.createChar(0, arrow);
LCD.setCursor(0, 0);
LCD.write(0);
}
else if (option == 1)
{
LCD.clear();
LCD.setCursor(0, 0);
LCD.write(0);
option = 0;
mode = 8;
}
else if (option == 2)
{
LCD.clear();
mode = 4;
}
else if (option == 3)
{
LCD.clear();
LCD.createChar(0, arrow);
LCD.createChar(1, interrog);
LCD.setCursor(0, 3);
LCD.write(0);
option = 0;
mode = 5;
}
useButton = true;
}
}
else if (mode == 7)
{
LCD.setCursor(1, 0);
LCD.print(F("Modo normal"));
LCD.setCursor(1, 1);
LCD.print(F("Modo infinito"));
LCD.setCursor(1, 3);
LCD.print(F("Volver"));
if (joyDelay <= 0)
{
if (analogRead(velocityY) < 1024 && option == 0) //768
{
LCD.setCursor(0, 0);
LCD.print(' ');
option++;
LCD.setCursor(0, 1);
LCD.write(0);
joyDelay = 4;
}
else if (analogRead(velocityY) < 1024 && option == 1) //256
{
LCD.setCursor(0, 1);
LCD.print(' ');
option++;
LCD.setCursor(0, 3);
LCD.write(0);
joyDelay = 4;
}
else if (analogRead(velocityY) >= 3072 && option == 1) //256
{
LCD.setCursor(0, 1);
LCD.print(' ');
option--;
LCD.setCursor(0, 0);
LCD.write(0);
joyDelay = 4;
}
else if (analogRead(velocityY) >= 3072 && option == 2) //256
{
LCD.setCursor(0, 3);
LCD.print(' ');
option--;
LCD.setCursor(0, 1);
LCD.write(0);
joyDelay = 4;
}
}
else if (analogRead(velocityY) < 768 && analogRead(velocityY) >= 256)
{
joyDelay = 0;
}
else
{
joyDelay--;
}
if (analogRead(button) > 256 && !useButton)
{
if (option == 0)
{
mode2 = false;
StartGame(mode2);
}
else if (option == 1)
{
mode2 = true;
StartGame(mode2);
}
else if (option == 2)
{
option = 0;
mode = 0;
LCD.clear();
LCD.createChar(0, arrow);
LCD.setCursor(0, 0);
LCD.write(0);
}
useButton = true;
}
}
else if (mode == 1)
{
Serial.println(analogRead(button2));
if (analogRead(button2) <= 256)
{
useButton2 = false;
}
if (analogRead(button2) > 256 && !useButton2)
{
useButton2 = true;
pauseGame = !pauseGame;
}
if (pauseGame)
{
return;
}
activeCount = 0;
if (timer % 6 == 0)
{
LCD.setCursor(15, 0);
LCD.print(score);
}
for (int i = 0; i < maxProjs; i++)
{
if (projActive[i])
{
UpdateProj(i);
}
}
for (int i = 0; i < maxNPCs; i++)
{
if (npcActive[i])
{
UpdateNPC(i);
}
if (npcActive[i])
{
activeCount++;
}
}
if (joyDelay <= 0)
{
if (analogRead(velocityY) >= 3072 && positionY > 0) //768
{
LCD.setCursor(0, positionY);
LCD.print(F(" "));
positionY--;
LCD.setCursor(0, positionY);
LCD.write(0);
LCD.setCursor(1, positionY);
LCD.write(1);
LCD.setCursor(2, positionY);
LCD.write(2);
joyDelay = 5;
}
else if (analogRead(velocityY) < 1024 && positionY < 3) //256
{
LCD.setCursor(0, positionY);
LCD.print(F(" "));
positionY++;
LCD.setCursor(0, positionY);
LCD.write(0);
LCD.setCursor(1, positionY);
LCD.write(1);
LCD.setCursor(2, positionY);
LCD.write(2);
joyDelay = 5;
}
}
else if (analogRead(velocityY) < 768 && analogRead(velocityY) >= 256)
{
joyDelay = 0;
}
else
{
joyDelay--;
}
useTime--;
if (analogRead(button) > 256 && useTime <= 0 && !useButton)
{
NewProj(playerDmg, false, 3, positionY);
useTime = 4;
useButton = true;
}
if (nivel == 1)
{
if (timer2 <= 450)
{
if (timer2 % 15 == 0)
{
NewNPC(1);
}
timer2++;
}
else if (activeCount == 0)
{
LCD.createChar(5, femi);
nivel++;
}
}
else if (nivel == 2)
{
if (timer2 <= 1200)
{
if (timer2 % 150 == 130)
{
NewNPC(2);
}
else if (timer2 % 30 == 10)
{
NewNPC(1);
}
timer2++;
}
else if (activeCount == 0)
{
LCD.createChar(6, ino);
nivel++;
}
}
else if (nivel == 3)
{
if (timer2 <= 1950)
{
if (timer2 % 250 == 235)
{
NewNPC(3);
}
else if (timer2 % 75 == 60 || timer2 % 175 == 160)
{
NewNPC(2);
}
else if (timer2 % 25 == 10)
{
NewNPC(1);
}
timer2++;
}
else if (activeCount == 0)
{
LCD.createChar(4, inv);
LCD.createChar(7, opr);
nivel++;
}
}
else if (nivel == 4)
{
if (timer2 <= 2750)
{
if (timer2 % 195 == 175)
{
NewNPC(3);
}
else if (timer2 % 150 == 130)
{
NewNPC(5);
}
else if (timer2 % 180 == 160)
{
NewNPC(4);
}
else if (timer2 % 30 == 10)
{
NewNPC(2);
}
timer2++;
}
else if (activeCount == 0)
{
LCD.createChar(5, tet);
nivel++;
}
}
else if (nivel == 5)
{
if (timer2 <= 3550)
{
if (timer2 % 200 == 180)
{
NewNPC(6);
}
else if (timer2 % 80 == 40)
{
NewNPC(4);
}
else if (timer2 % 60 == 30)
{
NewNPC(5);
}
timer2++;
}
else if (activeCount == 0)
{
LCD.createChar(6, nz);
nivel++;
}
}
else if (nivel == 6)
{
if (timer2 <= 4550)
{
if (timer2 % 250 == 240)
{
NewNPC(8);
}
else if (timer2 % 100 == 50)
{
NewNPC(6);
}
else if (timer2 % 50 == 30)
{
NewNPC(4);
}
timer2++;
}
else if (activeCount == 0)
{
nivel++;
}
}
else if (nivel == 7)
{
if (timer2 <= 5250)
{
if (timer2 % 100 == 50)
{
NewNPC(8);
}
timer2++;
}
else if (activeCount == 0)
{
LCD.createChar(4, nz1);
LCD.createChar(5, nz2);
LCD.createChar(6, nz3);
NewNPC(9);
nivel++;
}
}
else if (nivel == 8)
{
if (activeCount == 0)
{
if (timer2 < 5250)
{
timer2 = 5250;
}
if (timer2 <= 5400)
{
LCD.setCursor(7, 1);
LCD.print(F("Modo infinito"));
timer2++;
}
else
{
LCD.setCursor(7, 1);
LCD.print(F(" "));
LCD.createChar(4, enemy);
LCD.createChar(5, tet);
LCD.createChar(6, nz);
LCD.createChar(7, opr);
nivel++;
}
}
}
else
{
if (timer % 120 == 3 && random(10) == 0)
{
NewNPC(8);
}
if (timer % 70 == 2 && random(5) == 0)
{
NewNPC(6);
}
if (timer % 40 == 1 && random(3) == 0)
{
NewNPC(5);
}
if (timer % 15 == 0 && random(2) == 0)
{
NewNPC(1);
}
}
if (playerLife <= 0)
{
EndGame();
}
timer++;
if (timer % 6 == 0)
{
score++;
}
delay(33);
}
else if (mode == 2)
{
LCD.setCursor(1, 0);
LCD.print(F("Has morido"));
LCD.setCursor(1, 1);
String caca = "Tu puntuacion: " + (String)score;
LCD.print(caca);
LCD.setCursor(1, 3);
LCD.print(F("Reintentar"));
LCD.setCursor(14, 3);
LCD.print(F("Volver"));
if (analogRead(velocityY) >= 3072 && option == 0) //768
{
LCD.setCursor(0, 3);
LCD.print(' ');
option++;
LCD.setCursor(13, 3);
LCD.write(0);
}
else if (analogRead(velocityY) < 1024 && option == 1) //256
{
LCD.setCursor(13, 3);
LCD.print(' ');
option--;
LCD.setCursor(0, 3);
LCD.write(0);
}
if (analogRead(button) > 256 && !useButton)
{
if (option == 0)
{
StartGame(mode2);
}
else
{
LCD.clear();
LCD.write(0);
option = 0;
mode = 0;
}
score = 0;
useButton = true;
}
}
else if (mode == 3)
{
LCD.setCursor(1, 0);
LCD.print(F("Introduce tu nombre"));
for (int i = 0; i < 8; i++)
{
LCD.setCursor(i + 1, 1);
LCD.write(0);
LCD.setCursor(i + 1, 3);
LCD.write(1);
}
if (joyDelay <= 0)
{
if (analogRead(velocityY) >= 3072)
{
charPos++;
if (charPos > 255)
{
charPos = 33;
}
joyDelay = 1;
}
else if (analogRead(velocityY) < 1024)
{
charPos--;
if (charPos < 33)
{
charPos = 255;
}
joyDelay = 1;
}
}
else if (analogRead(velocityY) < 768 && analogRead(velocityY) >= 256)
{
joyDelay = 0;
}
else
{
joyDelay--;
}
LCD.setCursor(namePos + 1, 2);
LCD.print((char)charPos);
if (analogRead(button) > 256 && !useButton)
{
newName[namePos] = charPos;
namePos++;
useButton = true;
}
if (namePos >= 8)
{
for (int j = 3; j > posLead; j--)
{
doc["name"][j] = doc["name"][j - 1];
doc["score"][j] = doc["score"][j - 1];
}
doc["name"][posLead] = newName;
doc["score"][posLead] = score;
serializeJson(doc, json);
writeFile(json);
namePos = 0;
charPos = 65;
for (int i = 0; i < 9; i++)
{
newName[i] = 0;
}
LCD.clear();
LCD.createChar(0, arrow);
LCD.setCursor(0, 3);
LCD.write(0);
mode = 2;
}
}
else if (mode == 8)
{
int costHp = 1500 + 375 * (int)doc["lvHp"];
int costDmg = 1500 + 375 * (int)doc["lvDmg"];
LCD.setCursor(1, 0);
String sld = "Salud " + (String)doc["lvHp"];
LCD.print(sld);
LCD.setCursor(15, 0);
LCD.print("$" + (String)costHp);
LCD.setCursor(1, 1);
LCD.print("Da");
LCD.setCursor(3, 1);
LCD.write(238);
LCD.setCursor(4, 1);
String dn = "o " + (String)doc["lvDmg"];
LCD.print(dn);
LCD.setCursor(15, 1);
LCD.print("$" + (String)costDmg);
LCD.setCursor(1, 3);
LCD.print("Volver");
String mn = "$" + (String)doc["money"];
LCD.setCursor(14, 3);
LCD.print(mn);
if (joyDelay <= 0)
{
if (analogRead(velocityY) < 1024 && option == 0) //768
{
LCD.setCursor(0, 0);
LCD.print(' ');
option++;
LCD.setCursor(0, 1);
LCD.write(0);
joyDelay = 4;
}
else if (analogRead(velocityY) < 1024 && option == 1) //256
{
LCD.setCursor(0, 1);
LCD.print(' ');
option++;
LCD.setCursor(0, 3);
LCD.write(0);
joyDelay = 4;
}
else if (analogRead(velocityY) >= 3072 && option == 1) //256
{
LCD.setCursor(0, 1);
LCD.print(' ');
option--;
LCD.setCursor(0, 0);
LCD.write(0);
joyDelay = 4;
}
else if (analogRead(velocityY) >= 3072 && option == 2) //256
{
LCD.setCursor(0, 3);
LCD.print(' ');
option--;
LCD.setCursor(0, 1);
LCD.write(0);
joyDelay = 4;
}
}
else if (analogRead(velocityY) < 768 && analogRead(velocityY) >= 256)
{
joyDelay = 0;
}
else
{
joyDelay--;
}
if (analogRead(button) > 256 && !useButton)
{
if (option == 0 && doc["lvHp"] < 5 && doc["money"] > costHp)
{
doc["money"] = (int)doc["money"] - costHp;
doc["lvHp"] = (int)doc["lvHp"] + 1;
serializeJson(doc, json);
writeFile(json);
LCD.setCursor(15, 3);
LCD.print(" ");
}
else if (option == 1 && doc["lvDmg"] < 5 && doc["money"] > costDmg)
{
doc["money"] = (int)doc["money"] - costDmg;
doc["lvDmg"] = (int)doc["lvDmg"] + 1;
serializeJson(doc, json);
writeFile(json);
LCD.setCursor(15, 3);
LCD.print(" ");
}
else if (option == 2)
{
option = 0;
mode = 0;
LCD.clear();
LCD.createChar(0, arrow);
LCD.setCursor(0, 0);
LCD.write(0);
}
useButton = true;
}
}
else if (mode == 4)
{
for (int i = 0; i < 4; i++)
{
String nm = doc["name"][i];
int sc = doc["score"][i];
LCD.setCursor(1, i);
LCD.print(nm);
LCD.setCursor(15, i);
LCD.print(sc);
}
if (analogRead(button) > 256 && !useButton)
{
option = 0;
mode = 0;
LCD.clear();
LCD.createChar(0, arrow);
LCD.setCursor(0, 0);
LCD.write(0);
useButton = true;
}
}
else if (mode == 5)
{
LCD.setCursor(3, 1);
LCD.write(1);
LCD.setCursor(4, 1);
LCD.print(F("Estas seguro?"));
LCD.setCursor(1, 3);
LCD.print(F("No"));
LCD.setCursor(18, 3);
LCD.print(F("Si"));
if (analogRead(velocityY) >= 3072 && option == 0) //768
{
LCD.setCursor(0, 3);
LCD.print(' ');
option++;
LCD.setCursor(17, 3);
LCD.write(0);
}
else if (analogRead(velocityY) < 1024 && option == 1) //256
{
LCD.setCursor(17, 3);
LCD.print(' ');
option--;
LCD.setCursor(0, 3);
LCD.write(0);
}
if (analogRead(button) > 256 && !useButton)
{
if (option == 1)
{
doc["name"][0] = "Vacio";
doc["score"][0] = 0;
doc["name"][1] = "Vacio";
doc["score"][1] = 0;
doc["name"][2] = "Vacio";
doc["score"][2] = 0;
doc["name"][3] = "Vacio";
doc["score"][3] = 0;
doc["money"] = 0;
doc["lvHp"] = 0;
doc["lvDmg"] = 0;
serializeJson(doc, json);
writeFile(json);
}
LCD.clear();
LCD.createChar(0, arrow);
LCD.setCursor(0, 0);
LCD.write(0);
option = 0;
mode = 0;
}
}
}
void StartGame(bool flag)
{
LCD.clear();
LCD.createChar(0, player1);
LCD.createChar(1, player2);
LCD.createChar(2, player3);
LCD.createChar(3, gun);
LCD.createChar(4, enemy);
positionY = 0;
LCD.setCursor(0, positionY);
LCD.write(0);
LCD.setCursor(1, positionY);
LCD.write(1);
LCD.setCursor(2, positionY);
LCD.write(2);
playerLife = 100 + 20 * (int)doc["lvHp"];
playerDmg = 5 + 1 * (int)doc["lvDmg"];
timer = 0;
timer2 = 0;
if (mode2)
{
LCD.createChar(5, tet);
LCD.createChar(6, nz);
LCD.createChar(7, opr);
nivel = 8;
}
else
{
nivel = 1;
}
mode = 1;
}
void UpdateProj(int i)
{
if (!projHostile[i])
{
for (int j = 0; j < maxNPCs; j++)
{
if (npcActive[j])
{
bool flag = (projPositionX[i] == npcPositionX[j] || projPositionX[i] - 1 == npcPositionX[j]) && projPositionY[i] == npcPositionY[j];
if (npcType[j] == 9)
{
flag = projPositionX[i] >= 15;
}
if (flag)
{
projActive[i] = false;
npcLife[j] -= projDamage[i];
if (npcLife[j] <= 0)
{
score += npcDrop[j];
}
LCD.setCursor(npcPositionX[j], npcPositionY[j]);
LCD.print(" ");
return;
}
}
}
LCD.setCursor(projPositionX[i], projPositionY[i]);
LCD.print(' ');
projPositionX[i]++;
if (projPositionX[i] >= 20)
{
projActive[i] = false;
}
else
{
LCD.setCursor(projPositionX[i], projPositionY[i]);
LCD.write(3);
}
}
else
{
LCD.setCursor(projPositionX[i], projPositionY[i]);
LCD.print(' ');
if (timer % 2 == 0)
{
projPositionX[i]--;
}
if (projPositionX[i] < 0)
{
projActive[i] = false;
}
else if (projPositionX[i] < 3 && projPositionY[i] == positionY)
{
projActive[i] = false;
playerLife -= projDamage[i];
}
else
{
LCD.setCursor(projPositionX[i], projPositionY[i]);
LCD.write(3);
}
}
}
void UpdateNPC(int i)
{
if (npcType[i] == 1)
{
if (timer % 3 == 0)
{
LCD.setCursor(npcPositionX[i], npcPositionY[i]);
LCD.print(' ');
npcPositionX[i]--;
if (!CheckDead(i))
{
LCD.setCursor(npcPositionX[i], npcPositionY[i]);
LCD.write(4);
}
}
}
else if (npcType[i] == 2)
{
if (timer % 4 == 0)
{
LCD.setCursor(npcPositionX[i], npcPositionY[i]);
LCD.print(' ');
npcPositionX[i]--;
if (!CheckDead(i))
{
LCD.setCursor(npcPositionX[i], npcPositionY[i]);
LCD.write(5);
}
}
}
else if (npcType[i] == 3)
{
if (timer % 3 == 0)
{
LCD.setCursor(npcPositionX[i], npcPositionY[i]);
LCD.print(' ');
npcPositionX[i]--;
if (!CheckDead(i))
{
LCD.setCursor(npcPositionX[i], npcPositionY[i]);
LCD.write(6);
}
}
}
else if (npcType[i] == 4)
{
if (timer % 5 == 0)
{
LCD.setCursor(npcPositionX[i], npcPositionY[i]);
LCD.print(' ');
npcPositionX[i]--;
if (!CheckDead(i))
{
LCD.setCursor(npcPositionX[i], npcPositionY[i]);
LCD.write(4);
}
}
}
else if (npcType[i] == 5)
{
if (timer % 11 == 0)
{
LCD.setCursor(npcPositionX[i], npcPositionY[i]);
LCD.print(' ');
npcPositionX[i]--;
if (!CheckDead(i))
{
LCD.setCursor(npcPositionX[i], npcPositionY[i]);
LCD.write(7);
}
}
if (timer % 105 == 0)
{
NewProj(13, true, npcPositionX[i] - 1, npcPositionY[i]);
}
}
else if (npcType[i] == 6)
{
if (timer % 4 == 0)
{
LCD.setCursor(npcPositionX[i], npcPositionY[i]);
LCD.print(' ');
npcPositionX[i]--;
if (!CheckDead(i))
{
LCD.setCursor(npcPositionX[i], npcPositionY[i]);
LCD.write(5);
}
}
}
else if (npcType[i] == 7)
{
if (timer % 3 == 0)
{
LCD.setCursor(npcPositionX[i], npcPositionY[i]);
LCD.print(' ');
npcPositionX[i]--;
if (!CheckDead(i))
{
LCD.setCursor(npcPositionX[i], npcPositionY[i]);
LCD.write(projAlt[i]);
}
}
}
else if (npcType[i] == 8)
{
if (timer % 20 == 0)
{
LCD.setCursor(npcPositionX[i], npcPositionY[i]);
LCD.print(' ');
switch (random(5))
{
case 0:
npcPositionX[i]--;
break;
case 1:
if (npcPositionX[i] < 19)
{
npcPositionX[i]++;
}
break;
case 2:
if (npcPositionY[i] > 0)
{
npcPositionY[i]--;
}
break;
case 3:
if (npcPositionY[i] < 3)
{
npcPositionY[i]++;
}
break;
default:
break;
}
npcPositionX[i]--;
if (!CheckDead(i))
{
LCD.setCursor(npcPositionX[i], npcPositionY[i]);
LCD.write(6);
}
}
if (timer % 75 == 0)
{
NewProj(20, true, npcPositionX[i] - 1, npcPositionY[i]);
}
}
else if (npcType[i] == 9)
{
if (timer % 3 == 0)
{
if (!CheckDead(i))
{
LCD.setCursor(15, 0);
LCD.write(4);
LCD.setCursor(17, 0);
LCD.write(4);
LCD.setCursor(18, 0);
LCD.write(4);
LCD.setCursor(19, 0);
LCD.write(4);
LCD.setCursor(15, 1);
LCD.write(5);
LCD.setCursor(16, 1);
LCD.write(4);
LCD.setCursor(17, 1);
LCD.write(5);
LCD.setCursor(18, 1);
LCD.write(4);
LCD.setCursor(19, 1);
LCD.write(4);
LCD.setCursor(15, 2);
LCD.write(6);
LCD.setCursor(16, 2);
LCD.write(6);
LCD.setCursor(17, 2);
LCD.write(5);
LCD.setCursor(18, 2);
LCD.write(6);
LCD.setCursor(19, 2);
LCD.write(5);
LCD.setCursor(15, 3);
LCD.write(6);
LCD.setCursor(16, 3);
LCD.write(6);
LCD.setCursor(17, 3);
LCD.write(6);
LCD.setCursor(19, 3);
LCD.write(6);
}
else
{
LCD.setCursor(15, 0);
LCD.print(" ");
LCD.setCursor(15, 1);
LCD.print(" ");
LCD.setCursor(15, 2);
LCD.print(" ");
LCD.setCursor(15, 3);
LCD.print(" ");
}
}
if (timer % 50 == 0)
{
for (int i = 0; i < 4; i++)
{
if (random(3) == 0)
{
NewProj(20, true, 14, i);
}
}
}
if (timer % 175 >= 125 && timer % 5 == 0)
{
NewNPC(7);
}
}
}
bool CheckDead(int i)
{
if (npcPositionX[i] < 0 || npcLife[i] <= 0)
{
npcActive[i] = false;
return true;
}
else if (npcActive[i] && npcPositionX[i] < 3 && npcPositionY[i] == positionY)
{
npcActive[i] = false;
playerLife -= npcDmg[i];
return true;
}
return false;
}
void EndGame()
{
for (int i = 0; i < maxProjs; i++)
{
if (projActive[i])
{
projPositionX[i] = 0;
projActive[i] = false;
}
}
for (int i = 0; i < maxNPCs; i++)
{
if (npcActive[i])
{
npcPositionX[i] = 19;
npcLife[i] = 0;
npcActive[i] = false;
}
}
doc["money"] = (int)doc["money"] + score;
serializeJson(doc, json);
writeFile(json);
option = 0;
Serial.println((String)score);
for (int i = 0; i < 4; i++)
{
if (score > (int)doc["score"][i])
{
posLead = i;
LCD.clear();
LCD.createChar(0, arrowUp);
LCD.createChar(1, arrowDown);
mode = 3;
return;
}
}
LCD.clear();
LCD.createChar(0, arrow);
LCD.setCursor(0, 3);
LCD.write(0);
timer = 0;
timer2 = 0;
mode = 2;
}
void NewProj(int dmg, bool flag, int posX, int posY)
{
for (int i = 0; i < maxProjs; i++)
{
if (!projActive[i])
{
projPositionY[i] = posY;
projPositionX[i] = posX;
LCD.setCursor(projPositionX[i], projPositionY[i]);
LCD.write(3);
projActive[i] = true;
projDamage[i] = dmg;
projHostile[i] = flag;
break;
}
}
}
void NewNPC2(int j)
{
for (int i = 0; i < maxNPCs; i++)
{
if (!npcActive[i])
{
npcType[i] = 6;
npcLife[i] = 25;
npcDrop[i] = 26;
npcPositionX[i] = 19;
npcPositionY[i] = j;
LCD.setCursor(npcPositionX[i], npcPositionY[i]);
LCD.write(5);
npcDmg[i] = 35;
npcActive[i] = true;
break;
}
}
}
void NewNPC(int type)
{
for (int i = 0; i < maxNPCs; i++)
{
if (!npcActive[i])
{
npcType[i] = type;
if (type == 1)
{
npcLife[i] = 10;
npcDrop[i] = 4;
npcPositionX[i] = 19;
npcPositionY[i] = random(4);
LCD.setCursor(npcPositionX[i], npcPositionY[i]);
LCD.write(4);
npcDmg[i] = 15;
}
else if (type == 2)
{
npcLife[i] = 25;
npcDrop[i] = 9;
npcPositionX[i] = 19;
npcPositionY[i] = random(4);
LCD.setCursor(npcPositionX[i], npcPositionY[i]);
LCD.write(5);
npcDmg[i] = 10;
}
else if (type == 3)
{
npcLife[i] = 1;
npcDrop[i] = -40;
npcPositionX[i] = 19;
npcPositionY[i] = random(4);
LCD.setCursor(npcPositionX[i], npcPositionY[i]);
LCD.write(6);
npcDmg[i] = 0;
}
else if (type == 4)
{
npcLife[i] = 45;
npcDrop[i] = 34;
npcPositionX[i] = 19;
npcPositionY[i] = random(4);
LCD.setCursor(npcPositionX[i], npcPositionY[i]);
LCD.write(4);
npcDmg[i] = 30;
}
else if (type == 5)
{
npcLife[i] = 35;
npcDrop[i] = 30;
npcPositionX[i] = 19;
npcPositionY[i] = random(4);
LCD.setCursor(npcPositionX[i], npcPositionY[i]);
LCD.write(7);
npcDmg[i] = 25;
}
else if (type == 6)
{
npcLife[i] = 25;
npcDrop[i] = 26;
npcPositionX[i] = 18;
npcPositionY[i] = random(4);
LCD.setCursor(npcPositionX[i], npcPositionY[i]);
LCD.write(5);
npcDmg[i] = 35;
}
else if (type == 7)
{
projAlt[i] = 166 + random(56);
npcLife[i] = 20;
npcDrop[i] = 0;
npcPositionX[i] = 19;
npcPositionY[i] = random(4);
LCD.setCursor(npcPositionX[i], npcPositionY[i]);
LCD.write(projAlt[i]);
npcDmg[i] = 15;
}
else if (type == 8)
{
npcLife[i] = 75;
npcDrop[i] = 56;
npcPositionX[i] = 19;
npcPositionY[i] = random(4);
LCD.setCursor(npcPositionX[i], npcPositionY[i]);
LCD.write(6);
npcDmg[i] = 30;
}
else if (type == 9)
{
npcLife[i] = 2250;
npcDrop[i] = 2150;
LCD.setCursor(15, 0);
LCD.write(4);
LCD.setCursor(17, 0);
LCD.write(4);
LCD.setCursor(18, 0);
LCD.write(4);
LCD.setCursor(19, 0);
LCD.write(4);
LCD.setCursor(15, 1);
LCD.write(5);
LCD.setCursor(16, 1);
LCD.write(4);
LCD.setCursor(17, 1);
LCD.write(5);
LCD.setCursor(18, 1);
LCD.write(4);
LCD.setCursor(19, 1);
LCD.write(4);
LCD.setCursor(15, 2);
LCD.write(6);
LCD.setCursor(16, 2);
LCD.write(6);
LCD.setCursor(17, 2);
LCD.write(5);
LCD.setCursor(18, 2);
LCD.write(6);
LCD.setCursor(19, 2);
LCD.write(5);
LCD.setCursor(15, 3);
LCD.write(6);
LCD.setCursor(16, 3);
LCD.write(6);
LCD.setCursor(17, 3);
LCD.write(6);
LCD.setCursor(19, 3);
LCD.write(6);
npcDmg[i] = 100;
}
npcActive[i] = true;
if (type == 6)
{
NewNPC2(npcPositionY[i]);
}
break;
}
}
}
String readFile()
{
String text;
File textFile = SD.open("/prieto.txt");
if (textFile)
{
while (textFile.available())
{
text += (char)textFile.read();
}
textFile.close();
}
else
{
serializeJson(doc, text);
writeFile(text);
}
Serial.println(text);
return text;
}
void writeFile(String text)
{
File textFile = SD.open("/prieto.txt", FILE_WRITE);
if (textFile)
{
textFile.print(text);
textFile.close();
Serial.println(readFile());
}
else
{
Serial.println("Caca");
}
}