#include <QPainter>
#include <QKeyEvent>
#include "TestVis.h"
TestVis::TestVis()
{
m_gameSim = new GameSim();
m_ownSim = true;
m_running = true;
startTimer(100);
}
TestVis::TestVis(GameSim* a_gameSim)
{
m_gameSim = a_gameSim;
m_ownSim = false;
m_running = true;
startTimer(100);
}
TestVis::~TestVis()
{
if (m_ownSim)
delete m_gameSim;
}
void TestVis::timerEvent(QTimerEvent*)
{
if (m_running)
{
m_gameSim->update(1);
if (m_gameSim->isGameOver())
{
m_running = false;
}
}
update();
}
void TestVis::keyPressEvent(QKeyEvent* a_ke)
{
if (a_ke->key() == Qt::Key_Down)
m_gameSim->movePlayer(Forward);
if (a_ke->key() == Qt::Key_Up)
m_gameSim->movePlayer(Backward);
if (a_ke->key() == Qt::Key_Left)
m_gameSim->movePlayer(Left);
if (a_ke->key() == Qt::Key_Right)
m_gameSim->movePlayer(Right);
}
void TestVis::mousePressEvent(QMouseEvent*)
{
m_gameSim->movePlayer(Forward);
if (!m_running)
{
m_gameSim->reset();
m_running = true;
}
}
void TestVis::paintEvent(QPaintEvent*)
{
QPainter p(this);
std::vector<GameObjectList*> objLists = m_gameSim->objectLists();
for (GameObjectList* objList : objLists)
{
for (const GameObject& obj : *objList)
{
p.setPen(QColor(QRgb(0)));
uint32_t color = obj.m_color;
if (obj.m_type == Ground) color = 0x47bd37;
if (obj.m_type == Road) color = 0x6a6963;
if (obj.m_type == Rail) color = 0x464441;
if (obj.m_type == Water) color = 0x3a31f5;
if (obj.m_type == WaterLane)color = 0x3a31f5;
if (obj.m_type == Tree) color = 0x508c23;
if (obj.m_type == Shrub) color = 0x508c23;
if (obj.m_type == Building) color = 0xd38b37;
if (obj.m_type == Rocks) color = 0x5c4932;
if (obj.m_type == Bike) color = 0xdf1f24;
if (obj.m_type == Car) color = 0xf52542;
if (obj.m_type == Van) color = 0x3a4b88;
if (obj.m_type == Truck) color = 0xdfd41f;
if (obj.m_type == Train1) color = 0x8bbb38;
if (obj.m_type == Train2) color = 0xa2d051;
if (obj.m_type == Train3) color = 0x6ca50a;
if (obj.m_type == Train4) color = 0x0a53a5;
if (obj.m_type == Log) color = 0x6b5331;
if (obj.m_type == Boat) color = 0xd5d5d5;
if (obj.m_type == Crocodile)color = 0x597e3e;
if (obj.m_type == LilyPad) color = 0x71df1f;
if (obj.m_type == SteppingStone) color = 0x71df1f;
/*
// Pickup
Coin,
Food,
Life,
Bonus
*/
if (obj.m_type == Player) color = 0xffffff;
p.setBrush(QColor(QRgb(color)));
p.drawRect(obj.m_x, obj.m_y, obj.m_w, obj.m_h);
}
}
if (!m_running)
{
p.setPen(QColor(QRgb(0x2fd6d8)));
QFont f = p.font();
f.setPointSize(24);
p.setFont(f);
if (m_gameSim->hasWon())
{
p.drawText(270, 210, "You Won!");
}
else
{
p.drawText(270, 210, "You Lost!");
}
p.drawText(270, 250, "Game Over");
p.drawText(240, 290, "Click to Restart");
}
}