이대로 제출해버릴까..
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
#pragma once
|
||||
#include "utils/snowflake.h"
|
||||
|
||||
extern std::uint8_t CLIENTID;
|
||||
|
||||
@@ -6,5 +7,5 @@ namespace veng {
|
||||
class Engine;
|
||||
}
|
||||
|
||||
void BeginPlay(veng::Engine& engine);
|
||||
void BeginPlay(veng::Engine& engine, utils::Snowflake playerID);
|
||||
void Tick(veng::Engine& engine, std::float_t delta_time);
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
#include "asteroid/game.h"
|
||||
#include "vulkan_engine/vulkan/engine.h"
|
||||
#include "utils/ConfigManager.h"
|
||||
|
||||
void BeginPlay(veng::Engine& engine) {
|
||||
void BeginPlay(veng::Engine& engine, utils::Snowflake playerID) {
|
||||
std::random_device rd;
|
||||
std::mt19937 gen = std::mt19937(rd());
|
||||
std::uniform_real_distribution<float> jitterDist =
|
||||
@@ -11,6 +12,8 @@ void BeginPlay(veng::Engine& engine) {
|
||||
engine.SpawnLifedModel("player", "player", -1);
|
||||
{
|
||||
std::lock_guard lock(player->modding);
|
||||
if (playerID.snowflake != 0)
|
||||
player->ID = playerID;
|
||||
player->position = glm::vec3(jitterDist(gen), jitterDist(gen), 0.f);
|
||||
player->scale = glm::vec3(.02f);
|
||||
player->colision = true;
|
||||
@@ -46,7 +49,7 @@ void BeginPlay(veng::Engine& engine) {
|
||||
player_flame->needsUpdate = true;
|
||||
}
|
||||
|
||||
std::shared_ptr<veng::Model> const other_player =
|
||||
/*std::shared_ptr<veng::Model> const other_player =
|
||||
engine.SpawnLifedModel("player", "other_player", -1);
|
||||
{
|
||||
std::lock_guard lock(other_player->modding);
|
||||
@@ -74,7 +77,7 @@ void BeginPlay(veng::Engine& engine) {
|
||||
other->shouldBeDestroyed = true;
|
||||
};
|
||||
other_player->colision = true;
|
||||
}
|
||||
}*/
|
||||
|
||||
std::shared_ptr<veng::Model> const camera_lag =
|
||||
engine.SpawnLifedModel("", "camera_lag", -1);
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include "socket/tcp_socket.h"
|
||||
#include "socket/udp_socket.h"
|
||||
#include "socket/wsa_manager.h"
|
||||
#include "utils/ConfigManager.h"
|
||||
#include "utils/log.h"
|
||||
#include "utils/snowflake.h"
|
||||
#include "utils/utils.h"
|
||||
@@ -17,21 +18,21 @@ std::uint8_t CLIENTID = 0;
|
||||
|
||||
std::int32_t main(std::int32_t argc, gsl::zstring* argv) {
|
||||
Network::WSAManager wsamanager;
|
||||
#if !defined(NDEBUG)
|
||||
utils::setDefaultLogger(spdlog::level::level_enum::debug, "log.log", 1024, 2);
|
||||
#endif
|
||||
|
||||
auto config = utils::ConfigManager::load();
|
||||
utils::setDefaultLogger(config.logLevel, config.logFileName,
|
||||
config.logfileSize, config.logfileCount);
|
||||
|
||||
utils::ThreadPool tp(0);
|
||||
Network::IOCP iocp;
|
||||
iocp.init(&tp, SessionProtocol::TCP);
|
||||
|
||||
Network::Address addr;
|
||||
in6_addr in6addr;
|
||||
|
||||
addr.set(AF_INET6, "::1", 9010);
|
||||
addr.set(config.ipVersion, config.IP, config.Port);
|
||||
|
||||
std::shared_ptr<Network::TCPSocket> TCPSock =
|
||||
std::make_shared<Network::TCPSocket>();
|
||||
TCPSock->init(AF_INET6);
|
||||
std::make_shared<Network::TCPSocket>(config.ipVersion);
|
||||
if (TCPSock->connect(addr) == INVALID_SOCKET) {
|
||||
spdlog::error("connect()");
|
||||
std::exit(EXIT_FAILURE);
|
||||
@@ -61,11 +62,19 @@ std::int32_t main(std::int32_t argc, gsl::zstring* argv) {
|
||||
engine.LoadModelAsset("assets/bullet.fbx", "bullet");
|
||||
engine.LoadModelAsset("assets/background.fbx", "background");
|
||||
|
||||
engine.BeginPlay = BeginPlay;
|
||||
engine.BeginPlay = [ID64 = config.playerid](veng::Engine& engine) {
|
||||
utils::Snowflake ID;
|
||||
ID.snowflake = ID64;
|
||||
BeginPlay(engine, ID);
|
||||
};
|
||||
engine.Tick = Tick;
|
||||
|
||||
engine.init();
|
||||
|
||||
std::shared_ptr<veng::Model> const player = engine.GetSpawnedObject("player");
|
||||
config.playerid = player->ID.snowflake;
|
||||
utils::ConfigManager::save(config);
|
||||
|
||||
tp.enqueueJob(
|
||||
[engine = &engine, TCPSock](utils::ThreadPool* tp, std::uint32_t __) {
|
||||
engine->ResponseToServerAndRefresh(TCPSock);
|
||||
|
||||
@@ -90,6 +90,7 @@ void Tick(veng::Engine& engine, std::float_t delta_time) {
|
||||
bullet->linear_velocity = player->linear_velocity + forward * 10.f;
|
||||
bullet->position = player->position + forward * player->scale.x * 10.f;
|
||||
bullet->owner = player;
|
||||
bullet->OwnerID = player->ID;
|
||||
bullet->scale = player->scale;
|
||||
bullet->colision = true;
|
||||
bullet->OnColision = [](utils::ThreadPool* thread_pool,
|
||||
|
||||
60
Client/src/utils/ConfigManager.cpp
Normal file
60
Client/src/utils/ConfigManager.cpp
Normal file
@@ -0,0 +1,60 @@
|
||||
#include "Utils/ConfigManager.h"
|
||||
|
||||
#include <json/json.h>
|
||||
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
namespace utils {
|
||||
Config ConfigManager::load() {
|
||||
Config config;
|
||||
|
||||
std::ifstream configfile("config.json", std::ifstream::binary);
|
||||
if (!configfile.is_open()) {
|
||||
std::ofstream defaultConfig("config.json", std::ios::out);
|
||||
config.configJsonRoot["IP Version"] = AF_INET6;
|
||||
config.configJsonRoot["IP"] = "::1";
|
||||
config.configJsonRoot["Port"] = 9010;
|
||||
config.configJsonRoot["LogLevel"] = 1;
|
||||
config.configJsonRoot["LogfileName"] = "log.log";
|
||||
config.configJsonRoot["LogfileSize"] = UINT32_MAX;
|
||||
config.configJsonRoot["LogfileCount"] = 5;
|
||||
config.configJsonRoot["PlayerID"] = 0;
|
||||
defaultConfig << config.configJsonRoot;
|
||||
defaultConfig.close();
|
||||
spdlog::critical(
|
||||
"\"config.json\" is missing. Default configuration has been written.");
|
||||
}
|
||||
try {
|
||||
if (configfile.is_open()) configfile >> config.configJsonRoot;
|
||||
|
||||
config.ipVersion = config.configJsonRoot["IP Version"].asInt();
|
||||
if (config.ipVersion != AF_INET && config.ipVersion != AF_INET6)
|
||||
throw std::runtime_error("Invalid IP Version.");
|
||||
|
||||
config.IP = config.configJsonRoot["IP"].asCString();
|
||||
|
||||
config.Port = config.configJsonRoot["Port"].asInt();
|
||||
if (config.Port < 0 || config.Port > 65535)
|
||||
throw std::runtime_error("Invalid listen port.");
|
||||
|
||||
int ll_ = config.configJsonRoot["LogLevel"].asInt();
|
||||
if (ll_ >= 0 && ll_ < spdlog::level::n_levels)
|
||||
config.logLevel = (spdlog::level::level_enum)ll_;
|
||||
else
|
||||
throw std::runtime_error("Invalid log level.");
|
||||
|
||||
config.logFileName = config.configJsonRoot["LogfileName"].asCString();
|
||||
config.logfileSize = config.configJsonRoot["LogfileSize"].asUInt();
|
||||
config.logfileCount = config.configJsonRoot["LogfileCount"].asUInt();
|
||||
|
||||
config.playerid = config.configJsonRoot["PlayerID"].asUInt64();
|
||||
} catch (Json::RuntimeError e) {
|
||||
spdlog::critical(
|
||||
std::string(std::string("[Json Error: ]") + e.what()).c_str());
|
||||
std::exit(EXIT_FAILURE);
|
||||
}
|
||||
return config;
|
||||
}
|
||||
} // namespace Chattr
|
||||
Reference in New Issue
Block a user