Snake/src/Core/Crawl/ipreviewscaneworld.cpp

109 lines
2.8 KiB
C++
Raw Normal View History

2021-08-11 00:21:22 +03:00
//#
//# Copyright (C) 2021-2021 QuasarApp.
//# Distributed under the GPLv3 software license, see the accompanying
//# Everyone is permitted to copy and distribute verbatim copies
//# of this license document, but changing it is not allowed.
//#
#include "ipreviewscaneworld.h"
2021-08-12 14:22:59 +03:00
#include "previewcontrol.h"
2021-08-11 00:21:22 +03:00
2021-08-12 14:22:59 +03:00
namespace CRAWL {
2021-08-11 00:21:22 +03:00
2021-08-12 14:22:59 +03:00
IPreviewScaneWorld::IPreviewScaneWorld(const IWorld* mainWorld) {
debug_assert(mainWorld, "The mainWorld world option should be initialized.");
_mainWorld = mainWorld;
2021-08-11 00:21:22 +03:00
}
QString IPreviewScaneWorld::itemTextId() const {
return "preview";
}
QString IPreviewScaneWorld::itemName() const {
return itemTextId();
}
QString IPreviewScaneWorld::description() const {
return "";
}
QString IPreviewScaneWorld::image() const {
return "";
}
int IPreviewScaneWorld::cost() const {
return 0;
}
int IPreviewScaneWorld::requiredTier() const {
return 0;
}
2021-08-12 14:22:59 +03:00
PlayableObject *IPreviewScaneWorld::initPlayer(int objectType) const {
return _mainWorld->initPlayer(objectType);
}
2021-08-11 00:21:22 +03:00
bool IPreviewScaneWorld::start(const StartData& config) {
_configuration = config;
2021-08-12 14:22:59 +03:00
setPlayer(initPlayer(config.snakeType()));
worldChanged(worldRules()->cbegin());
setTargetFps(60);
setRunning(true);
return true;
2021-08-11 00:21:22 +03:00
}
bool IPreviewScaneWorld::stop() {
2021-08-12 14:22:59 +03:00
setRunning(false);
reset();
return true;
}
void IPreviewScaneWorld::handleStop() {
stop();
}
void IPreviewScaneWorld::handleRotation(double x, double y) {
auto normal = (QQuaternion::fromEulerAngles(x, y, 0) * QVector3D{0,0,1}).normalized();
setCameraReleativePosition(normal * 100);
setCameraRotation(QQuaternion::rotationTo({1.0f, 0.0, 0.0}, {0,0,0}));
}
void IPreviewScaneWorld::handleStart() {
auto playerObj = dynamic_cast<IItem*>(player());
_configuration.setSnakePerks(playerObj->activeItems());
emit sigPrepareIsFinished(_configuration);
handleStop();
}
void IPreviewScaneWorld::handleSelect(int item, bool isSelected) {
auto playerItem = dynamic_cast<IItem*>(player());
if (isSelected) {
playerItem->activate(item);
} else {
playerItem->deactivate(item);
}
}
void IPreviewScaneWorld::initControl(IControl *control) {
auto controlObject = dynamic_cast<PreviewControl*>(control);
if (controlObject) {
connect(controlObject, &PreviewControl::backToMenu, this, &IPreviewScaneWorld::handleStop);
connect(controlObject, &PreviewControl::mousePositionChanged, this, &IPreviewScaneWorld::handleRotation);
connect(controlObject, &PreviewControl::start, this, &IPreviewScaneWorld::handleStart);
connect(controlObject, &PreviewControl::select, this, &IPreviewScaneWorld::handleSelect);
}
}
2021-08-11 00:21:22 +03:00
2021-08-12 14:22:59 +03:00
IControl *IPreviewScaneWorld::initUserInterface() const {
return new PreviewControl();
2021-08-11 00:21:22 +03:00
}
}