diff --git a/CMakeLists.txt b/CMakeLists.txt index 5ea9c43..6e8b3bf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) \ No newline at end of file diff --git a/assets/adopravnikstary.png b/assets/adopravnikstary.png new file mode 100644 index 0000000..53c5dea Binary files /dev/null and b/assets/adopravnikstary.png differ diff --git a/assets/items/blanker.png b/assets/items/blanker.png new file mode 100644 index 0000000..0ff0925 Binary files /dev/null and b/assets/items/blanker.png differ diff --git a/assets/tiles/dopravnik.png b/assets/tiles/dopravnik.png index 53c5dea..1558e63 100644 Binary files a/assets/tiles/dopravnik.png and b/assets/tiles/dopravnik.png differ diff --git a/items/item.c b/items/item.c index 262b280..58230c2 100644 --- a/items/item.c +++ b/items/item.c @@ -16,56 +16,60 @@ 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) - ) { - int nx = x, ny = y; - if (t->direction == ORIENT_LEFT) nx--; - if (t->direction == ORIENT_RIGHT) nx++; - if (t->direction == ORIENT_UP) ny--; - 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; - } else { - continue; + if ( + (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--; + if (t->direction == ORIENT_RIGHT) nx++; + if (t->direction == ORIENT_UP) ny--; + 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->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->items[lane][itemIndex].active = false; + } } - } else { - t->item.active = false; - } - } - float speed = 0.02f; - switch (t->direction) { - case ORIENT_LEFT: - t->item.x -= speed; - break; - case ORIENT_RIGHT: - t->item.x += speed; - break; - case ORIENT_UP: - t->item.y -= speed; - break; - case ORIENT_DOWN: - t->item.y += speed; - break; - default: - break; + float speed = 0.002f; + switch (t->direction) { + case ORIENT_LEFT: + t->items[lane][itemIndex].x -= speed; + break; + case ORIENT_RIGHT: + t->items[lane][itemIndex].x += speed; + break; + case ORIENT_UP: + t->items[lane][itemIndex].y -= speed; + break; + case ORIENT_DOWN: + t->items[lane][itemIndex].y += speed; + break; + default: + break; + } + } } } } @@ -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) { diff --git a/items/item.h b/items/item.h index 7f36d01..56e8160 100644 --- a/items/item.h +++ b/items/item.h @@ -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 diff --git a/main.c b/main.c index 05f59b6..5134b52 100644 --- a/main.c +++ b/main.c @@ -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); } } diff --git a/tiles/belt.c b/tiles/belt.c index 7ce317f..e9daaf8 100644 --- a/tiles/belt.c +++ b/tiles/belt.c @@ -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); + } + } } } } diff --git a/tiles/tile.c b/tiles/tile.c index d339e3e..8e3a5a0 100644 --- a/tiles/tile.c +++ b/tiles/tile.c @@ -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; } } } diff --git a/tiles/tile.h b/tiles/tile.h index dc50e5f..8cd6470 100644 --- a/tiles/tile.h +++ b/tiles/tile.h @@ -36,7 +36,7 @@ typedef struct { OrientDirection direction; uint16_t type; int frameOffset; - ItemOnBelt item; + ItemOnBelt items[2][2]; int x; int y; } Tile;