test some stuff - still wip
This commit is contained in:
BIN
assets/PublicPixel.ttf
Normal file
BIN
assets/PublicPixel.ttf
Normal file
Binary file not shown.
216
items/item.c
216
items/item.c
@@ -6,6 +6,7 @@
|
|||||||
#include "../tiles/tile.h"
|
#include "../tiles/tile.h"
|
||||||
#include "../util/util.h"
|
#include "../util/util.h"
|
||||||
#include "../player/player.h"
|
#include "../player/player.h"
|
||||||
|
#include "../util/font.h"
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
|
|
||||||
Item ItemRegistry[ITEMREGISTRY_SIZE];
|
Item ItemRegistry[ITEMREGISTRY_SIZE];
|
||||||
@@ -13,61 +14,84 @@ Item ItemRegistry[ITEMREGISTRY_SIZE];
|
|||||||
uint16_t itemRegistryIndex = 0;
|
uint16_t itemRegistryIndex = 0;
|
||||||
|
|
||||||
void updateItems() {
|
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++) {
|
for (int y = 0; y < MAP_HEIGHT; y++) {
|
||||||
for (int x = 0; x < MAP_WIDTH; x++) {
|
for (int x = 0; x < MAP_WIDTH; x++) {
|
||||||
Tile *t = &tileMap[y][x];
|
Tile *t = &tileMap[y][x];
|
||||||
|
if (t->type != TYPE_BELT) continue;
|
||||||
|
int dir = t->direction;
|
||||||
|
|
||||||
|
bool horz = (dir == ORIENT_LEFT || dir == ORIENT_RIGHT);
|
||||||
|
bool vert = (dir == ORIENT_UP || dir == ORIENT_DOWN);
|
||||||
|
|
||||||
for (uint8_t lane = 0; lane < 2; lane++) {
|
for (uint8_t lane = 0; lane < 2; lane++) {
|
||||||
for (uint8_t itemIndex = 0; itemIndex < 2; itemIndex++) {
|
for (uint8_t slot = 0; slot < 2; slot++) {
|
||||||
if (t->type != TYPE_BELT || !t->items[lane][itemIndex].active) continue;
|
ItemOnBelt *itm = &t->items[lane][slot];
|
||||||
|
if (!itm->active) continue;
|
||||||
|
|
||||||
if (
|
// 1) Advance
|
||||||
(t->items[lane][itemIndex].x < -0.5 && t->direction == ORIENT_LEFT) ||
|
itm->offset += speed;
|
||||||
(t->items[lane][itemIndex].x > 1 && t->direction == ORIENT_RIGHT) ||
|
|
||||||
(t->items[lane][itemIndex].y < -0.5 && t->direction == ORIENT_UP) ||
|
// 2) Time to hop?
|
||||||
(t->items[lane][itemIndex].y > 1 && t->direction == ORIENT_DOWN)
|
if (itm->offset >= 0.5f) {
|
||||||
) {
|
itm->offset -= 1.0f;
|
||||||
int nx = x, ny = y;
|
|
||||||
if (t->direction == ORIENT_LEFT) nx--;
|
// target coords
|
||||||
if (t->direction == ORIENT_RIGHT) nx++;
|
int nx = x + dirDx[dir];
|
||||||
if (t->direction == ORIENT_UP) ny--;
|
int ny = y + dirDy[dir];
|
||||||
if (t->direction == ORIENT_DOWN) ny++;
|
|
||||||
if (nx >= 0 && nx < MAP_WIDTH && ny >= 0 && ny < MAP_HEIGHT) {
|
// bounds & belt?
|
||||||
Tile *next = &tileMap[ny][nx];
|
if (nx < 0 || nx >= MAP_WIDTH || ny < 0 || ny >= MAP_HEIGHT) {
|
||||||
if (next->type == TYPE_BELT && !next->items[lane][itemIndex].active) {
|
itm->active = false;
|
||||||
memcpy(&next->items[lane][itemIndex], &t->items[lane][itemIndex], sizeof(ItemOnBelt));
|
continue;
|
||||||
printf("Moved to X=%d, Y=%d\n", nx, ny);
|
}
|
||||||
next->items[lane][itemIndex].tileX = nx;
|
Tile *next = &tileMap[ny][nx];
|
||||||
next->items[lane][itemIndex].tileY = ny;
|
if (next->type != TYPE_BELT) {
|
||||||
if (t->direction == ORIENT_LEFT) next->items[lane][itemIndex].x = 0.5f;
|
itm->active = false;
|
||||||
if (t->direction == ORIENT_RIGHT) next->items[lane][itemIndex].x = 0;
|
continue;
|
||||||
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;
|
// Decide new lane
|
||||||
switch (t->direction) {
|
int newLane = lane;
|
||||||
case ORIENT_LEFT:
|
int newDir = next->direction;
|
||||||
t->items[lane][itemIndex].x -= speed;
|
bool nH = (newDir == ORIENT_LEFT || newDir == ORIENT_RIGHT);
|
||||||
break;
|
bool nV = (newDir == ORIENT_UP || newDir == ORIENT_DOWN);
|
||||||
case ORIENT_RIGHT:
|
|
||||||
t->items[lane][itemIndex].x += speed;
|
if ((horz && nH) || (vert && nV)) {
|
||||||
break;
|
// same axis → keep lane
|
||||||
case ORIENT_UP:
|
} else if (horz && nV) {
|
||||||
t->items[lane][itemIndex].y -= speed;
|
// came off a horizontal: lane0=top→vertical.left, lane1=bottom→vertical.right
|
||||||
break;
|
newLane = (dir == ORIENT_LEFT ? 1 : 0);
|
||||||
case ORIENT_DOWN:
|
} else if (vert && nH) {
|
||||||
t->items[lane][itemIndex].y += speed;
|
// came off vertical: lane0=left→horizontal.top, lane1=right→horizontal.bottom
|
||||||
break;
|
newLane = (dir == ORIENT_UP ? 0 : 1);
|
||||||
default:
|
}
|
||||||
break;
|
// (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;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
// clear this one
|
||||||
|
itm->active = false;
|
||||||
|
} else {
|
||||||
|
// both slots full → wait at end
|
||||||
|
itm->offset = epsilon;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -75,6 +99,7 @@ void updateItems() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void registerItem(char name[20], SDL_Renderer *renderer) {
|
void registerItem(char name[20], SDL_Renderer *renderer) {
|
||||||
const char *dot = strchr(name, '.');
|
const char *dot = strchr(name, '.');
|
||||||
memcpy(ItemRegistry[itemRegistryIndex].name, name, dot - name);
|
memcpy(ItemRegistry[itemRegistryIndex].name, name, dot - name);
|
||||||
@@ -90,14 +115,96 @@ void registerItem(char name[20], SDL_Renderer *renderer) {
|
|||||||
itemRegistryIndex++;
|
itemRegistryIndex++;
|
||||||
}
|
}
|
||||||
|
|
||||||
void renderItem(ItemOnBelt item, SDL_Renderer *renderer) {
|
void renderItem(ItemOnBelt item, SDL_Renderer *renderer, int lane) {
|
||||||
SDL_Rect rect = {0};
|
SDL_Rect rect = {0};
|
||||||
rect.x = (item.tileX * TILE_SIZE) + (item.x * TILE_SIZE);
|
rect.x = item.tileX * TILE_SIZE;
|
||||||
rect.y = (item.tileY * TILE_SIZE) + (item.y * TILE_SIZE);
|
rect.y = item.tileY * TILE_SIZE;
|
||||||
|
|
||||||
|
int dir = tileMap[item.tileY][item.tileX].direction;
|
||||||
|
int offset = item.offset * TILE_SIZE;
|
||||||
|
|
||||||
|
int dx = 0, dy = 0; // Direction vector
|
||||||
|
switch (dir) {
|
||||||
|
case ORIENT_LEFT_DOWN:
|
||||||
|
dx = -1;
|
||||||
|
dy = 1;
|
||||||
|
break;
|
||||||
|
case ORIENT_LEFT:
|
||||||
|
dx = -1;
|
||||||
|
dy = 0;
|
||||||
|
break;
|
||||||
|
case ORIENT_LEFT_UP:
|
||||||
|
dx = -1;
|
||||||
|
dy = -1;
|
||||||
|
break;
|
||||||
|
case ORIENT_UP:
|
||||||
|
dx = 0;
|
||||||
|
dy = -1;
|
||||||
|
break;
|
||||||
|
case ORIENT_RIGHT_UP:
|
||||||
|
dx = 1;
|
||||||
|
dy = -1;
|
||||||
|
break;
|
||||||
|
case ORIENT_RIGHT:
|
||||||
|
dx = 1;
|
||||||
|
dy = 0;
|
||||||
|
break;
|
||||||
|
case ORIENT_RIGHT_DOWN:
|
||||||
|
dx = 1;
|
||||||
|
dy = 1;
|
||||||
|
break;
|
||||||
|
case ORIENT_DOWN:
|
||||||
|
dx = 0;
|
||||||
|
dy = 1;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Main offset along belt direction
|
||||||
|
int xOffset = (offset * dx) % TILE_SIZE;
|
||||||
|
int yOffset = (offset * dy) % TILE_SIZE;
|
||||||
|
|
||||||
|
// Perpendicular vector: (dy, -dx)
|
||||||
|
|
||||||
|
float perpX = dy;
|
||||||
|
float perpY = -dx;
|
||||||
|
|
||||||
|
float length = sqrtf(perpX * perpX + perpY * perpY);
|
||||||
|
if (length != 0) {
|
||||||
|
perpX /= length;
|
||||||
|
perpY /= length;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Lane offset distance
|
||||||
|
float laneOffset = (lane == 1 ? 1.0f : -1.0f);
|
||||||
|
|
||||||
|
xOffset += (int) (perpX * laneOffset * (TILE_SIZE / 2));
|
||||||
|
yOffset += (int) (perpY * laneOffset * (TILE_SIZE / 2));
|
||||||
|
|
||||||
|
rect.x += xOffset;
|
||||||
|
rect.y += 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);
|
||||||
|
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);
|
||||||
|
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
|
||||||
|
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 32);
|
||||||
|
adjustRect(&rectA);
|
||||||
|
SDL_RenderFillRect(renderer, &rectA);
|
||||||
SDL_RenderCopy(renderer, ItemRegistry[item.type].textureOnBelt, NULL, &rect);
|
SDL_RenderCopy(renderer, ItemRegistry[item.type].textureOnBelt, NULL, &rect);
|
||||||
|
SDL_Texture *oldRenderTarget = SDL_GetRenderTarget(renderer);
|
||||||
|
SDL_SetRenderTarget(renderer, NULL);
|
||||||
|
renderText(renderer, fonts[3], tempStr, rectA.x, rectA.y);
|
||||||
|
SDL_SetRenderTarget(renderer, oldRenderTarget);
|
||||||
}
|
}
|
||||||
|
|
||||||
void renderBeltItems(SDL_Renderer *renderer) {
|
void renderBeltItems(SDL_Renderer *renderer) {
|
||||||
@@ -106,12 +213,7 @@ void renderBeltItems(SDL_Renderer *renderer) {
|
|||||||
|
|
||||||
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) {
|
||||||
tileMap[y][x].items[lane][itemIndex].type = itemType;
|
tileMap[y][x].items[lane][itemIndex].type = itemType;
|
||||||
tileMap[y][x].items[lane][itemIndex].x = 0.25f;
|
tileMap[y][x].items[lane][itemIndex].offset = 0;
|
||||||
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].active = true;
|
||||||
tileMap[y][x].items[lane][itemIndex].tileX = x;
|
tileMap[y][x].items[lane][itemIndex].tileX = x;
|
||||||
tileMap[y][x].items[lane][itemIndex].tileY = y;
|
tileMap[y][x].items[lane][itemIndex].tileY = y;
|
||||||
|
@@ -19,18 +19,17 @@ typedef struct {
|
|||||||
extern Item ItemRegistry[ITEMREGISTRY_SIZE];
|
extern Item ItemRegistry[ITEMREGISTRY_SIZE];
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
float x, y; // local position in tile (0.0–1.0)
|
float offset;
|
||||||
int tileX, tileY;
|
int tileX, tileY;
|
||||||
bool active;
|
bool active;
|
||||||
uint16_t type;
|
uint16_t type;
|
||||||
uint8_t place;
|
|
||||||
} ItemOnBelt;
|
} ItemOnBelt;
|
||||||
|
|
||||||
void updateItems();
|
void updateItems();
|
||||||
|
|
||||||
void loadItems(SDL_Renderer *renderer);
|
void loadItems(SDL_Renderer *renderer);
|
||||||
|
|
||||||
void renderItem(ItemOnBelt item, SDL_Renderer *renderer);
|
void renderItem(ItemOnBelt item, SDL_Renderer *renderer, int lane);
|
||||||
|
|
||||||
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
|
||||||
|
11
main.c
11
main.c
@@ -25,10 +25,8 @@ SDL_Renderer *renderer = NULL;
|
|||||||
#define biggerFont fonts[0]
|
#define biggerFont fonts[0]
|
||||||
#define smallFont fonts[1]
|
#define smallFont fonts[1]
|
||||||
#define smallerFont fonts[2]
|
#define smallerFont fonts[2]
|
||||||
|
#define smallestFont fonts[3]
|
||||||
|
|
||||||
#define fontCount 3
|
|
||||||
|
|
||||||
BitmapFont fonts[fontCount];
|
|
||||||
|
|
||||||
unsigned long frames = 0;
|
unsigned long frames = 0;
|
||||||
bool cursor = true;
|
bool cursor = true;
|
||||||
@@ -110,9 +108,10 @@ int init() {
|
|||||||
SDL_RenderSetViewport(renderer, &viewport);
|
SDL_RenderSetViewport(renderer, &viewport);
|
||||||
|
|
||||||
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
|
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
|
||||||
biggerFont = prepText(renderer, 16, "PublicPixel.ttf", 255, 255, 255, 255);
|
biggerFont = prepText(renderer, 16, "assets/PublicPixel.ttf", 255, 255, 255, 255);
|
||||||
smallFont = prepText(renderer, 12, "PublicPixel.ttf", 255, 255, 255, 255);
|
smallFont = prepText(renderer, 12, "assets/PublicPixel.ttf", 255, 255, 255, 255);
|
||||||
smallerFont = prepText(renderer, 8, "PublicPixel.ttf", 255, 255, 255, 255);
|
smallerFont = prepText(renderer, 8, "assets/PublicPixel.ttf", 255, 255, 255, 255);
|
||||||
|
smallestFont = prepText(renderer, 4, "assets/PublicPixel.ttf", 255, 255, 255, 255);
|
||||||
SDL_RenderSetLogicalSize(renderer, SCREEN_WIDTH, SCREEN_HEIGHT);
|
SDL_RenderSetLogicalSize(renderer, SCREEN_WIDTH, SCREEN_HEIGHT);
|
||||||
|
|
||||||
generateTestMap();
|
generateTestMap();
|
||||||
|
@@ -90,7 +90,7 @@ void renderAllBelts(SDL_Renderer *renderer) {
|
|||||||
for (uint8_t lane = 0; lane < 2; lane++) {
|
for (uint8_t lane = 0; lane < 2; lane++) {
|
||||||
for (uint8_t itemIndex = 0; itemIndex < 2; itemIndex++) {
|
for (uint8_t itemIndex = 0; itemIndex < 2; itemIndex++) {
|
||||||
if (t.items[lane][itemIndex].active) {
|
if (t.items[lane][itemIndex].active) {
|
||||||
renderItem(t.items[lane][itemIndex], renderer);
|
renderItem(t.items[lane][itemIndex], renderer, lane);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -4,6 +4,9 @@
|
|||||||
|
|
||||||
#include "font.h"
|
#include "font.h"
|
||||||
|
|
||||||
|
BitmapFont fonts[fontCount];
|
||||||
|
|
||||||
|
|
||||||
BitmapFont
|
BitmapFont
|
||||||
prepText(SDL_Renderer *renderer, unsigned char pxSize, const char *file, uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
|
prepText(SDL_Renderer *renderer, unsigned char pxSize, const char *file, uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
|
||||||
TTF_Font *gFont = TTF_OpenFont(file, pxSize);
|
TTF_Font *gFont = TTF_OpenFont(file, pxSize);
|
||||||
|
@@ -9,6 +9,8 @@
|
|||||||
#include <SDL2/SDL_ttf.h>
|
#include <SDL2/SDL_ttf.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
|
||||||
|
#define fontCount 4
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
SDL_Texture *texture[256];
|
SDL_Texture *texture[256];
|
||||||
SDL_Surface *surface[256];
|
SDL_Surface *surface[256];
|
||||||
@@ -16,6 +18,8 @@ typedef struct {
|
|||||||
SDL_Color color;
|
SDL_Color color;
|
||||||
} BitmapFont;
|
} BitmapFont;
|
||||||
|
|
||||||
|
extern BitmapFont fonts[fontCount];
|
||||||
|
|
||||||
BitmapFont
|
BitmapFont
|
||||||
prepText(SDL_Renderer *renderer, unsigned char pxSize, const char *file, uint8_t r, uint8_t g, uint8_t b, uint8_t a);
|
prepText(SDL_Renderer *renderer, unsigned char pxSize, const char *file, uint8_t r, uint8_t g, uint8_t b, uint8_t a);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user