Files
Np_Term/Client/src/asteroid/tick.cpp

130 lines
4.9 KiB
C++

#include "asteroid/game.h"
#include "vulkan_engine/vulkan/engine.h"
void Tick(veng::Engine& engine, std::float_t delta_time) {
static std::float_t bullet_cooldown = 0.f;
veng::Model* const player = engine.GetSpawnedObject("player");
veng::Model* const player_flame = engine.GetSpawnedObject("player_flame");
veng::Model* const camera_lag = engine.GetSpawnedObject("camera_lag");
veng::Model* const background = engine.GetSpawnedObject("background");
veng::Model* const background0 = engine.GetSpawnedObject("background0");
veng::Model* const background1 = engine.GetSpawnedObject("background1");
veng::Model* const background2 = engine.GetSpawnedObject("background2");
veng::Model* const background3 = engine.GetSpawnedObject("background3");
glm::vec3 forward = player->rotation * glm::vec3(0, 1, 0);
glm::vec3 right = player->rotation * glm::vec3(1, 0, 0);
std::float_t stiffness =
500.0f * ((glm::length(player->linear_velocity) > 1.f)
? glm::length(player->linear_velocity)
: 1.f); // 더 크면 빠르게 따라감
std::float_t damping = 10.f * glm::sqrt(stiffness); // 임계 감쇠
// 감쇠 스프링 업데이트
glm::vec3 displacement = camera_lag->position - player->position;
camera_lag->linear_velocity +=
(-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()) {
bullet_cooldown -= delta_time;
}
if (glfwGetKey(engine.vulkan_graphics->window->GetHandle(), GLFW_KEY_SPACE) ==
GLFW_PRESS) {
if (bullet_cooldown > std::numeric_limits<std::float_t>::epsilon()) {
bullet_cooldown -= delta_time;
} else {
bullet_cooldown = .2f;
veng::Model* const bullet =
engine.SpawnLifedModel("bullet", "bullet", 10.f);
bullet->linear_velocity = player->linear_velocity + forward * 10.f;
bullet->position = player->position + forward * player->scale.x * 10.f;
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;
player_flame->position =
player->position + player->rotation * player_flame->original_offset *
0.5f * player_flame->scale;
if (player->position.x - background->position.x >= background->scale.x)
background->position += glm::vec3(2.f, 0.f, 0.f) * background->scale;
if (player->position.x - background->position.x < -background->scale.x)
background->position -= glm::vec3(2.f, 0.f, 0.f) * background->scale;
if (player->position.y - background->position.y >= background->scale.y)
background->position += glm::vec3(0.f, 2.f, 0.f) * background->scale;
if (player->position.y - background->position.y < -background->scale.y)
background->position -= glm::vec3(0.f, 2.f, 0.f) * background->scale;
glm::vec3 sparse;
sparse = glm::vec3(1.f, 1.f, 0.f);
background0->position = background->position + sparse * background->scale;
sparse = glm::vec3(-1.f, 1.f, 0.f);
background1->position = background->position + sparse * background->scale;
sparse = glm::vec3(1.f, -1.f, 0.f);
background2->position = background->position + sparse * background->scale;
sparse = glm::vec3(-1.f, -1.f, 0.f);
background3->position = background->position + sparse * background->scale;
}