2021-10-02 20:36:07 +03:00
|
|
|
//
|
|
|
|
// Created by Иван Ильин on 22.09.2021.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "Shooter.h"
|
|
|
|
#include <fstream>
|
2021-10-18 09:21:09 +03:00
|
|
|
#include <utility>
|
2021-10-09 13:41:12 +03:00
|
|
|
#include "engine/animation/AColor.h"
|
|
|
|
#include "engine/animation/AFunction.h"
|
|
|
|
#include "engine/animation/ARotate.h"
|
|
|
|
#include "engine/animation/Timeline.h"
|
2021-10-16 20:22:55 +03:00
|
|
|
#include "ShooterConsts.h"
|
2021-10-17 19:38:16 +03:00
|
|
|
#include "engine/SoundController.h"
|
2021-10-31 22:11:04 +03:00
|
|
|
#include "engine/animation/AAttractToPoint.h"
|
|
|
|
#include "engine/animation/ARotateRelativePoint.h"
|
|
|
|
#include "engine/animation/ATranslateToPoint.h"
|
|
|
|
#include "engine/animation/AWait.h"
|
2021-11-01 19:04:10 +03:00
|
|
|
#include "engine/animation/ATranslate.h"
|
2021-10-02 20:36:07 +03:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
// Read server/client settings and start both.
|
|
|
|
// If client doesn't connect to the localhost - server doesn't start.
|
2021-10-28 16:58:02 +03:00
|
|
|
void Shooter::InitNetwork() {
|
2021-10-02 20:36:07 +03:00
|
|
|
std::string clientIp;
|
|
|
|
sf::Uint16 clientPort;
|
|
|
|
sf::Uint16 serverPort;
|
2021-10-26 09:40:35 +03:00
|
|
|
std::string playerName;
|
2021-10-02 20:36:07 +03:00
|
|
|
std::ifstream connectFile("connect.txt", std::ifstream::in);
|
|
|
|
|
|
|
|
// If failed to read client settings
|
2021-10-31 11:39:08 +03:00
|
|
|
if (!connectFile.is_open() || !(connectFile >> clientIp >> clientPort >> playerName) ||
|
|
|
|
sf::IpAddress(clientIp) == sf::IpAddress::None) {
|
2021-10-02 20:36:07 +03:00
|
|
|
connectFile.close();
|
|
|
|
// Create file and write default settings
|
|
|
|
clientIp = "127.0.0.1";
|
|
|
|
clientPort = 54000;
|
2021-10-26 09:40:35 +03:00
|
|
|
playerName = "PlayerName";
|
2021-10-02 20:36:07 +03:00
|
|
|
std::ofstream temp("connect.txt", std::ofstream::out);
|
2021-10-26 09:40:35 +03:00
|
|
|
temp << clientIp << std::endl << clientPort << std::endl << playerName;
|
2021-10-02 20:36:07 +03:00
|
|
|
temp.close();
|
|
|
|
}
|
|
|
|
connectFile.close();
|
|
|
|
|
|
|
|
// If failed to read server settings
|
|
|
|
connectFile.open("server.txt", std::ifstream::in);
|
2021-10-31 11:39:08 +03:00
|
|
|
if (!connectFile.is_open() || !(connectFile >> serverPort)) {
|
2021-10-02 20:36:07 +03:00
|
|
|
connectFile.close();
|
|
|
|
// Create file and write default settings
|
|
|
|
serverPort = 54000;
|
|
|
|
std::ofstream temp("server.txt", std::ofstream::out);
|
|
|
|
temp << serverPort;
|
|
|
|
temp.close();
|
|
|
|
}
|
|
|
|
connectFile.close();
|
|
|
|
|
|
|
|
if (clientIp == sf::IpAddress::LocalHost) {
|
|
|
|
server->start(serverPort);
|
2021-10-31 11:39:08 +03:00
|
|
|
if (server->isWorking())
|
2021-10-02 20:36:07 +03:00
|
|
|
server->generateBonuses();
|
|
|
|
}
|
|
|
|
|
|
|
|
client->connect(clientIp, clientPort);
|
2021-10-28 16:58:02 +03:00
|
|
|
player->setPlayerNickName(playerName);
|
2021-10-02 20:36:07 +03:00
|
|
|
|
2021-10-28 17:06:10 +03:00
|
|
|
// TODO: encapsulate call backs inside ShooterClient
|
2021-10-31 11:39:08 +03:00
|
|
|
client->setSpawnPlayerCallBack([this](sf::Uint16 id) { spawnPlayer(id); });
|
|
|
|
client->setRemovePlayerCallBack([this](sf::Uint16 id) { removePlayer(id); });
|
|
|
|
client->setAddFireTraceCallBack([this](const Vec3D &from, const Vec3D &to) { addFireTrace(from, to); });
|
|
|
|
client->setAddBonusCallBack(
|
|
|
|
[this](const std::string &bonusName, const Vec3D &position) { addBonus(bonusName, position); });
|
|
|
|
client->setRemoveBonusCallBack([this](const ObjectNameTag &bonusName) { removeBonus(bonusName); });
|
|
|
|
client->setChangeEnemyWeaponCallBack(
|
|
|
|
[this](const std::string &weaponName, sf::Uint16 id) { changeEnemyWeapon(weaponName, id); });
|
2021-10-02 20:36:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Shooter::start() {
|
|
|
|
// This code executed once in the beginning:
|
|
|
|
setUpdateWorld(false);
|
|
|
|
|
2021-10-28 16:58:02 +03:00
|
|
|
screen->setMouseCursorVisible(true);
|
2021-10-02 20:36:07 +03:00
|
|
|
|
2021-10-17 20:52:21 +03:00
|
|
|
world->loadMap(ShooterConsts::MAP_OBJ, Vec3D{5, 5, 5});
|
2021-10-02 20:36:07 +03:00
|
|
|
|
2021-10-17 20:52:21 +03:00
|
|
|
// TODO: encapsulate call backs inside Player
|
2021-10-31 11:39:08 +03:00
|
|
|
player->setAddTraceCallBack([this](const Vec3D &from, const Vec3D &to) {
|
|
|
|
client->addTrace(from, to);
|
|
|
|
addFireTrace(from, to);
|
|
|
|
});
|
|
|
|
player->setDamagePlayerCallBack(
|
|
|
|
[this](sf::Uint16 targetId, double damage) { client->damagePlayer(targetId, damage); });
|
|
|
|
player->setRayCastFunction(
|
|
|
|
[this](const Vec3D &from, const Vec3D &to) { return world->rayCast(from, to, "Player Weapon"); });
|
|
|
|
player->setTakeBonusCallBack([this](const string &bonusName) { client->takeBonus(bonusName); });
|
|
|
|
player->setAddWeaponCallBack([this](std::shared_ptr<Weapon> weapon) { addWeapon(std::move(weapon)); });
|
|
|
|
player->setRemoveWeaponCallBack([this](std::shared_ptr<Weapon> weapon) { removeWeapon(std::move(weapon)); });
|
2021-10-12 17:12:47 +03:00
|
|
|
|
2021-10-31 19:01:06 +03:00
|
|
|
player->reInitWeapons();
|
2021-10-02 20:36:07 +03:00
|
|
|
|
2021-10-29 23:44:37 +03:00
|
|
|
player->translate(Vec3D{0, 0, 0});
|
2021-10-12 17:12:47 +03:00
|
|
|
camera->translateToPoint(player->position() + Vec3D{0, 1.8, 0});
|
2021-10-28 16:58:02 +03:00
|
|
|
player->attach(camera);
|
2021-10-02 20:36:07 +03:00
|
|
|
|
2021-10-28 16:58:02 +03:00
|
|
|
world->addBody(player);
|
2021-10-02 20:36:07 +03:00
|
|
|
|
|
|
|
// connecting to the server
|
|
|
|
InitNetwork();
|
|
|
|
// Waiting for connect and updating server if it's same window
|
2021-10-28 16:58:02 +03:00
|
|
|
while (client->isWorking() && !client->connected()) {
|
2021-10-02 20:36:07 +03:00
|
|
|
client->update();
|
|
|
|
server->update();
|
|
|
|
Time::update();
|
|
|
|
}
|
|
|
|
// If connect fail - return to menu
|
2021-10-28 16:58:02 +03:00
|
|
|
if (!client->isWorking()) {
|
2021-10-02 20:36:07 +03:00
|
|
|
inGame = false;
|
|
|
|
server->stop();
|
|
|
|
}
|
2021-10-25 22:32:55 +03:00
|
|
|
|
|
|
|
// windows init:
|
2021-10-28 16:58:02 +03:00
|
|
|
mainMenu.setTitle("Main menu");
|
2021-10-25 22:32:55 +03:00
|
|
|
mainMenu.setBackgroundTexture(ShooterConsts::MAIN_MENU_BACK, 1.1, 1.1, screen->width(), screen->height());
|
|
|
|
|
2021-10-31 11:39:08 +03:00
|
|
|
mainMenu.addButton(screen->width() / 2, 200, 200, 20, [this]() {
|
|
|
|
this->play();
|
|
|
|
SoundController::playSound(SoundTag("click"), ShooterConsts::CLICK_SOUND);
|
|
|
|
}, "Server: " + client->serverIp().toString(), 5, 5, ShooterConsts::MAIN_MENU_GUI, {0, 66}, {0, 86}, {0, 46},
|
|
|
|
Consts::MEDIUM_FONT, {255, 255, 255});
|
|
|
|
mainMenu.addButton(screen->width() / 2, 350, 200, 20, [this]() {
|
|
|
|
this->player->translateToPoint(Vec3D{0, 0, 0});
|
|
|
|
this->player->setVelocity({});
|
|
|
|
this->play();
|
|
|
|
SoundController::playSound(SoundTag("click"), ShooterConsts::CLICK_SOUND);
|
|
|
|
}, "Respawn", 5, 5, ShooterConsts::MAIN_MENU_GUI, {0, 66}, {0, 86}, {0, 46}, Consts::MEDIUM_FONT, {255, 255, 255});
|
|
|
|
|
|
|
|
mainMenu.addButton(screen->width() / 2, 500, 200, 20, [this]() {
|
|
|
|
client->disconnect();
|
|
|
|
server->stop();
|
|
|
|
this->exit();
|
|
|
|
}, "Exit", 5, 5, ShooterConsts::MAIN_MENU_GUI, {0, 66}, {0, 86}, {0, 46}, Consts::MEDIUM_FONT, {255, 255, 255});
|
2021-10-02 20:36:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Shooter::update() {
|
|
|
|
// This code executed every time step:
|
|
|
|
|
|
|
|
server->update();
|
|
|
|
client->update();
|
|
|
|
|
|
|
|
// Check all input after this condition please
|
2021-10-28 16:58:02 +03:00
|
|
|
if (!screen->hasFocus()) {
|
2021-10-02 20:36:07 +03:00
|
|
|
return;
|
2021-10-28 16:58:02 +03:00
|
|
|
}
|
2021-10-02 20:36:07 +03:00
|
|
|
|
2021-10-31 11:39:08 +03:00
|
|
|
if (keyboard->isKeyTapped(sf::Keyboard::Escape)) {
|
2021-10-02 20:36:07 +03:00
|
|
|
inGame = !inGame;
|
2021-10-28 16:58:02 +03:00
|
|
|
screen->setMouseCursorVisible(!inGame);
|
2021-10-02 20:36:07 +03:00
|
|
|
}
|
|
|
|
|
2021-11-05 12:34:46 +03:00
|
|
|
if (keyboard->isKeyTapped(sf::Keyboard::O)) {
|
|
|
|
setGlEnable(!glEnable());
|
|
|
|
}
|
|
|
|
|
2021-11-03 12:49:07 +03:00
|
|
|
if (keyboard->isKeyTapped(sf::Keyboard::Tab)) {
|
|
|
|
setDebugInfo(!showDebugInfo());
|
|
|
|
}
|
|
|
|
|
2021-10-31 11:39:08 +03:00
|
|
|
if (inGame) {
|
2021-10-16 20:22:55 +03:00
|
|
|
screen->setTitle(ShooterConsts::PROJECT_NAME);
|
2021-10-02 20:36:07 +03:00
|
|
|
playerController->update();
|
|
|
|
} else {
|
|
|
|
mainMenu.update();
|
|
|
|
}
|
|
|
|
|
|
|
|
setUpdateWorld(inGame);
|
|
|
|
|
|
|
|
// background sounds and music control
|
2021-10-31 11:39:08 +03:00
|
|
|
if (SoundController::getStatus(SoundTag("background")) != sf::Sound::Status::Playing) {
|
2021-10-17 19:38:16 +03:00
|
|
|
SoundController::playSound(SoundTag("background"), ShooterConsts::BACK_NOISE);
|
2021-10-28 16:58:02 +03:00
|
|
|
}
|
2021-10-02 20:36:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Shooter::gui() {
|
|
|
|
sf::Sprite sprite;
|
2021-10-16 20:22:55 +03:00
|
|
|
sprite.setTexture(*ResourceManager::loadTexture(ShooterConsts::MAIN_MENU_GUI));
|
2021-10-02 20:36:07 +03:00
|
|
|
sprite.setTextureRect(sf::IntRect(243, 3, 9, 9));
|
|
|
|
sprite.scale(3, 3);
|
2021-10-31 11:39:08 +03:00
|
|
|
sprite.setPosition(static_cast<float>(screen->width()) / 2.0f - 27.0f / 2.0f,
|
|
|
|
static_cast<float>(screen->height()) / 2.0f - 27.0f / 2.0f);
|
2021-10-26 09:40:35 +03:00
|
|
|
sprite.setColor(sf::Color(0, 0, 0, 250));
|
2021-10-02 20:36:07 +03:00
|
|
|
screen->drawSprite(sprite);
|
|
|
|
|
|
|
|
// health player stats
|
|
|
|
drawPlayerStats();
|
2021-10-26 09:40:35 +03:00
|
|
|
drawStatsTable();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Shooter::drawStatsTable() {
|
2021-10-26 09:50:59 +03:00
|
|
|
int i = 1;
|
|
|
|
|
2021-10-31 11:39:08 +03:00
|
|
|
screen->drawText(client->lastEvent(), Vec2D{10, 10}, 25, sf::Color(0, 0, 0, 100));
|
2021-10-26 09:40:35 +03:00
|
|
|
|
|
|
|
vector<shared_ptr<Player>> allPlayers;
|
|
|
|
allPlayers.push_back(player);
|
2021-10-31 11:39:08 +03:00
|
|
|
for (auto&[playerId, player] : client->players())
|
2021-10-26 09:40:35 +03:00
|
|
|
allPlayers.push_back(player);
|
|
|
|
|
2021-10-31 11:39:08 +03:00
|
|
|
std::sort(allPlayers.begin(), allPlayers.end(), [](std::shared_ptr<Player> p1, std::shared_ptr<Player> p2) {
|
2021-10-26 09:40:35 +03:00
|
|
|
return p1->kills() - p1->deaths() > p2->kills() - p2->deaths();
|
2021-10-31 11:39:08 +03:00
|
|
|
});
|
2021-10-26 09:40:35 +03:00
|
|
|
|
2021-10-31 11:39:08 +03:00
|
|
|
for (auto &p : allPlayers) {
|
|
|
|
screen->drawText(std::to_string(i) + "\t" + p->playerNickName() + "\t" + std::to_string(p->kills()) + " / " +
|
|
|
|
std::to_string(p->deaths()),
|
|
|
|
Vec2D{10, 15 + 35.0 * i}, 25, sf::Color(0, 0, 0, 150));
|
2021-10-26 09:40:35 +03:00
|
|
|
i++;
|
|
|
|
}
|
2021-10-02 20:36:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Shooter::drawPlayerStats() {
|
|
|
|
// health bar
|
|
|
|
double xPos = 10;
|
2021-10-09 13:41:12 +03:00
|
|
|
double yPos = screen->height() - 20;
|
2021-10-02 20:36:07 +03:00
|
|
|
|
2021-10-31 11:39:08 +03:00
|
|
|
int width = screen->width() / 2 - 20;
|
2021-10-02 20:36:07 +03:00
|
|
|
int height = 10;
|
|
|
|
|
2021-10-12 20:19:02 +03:00
|
|
|
screen->drawTetragon(Vec2D{xPos, yPos},
|
2021-10-16 20:22:55 +03:00
|
|
|
Vec2D{xPos + width * player->health() / ShooterConsts::HEALTH_MAX, yPos},
|
|
|
|
Vec2D{xPos + width * player->health() / ShooterConsts::HEALTH_MAX, yPos + height},
|
2021-10-12 20:19:02 +03:00
|
|
|
Vec2D{xPos, yPos + height},
|
2021-10-31 11:39:08 +03:00
|
|
|
{static_cast<sf::Uint8>((ShooterConsts::HEALTH_MAX - player->health()) /
|
|
|
|
ShooterConsts::HEALTH_MAX * 255),
|
|
|
|
static_cast<sf::Uint8>(player->health() * 255 / ShooterConsts::HEALTH_MAX), 0, 100});
|
2021-10-02 20:36:07 +03:00
|
|
|
|
2021-10-12 20:19:02 +03:00
|
|
|
screen->drawTetragon(Vec2D{xPos, yPos - 15},
|
2021-10-16 20:22:55 +03:00
|
|
|
Vec2D{xPos + width * player->ability() / ShooterConsts::ABILITY_MAX, yPos - 15},
|
|
|
|
Vec2D{xPos + width * player->ability() / ShooterConsts::ABILITY_MAX, yPos - 15 + height},
|
2021-10-12 20:19:02 +03:00
|
|
|
Vec2D{xPos, yPos - 15 + height},
|
2021-10-31 11:39:08 +03:00
|
|
|
{255, 168, 168, 100});
|
2021-10-02 20:36:07 +03:00
|
|
|
|
2021-10-31 19:01:06 +03:00
|
|
|
auto balance = player->weapon()->balance();
|
2021-10-02 20:36:07 +03:00
|
|
|
|
2021-10-31 11:39:08 +03:00
|
|
|
screen->drawText(std::to_string((int) balance.first), Vec2D{150, static_cast<double>(screen->height() - 150)}, 100,
|
|
|
|
sf::Color(0, 0, 0, 100));
|
|
|
|
screen->drawText(std::to_string((int) balance.second), Vec2D{50, static_cast<double>(screen->height() - 100)}, 50,
|
|
|
|
sf::Color(0, 0, 0, 70));
|
2021-10-02 20:36:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Shooter::play() {
|
|
|
|
inGame = true;
|
2021-10-28 16:58:02 +03:00
|
|
|
screen->setMouseCursorVisible(false);
|
2021-10-02 20:36:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Shooter::spawnPlayer(sf::Uint16 id) {
|
2021-10-09 14:38:24 +03:00
|
|
|
std::string name = "Enemy_" + std::to_string(id);
|
2021-10-28 16:58:02 +03:00
|
|
|
|
2021-11-06 22:11:06 +03:00
|
|
|
std::shared_ptr<Player> newPlayer = std::make_shared<Player>(ObjectNameTag(name), ShooterConsts::BODY_OBJ,
|
|
|
|
Vec3D{0.4, 0.4, 0.4});
|
2021-10-02 20:36:07 +03:00
|
|
|
|
|
|
|
client->addPlayer(id, newPlayer);
|
2021-10-28 16:58:02 +03:00
|
|
|
world->addBody(newPlayer);
|
2021-10-02 20:36:07 +03:00
|
|
|
newPlayer->setVisible(true);
|
2021-11-06 22:11:06 +03:00
|
|
|
newPlayer->setCollision(false);
|
2021-10-12 17:12:47 +03:00
|
|
|
newPlayer->setAcceleration(Vec3D{0, 0, 0});
|
2021-10-02 20:36:07 +03:00
|
|
|
|
|
|
|
// add head and other stuff:
|
2021-11-06 22:11:06 +03:00
|
|
|
world->loadBody(ObjectNameTag(name + "_head"), ShooterConsts::HEAD_OBJ, Vec3D{0.4, 0.4, 0.4});
|
|
|
|
world->body(ObjectNameTag(name + "_head"))->translate(Vec3D{0, 2.2, 0});
|
2021-10-28 16:58:02 +03:00
|
|
|
newPlayer->attach(world->body(ObjectNameTag(name + "_head")));
|
2021-10-17 10:21:10 +03:00
|
|
|
|
2021-11-06 22:11:06 +03:00
|
|
|
world->loadBody(ObjectNameTag(name + "_foot_1"), ShooterConsts::FOOT_OBJ, Vec3D{0.4, 0.4, 0.4});
|
|
|
|
world->body(ObjectNameTag(name + "_foot_1"))->translate(Vec3D{-0.25, 0, 0});
|
|
|
|
newPlayer->attach(world->body(ObjectNameTag(name + "_foot_1")));
|
2021-10-17 10:21:10 +03:00
|
|
|
|
2021-11-06 22:11:06 +03:00
|
|
|
world->loadBody(ObjectNameTag(name + "_foot_2"), ShooterConsts::FOOT_OBJ, Vec3D{0.4, 0.4, 0.4});
|
|
|
|
world->body(ObjectNameTag(name + "_foot_2"))->translate(Vec3D{0.25, 0, 0});
|
|
|
|
newPlayer->attach(world->body(ObjectNameTag(name + "_foot_2")));
|
|
|
|
|
|
|
|
Vec3D randColor1 = Vec3D::Random();
|
|
|
|
Vec3D randColor2 = Vec3D::Random();
|
|
|
|
|
2021-11-07 10:47:24 +03:00
|
|
|
int colorBodyNum = (int) ((double) (rand()-1) / RAND_MAX * 5);
|
|
|
|
int colorFootNum = (int) ((double) (rand()-1) / RAND_MAX * 5);
|
|
|
|
|
|
|
|
newPlayer->setColor(ShooterConsts::WHITE_COLORS[colorBodyNum]);
|
|
|
|
world->body(ObjectNameTag(name + "_foot_1"))->setColor(ShooterConsts::DARK_COLORS[colorFootNum]);
|
|
|
|
world->body(ObjectNameTag(name + "_foot_2"))->setColor(ShooterConsts::DARK_COLORS[colorFootNum]);
|
2021-10-18 18:30:02 +03:00
|
|
|
|
|
|
|
changeEnemyWeapon("gun", id);
|
2021-10-02 20:36:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Shooter::removePlayer(sf::Uint16 id) {
|
2021-10-17 10:38:15 +03:00
|
|
|
std::string name = "Enemy_" + std::to_string(id);
|
2021-10-17 10:21:10 +03:00
|
|
|
world->removeBody(ObjectNameTag(name));
|
|
|
|
world->removeBody(ObjectNameTag(name + "_head"));
|
2021-11-06 22:11:06 +03:00
|
|
|
world->removeBody(ObjectNameTag(name + "_weapon"));
|
|
|
|
world->removeBody(ObjectNameTag(name + "_foot_1"));
|
|
|
|
world->removeBody(ObjectNameTag(name + "_foot_2"));
|
|
|
|
|
2021-10-02 20:36:07 +03:00
|
|
|
}
|
|
|
|
|
2021-10-12 17:12:47 +03:00
|
|
|
void Shooter::addFireTrace(const Vec3D &from, const Vec3D &to) {
|
2021-10-02 20:36:07 +03:00
|
|
|
std::string traceName = "Client_fireTraces_" + std::to_string(fireTraces++);
|
2021-10-28 16:58:02 +03:00
|
|
|
world->addBody(std::make_shared<RigidBody>(Mesh::LineTo(ObjectNameTag(traceName), from, to, 0.05)));
|
2021-10-17 10:21:10 +03:00
|
|
|
world->body(ObjectNameTag(traceName))->setCollider(false);
|
2021-10-02 20:36:07 +03:00
|
|
|
|
2021-10-31 11:39:08 +03:00
|
|
|
Timeline::animate(AnimationListTag(traceName + "_fadeOut"),
|
|
|
|
std::make_shared<AColor>(world->body(ObjectNameTag(traceName)), sf::Color{150, 150, 150, 0}));
|
|
|
|
Timeline::animate(AnimationListTag(traceName + "_delete"),
|
|
|
|
std::make_shared<AFunction>([this, traceName]() { removeFireTrace(ObjectNameTag(traceName)); }, 1,
|
2021-11-03 22:57:48 +03:00
|
|
|
1));
|
2021-10-02 20:36:07 +03:00
|
|
|
}
|
|
|
|
|
2021-10-31 11:39:08 +03:00
|
|
|
void Shooter::removeFireTrace(const ObjectNameTag &traceName) {
|
2021-10-02 20:36:07 +03:00
|
|
|
world->removeBody(traceName);
|
|
|
|
}
|
|
|
|
|
2021-10-12 17:12:47 +03:00
|
|
|
void Shooter::addBonus(const string &bonusName, const Vec3D &position) {
|
2021-10-31 11:39:08 +03:00
|
|
|
std::string name = bonusName.substr(6, bonusName.size() - 3 - 5);
|
2021-10-28 16:58:02 +03:00
|
|
|
|
|
|
|
ObjectNameTag nameTag(bonusName);
|
|
|
|
|
|
|
|
world->addBody(std::make_shared<RigidBody>(ObjectNameTag(bonusName), "obj/" + name + ".obj", Vec3D{3, 3, 3}));
|
2021-10-17 10:21:10 +03:00
|
|
|
world->body(ObjectNameTag(bonusName))->translateToPoint(position);
|
2021-10-28 16:58:02 +03:00
|
|
|
world->body(ObjectNameTag(bonusName))->setCollider(false);
|
2021-11-03 22:57:48 +03:00
|
|
|
world->body(ObjectNameTag(bonusName))->setSimpleHitBox(true);
|
|
|
|
world->body(ObjectNameTag(bonusName))->setTrigger(true);
|
2021-10-31 11:39:08 +03:00
|
|
|
Timeline::animate(AnimationListTag(bonusName + "_rotation"),
|
|
|
|
std::make_shared<ARotate>(world->body(ObjectNameTag(bonusName)), Vec3D{0, 2 * Consts::PI, 0}, 4,
|
|
|
|
Animation::LoopOut::Continue, Animation::InterpolationType::Linear));
|
2021-10-02 20:36:07 +03:00
|
|
|
}
|
|
|
|
|
2021-10-17 10:21:10 +03:00
|
|
|
void Shooter::removeBonus(const ObjectNameTag &bonusName) {
|
2021-10-02 20:36:07 +03:00
|
|
|
world->removeBody(bonusName);
|
2021-10-26 09:40:35 +03:00
|
|
|
Timeline::deleteAnimationList(AnimationListTag(bonusName.str() + "_rotation"));
|
2021-10-02 20:36:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Shooter::addWeapon(std::shared_ptr<Weapon> weapon) {
|
2021-10-28 16:58:02 +03:00
|
|
|
world->addBody(weapon);
|
2021-10-18 18:30:02 +03:00
|
|
|
|
2021-10-31 11:39:08 +03:00
|
|
|
if (client != nullptr) {
|
2021-10-18 18:30:02 +03:00
|
|
|
client->changeWeapon(weapon->name().str());
|
2021-10-28 16:58:02 +03:00
|
|
|
}
|
2021-10-18 18:30:02 +03:00
|
|
|
}
|
|
|
|
|
2021-10-31 11:39:08 +03:00
|
|
|
void Shooter::changeEnemyWeapon(const std::string &weaponName, sf::Uint16 enemyId) {
|
2021-10-28 16:58:02 +03:00
|
|
|
ObjectNameTag weaponTag("Enemy_" + std::to_string(enemyId) + "_weapon");
|
2021-10-18 18:30:02 +03:00
|
|
|
auto head = world->body(ObjectNameTag("Enemy_" + std::to_string(enemyId) + "_head"));
|
|
|
|
auto enemy = world->body(ObjectNameTag("Enemy_" + std::to_string(enemyId)));
|
|
|
|
|
|
|
|
// remove old weapon:
|
|
|
|
world->removeBody(weaponTag);
|
2021-10-29 18:19:30 +03:00
|
|
|
enemy->unattach(weaponTag);
|
2021-10-18 18:30:02 +03:00
|
|
|
|
|
|
|
world->loadBody(weaponTag, "obj/" + weaponName + ".obj");
|
|
|
|
world->body(weaponTag)->setCollider(false);
|
|
|
|
world->body(weaponTag)->scale(Vec3D(3, 3, 3));
|
|
|
|
|
2021-11-06 22:11:06 +03:00
|
|
|
world->body(weaponTag)->translateToPoint(
|
|
|
|
head->position() - enemy->left() * 2.5 - enemy->up() * 2.5 + enemy->lookAt());
|
2021-10-18 18:30:02 +03:00
|
|
|
|
2021-11-06 22:11:06 +03:00
|
|
|
world->body(weaponTag)->rotate(Vec3D(0, enemy->angle().y(), 0));
|
|
|
|
world->body(weaponTag)->rotateLeft(head->angleLeftUpLookAt().x());
|
2021-10-28 16:58:02 +03:00
|
|
|
enemy->attach(world->body(weaponTag));
|
2021-10-02 20:36:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Shooter::removeWeapon(std::shared_ptr<Weapon> weapon) {
|
2021-10-28 16:58:02 +03:00
|
|
|
world->removeBody(weapon->name());
|
2021-10-02 20:36:07 +03:00
|
|
|
}
|