Big update
This commit is contained in:
380
src/game/actor_base.cpp
Normal file
380
src/game/actor_base.cpp
Normal file
@@ -0,0 +1,380 @@
|
||||
#include "actor_base.h"
|
||||
#include "inputmanager.h"
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
REGISTER_ENTITY(ActorBase);
|
||||
|
||||
ActorBase::ActorBase()
|
||||
{
|
||||
}
|
||||
|
||||
ActorBase::~ActorBase()
|
||||
{
|
||||
}
|
||||
|
||||
void ActorBase::Update(float dt)
|
||||
{
|
||||
UpdateBodyDirty();
|
||||
|
||||
Entity::Update(dt);
|
||||
|
||||
AfterEngineStep();
|
||||
}
|
||||
|
||||
void ActorBase::AfterEngineStep()
|
||||
{
|
||||
//btTransform xform = m_rigidBody->getWorldTransform();
|
||||
//m_position = btVectorToGlm(xform.getOrigin());
|
||||
|
||||
m_position = btVectorToGlm(m_ph_motion_state.m_transform.getOrigin());
|
||||
|
||||
glm::vec3 cameraPos = m_position;
|
||||
|
||||
if (m_luaObject.IsTable())
|
||||
{
|
||||
LuaPlus::LuaObject m_camera_offset_y = m_luaObject.GetByName("m_camera_offset_y");
|
||||
if (m_camera_offset_y.IsNumber())
|
||||
cameraPos.y += (float)m_camera_offset_y.ToNumber();
|
||||
}
|
||||
|
||||
m_camera.SetPosition(cameraPos);
|
||||
}
|
||||
|
||||
void ActorBase::UpdateCameraMovement(float dt)
|
||||
{
|
||||
// calculate player movement
|
||||
float speed = 12.0f * dt;
|
||||
|
||||
uint32_t movementDir = GenMovementDir();
|
||||
|
||||
if (movementDir & EMovementDir_Forward)
|
||||
m_position += speed * m_camera.GetFront();
|
||||
if (movementDir & EMovementDir_Backward)
|
||||
m_position -= speed * m_camera.GetFront();
|
||||
if (movementDir & EMovementDir_Left)
|
||||
m_position -= glm::normalize(glm::cross(m_camera.GetFront(), m_camera.GetUp())) * speed;
|
||||
if (movementDir & EMovementDir_Right)
|
||||
m_position += glm::normalize(glm::cross(m_camera.GetFront(), m_camera.GetUp())) * speed;
|
||||
|
||||
// set position back to camera for calculation view matrix
|
||||
m_camera.SetPosition(m_position);
|
||||
}
|
||||
|
||||
void ActorBase::UpdateBodyMovement(float dt)
|
||||
{
|
||||
|
||||
glm::vec3 dir = glm::vec3(0.0f);
|
||||
|
||||
glm::vec3 camFront = m_camera.GetFront();
|
||||
camFront.y = 0.0f;
|
||||
camFront = glm::normalize(camFront);
|
||||
|
||||
// calculate player movement
|
||||
float speed = 4.f;
|
||||
|
||||
uint32_t movementDir = GenMovementDir();
|
||||
|
||||
if (movementDir & EMovementDir_Forward)
|
||||
dir += camFront;
|
||||
if (movementDir & EMovementDir_Backward)
|
||||
dir -= camFront;
|
||||
if (movementDir & EMovementDir_Left)
|
||||
dir -= glm::normalize(glm::cross(camFront, m_camera.GetUp()));
|
||||
if (movementDir & EMovementDir_Right)
|
||||
dir += glm::normalize(glm::cross(camFront, m_camera.GetUp()));
|
||||
|
||||
btVector3 currentvel = m_rigidBody->getLinearVelocity();
|
||||
glm::vec3 velocity = dir * speed;
|
||||
|
||||
if (OnGround()) {
|
||||
m_rigidBody->setLinearVelocity(btVector3(velocity.x, currentvel.y(), velocity.z));
|
||||
if (movementDir & EMovementDir_Jump) {
|
||||
m_rigidBody->setLinearVelocity(btVector3(currentvel.x(), PLAYER_PHYS_JUMPSPEEDY, currentvel.z()));
|
||||
}
|
||||
}
|
||||
else {
|
||||
float airControl = 0.2f;
|
||||
btVector3 airvel = currentvel + glmVectorToBt(dir * speed * airControl * dt);
|
||||
m_rigidBody->setLinearVelocity(btVector3(airvel.x(), currentvel.y(), airvel.z()));
|
||||
}
|
||||
|
||||
/*if (glm::length(velocity) > 0.1f && OnGround() && !(movementDir & EMovementDir_Jump))
|
||||
m_rigidBody->setLinearVelocity(glmVectorToBt(velocity));
|
||||
|
||||
if ((movementDir & EMovementDir_Jump) && OnGround()) {
|
||||
m_rigidBody->applyCentralImpulse(btVector3(0.0f, PLAYER_PHYS_JUMPSPEEDY, 0.0f));
|
||||
movementDir &= ~EMovementDir_Jump;
|
||||
}*/
|
||||
}
|
||||
|
||||
void ActorBase::UpdateCameraLook()
|
||||
{
|
||||
if (!g_inputManager.GetRelativeMouseMode())
|
||||
return;
|
||||
|
||||
glm::ivec2 mousePos = g_inputManager.GetMousePos();
|
||||
|
||||
// calculate yaw and pitch
|
||||
static float yaw = 0.0f, pitch = 0.0f;
|
||||
|
||||
int deltaX = mousePos.x;
|
||||
int deltaY = mousePos.y;
|
||||
|
||||
float sensitivity = 0.15f;
|
||||
|
||||
yaw += deltaX * sensitivity;
|
||||
pitch -= deltaY * sensitivity;
|
||||
|
||||
if (pitch > 89.0f) pitch = 89.0f;
|
||||
if (pitch < -89.0f) pitch = -89.0f;
|
||||
|
||||
m_camera.SetYawPitch(yaw, pitch);
|
||||
}
|
||||
|
||||
void ActorBase::ActivateCamera()
|
||||
{
|
||||
g_inputManager.SetRelativeMouseMode(true);
|
||||
|
||||
g_cameraManager.SetActiveCamera(&m_camera);
|
||||
}
|
||||
|
||||
void ActorBase::CreatePlayerBody(float radius, float height, float mass, float friction, float damping)
|
||||
{
|
||||
|
||||
m_shape = new btCapsuleShape(radius, height);
|
||||
|
||||
m_mass = mass;
|
||||
btVector3 local_inertia(0.0f, 0.0f, 0.0f);
|
||||
if (m_mass > 0.f) {
|
||||
m_shape->calculateLocalInertia(m_mass, local_inertia);
|
||||
}
|
||||
|
||||
btRigidBody::btRigidBodyConstructionInfo rigid_body_ci(m_mass, nullptr, m_shape, local_inertia);
|
||||
|
||||
m_rigidBody = new btRigidBody(rigid_body_ci);
|
||||
m_rigidBody->setUserPointer(this);
|
||||
m_rigidBody->setMotionState(&m_ph_motion_state);
|
||||
m_ph_motion_state.setBody(m_rigidBody);
|
||||
|
||||
// I'm sure that position is valid
|
||||
btTransform xform;
|
||||
xform.setIdentity();
|
||||
xform.setOrigin(glmVectorToBt(m_position));
|
||||
m_rigidBody->setWorldTransform(xform);
|
||||
|
||||
// ACTOR STUFF
|
||||
m_rigidBody->setAngularFactor(btVector3(0.0f, 0.0f, 0.0f));
|
||||
|
||||
m_rigidBody->setFriction(friction);
|
||||
m_rigidBody->setAnisotropicFriction(btVector3(0.0f, 0.0f, 0.0f));
|
||||
m_rigidBody->setDamping(damping, 0.0f);
|
||||
|
||||
m_rigidBody->setActivationState(DISABLE_DEACTIVATION);
|
||||
|
||||
// #TODO: body filter and mask
|
||||
g_PhysicsWorld->GetWorld()->addRigidBody(m_rigidBody);
|
||||
|
||||
m_bodyDirty = true;
|
||||
|
||||
}
|
||||
|
||||
void ActorBase::CreatePlayerBody_Old()
|
||||
{
|
||||
|
||||
m_shape = new btCapsuleShape(PLAYER_PHYS_RADIUS, PLAYER_PHYS_HEIGHT - PLAYER_PHYS_RADIUS * 2.0);
|
||||
|
||||
m_mass = 80.0f;
|
||||
btVector3 local_inertia(0.0f, 0.0f, 0.0f);
|
||||
if (m_mass > 0.f) {
|
||||
m_shape->calculateLocalInertia(m_mass, local_inertia);
|
||||
}
|
||||
|
||||
btRigidBody::btRigidBodyConstructionInfo rigid_body_ci(m_mass, nullptr, m_shape, local_inertia);
|
||||
|
||||
m_rigidBody = new btRigidBody(rigid_body_ci);
|
||||
m_rigidBody->setUserPointer(this);
|
||||
m_rigidBody->setMotionState(&m_ph_motion_state);
|
||||
m_ph_motion_state.setBody(m_rigidBody);
|
||||
|
||||
// I'm sure that position is valid
|
||||
btTransform xform;
|
||||
xform.setIdentity();
|
||||
xform.setOrigin(glmVectorToBt(m_position));
|
||||
m_rigidBody->setWorldTransform(xform);
|
||||
|
||||
// ACTOR STUFF
|
||||
m_rigidBody->setAngularFactor(btVector3(0.0f, 0.0f, 0.0f));
|
||||
|
||||
|
||||
//m_rigidBody->setFriction(2.5f);
|
||||
|
||||
m_rigidBody->setFriction(0.0f);
|
||||
m_rigidBody->setAnisotropicFriction(btVector3(0.0f, 0.0f, 0.0f));
|
||||
m_rigidBody->setDamping(0.0f, 0.0f);
|
||||
|
||||
m_rigidBody->setActivationState(DISABLE_DEACTIVATION);
|
||||
|
||||
// #TODO: body filter and mask
|
||||
g_PhysicsWorld->GetWorld()->addRigidBody(m_rigidBody);
|
||||
|
||||
m_bodyDirty = true;
|
||||
}
|
||||
|
||||
bool ActorBase::OnGround()
|
||||
{
|
||||
float rayLength = (PLAYER_PHYS_HEIGHT / 2.0f) + 0.1f;
|
||||
|
||||
btTransform xform = m_rigidBody->getWorldTransform();
|
||||
|
||||
btVector3 rayStart = xform.getOrigin();
|
||||
btVector3 rayEnd = rayStart - btVector3(0.0f, rayLength, 0.0f);
|
||||
|
||||
ClosestRayResultCallback RayResultCallback(rayStart, rayEnd, this);
|
||||
g_PhysicsWorld->GetWorld()->rayTest(rayStart, rayEnd, RayResultCallback);
|
||||
if (RayResultCallback.hasHit()) {
|
||||
btVector3 hitNormal = RayResultCallback.m_hitNormalWorld;
|
||||
if (hitNormal.y() > 0.7f) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void ActorBase::RegisterFunctions()
|
||||
{
|
||||
m_luaObject.Register("activate_camera", *this, &ActorBase::Lua_ActivateCamera);
|
||||
m_luaObject.Register("update_camera_look", *this, &ActorBase::Lua_UpdateCameraLook);
|
||||
m_luaObject.Register("update_camera_movement", *this, &ActorBase::Lua_UpdateCameraMovement);
|
||||
m_luaObject.Register("create_player_body", *this, &ActorBase::Lua_CreatePlayerBody);
|
||||
m_luaObject.Register("create_player_body_old", *this, &ActorBase::Lua_CreatePlayerBodyOld);
|
||||
m_luaObject.Register("update_body_movement", *this, &ActorBase::Lua_UpdateBodyMovement);
|
||||
m_luaObject.Register("get_action", *this, &ActorBase::Lua_GetAction);
|
||||
m_luaObject.Register("get_movement", *this, &ActorBase::Lua_GetMovement);
|
||||
m_luaObject.Register("on_ground", *this, &ActorBase::Lua_OnGround);
|
||||
|
||||
//m_luaObject.RegisterDirect("activate_camera", &ActorBase_ActivateCamera);
|
||||
//m_luaObject.RegisterDirect("update_camera_look", &ActorBase_UpdateCameraLook);
|
||||
//m_luaObject.RegisterDirect("update_camera_movement", &ActorBase_UpdateCameraMovement);
|
||||
}
|
||||
|
||||
int ActorBase::Lua_UpdateCameraMovement(LuaPlus::LuaState* state)
|
||||
{
|
||||
LuaPlus::LuaStack stack(state);
|
||||
|
||||
UpdateCameraMovement(stack[2].GetFloat());
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ActorBase::Lua_UpdateBodyMovement(LuaPlus::LuaState* state)
|
||||
{
|
||||
LuaPlus::LuaStack stack(state);
|
||||
|
||||
UpdateBodyMovement(stack[2].GetFloat());
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ActorBase::Lua_UpdateCameraLook(LuaPlus::LuaState* state)
|
||||
{
|
||||
UpdateCameraLook();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ActorBase::Lua_ActivateCamera(LuaPlus::LuaState* state)
|
||||
{
|
||||
ActivateCamera();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ActorBase::Lua_CreatePlayerBody(LuaPlus::LuaState* state)
|
||||
{
|
||||
LuaPlus::LuaStack stack(state);
|
||||
|
||||
float radius = (float)stack[2].GetNumber();
|
||||
float height = (float)stack[3].GetNumber();
|
||||
float mass = (float)stack[4].GetNumber();
|
||||
float friction = (float)stack[5].GetNumber();
|
||||
float damping = (float)stack[6].GetNumber();
|
||||
|
||||
CreatePlayerBody(radius, height, mass, friction, damping);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ActorBase::Lua_CreatePlayerBodyOld(LuaPlus::LuaState* state)
|
||||
{
|
||||
CreatePlayerBody_Old();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ActorBase::Lua_GetAction(LuaPlus::LuaState* state)
|
||||
{
|
||||
int action = GenAction();
|
||||
state->PushInteger(action);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int ActorBase::Lua_GetMovement(LuaPlus::LuaState* state)
|
||||
{
|
||||
uint32_t movement = GenMovementDir();
|
||||
state->PushInteger(movement);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int ActorBase::Lua_OnGround(LuaPlus::LuaState* state)
|
||||
{
|
||||
state->PushBoolean(OnGround());
|
||||
return 1;
|
||||
}
|
||||
|
||||
int ActorBase::GenAction()
|
||||
{
|
||||
int action = -1;
|
||||
|
||||
if (!g_inputManager.GetRelativeMouseMode())
|
||||
return action;
|
||||
|
||||
if (g_inputManager.GetMouse().IsKeyDown(EMouseButton_Left))
|
||||
action = 0;
|
||||
else if (g_inputManager.GetMouse().IsKeyDown(EMouseButton_Right))
|
||||
action = 1;
|
||||
else if (g_inputManager.GetKeyboard().IsKeyDown(SDLK_R))
|
||||
action = 2;
|
||||
|
||||
return action;
|
||||
}
|
||||
|
||||
uint32_t ActorBase::GenMovementDir()
|
||||
{
|
||||
uint32_t movementDir = EMovementDir_None;
|
||||
|
||||
if (!g_inputManager.GetRelativeMouseMode())
|
||||
return movementDir;
|
||||
|
||||
if (g_inputManager.GetKeyboard().IsKeyDown(SDLK_W)) {
|
||||
movementDir |= EMovementDir_Forward;
|
||||
}
|
||||
|
||||
if (g_inputManager.GetKeyboard().IsKeyDown(SDLK_S)) {
|
||||
movementDir |= EMovementDir_Backward;
|
||||
}
|
||||
|
||||
if (g_inputManager.GetKeyboard().IsKeyDown(SDLK_A)) {
|
||||
movementDir |= EMovementDir_Left;
|
||||
}
|
||||
|
||||
if (g_inputManager.GetKeyboard().IsKeyDown(SDLK_D)) {
|
||||
movementDir |= EMovementDir_Right;
|
||||
}
|
||||
|
||||
if (g_inputManager.GetKeyboard().IsKeyDown(SDLK_SPACE)) {
|
||||
movementDir |= EMovementDir_Jump;
|
||||
}
|
||||
|
||||
return movementDir;
|
||||
}
|
||||
65
src/game/actor_base.h
Normal file
65
src/game/actor_base.h
Normal file
@@ -0,0 +1,65 @@
|
||||
#ifndef ACTOR_BASE_H
|
||||
#define ACTOR_BASE_H
|
||||
|
||||
#include "game_object.h"
|
||||
|
||||
#define PLAYER_PHYS_MASS 80.0
|
||||
#define PLAYER_PHYS_RADIUS 0.40
|
||||
#define PLAYER_PHYS_HEIGHT 1.79
|
||||
#define PLAYER_PHYS_JUMPDIST PLAYER_PHYS_RADIUS
|
||||
#define PLAYER_PHYS_JUMPHEIGHT 2.0
|
||||
#define PLAYER_PHYS_JUMPSPEEDY 5.0
|
||||
#define PLAYER_PHYS_WALK_SPEED ( 5.5 )
|
||||
#define PLAYER_PHYS_RUN_SPEED_MUL 1.4
|
||||
#define PLAYER_PHYS_MOVE_SPEED_EXP 1.0
|
||||
#define PLAYER_PHYS_FLY_SPEED_EXP 4.0
|
||||
|
||||
class ActorBase : public Entity
|
||||
{
|
||||
public:
|
||||
ActorBase();
|
||||
~ActorBase();
|
||||
|
||||
virtual void Update(float dt);
|
||||
|
||||
void AfterEngineStep();
|
||||
|
||||
void UpdateCameraMovement(float dt);
|
||||
|
||||
void UpdateBodyMovement(float dt);
|
||||
|
||||
void UpdateCameraLook();
|
||||
|
||||
void ActivateCamera();
|
||||
|
||||
void CreatePlayerBody(float radius, float height, float mass, float friction, float damping);
|
||||
void CreatePlayerBody_Old();
|
||||
|
||||
bool OnGround();
|
||||
|
||||
// Lua bindings
|
||||
|
||||
virtual void RegisterFunctions();
|
||||
|
||||
int Lua_UpdateCameraMovement(LuaPlus::LuaState* state);
|
||||
int Lua_UpdateBodyMovement(LuaPlus::LuaState* state);
|
||||
int Lua_UpdateCameraLook(LuaPlus::LuaState* state);
|
||||
int Lua_ActivateCamera(LuaPlus::LuaState* state);
|
||||
int Lua_CreatePlayerBody(LuaPlus::LuaState* state);
|
||||
int Lua_CreatePlayerBodyOld(LuaPlus::LuaState* state);
|
||||
int Lua_GetAction(LuaPlus::LuaState* state);
|
||||
int Lua_GetMovement(LuaPlus::LuaState* state);
|
||||
int Lua_OnGround(LuaPlus::LuaState* state);
|
||||
|
||||
private:
|
||||
int GenAction();
|
||||
uint32_t GenMovementDir();
|
||||
|
||||
private:
|
||||
Camera m_camera;
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif // !ACTOR_BASE_H
|
||||
@@ -1,8 +1,11 @@
|
||||
#include "ifilesystem.h"
|
||||
#include "core.h"
|
||||
#include "log.h"
|
||||
#include "engine.h"
|
||||
#include "game.h"
|
||||
#include "game_lua_help.h"
|
||||
#include "game_ui.h"
|
||||
#include "inputmanager.h"
|
||||
#include "ientity.h"
|
||||
#include "entitymanager.h"
|
||||
#include "world.h"
|
||||
@@ -105,6 +108,129 @@ void engineAddEntityToWorld(LuaPlus::LuaObject& object)
|
||||
g_world->AddEntity(entity);
|
||||
}
|
||||
|
||||
LuaPlus::LuaObject engineTraceRay(float rayBeginX, float rayBeginY, float rayBeginZ,
|
||||
float rayEndX, float rayEndY, float rayEndZ, const LuaPlus::LuaObject& ignoreTable)
|
||||
{
|
||||
IEntityBase* pIgnoreEntity = nullptr;
|
||||
if (ignoreTable.IsTable())
|
||||
{
|
||||
LuaPlus::LuaObject ignoreEntityTable = ignoreTable.GetByName("__object");
|
||||
if (ignoreEntityTable.IsLightUserdata())
|
||||
pIgnoreEntity = (IEntityBase*)ignoreEntityTable.GetLightUserdata();
|
||||
}
|
||||
|
||||
TraceRayResult result = {};
|
||||
glm::vec3 rayBegin = glm::vec3(rayBeginX, rayBeginY, rayBeginZ);
|
||||
glm::vec3 rayEnd = glm::vec3(rayEndX, rayEndY, rayEndZ);
|
||||
|
||||
if (g_PhysicsWorld)
|
||||
g_PhysicsWorld->TraceRay(result, rayBegin, rayEnd, pIgnoreEntity);
|
||||
else
|
||||
Logger::Msg("engine.trace_ray(): no server started or game loaded!");
|
||||
|
||||
LuaPlus::LuaObject resultTable = GetLuaState().CreateTable();
|
||||
resultTable.SetNumber("pos_x", result.position.x);
|
||||
resultTable.SetNumber("pos_y", result.position.y);
|
||||
resultTable.SetNumber("pos_z", result.position.z);
|
||||
resultTable.SetNumber("normal_x", result.normal.x);
|
||||
resultTable.SetNumber("normal_y", result.normal.y);
|
||||
resultTable.SetNumber("normal_z", result.normal.z);
|
||||
resultTable.SetInteger("entity_id", result.pEntity ? result.pEntity->GetID() : -1);
|
||||
resultTable.SetInteger("hit", result.hit);
|
||||
return resultTable;
|
||||
}
|
||||
|
||||
float engineGetDelta()
|
||||
{
|
||||
return GetEngine()->GetDelta();
|
||||
}
|
||||
|
||||
void registerEngine()
|
||||
{
|
||||
using namespace LuaPlus;
|
||||
|
||||
// register engine functions
|
||||
LuaObject engineTable = GetLuaState().GetGlobals().CreateTable("engine");
|
||||
engineTable.RegisterDirect("error", &engineError);
|
||||
engineTable.RegisterDirect("warning", &engineWarning);
|
||||
engineTable.RegisterDirect("create_entity", &engineCreateEntity);
|
||||
engineTable.RegisterDirect("add_entity_to_world", &engineAddEntityToWorld);
|
||||
engineTable.RegisterDirect("get_entity_from_id", &engineGetEntityFromID);
|
||||
engineTable.RegisterDirect("play_sound", &enginePlaySound);
|
||||
engineTable.RegisterDirect("get_delta", &engineGetDelta);
|
||||
engineTable.RegisterDirect("trace_ray", &engineTraceRay);
|
||||
|
||||
LuaObject consoleTable = GetLuaState().GetGlobals().CreateTable("console");
|
||||
consoleTable.RegisterDirect("print", &consoleMsg);
|
||||
|
||||
registerCamera();
|
||||
|
||||
registerInput();
|
||||
|
||||
registerEngineUI();
|
||||
|
||||
// action globals
|
||||
GetLuaState().DoString("ACTION_FIRE = 0");
|
||||
GetLuaState().DoString("ACTION_ALT_FIRE = 1");
|
||||
GetLuaState().DoString("ACTION_RELOAD = 2");
|
||||
GetLuaState().DoString("ACTION_USE = 3");
|
||||
|
||||
// animations globals
|
||||
GetLuaState().DoString("ANIM_PLAYBACK_NONE = 0");
|
||||
GetLuaState().DoString("ANIM_PLAYBACK_REPEAT = 1");
|
||||
|
||||
|
||||
char buffer[64];
|
||||
|
||||
#define REGISTER_CONSTANT(constant) \
|
||||
snprintf(buffer, sizeof(buffer), #constant" = %d", constant); \
|
||||
GetLuaState().DoString(buffer)
|
||||
|
||||
REGISTER_CONSTANT(EMovementDir_None);
|
||||
REGISTER_CONSTANT(EMovementDir_Forward);
|
||||
REGISTER_CONSTANT(EMovementDir_Backward);
|
||||
REGISTER_CONSTANT(EMovementDir_Left);
|
||||
REGISTER_CONSTANT(EMovementDir_Right);
|
||||
REGISTER_CONSTANT(EMovementDir_Jump);
|
||||
|
||||
#undef REGISTER_CONSTANT
|
||||
}
|
||||
|
||||
int inputGetMousePos(LuaPlus::LuaState* state)
|
||||
{
|
||||
const glm::vec2& pos = g_inputManager.GetMousePos();
|
||||
state->PushNumber(pos.x);
|
||||
state->PushNumber(pos.y);
|
||||
return 2;
|
||||
}
|
||||
|
||||
int inputGetDeltaMousePos(LuaPlus::LuaState* state)
|
||||
{
|
||||
const glm::vec2& pos = g_inputManager.GetMouseDeltaPos();
|
||||
state->PushNumber(pos.x);
|
||||
state->PushNumber(pos.y);
|
||||
return 2;
|
||||
}
|
||||
|
||||
void inputLock(bool value)
|
||||
{
|
||||
g_inputManager.SetRelativeMouseMode(value);
|
||||
}
|
||||
|
||||
bool inputGetLock()
|
||||
{
|
||||
return g_inputManager.GetRelativeMouseMode();
|
||||
}
|
||||
|
||||
void registerInput()
|
||||
{
|
||||
LuaPlus::LuaObject inputTable = GetLuaState().GetGlobals().CreateTable("input");
|
||||
inputTable.Register("get_mouse_pos", &inputGetMousePos);
|
||||
inputTable.Register("get_delta_mouse_pos", &inputGetDeltaMousePos);
|
||||
inputTable.RegisterDirect("lock_mouse", &inputLock);
|
||||
inputTable.RegisterDirect("get_lock_mouse", &inputGetLock);
|
||||
}
|
||||
|
||||
int cameraGetPos(LuaPlus::LuaState* state)
|
||||
{
|
||||
glm::vec3 v = glm::vec3(0.0f);
|
||||
@@ -191,39 +317,23 @@ int cameraGetPitch(LuaPlus::LuaState* state)
|
||||
return 1;
|
||||
}
|
||||
|
||||
void registerEngine()
|
||||
void cameraSetYawPitch(float yaw, float pitch)
|
||||
{
|
||||
using namespace LuaPlus;
|
||||
Camera* camera = g_cameraManager.GetActiveCamera();
|
||||
if (camera)
|
||||
camera->SetYawPitch(yaw, pitch);
|
||||
}
|
||||
|
||||
// register engine functions
|
||||
LuaObject engineTable = GetLuaState().GetGlobals().CreateTable("engine");
|
||||
engineTable.RegisterDirect("error", &engineError);
|
||||
engineTable.RegisterDirect("warning", &engineWarning);
|
||||
engineTable.RegisterDirect("create_entity", &engineCreateEntity);
|
||||
engineTable.RegisterDirect("add_entity_to_world", &engineAddEntityToWorld);
|
||||
engineTable.RegisterDirect("get_entity_from_id", &engineGetEntityFromID);
|
||||
engineTable.RegisterDirect("play_sound", &enginePlaySound);
|
||||
|
||||
LuaObject consoleTable = GetLuaState().GetGlobals().CreateTable("console");
|
||||
consoleTable.RegisterDirect("print", &consoleMsg);
|
||||
|
||||
LuaObject cameraTable = GetLuaState().GetGlobals().CreateTable("camera");
|
||||
void registerCamera()
|
||||
{
|
||||
LuaPlus::LuaObject cameraTable = GetLuaState().GetGlobals().CreateTable("camera");
|
||||
cameraTable.Register("get_position", &cameraGetPos);
|
||||
cameraTable.Register("get_front", &cameraGetFront);
|
||||
cameraTable.Register("get_right", &cameraGetRight);
|
||||
cameraTable.Register("get_up", &cameraGetUp);
|
||||
cameraTable.Register("get_yaw", &cameraGetYaw);
|
||||
cameraTable.Register("get_pitch", &cameraGetPitch);
|
||||
|
||||
// action globals
|
||||
GetLuaState().DoString("ACTION_FIRE = 0");
|
||||
GetLuaState().DoString("ACTION_ALT_FIRE = 1");
|
||||
GetLuaState().DoString("ACTION_RELOAD = 2");
|
||||
|
||||
// animations globals
|
||||
GetLuaState().DoString("ANIM_PLAYBACK_NONE = 0");
|
||||
GetLuaState().DoString("ANIM_PLAYBACK_REPEAT = 1");
|
||||
|
||||
cameraTable.RegisterDirect("set_yaw_pitch", &cameraSetYawPitch);
|
||||
}
|
||||
|
||||
void registerClasses()
|
||||
@@ -477,6 +587,11 @@ void Game::Shutdown()
|
||||
{
|
||||
}
|
||||
|
||||
void Game::Render2D()
|
||||
{
|
||||
gameRenderUI();
|
||||
}
|
||||
|
||||
//LuaPrototype* pluaprototype = Lua_FindPrototype(classname);
|
||||
//
|
||||
//if (pluaprototype)
|
||||
|
||||
@@ -18,8 +18,13 @@ public:
|
||||
|
||||
void Shutdown();
|
||||
|
||||
void Render2D();
|
||||
};
|
||||
|
||||
extern Game* g_game;
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
||||
void registerCamera();
|
||||
|
||||
void registerInput();
|
||||
|
||||
@@ -1,68 +1,9 @@
|
||||
#include "core.h"
|
||||
#include "inputmanager.h"
|
||||
#include "debugrender.h"
|
||||
#include "game_object.h"
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#define PLAYER_PHYS_MASS 80.0
|
||||
#define PLAYER_PHYS_RADIUS 0.40
|
||||
#define PLAYER_PHYS_HEIGHT 1.79
|
||||
#define PLAYER_PHYS_JUMPDIST PLAYER_PHYS_RADIUS
|
||||
#define PLAYER_PHYS_JUMPHEIGHT 2.0
|
||||
#define PLAYER_PHYS_JUMPSPEEDY 5.0
|
||||
#define PLAYER_PHYS_WALK_SPEED ( 5.5 )
|
||||
#define PLAYER_PHYS_RUN_SPEED_MUL 1.4
|
||||
#define PLAYER_PHYS_MOVE_SPEED_EXP 1.0
|
||||
#define PLAYER_PHYS_FLY_SPEED_EXP 4.0
|
||||
|
||||
// Lua wrappers
|
||||
void Entity_LoadModel(LuaPlus::LuaState* state)
|
||||
{
|
||||
LuaPlus::LuaStack stack(state);
|
||||
|
||||
Entity* entity = (Entity*)stack[1].GetByName("__object").GetLightUserdata();
|
||||
entity->LoadModel(stack[2].GetString());
|
||||
}
|
||||
|
||||
void Entity_SetVisible(LuaPlus::LuaState* state)
|
||||
{
|
||||
LuaPlus::LuaStack stack(state);
|
||||
|
||||
Entity* entity = (Entity*)stack[1].GetByName("__object").GetLightUserdata();
|
||||
entity->SetVisible(stack[2].GetBoolean());
|
||||
}
|
||||
|
||||
bool Entity_GetVisible(LuaPlus::LuaState* state)
|
||||
{
|
||||
LuaPlus::LuaStack stack(state);
|
||||
|
||||
Entity* entity = (Entity*)stack[1].GetByName("__object").GetLightUserdata();
|
||||
return entity->GetVisible();
|
||||
}
|
||||
|
||||
void ActorBase_UpdateCameraMovement(LuaPlus::LuaState* state)
|
||||
{
|
||||
LuaPlus::LuaStack stack(state);
|
||||
|
||||
ActorBase* entity = (ActorBase*)stack[1].GetByName("__object").GetLightUserdata();
|
||||
entity->UpdateCameraMovement(stack[2].GetFloat());
|
||||
}
|
||||
|
||||
void ActorBase_UpdateCameraLook(LuaPlus::LuaState* state)
|
||||
{
|
||||
LuaPlus::LuaStack stack(state);
|
||||
|
||||
ActorBase* entity = (ActorBase*)stack[1].GetByName("__object").GetLightUserdata();
|
||||
entity->UpdateCameraLook();
|
||||
}
|
||||
|
||||
void ActorBase_ActivateCamera(LuaPlus::LuaState* state)
|
||||
{
|
||||
LuaPlus::LuaStack stack(state);
|
||||
|
||||
ActorBase* entity = (ActorBase*)stack[1].GetByName("__object").GetLightUserdata();
|
||||
entity->ActivateCamera();
|
||||
}
|
||||
|
||||
REGISTER_ENTITY(Entity);
|
||||
|
||||
Entity::Entity() :
|
||||
@@ -96,6 +37,20 @@ Entity::~Entity()
|
||||
|
||||
m_luaObject.AssignNil();
|
||||
}
|
||||
|
||||
if (m_rigidBody) {
|
||||
g_PhysicsWorld->GetWorld()->removeRigidBody(m_rigidBody);
|
||||
|
||||
delete m_rigidBody;
|
||||
m_rigidBody = nullptr;
|
||||
|
||||
m_ph_motion_state.setBody(nullptr);
|
||||
}
|
||||
|
||||
if (m_shape) {
|
||||
delete m_shape;
|
||||
m_shape = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void Entity::Update(float dt)
|
||||
@@ -113,16 +68,20 @@ void Entity::Update(float dt)
|
||||
}
|
||||
}
|
||||
|
||||
bool g_debugEntityDraw = false;
|
||||
|
||||
void Entity::Render()
|
||||
{
|
||||
if (m_model)
|
||||
{
|
||||
m_model->Draw(GetWorldTransform(), m_skeleton);
|
||||
|
||||
/*BoundingBox bbox = m_boundingBox;
|
||||
|
||||
|
||||
if (g_debugEntityDraw)
|
||||
{
|
||||
BoundingBox bbox = m_boundingBox;
|
||||
bbox.TransformAABB(GetWorldTransform());
|
||||
|
||||
g_debugRender->DrawBoundingBox(bbox, glm::vec3(1.0f, 0.0f, 0.0f));*/
|
||||
g_debugRender->DrawBoundingBox(bbox, glm::vec3(1.0f, 1.0f, 1.0f));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -210,10 +169,7 @@ void Entity::InitFromTable(LuaPlus::LuaObject& _object)
|
||||
m_luaObject = _object;
|
||||
|
||||
// find functions
|
||||
m_onInitFunction = m_luaObject.GetByName("on_init");
|
||||
m_onShutdownFunction = m_luaObject.GetByName("on_shutdown");
|
||||
m_onUpdateFunction = m_luaObject.GetByName("on_update");
|
||||
m_onCollideFunction = m_luaObject.GetByName("on_collide");
|
||||
InitLuaCallbacks();
|
||||
|
||||
// check
|
||||
//SDL_assert_always(m_onInitFunction.IsFunction() || m_onShutdownFunction.IsFunction() || m_onUpdateFunction.IsFunction());
|
||||
@@ -228,6 +184,14 @@ void Entity::InitFromTable(LuaPlus::LuaObject& _object)
|
||||
m_luaObject.SetLightUserdata("__object", this);
|
||||
}
|
||||
|
||||
void Entity::InitLuaCallbacks()
|
||||
{
|
||||
m_onInitFunction = m_luaObject.GetByName("on_init");
|
||||
m_onShutdownFunction = m_luaObject.GetByName("on_shutdown");
|
||||
m_onUpdateFunction = m_luaObject.GetByName("on_update");
|
||||
m_onCollideFunction = m_luaObject.GetByName("on_collide");
|
||||
}
|
||||
|
||||
void Entity::RegisterBaseFunctions()
|
||||
{
|
||||
m_luaObject.Register("load_model", *this, &Entity::Lua_LoadModel);
|
||||
@@ -236,13 +200,21 @@ void Entity::RegisterBaseFunctions()
|
||||
m_luaObject.Register("set_position", *this, &Entity::Lua_SetPosition);
|
||||
m_luaObject.Register("get_position", *this, &Entity::Lua_GetPosition);
|
||||
m_luaObject.Register("set_rotation", *this, &Entity::Lua_SetRotation);
|
||||
m_luaObject.Register("get_rotation", *this, &Entity::Lua_GetRotation);
|
||||
m_luaObject.Register("set_rotation_from_vectors", *this, &Entity::Lua_SetRotationFromVectors);
|
||||
m_luaObject.Register("get_classname", *this, &Entity::Lua_GetClassname);
|
||||
m_luaObject.Register("get_id", *this, &Entity::Lua_GetID);
|
||||
m_luaObject.Register("mark_for_delete", *this, &Entity::Lua_MarkForDelete);
|
||||
|
||||
// physics
|
||||
m_luaObject.Register("set_velocity", *this, &Entity::Lua_SetVelocity);
|
||||
m_luaObject.Register("get_velocity", *this, &Entity::Lua_GetVelocity);
|
||||
m_luaObject.Register("has_rigid_body", *this, &Entity::Lua_HasRigidBody);
|
||||
|
||||
// animation
|
||||
m_luaObject.Register("find_animation", *this, &Entity::Lua_FindAnimation);
|
||||
m_luaObject.Register("play_animation", *this, &Entity::Lua_PlayAnimation);
|
||||
m_luaObject.Register("stop_animation", *this, &Entity::Lua_StopAnimation);
|
||||
m_luaObject.Register("get_current_animation", *this, &Entity::Lua_GetCurrentAnimation);
|
||||
m_luaObject.Register("get_current_animation_time", *this, &Entity::Lua_GetCurrentAnimationTime);
|
||||
m_luaObject.Register("get_animation_time", *this, &Entity::Lua_GetAnimationTime);
|
||||
@@ -299,6 +271,12 @@ void Entity::Help_SetRotationFromVectors(const glm::vec3& front, const glm::vec3
|
||||
m_rotation = glm::degrees(glm::eulerAngles(q));
|
||||
}
|
||||
|
||||
void Entity::Help_SetVelocity(float x, float y, float z)
|
||||
{
|
||||
if (m_rigidBody)
|
||||
m_rigidBody->setLinearVelocity(btVector3(x, y, z));
|
||||
}
|
||||
|
||||
int Entity::Lua_LoadModel(LuaPlus::LuaState* state)
|
||||
{
|
||||
LuaPlus::LuaStack stack(state);
|
||||
@@ -349,6 +327,14 @@ int Entity::Lua_GetPosition(LuaPlus::LuaState* state)
|
||||
return 3;
|
||||
}
|
||||
|
||||
int Entity::Lua_GetRotation(LuaPlus::LuaState* state)
|
||||
{
|
||||
state->PushNumber(m_rotation.x);
|
||||
state->PushNumber(m_rotation.y);
|
||||
state->PushNumber(m_rotation.z);
|
||||
return 3;
|
||||
}
|
||||
|
||||
int Entity::Lua_SetRotation(LuaPlus::LuaState* state)
|
||||
{
|
||||
LuaPlus::LuaStack stack(state);
|
||||
@@ -405,11 +391,58 @@ int Entity::Lua_UpdateTransform(LuaPlus::LuaState* state)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int Entity::Lua_MarkForDelete(LuaPlus::LuaState* state)
|
||||
{
|
||||
MarkForDelete();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int Entity::Lua_SetVelocity(LuaPlus::LuaState* state)
|
||||
{
|
||||
LuaPlus::LuaStack stack(state);
|
||||
|
||||
float x = stack[2].GetNumber();
|
||||
float y = stack[3].GetNumber();
|
||||
float z = stack[4].GetNumber();
|
||||
|
||||
Help_SetVelocity(x, y, z);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int Entity::Lua_GetVelocity(LuaPlus::LuaState* state)
|
||||
{
|
||||
glm::vec3 velocity = glm::vec3(0.0f);
|
||||
if (m_rigidBody)
|
||||
{
|
||||
velocity = btVectorToGlm(m_rigidBody->getLinearVelocity());
|
||||
}
|
||||
|
||||
state->PushNumber(velocity.x);
|
||||
state->PushNumber(velocity.y);
|
||||
state->PushNumber(velocity.z);
|
||||
|
||||
return 3;
|
||||
}
|
||||
|
||||
int Entity::Lua_HasRigidBody(LuaPlus::LuaState* state)
|
||||
{
|
||||
state->PushBoolean(!!m_rigidBody);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int Entity::Lua_FindAnimation(LuaPlus::LuaState* state)
|
||||
{
|
||||
LuaPlus::LuaStack stack(state);
|
||||
const char* name = stack[2].GetString();
|
||||
|
||||
if (!stack[2].IsString())
|
||||
{
|
||||
Core::Warning("load_model: first argument is not an string");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (m_model)
|
||||
state->PushInteger(m_model->FindAnimation(name));
|
||||
else
|
||||
@@ -421,6 +454,13 @@ int Entity::Lua_FindAnimation(LuaPlus::LuaState* state)
|
||||
int Entity::Lua_PlayAnimation(LuaPlus::LuaState* state)
|
||||
{
|
||||
LuaPlus::LuaStack stack(state);
|
||||
|
||||
if (!stack[2].IsNumber())
|
||||
{
|
||||
Core::Warning("play_animation: first argument is not an number");
|
||||
return 0;
|
||||
}
|
||||
|
||||
AnimationId_t id = stack[2].GetInteger();
|
||||
int mode = stack[3].GetInteger();
|
||||
|
||||
@@ -430,6 +470,14 @@ int Entity::Lua_PlayAnimation(LuaPlus::LuaState* state)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int Entity::Lua_StopAnimation(LuaPlus::LuaState* state)
|
||||
{
|
||||
if (m_skeleton)
|
||||
m_skeleton->StopAnimation();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int Entity::Lua_GetCurrentAnimation(LuaPlus::LuaState* state)
|
||||
{
|
||||
if (m_skeleton)
|
||||
@@ -517,336 +565,6 @@ void Entity::UpdateBodyDirty()
|
||||
}
|
||||
}
|
||||
|
||||
REGISTER_ENTITY(ActorBase);
|
||||
|
||||
ActorBase::ActorBase()
|
||||
{
|
||||
}
|
||||
|
||||
ActorBase::~ActorBase()
|
||||
{
|
||||
if (m_rigidBody) {
|
||||
g_PhysicsWorld->GetWorld()->removeRigidBody(m_rigidBody);
|
||||
|
||||
delete m_rigidBody;
|
||||
m_rigidBody = nullptr;
|
||||
|
||||
m_ph_motion_state.setBody(nullptr);
|
||||
}
|
||||
|
||||
if (m_shape) {
|
||||
delete m_shape;
|
||||
m_shape = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void ActorBase::Update(float dt)
|
||||
{
|
||||
static bool s_test = true;
|
||||
|
||||
if (s_test) {
|
||||
UpdateBodyDirty();
|
||||
Entity::Update(dt);
|
||||
AfterEngineStep();
|
||||
} else {
|
||||
ActivateCamera();
|
||||
|
||||
UpdateCameraLook();
|
||||
|
||||
UpdateCameraMovement(dt);
|
||||
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void ActorBase::AfterEngineStep()
|
||||
{
|
||||
//btTransform xform = m_rigidBody->getWorldTransform();
|
||||
//m_position = btVectorToGlm(xform.getOrigin());
|
||||
|
||||
m_position = btVectorToGlm(m_ph_motion_state.m_transform.getOrigin());
|
||||
|
||||
glm::vec3 cameraPos = m_position;
|
||||
|
||||
if (m_luaObject.IsTable())
|
||||
{
|
||||
LuaPlus::LuaObject m_camera_offset_y = m_luaObject.GetByName("m_camera_offset_y");
|
||||
if (m_camera_offset_y.IsNumber())
|
||||
cameraPos.y += m_camera_offset_y.ToNumber();
|
||||
}
|
||||
|
||||
m_camera.SetPosition(cameraPos);
|
||||
}
|
||||
|
||||
void ActorBase::UpdateCameraMovement(float dt)
|
||||
{
|
||||
// calculate player movement
|
||||
float speed = 12.0f * dt;
|
||||
|
||||
uint32_t movementDir = GenMovementDir();
|
||||
|
||||
if (movementDir & EMovementDir_Forward)
|
||||
m_position += speed * m_camera.GetFront();
|
||||
if (movementDir & EMovementDir_Backward)
|
||||
m_position -= speed * m_camera.GetFront();
|
||||
if (movementDir & EMovementDir_Left)
|
||||
m_position -= glm::normalize(glm::cross(m_camera.GetFront(), m_camera.GetUp())) * speed;
|
||||
if (movementDir & EMovementDir_Right)
|
||||
m_position += glm::normalize(glm::cross(m_camera.GetFront(), m_camera.GetUp())) * speed;
|
||||
|
||||
// set position back to camera for calculation view matrix
|
||||
m_camera.SetPosition(m_position);
|
||||
}
|
||||
|
||||
void ActorBase::UpdateBodyMovement(float dt)
|
||||
{
|
||||
|
||||
glm::vec3 dir = glm::vec3(0.0f);
|
||||
|
||||
glm::vec3 camFront = m_camera.GetFront();
|
||||
camFront.y = 0.0f;
|
||||
camFront = glm::normalize(camFront);
|
||||
|
||||
// calculate player movement
|
||||
float speed = 4.f;
|
||||
|
||||
uint32_t movementDir = GenMovementDir();
|
||||
|
||||
if (movementDir & EMovementDir_Forward)
|
||||
dir += camFront;
|
||||
if (movementDir & EMovementDir_Backward)
|
||||
dir -= camFront;
|
||||
if (movementDir & EMovementDir_Left)
|
||||
dir -= glm::normalize(glm::cross(camFront, m_camera.GetUp()));
|
||||
if (movementDir & EMovementDir_Right)
|
||||
dir += glm::normalize(glm::cross(camFront, m_camera.GetUp()));
|
||||
|
||||
btVector3 currentvel = m_rigidBody->getLinearVelocity();
|
||||
glm::vec3 velocity = dir * speed;
|
||||
|
||||
if (OnGround()) {
|
||||
m_rigidBody->setLinearVelocity(btVector3(velocity.x, currentvel.y(), velocity.z));
|
||||
if (movementDir & EMovementDir_Jump) {
|
||||
m_rigidBody->setLinearVelocity(btVector3(currentvel.x(), PLAYER_PHYS_JUMPSPEEDY, currentvel.z()));
|
||||
}
|
||||
} else {
|
||||
float airControl = 0.2f;
|
||||
btVector3 airvel = currentvel + glmVectorToBt(dir * speed * airControl * dt);
|
||||
m_rigidBody->setLinearVelocity(btVector3(airvel.x(), currentvel.y(), airvel.z()));
|
||||
}
|
||||
|
||||
/*if (glm::length(velocity) > 0.1f && OnGround() && !(movementDir & EMovementDir_Jump))
|
||||
m_rigidBody->setLinearVelocity(glmVectorToBt(velocity));
|
||||
|
||||
if ((movementDir & EMovementDir_Jump) && OnGround()) {
|
||||
m_rigidBody->applyCentralImpulse(btVector3(0.0f, PLAYER_PHYS_JUMPSPEEDY, 0.0f));
|
||||
movementDir &= ~EMovementDir_Jump;
|
||||
}*/
|
||||
}
|
||||
|
||||
void ActorBase::UpdateCameraLook()
|
||||
{
|
||||
glm::ivec2 mousePos = g_inputManager.GetMousePos();
|
||||
|
||||
// calculate yaw and pitch
|
||||
static float yaw = 0.0f, pitch = 0.0f;
|
||||
|
||||
int deltaX = mousePos.x;
|
||||
int deltaY = mousePos.y;
|
||||
|
||||
float sensitivity = 0.15f;
|
||||
|
||||
yaw += deltaX * sensitivity;
|
||||
pitch -= deltaY * sensitivity;
|
||||
|
||||
if (pitch > 89.0f) pitch = 89.0f;
|
||||
if (pitch < -89.0f) pitch = -89.0f;
|
||||
|
||||
m_camera.SetYawPitch(yaw, pitch);
|
||||
}
|
||||
|
||||
void ActorBase::ActivateCamera()
|
||||
{
|
||||
g_inputManager.SetRelativeMouseMode(true);
|
||||
|
||||
g_cameraManager.SetActiveCamera(&m_camera);
|
||||
}
|
||||
|
||||
void ActorBase::CreatePlayerBody()
|
||||
{
|
||||
|
||||
m_shape = new btCapsuleShape(PLAYER_PHYS_RADIUS, PLAYER_PHYS_HEIGHT - PLAYER_PHYS_RADIUS * 2.0);
|
||||
|
||||
m_mass = 80.0f;
|
||||
btVector3 local_inertia(0.0f, 0.0f, 0.0f);
|
||||
if (m_mass > 0.f) {
|
||||
m_shape->calculateLocalInertia(m_mass, local_inertia);
|
||||
}
|
||||
|
||||
btRigidBody::btRigidBodyConstructionInfo rigid_body_ci(m_mass, nullptr, m_shape, local_inertia);
|
||||
|
||||
m_rigidBody = new btRigidBody(rigid_body_ci);
|
||||
m_rigidBody->setUserPointer(this);
|
||||
m_rigidBody->setMotionState(&m_ph_motion_state);
|
||||
m_ph_motion_state.setBody(m_rigidBody);
|
||||
|
||||
// I'm sure that position is valid
|
||||
btTransform xform;
|
||||
xform.setIdentity();
|
||||
xform.setOrigin(glmVectorToBt(m_position));
|
||||
m_rigidBody->setWorldTransform(xform);
|
||||
|
||||
// ACTOR STUFF
|
||||
m_rigidBody->setAngularFactor(btVector3(0.0f, 0.0f, 0.0f));
|
||||
|
||||
|
||||
//m_rigidBody->setFriction(2.5f);
|
||||
|
||||
m_rigidBody->setFriction(0.0f);
|
||||
m_rigidBody->setAnisotropicFriction(btVector3(0.0f, 0.0f, 0.0f));
|
||||
m_rigidBody->setDamping(0.0f, 0.0f);
|
||||
|
||||
m_rigidBody->setActivationState(DISABLE_DEACTIVATION);
|
||||
|
||||
// #TODO: body filter and mask
|
||||
g_PhysicsWorld->GetWorld()->addRigidBody(m_rigidBody);
|
||||
|
||||
m_bodyDirty = true;
|
||||
}
|
||||
|
||||
bool ActorBase::OnGround()
|
||||
{
|
||||
float rayLength = (PLAYER_PHYS_HEIGHT / 2.0f) + 0.1f;
|
||||
|
||||
btTransform xform = m_rigidBody->getWorldTransform();
|
||||
|
||||
btVector3 rayStart = xform.getOrigin();
|
||||
btVector3 rayEnd = rayStart - btVector3(0.0f, rayLength, 0.0f);
|
||||
|
||||
ClosestRayResultCallback RayResultCallback(rayStart, rayEnd, this);
|
||||
g_PhysicsWorld->GetWorld()->rayTest(rayStart, rayEnd, RayResultCallback);
|
||||
if (RayResultCallback.hasHit()) {
|
||||
btVector3 hitNormal = RayResultCallback.m_hitNormalWorld;
|
||||
if (hitNormal.y() > 0.7f) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void ActorBase::RegisterFunctions()
|
||||
{
|
||||
m_luaObject.Register("activate_camera", *this, &ActorBase::Lua_ActivateCamera);
|
||||
m_luaObject.Register("update_camera_look", *this, &ActorBase::Lua_UpdateCameraLook);
|
||||
m_luaObject.Register("update_camera_movement", *this, &ActorBase::Lua_UpdateCameraMovement);
|
||||
m_luaObject.Register("create_body", *this, &ActorBase::Lua_CreateBody);
|
||||
m_luaObject.Register("update_body_movement", *this, &ActorBase::Lua_UpdateBodyMovement);
|
||||
m_luaObject.Register("get_action", *this, &ActorBase::Lua_GetAction);
|
||||
|
||||
//m_luaObject.RegisterDirect("activate_camera", &ActorBase_ActivateCamera);
|
||||
//m_luaObject.RegisterDirect("update_camera_look", &ActorBase_UpdateCameraLook);
|
||||
//m_luaObject.RegisterDirect("update_camera_movement", &ActorBase_UpdateCameraMovement);
|
||||
}
|
||||
|
||||
int ActorBase::Lua_UpdateCameraMovement(LuaPlus::LuaState* state)
|
||||
{
|
||||
LuaPlus::LuaStack stack(state);
|
||||
|
||||
UpdateCameraMovement(stack[2].GetFloat());
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ActorBase::Lua_UpdateBodyMovement(LuaPlus::LuaState* state)
|
||||
{
|
||||
LuaPlus::LuaStack stack(state);
|
||||
|
||||
UpdateBodyMovement(stack[2].GetFloat());
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ActorBase::Lua_UpdateCameraLook(LuaPlus::LuaState* state)
|
||||
{
|
||||
UpdateCameraLook();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ActorBase::Lua_ActivateCamera(LuaPlus::LuaState* state)
|
||||
{
|
||||
ActivateCamera();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ActorBase::Lua_CreateBody(LuaPlus::LuaState* state)
|
||||
{
|
||||
CreatePlayerBody();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ActorBase::Lua_GetAction(LuaPlus::LuaState* state)
|
||||
{
|
||||
// uint32_t action = 0;
|
||||
|
||||
//float x, y;
|
||||
//SDL_MouseButtonFlags buttons = SDL_GetMouseState(&x, &y);
|
||||
|
||||
//if (buttons & SDL_BUTTON_MASK(SDL_BUTTON_LEFT))
|
||||
// state->PushInteger(0);
|
||||
//if (buttons & SDL_BUTTON_MASK(SDL_BUTTON_RIGHT))
|
||||
// state->PushInteger(1);
|
||||
//else
|
||||
// state->PushInteger(-1);
|
||||
|
||||
int action = GenAction();
|
||||
state->PushInteger(action);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int ActorBase::GenAction()
|
||||
{
|
||||
int action = -1;
|
||||
|
||||
if (g_inputManager.GetMouse().IsKeyDown(EMouseButton_Left))
|
||||
action = 0;
|
||||
else if (g_inputManager.GetMouse().IsKeyDown(EMouseButton_Right))
|
||||
action = 1;
|
||||
else if (g_inputManager.GetKeyboard().IsKeyDown(SDLK_R))
|
||||
action = 2;
|
||||
|
||||
return action;
|
||||
}
|
||||
|
||||
uint32_t ActorBase::GenMovementDir()
|
||||
{
|
||||
uint32_t movementDir = EMovementDir_None;
|
||||
|
||||
if (g_inputManager.GetKeyboard().IsKeyDown(SDLK_W)) {
|
||||
movementDir |= EMovementDir_Forward;
|
||||
}
|
||||
|
||||
if (g_inputManager.GetKeyboard().IsKeyDown(SDLK_S)) {
|
||||
movementDir |= EMovementDir_Backward;
|
||||
}
|
||||
|
||||
if (g_inputManager.GetKeyboard().IsKeyDown(SDLK_A)) {
|
||||
movementDir |= EMovementDir_Left;
|
||||
}
|
||||
|
||||
if (g_inputManager.GetKeyboard().IsKeyDown(SDLK_D)) {
|
||||
movementDir |= EMovementDir_Right;
|
||||
}
|
||||
|
||||
if (g_inputManager.GetKeyboard().IsKeyDown(SDLK_SPACE)) {
|
||||
movementDir |= EMovementDir_Jump;
|
||||
}
|
||||
|
||||
return movementDir;
|
||||
}
|
||||
|
||||
REGISTER_ENTITY(WeaponBase);
|
||||
|
||||
WeaponBase::WeaponBase()
|
||||
@@ -859,8 +577,58 @@ WeaponBase::~WeaponBase()
|
||||
|
||||
void WeaponBase::Update(float dt)
|
||||
{
|
||||
Entity::Update(dt);
|
||||
}
|
||||
|
||||
void WeaponBase::Fire(const glm::vec3& direction, float damage)
|
||||
{
|
||||
}
|
||||
|
||||
//// Lua wrappers
|
||||
//void Entity_LoadModel(LuaPlus::LuaState* state)
|
||||
//{
|
||||
// LuaPlus::LuaStack stack(state);
|
||||
//
|
||||
// Entity* entity = (Entity*)stack[1].GetByName("__object").GetLightUserdata();
|
||||
// entity->LoadModel(stack[2].GetString());
|
||||
//}
|
||||
//
|
||||
//void Entity_SetVisible(LuaPlus::LuaState* state)
|
||||
//{
|
||||
// LuaPlus::LuaStack stack(state);
|
||||
//
|
||||
// Entity* entity = (Entity*)stack[1].GetByName("__object").GetLightUserdata();
|
||||
// entity->SetVisible(stack[2].GetBoolean());
|
||||
//}
|
||||
//
|
||||
//bool Entity_GetVisible(LuaPlus::LuaState* state)
|
||||
//{
|
||||
// LuaPlus::LuaStack stack(state);
|
||||
//
|
||||
// Entity* entity = (Entity*)stack[1].GetByName("__object").GetLightUserdata();
|
||||
// return entity->GetVisible();
|
||||
//}
|
||||
//
|
||||
//void ActorBase_UpdateCameraMovement(LuaPlus::LuaState* state)
|
||||
//{
|
||||
// LuaPlus::LuaStack stack(state);
|
||||
//
|
||||
// ActorBase* entity = (ActorBase*)stack[1].GetByName("__object").GetLightUserdata();
|
||||
// entity->UpdateCameraMovement(stack[2].GetFloat());
|
||||
//}
|
||||
//
|
||||
//void ActorBase_UpdateCameraLook(LuaPlus::LuaState* state)
|
||||
//{
|
||||
// LuaPlus::LuaStack stack(state);
|
||||
//
|
||||
// ActorBase* entity = (ActorBase*)stack[1].GetByName("__object").GetLightUserdata();
|
||||
// entity->UpdateCameraLook();
|
||||
//}
|
||||
//
|
||||
//void ActorBase_ActivateCamera(LuaPlus::LuaState* state)
|
||||
//{
|
||||
// LuaPlus::LuaStack stack(state);
|
||||
//
|
||||
// ActorBase* entity = (ActorBase*)stack[1].GetByName("__object").GetLightUserdata();
|
||||
// entity->ActivateCamera();
|
||||
//}
|
||||
|
||||
@@ -89,6 +89,7 @@ public:
|
||||
// Game entity lua bindings
|
||||
|
||||
void InitFromTable(LuaPlus::LuaObject& _object);
|
||||
void InitLuaCallbacks();
|
||||
void RegisterBaseFunctions();
|
||||
virtual void RegisterFunctions();
|
||||
|
||||
@@ -97,19 +98,27 @@ public:
|
||||
void Help_SetPosition(float x, float y, float z);
|
||||
void Help_SetRotation(float x, float y, float z);
|
||||
void Help_SetRotationFromVectors(const glm::vec3& front, const glm::vec3& right, const glm::vec3& up);
|
||||
void Help_SetVelocity(float x, float y, float z);
|
||||
|
||||
int Lua_LoadModel(LuaPlus::LuaState* state);
|
||||
int Lua_Translate(LuaPlus::LuaState* state);
|
||||
int Lua_SetPosition(LuaPlus::LuaState* state);
|
||||
int Lua_GetPosition(LuaPlus::LuaState* state);
|
||||
int Lua_GetRotation(LuaPlus::LuaState* state);
|
||||
int Lua_SetRotation(LuaPlus::LuaState* state);
|
||||
int Lua_SetRotationFromVectors(LuaPlus::LuaState* state);
|
||||
int Lua_GetClassname(LuaPlus::LuaState* state);
|
||||
int Lua_GetID(LuaPlus::LuaState* state);
|
||||
int Lua_UpdateTransform(LuaPlus::LuaState* state);
|
||||
int Lua_MarkForDelete(LuaPlus::LuaState* state);
|
||||
|
||||
int Lua_SetVelocity(LuaPlus::LuaState* state);
|
||||
int Lua_GetVelocity(LuaPlus::LuaState* state);
|
||||
int Lua_HasRigidBody(LuaPlus::LuaState* state);
|
||||
|
||||
int Lua_FindAnimation(LuaPlus::LuaState* state);
|
||||
int Lua_PlayAnimation(LuaPlus::LuaState* state);
|
||||
int Lua_StopAnimation(LuaPlus::LuaState* state);
|
||||
int Lua_GetCurrentAnimation(LuaPlus::LuaState* state);
|
||||
int Lua_GetCurrentAnimationTime(LuaPlus::LuaState* state);
|
||||
int Lua_GetAnimationTime(LuaPlus::LuaState* state);
|
||||
@@ -136,49 +145,6 @@ protected:
|
||||
bool m_bodyDirty;
|
||||
};
|
||||
|
||||
class ActorBase : public Entity
|
||||
{
|
||||
public:
|
||||
ActorBase();
|
||||
~ActorBase();
|
||||
|
||||
virtual void Update(float dt);
|
||||
|
||||
void AfterEngineStep();
|
||||
|
||||
void UpdateCameraMovement(float dt);
|
||||
|
||||
void UpdateBodyMovement(float dt);
|
||||
|
||||
void UpdateCameraLook();
|
||||
|
||||
void ActivateCamera();
|
||||
|
||||
void CreatePlayerBody();
|
||||
|
||||
bool OnGround();
|
||||
|
||||
// Lua bindings
|
||||
|
||||
virtual void RegisterFunctions();
|
||||
|
||||
int Lua_UpdateCameraMovement(LuaPlus::LuaState* state);
|
||||
int Lua_UpdateBodyMovement(LuaPlus::LuaState* state);
|
||||
int Lua_UpdateCameraLook(LuaPlus::LuaState* state);
|
||||
int Lua_ActivateCamera(LuaPlus::LuaState* state);
|
||||
int Lua_CreateBody(LuaPlus::LuaState* state);
|
||||
int Lua_GetAction(LuaPlus::LuaState* state);
|
||||
|
||||
private:
|
||||
int GenAction();
|
||||
uint32_t GenMovementDir();
|
||||
|
||||
private:
|
||||
Camera m_camera;
|
||||
|
||||
|
||||
};
|
||||
|
||||
class WeaponBase : public Entity
|
||||
{
|
||||
public:
|
||||
|
||||
79
src/game/game_ui.cpp
Normal file
79
src/game/game_ui.cpp
Normal file
@@ -0,0 +1,79 @@
|
||||
#include "core.h"
|
||||
#include "game_ui.h"
|
||||
#include "game_lua_help.h"
|
||||
#include "texturesmanager.h"
|
||||
|
||||
#include <ImGui.h>
|
||||
|
||||
using namespace LuaPlus;
|
||||
|
||||
uint32_t ColorFromLua(const LuaObject& color)
|
||||
{
|
||||
SDL_assert_always(color.IsTable());
|
||||
|
||||
float r = color[1].ToNumber();
|
||||
float g = color[2].ToNumber();
|
||||
float b = color[3].ToNumber();
|
||||
float a = color[4].ToNumber();
|
||||
|
||||
return ImGui::ColorConvertFloat4ToU32(ImVec4(r, g, b, a));
|
||||
}
|
||||
|
||||
void uiDrawText(const char* text, float x, float y, const LuaObject& color)
|
||||
{
|
||||
ImGui::GetBackgroundDrawList()->AddText(ImVec2(x, y), ColorFromLua(color), text);
|
||||
}
|
||||
|
||||
void uiDrawRect(float x, float y, float w, float h, const LuaObject& color)
|
||||
{
|
||||
ImGui::GetBackgroundDrawList()->AddRectFilled(ImVec2(x, y), ImVec2(w, h), ColorFromLua(color));
|
||||
}
|
||||
|
||||
void uiDrawImage(const char* filename, float x, float y, float w, float h, const LuaObject& color)
|
||||
{
|
||||
ImTextureID texture = (ImTextureID)g_texturesManager->LoadTexture2D(filename);
|
||||
ImGui::GetBackgroundDrawList()->AddImage(texture, ImVec2(x, y), ImVec2(w, h), ImVec2(0.0f, 0.0f), ImVec2(1.0f, 1.0f), ColorFromLua(color));
|
||||
}
|
||||
|
||||
int uiGetDisplaySize(LuaState* state)
|
||||
{
|
||||
state->PushNumber(ImGui::GetIO().DisplaySize.x);
|
||||
state->PushNumber(ImGui::GetIO().DisplaySize.y);
|
||||
return 2;
|
||||
}
|
||||
|
||||
int uiGetMousePos(LuaState* state)
|
||||
{
|
||||
state->PushNumber(ImGui::GetIO().MousePos.x);
|
||||
state->PushNumber(ImGui::GetIO().MousePos.y);
|
||||
return 2;
|
||||
}
|
||||
|
||||
int uiCalcTextWidth(LuaState* state)
|
||||
{
|
||||
LuaStack stack(state);
|
||||
SDL_assert_always(stack[1].IsString());
|
||||
|
||||
state->PushNumber(ImGui::CalcTextSize(stack[1].GetString()).x);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void registerEngineUI()
|
||||
{
|
||||
LuaObject uiTable = GetLuaState().GetGlobals().CreateTable("ui");
|
||||
uiTable.RegisterDirect("draw_text", &uiDrawText);
|
||||
uiTable.RegisterDirect("draw_rect", &uiDrawRect);
|
||||
uiTable.RegisterDirect("draw_image", &uiDrawImage);
|
||||
uiTable.Register("get_display_size", &uiGetDisplaySize);
|
||||
uiTable.Register("get_mouse_pos", &uiGetMousePos);
|
||||
uiTable.Register("calc_text_width", &uiCalcTextWidth);
|
||||
}
|
||||
|
||||
void gameRenderUI()
|
||||
{
|
||||
LuaObject renderFunction = GetLuaState().GetGlobal("game_hud_draw");
|
||||
|
||||
LuaFunctionVoid function = renderFunction;
|
||||
function();
|
||||
}
|
||||
8
src/game/game_ui.h
Normal file
8
src/game/game_ui.h
Normal file
@@ -0,0 +1,8 @@
|
||||
#ifndef GAME_UI_H
|
||||
#define GAME_UI_H
|
||||
|
||||
void registerEngineUI();
|
||||
|
||||
void gameRenderUI();
|
||||
|
||||
#endif // !GAME_UI_H
|
||||
Reference in New Issue
Block a user