Refactor
This commit is contained in:
parent
e2bb7a34aa
commit
9760cff8f4
@ -49,6 +49,10 @@ void InventoryView::setSlotConsumer(slotconsumer consumer) {
|
||||
this->consumer = consumer;
|
||||
}
|
||||
|
||||
void InventoryView::setItems(std::vector<itemid_t> items) {
|
||||
this->items = items;
|
||||
}
|
||||
|
||||
void InventoryView::actAndDraw(const GfxContext* ctx) {
|
||||
Assets* assets = frontend->getAssets();
|
||||
Shader* uiShader = assets->getShader("ui");
|
||||
|
||||
@ -39,6 +39,8 @@ public:
|
||||
|
||||
virtual void actAndDraw(const GfxContext* ctx);
|
||||
|
||||
void setItems(std::vector<itemid_t> items);
|
||||
|
||||
void setPosition(int x, int y);
|
||||
int getWidth() const;
|
||||
int getHeight() const;
|
||||
|
||||
@ -168,7 +168,7 @@ void WorldRenderer::draw(const GfxContext& pctx, Camera* camera){
|
||||
shader->uniform3f("u_cameraPos", camera->position);
|
||||
shader->uniform1i("u_cubemap", 1);
|
||||
{
|
||||
blockid_t id = level->player->chosenItem;
|
||||
blockid_t id = level->player->getChosenItem();
|
||||
Block* block = contentIds->getBlockDef(id);
|
||||
assert(block != nullptr);
|
||||
float multiplier = 0.5f;
|
||||
|
||||
@ -109,7 +109,7 @@ void HudRenderer::createDebugPanel(Engine* engine) {
|
||||
// Coord input
|
||||
TextBox* box = new TextBox(L"");
|
||||
box->textSupplier([=]() {
|
||||
Hitbox* hitbox = level->player->hitbox;
|
||||
Hitbox* hitbox = level->player->hitbox.get();
|
||||
return std::to_wstring(hitbox->position[ax]);
|
||||
});
|
||||
box->textConsumer([=](std::wstring text) {
|
||||
@ -183,15 +183,18 @@ HudRenderer::HudRenderer(Engine* engine, LevelFrontend* frontend)
|
||||
auto menu = gui->getMenu();
|
||||
auto content = level->content;
|
||||
auto indices = content->indices;
|
||||
|
||||
std::vector<itemid_t> items;
|
||||
for (itemid_t id = 1; id < indices->countItemDefs(); id++) {
|
||||
items.push_back(id);
|
||||
}
|
||||
contentAccess.reset(new InventoryView(8, content, frontend, items));
|
||||
contentAccess->setSlotConsumer([=](blockid_t id) {
|
||||
level->player->chosenItem = id;
|
||||
level->player->setChosenItem(id);
|
||||
});
|
||||
|
||||
hotbarView.reset(new InventoryView(1, content, frontend, std::vector<itemid_t> {0}));
|
||||
|
||||
uicamera = new Camera(vec3(), 1);
|
||||
uicamera->perspective = false;
|
||||
uicamera->flipped = true;
|
||||
@ -270,50 +273,16 @@ void HudRenderer::draw(const GfxContext& ctx){
|
||||
}
|
||||
|
||||
Player* player = level->player;
|
||||
batch->color = vec4(0.0f, 0.0f, 0.0f, 0.75f);
|
||||
batch->rect(width - 68, height - 68, 68, 68);
|
||||
batch->color = vec4(1.0f);
|
||||
batch->render();
|
||||
|
||||
|
||||
{
|
||||
Window::clearDepth();
|
||||
GfxContext subctx = ctx.sub();
|
||||
subctx.depthTest(true);
|
||||
subctx.cullFace(true);
|
||||
|
||||
ItemDef* item = contentIds->getItemDef(player->chosenItem);
|
||||
switch (item->iconType) {
|
||||
case item_icon_type::none:
|
||||
break;
|
||||
case item_icon_type::block: {
|
||||
Block* cblock = content->findBlock(item->icon);
|
||||
assert(cblock != nullptr);
|
||||
|
||||
auto blocksPreview = frontend->getBlocksPreview();
|
||||
blocksPreview->begin(&ctx.getViewport());
|
||||
blocksPreview->draw(cblock, width - 56, uicamera->getFov() - 56, 48, vec4(1.0f));
|
||||
break;
|
||||
}
|
||||
case item_icon_type::sprite: {
|
||||
size_t index = item->icon.find(':');
|
||||
std::string name = item->icon.substr(index+1);
|
||||
UVRegion region(0.0f, 0.0, 1.0f, 1.0f);
|
||||
if (index == std::string::npos) {
|
||||
batch->texture(assets->getTexture(name));
|
||||
} else {
|
||||
std::string atlasname = item->icon.substr(0, index);
|
||||
Atlas* atlas = assets->getAtlas(atlasname);
|
||||
if (atlas && atlas->has(name)) {
|
||||
region = atlas->get(name);
|
||||
batch->texture(atlas->getTexture());
|
||||
}
|
||||
}
|
||||
batch->rect(width - 56, uicamera->getFov() - 56, 48, 48, 0, 0, 0,
|
||||
region, false, true, glm::vec4(1.0f));
|
||||
batch->render();
|
||||
}
|
||||
}
|
||||
|
||||
hotbarView->setPosition(width-56, height-56);
|
||||
hotbarView->setItems({player->getChosenItem()});
|
||||
hotbarView->actAndDraw(&subctx);
|
||||
}
|
||||
uishader->use();
|
||||
batch->begin();
|
||||
|
||||
@ -35,6 +35,7 @@ class HudRenderer {
|
||||
bool pause = false;
|
||||
|
||||
std::unique_ptr<InventoryView> contentAccess;
|
||||
std::unique_ptr<InventoryView> hotbarView;
|
||||
std::shared_ptr<gui::UINode> debugPanel;
|
||||
gui::GUI* gui;
|
||||
LevelFrontend* frontend;
|
||||
|
||||
@ -144,12 +144,12 @@ void LevelScreen::update(float delta) {
|
||||
}
|
||||
|
||||
void LevelScreen::draw(float delta) {
|
||||
Camera* camera = level->player->currentViewCamera;
|
||||
auto camera = level->player->currentCamera;
|
||||
|
||||
Viewport viewport(Window::width, Window::height);
|
||||
GfxContext ctx(nullptr, viewport, batch.get());
|
||||
|
||||
worldRenderer->draw(ctx, camera);
|
||||
worldRenderer->draw(ctx, camera.get());
|
||||
|
||||
if (hudVisible) {
|
||||
hud->draw(ctx);
|
||||
|
||||
@ -32,7 +32,7 @@ const float CROUCH_SHIFT_Y = -0.2f;
|
||||
CameraControl::CameraControl(Player* player, const CameraSettings& settings)
|
||||
: player(player),
|
||||
camera(player->camera),
|
||||
currentViewCamera(player->currentViewCamera), //TODO "start view" settings (for custom worlds and minigames, maybe)
|
||||
currentViewCamera(player->currentCamera),
|
||||
settings(settings),
|
||||
offset(0.0f, 0.7f, 0.0f) {
|
||||
}
|
||||
@ -61,7 +61,7 @@ void CameraControl::updateMouse(PlayerInput& input) {
|
||||
}
|
||||
|
||||
void CameraControl::update(PlayerInput& input, float delta, Chunks* chunks) {
|
||||
Hitbox* hitbox = player->hitbox;
|
||||
Hitbox* hitbox = player->hitbox.get();
|
||||
|
||||
offset = glm::vec3(0.0f, 0.7f, 0.0f);
|
||||
|
||||
@ -102,23 +102,26 @@ void CameraControl::update(PlayerInput& input, float delta, Chunks* chunks) {
|
||||
camera->zoom = zoomValue * dt + camera->zoom * (1.0f - dt);
|
||||
}
|
||||
|
||||
auto spCamera = player->spCamera;
|
||||
auto tpCamera = player->tpCamera;
|
||||
|
||||
if (input.cameraMode) { //ugly but effective
|
||||
if (player->currentViewCamera == camera)
|
||||
player->currentViewCamera = player->SPCamera;
|
||||
else if (player->currentViewCamera == player->SPCamera)
|
||||
player->currentViewCamera = player->TPCamera;
|
||||
else if (player->currentViewCamera == player->TPCamera)
|
||||
player->currentViewCamera = camera;
|
||||
if (player->currentCamera == camera)
|
||||
player->currentCamera = tpCamera;
|
||||
else if (player->currentCamera == spCamera)
|
||||
player->currentCamera = camera;
|
||||
else if (player->currentCamera == tpCamera)
|
||||
player->currentCamera = spCamera;
|
||||
}
|
||||
if (player->currentViewCamera == player->SPCamera) {
|
||||
player->SPCamera->position = chunks->rayCastToObstacle(camera->position, camera->front, 3.0f) - 0.2f*(camera->front);
|
||||
player->SPCamera->dir = -camera->dir;
|
||||
player->SPCamera->front = -camera->front;
|
||||
if (player->currentCamera == spCamera) {
|
||||
spCamera->position = chunks->rayCastToObstacle(camera->position, camera->front, 3.0f) - 0.2f*(camera->front);
|
||||
spCamera->dir = -camera->dir;
|
||||
spCamera->front = -camera->front;
|
||||
}
|
||||
else if (player->currentViewCamera == player->TPCamera) {
|
||||
player->TPCamera->position = chunks->rayCastToObstacle(camera->position, -camera->front, 3.0f) + 0.2f * (camera->front);
|
||||
player->TPCamera->dir = camera->dir;
|
||||
player->TPCamera->front = camera->front;
|
||||
else if (player->currentCamera == tpCamera) {
|
||||
tpCamera->position = chunks->rayCastToObstacle(camera->position, -camera->front, 3.0f) + 0.2f * (camera->front);
|
||||
tpCamera->dir = camera->dir;
|
||||
tpCamera->front = camera->front;
|
||||
}
|
||||
}
|
||||
|
||||
@ -201,7 +204,7 @@ void PlayerController::updateInteraction(){
|
||||
Chunks* chunks = level->chunks;
|
||||
Player* player = level->player;
|
||||
Lighting* lighting = level->lighting;
|
||||
Camera* camera = player->camera;
|
||||
Camera* camera = player->camera.get();
|
||||
glm::vec3 end;
|
||||
glm::ivec3 iend;
|
||||
glm::ivec3 norm;
|
||||
@ -232,8 +235,7 @@ void PlayerController::updateInteraction(){
|
||||
int z = iend.z;
|
||||
uint8_t states = 0;
|
||||
|
||||
ItemDef* item = contentIds->getItemDef(player->chosenItem);
|
||||
|
||||
ItemDef* item = contentIds->getItemDef(player->getChosenItem());
|
||||
Block* def = contentIds->getBlockDef(item->rt.placingBlock);
|
||||
if (def && def->rotatable){
|
||||
const std::string& name = def->rotations.name;
|
||||
@ -257,24 +259,24 @@ void PlayerController::updateInteraction(){
|
||||
}
|
||||
}
|
||||
|
||||
Block* block = contentIds->getBlockDef(vox->id);
|
||||
if (lclick && block->breakable){
|
||||
blocksController->breakBlock(player, block, x, y, z);
|
||||
Block* target = contentIds->getBlockDef(vox->id);
|
||||
if (lclick && target->breakable){
|
||||
blocksController->breakBlock(player, target, x, y, z);
|
||||
}
|
||||
if (def && rclick){
|
||||
if (!input.shift && block->rt.funcsset.oninteract) {
|
||||
scripting::on_block_interact(player, block, x, y, z);
|
||||
if (!input.shift && target->rt.funcsset.oninteract) {
|
||||
scripting::on_block_interact(player, target, x, y, z);
|
||||
return;
|
||||
}
|
||||
if (block->model != BlockModel::xsprite){
|
||||
if (target->model != BlockModel::xsprite){
|
||||
x = (iend.x)+(norm.x);
|
||||
y = (iend.y)+(norm.y);
|
||||
z = (iend.z)+(norm.z);
|
||||
}
|
||||
vox = chunks->get(x, y, z);
|
||||
blockid_t chosenBlock = def->rt.id;
|
||||
if (vox && (block = contentIds->getBlockDef(vox->id))->replaceable) {
|
||||
if (!level->physics->isBlockInside(x,y,z, player->hitbox)
|
||||
if (vox && (target = contentIds->getBlockDef(vox->id))->replaceable) {
|
||||
if (!level->physics->isBlockInside(x,y,z, player->hitbox.get())
|
||||
|| !def->obstacle){
|
||||
if (def->grounded && !chunks->isSolidBlock(x, y-1, z)) {
|
||||
chosenBlock = 0;
|
||||
@ -292,7 +294,7 @@ void PlayerController::updateInteraction(){
|
||||
}
|
||||
if (Events::jactive(BIND_PLAYER_PICK)){
|
||||
Block* block = contentIds->getBlockDef(chunks->get(x,y,z)->id);
|
||||
player->chosenItem = block->rt.pickingItem;
|
||||
player->setChosenItem(block->rt.pickingItem);
|
||||
}
|
||||
} else {
|
||||
selectedBlockId = -1;
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
#ifndef PLAYER_CONTROL_H_
|
||||
#define PLAYER_CONTROL_H_
|
||||
|
||||
#include <memory>
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
#include "../settings.h"
|
||||
@ -12,7 +13,7 @@ class BlocksController;
|
||||
|
||||
class CameraControl {
|
||||
Player* player;
|
||||
Camera* camera, *currentViewCamera;
|
||||
std::shared_ptr<Camera> camera, currentViewCamera;
|
||||
const CameraSettings& settings;
|
||||
glm::vec3 offset;
|
||||
float shake = 0.0f;
|
||||
|
||||
@ -18,16 +18,15 @@ const float JUMP_FORCE = 8.0f;
|
||||
|
||||
Player::Player(glm::vec3 position, float speed) :
|
||||
speed(speed),
|
||||
chosenItem(1) {
|
||||
camera = new Camera(position, glm::radians(90.0f));
|
||||
currentViewCamera = camera;
|
||||
SPCamera = new Camera(position, glm::radians(90.0f));
|
||||
TPCamera = new Camera(position, glm::radians(90.0f));
|
||||
hitbox = new Hitbox(position, glm::vec3(0.3f,0.9f,0.3f));
|
||||
chosenItem(0),
|
||||
camera(new Camera(position, glm::radians(90.0f))),
|
||||
spCamera(new Camera(position, glm::radians(90.0f))),
|
||||
tpCamera(new Camera(position, glm::radians(90.0f))),
|
||||
currentCamera(camera),
|
||||
hitbox(new Hitbox(position, glm::vec3(0.3f,0.9f,0.3f))) {
|
||||
}
|
||||
|
||||
Player::~Player(){
|
||||
delete hitbox;
|
||||
}
|
||||
|
||||
void Player::update(
|
||||
@ -75,7 +74,7 @@ void Player::update(
|
||||
float vel = std::max(glm::length(hitbox->velocity * 0.25f), 1.0f);
|
||||
int substeps = int(delta * vel * 1000);
|
||||
substeps = std::min(100, std::max(1, substeps));
|
||||
level->physics->step(level->chunks, hitbox,
|
||||
level->physics->step(level->chunks, hitbox.get(),
|
||||
delta, substeps,
|
||||
crouch, flight ? 0.0f : 1.0f,
|
||||
!noclip);
|
||||
@ -122,6 +121,14 @@ void Player::teleport(glm::vec3 position) {
|
||||
hitbox->position = position;
|
||||
}
|
||||
|
||||
void Player::setChosenItem(itemid_t id) {
|
||||
chosenItem = id;
|
||||
}
|
||||
|
||||
itemid_t Player::getChosenItem() const {
|
||||
return chosenItem;
|
||||
}
|
||||
|
||||
float Player::getSpeed() const {
|
||||
return speed;
|
||||
}
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
#ifndef SRC_OBJECTS_PLAYER_H_
|
||||
#define SRC_OBJECTS_PLAYER_H_
|
||||
|
||||
#include <memory>
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
#include "../voxels/voxel.h"
|
||||
@ -29,13 +30,14 @@ struct PlayerInput {
|
||||
|
||||
class Player {
|
||||
float speed;
|
||||
itemid_t chosenItem;
|
||||
public:
|
||||
Camera* camera, *SPCamera, *TPCamera, *currentViewCamera;
|
||||
Hitbox* hitbox;
|
||||
std::shared_ptr<Camera> camera, spCamera, tpCamera;
|
||||
std::shared_ptr<Camera> currentCamera;
|
||||
std::unique_ptr<Hitbox> hitbox;
|
||||
bool flight = false;
|
||||
bool noclip = false;
|
||||
bool debug = false;
|
||||
int chosenItem;
|
||||
voxel selectedVoxel {0, 0};
|
||||
|
||||
glm::vec2 cam = {};
|
||||
@ -44,9 +46,12 @@ public:
|
||||
~Player();
|
||||
|
||||
void teleport(glm::vec3 position);
|
||||
|
||||
float getSpeed() const;
|
||||
void update(Level* level, PlayerInput& input, float delta);
|
||||
|
||||
void setChosenItem(itemid_t id);
|
||||
|
||||
itemid_t getChosenItem() const;
|
||||
float getSpeed() const;
|
||||
};
|
||||
|
||||
#endif /* SRC_OBJECTS_PLAYER_H_ */
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user