void playerDamaged(int assaulter, int victim, int amount) { GAME.state.hasChanged = true; if (GAME.isClient) return; amount = 12; printf("Player %d damaged player %d! It was %d effective.\n", assaulter, victim, amount); auto& players = GAME.world.players; if (assaulter < 1) return; if (players.find(victim) == players.end()) return; // ??? if (players.find(assaulter) == players.end()) assaulter = (int)CauseOfDeath::PastPlayer; Player& victimPlayer = players[victim]; if (victimPlayer.health < amount) victimPlayer.health = 0; // Bye bye... else victimPlayer.health -= amount; printf(" health = %d\n", victimPlayer.health); if (victimPlayer.health == 0) { playerDied(assaulter, victimPlayer); } GAME.state.hasChanged = true; }
void Quake::output(const QString &output) { if(output.contains("entered the game")){ } else if(output.contains("You got the shells")){ } else if(output.contains("You receive 100 health")){ } else if(output.contains("was shot by a Grunt")){ emit playerDied(); } else; }
void ScriptRunner::bootstrap() { if (QThread::currentThread() != m_thread) { bool success; success = QMetaObject::invokeMethod(this, "bootstrap", Qt::QueuedConnection); Q_ASSERT(success); return; } // initialize the MF object before we run any user code QScriptValue mf_obj = m_engine->newObject(); m_engine->globalObject().setProperty("mf", mf_obj); // add utility functions mf_obj.setProperty("include", m_engine->newFunction(include)); mf_obj.setProperty("exit", m_engine->newFunction(exit)); mf_obj.setProperty("print", m_engine->newFunction(print)); mf_obj.setProperty("debug", m_engine->newFunction(debug)); mf_obj.setProperty("setTimeout", m_engine->newFunction(setTimeout)); mf_obj.setProperty("clearTimeout", m_engine->newFunction(clearTimeout)); mf_obj.setProperty("setInterval", m_engine->newFunction(setInterval)); mf_obj.setProperty("clearInterval", m_engine->newFunction(clearTimeout)); mf_obj.setProperty("currentTimestamp", m_engine->newFunction(currentTimestamp)); mf_obj.setProperty("readFile", m_engine->newFunction(readFile)); mf_obj.setProperty("writeFile", m_engine->newFunction(writeFile)); mf_obj.setProperty("args", m_engine->newFunction(args)); // init event handler framework { QString file_name = ":/js/create_handlers.js"; m_handler_map = evalJsonContents(internalReadFile(file_name), file_name); checkEngine("creating event handlers"); } // init builtin types { QString file_name = ":/js/builtin_types.js"; m_engine->evaluate(internalReadFile(file_name), file_name); checkEngine("evaluating builtin type"); m_point_class = mf_obj.property("Point"); m_entity_class = mf_obj.property("Entity"); m_item_class = mf_obj.property("Item"); m_block_class = mf_obj.property("Block"); m_health_status_class = mf_obj.property("HealthStatus"); m_status_effect_class = mf_obj.property("StatusEffect"); } // create the mf.ItemType enum { QScriptValue item_type_obj = m_engine->newObject(); mf_obj.setProperty("ItemType", item_type_obj); const QHash<Item::ItemType, Item::ItemData*> * item_data_hash = Item::itemDataHash(); for (QHash<Item::ItemType, Item::ItemData*>::const_iterator it = item_data_hash->constBegin(); it != item_data_hash->constEnd(); ++it) { const Item::ItemData * item_data = it.value(); item_type_obj.setProperty(item_data->name, item_data->id); } } // hook up mf functions mf_obj.setProperty("chat", m_engine->newFunction(chat)); mf_obj.setProperty("timeOfDay", m_engine->newFunction(timeOfDay)); mf_obj.setProperty("itemStackHeight", m_engine->newFunction(itemStackHeight)); mf_obj.setProperty("isPhysical", m_engine->newFunction(isPhysical)); mf_obj.setProperty("isSafe", m_engine->newFunction(isSafe)); mf_obj.setProperty("isDiggable", m_engine->newFunction(isDiggable)); mf_obj.setProperty("healthStatus", m_engine->newFunction(healthStatus)); mf_obj.setProperty("blockAt", m_engine->newFunction(blockAt)); mf_obj.setProperty("isBlockLoaded", m_engine->newFunction(isBlockLoaded)); mf_obj.setProperty("signTextAt", m_engine->newFunction(signTextAt)); mf_obj.setProperty("self", m_engine->newFunction(self)); mf_obj.setProperty("setControlState", m_engine->newFunction(setControlState)); mf_obj.setProperty("clearControlStates", m_engine->newFunction(clearControlStates)); mf_obj.setProperty("lookAt", m_engine->newFunction(lookAt)); mf_obj.setProperty("respawn", m_engine->newFunction(respawn)); mf_obj.setProperty("entity", m_engine->newFunction(entity)); mf_obj.setProperty("startDigging", m_engine->newFunction(startDigging)); mf_obj.setProperty("stopDigging", m_engine->newFunction(stopDigging)); mf_obj.setProperty("attackEntity", m_engine->newFunction(attackEntity)); mf_obj.setProperty("selectEquipSlot", m_engine->newFunction(selectEquipSlot)); mf_obj.setProperty("selectedEquipSlot", m_engine->newFunction(selectedEquipSlot)); mf_obj.setProperty("openInventoryWindow", m_engine->newFunction(openInventoryWindow)); mf_obj.setProperty("clickInventorySlot", m_engine->newFunction(clickInventorySlot)); mf_obj.setProperty("clickUniqueSlot", m_engine->newFunction(clickUniqueSlot)); mf_obj.setProperty("clickOutsideWindow", m_engine->newFunction(clickOutsideWindow)); mf_obj.setProperty("closeWindow", m_engine->newFunction(closeWindow)); mf_obj.setProperty("inventoryItem", m_engine->newFunction(inventoryItem)); mf_obj.setProperty("uniqueWindowItem", m_engine->newFunction(uniqueWindowItem)); mf_obj.setProperty("canPlaceBlock", m_engine->newFunction(canPlaceBlock)); mf_obj.setProperty("activateItem", m_engine->newFunction(activateItem)); mf_obj.setProperty("dimension", m_engine->newFunction(dimension)); mf_obj.setProperty("onlinePlayers", m_engine->newFunction(onlinePlayers)); // hook up hax functions QScriptValue hax_obj = m_engine->newObject(); mf_obj.setProperty("hax", hax_obj); hax_obj.setProperty("setPosition", m_engine->newFunction(setPosition)); hax_obj.setProperty("positionUpdateInterval", m_engine->newFunction(positionUpdateInterval)); hax_obj.setProperty("setGravityEnabled", m_engine->newFunction(setGravityEnabled)); hax_obj.setProperty("setJesusModeEnabled", m_engine->newFunction(setJesusModeEnabled)); hax_obj.setProperty("placeBlock", m_engine->newFunction(placeBlock)); hax_obj.setProperty("activateBlock", m_engine->newFunction(activateBlock)); // run main script QString main_script_contents = internalReadFile(m_main_script_filename); if (main_script_contents.isNull()) { m_stderr << "file not found: " << m_main_script_filename << "\n"; m_stderr.flush(); QCoreApplication::instance()->exit(1); return; } m_engine->evaluate(main_script_contents, m_main_script_filename); checkEngine("evaluating main script"); if (m_exiting) return; // connect to server bool success; success = connect(m_game, SIGNAL(entitySpawned(QSharedPointer<Game::Entity>)), this, SLOT(handleEntitySpawned(QSharedPointer<Game::Entity>))); Q_ASSERT(success); success = connect(m_game, SIGNAL(entityMoved(QSharedPointer<Game::Entity>)), this, SLOT(handleEntityMoved(QSharedPointer<Game::Entity>))); Q_ASSERT(success); success = connect(m_game, SIGNAL(entityDespawned(QSharedPointer<Game::Entity>)), this, SLOT(handleEntityDespawned(QSharedPointer<Game::Entity>))); Q_ASSERT(success); success = connect(m_game, SIGNAL(animation(QSharedPointer<Game::Entity>,Message::AnimationType)), this, SLOT(handleAnimation(QSharedPointer<Game::Entity>,Message::AnimationType))); Q_ASSERT(success); success = connect(m_game, SIGNAL(entityEffect(QSharedPointer<Game::Entity>,QSharedPointer<Game::StatusEffect>)), this, SLOT(handleEntityEffect(QSharedPointer<Game::Entity>,QSharedPointer<Game::StatusEffect>))); Q_ASSERT(success); success = connect(m_game, SIGNAL(removeEntityEffect(QSharedPointer<Game::Entity>,QSharedPointer<Game::StatusEffect>)), this, SLOT(handleRemoveEntityEffect(QSharedPointer<Game::Entity>,QSharedPointer<Game::StatusEffect>))); Q_ASSERT(success); success = connect(m_game, SIGNAL(chunkUpdated(Int3D,Int3D)), this, SLOT(handleChunkUpdated(Int3D,Int3D))); Q_ASSERT(success); success = connect(m_game, SIGNAL(signUpdated(Int3D,QString)), this, SLOT(handleSignUpdated(Int3D,QString))); Q_ASSERT(success); success = connect(m_game, SIGNAL(playerPositionUpdated()), this, SLOT(movePlayerPosition())); Q_ASSERT(success); success = connect(m_game, SIGNAL(loginStatusUpdated(Server::LoginStatus)), this, SLOT(handleLoginStatusUpdated(Server::LoginStatus))); Q_ASSERT(success); success = connect(m_game, SIGNAL(chatReceived(QString,QString)), this, SLOT(handleChatReceived(QString,QString))); Q_ASSERT(success); success = connect(m_game, SIGNAL(nonSpokenChatReceived(QString)), this, SLOT(handleNonSpokenChatReceived(QString))); Q_ASSERT(success); success = connect(m_game, SIGNAL(timeUpdated(double)), this, SLOT(handleTimeUpdated(double))); Q_ASSERT(success); success = connect(m_game, SIGNAL(playerDied()), this, SLOT(handlePlayerDied())); Q_ASSERT(success); success = connect(m_game, SIGNAL(playerSpawned(int)), this, SLOT(handlePlayerSpawned(int))); Q_ASSERT(success); success = connect(m_game, SIGNAL(playerHealthStatusUpdated()), this, SLOT(handlePlayerHealthStatusUpdated())); Q_ASSERT(success); success = connect(m_game, SIGNAL(inventoryUpdated()), this, SLOT(handleInventoryUpdated())); Q_ASSERT(success); success = connect(m_game, SIGNAL(stoppedDigging(Game::StoppedDiggingReason)), this, SLOT(handleStoppedDigging(Game::StoppedDiggingReason))); Q_ASSERT(success); success = connect(m_game, SIGNAL(windowOpened(Message::WindowType)), this, SLOT(handleWindowOpened(Message::WindowType))); Q_ASSERT(success); success = connect(m_game, SIGNAL(equippedItemChanged()), this, SLOT(handleEquippedItemChanged())); Q_ASSERT(success); success = connect(&m_stdin_reader, SIGNAL(readLine(QString)), this, SLOT(handleReadLine(QString))); Q_ASSERT(success); success = connect(&m_stdin_reader, SIGNAL(eof()), QCoreApplication::instance(), SLOT(quit())); Q_ASSERT(success); m_physics_doer = new PhysicsDoer(m_game); m_started_game = true; m_game->start(); m_stdin_reader.start(); }
void GameEngine::run() { spriteVec.push_back(levelVec.at(currentLevel)->getBackground()); spriteVec.push_back(player); bool goOn = true; long laps = 0; while (goOn) { Uint32 nextTick = SDL_GetTicks() + ticks; SDL_RenderClear(win->getRen()); if (initialLoop) { textInput(); initialLoop = false; } SDL_Event eve2; for (size_t i = 0; i < spriteVec.size(); i++) { spriteVec.at(i)->draw(); cd.checkCollisions(spriteVec.at(i)); } removeSprites(); cd.clearVector(); for (size_t i = 0; i < textBoxVec.size(); i++) { textBoxVec.at(i)->draw(); } if (pointCounter != nullptr) { pointCounter->draw(); } SDL_RenderPresent(win->getRen()); if (player == nullptr) { playerDied(); } while (SDL_PollEvent(&eve2)) { switch (eve2.type) { case SDL_QUIT: goOn = false; break; case SDL_KEYDOWN: if (functionMap.count(eve2.key.keysym.sym) != 0) { functionMap[eve2.key.keysym.sym](); } for (size_t i = 0; i < memberFuncVec.size(); i++) { memberFuncVec.at(i)->perform(eve2.key.keysym.sym); } for (size_t i = 0; i < spriteVec.size(); i++) { spriteVec.at(i)->draw(eve2); } break; case SDL_KEYUP: for (size_t i = 0; i < spriteVec.size(); i++) { spriteVec.at(i)->draw(eve2); } break; } } laps++; levelVec.at(currentLevel)->spawnWave(this, laps); deleteSpawns(laps); //This is triggered when the level is over: if (laps > levelVec.at(currentLevel)->getEndFrame()) { SDL_RenderClear(win->getRen()); for (size_t i = 0; i < spriteVec.size(); i++) { if (spriteVec.at(i)->getIsPlayer() == false) { spriteVec.at(i)->destroy(); //Prepare every sprite for removal except the player } } removeSprites(); spriteVec.clear(); deleteAllSpawns(); SDL_RenderClear(win->getRen()); levelSwap(); //Needs to be set by the game programmer currentLevel++; return this->run(); } if (SDL_GetTicks() < nextTick) { SDL_Delay(nextTick - SDL_GetTicks()); } } }