PlayerController class (more OOP)
This commit is contained in:
+12
-14
@@ -1,6 +1,5 @@
|
|||||||
#include "player_control.h"
|
#include "player_control.h"
|
||||||
|
|
||||||
#include "graphics/LineBatch.h"
|
|
||||||
#include "objects/Player.h"
|
#include "objects/Player.h"
|
||||||
#include "physics/PhysicsSolver.h"
|
#include "physics/PhysicsSolver.h"
|
||||||
#include "physics/Hitbox.h"
|
#include "physics/Hitbox.h"
|
||||||
@@ -30,10 +29,11 @@
|
|||||||
#define CHEAT_SPEED_MUL 5.0f
|
#define CHEAT_SPEED_MUL 5.0f
|
||||||
#define JUMP_FORCE 7.0f
|
#define JUMP_FORCE 7.0f
|
||||||
|
|
||||||
void update_controls(PhysicsSolver* physics,
|
PlayerController::PlayerController(Level* level) : level(level) {
|
||||||
Chunks* chunks,
|
}
|
||||||
Player* player,
|
|
||||||
float delta){
|
void PlayerController::update_controls(float delta){
|
||||||
|
Player* player = level->player;
|
||||||
|
|
||||||
if (Events::jpressed(GLFW_KEY_ESCAPE)){
|
if (Events::jpressed(GLFW_KEY_ESCAPE)){
|
||||||
Window::setShouldClose(true);
|
Window::setShouldClose(true);
|
||||||
@@ -65,7 +65,7 @@ void update_controls(PhysicsSolver* physics,
|
|||||||
}
|
}
|
||||||
int substeps = (int)(delta * 1000);
|
int substeps = (int)(delta * 1000);
|
||||||
substeps = (substeps <= 0 ? 1 : (substeps > 100 ? 100 : substeps));
|
substeps = (substeps <= 0 ? 1 : (substeps > 100 ? 100 : substeps));
|
||||||
physics->step(chunks, hitbox, delta, substeps, shift, player->flight ? 0.0f : 1.0f);
|
level->physics->step(level->chunks, hitbox, delta, substeps, shift, player->flight ? 0.0f : 1.0f);
|
||||||
camera->position.x = hitbox->position.x;
|
camera->position.x = hitbox->position.x;
|
||||||
camera->position.y = hitbox->position.y + 0.7f;
|
camera->position.y = hitbox->position.y + 0.7f;
|
||||||
camera->position.z = hitbox->position.z;
|
camera->position.z = hitbox->position.z;
|
||||||
@@ -169,22 +169,18 @@ void update_controls(PhysicsSolver* physics,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void update_interaction(Level* level, LineBatch* lineBatch){
|
void PlayerController::update_interaction(){
|
||||||
Chunks* chunks = level->chunks;
|
Chunks* chunks = level->chunks;
|
||||||
Player* player = level->player;
|
Player* player = level->player;
|
||||||
Camera* camera = player->camera;
|
|
||||||
Lighting* lighting = level->lighting;
|
Lighting* lighting = level->lighting;
|
||||||
|
Camera* camera = player->camera;
|
||||||
vec3 end;
|
vec3 end;
|
||||||
vec3 norm;
|
vec3 norm;
|
||||||
vec3 iend;
|
vec3 iend;
|
||||||
voxel* vox = chunks->rayCast(camera->position, camera->front, 10.0f, end, norm, iend);
|
voxel* vox = chunks->rayCast(camera->position, camera->front, 10.0f, end, norm, iend);
|
||||||
if (vox != nullptr){
|
if (vox != nullptr){
|
||||||
if (Block::blocks[vox->id]->model == 1){
|
selectedBlockId = vox->id;
|
||||||
lineBatch->box(iend.x+0.5f, iend.y+0.5f, iend.z+0.5f, 1.005f,1.005f,1.005f, 0,0,0,0.5f);
|
selectedBlockPosition = iend;
|
||||||
} else if (Block::blocks[vox->id]->model == 2){
|
|
||||||
lineBatch->box(iend.x+0.4f, iend.y+0.3f, iend.z+0.4f, 0.805f,0.805f,0.805f, 0,0,0,0.5f);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
if (Events::jclicked(GLFW_MOUSE_BUTTON_1) && Block::blocks[vox->id]->breakable){
|
if (Events::jclicked(GLFW_MOUSE_BUTTON_1) && Block::blocks[vox->id]->breakable){
|
||||||
int x = (int)iend.x;
|
int x = (int)iend.x;
|
||||||
@@ -208,5 +204,7 @@ void update_interaction(Level* level, LineBatch* lineBatch){
|
|||||||
int z = (int)iend.z;
|
int z = (int)iend.z;
|
||||||
player->choosenBlock = chunks->get(x,y,z)->id;
|
player->choosenBlock = chunks->get(x,y,z)->id;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
selectedBlockId = -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+11
-3
@@ -1,13 +1,21 @@
|
|||||||
#ifndef PLAYER_CONTROL_H_
|
#ifndef PLAYER_CONTROL_H_
|
||||||
#define PLAYER_CONTROL_H_
|
#define PLAYER_CONTROL_H_
|
||||||
|
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
|
||||||
class PhysicsSolver;
|
class PhysicsSolver;
|
||||||
class Chunks;
|
class Chunks;
|
||||||
class Player;
|
class Player;
|
||||||
class LineBatch;
|
|
||||||
class Level;
|
class Level;
|
||||||
|
|
||||||
void update_controls(PhysicsSolver* physics, Chunks* chunks, Player* player, float delta);
|
class PlayerController {
|
||||||
void update_interaction(Level* level, LineBatch* lineBatch);
|
Level* level;
|
||||||
|
public:
|
||||||
|
glm::vec3 selectedBlockPosition;
|
||||||
|
int selectedBlockId = -1;
|
||||||
|
PlayerController(Level* level);
|
||||||
|
void update_controls(float delta);
|
||||||
|
void update_interaction();
|
||||||
|
};
|
||||||
|
|
||||||
#endif /* PLAYER_CONTROL_H_ */
|
#endif /* PLAYER_CONTROL_H_ */
|
||||||
|
|||||||
+17
-4
@@ -78,9 +78,9 @@ void write_world(World* world, Level* level){
|
|||||||
world->wfile->writePlayer(level->player);
|
world->wfile->writePlayer(level->player);
|
||||||
}
|
}
|
||||||
|
|
||||||
void update_level(World* world, Level* level, vec3 position, float delta, long frame, VoxelRenderer* renderer, LineBatch* lineBatch){
|
void update_level(World* world, Level* level, vec3 position, float delta, long frame, VoxelRenderer* renderer, PlayerController* playerController){
|
||||||
update_controls(level->physics, level->chunks, level->player, delta);
|
playerController->update_controls(delta);
|
||||||
update_interaction(level, lineBatch);
|
playerController->update_interaction();
|
||||||
|
|
||||||
level->chunks->setCenter(world->wfile, position.x, 0, position.z);
|
level->chunks->setCenter(world->wfile, position.x, 0, position.z);
|
||||||
int freeLoaders = level->chunksController->countFreeLoaders();
|
int freeLoaders = level->chunksController->countFreeLoaders();
|
||||||
@@ -137,6 +137,7 @@ int main() {
|
|||||||
std::cout << "-- preparing systems" << std::endl;
|
std::cout << "-- preparing systems" << std::endl;
|
||||||
HudRenderer hud;
|
HudRenderer hud;
|
||||||
WorldRenderer worldRenderer(level);
|
WorldRenderer worldRenderer(level);
|
||||||
|
PlayerController playerController(level);
|
||||||
|
|
||||||
float lastTime = glfwGetTime();
|
float lastTime = glfwGetTime();
|
||||||
float delta = 0.0f;
|
float delta = 0.0f;
|
||||||
@@ -161,8 +162,18 @@ int main() {
|
|||||||
devdata = !devdata;
|
devdata = !devdata;
|
||||||
}
|
}
|
||||||
|
|
||||||
update_level(world, level, camera->position, delta, frame, worldRenderer.renderer, worldRenderer.lineBatch);
|
update_level(world, level, camera->position, delta, frame, worldRenderer.renderer, &playerController);
|
||||||
worldRenderer.draw(world, camera, assets, occlusion);
|
worldRenderer.draw(world, camera, assets, occlusion);
|
||||||
|
if (playerController.selectedBlockId != -1){
|
||||||
|
Block* selectedBlock = Block::blocks[playerController.selectedBlockId];
|
||||||
|
LineBatch* lineBatch = worldRenderer.lineBatch;
|
||||||
|
vec3 pos = playerController.selectedBlockPosition;
|
||||||
|
if (selectedBlock->model == 1){
|
||||||
|
lineBatch->box(pos.x+0.5f, pos.y+0.5f, pos.z+0.5f, 1.005f,1.005f,1.005f, 0,0,0,0.5f);
|
||||||
|
} else if (selectedBlock->model == 2){
|
||||||
|
lineBatch->box(pos.x+0.4f, pos.y+0.3f, pos.z+0.4f, 0.805f,0.805f,0.805f, 0,0,0,0.5f);
|
||||||
|
}
|
||||||
|
}
|
||||||
hud.draw(world, level, assets, devdata, fps);
|
hud.draw(world, level, assets, devdata, fps);
|
||||||
|
|
||||||
Window::swapBuffers();
|
Window::swapBuffers();
|
||||||
@@ -170,6 +181,8 @@ int main() {
|
|||||||
}
|
}
|
||||||
std::cout << "-- saving world" << std::endl;
|
std::cout << "-- saving world" << std::endl;
|
||||||
write_world(world, level);
|
write_world(world, level);
|
||||||
|
|
||||||
|
delete level;
|
||||||
delete world;
|
delete world;
|
||||||
|
|
||||||
std::cout << "-- shutting down" << std::endl;
|
std::cout << "-- shutting down" << std::endl;
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
void setSeed(int number){
|
void setSeed(int number){
|
||||||
seed = (unsigned short)number+23729 xor (unsigned short)number+16786;
|
seed = ((unsigned short)number+23729 xor (unsigned short)number+16786);
|
||||||
rand();
|
rand();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -102,7 +102,7 @@ void WorldGenerator::generate(voxel* voxels, int cx, int cy, int cz, int seed){
|
|||||||
|
|
||||||
float heights[CHUNK_VOL];
|
float heights[CHUNK_VOL];
|
||||||
|
|
||||||
std::cout << calc_height(&noise, cx, cy) << "\n";
|
// std::cout << calc_height(&noise, cx, cy) << "\n";
|
||||||
|
|
||||||
for (int z = 0; z < CHUNK_D; z++){
|
for (int z = 0; z < CHUNK_D; z++){
|
||||||
for (int x = 0; x < CHUNK_W; x++){
|
for (int x = 0; x < CHUNK_W; x++){
|
||||||
@@ -130,11 +130,11 @@ void WorldGenerator::generate(voxel* voxels, int cx, int cy, int cz, int seed){
|
|||||||
id = 1;
|
id = 1;
|
||||||
} else {
|
} else {
|
||||||
int tree = generate_tree(&noise, &randomtree, heights, real_x, real_y, real_z, 16);
|
int tree = generate_tree(&noise, &randomtree, heights, real_x, real_y, real_z, 16);
|
||||||
if (tree)
|
if (tree) {
|
||||||
id = tree;
|
id = tree;
|
||||||
else if ((tree = generate_tree(&noise, &randomtree, heights, real_x, real_y, real_z, 19))){
|
} else if ((tree = generate_tree(&noise, &randomtree, heights, real_x, real_y, real_z, 19))){
|
||||||
id = tree;
|
id = tree;
|
||||||
}else if ((tree = generate_tree(&noise, &randomtree, heights, real_x, real_y, real_z, 23))){
|
} else if ((tree = generate_tree(&noise, &randomtree, heights, real_x, real_y, real_z, 23))){
|
||||||
id = tree;
|
id = tree;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user