127 lines
4.7 KiB
C
127 lines
4.7 KiB
C
//
|
|
// Created by bruno on 4/24/25.
|
|
//
|
|
|
|
#include "item.h"
|
|
#include "../tiles/tile.h"
|
|
#include "../util/util.h"
|
|
#include "../player/player.h"
|
|
#include <dirent.h>
|
|
|
|
Item ItemRegistry[ITEMREGISTRY_SIZE];
|
|
|
|
uint16_t itemRegistryIndex = 0;
|
|
|
|
void updateItems() {
|
|
for (int y = 0; y < MAP_HEIGHT; y++) {
|
|
for (int x = 0; x < MAP_WIDTH; x++) {
|
|
Tile *t = &tileMap[y][x];
|
|
if (t->type != TYPE_BELT || !t->item.active) continue;
|
|
|
|
if (
|
|
(t->item.x < 0 && t->direction == ORIENT_LEFT) ||
|
|
(t->item.x > 0.75f && t->direction == ORIENT_RIGHT) ||
|
|
(t->item.y < 0.25f && t->direction == ORIENT_UP) ||
|
|
(t->item.y > 0.625f && t->direction == ORIENT_DOWN)
|
|
) {
|
|
int nx = x, ny = y;
|
|
if (t->direction == ORIENT_LEFT) nx--;
|
|
if (t->direction == ORIENT_RIGHT) nx++;
|
|
if (t->direction == ORIENT_UP) ny--;
|
|
if (t->direction == ORIENT_DOWN) ny++;
|
|
if (nx >= 0 && nx < MAP_WIDTH && ny >= 0 && ny < MAP_HEIGHT) {
|
|
Tile *next = &tileMap[ny][nx];
|
|
if (next->type == TYPE_BELT && !next->item.active) {
|
|
memcpy(&next->item, &t->item, sizeof(ItemOnBelt));
|
|
printf("Moved to X=%d, Y=%d", nx, ny);
|
|
next->item.tileX = nx;
|
|
next->item.tileY = ny;
|
|
if (t->direction == ORIENT_LEFT) next->item.x = 0.5f;
|
|
if (t->direction == ORIENT_RIGHT) next->item.x = 0.25f;
|
|
if (t->direction == ORIENT_UP) next->item.y = 0.5f;
|
|
if (t->direction == ORIENT_DOWN) next->item.y = 0;
|
|
next->item.active = true;
|
|
t->item.active = false;
|
|
} else {
|
|
continue;
|
|
}
|
|
} else {
|
|
t->item.active = false;
|
|
}
|
|
}
|
|
|
|
float speed = 0.02f;
|
|
switch (t->direction) {
|
|
case ORIENT_LEFT:
|
|
t->item.x -= speed;
|
|
break;
|
|
case ORIENT_RIGHT:
|
|
t->item.x += speed;
|
|
break;
|
|
case ORIENT_UP:
|
|
t->item.y -= speed;
|
|
break;
|
|
case ORIENT_DOWN:
|
|
t->item.y += speed;
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void registerItem(char name[20], SDL_Renderer *renderer) {
|
|
const char *dot = strchr(name, '.');
|
|
memcpy(ItemRegistry[itemRegistryIndex].name, name, dot - name);
|
|
char texturePath[80];
|
|
snprintf(texturePath, 80, "./assets/items/%s", name);
|
|
ItemRegistry[itemRegistryIndex].texture = IMG_LoadTexture(renderer, texturePath);
|
|
SDL_SetTextureBlendMode(ItemRegistry[itemRegistryIndex].texture, SDL_BLENDMODE_BLEND);
|
|
ItemRegistry[itemRegistryIndex].textureOnBelt = ScaleTexture(renderer, ItemRegistry[itemRegistryIndex].texture,
|
|
TILE_SIZE / 2, TILE_SIZE / 2);
|
|
SDL_SetTextureBlendMode(ItemRegistry[itemRegistryIndex].textureOnBelt, SDL_BLENDMODE_BLEND);
|
|
ItemRegistry[itemRegistryIndex].type = itemRegistryIndex;
|
|
|
|
itemRegistryIndex++;
|
|
}
|
|
|
|
void renderItem(ItemOnBelt item, SDL_Renderer *renderer) {
|
|
SDL_Rect rect = {0};
|
|
rect.x = (item.tileX * TILE_SIZE) + (item.x * TILE_SIZE);
|
|
rect.y = (item.tileY * TILE_SIZE) + (item.y * TILE_SIZE);
|
|
rect.w = TILE_SIZE / 2;
|
|
rect.h = TILE_SIZE / 2;
|
|
adjustRect(&rect);
|
|
SDL_RenderCopy(renderer, ItemRegistry[item.type].textureOnBelt, NULL, &rect);
|
|
}
|
|
|
|
void renderBeltItems(SDL_Renderer *renderer) {
|
|
|
|
}
|
|
|
|
void putItem(int x, int y, uint16_t itemType) {
|
|
tileMap[y][x].item.type = itemType;
|
|
tileMap[y][x].item.x = 0.25f;
|
|
tileMap[y][x].item.y = 0.25f;
|
|
if (tileMap[y][x].direction == ORIENT_LEFT) tileMap[y][x].item.x = 0.5f;
|
|
if (tileMap[y][x].direction == ORIENT_RIGHT) tileMap[y][x].item.x = 0.25f;
|
|
if (tileMap[y][x].direction == ORIENT_UP) tileMap[y][x].item.y = 0.5f;
|
|
if (tileMap[y][x].direction == ORIENT_DOWN) tileMap[y][x].item.y = 0.25f;
|
|
tileMap[y][x].item.active = true;
|
|
tileMap[y][x].item.tileX = x;
|
|
tileMap[y][x].item.tileY = y;
|
|
}
|
|
|
|
void loadItems(SDL_Renderer *renderer) {
|
|
DIR *dir = opendir("./assets/items");
|
|
if (dir) {
|
|
struct dirent *entry;
|
|
while ((entry = readdir(dir)) != NULL) {
|
|
if (entry->d_name[0] == '.') {
|
|
continue;
|
|
}
|
|
registerItem(entry->d_name, renderer);
|
|
}
|
|
}
|
|
} |