Some progress
This commit is contained in:
@@ -6,10 +6,23 @@
|
||||
#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) {
|
||||
void adjustRect(SDL_Rect *rect) {
|
||||
rect->x -= playerX;
|
||||
rect->y -= playerY;
|
||||
rect->x += DISPLAY_WIDTH / 2;
|
||||
@@ -17,8 +30,9 @@ void adjustRect(SDL_Rect * rect) {
|
||||
}
|
||||
|
||||
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;
|
||||
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) {
|
||||
@@ -45,4 +59,49 @@ bool isInboundsRect(SDL_Rect rect) {
|
||||
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);
|
||||
}
|
Reference in New Issue
Block a user