Some more hopefully working progress
This commit is contained in:
210
player/player.c
210
player/player.c
@@ -5,16 +5,27 @@
|
||||
#include <SDL2/SDL_rect.h>
|
||||
#include "player.h"
|
||||
#include "../tiles/tile.h"
|
||||
#include "../util/font.h"
|
||||
|
||||
#define HEALTH_MARGIN 4
|
||||
|
||||
int playerSpeed = 4;
|
||||
int playerSpeed = 2;
|
||||
|
||||
int playerReach = 4;
|
||||
int playerReach = DISPLAY_MAP_HEIGHT / 2 - 2;
|
||||
|
||||
SDL_Texture *entityTexture;
|
||||
SDL_Texture *hudTexture;
|
||||
|
||||
SDL_Texture *PlayerTexture;
|
||||
SDL_Rect PlayerRect;
|
||||
|
||||
SDL_Rect targetItemBGRect;
|
||||
|
||||
SDL_Rect targetItemRect;
|
||||
|
||||
SDL_Color healthBarColor = {0, 240, 0, 255};
|
||||
SDL_Color breakingBarColor = {128, 128, 0, 255};
|
||||
|
||||
void setActivePlayerSlot(Player *plr, uint16_t activeSlotIndex) {
|
||||
activeSlotIndex = activeSlotIndex % itemRegistryIndex;
|
||||
if (activeSlotIndex <= 0) {
|
||||
@@ -35,7 +46,7 @@ void moveActivePlayerSlot(Player *plr, bool up, bool seek) {
|
||||
if (newSlot == prevSlot) break;
|
||||
|
||||
// Stop if we found a slot with count > 0
|
||||
if (plr->inventory.slotCounts[newSlot] > 0) break;
|
||||
if (plr->inventory.slotCounts[newSlot] > 0 && newSlot != 0) break;
|
||||
} while (true);
|
||||
plr->inventory.activeSlotIndex = newSlot;
|
||||
} else {
|
||||
@@ -52,58 +63,88 @@ void moveActivePlayerSlot(Player *plr, bool up, bool seek) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
int playerX = (MAP_WIDTH / 2) * 16;
|
||||
int playerY = (MAP_HEIGHT / 2) * 16;
|
||||
|
||||
void adjustRect(SDL_Rect *rect) {
|
||||
rect->x -= playerX;
|
||||
rect->y -= playerY;
|
||||
void adjustRect(SDL_Rect *rect, SDL_Rect playerRect) {
|
||||
rect->x -= playerRect.x;
|
||||
rect->y -= playerRect.y;
|
||||
rect->x += DISPLAY_WIDTH / 2;
|
||||
rect->y += DISPLAY_HEIGHT / 2;
|
||||
}
|
||||
|
||||
bool isInboundsTile(int x, int y) {
|
||||
return (playerX / TILE_SIZE) - (DISPLAY_MAP_WIDTH / 2) < x &&
|
||||
(playerY / TILE_SIZE) - (DISPLAY_MAP_HEIGHT / 2) < y &&
|
||||
(playerX / TILE_SIZE) + (DISPLAY_MAP_WIDTH / 2) > x && (playerX / TILE_SIZE) + (DISPLAY_MAP_WIDTH / 2) > y;
|
||||
}
|
||||
//bool isInboundsTile(int x, int y) {
|
||||
// return (playerX / TILE_SIZE) - (DISPLAY_MAP_WIDTH / 2) < x &&
|
||||
// (playerY / TILE_SIZE) - (DISPLAY_MAP_HEIGHT / 2) < y &&
|
||||
// (playerX / TILE_SIZE) + (DISPLAY_MAP_WIDTH / 2) > x && (playerX / TILE_SIZE) + (DISPLAY_MAP_WIDTH / 2) > y;
|
||||
//}
|
||||
//
|
||||
//bool isInbounds(int x, int y) {
|
||||
// return x > 0 && y > 0 && x < DISPLAY_WIDTH && y < DISPLAY_HEIGHT;
|
||||
//}
|
||||
|
||||
bool isInbounds(int x, int y) {
|
||||
return x > 0 && y > 0 && x < DISPLAY_WIDTH && y < DISPLAY_HEIGHT;
|
||||
}
|
||||
|
||||
bool isInboundsRect(SDL_Rect rect) {
|
||||
if (isInbounds(rect.x, rect.y)) {
|
||||
return true;
|
||||
}
|
||||
if (rect.x < 0) {
|
||||
rect.x += rect.w;
|
||||
}
|
||||
if (rect.y < 0) {
|
||||
rect.y += rect.h;
|
||||
}
|
||||
if (isInbounds(rect.x, rect.y)) {
|
||||
return true;
|
||||
}
|
||||
if (rect.x > DISPLAY_WIDTH) {
|
||||
rect.x -= rect.w;
|
||||
}
|
||||
if (rect.y > DISPLAY_HEIGHT) {
|
||||
rect.y -= rect.h;
|
||||
}
|
||||
return isInbounds(rect.x, rect.y);
|
||||
}
|
||||
//bool isInboundsRect(SDL_Rect rect) {
|
||||
// if (isInbounds(rect.x, rect.y)) {
|
||||
// return true;
|
||||
// }
|
||||
// if (rect.x < 0) {
|
||||
// rect.x += rect.w;
|
||||
// }
|
||||
// if (rect.y < 0) {
|
||||
// rect.y += rect.h;
|
||||
// }
|
||||
// if (isInbounds(rect.x, rect.y)) {
|
||||
// return true;
|
||||
// }
|
||||
// if (rect.x > DISPLAY_WIDTH) {
|
||||
// rect.x -= rect.w;
|
||||
// }
|
||||
// if (rect.y > DISPLAY_HEIGHT) {
|
||||
// rect.y -= rect.h;
|
||||
// }
|
||||
// return isInbounds(rect.x, rect.y);
|
||||
//}
|
||||
|
||||
void initPlayer(Player *plr) {
|
||||
plr->cursor.direction = ORIENT_UP;
|
||||
setActivePlayerSlot(plr, 1);
|
||||
PlayerTexture = IMG_LoadTexture(renderer, "assets/player.png");
|
||||
PlayerTexture = IMG_LoadTexture(mainRenderer, "assets/player.png");
|
||||
SDL_QueryTexture(PlayerTexture, NULL, NULL, &PlayerRect.w, &PlayerRect.h);
|
||||
PlayerRect.x = (DISPLAY_WIDTH / 2) - (PlayerRect.w / 2);
|
||||
PlayerRect.y = (DISPLAY_HEIGHT / 2) - (PlayerRect.h / 2);
|
||||
plr->health = 64;
|
||||
plr->healthIdle = 0;
|
||||
|
||||
plr->rect.x = DISPLAY_WIDTH / 2;
|
||||
plr->rect.y = DISPLAY_HEIGHT / 2;
|
||||
plr->rect.w = TILE_SIZE;
|
||||
plr->rect.h = TILE_SIZE;
|
||||
|
||||
for (uint16_t ui = 0; ui < itemRegistryIndex; ui++) {
|
||||
plr->inventory.slotCounts[ui] = 64;
|
||||
}
|
||||
|
||||
hudTexture = SDL_CreateTexture(mainRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, screenRect.w,
|
||||
screenRect.h);
|
||||
entityTexture = SDL_CreateTexture(mainRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, screenRect.w,
|
||||
screenRect.h);
|
||||
|
||||
SDL_SetTextureBlendMode(entityTexture, SDL_BLENDMODE_BLEND);
|
||||
SDL_SetTextureBlendMode(hudTexture, SDL_BLENDMODE_BLEND);
|
||||
|
||||
plr->cursor.targetTileRect.w = TILE_SIZE;
|
||||
plr->cursor.targetTileRect.h = TILE_SIZE;
|
||||
|
||||
plr->cursor.targetTileRect.w = TILE_SIZE;
|
||||
plr->cursor.targetTileRect.h = TILE_SIZE;
|
||||
|
||||
targetItemBGRect.w = DISPLAY_WIDTH;
|
||||
targetItemBGRect.h = TILE_SIZE;
|
||||
targetItemBGRect.x = 0;
|
||||
targetItemBGRect.y = DISPLAY_HEIGHT - 32;
|
||||
|
||||
targetItemRect.w = TILE_SIZE / 2;
|
||||
targetItemRect.h = TILE_SIZE / 2;
|
||||
|
||||
plr->cursor.heldItemRect.w = TILE_SIZE;
|
||||
plr->cursor.heldItemRect.h = TILE_SIZE;
|
||||
}
|
||||
|
||||
void updatePlayer(Player *plr) {
|
||||
@@ -118,31 +159,26 @@ void updatePlayer(Player *plr) {
|
||||
|
||||
void renderPlayer(Player *plr) {
|
||||
|
||||
plr->cursor.targetTileRect.x = plr->cursor.tileX * TILE_SIZE;
|
||||
plr->cursor.targetTileRect.y = plr->cursor.tileY * TILE_SIZE;
|
||||
plr->cursor.targetTileRect.w = TILE_SIZE;
|
||||
plr->cursor.targetTileRect.h = TILE_SIZE;
|
||||
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
|
||||
plr->cursor.tileDiffX = plr->cursor.tileX - playerTileX;
|
||||
plr->cursor.tileDiffY = plr->cursor.tileY - playerTileY;
|
||||
if (abs(plr->cursor.tileDiffX) > abs(plr->cursor.tileDiffY)) {
|
||||
plr->cursor.tileDiff = plr->cursor.tileDiffX;
|
||||
} else {
|
||||
plr->cursor.tileDiff = plr->cursor.tileDiffY;
|
||||
}
|
||||
plr->cursor.canReach = abs(plr->cursor.tileDiff) <= playerReach;
|
||||
SDL_SetRenderDrawColor(renderer, plr->cursor.canReach ? 0 : 255, plr->cursor.canReach ? 255 : 0, 0, 128);
|
||||
adjustRect(&plr->cursor.targetTileRect);
|
||||
DrawThickRect(renderer, plr->cursor.targetTileRect, 4);
|
||||
SDL_Texture *originalTarget = SDL_GetRenderTarget(mainRenderer);
|
||||
|
||||
SDL_SetRenderDrawColor(mainRenderer, 0, 0, 0, 0);
|
||||
|
||||
SDL_SetRenderTarget(mainRenderer, entityTexture);
|
||||
SDL_RenderClear(mainRenderer);
|
||||
SDL_RenderCopy(mainRenderer, PlayerTexture, NULL, &PlayerRect);
|
||||
SDL_SetRenderTarget(mainRenderer, hudTexture);
|
||||
SDL_RenderClear(mainRenderer);
|
||||
|
||||
SDL_SetRenderDrawColor(mainRenderer, plr->cursor.canReach ? 0 : 255, plr->cursor.canReach ? 255 : 0, 0, 128);
|
||||
DrawThickRect(mainRenderer, plr->cursor.targetTileRect, 4);
|
||||
|
||||
uint16_t itemIndex = plr->inventory.activeSlotIndex;
|
||||
SDL_Texture *itemTex;
|
||||
char itemStringCount[6];
|
||||
if (itemIndex < itemRegistryIndex) {
|
||||
SDL_Rect heldItemRect;
|
||||
heldItemRect.x = plr->cursor.windowX;
|
||||
heldItemRect.y = plr->cursor.windowY;
|
||||
heldItemRect.w = TILE_SIZE;
|
||||
heldItemRect.h = TILE_SIZE;
|
||||
SDL_Texture *itemTex = ItemRegistry[itemIndex].textureOnBelt[plr->cursor.direction];
|
||||
plr->cursor.heldItemRect.x = plr->cursor.windowX;
|
||||
plr->cursor.heldItemRect.y = plr->cursor.windowY;
|
||||
itemTex = ItemRegistry[itemIndex].textureOnBelt[plr->cursor.direction];
|
||||
if (itemTex == NULL) {
|
||||
itemTex = ItemRegistry[itemIndex].textureOnBelt[ORIENT_LEFT];
|
||||
}
|
||||
@@ -158,37 +194,29 @@ void renderPlayer(Player *plr) {
|
||||
|
||||
SDL_SetTextureAlphaMod(itemTex, 255);
|
||||
}
|
||||
SDL_RenderCopy(renderer, itemTex, NULL,
|
||||
&heldItemRect);
|
||||
SDL_RenderCopy(mainRenderer, itemTex, NULL,
|
||||
&plr->cursor.heldItemRect);
|
||||
renderText(mainRenderer, fonts[2], ItemRegistry[itemIndex].name, plr->cursor.heldItemRect.x,
|
||||
plr->cursor.heldItemRect.y - (fonts[2].size * 3 / 2));
|
||||
}
|
||||
}
|
||||
SDL_RenderCopy(renderer, PlayerTexture, NULL, &PlayerRect);
|
||||
|
||||
SDL_Color healthBarColor = {0, 240, 0, 255};
|
||||
|
||||
renderBar(renderer, (DISPLAY_WIDTH / 2) - 128, DISPLAY_HEIGHT - 50, 200, 8, playerMaxHealth, plr->health, healthBarColor, 4);
|
||||
renderBar(mainRenderer, (DISPLAY_WIDTH / 2) - 128, DISPLAY_HEIGHT - 50, 200, 8, playerMaxHealth, plr->health,
|
||||
healthBarColor, 4);
|
||||
|
||||
SDL_Color breakingBarColor = {128, 128, 0, 255};
|
||||
|
||||
renderBar(renderer, (DISPLAY_WIDTH / 2) - 128, DISPLAY_HEIGHT - 70, 200, 8,
|
||||
renderBar(mainRenderer, (DISPLAY_WIDTH / 2) - 128, DISPLAY_HEIGHT - 70, 200, 8,
|
||||
getBreakTime(plr->cursor.targetTile->type), plr->cursor.breakingProgress, breakingBarColor, 4);
|
||||
|
||||
SDL_Rect targetItemBGRect;
|
||||
targetItemBGRect.w = DISPLAY_WIDTH;
|
||||
targetItemBGRect.h = TILE_SIZE;
|
||||
targetItemBGRect.x = 0;
|
||||
targetItemBGRect.y = DISPLAY_HEIGHT - 30;
|
||||
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
|
||||
SDL_RenderFillRect(renderer, &targetItemBGRect);
|
||||
|
||||
SDL_Rect targetItemRect;
|
||||
targetItemRect.w = TILE_SIZE / 2;
|
||||
targetItemRect.h = TILE_SIZE / 2;
|
||||
targetItemRect.y = DISPLAY_HEIGHT - 30 + TILE_SIZE / 4;
|
||||
SDL_SetRenderDrawColor(mainRenderer, 0, 0, 0, 255);
|
||||
SDL_RenderFillRect(mainRenderer, &targetItemBGRect);
|
||||
|
||||
targetItemRect.y = DISPLAY_HEIGHT - 30 + TILE_SIZE / 4 + 3;
|
||||
targetItemRect.x = TILE_SIZE / 4;
|
||||
|
||||
for (uint16_t i = 1; i < ITEMREGISTRY_SIZE; i++) {
|
||||
SDL_Texture *itemTex = ItemRegistry[i].textureOnBelt[plr->cursor.direction];
|
||||
for (uint16_t i = 1; i < itemRegistryIndex; i++) {
|
||||
itemTex = ItemRegistry[i].textureOnBelt[plr->cursor.direction];
|
||||
if (itemTex == NULL) {
|
||||
itemTex = ItemRegistry[i].textureOnBelt[ORIENT_LEFT];
|
||||
}
|
||||
@@ -197,15 +225,21 @@ void renderPlayer(Player *plr) {
|
||||
// Set a red tint (255, 0, 0)
|
||||
SDL_SetTextureColorMod(itemTex, 128, 128, 255);
|
||||
}
|
||||
SDL_RenderCopy(renderer, itemTex, NULL, &targetItemRect);
|
||||
SDL_RenderCopy(mainRenderer, itemTex, NULL, &targetItemRect);
|
||||
SDL_SetTextureColorMod(itemTex, 255, 255, 255);
|
||||
|
||||
}
|
||||
if (plr->inventory.activeSlotIndex == i) {
|
||||
SDL_SetRenderDrawColor(renderer, 16, plr->inventory.slotCounts[i] > 0 ? 128 : 16,
|
||||
SDL_SetRenderDrawColor(mainRenderer, 16, plr->inventory.slotCounts[i] > 0 ? 128 : 16,
|
||||
plr->inventory.slotCounts[i] > 0 ? 32 : 128, 255);
|
||||
DrawThickRect(renderer, targetItemRect, 4);
|
||||
DrawThickRect(mainRenderer, targetItemRect, 4);
|
||||
}
|
||||
targetItemRect.x += (TILE_SIZE / 2) + (TILE_SIZE / 4);
|
||||
sprintf(itemStringCount, "%d", plr->inventory.slotCounts[i]);
|
||||
renderText(mainRenderer, fonts[2], itemStringCount, targetItemRect.x - 3,
|
||||
targetItemRect.y - (fonts[2].size * 3 / 2));
|
||||
//targetItemRect.x += (TILE_SIZE / 2) + (TILE_SIZE / 4);
|
||||
targetItemRect.x += TILE_SIZE;
|
||||
}
|
||||
|
||||
SDL_SetRenderTarget(mainRenderer, originalTarget);
|
||||
}
|
Reference in New Issue
Block a user