107 lines
3.3 KiB
C
107 lines
3.3 KiB
C
//
|
|
// Created by bruno on 4/24/25.
|
|
//
|
|
|
|
#include <SDL2/SDL_rect.h>
|
|
#include "player.h"
|
|
#include "../tiles/tile.h"
|
|
|
|
int playerSpeed = 4;
|
|
|
|
int playerReach = 4;
|
|
|
|
void setActivePlayerSlot(Player *plr, uint16_t activeSlotIndex) {
|
|
activeSlotIndex = activeSlotIndex % ITEMREGISTRY_SIZE;
|
|
if (activeSlotIndex < 0) {
|
|
activeSlotIndex = 0;
|
|
}
|
|
plr->inventory.activeSlotIndex = activeSlotIndex;
|
|
}
|
|
|
|
|
|
int playerX = (MAP_WIDTH / 2) * 16;
|
|
int playerY = (MAP_HEIGHT / 2) * 16;
|
|
|
|
void adjustRect(SDL_Rect *rect) {
|
|
rect->x -= playerX;
|
|
rect->y -= playerY;
|
|
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 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);
|
|
}
|
|
|
|
void initPlayer(Player *plr) {
|
|
plr->cursor.direction = ORIENT_UP;
|
|
setActivePlayerSlot(plr, 0);
|
|
}
|
|
|
|
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 (plr->cursor.tileDiffX > 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);
|
|
|
|
uint16_t itemIndex = plr->inventory.activeSlotIndex;
|
|
if (itemIndex < itemRegistryIndex) {
|
|
if (plr->inventory.slotCounts[itemIndex] > 0) {
|
|
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];
|
|
if (itemTex == NULL) {
|
|
itemTex = ItemRegistry[itemIndex].textureOnBelt[ORIENT_LEFT];
|
|
}
|
|
if (itemTex != NULL) {
|
|
SDL_RenderCopy(renderer, ItemRegistry[itemIndex].textureOnBelt[plr->cursor.direction], NULL,
|
|
&heldItemRect);
|
|
}
|
|
}
|
|
}
|
|
//printf("Player inventory index: %d\n", plr->inventory.activeSlotIndex);
|
|
} |