test even some stuff - still quite wip
This commit is contained in:
145
items/item.c
145
items/item.c
@@ -13,11 +13,12 @@ Item ItemRegistry[ITEMREGISTRY_SIZE];
|
|||||||
|
|
||||||
uint16_t itemRegistryIndex = 0;
|
uint16_t itemRegistryIndex = 0;
|
||||||
|
|
||||||
|
float speed = 0.002f; // fraction of tile per tick
|
||||||
|
|
||||||
void updateItems() {
|
void updateItems() {
|
||||||
const int dirDx[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
|
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 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
|
const float epsilon = 0.999f; // if we can't move, back it off just below 1
|
||||||
|
|
||||||
for (int y = 0; y < MAP_HEIGHT; y++) {
|
for (int y = 0; y < MAP_HEIGHT; y++) {
|
||||||
@@ -66,10 +67,12 @@ void updateItems() {
|
|||||||
// same axis → keep lane
|
// same axis → keep lane
|
||||||
} else if (horz && nV) {
|
} else if (horz && nV) {
|
||||||
// came off a horizontal: lane0=top→vertical.left, lane1=bottom→vertical.right
|
// 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) {
|
} else if (vert && nH) {
|
||||||
// came off vertical: lane0=left→horizontal.top, lane1=right→horizontal.bottom
|
// came off vertical: lane0=left→horizontal.top, lane1=right→horizontal.bottom
|
||||||
newLane = (dir == ORIENT_UP ? 0 : 1);
|
newLane = (dir == ORIENT_UP ? 0 : 1);
|
||||||
|
itm->offset = 0.0f;
|
||||||
}
|
}
|
||||||
// (diagonals fall back to same-lane)
|
// (diagonals fall back to same-lane)
|
||||||
|
|
||||||
@@ -115,87 +118,151 @@ void registerItem(char name[20], SDL_Renderer *renderer) {
|
|||||||
itemRegistryIndex++;
|
itemRegistryIndex++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t laneTarget = 0;
|
||||||
|
|
||||||
void renderItem(ItemOnBelt item, SDL_Renderer *renderer, int lane) {
|
void renderItem(ItemOnBelt item, SDL_Renderer *renderer, int lane) {
|
||||||
SDL_Rect rect = {0};
|
SDL_Rect rect = {0};
|
||||||
rect.x = item.tileX * TILE_SIZE;
|
rect.x = item.tileX * TILE_SIZE;
|
||||||
rect.y = item.tileY * TILE_SIZE;
|
rect.y = item.tileY * TILE_SIZE;
|
||||||
|
|
||||||
|
// get raw direction code
|
||||||
int dir = tileMap[item.tileY][item.tileX].direction;
|
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) {
|
switch (dir) {
|
||||||
case ORIENT_LEFT_DOWN:
|
case ORIENT_LEFT_DOWN:
|
||||||
dx = -1;
|
dxf = -D;
|
||||||
dy = 1;
|
dyf = +D;
|
||||||
break;
|
break;
|
||||||
case ORIENT_LEFT:
|
case ORIENT_LEFT:
|
||||||
dx = -1;
|
dxf = -1.0f;
|
||||||
dy = 0;
|
dyf = 0.0f;
|
||||||
break;
|
break;
|
||||||
case ORIENT_LEFT_UP:
|
case ORIENT_LEFT_UP:
|
||||||
dx = -1;
|
dxf = -D;
|
||||||
dy = -1;
|
dyf = -D;
|
||||||
break;
|
break;
|
||||||
case ORIENT_UP:
|
case ORIENT_UP:
|
||||||
dx = 0;
|
dxf = 0.0f;
|
||||||
dy = -1;
|
dyf = -1.0f;
|
||||||
break;
|
break;
|
||||||
case ORIENT_RIGHT_UP:
|
case ORIENT_RIGHT_UP:
|
||||||
dx = 1;
|
dxf = +D;
|
||||||
dy = -1;
|
dyf = -D;
|
||||||
break;
|
break;
|
||||||
case ORIENT_RIGHT:
|
case ORIENT_RIGHT:
|
||||||
dx = 1;
|
dxf = +1.0f;
|
||||||
dy = 0;
|
dyf = 0.0f;
|
||||||
break;
|
break;
|
||||||
case ORIENT_RIGHT_DOWN:
|
case ORIENT_RIGHT_DOWN:
|
||||||
dx = 1;
|
dxf = +D;
|
||||||
dy = 1;
|
dyf = +D;
|
||||||
break;
|
break;
|
||||||
case ORIENT_DOWN:
|
case ORIENT_DOWN:
|
||||||
dx = 0;
|
dxf = 0.0f;
|
||||||
dy = 1;
|
dyf = +1.0f;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Main offset along belt direction
|
//--- 2) ease the offset and compute forward distance
|
||||||
int xOffset = (offset * dx) % TILE_SIZE;
|
float t = ease_in_out(item.offset); // 0..1
|
||||||
int yOffset = (offset * dy) % TILE_SIZE;
|
float dist = t * TILE_SIZE; // 0..TILE_SIZE pixels
|
||||||
|
|
||||||
// Perpendicular vector: (dy, -dx)
|
float xOffset = dxf * dist;
|
||||||
|
float yOffset = dyf * dist;
|
||||||
|
|
||||||
float perpX = dy;
|
switch (dir) {
|
||||||
float perpY = -dx;
|
case ORIENT_LEFT_DOWN:
|
||||||
|
xOffset += 0.0f * TILE_SIZE;
|
||||||
float length = sqrtf(perpX * perpX + perpY * perpY);
|
yOffset += 0.0f * TILE_SIZE;
|
||||||
if (length != 0) {
|
break;
|
||||||
perpX /= length;
|
case ORIENT_LEFT:
|
||||||
perpY /= length;
|
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
|
//--- 3) compute perpendicular unit vector (perpX, perpY) = (-dy, dx)
|
||||||
float laneOffset = (lane == 1 ? 1.0f : -1.0f);
|
float perpX = -dyf;
|
||||||
|
float perpY = dxf;
|
||||||
|
// perp is already unit length because (dxf,dyf) is unit
|
||||||
|
|
||||||
xOffset += (int) (perpX * laneOffset * (TILE_SIZE / 2));
|
bool horz = (dir == ORIENT_LEFT || dir == ORIENT_RIGHT);
|
||||||
yOffset += (int) (perpY * laneOffset * (TILE_SIZE / 2));
|
bool vert = (dir == ORIENT_UP || dir == ORIENT_DOWN);
|
||||||
|
|
||||||
rect.x += xOffset;
|
//--- 4) lane offset
|
||||||
rect.y += yOffset;
|
// 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.w = TILE_SIZE / 2;
|
||||||
rect.h = TILE_SIZE / 2;
|
rect.h = TILE_SIZE / 2;
|
||||||
|
|
||||||
adjustRect(&rect);
|
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};
|
SDL_Rect rectA = {0};
|
||||||
rectA.x = item.tileX * TILE_SIZE;
|
rectA.x = item.tileX * TILE_SIZE;
|
||||||
rectA.y = item.tileY * TILE_SIZE;
|
rectA.y = item.tileY * TILE_SIZE;
|
||||||
rectA.w = TILE_SIZE;
|
rectA.w = TILE_SIZE;
|
||||||
rectA.h = TILE_SIZE;
|
rectA.h = TILE_SIZE;
|
||||||
char tempStr[20];
|
sprintf(tempStr, "L%d\n%f\n%f\n%f", lane, item.offset, xOffset, yOffset);
|
||||||
sprintf(tempStr, "L%d\n%f\n%d\n%d", lane, item.offset, xOffset, yOffset);
|
|
||||||
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
|
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
|
||||||
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 32);
|
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 32);
|
||||||
adjustRect(&rectA);
|
adjustRect(&rectA);
|
||||||
|
@@ -31,5 +31,9 @@ void loadItems(SDL_Renderer *renderer);
|
|||||||
|
|
||||||
void renderItem(ItemOnBelt item, SDL_Renderer *renderer, int lane);
|
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);
|
void putItem(int x, int y, uint16_t itemType, uint8_t lane, uint8_t itemIndex);
|
||||||
#endif //FACTORYGAME_ITEM_H
|
#endif //FACTORYGAME_ITEM_H
|
||||||
|
7
main.c
7
main.c
@@ -156,7 +156,14 @@ int processEvent(SDL_Event e) {
|
|||||||
int keyMod = e.key.keysym.mod;
|
int keyMod = e.key.keysym.mod;
|
||||||
cursor = true;
|
cursor = true;
|
||||||
switch (keySym) {
|
switch (keySym) {
|
||||||
|
case SDLK_q:
|
||||||
|
laneTarget = laneTarget == 1 ? 0 : 1;
|
||||||
|
break;
|
||||||
|
case SDLK_p:
|
||||||
|
speed = speed == 0 ? 0.004f : 0;
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} else if (e.type == SDL_MOUSEBUTTONDOWN) {
|
} else if (e.type == SDL_MOUSEBUTTONDOWN) {
|
||||||
|
Reference in New Issue
Block a user