불안정하게 멀티 됨.. 일단은 두번째 플레이어가 첫번째 플레이어한테 보이기는 하니까...

This commit is contained in:
2025-06-15 22:36:04 +09:00
parent 06a1e41dc8
commit 711fc4c93d
11 changed files with 328 additions and 77 deletions

View File

@@ -2,8 +2,14 @@
#include "vulkan_engine/vulkan/engine.h"
void BeginPlay(veng::Engine& engine) {
veng::Model* const player = engine.SpawnModel("player", "player");
std::random_device rd;
std::mt19937 gen = std::mt19937(rd());
std::uniform_real_distribution<float> jitterDist = std::uniform_real_distribution<float>(-2, 2);
veng::Model* const player = engine.SpawnLifedModel("player", "player", -1);
player->position = glm::vec3(jitterDist(gen), jitterDist(gen), 0.f);
player->scale = glm::vec3(.02f);
player->colision = true;
player->OnColision = [](veng::Model* self, veng::Model* other) {
@@ -15,16 +21,17 @@ void BeginPlay(veng::Engine& engine) {
other->colision = false;
other->visible = false;
other->shouldBeDestroyed = true;
};
veng::Model* const player_flame =
engine.SpawnModel("player_flame", "player_flame");
engine.SpawnLifedModel("player_flame", "player_flame", -1);
player_flame->scale = player->scale;
player_flame->colision = false;
player_flame->networkReplicated = false;
spdlog::info("player addr: {}", (void*)player);
veng::Model* const other_player = engine.SpawnModel("player", "other_player");
veng::Model* const other_player = engine.SpawnLifedModel("player", "other_player", -1);
other_player->position = glm::vec3(1.f, 0.f, 0.f);
other_player->scale = glm::vec3(.02f);
other_player->colision = true;
@@ -37,34 +44,35 @@ void BeginPlay(veng::Engine& engine) {
other->colision = false;
other->visible = false;
other->shouldBeDestroyed = true;
};
spdlog::info("other player addr: {}", (void*)other_player);
veng::Model* const camera_lag = engine.SpawnModel("", "camera_lag");
veng::Model* const camera_lag = engine.SpawnLifedModel("", "camera_lag", -1);
camera_lag->colision = false;
camera_lag->position = player->position;
veng::Model* const background = engine.SpawnModel("", "background");
veng::Model* const background = engine.SpawnLifedModel("", "background", -1);
background->colision = false;
background->position = {background->position.x, background->position.y, 30.f};
background->scale *= 100;
background->networkReplicated = false;
veng::Model* const background0 =
engine.SpawnModel("background", "background0");
engine.SpawnLifedModel("background", "background0", -1);
background0->scale = background->scale;
background0->networkReplicated = false;
veng::Model* const background1 =
engine.SpawnModel("background", "background1");
engine.SpawnLifedModel("background", "background1", -1);
background1->scale = background->scale;
background1->networkReplicated = false;
veng::Model* const background2 =
engine.SpawnModel("background", "background2");
engine.SpawnLifedModel("background", "background2", -1);
background2->scale = background->scale;
background2->networkReplicated = false;
veng::Model* const background3 =
engine.SpawnModel("background", "background3");
engine.SpawnLifedModel("background", "background3", -1);
background3->scale = background->scale;
background3->networkReplicated = false;
}

View File

@@ -28,26 +28,29 @@ std::int32_t main(std::int32_t argc, gsl::zstring* argv) {
addr.set(AF_INET6, "::1", 9010);
Network::Socket sock;
Network::TCPSocket TCPSock;
TCPSock.init(AF_INET6);
sock = TCPSock;
if (sock.connect(addr) == INVALID_SOCKET) {
if (TCPSock.connect(addr) == INVALID_SOCKET) {
spdlog::error("connect()");
std::exit(EXIT_FAILURE);
}
iocp.registerTCPSocket(sock, 16 * 1024);
iocp.registerTCPSocket(TCPSock, 16 * 1024);
while (iocp.GetRecvedBytes(sock.sock) < 6);
while (iocp.GetRecvedBytes(TCPSock.sock) < 6);
std::vector<char> header_serialized(6);
iocp.recv(sock, header_serialized);
iocp.recv(TCPSock, header_serialized);
Packet::Header header;
header.Deserialize(header_serialized);
while (iocp.GetRecvedBytes(sock.sock) < header.body_length);
while (iocp.GetRecvedBytes(TCPSock.sock) < header.body_length);
std::vector<char> data(header.body_length);
iocp.recv(sock, data);
iocp.recv(TCPSock, data);
::memcpy(&CLIENTID, data.data(), 1); //id 받기
header.opcode = Packet::Opcode::CLIENTID;
header.body_length = 0;
header_serialized = header.Serialize();
iocp.send(TCPSock, header_serialized);
const veng::GlfwInitialization _glfw;
veng::Window window("Vulkan Engine", {800, 600});
@@ -70,7 +73,7 @@ std::int32_t main(std::int32_t argc, gsl::zstring* argv) {
glfwPollEvents();
engine.Update();
engine.NetUpdate(std::make_shared<Network::Socket>(sock));
engine.NetUpdate(std::make_shared<Network::Socket>(TCPSock));
}
return EXIT_SUCCESS;

View File

@@ -31,17 +31,27 @@ void Tick(veng::Engine& engine, std::float_t delta_time) {
(-stiffness * displacement - damping * camera_lag->linear_velocity) *
delta_time;
camera_lag->linear_velocity = glm::vec3(0);
camera_lag->position = player->position;
engine.view = glm::lookAt(
glm::vec3(camera_lag->position.x, camera_lag->position.y, -5.f),
camera_lag->position, glm::vec3(0.f, -1.f, 0.f));
static bool w_laststate = false;
if (glfwGetKey(engine.vulkan_graphics->window->GetHandle(), GLFW_KEY_W) ==
GLFW_PRESS) {
player->linear_acceleration = glm::normalize(forward) * 10.f;
player_flame->visible = true;
player->needsUpdate = true;
w_laststate = true;
} else {
player->linear_acceleration = forward * .0f;
player_flame->visible = false;
if (w_laststate) {
player->needsUpdate = true;
w_laststate = false;
}
}
if (bullet_cooldown > std::numeric_limits<std::float_t>::epsilon()) {
@@ -60,21 +70,33 @@ void Tick(veng::Engine& engine, std::float_t delta_time) {
bullet->owner = player;
bullet->scale = player->scale;
bullet->colision = true;
bullet->networkReplicated = true;
bullet->needsUpdate = true;
spdlog::info("bullet address: {}", (void*)bullet);
}
}
static bool ad_laststate = false;
if (glfwGetKey(engine.vulkan_graphics->window->GetHandle(), GLFW_KEY_A) ==
GLFW_PRESS) {
right = player->rotation * glm::vec3(0, 0, 1);
player->angular_velocity = right * 6.f;
player->needsUpdate = true;
ad_laststate = true;
} else if (glfwGetKey(engine.vulkan_graphics->window->GetHandle(),
GLFW_KEY_D) == GLFW_PRESS) {
right = player->rotation * glm::vec3(0, 0, 1);
player->angular_velocity = right * -6.f;
player->needsUpdate = true;
ad_laststate = true;
} else {
right = player->rotation * glm::vec3(0, 0, 1);
player->angular_velocity = right * 0.f;
if (ad_laststate) {
player->needsUpdate = true;
ad_laststate = false;
}
}
player_flame->rotation = player->rotation;