Moved player struct into world
This commit is contained in:
132
src/gameloop.c
132
src/gameloop.c
@@ -19,6 +19,9 @@
|
||||
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
||||
|
||||
World world = { 0 };
|
||||
Player *player = &world.player;
|
||||
|
||||
static Coords playerOffsetPos = { 0.0, 0.0, 0.0 };
|
||||
|
||||
int gameState = STATE_TITLE;
|
||||
int gamePopup;
|
||||
@@ -26,9 +29,6 @@ int gamePopup;
|
||||
static int guiOn;
|
||||
static int debugOn;
|
||||
|
||||
Player player = { 0 };
|
||||
|
||||
static Coords playerOffsetPos = { 0.0, 0.0, 0.0 };
|
||||
static SDL_Rect backgroundRect;
|
||||
static char *errorMessage = NULL;
|
||||
static long l;
|
||||
@@ -49,11 +49,11 @@ static u_int32_t
|
||||
void gameLoop_resetGame () {
|
||||
l = SDL_GetTicks();
|
||||
|
||||
player = (const Player) { 0 };
|
||||
player.pos.x = 40;
|
||||
*player = (const Player) { 0 };
|
||||
player->pos.x = 40;
|
||||
// player.pos.x = 32.5;
|
||||
player.pos.y = 16;
|
||||
player.pos.z = -4;
|
||||
player->pos.y = 16;
|
||||
player->pos.z = -4;
|
||||
// player.pos.z = 32.5;
|
||||
|
||||
gamePopup = 0;
|
||||
@@ -107,7 +107,7 @@ int gameLoop (
|
||||
|
||||
case STATE_LOADING:
|
||||
// Generate a world and present a loading screen
|
||||
if (state_loading(renderer, &world, world.seed, player.pos)) {
|
||||
if (state_loading(renderer, &world, world.seed, player->pos)) {
|
||||
gameLoop_resetGame();
|
||||
gameState = 5;
|
||||
};
|
||||
@@ -201,29 +201,29 @@ static void gameLoop_gameplay (SDL_Renderer *renderer, Inputs *inputs) {
|
||||
// }
|
||||
|
||||
;int headInWater = World_getBlock (&world,
|
||||
player.pos.x,
|
||||
player.pos.y,
|
||||
player.pos.z) == BLOCK_WATER;
|
||||
player->pos.x,
|
||||
player->pos.y,
|
||||
player->pos.z) == BLOCK_WATER;
|
||||
|
||||
int feetInWater = World_getBlock (&world,
|
||||
player.pos.x,
|
||||
player.pos.y + 1,
|
||||
player.pos.z) == BLOCK_WATER;
|
||||
player->pos.x,
|
||||
player->pos.y + 1,
|
||||
player->pos.z) == BLOCK_WATER;
|
||||
|
||||
int effectDrawDistance = data_options.drawDistance;
|
||||
// Restrict view distance while in water
|
||||
if (headInWater) { effectDrawDistance = 10; }
|
||||
|
||||
// Update directional vectors
|
||||
player.vectorH.x = sin(player.hRot);
|
||||
player.vectorH.y = cos(player.hRot);
|
||||
player.vectorV.x = sin(player.vRot);
|
||||
player.vectorV.y = cos(player.vRot);
|
||||
player->vectorH.x = sin(player->hRot);
|
||||
player->vectorH.y = cos(player->hRot);
|
||||
player->vectorV.x = sin(player->vRot);
|
||||
player->vectorV.y = cos(player->vRot);
|
||||
|
||||
// Update offset player position
|
||||
playerOffsetPos.x = player.pos.x + PLAYER_POSITION_OFFSET;
|
||||
playerOffsetPos.y = player.pos.y + PLAYER_POSITION_OFFSET;
|
||||
playerOffsetPos.z = player.pos.z + PLAYER_POSITION_OFFSET;
|
||||
playerOffsetPos.x = player->pos.x + PLAYER_POSITION_OFFSET;
|
||||
playerOffsetPos.y = player->pos.y + PLAYER_POSITION_OFFSET;
|
||||
playerOffsetPos.z = player->pos.z + PLAYER_POSITION_OFFSET;
|
||||
|
||||
// Skybox, basically
|
||||
float timeCoef;
|
||||
@@ -286,8 +286,8 @@ static void gameLoop_gameplay (SDL_Renderer *renderer, Inputs *inputs) {
|
||||
|
||||
if (gamePopup == POPUP_HUD) {
|
||||
if (blockSelected) {
|
||||
InvSlot *activeSlot = &player.inventory.hotbar [
|
||||
player.inventory.hotbarSelect
|
||||
InvSlot *activeSlot = &player->inventory.hotbar [
|
||||
player->inventory.hotbarSelect
|
||||
];
|
||||
|
||||
// Breaking blocks
|
||||
@@ -307,7 +307,7 @@ static void gameLoop_gameplay (SDL_Renderer *renderer, Inputs *inputs) {
|
||||
.durability = 1
|
||||
};
|
||||
|
||||
Inventory_transferIn(&player.inventory, &pickedUp);
|
||||
Inventory_transferIn(&player->inventory, &pickedUp);
|
||||
|
||||
World_setBlock (
|
||||
&world,
|
||||
@@ -327,9 +327,9 @@ static void gameLoop_gameplay (SDL_Renderer *renderer, Inputs *inputs) {
|
||||
if (
|
||||
// Player cannot be obstructing the block
|
||||
(
|
||||
fabs(player.pos.x - 0.5 - blockSelectOffset.x) >= 0.8 ||
|
||||
fabs(player.pos.y - blockSelectOffset.y) >= 1.45 ||
|
||||
fabs(player.pos.z - 0.5 - blockSelectOffset.z) >= 0.8
|
||||
fabs(player->pos.x - 0.5 - blockSelectOffset.x) >= 0.8 ||
|
||||
fabs(player->pos.y - blockSelectOffset.y) >= 1.45 ||
|
||||
fabs(player->pos.z - 0.5 - blockSelectOffset.z) >= 0.8
|
||||
) &&
|
||||
// Player must have enough of that block
|
||||
activeSlot->amount > 0
|
||||
@@ -391,29 +391,29 @@ static void gameLoop_gameplay (SDL_Renderer *renderer, Inputs *inputs) {
|
||||
if (inputs->keyboard.f) {
|
||||
inputs->keyboard.f = 0;
|
||||
InvSlot_swap (
|
||||
&player.inventory.hotbar[player.inventory.hotbarSelect],
|
||||
&player.inventory.offhand
|
||||
&player->inventory.hotbar[player->inventory.hotbarSelect],
|
||||
&player->inventory.offhand
|
||||
);
|
||||
}
|
||||
|
||||
// Select hotbar slots with scroll wheel
|
||||
if (inputs->mouse.wheel != 0) {
|
||||
player.inventory.hotbarSelect -= inputs->mouse.wheel;
|
||||
player.inventory.hotbarSelect = nmod (
|
||||
player.inventory.hotbarSelect, 9);
|
||||
player->inventory.hotbarSelect -= inputs->mouse.wheel;
|
||||
player->inventory.hotbarSelect = nmod (
|
||||
player->inventory.hotbarSelect, 9);
|
||||
inputs->mouse.wheel = 0;
|
||||
}
|
||||
|
||||
// Select hotbar slots with number keys
|
||||
if (inputs->keyboard.num1) { player.inventory.hotbarSelect = 0; }
|
||||
if (inputs->keyboard.num2) { player.inventory.hotbarSelect = 1; }
|
||||
if (inputs->keyboard.num3) { player.inventory.hotbarSelect = 2; }
|
||||
if (inputs->keyboard.num4) { player.inventory.hotbarSelect = 3; }
|
||||
if (inputs->keyboard.num5) { player.inventory.hotbarSelect = 4; }
|
||||
if (inputs->keyboard.num6) { player.inventory.hotbarSelect = 5; }
|
||||
if (inputs->keyboard.num7) { player.inventory.hotbarSelect = 6; }
|
||||
if (inputs->keyboard.num8) { player.inventory.hotbarSelect = 7; }
|
||||
if (inputs->keyboard.num9) { player.inventory.hotbarSelect = 8; }
|
||||
if (inputs->keyboard.num1) { player->inventory.hotbarSelect = 0; }
|
||||
if (inputs->keyboard.num2) { player->inventory.hotbarSelect = 1; }
|
||||
if (inputs->keyboard.num3) { player->inventory.hotbarSelect = 2; }
|
||||
if (inputs->keyboard.num4) { player->inventory.hotbarSelect = 3; }
|
||||
if (inputs->keyboard.num5) { player->inventory.hotbarSelect = 4; }
|
||||
if (inputs->keyboard.num6) { player->inventory.hotbarSelect = 5; }
|
||||
if (inputs->keyboard.num7) { player->inventory.hotbarSelect = 6; }
|
||||
if (inputs->keyboard.num8) { player->inventory.hotbarSelect = 7; }
|
||||
if (inputs->keyboard.num9) { player->inventory.hotbarSelect = 8; }
|
||||
}
|
||||
|
||||
/* Cast rays. selectedPass passes wether or not a block is
|
||||
@@ -436,10 +436,10 @@ static void gameLoop_gameplay (SDL_Renderer *renderer, Inputs *inputs) {
|
||||
// Ray offset Z?
|
||||
f21 = 1.0;
|
||||
|
||||
f22 = f21 * player.vectorV.y + rayOffsetY * player.vectorV.x;
|
||||
f23 = rayOffsetY * player.vectorV.y - f21 * player.vectorV.x;
|
||||
f24 = rayOffsetX * player.vectorH.y + f22 * player.vectorH.x;
|
||||
f25 = f22 * player.vectorH.y - rayOffsetX * player.vectorH.x;
|
||||
f22 = f21 * player->vectorV.y + rayOffsetY * player->vectorV.x;
|
||||
f23 = rayOffsetY * player->vectorV.y - f21 * player->vectorV.x;
|
||||
f24 = rayOffsetX * player->vectorH.y + f22 * player->vectorH.x;
|
||||
f25 = f22 * player->vectorH.y - rayOffsetX * player->vectorH.x;
|
||||
|
||||
double rayDistanceLimit = effectDrawDistance;
|
||||
|
||||
@@ -684,7 +684,7 @@ void gameLoop_drawPopup (SDL_Renderer *renderer, Inputs *inputs) {
|
||||
if (data_options.trapMouse) SDL_SetRelativeMouseMode(1);
|
||||
if (guiOn) popup_hud (
|
||||
renderer, inputs, &world,
|
||||
&debugOn, &fps_now, &player
|
||||
&debugOn, &fps_now, player
|
||||
);
|
||||
break;
|
||||
|
||||
@@ -704,7 +704,7 @@ void gameLoop_drawPopup (SDL_Renderer *renderer, Inputs *inputs) {
|
||||
|
||||
case POPUP_INVENTORY:
|
||||
// Inventory
|
||||
popup_inventory(renderer, inputs, &player, &gamePopup);
|
||||
popup_inventory(renderer, inputs, player, &gamePopup);
|
||||
break;
|
||||
|
||||
#ifndef small
|
||||
@@ -719,7 +719,7 @@ void gameLoop_drawPopup (SDL_Renderer *renderer, Inputs *inputs) {
|
||||
// Chunk peek
|
||||
tblack(renderer);
|
||||
SDL_RenderFillRect(renderer, &backgroundRect);
|
||||
popup_chunkPeek(renderer, inputs, &world, &gamePopup, &player);
|
||||
popup_chunkPeek(renderer, inputs, &world, &gamePopup, player);
|
||||
break;
|
||||
|
||||
case POPUP_ROLL_CALL:
|
||||
@@ -754,8 +754,8 @@ static void gameLoop_processMovement (Inputs *inputs, int inWater) {
|
||||
if (gamePopup == 0) {
|
||||
// Looking around
|
||||
if (data_options.trapMouse) {
|
||||
player.hRot += (float)inputs->mouse.x / 64;
|
||||
player.vRot -= (float)inputs->mouse.y / 64;
|
||||
player->hRot += (float)inputs->mouse.x / 64;
|
||||
player->vRot -= (float)inputs->mouse.y / 64;
|
||||
} else {
|
||||
float cameraMoveX =
|
||||
(inputs->mouse.x - BUFFER_W * 2) /
|
||||
@@ -772,24 +772,24 @@ static void gameLoop_processMovement (Inputs *inputs, int inWater) {
|
||||
cameraMoveDistance = 0.0;
|
||||
}
|
||||
if (cameraMoveDistance > 0.0) {
|
||||
player.hRot += cameraMoveX *
|
||||
player->hRot += cameraMoveX *
|
||||
cameraMoveDistance / 400.0;
|
||||
player.vRot -= cameraMoveY *
|
||||
player->vRot -= cameraMoveY *
|
||||
cameraMoveDistance / 400.0;
|
||||
}
|
||||
}
|
||||
|
||||
// Restrict camera vertical position
|
||||
if (player.vRot < -1.57) player.vRot = -1.57;
|
||||
if (player.vRot > 1.57) player.vRot = 1.57;
|
||||
if (player->vRot < -1.57) player->vRot = -1.57;
|
||||
if (player->vRot > 1.57) player->vRot = 1.57;
|
||||
|
||||
float speed = 0.02;
|
||||
|
||||
if (doPhysics) {
|
||||
player.FBVelocity =
|
||||
player->FBVelocity =
|
||||
(inputs->keyboard.w - inputs->keyboard.s) *
|
||||
speed;
|
||||
player.LRVelocity =
|
||||
player->LRVelocity =
|
||||
(inputs->keyboard.d - inputs->keyboard.a) *
|
||||
speed;
|
||||
}
|
||||
@@ -804,11 +804,11 @@ static void gameLoop_processMovement (Inputs *inputs, int inWater) {
|
||||
playerMovement.z *= 0.5;
|
||||
|
||||
playerMovement.x +=
|
||||
player.vectorH.x * player.FBVelocity +
|
||||
player.vectorH.y * player.LRVelocity;
|
||||
player->vectorH.x * player->FBVelocity +
|
||||
player->vectorH.y * player->LRVelocity;
|
||||
playerMovement.z +=
|
||||
player.vectorH.y * player.FBVelocity -
|
||||
player.vectorH.x * player.LRVelocity;
|
||||
player->vectorH.y * player->FBVelocity -
|
||||
player->vectorH.x * player->LRVelocity;
|
||||
playerMovement.y += 0.003;
|
||||
}
|
||||
|
||||
@@ -818,9 +818,9 @@ static void gameLoop_processMovement (Inputs *inputs, int inWater) {
|
||||
if (!doPhysics) { break; }
|
||||
|
||||
Coords playerPosTry = {
|
||||
player.pos.x + playerMovement.x * (float)((axis + 2) % 3 / 2),
|
||||
player.pos.y + playerMovement.y * (float)((axis + 1) % 3 / 2),
|
||||
player.pos.z + playerMovement.z * (float)((axis + 3) % 3 / 2),
|
||||
player->pos.x + playerMovement.x * (float)((axis + 2) % 3 / 2),
|
||||
player->pos.y + playerMovement.y * (float)((axis + 1) % 3 / 2),
|
||||
player->pos.z + playerMovement.z * (float)((axis + 3) % 3 / 2),
|
||||
};
|
||||
|
||||
for (int step = 0; step < 12; step++) {
|
||||
@@ -869,9 +869,9 @@ static void gameLoop_processMovement (Inputs *inputs, int inWater) {
|
||||
|
||||
}
|
||||
|
||||
player.pos.x = playerPosTry.x;
|
||||
player.pos.y = playerPosTry.y;
|
||||
player.pos.z = playerPosTry.z;
|
||||
player->pos.x = playerPosTry.x;
|
||||
player->pos.y = playerPosTry.y;
|
||||
player->pos.z = playerPosTry.z;
|
||||
|
||||
stopCheck:;
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include "blocks.h"
|
||||
#include "coords.h"
|
||||
#include "utility.h"
|
||||
#include "player.h"
|
||||
|
||||
#define CHUNKARR_DIAM 3
|
||||
#define CHUNKARR_RAD (CHUNKARR_DIAM - 1) / 2
|
||||
@@ -59,6 +60,7 @@ struct _Chunk {
|
||||
* Stores chunks.
|
||||
*/
|
||||
struct _World {
|
||||
Player player;
|
||||
int type;
|
||||
u_int64_t seed;
|
||||
int dayNightMode;
|
||||
|
||||
Reference in New Issue
Block a user