add core:mob component & move player movement to scripting

This commit is contained in:
MihailRis
2025-08-10 22:55:34 +03:00
parent 67ef84d6b0
commit dfb83f6835
10 changed files with 132 additions and 100 deletions
-5
View File
@@ -271,7 +271,6 @@ void PlayerController::update(float delta, const Input* inputEvents) {
} else {
resetKeyboard();
}
updatePlayer(delta);
}
void PlayerController::postUpdate(
@@ -313,10 +312,6 @@ void PlayerController::resetKeyboard() {
input = {};
}
void PlayerController::updatePlayer(float delta) {
player.updateInput(input, delta);
}
static int determine_rotation(
const Block* def, const glm::ivec3& norm, const glm::vec3& camDir
) {
-1
View File
@@ -60,7 +60,6 @@ class PlayerController {
void updateKeyboard(const Input& inputEvents);
void resetKeyboard();
void updatePlayer(float delta);
void updateEntityInteraction(entityid_t eid, bool lclick, bool rclick);
void updateInteraction(const Input& inputEvents, float delta);
+3 -1
View File
@@ -297,7 +297,9 @@ void Entities::updatePhysics(float delta) {
int substeps = static_cast<int>(delta * vel * 20);
substeps = std::min(100, std::max(2, substeps));
physics->step(*level.chunks, hitbox, delta, substeps, eid.uid);
hitbox.linearDamping = hitbox.grounded * 24;
hitbox.friction = glm::abs(hitbox.gravityScale <= 1e-7f)
? 8.0f
: (!grounded ? 2.0f : 10.0f);
transform.setPos(hitbox.position);
if (hitbox.grounded && !grounded) {
scripting::on_entity_grounded(
-82
View File
@@ -21,13 +21,6 @@
static debug::Logger logger("player");
constexpr float CROUCH_SPEED_MUL = 0.35f;
constexpr float RUN_SPEED_MUL = 1.5f;
constexpr float PLAYER_GROUND_DAMPING = 10.0f;
constexpr float PLAYER_AIR_DAMPING = 8.0f;
constexpr float FLIGHT_SPEED_MUL = 4.0f;
constexpr float CHEAT_SPEED_MUL = 5.0f;
constexpr float JUMP_FORCE = 8.0f;
constexpr int SPAWN_ATTEMPTS_PER_UPDATE = 64;
Player::Player(
@@ -82,17 +75,6 @@ void Player::updateEntity() {
"will be respawned";
eid = ENTITY_AUTO;
}
auto hitbox = getHitbox();
if (hitbox == nullptr) {
return;
}
hitbox->linearDamping = PLAYER_GROUND_DAMPING;
hitbox->verticalDamping = flight;
hitbox->gravityScale = flight ? 0.0f : 1.0f;
if (flight || !hitbox->grounded) {
hitbox->linearDamping = PLAYER_AIR_DAMPING;
}
hitbox->type = noclip ? BodyType::KINEMATIC : BodyType::DYNAMIC;
}
Hitbox* Player::getHitbox() {
@@ -102,70 +84,6 @@ Hitbox* Player::getHitbox() {
return nullptr;
}
void Player::updateInput(PlayerInput& input, float delta) {
auto hitbox = getHitbox();
if (hitbox == nullptr) {
return;
}
bool crouch = input.shift && hitbox->grounded && !input.sprint;
float speed = this->speed;
if (flight) {
speed *= FLIGHT_SPEED_MUL;
}
if (input.cheat) {
speed *= CHEAT_SPEED_MUL;
}
hitbox->crouching = crouch;
if (crouch) {
speed *= CROUCH_SPEED_MUL;
} else if (input.sprint) {
speed *= RUN_SPEED_MUL;
}
glm::vec3 dir(0, 0, 0);
if (input.moveForward) {
dir += fpCamera->dir;
}
if (input.moveBack) {
dir -= fpCamera->dir;
}
if (input.moveRight) {
dir += fpCamera->right;
}
if (input.moveLeft) {
dir -= fpCamera->right;
}
if (glm::length(dir) > 0.0f) {
dir = glm::normalize(dir);
doMove(dir, speed, delta);
}
if (flight) {
if (input.jump) {
hitbox->velocity.y += speed * delta * 9;
}
if (input.shift) {
hitbox->velocity.y -= speed * delta * 9;
}
} else if (input.jump) {
doJump();
}
}
void Player::doMove(const glm::vec3& dir, float speed, float delta) {
if (auto hitbox = getHitbox()) {
hitbox->velocity += dir * speed * delta * 9.0f;
}
}
void Player::doJump() {
if (auto hitbox = getHitbox()) {
if (hitbox->grounded) {
hitbox->velocity.y = JUMP_FORCE;
}
}
}
void Player::updateSelectedEntity() {
selectedEid = selection.entity;
}
-4
View File
@@ -59,9 +59,6 @@ class Player : public Serializable {
entityid_t eid = ENTITY_AUTO;
entityid_t selectedEid = 0;
void doMove(const glm::vec3& dir, float speed, float delta);
void doJump();
glm::vec3 rotation {};
public:
util::VecInterpolation<3, float, true> rotationInterpolation {true};
@@ -85,7 +82,6 @@ public:
void teleport(glm::vec3 position);
void updateEntity();
void updateInput(PlayerInput& input, float delta);
void updateSelectedEntity();
void postUpdate();
+1 -2
View File
@@ -6,6 +6,5 @@ Hitbox::Hitbox(BodyType type, glm::vec3 position, glm::vec3 halfsize)
: type(type),
position(position),
halfsize(halfsize),
velocity(0.0f,0.0f,0.0f),
linearDamping(0.1f)
velocity(0.0f,0.0f,0.0f)
{}
+2 -1
View File
@@ -52,7 +52,8 @@ struct Hitbox {
glm::vec3 position;
glm::vec3 halfsize;
glm::vec3 velocity;
float linearDamping;
float linearDamping = 0.5;
float friction = 1.0f;
bool verticalDamping = false;
bool grounded = false;
float gravityScale = 1.0f;
+4 -4
View File
@@ -25,7 +25,7 @@ void PhysicsSolver::step(
entityid_t entity
) {
float dt = delta / static_cast<float>(substeps);
float linearDamping = hitbox.linearDamping;
float linearDamping = hitbox.linearDamping * hitbox.friction;
float s = 2.0f/BLOCK_AABB_GRID;
const glm::vec3& half = hitbox.halfsize;
@@ -45,11 +45,11 @@ void PhysicsSolver::step(
colisionCalc(chunks, hitbox, vel, pos, half,
(prevGrounded && gravityScale > 0.0f) ? 0.5f : 0.0f);
}
vel.x *= glm::max(0.0f, 1.0f - dt * linearDamping);
vel.x /= 1.0f + dt * linearDamping;
vel.z /= 1.0f + dt * linearDamping;
if (hitbox.verticalDamping) {
vel.y *= glm::max(0.0f, 1.0f - dt * linearDamping);
vel.y /= 1.0f + dt * linearDamping;
}
vel.z *= glm::max(0.0f, 1.0f - dt * linearDamping);
pos += vel * dt + gravity * gravityScale * dt * dt * 0.5f;
if (hitbox.grounded && pos.y < py) {