This commit is contained in:
2025-04-30 19:01:44 +02:00
parent 451e80f750
commit 429627c095
10 changed files with 100 additions and 73 deletions

View File

@@ -29,12 +29,15 @@ add_executable(factorygame
set(ASSETS_SOURCE_DIR "${CMAKE_SOURCE_DIR}/assets")
set(ASSETS_BINARY_DIR "${CMAKE_BINARY_DIR}/assets")
# Copy assets directory after build
add_custom_command(TARGET factorygame POST_BUILD
# Copy assets directory always
add_custom_target(copy_assets ALL
COMMAND ${CMAKE_COMMAND} -E copy_directory
"${ASSETS_SOURCE_DIR}" "${ASSETS_BINARY_DIR}"
COMMENT "Copying assets directory to build output..."
)
# Make sure factorygame depends on the assets being copied
add_dependencies(factorygame copy_assets)
target_link_libraries(factorygame SDL2 SDL2_ttf SDL2_image SDL2_gfx SDL2_mixer SDL2_net m)

BIN
assets/adopravnikstary.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 215 B

BIN
assets/items/blanker.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 120 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 215 B

After

Width:  |  Height:  |  Size: 247 B

View File

@@ -16,13 +16,15 @@ void updateItems() {
for (int y = 0; y < MAP_HEIGHT; y++) {
for (int x = 0; x < MAP_WIDTH; x++) {
Tile *t = &tileMap[y][x];
if (t->type != TYPE_BELT || !t->item.active) continue;
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->item.x < 0 && t->direction == ORIENT_LEFT) ||
(t->item.x > 0.75f && t->direction == ORIENT_RIGHT) ||
(t->item.y < 0.25f && t->direction == ORIENT_UP) ||
(t->item.y > 0.625f && t->direction == ORIENT_DOWN)
(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--;
@@ -31,44 +33,46 @@ void updateItems() {
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->item.active) {
memcpy(&next->item, &t->item, sizeof(ItemOnBelt));
printf("Moved to X=%d, Y=%d", nx, ny);
next->item.tileX = nx;
next->item.tileY = ny;
if (t->direction == ORIENT_LEFT) next->item.x = 0.5f;
if (t->direction == ORIENT_RIGHT) next->item.x = 0.25f;
if (t->direction == ORIENT_UP) next->item.y = 0.5f;
if (t->direction == ORIENT_DOWN) next->item.y = 0;
next->item.active = true;
t->item.active = false;
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->item.active = false;
t->items[lane][itemIndex].active = false;
}
}
float speed = 0.02f;
float speed = 0.002f;
switch (t->direction) {
case ORIENT_LEFT:
t->item.x -= speed;
t->items[lane][itemIndex].x -= speed;
break;
case ORIENT_RIGHT:
t->item.x += speed;
t->items[lane][itemIndex].x += speed;
break;
case ORIENT_UP:
t->item.y -= speed;
t->items[lane][itemIndex].y -= speed;
break;
case ORIENT_DOWN:
t->item.y += speed;
t->items[lane][itemIndex].y += speed;
break;
default:
break;
}
}
}
}
}
}
void registerItem(char name[20], SDL_Renderer *renderer) {
@@ -100,17 +104,17 @@ void renderBeltItems(SDL_Renderer *renderer) {
}
void putItem(int x, int y, uint16_t itemType) {
tileMap[y][x].item.type = itemType;
tileMap[y][x].item.x = 0.25f;
tileMap[y][x].item.y = 0.25f;
if (tileMap[y][x].direction == ORIENT_LEFT) tileMap[y][x].item.x = 0.5f;
if (tileMap[y][x].direction == ORIENT_RIGHT) tileMap[y][x].item.x = 0.25f;
if (tileMap[y][x].direction == ORIENT_UP) tileMap[y][x].item.y = 0.5f;
if (tileMap[y][x].direction == ORIENT_DOWN) tileMap[y][x].item.y = 0.25f;
tileMap[y][x].item.active = true;
tileMap[y][x].item.tileX = x;
tileMap[y][x].item.tileY = y;
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) {

View File

@@ -23,6 +23,7 @@ typedef struct {
int tileX, tileY;
bool active;
uint16_t type;
uint8_t place;
} ItemOnBelt;
void updateItems();
@@ -31,5 +32,5 @@ void loadItems(SDL_Renderer *renderer);
void renderItem(ItemOnBelt item, SDL_Renderer *renderer);
void putItem(int x, int y, uint16_t itemType);
void putItem(int x, int y, uint16_t itemType, uint8_t lane, uint8_t itemIndex);
#endif //FACTORYGAME_ITEM_H

10
main.c
View File

@@ -6,6 +6,7 @@
#include "tiles/tile.h"
#include "tiles/belt.h"
#include "items/item.h"
#include "stdlib.h"
#include "player/player.h"
//Screen dimension constants
@@ -42,6 +43,9 @@ void msleep(unsigned int milliseconds) {
int init() {
//Initialize SDL
srand(0);
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, NULL);
SDL_SetHint(SDL_HINT_VIDEO_HIGHDPI_DISABLED, "1");
SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl");
@@ -181,9 +185,9 @@ int main(__attribute__((unused)) int argc, __attribute__((unused)) char *args[])
}
uint8_t type = 0;
for (int x = 149; x < 152; x++) {
for(int y = 87; y < 90; y++) {
putItem(x, y, type++);
for (int x = 142; x < 154; x+=3) {
for(int y = 80; y < 94; y+=3) {
putItem(x, y, type++ % ITEMREGISTRY_SIZE, 0, 0);
}
}

View File

@@ -12,7 +12,7 @@ static int scrollFrame = 0;
unsigned long beltFrames = 0;
void renderBelt(int x, int y, int w, int h, OrientDirection dir, SDL_Renderer * renderer) {
void renderBelt(int x, int y, int w, int h, OrientDirection dir, SDL_Renderer *renderer) {
int px = x * TILE_SIZE;
int py = y * TILE_SIZE;
@@ -63,8 +63,8 @@ void renderBelt(int x, int y, int w, int h, OrientDirection dir, SDL_Renderer *
}
void renderAllBelts(SDL_Renderer * renderer) {
int scrollSpeed = 1; // pixels per step
void renderAllBelts(SDL_Renderer *renderer) {
int scrollSpeed = 0; // pixels per step
int scrollDelay = 1; // frames between steps
if (beltFrames++ % scrollDelay == 0) {
@@ -72,13 +72,27 @@ void renderAllBelts(SDL_Renderer * renderer) {
}
int tileSize = TILE_SIZE;
for (int y = (playerY / TILE_SIZE) - (DISPLAY_MAP_HEIGHT / 2); y < (playerY / TILE_SIZE) + (DISPLAY_MAP_HEIGHT / 2); y++) {
for (int x = (playerX / TILE_SIZE) - (DISPLAY_MAP_WIDTH / 2); x < (playerX / TILE_SIZE) + (DISPLAY_MAP_WIDTH / 2); x++) {
for (int y = (playerY / TILE_SIZE) - (DISPLAY_MAP_HEIGHT / 2);
y < (playerY / TILE_SIZE) + (DISPLAY_MAP_HEIGHT / 2); y++) {
for (int x = (playerX / TILE_SIZE) - (DISPLAY_MAP_WIDTH / 2);
x < (playerX / TILE_SIZE) + (DISPLAY_MAP_WIDTH / 2); x++) {
Tile t = tileMap[y][x];
if (t.type != TYPE_BELT) continue;
renderBelt(x, y, tileSize, tileSize, t.direction, renderer);
if (t.item.active) {
renderItem(t.item, renderer);
}
}
for (int y = (playerY / TILE_SIZE) - (DISPLAY_MAP_HEIGHT / 2);
y < (playerY / TILE_SIZE) + (DISPLAY_MAP_HEIGHT / 2); y++) {
for (int x = (playerX / TILE_SIZE) - (DISPLAY_MAP_WIDTH / 2);
x < (playerX / TILE_SIZE) + (DISPLAY_MAP_WIDTH / 2); x++) {
Tile t = tileMap[y][x];
if (t.type != TYPE_BELT) continue;
for (uint8_t lane = 0; lane < 2; lane++) {
for (uint8_t itemIndex = 0; itemIndex < 2; itemIndex++) {
if (t.items[lane][itemIndex].active) {
renderItem(t.items[lane][itemIndex], renderer);
}
}
}
}
}

View File

@@ -31,7 +31,8 @@ void generateTestMap() {
tileMap[y][x].type = TYPE_BELT;
tileMap[y][x].frameOffset = 0;
//tileMap[y][x].direction = ((x + y) % 4 * 2) + 1;
tileMap[y][x].direction = 5;
//tileMap[y][x].direction = 5;
tileMap[y][x].direction = (rand() % 4 * 2) + 1;
}
}
}

View File

@@ -36,7 +36,7 @@ typedef struct {
OrientDirection direction;
uint16_t type;
int frameOffset;
ItemOnBelt item;
ItemOnBelt items[2][2];
int x;
int y;
} Tile;