mirror of
https://github.com/QuasarApp/Snake.git
synced 2025-04-26 09:44:40 +00:00
ref #63 snake object are done
This commit is contained in:
parent
04ceb5fc13
commit
8ccd31cbef
@ -35,6 +35,11 @@ IControl *IWorld::initUserInterface() const {
|
||||
|
||||
void IWorld::render(unsigned int tbfMsec) {
|
||||
|
||||
if (!_running) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
|
||||
return;
|
||||
}
|
||||
|
||||
_ItemsMutex.lock();
|
||||
|
||||
for (auto i = _items.begin(); i != _items.end(); ++i) {
|
||||
@ -77,6 +82,8 @@ bool IWorld::start() {
|
||||
|
||||
worldChanged(*_worldRules->begin());
|
||||
setTargetFps(60);
|
||||
setRunning(true);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -109,15 +116,7 @@ IWorldItem *IWorld::generate(const QString &objectType) const {
|
||||
}
|
||||
|
||||
bool IWorld::stop() {
|
||||
start();
|
||||
|
||||
setWorldStatus(WorldStatus::Background);
|
||||
_player->setControl(dynamic_cast<IControl*>(_backgroundAI));
|
||||
|
||||
_backgroundAI->startAI();
|
||||
|
||||
setTargetFps(30);
|
||||
|
||||
setRunning(false);
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -233,6 +232,7 @@ void IWorld::removeItem(int id, QList<int> *removedObjectsList) {
|
||||
}
|
||||
|
||||
void IWorld::reset() {
|
||||
|
||||
if (_player) {
|
||||
delete _player;
|
||||
_player = nullptr;
|
||||
@ -309,6 +309,14 @@ void IWorld::removeAnyItemFromGroup(const QString &group,
|
||||
removeItem(anyObjectId, removedObjectsList);
|
||||
}
|
||||
|
||||
bool IWorld::running() const {
|
||||
return _running;
|
||||
}
|
||||
|
||||
void IWorld::setRunning(bool newRunning) {
|
||||
_running = newRunning;
|
||||
}
|
||||
|
||||
int IWorld::targetFps() const {
|
||||
return _targetFps;
|
||||
}
|
||||
@ -357,7 +365,7 @@ void IWorld::setCameraReleativePosition(const QVector3D &newCameraReleativePosit
|
||||
}
|
||||
|
||||
void IWorld::handleStop() {
|
||||
stop();
|
||||
runAsBackGround();
|
||||
}
|
||||
|
||||
const QVector3D &IWorld::cameraReleativePosition() const {
|
||||
@ -400,3 +408,14 @@ void IWorld::setWorldStatus(int newWorldStatus) {
|
||||
const QString &IWorld::hdr() const {
|
||||
return _hdrMap;
|
||||
}
|
||||
|
||||
void IWorld::runAsBackGround() {
|
||||
start();
|
||||
|
||||
setWorldStatus(WorldStatus::Background);
|
||||
_player->setControl(dynamic_cast<IControl*>(_backgroundAI));
|
||||
|
||||
_backgroundAI->startAI();
|
||||
|
||||
setTargetFps(30);
|
||||
}
|
||||
|
@ -222,6 +222,12 @@ public:
|
||||
*/
|
||||
const QString &hdr() const;
|
||||
|
||||
/**
|
||||
* @brief runAsBackGround This method run this world as a backgroud.
|
||||
* The player start new game and sets new control from the backgroundAI method.
|
||||
*/
|
||||
void runAsBackGround();
|
||||
|
||||
signals:
|
||||
/**
|
||||
* @brief sigGameFinished This signal emit when game are finished
|
||||
@ -375,6 +381,19 @@ private:
|
||||
bool prepare();
|
||||
void reset();
|
||||
|
||||
|
||||
/**
|
||||
* @brief running This varibale check in render function if the running is true then render loop are working correctly
|
||||
* @return
|
||||
*/
|
||||
bool running() const;
|
||||
|
||||
/**
|
||||
* @brief setRunning
|
||||
* @param newRunning
|
||||
*/
|
||||
void setRunning(bool newRunning);
|
||||
|
||||
/**
|
||||
* @brief worldChanged This method generate diff for the qml
|
||||
* @param objects This is list of object on lvl
|
||||
@ -447,6 +466,7 @@ private:
|
||||
QHash<QString, std::function<IWorldItem*()>> _registeredTypes;
|
||||
|
||||
int _targetFps = 60;
|
||||
bool _running = false;
|
||||
|
||||
// engine
|
||||
friend class Engine;
|
||||
|
@ -31,12 +31,16 @@ const IWorldItem *IWorldItem::getPlayer() const {
|
||||
}
|
||||
|
||||
void IWorldItem::render(unsigned int) {
|
||||
if (_playerObject->position().x() - position().x() >
|
||||
_world->cameraReleativePosition().z() * 2) {
|
||||
setX(_playerObject->position().x() + _world->cameraReleativePosition().z() * 4);
|
||||
if (_playerObject->position().x() - _world->cameraReleativePosition().z() >
|
||||
position().x()) {
|
||||
|
||||
float dY = rand() % static_cast<int>(_world->cameraReleativePosition().z() * 2
|
||||
- _world->cameraReleativePosition().z());
|
||||
float dX = _world->cameraReleativePosition().z() * 2 +
|
||||
(rand() % static_cast<int>(_world->cameraReleativePosition().z()));
|
||||
|
||||
setX(_playerObject->position().x() + dX);
|
||||
|
||||
float dY = (rand() % static_cast<int>(_world->cameraReleativePosition().z() * 4)
|
||||
- _world->cameraReleativePosition().z() * 2);
|
||||
|
||||
setY(_playerObject->position().y() + dY);
|
||||
}
|
||||
|
@ -26,7 +26,11 @@ void SnakeItem::render(unsigned int tbfMsec) {
|
||||
auto ratationVector = (_prevObject->position() - position());
|
||||
|
||||
if (auto claster = static_cast<Snake*>(parentClaster())) {
|
||||
if (ratationVector.length() > claster->lengthBetwinItems()) {
|
||||
float ratationLength = ratationVector.length();
|
||||
|
||||
if (ratationLength > claster->lengthBetwinItems() * 10) {
|
||||
setposition(_prevObject->position());
|
||||
} else if (ratationLength > claster->lengthBetwinItems()) {
|
||||
setMovableVector(ratationVector.normalized() * claster->currentMovableVector().length());
|
||||
}
|
||||
}
|
||||
|
@ -11,6 +11,8 @@
|
||||
DefaultBackgroundAI::DefaultBackgroundAI() {
|
||||
_timer = new QTimer();
|
||||
_timer->setInterval(1000);
|
||||
|
||||
connect(_timer, &QTimer::timeout, this, &DefaultBackgroundAI::handleTimerTriger);
|
||||
}
|
||||
|
||||
DefaultBackgroundAI::~DefaultBackgroundAI() {
|
||||
@ -27,7 +29,7 @@ void DefaultBackgroundAI::stopAI() {
|
||||
}
|
||||
|
||||
void DefaultBackgroundAI::handleTimerTriger() {
|
||||
_timer->setInterval(rand() % 1000 + 10);
|
||||
_timer->setInterval(rand() % 2000 + 200);
|
||||
|
||||
emit userTap();
|
||||
}
|
||||
|
@ -46,9 +46,16 @@ void Engine::setWorld(IWorld *world) {
|
||||
_currentWorld = world;
|
||||
emit worldChanged();
|
||||
|
||||
prepareNewWorld();
|
||||
if (!prepareNewWorld()) {
|
||||
QuasarAppUtils::Params::log("Failed to init world. World name: " + _currentWorld->name(),
|
||||
QuasarAppUtils::Error);
|
||||
|
||||
_currentWorld = nullptr;
|
||||
return;
|
||||
}
|
||||
|
||||
startRenderLoop();
|
||||
_currentWorld->runAsBackGround();
|
||||
}
|
||||
|
||||
void Engine::setScane(QObject *newScane) {
|
||||
@ -129,20 +136,18 @@ void Engine::setPrepareLvlProgress(int newPrepareLvlProgress) {
|
||||
emit prepareLvlProgressChanged();
|
||||
}
|
||||
|
||||
void Engine::prepareNewWorld() {
|
||||
bool Engine::prepareNewWorld() {
|
||||
if (!_currentWorld->prepare()) {
|
||||
QuasarAppUtils::Params::log("Failed to init world. World name: " + _currentWorld->name(),
|
||||
QuasarAppUtils::Error);
|
||||
|
||||
_currentWorld = nullptr;
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!_currentWorld->userInterface()->init()) {
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
setMenu(_currentWorld->userInterface());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void Engine::renderLoop() {
|
||||
|
@ -132,7 +132,7 @@ signals:
|
||||
|
||||
private:
|
||||
void setPrepareLvlProgress(int newPrepareLvlProgress);
|
||||
void prepareNewWorld();
|
||||
bool prepareNewWorld();
|
||||
void renderLoop();
|
||||
|
||||
|
||||
|
@ -1 +1 @@
|
||||
Subproject commit 0f022f42093a1e5311cbcd13c9ed40b54163c251
|
||||
Subproject commit 9f06054ae836016881dc132d336c5da5bb3dca6c
|
Loading…
x
Reference in New Issue
Block a user