Add ability to parent entity rotations, WIP.

master
Nicole Collings 2020-03-03 21:45:22 -08:00
parent 8bd2e404cd
commit 24991f4590
14 changed files with 66 additions and 38 deletions

View File

@ -6,6 +6,8 @@
#include <string>
#include "../game/entity/Model.h"
class ItemDef {
public:
enum class Type {
@ -21,5 +23,7 @@ public:
unsigned short maxStackSize;
Type type = Type::INVALID;
std::shared_ptr<Model> entityModel = std::make_shared<Model>();
};

View File

@ -6,11 +6,12 @@
#include <string>
#include <sol2/sol.hpp>
#include "SelectionBox.h"
#include "BlockModel.h"
#include "../ItemDef.h"
#include "BlockModel.h"
#include "SelectionBox.h"
#include "../../lua/Callback.h"
#include "../../game/entity/Model.h"
class BlockDef : public ItemDef {
public:
@ -31,7 +32,6 @@ public:
std::vector<SelectionBox> sBoxes;
std::vector<SelectionBox> cBoxes;
std::shared_ptr<Model> entityModel {};
std::unordered_map<Callback, sol::function, Util::EnumClassHash> callbacks {};
};

View File

@ -5,9 +5,10 @@
#pragma once
#include <vector>
#include "../ItemDef.h"
#include "../texture/AtlasRef.h"
#include "../../game/entity/Model.h"
class CraftItemDef : public ItemDef {
public:
@ -22,6 +23,4 @@ public:
std::vector<std::string> textures {};
std::vector<std::shared_ptr<AtlasRef>> textureRefs {};
std::shared_ptr<Model> entityModel = std::make_shared<Model>();
};

View File

@ -129,16 +129,22 @@ glm::mat4 Entity::getModelMatrix() {
glm::mat4 model = glm::mat4(1.0);
model = glm::translate(model, visualPosition + visualVisualOffset);
model = glm::rotate(model, glm::radians(visualRotation.x), {1, 0, 0});
model = glm::rotate(model, glm::radians(visualRotation.y), {0, 1, 0});
model = glm::rotate(model, glm::radians(visualRotation.z), {0, 0, 1});
model = model * getRotationMatrix();
model = glm::scale(model, visualScale);
return model;
}
glm::mat4 Entity::getRotationMatrix() {
glm::mat4 parentMatrix = (parent != nullptr ? parent->getRotationMatrix() : glm::mat4(1.0f));
glm::mat4 rotMatrix = glm::mat4(1.0f);
rotMatrix = glm::rotate(rotMatrix, glm::radians(visualRotation.x), {1, 0, 0});
rotMatrix = glm::rotate(rotMatrix, glm::radians(visualRotation.y), {0, 1, 0});
rotMatrix = glm::rotate(rotMatrix, glm::radians(visualRotation.z), {0, 0, 1});
return parentMatrix * rotMatrix;
}
void Entity::cleanup() {
model = nullptr;
}

View File

@ -57,8 +57,11 @@ public:
~Entity() override;
AnimationState animState {};
Entity* parent = nullptr;
protected:
glm::mat4 getModelMatrix();
glm::mat4 getRotationMatrix();
glm::vec3 position {};
glm::vec3 visualPosition {};

View File

@ -25,7 +25,7 @@ void Camera::createMatrices() {
projectionMatrix = glm::perspective(glm::radians(fov), ratio, nearClip, farClip);
frustum.setCamInternals(glm::radians(fov) / 1.2f, ratio, nearClip, farClip);
orthographicMatrix = glm::ortho(0.0f, bufferDimensions.x, bufferDimensions.y, 0.0f, -100.0f, 100.0f);
orthographicMatrix = glm::ortho(0.0f, bufferDimensions.x, bufferDimensions.y, 0.0f, -1000.0f, 1000.0f);
}
void Camera::changeWindowDimensions(glm::vec2 size) {

View File

@ -16,7 +16,7 @@ void InventoryRefs::update() {
}
std::shared_ptr<Inventory> InventoryRefs::createInv(const std::string &inv) {
inventories.emplace(inv, std::make_shared<Inventory>(defs, clients, inv));
if (!inventories.count(inv)) inventories.emplace(inv, std::make_shared<Inventory>(defs, clients, inv));
return inventories[inv];
}
@ -46,11 +46,9 @@ bool InventoryRefs::removeWatcher(const std::string &inv, const std::string &lis
}
void InventoryRefs::primaryInteract(const std::string &inv, const std::string &list, unsigned short ind, unsigned int cid) {
std::cout << "primary interaction" << std::endl;
inventories[inv]->operator[](list)->primaryInteract(*inventories["player:" + std::to_string(cid)]->operator[]("hand"), ind);
}
void InventoryRefs::secondaryInteract(const std::string &inv, const std::string &list, unsigned short ind, unsigned int cid) {
std::cout << "secondary interaction" << std::endl;
inventories[inv]->operator[](list)->secondaryInteract(*inventories["player:" + std::to_string(cid)]->operator[]("hand"), ind);
}

View File

@ -17,8 +17,9 @@ Player::Player(LocalWorld& world, ClientGame& defs, Renderer& renderer, LocalInv
void Player::update(Input &input, double delta, glm::vec2 mouseDelta) {
if (activeBlock == -1) {
activeBlock = defs.defs.blockFromStr("zeus:default:stone").index;
handModel.setModel(defs.defs.blockFromId(activeBlock).entityModel);
activeBlock = defs.defs.fromStr("zeus:default:stone").index;
handItemModel.setModel(defs.defs.fromId(activeBlock).entityModel);
handItemModel.parent = &handModel;
}
gameGui.update(delta);
@ -34,6 +35,7 @@ void Player::update(Input &input, double delta, glm::vec2 mouseDelta) {
}
void Player::moveAndLook(Input &input, double delta, glm::vec2 mouseDelta) {
//Position movement
bool sprinting = input.isKeyDown(GLFW_KEY_LEFT_CONTROL);
@ -105,6 +107,8 @@ void Player::updateCamera() {
renderer.camera.setYaw(yaw);
renderer.camera.setPitch(pitch);
auto type = defs.defs.fromId(activeBlock).type;
glm::vec3 eyesPos = {pos.x, pos.y + EYE_HEIGHT, pos.z};
renderer.camera.setPos(eyesPos);
@ -117,13 +121,24 @@ void Player::updateCamera() {
glm::vec3 right = glm::normalize(glm::cross(front, {0, 1, 0}));
glm::vec3 up = glm::normalize(glm::cross(right, front));
glm::vec3 handPos = eyesPos + front * 0.25f + right * 0.25f + up * -0.2f;
glm::vec3 handPos = eyesPos + front * 0.25f + right * 0.25f + up * (type == ItemDef::Type::CRAFTITEM ? -0.15f : -0.2f);
handModel.setRotateY(-yaw);
handModel.setRotateZ(pitch);
handModel.setPos(handPos + vel * 0.1f);
handModel.setScale(0.12f);
if (type == ItemDef::Type::CRAFTITEM) {
handItemModel.setRotateX(45);
handItemModel.setRotateY(110);
handItemModel.setRotateZ(-25);
}
else {
handItemModel.setRotateX(0);
handItemModel.setRotateY(0);
handItemModel.setRotateZ(0);
}
handItemModel.setPos(handPos + vel * 0.1f);
handItemModel.setScale((type == ItemDef::Type::CRAFTITEM ? 0.2f : 0.12f));
}
void Player::findPointedThing(Input &input) {
@ -256,8 +271,8 @@ LocalInventory& Player::getInventory() {
*/
void Player::setActiveBlock(const std::string& block) {
activeBlock = defs.defs.blockFromStr(block).index;
handModel.setModel(defs.defs.blockFromStr(block).entityModel);
activeBlock = defs.defs.fromStr(block).index;
handItemModel.setModel(defs.defs.fromId(activeBlock).entityModel);
}
void Player::setMenu(const std::string& menu, const std::map<std::string, GuiBuilder::ComponentCallbacks>& callbacks) {
@ -284,7 +299,8 @@ std::string Player::getMenuState() {
void Player::draw(Renderer &renderer) {
wireframe.draw(renderer);
handModel.draw(renderer);
// handModel.draw(renderer);
handItemModel.draw(renderer);
}
void Player::drawGUI(Renderer &renderer) {

View File

@ -67,6 +67,7 @@ private:
GameGui gameGui;
Entity handModel;
Entity handItemModel;
WireframeEntity wireframe;
float yaw = 0;

View File

@ -3,6 +3,7 @@ runfile(_PATH .. "entity/_index")
local blockTypes = {
"@aurailus:tnt:tnt",
"@aurailus:basictools:flint_pickaxe",
"zeus:default:grass_slab",
"zeus:kinetic:axle_0",
"zeus:default:stone",

View File

@ -1,9 +1,9 @@
zepha.register_item("zeus:materials:plant_fibre", {
name = "Plant Fibre",
groups = {
organic = 1,
},
textures = {
"zeus:materials:plant_fibre"
}
},
groups = {
organic = 1,
}
})

View File

@ -1,11 +1,11 @@
zepha.register_item("zeus:materials:plant_twine", {
name = "Plant Twine",
groups = {
rope = 1,
},
textures = {
"zeus:materials:plant_twine"
}
},
groups = {
rope = 1,
}
})
crafting.register_recipe({

View File

@ -1,9 +1,9 @@
zepha.register_item("zeus:materials:rock", {
name = "Rock",
groups = {
rock = 1,
},
textures = {
"zeus:materials:rock"
},
groups = {
rock = 1,
}
})

View File

@ -1,12 +1,12 @@
zepha.register_item("zeus:materials:stick", {
name = "Stick",
groups = {
stick = 1,
},
textures = {
"zeus:materials:stick_0",
"zeus:materials:stick_1",
"zeus:materials:stick_2",
"zeus:materials:stick_3"
},
groups = {
stick = 1,
}
})