Some more hopefully working progress
This commit is contained in:
227
items/item.c
227
items/item.c
@@ -11,7 +11,7 @@ Item ItemRegistry[ITEMREGISTRY_SIZE];
|
||||
|
||||
uint16_t itemRegistryIndex = 0;
|
||||
|
||||
float speed = 0.002f; // fraction of tile per tick
|
||||
double speed = 1.0f / TILE_SIZE; // fraction of tile per tick
|
||||
|
||||
void updateItems() {
|
||||
const int dirDx[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
|
||||
@@ -29,70 +29,66 @@ void updateItems() {
|
||||
bool vert = (dir == ORIENT_UP || dir == ORIENT_DOWN);
|
||||
|
||||
for (uint8_t lane = 0; lane < 2; lane++) {
|
||||
for (uint8_t slot = 0; slot < 2; slot++) {
|
||||
ItemOnBelt *itm = &t->items[lane][slot];
|
||||
if (!itm->active) continue;
|
||||
ItemOnBelt *itm = &t->items[lane];
|
||||
if (itm->type == 0) continue;
|
||||
|
||||
// 1) Advance
|
||||
itm->offset += speed;
|
||||
// 1) Advance
|
||||
itm->offset += speed;
|
||||
|
||||
// 2) Time to hop?
|
||||
if (itm->offset >= 0.5f) {
|
||||
itm->offset -= 1.0f;
|
||||
// 2) Time to hop?
|
||||
if (itm->offset >= 0.5f) {
|
||||
itm->offset -= 1.0f;
|
||||
|
||||
// target coords
|
||||
int nx = x + dirDx[dir];
|
||||
int ny = y + dirDy[dir];
|
||||
// target coords
|
||||
int nx = x + dirDx[dir];
|
||||
int ny = y + dirDy[dir];
|
||||
|
||||
// bounds & belt?
|
||||
if (nx < 0 || nx >= MAP_WIDTH || ny < 0 || ny >= MAP_HEIGHT) {
|
||||
itm->active = false;
|
||||
continue;
|
||||
}
|
||||
Tile *next = &tileMap[ny][nx];
|
||||
if (next->type != TYPE_BELT) {
|
||||
itm->active = false;
|
||||
continue;
|
||||
}
|
||||
// bounds & belt?
|
||||
if (nx < 0 || nx >= MAP_WIDTH || ny < 0 || ny >= MAP_HEIGHT) {
|
||||
//itm->type = 0;
|
||||
itm->offset += 1.0f - speed;
|
||||
continue;
|
||||
}
|
||||
Tile *next = &tileMap[ny][nx];
|
||||
if (next->type != TYPE_BELT) {
|
||||
//itm->type = 0;
|
||||
itm->offset += 1.0f - speed;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Decide new lane
|
||||
int newLane = lane;
|
||||
int newDir = next->direction;
|
||||
bool nH = (newDir == ORIENT_LEFT || newDir == ORIENT_RIGHT);
|
||||
bool nV = (newDir == ORIENT_UP || newDir == ORIENT_DOWN);
|
||||
// Decide new lane
|
||||
int newLane = lane;
|
||||
int newDir = next->direction;
|
||||
bool nH = (newDir == ORIENT_LEFT || newDir == ORIENT_RIGHT);
|
||||
bool nV = (newDir == ORIENT_UP || newDir == ORIENT_DOWN);
|
||||
|
||||
if ((horz && nH) || (vert && nV)) {
|
||||
// same axis → keep lane
|
||||
} else if (horz && nV) {
|
||||
// came off a horizontal: lane0=top→vertical.left, lane1=bottom→vertical.right
|
||||
newLane = (dir == ORIENT_RIGHT ? 1 : 0);
|
||||
itm->offset = 0.0f;
|
||||
} else if (vert && nH) {
|
||||
// came off vertical: lane0=left→horizontal.top, lane1=right→horizontal.bottom
|
||||
newLane = (dir == ORIENT_UP ? 0 : 1);
|
||||
itm->offset = 0.0f;
|
||||
}
|
||||
// (diagonals fall back to same-lane)
|
||||
if ((horz && nH) || (vert && nV)) {
|
||||
// same axis → keep lane
|
||||
} else if (horz && nV) {
|
||||
// came off a horizontal: lane0=top→vertical.left, lane1=bottom→vertical.right
|
||||
newLane = (dir == ORIENT_RIGHT ^ newDir == ORIENT_UP ? 0 : 1);
|
||||
itm->offset = 0.0f;
|
||||
} else if (vert && nH) {
|
||||
// came off vertical: lane0=left→horizontal.top, lane1=right→horizontal.bottom
|
||||
newLane = (dir == ORIENT_UP ^ newDir == ORIENT_RIGHT ? 1 : 0);
|
||||
itm->offset = 0.0f;
|
||||
}
|
||||
// (diagonals fall back to same-lane)
|
||||
|
||||
// Find a free slot in newLane
|
||||
int destSlot = -1;
|
||||
if (!next->items[newLane][0].active) destSlot = 0;
|
||||
else if (!next->items[newLane][1].active) destSlot = 1;
|
||||
// Find a free slot in
|
||||
|
||||
if (destSlot >= 0) {
|
||||
// MOVE it
|
||||
ItemOnBelt moved = *itm;
|
||||
moved.tileX = nx;
|
||||
moved.tileY = ny;
|
||||
next->items[newLane][destSlot] = moved;
|
||||
next->items[newLane][destSlot].active = true;
|
||||
if (next->items[newLane].type == 0) {
|
||||
// MOVE it
|
||||
ItemOnBelt moved = *itm;
|
||||
moved.tileX = nx;
|
||||
moved.tileY = ny;
|
||||
next->items[newLane] = moved;
|
||||
|
||||
// clear this one
|
||||
itm->active = false;
|
||||
} else {
|
||||
// both slots full → wait at end
|
||||
itm->offset = epsilon;
|
||||
}
|
||||
// clear this one
|
||||
itm->type = 0;
|
||||
} else {
|
||||
// both slots full → wait at end
|
||||
itm->offset = epsilon;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -108,8 +104,9 @@ void registerItem(char name[20], SDL_Renderer *renderer) {
|
||||
snprintf(texturePath, 80, "./assets/items/%s", name);
|
||||
ItemRegistry[itemRegistryIndex].texture[ORIENT_LEFT] = IMG_LoadTexture(renderer, texturePath);
|
||||
SDL_SetTextureBlendMode(ItemRegistry[itemRegistryIndex].texture[0], SDL_BLENDMODE_BLEND);
|
||||
ItemRegistry[itemRegistryIndex].textureOnBelt[ORIENT_LEFT] = ScaleTexture(renderer, ItemRegistry[itemRegistryIndex].texture[ORIENT_LEFT],
|
||||
TILE_SIZE / 2, TILE_SIZE / 2);
|
||||
ItemRegistry[itemRegistryIndex].textureOnBelt[ORIENT_LEFT] = ScaleTexture(renderer,
|
||||
ItemRegistry[itemRegistryIndex].texture[ORIENT_LEFT],
|
||||
TILE_SIZE / 2, TILE_SIZE / 2);
|
||||
SDL_SetTextureBlendMode(ItemRegistry[itemRegistryIndex].textureOnBelt[ORIENT_LEFT], SDL_BLENDMODE_BLEND);
|
||||
ItemRegistry[itemRegistryIndex].type = itemRegistryIndex;
|
||||
|
||||
@@ -117,19 +114,28 @@ void registerItem(char name[20], SDL_Renderer *renderer) {
|
||||
}
|
||||
|
||||
// easing function: cosine ease‐in‐out
|
||||
//static float ease_in_out(float t) {
|
||||
// if (t < -1.0f) t = -1.0f;
|
||||
// if (t > 1.0f) t = 1.0f;
|
||||
//
|
||||
// // Even symmetric easing: reflected across t = 0
|
||||
// return (t < 0.0f)
|
||||
// ? -0.5f * (1.0f - cosf(M_PI * -t)) // negative side
|
||||
// : 0.5f * (1.0f - cosf(M_PI * t)); // positive side
|
||||
//}
|
||||
|
||||
static float ease_in_out(float t) {
|
||||
// Clamp t to [-1.0, 1.0]
|
||||
if (t < -1.0f) t = -1.0f;
|
||||
if (t > 1.0f) t = 1.0f;
|
||||
|
||||
// Even symmetric easing: reflected across t = 0
|
||||
return (t < 0.0f)
|
||||
? -0.5f * (1.0f - cosf(M_PI * -t)) // negative side
|
||||
: 0.5f * (1.0f - cosf(M_PI * t)); // positive side
|
||||
return t; // Linear: no easing
|
||||
}
|
||||
|
||||
|
||||
uint8_t laneTarget = 0;
|
||||
|
||||
void renderItem(ItemOnBelt item, SDL_Renderer *renderer, int lane) {
|
||||
void renderItem(ItemOnBelt item, SDL_Renderer *renderer, int lane, SDL_Rect playerRect) {
|
||||
SDL_Rect rect = {0};
|
||||
rect.x = item.tileX * TILE_SIZE;
|
||||
rect.y = item.tileY * TILE_SIZE;
|
||||
@@ -190,7 +196,7 @@ void renderItem(ItemOnBelt item, SDL_Renderer *renderer, int lane) {
|
||||
yOffset += 0.0f * TILE_SIZE;
|
||||
break;
|
||||
case ORIENT_LEFT:
|
||||
xOffset += 0.0f * TILE_SIZE;
|
||||
xOffset += 0.0f * TILE_SIZE + (TILE_SIZE / 2);
|
||||
yOffset += 0.26f * TILE_SIZE;
|
||||
break;
|
||||
case ORIENT_LEFT_UP:
|
||||
@@ -199,7 +205,7 @@ void renderItem(ItemOnBelt item, SDL_Renderer *renderer, int lane) {
|
||||
break;
|
||||
case ORIENT_UP:
|
||||
xOffset += 0.22f * TILE_SIZE; //GOTO HEHREHRHE
|
||||
yOffset += 0.0f * TILE_SIZE;
|
||||
yOffset += 0.0f * TILE_SIZE + (TILE_SIZE / 2);
|
||||
break;
|
||||
case ORIENT_RIGHT_UP:
|
||||
xOffset += 0.0f * TILE_SIZE;
|
||||
@@ -214,7 +220,7 @@ void renderItem(ItemOnBelt item, SDL_Renderer *renderer, int lane) {
|
||||
yOffset += 0.0f * TILE_SIZE;
|
||||
break;
|
||||
case ORIENT_DOWN:
|
||||
xOffset += 0.18f * TILE_SIZE;
|
||||
xOffset += 0.18f * TILE_SIZE + (TILE_SIZE / 4);
|
||||
yOffset += 0.0f * TILE_SIZE;
|
||||
break;
|
||||
default:
|
||||
@@ -243,55 +249,50 @@ void renderItem(ItemOnBelt item, SDL_Renderer *renderer, int lane) {
|
||||
rect.w = TILE_SIZE / 2;
|
||||
rect.h = TILE_SIZE / 2;
|
||||
|
||||
adjustRect(&rect);
|
||||
adjustRect(&rect, playerRect);
|
||||
|
||||
// (Optional debug overlay)
|
||||
// SDL_Rect tileArea = {item.tileX * TILE_SIZE, item.tileY * TILE_SIZE,
|
||||
// TILE_SIZE, TILE_SIZE};
|
||||
// SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
|
||||
// SDL_SetRenderDrawColor(renderer, 255, 0, 0, 32);
|
||||
// adjustRect(&tileArea);
|
||||
// SDL_RenderFillRect(renderer, &tileArea);
|
||||
|
||||
char tempStr[50];
|
||||
|
||||
// SDL_Rect rectA = {0};
|
||||
// rectA.x = item.tileX * TILE_SIZE;
|
||||
// rectA.y = item.tileY * TILE_SIZE;
|
||||
// rectA.w = TILE_SIZE;
|
||||
// rectA.h = TILE_SIZE;
|
||||
// sprintf(tempStr, "L%d\n%f\n%f\n%f", lane, item.offset, xOffset, yOffset);
|
||||
// SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
|
||||
// SDL_SetRenderDrawColor(renderer, 255, 0, 0, 32);
|
||||
// adjustRect(&rectA);
|
||||
// SDL_RenderFillRect(renderer, &rectA);
|
||||
SDL_Rect rectA = {0};
|
||||
if (debugMode) {
|
||||
SDL_Rect tileArea = {item.tileX * TILE_SIZE, item.tileY * TILE_SIZE,
|
||||
TILE_SIZE, TILE_SIZE};
|
||||
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 32);
|
||||
adjustRect(&tileArea, playerRect);
|
||||
SDL_RenderFillRect(renderer, &tileArea);
|
||||
rectA.x = item.tileX * TILE_SIZE;
|
||||
rectA.y = item.tileY * TILE_SIZE;
|
||||
rectA.w = TILE_SIZE;
|
||||
rectA.h = TILE_SIZE;
|
||||
sprintf(tempStr, "L%d\n%f\n%f\n%f", lane, item.offset, xOffset, yOffset);
|
||||
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 32);
|
||||
adjustRect(&rectA, playerRect);
|
||||
SDL_RenderFillRect(renderer, &rectA);
|
||||
}
|
||||
SDL_RenderCopy(renderer, ItemRegistry[item.type].textureOnBelt[ORIENT_LEFT], NULL, &rect);
|
||||
// renderText(renderer, fonts[3], tempStr, rectA.x, rectA.y);
|
||||
if (debugMode) {
|
||||
renderText(renderer, fonts[3], tempStr, rectA.x, rectA.y);
|
||||
}
|
||||
}
|
||||
|
||||
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].offset = 0;
|
||||
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 putItem(int x, int y, uint16_t itemType, uint8_t lane) {
|
||||
tileMap[y][x].items[lane].type = itemType;
|
||||
tileMap[y][x].items[lane].offset = 0;
|
||||
tileMap[y][x].items[lane].tileX = x;
|
||||
tileMap[y][x].items[lane].tileY = y;
|
||||
}
|
||||
|
||||
void loadItems(SDL_Renderer *renderer) {
|
||||
for(int i = 0; i < tileTypeIndex; i++) {
|
||||
for (int i = 0; i < tileTypeIndex; i++) {
|
||||
TileType tile = TileRegistry[i];
|
||||
|
||||
strcpy(ItemRegistry[itemRegistryIndex].name, tile.name);
|
||||
memcpy(ItemRegistry[itemRegistryIndex].texture, tile.textures, sizeof (tile.textures));
|
||||
memcpy(ItemRegistry[itemRegistryIndex].texture, tile.textures, sizeof(tile.textures));
|
||||
for (int a = 0; a < ORIENT_DIRECTION_COUNT; a++) {
|
||||
SDL_SetTextureBlendMode(ItemRegistry[itemRegistryIndex].texture[a], SDL_BLENDMODE_BLEND);
|
||||
ItemRegistry[itemRegistryIndex].textureOnBelt[a] = ScaleTexture(renderer, tile.textures[a],
|
||||
TILE_SIZE / 2, TILE_SIZE / 2);
|
||||
SDL_SetTextureBlendMode(ItemRegistry[itemRegistryIndex].textureOnBelt[a], SDL_BLENDMODE_BLEND);
|
||||
SDL_SetTextureBlendMode(ItemRegistry[itemRegistryIndex].texture[a], SDL_BLENDMODE_BLEND);
|
||||
ItemRegistry[itemRegistryIndex].textureOnBelt[a] = ScaleTexture(renderer, tile.textures[a],
|
||||
TILE_SIZE / 2, TILE_SIZE / 2);
|
||||
SDL_SetTextureBlendMode(ItemRegistry[itemRegistryIndex].textureOnBelt[a], SDL_BLENDMODE_BLEND);
|
||||
}
|
||||
ItemRegistry[itemRegistryIndex].type = itemRegistryIndex;
|
||||
|
||||
@@ -299,14 +300,16 @@ 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);
|
||||
}
|
||||
}
|
||||
iterateSortedDir("./assets/items", (DirEntryCallback) registerItem, 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, mainRenderer);
|
||||
// }
|
||||
// }
|
||||
}
|
@@ -14,7 +14,6 @@
|
||||
typedef struct {
|
||||
float offset;
|
||||
int tileX, tileY;
|
||||
bool active;
|
||||
uint16_t type;
|
||||
} ItemOnBelt;
|
||||
|
||||
@@ -30,15 +29,15 @@ extern Item ItemRegistry[ITEMREGISTRY_SIZE];
|
||||
|
||||
void updateItems();
|
||||
|
||||
void loadItems(SDL_Renderer *renderer);
|
||||
void loadItems(SDL_Renderer *mainRenderer);
|
||||
|
||||
void renderItem(ItemOnBelt item, SDL_Renderer *renderer, int lane);
|
||||
void renderItem(ItemOnBelt item, SDL_Renderer *mainRenderer, int lane, SDL_Rect playerRect);
|
||||
|
||||
extern uint16_t itemRegistryIndex;
|
||||
|
||||
extern uint8_t laneTarget;
|
||||
extern float speed;
|
||||
extern double speed;
|
||||
|
||||
|
||||
void putItem(int x, int y, uint16_t itemType, uint8_t lane, uint8_t itemIndex);
|
||||
void putItem(int x, int y, uint16_t itemType, uint8_t lane);
|
||||
#endif //FACTORYGAME_ITEM_H
|
||||
|
Reference in New Issue
Block a user