Skip to content

Commit

Permalink
master: -add inputManager to input on component
Browse files Browse the repository at this point in the history
	-manipulate player with asdw
	-fix aspect ratio
	-clean engine core
  • Loading branch information
paulgheorghecristian committed Oct 8, 2017
1 parent 8658721 commit 2a5f18f
Show file tree
Hide file tree
Showing 15 changed files with 68 additions and 40 deletions.
3 changes: 2 additions & 1 deletion Core/Component.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
#define COMPONENT_H

#include "Entity.h"
#include "Input.h"

class Entity;

class Component
{
public:
Component();
virtual void input() = 0;
virtual void input(Input &inputManager) = 0;
virtual void update() = 0;
virtual void render() = 0;
virtual const unsigned int getFlag() const = 0;
Expand Down
42 changes: 18 additions & 24 deletions Core/EngineCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ EngineCore::EngineCore(rapidjson::Document &gameDocument) : isRunning (false) {
nearPlane = (configDocument["camera"].HasMember("near")) ? configDocument["camera"]["near"].GetFloat() : 1.0f;
farPlane = (configDocument["camera"].HasMember("far")) ? configDocument["camera"]["far"].GetFloat() : 5000.0f;
fov = (configDocument["camera"].HasMember("fov")) ? configDocument["camera"]["fov"].GetFloat() : 75.0;
aspectRatio = (float ) screenWidth / (float) screenHeight;

this->mouseMoveSpeed = (configDocument["mouse"].HasMember("moveSpeed")) ? configDocument["mouse"]["moveSpeed"].GetFloat() : 0.01f;
this->mouseRotationSpeed = (configDocument["mouse"].HasMember("rotationSpeed")) ? configDocument["mouse"]["rotationSpeed"].GetFloat() : 0.001f;
Expand All @@ -46,6 +45,7 @@ EngineCore::EngineCore(rapidjson::Document &gameDocument) : isRunning (false) {

/* this needs to be called first before doing anything with OpenGL */
display = new Display (screenWidth, screenHeight, screenTitle, isFullScreen, maxFps, vSync);
aspectRatio = (float) display->getWidth() / (float ) display->getHeight();
inputManager.setWarpMouse (this->warpMouse);
SDL_ShowCursor(this->showMouse);

Expand Down Expand Up @@ -92,11 +92,7 @@ EngineCore::EngineCore(rapidjson::Document &gameDocument) : isRunning (false) {
entities.push_back (new_entity);
}
}

Transform playerTrans (glm::vec3(0, 1000, 0), glm::vec3(0), glm::vec3(20));
entities.push_back ((new Player (renderingMaster->getCamera(), playerTrans))
->addComponent (new PhysicsComponent (PhysicsComponent::BoundingBodyType::SPHERE, glm::vec3(20), 50.0f)));

constructPlayer();
std::cout << "Number of entities: " << entities.size() << std::endl;
}

Expand Down Expand Up @@ -187,22 +183,6 @@ void EngineCore::input() {
stop ();
}

if (inputManager.getKey (SDLK_w)) {
renderingMaster->moveCameraForward (this->mouseMoveSpeed);
}

if (inputManager.getKey (SDLK_s)) {
renderingMaster->moveCameraForward (-this->mouseMoveSpeed);
}

if (inputManager.getKey (SDLK_d)) {
renderingMaster->moveCameraSideways (this->mouseMoveSpeed);
}

if (inputManager.getKey (SDLK_a)) {
renderingMaster->moveCameraSideways (-this->mouseMoveSpeed);
}

renderingMaster->rotateCameraX(inputManager.getMouseDelta().y * this->mouseRotationSpeed);
renderingMaster->rotateCameraY(inputManager.getMouseDelta().x * this->mouseRotationSpeed);

Expand All @@ -212,7 +192,7 @@ void EngineCore::input() {
glm::vec3 (20));
entities.push_back ((new Entity(transform))->addComponent (new RenderComponent(Mesh::loadObject("res/models/cube4.obj"),
(new Shader())->construct("res/shaders/example.json"),
NULL,
new Texture ("res/textures/196.bmp",0),
Material (glm::vec3(1, 0, 0),
glm::vec3(0),
glm::vec3(0),
Expand All @@ -223,7 +203,7 @@ void EngineCore::input() {
}

for (auto const &entity : entities) {
entity->input ();
entity->input (inputManager);
}
}

Expand All @@ -250,6 +230,20 @@ std::vector<Entity *> &EngineCore::getEntities() {
return entities;
}

void EngineCore::constructPlayer() {
Transform playerTrans (glm::vec3(0, 1000, 0), glm::vec3(0), glm::vec3(10));
PhysicsComponent *playerPhysicsComponent = new PhysicsComponent (PhysicsComponent::BoundingBodyType::CAPSULE,
glm::vec3(10),
50.0f);
entities.push_back ((new Player (renderingMaster->getCamera(), playerTrans))
->addComponent (playerPhysicsComponent));
playerPhysicsComponent->getRigidBody()->setDamping(btScalar(0.1), btScalar(0.1));
playerPhysicsComponent->getRigidBody()->setSleepingThresholds(0.0, 0.0);
playerPhysicsComponent->getRigidBody()->setAngularFactor(0.0);
playerPhysicsComponent->getRigidBody()->setFriction(0.7);
playerPhysicsComponent->getRigidBody()->setRestitution(0.6);
}

EngineCore::~EngineCore() {
for (auto entity : entities) {
delete entity;
Expand Down
1 change: 1 addition & 0 deletions Core/EngineCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class EngineCore
float mouseMoveSpeed, mouseRotationSpeed;
bool warpMouse, showMouse;

void constructPlayer();
};

#endif // ENGINECORE_H
6 changes: 3 additions & 3 deletions Core/Entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ void Entity::update() {
}
}

void Entity::input() {
void Entity::input(Input &inputManager) {
for (auto const &it : components) {
if (it.second != NULL) {
it.second->input();
it.second->input(inputManager);
}
}

for (unsigned int i = 0; i < children.size (); i++) {
children[i]->input ();
children[i]->input (inputManager);
}
}

Expand Down
3 changes: 2 additions & 1 deletion Core/Entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "Component.h"
#include <vector>
#include <unordered_map>
#include "Input.h"

class Component;

Expand All @@ -17,7 +18,7 @@ class Entity
Transform &getTransform();
void setTransform(const Transform &transform);
Component *getComponent (const unsigned int flag);
void input();
void input(Input &inputManager);
void update();
void render();
virtual ~Entity();
Expand Down
29 changes: 27 additions & 2 deletions Core/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,33 @@ const unsigned int Player::FirstPersonComponent::getFlag() const {
return Entity::Flags::THIRD_PERSON_CAMERA;
}

void Player::FirstPersonComponent::input() {

void Player::FirstPersonComponent::input(Input &inputManager) {
PhysicsComponent *physicsComponent = dynamic_cast<PhysicsComponent *> (_entity->getComponent (Entity::Flags::DYNAMIC));

if (physicsComponent != NULL) {
glm::vec3 forward = camera->getForward();
glm::vec3 right = camera->getRight();
glm::vec3 vel(0);

if (inputManager.getKey(SDLK_w)) {
vel += glm::vec3 (forward.x, 0, forward.z);
} else if (inputManager.getKey(SDLK_s)) {
vel -= glm::vec3 (forward.x, 0, forward.z);
}

if (inputManager.getKey(SDLK_d)) {
vel += glm::vec3 (right.x, 0, right.z);
} else if (inputManager.getKey(SDLK_a)) {
vel -= glm::vec3 (right.x, 0, right.z);
}

if (glm::length (vel) != 0) {
vel = glm::normalize(vel);
}
btVector3 velBt(vel.x, 0, vel.z);
velBt *= PLAYER_SPEED;
physicsComponent->getRigidBody()->applyCentralForce(velBt);
}
}

void Player::FirstPersonComponent::update() {
Expand Down
6 changes: 4 additions & 2 deletions Core/Player.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

#include "Entity.h"
#include "Camera.h"
#include "PhysicsComponent.h"

#define NECK_HEIGHT 20
#define NECK_HEIGHT 10
#define PLAYER_SPEED 450

class Player : public Entity
{
Expand All @@ -20,7 +22,7 @@ class Player : public Entity
public:
FirstPersonComponent (Camera *camera);

void input();
void input(Input &inputManager);
void update();
void render();
const unsigned int getFlag() const;
Expand Down
5 changes: 4 additions & 1 deletion Physics/PhysicsComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ PhysicsComponent::PhysicsComponent(BoundingBodyType type, glm::vec3 scale, float
bodyMass (mass) {
}

void PhysicsComponent::input() {
void PhysicsComponent::input(Input &inputManager) {

}

Expand Down Expand Up @@ -60,6 +60,9 @@ void PhysicsComponent::init() {
assert (boundingBodyScale.y == boundingBodyScale.z);
collisionShape = new btSphereShape(boundingBodyScale.x / 2.0f);
break;
case BoundingBodyType::CAPSULE:
collisionShape = new btCapsuleShape(boundingBodyScale.x*2.0f, boundingBodyScale.y*2.0f);
break;
default:
assert (false);
}
Expand Down
3 changes: 2 additions & 1 deletion Physics/PhysicsComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ class PhysicsComponent : public Component
enum BoundingBodyType {
CUBE,
SPHERE,
CAPSULE,
SIMPLIFIED_MESH,
};

PhysicsComponent(BoundingBodyType type, glm::vec3 scale, float mass);
void input();
void input(Input &inputManager);
void update();
void render();
void init();
Expand Down
2 changes: 1 addition & 1 deletion Rendering/RenderComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ RenderComponent::RenderComponent(Mesh * mesh,
assert (result);
}

void RenderComponent::input() {
void RenderComponent::input(Input &inputManager) {

}

Expand Down
2 changes: 1 addition & 1 deletion Rendering/RenderComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class RenderComponent : public Component
{
public:
RenderComponent(Mesh *mesh, Shader *shader, Texture *texture, const Material &material);
void input();
void input(Input &inputManager);
void update();
void render();
const unsigned int getFlag() const;
Expand Down
2 changes: 1 addition & 1 deletion Rendering/TempAnimationComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ TempAnimationComponent::TempAnimationComponent(glm::vec3 addPosition,
{
}

void TempAnimationComponent::input() {
void TempAnimationComponent::input(Input &inputManager) {

}

Expand Down
2 changes: 1 addition & 1 deletion Rendering/TempAnimationComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class TempAnimationComponent : public Component
glm::vec3 addRotation,
glm::vec3 addScale);
glm::vec3 m_addPosition, m_addRotation, m_addScale;
void input();
void input(Input &inputManager);
void update();
void render();
const unsigned int getFlag() const;
Expand Down
2 changes: 1 addition & 1 deletion res/configs/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"camera" : {
"near" : 1.0,
"far" : 5000.0,
"fov" : 80.0
"fov" : 75.0
},
"mouse" : {
"moveSpeed" : 1.0,
Expand Down
Binary file added res/textures/196.bmp
Binary file not shown.

0 comments on commit 2a5f18f

Please sign in to comment.