test even some stuff - still quite wip

This commit is contained in:
2025-05-27 21:52:47 +02:00
parent 78620af37a
commit f6e59e74c7
3 changed files with 117 additions and 39 deletions

View File

@@ -13,11 +13,12 @@ Item ItemRegistry[ITEMREGISTRY_SIZE];
uint16_t itemRegistryIndex = 0;
float speed = 0.002f; // fraction of tile per tick
void updateItems() {
const int dirDx[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
const int dirDy[8] = {1, 0, -1, -1, -1, 0, 1, 1};
const float speed = 0.002f; // fraction of tile per tick
const float epsilon = 0.999f; // if we can't move, back it off just below 1
for (int y = 0; y < MAP_HEIGHT; y++) {
@@ -66,10 +67,12 @@ void updateItems() {
// same axis → keep lane
} else if (horz && nV) {
// came off a horizontal: lane0=top→vertical.left, lane1=bottom→vertical.right
newLane = (dir == ORIENT_LEFT ? 1 : 0);
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)
@@ -115,87 +118,151 @@ void registerItem(char name[20], SDL_Renderer *renderer) {
itemRegistryIndex++;
}
// easing function: cosine easeinout
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
}
uint8_t laneTarget = 0;
void renderItem(ItemOnBelt item, SDL_Renderer *renderer, int lane) {
SDL_Rect rect = {0};
rect.x = item.tileX * TILE_SIZE;
rect.y = item.tileY * TILE_SIZE;
// get raw direction code
int dir = tileMap[item.tileY][item.tileX].direction;
int offset = item.offset * TILE_SIZE;
int dx = 0, dy = 0; // Direction vector
//--- 1) build a unit vector (dxf, dyf) for this orientation
float dxf = 0.0f, dyf = 0.0f;
const float D = M_SQRT1_2; // 1/√2 ≈ 0.7071
switch (dir) {
case ORIENT_LEFT_DOWN:
dx = -1;
dy = 1;
dxf = -D;
dyf = +D;
break;
case ORIENT_LEFT:
dx = -1;
dy = 0;
dxf = -1.0f;
dyf = 0.0f;
break;
case ORIENT_LEFT_UP:
dx = -1;
dy = -1;
dxf = -D;
dyf = -D;
break;
case ORIENT_UP:
dx = 0;
dy = -1;
dxf = 0.0f;
dyf = -1.0f;
break;
case ORIENT_RIGHT_UP:
dx = 1;
dy = -1;
dxf = +D;
dyf = -D;
break;
case ORIENT_RIGHT:
dx = 1;
dy = 0;
dxf = +1.0f;
dyf = 0.0f;
break;
case ORIENT_RIGHT_DOWN:
dx = 1;
dy = 1;
dxf = +D;
dyf = +D;
break;
case ORIENT_DOWN:
dx = 0;
dy = 1;
dxf = 0.0f;
dyf = +1.0f;
break;
default:
break;
}
// Main offset along belt direction
int xOffset = (offset * dx) % TILE_SIZE;
int yOffset = (offset * dy) % TILE_SIZE;
//--- 2) ease the offset and compute forward distance
float t = ease_in_out(item.offset); // 0..1
float dist = t * TILE_SIZE; // 0..TILE_SIZE pixels
// Perpendicular vector: (dy, -dx)
float xOffset = dxf * dist;
float yOffset = dyf * dist;
float perpX = dy;
float perpY = -dx;
float length = sqrtf(perpX * perpX + perpY * perpY);
if (length != 0) {
perpX /= length;
perpY /= length;
switch (dir) {
case ORIENT_LEFT_DOWN:
xOffset += 0.0f * TILE_SIZE;
yOffset += 0.0f * TILE_SIZE;
break;
case ORIENT_LEFT:
xOffset += 0.0f * TILE_SIZE;
yOffset += 0.26f * TILE_SIZE;
break;
case ORIENT_LEFT_UP:
xOffset += 0.0f * TILE_SIZE;
yOffset += 0.0f * TILE_SIZE;
break;
case ORIENT_UP:
xOffset += 0.22f * TILE_SIZE; //GOTO HEHREHRHE
yOffset += 0.0f * TILE_SIZE;
break;
case ORIENT_RIGHT_UP:
xOffset += 0.0f * TILE_SIZE;
yOffset += 0.0f * TILE_SIZE;
break;
case ORIENT_RIGHT:
xOffset += 0.0f * TILE_SIZE;
yOffset += 0.18f * TILE_SIZE; //FIX THIS
break;
case ORIENT_RIGHT_DOWN:
xOffset += 0.0f * TILE_SIZE;
yOffset += 0.0f * TILE_SIZE;
break;
case ORIENT_DOWN:
xOffset += 0.18f * TILE_SIZE;
yOffset += 0.0f * TILE_SIZE;
break;
default:
break;
}
// Lane offset distance
float laneOffset = (lane == 1 ? 1.0f : -1.0f);
//--- 3) compute perpendicular unit vector (perpX, perpY) = (-dy, dx)
float perpX = -dyf;
float perpY = dxf;
// perp is already unit length because (dxf,dyf) is unit
xOffset += (int) (perpX * laneOffset * (TILE_SIZE / 2));
yOffset += (int) (perpY * laneOffset * (TILE_SIZE / 2));
bool horz = (dir == ORIENT_LEFT || dir == ORIENT_RIGHT);
bool vert = (dir == ORIENT_UP || dir == ORIENT_DOWN);
rect.x += xOffset;
rect.y += yOffset;
//--- 4) lane offset
// total lane spacing = TILE_SIZE/2, so half of that each side = TILE_SIZE/4
float laneSign = (lane == laneTarget ? +1.0f : -1.0f);
float laneOffset = TILE_SIZE * (horz ? 0.12f : 0.14f); // = TILE_SIZE/4
xOffset += perpX * laneSign * laneOffset;
yOffset += perpY * laneSign * laneOffset;
//--- 5) apply to rect
rect.x += (int) roundf(xOffset);
rect.y += (int) roundf(yOffset);
rect.w = TILE_SIZE / 2;
rect.h = TILE_SIZE / 2;
adjustRect(&rect);
// (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;
char tempStr[20];
sprintf(tempStr, "L%d\n%f\n%d\n%d", lane, item.offset, xOffset, yOffset);
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);

View File

@@ -31,5 +31,9 @@ void loadItems(SDL_Renderer *renderer);
void renderItem(ItemOnBelt item, SDL_Renderer *renderer, int lane);
extern uint8_t laneTarget;
extern float speed;
void putItem(int x, int y, uint16_t itemType, uint8_t lane, uint8_t itemIndex);
#endif //FACTORYGAME_ITEM_H