Files
factorygame/items/item.c
2025-04-30 19:01:44 +02:00

131 lines
5.8 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];
for (uint8_t lane = 0; lane < 2; lane++) {
for (uint8_t itemIndex = 0; itemIndex < 2; itemIndex++) {
if (t->type != TYPE_BELT || !t->items[lane][itemIndex].active) continue;
if (
(t->items[lane][itemIndex].x < -0.5 && t->direction == ORIENT_LEFT) ||
(t->items[lane][itemIndex].x > 1 && t->direction == ORIENT_RIGHT) ||
(t->items[lane][itemIndex].y < -0.5 && t->direction == ORIENT_UP) ||
(t->items[lane][itemIndex].y > 1 && 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->items[lane][itemIndex].active) {
memcpy(&next->items[lane][itemIndex], &t->items[lane][itemIndex], sizeof(ItemOnBelt));
printf("Moved to X=%d, Y=%d\n", nx, ny);
next->items[lane][itemIndex].tileX = nx;
next->items[lane][itemIndex].tileY = ny;
if (t->direction == ORIENT_LEFT) next->items[lane][itemIndex].x = 0.5f;
if (t->direction == ORIENT_RIGHT) next->items[lane][itemIndex].x = 0;
if (t->direction == ORIENT_UP) next->items[lane][itemIndex].y = 0.5f;
if (t->direction == ORIENT_DOWN) next->items[lane][itemIndex].y = 0;
next->items[lane][itemIndex].active = true;
t->items[lane][itemIndex].active = false;
} else {
continue;
}
} else {
t->items[lane][itemIndex].active = false;
}
}
float speed = 0.002f;
switch (t->direction) {
case ORIENT_LEFT:
t->items[lane][itemIndex].x -= speed;
break;
case ORIENT_RIGHT:
t->items[lane][itemIndex].x += speed;
break;
case ORIENT_UP:
t->items[lane][itemIndex].y -= speed;
break;
case ORIENT_DOWN:
t->items[lane][itemIndex].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, uint8_t lane, uint8_t itemIndex) {
tileMap[y][x].items[lane][itemIndex].type = itemType;
tileMap[y][x].items[lane][itemIndex].x = 0.25f;
tileMap[y][x].items[lane][itemIndex].y = 0.25f;
if (tileMap[y][x].direction == ORIENT_LEFT) tileMap[y][x].items[lane][itemIndex].x = 0.5f;
if (tileMap[y][x].direction == ORIENT_RIGHT) tileMap[y][x].items[lane][itemIndex].x = 0.25f;
if (tileMap[y][x].direction == ORIENT_UP) tileMap[y][x].items[lane][itemIndex].y = 0.5f;
if (tileMap[y][x].direction == ORIENT_DOWN) tileMap[y][x].items[lane][itemIndex].y = 0.25f;
tileMap[y][x].items[lane][itemIndex].active = true;
tileMap[y][x].items[lane][itemIndex].tileX = x;
tileMap[y][x].items[lane][itemIndex].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);
}
}
}