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