Hopefully last commit
@@ -53,6 +53,16 @@ set(SOURCE_FILES
|
||||
tiles/turret.h
|
||||
util/crafter.c
|
||||
util/crafter.h
|
||||
util/gamestate.c
|
||||
util/gamestate.h
|
||||
util/button.c
|
||||
util/button.h
|
||||
tiles/ammoCrafter.c
|
||||
tiles/ammoCrafter.h
|
||||
tiles/wiredrawer.c
|
||||
tiles/wiredrawer.h
|
||||
tiles/core.c
|
||||
tiles/core.h
|
||||
)
|
||||
|
||||
add_executable(factorygame ${SOURCE_FILES})
|
||||
|
BIN
assets/backgrounds/28_enemy-floor_00.png
Normal file
After Width: | Height: | Size: 828 B |
BIN
assets/backgrounds/28_enemy-floor_01.png
Normal file
After Width: | Height: | Size: 805 B |
BIN
assets/backgrounds/28_enemy-floor_02.png
Normal file
After Width: | Height: | Size: 792 B |
BIN
assets/backgrounds/28_enemy-floor_03.png
Normal file
After Width: | Height: | Size: 838 B |
BIN
assets/backgrounds/28_enemy-floor_04.png
Normal file
After Width: | Height: | Size: 841 B |
BIN
assets/backgrounds/28_enemy-floor_05.png
Normal file
After Width: | Height: | Size: 876 B |
Before Width: | Height: | Size: 405 B After Width: | Height: | Size: 482 B |
BIN
assets/entities/00_enemy_01.png
Normal file
After Width: | Height: | Size: 482 B |
BIN
assets/entities/00_enemy_02.png
Normal file
After Width: | Height: | Size: 484 B |
BIN
assets/entities/00_enemy_03.png
Normal file
After Width: | Height: | Size: 484 B |
BIN
assets/entities/00_enemy_04.png
Normal file
After Width: | Height: | Size: 483 B |
BIN
assets/icon.png
Normal file
After Width: | Height: | Size: 684 B |
BIN
assets/items/13-silver_plate-00.png
Normal file
After Width: | Height: | Size: 520 B |
BIN
assets/items/14-gold_plate-00.png
Normal file
After Width: | Height: | Size: 587 B |
BIN
assets/items/15-platinum_plate-00.png
Normal file
After Width: | Height: | Size: 555 B |
BIN
assets/items/16-iron_plate-00.png
Normal file
After Width: | Height: | Size: 544 B |
BIN
assets/items/17-silver_rod-00.png
Normal file
After Width: | Height: | Size: 526 B |
BIN
assets/items/18-gold_rod-00.png
Normal file
After Width: | Height: | Size: 531 B |
BIN
assets/items/19-platinum_rod-00.png
Normal file
After Width: | Height: | Size: 496 B |
BIN
assets/items/20-iron_rod-00.png
Normal file
After Width: | Height: | Size: 494 B |
Before Width: | Height: | Size: 398 B After Width: | Height: | Size: 573 B |
BIN
assets/tiles/08_ammocrafter_00.png
Normal file
After Width: | Height: | Size: 636 B |
BIN
assets/tiles/08_ammocrafter_01.png
Normal file
After Width: | Height: | Size: 636 B |
BIN
assets/tiles/08_ammocrafter_02.png
Normal file
After Width: | Height: | Size: 662 B |
BIN
assets/tiles/08_ammocrafter_03.png
Normal file
After Width: | Height: | Size: 673 B |
BIN
assets/tiles/08_ammocrafter_04.png
Normal file
After Width: | Height: | Size: 672 B |
BIN
assets/tiles/09_wirecrafter_00.png
Normal file
After Width: | Height: | Size: 522 B |
BIN
assets/tiles/09_wirecrafter_01.png
Normal file
After Width: | Height: | Size: 528 B |
BIN
assets/tiles/09_wirecrafter_02.png
Normal file
After Width: | Height: | Size: 525 B |
BIN
assets/tiles/09_wirecrafter_03.png
Normal file
After Width: | Height: | Size: 524 B |
BIN
assets/tiles/09_wirecrafter_04.png
Normal file
After Width: | Height: | Size: 525 B |
BIN
assets/tiles/09_wirecrafter_05.png
Normal file
After Width: | Height: | Size: 525 B |
BIN
assets/tiles/09_wirecrafter_06.png
Normal file
After Width: | Height: | Size: 526 B |
BIN
assets/tiles/09_wirecrafter_07.png
Normal file
After Width: | Height: | Size: 526 B |
BIN
assets/tiles/09_wirecrafter_08.png
Normal file
After Width: | Height: | Size: 527 B |
BIN
assets/tiles/09_wirecrafter_09.png
Normal file
After Width: | Height: | Size: 528 B |
BIN
assets/tiles/09_wirecrafter_10.png
Normal file
After Width: | Height: | Size: 528 B |
BIN
assets/tiles/09_wirecrafter_11.png
Normal file
After Width: | Height: | Size: 528 B |
BIN
assets/tiles/09_wirecrafter_12.png
Normal file
After Width: | Height: | Size: 527 B |
BIN
assets/tiles/09_wirecrafter_13.png
Normal file
After Width: | Height: | Size: 528 B |
BIN
assets/tiles/09_wirecrafter_14.png
Normal file
After Width: | Height: | Size: 528 B |
BIN
assets/tiles/09_wirecrafter_15.png
Normal file
After Width: | Height: | Size: 528 B |
BIN
assets/tiles/09_wirecrafter_16.png
Normal file
After Width: | Height: | Size: 526 B |
BIN
assets/tiles/09_wirecrafter_17.png
Normal file
After Width: | Height: | Size: 526 B |
BIN
assets/tiles/09_wirecrafter_18.png
Normal file
After Width: | Height: | Size: 525 B |
BIN
assets/tiles/09_wirecrafter_19.png
Normal file
After Width: | Height: | Size: 525 B |
BIN
assets/tiles/09_wirecrafter_20.png
Normal file
After Width: | Height: | Size: 524 B |
BIN
assets/tiles/09_wirecrafter_21.png
Normal file
After Width: | Height: | Size: 525 B |
BIN
assets/tiles/09_wirecrafter_22.png
Normal file
After Width: | Height: | Size: 528 B |
161
entity/entity.c
@@ -14,6 +14,8 @@ EntityArray entities;
|
||||
|
||||
EntityTypeReg EntityRegistry[ENTITY_MAX_COUNT];
|
||||
|
||||
EnemySpawnState currentSpawnState;
|
||||
|
||||
void renderEntities(SDL_Renderer *renderer, SDL_Rect playerRect) {
|
||||
SDL_Texture *oldTarget = SDL_GetRenderTarget(renderer);
|
||||
SDL_SetRenderTarget(renderer, entityTexture);
|
||||
@@ -44,6 +46,7 @@ void updateEntities(Player *plr) {
|
||||
remove_entity(&entities, i);
|
||||
continue;
|
||||
}
|
||||
|
||||
bool atTargetSnapshot = ent->tileRect.x == ent->targetSnapshot.x &&
|
||||
ent->tileRect.y == ent->targetSnapshot.y;
|
||||
|
||||
@@ -51,22 +54,19 @@ void updateEntities(Player *plr) {
|
||||
ent->tileRect.y == ent->target.y;
|
||||
|
||||
if (animationStep >= ent->entityNextTick) {
|
||||
if (sqrt(pow(abs(plr->tileRect.x - ent->tileRect.x), 2) + pow(abs(plr->tileRect.y - ent->tileRect.y), 2)) < ENEMY_RANGE) {
|
||||
if (sqrt(pow(abs(plr->tileRect.x - ent->tileRect.x), 2) +
|
||||
pow(abs(plr->tileRect.y - ent->tileRect.y), 2)) < ENEMY_RANGE) {
|
||||
plr->health -= ENEMY_DAMAGE;
|
||||
}
|
||||
if (plr->tileRect.x)
|
||||
for (int y = ent->tileRect.y - ENEMY_RANGE; y < ent->tileRect.y + ENEMY_RANGE; y++) {
|
||||
if (y < 0 || y >= MAP_HEIGHT) {
|
||||
continue;
|
||||
}
|
||||
for (int x = ent->tileRect.x - ENEMY_RANGE; x < ent->tileRect.x + ENEMY_RANGE; x++) {
|
||||
if (x < 0 || x >= MAP_WIDTH) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (int y = ent->tileRect.y - ENEMY_RANGE; y <= ent->tileRect.y + ENEMY_RANGE; y++) {
|
||||
if (y < 0 || y >= MAP_HEIGHT) continue;
|
||||
for (int x = ent->tileRect.x - ENEMY_RANGE; x <= ent->tileRect.x + ENEMY_RANGE; x++) {
|
||||
if (x < 0 || x >= MAP_WIDTH) continue;
|
||||
|
||||
Tile *targTile = &tileMap[y][x];
|
||||
if (targTile->type == TYPE_AIR) {
|
||||
continue;
|
||||
}
|
||||
if (targTile->type == TYPE_AIR) continue;
|
||||
|
||||
targTile->health -= ENEMY_DAMAGE;
|
||||
if (targTile->health <= 0) {
|
||||
if (targTile->audioCh < NUM_SYNTH_VOICES) {
|
||||
@@ -79,31 +79,63 @@ void updateEntities(Player *plr) {
|
||||
}
|
||||
}
|
||||
|
||||
// Retry pathfinding every 10 ticks if we don't have a path and aren't at the target
|
||||
bool shouldRetryPathfinding = (
|
||||
ent->path.length == 0 &&
|
||||
!atTarget &&
|
||||
(animationStep % 10 == 0)
|
||||
);
|
||||
|
||||
// Also try pathfinding if we reached the snapshot (end of path)
|
||||
if (atTargetSnapshot || shouldRetryPathfinding) {
|
||||
if (isWalkable(ent->target) && find_path(ent->tileRect, ent->target)) {
|
||||
ent->path = reconstruct_path(ent->target);
|
||||
MiniRect fallbackTarget = ent->target;
|
||||
|
||||
// If the target is not walkable, search nearby
|
||||
if (!isWalkable(ent->target)) {
|
||||
int bestDist = 999999;
|
||||
bool found = false;
|
||||
|
||||
for (int dy = -5; dy <= 5; dy++) {
|
||||
for (int dx = -5; dx <= 5; dx++) {
|
||||
int nx = ent->target.x + dx;
|
||||
int ny = ent->target.y + dy;
|
||||
|
||||
if (nx < 0 || ny < 0 || nx >= MAP_WIDTH || ny >= MAP_HEIGHT) continue;
|
||||
|
||||
MiniRect check = {nx, ny};
|
||||
|
||||
if (!isWalkable(check)) continue;
|
||||
|
||||
int dist = abs(dx) + abs(dy); // Manhattan distance
|
||||
if (dist < bestDist) {
|
||||
bestDist = dist;
|
||||
fallbackTarget = check;
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
// No walkable fallback tile found
|
||||
ent->path.length = 0;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// Attempt pathfinding to fallbackTarget
|
||||
if (find_path(ent->tileRect, fallbackTarget)) {
|
||||
ent->path = reconstruct_path(fallbackTarget);
|
||||
ent->path.stepIndex = 0;
|
||||
ent->targetSnapshot = ent->target; // snapshot the current target
|
||||
ent->targetSnapshot = fallbackTarget;
|
||||
ent->fromTile = ent->tileRect;
|
||||
ent->toTile = ent->path.steps[0];
|
||||
ent->interpolateTick = 0;
|
||||
ent->entityNextTick = animationStep + entT.entityTickRate;
|
||||
} else if (atTargetSnapshot) {
|
||||
// No path found and we finished current one — freeze
|
||||
ent->path.length = 0;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// Step 2: Movement
|
||||
// Movement
|
||||
if (ent->path.length > 0 && ent->path.stepIndex < ent->path.length &&
|
||||
animationStep >= ent->entityNextTick) {
|
||||
ent->fromTile = ent->tileRect;
|
||||
@@ -114,7 +146,7 @@ void updateEntities(Player *plr) {
|
||||
ent->path.stepIndex++;
|
||||
}
|
||||
|
||||
// Step 3: Interpolation
|
||||
// Interpolation
|
||||
MiniRect from = {
|
||||
.x = ent->fromTile.x * TILE_SIZE,
|
||||
.y = ent->fromTile.y * TILE_SIZE
|
||||
@@ -166,7 +198,7 @@ void registerEntity(char fname[20], SDL_Renderer *renderer) {
|
||||
EntityRegistry[indexEntity].type = indexEntity;
|
||||
EntityRegistry[indexEntity].animation.frameCount = frame + 1;
|
||||
EntityRegistry[indexEntity].animation.divisor = 1;
|
||||
EntityRegistry[indexEntity].entityTickRate = 8;
|
||||
EntityRegistry[indexEntity].entityTickRate = 16;
|
||||
EntityRegistry[indexEntity].maxHealth = 100;
|
||||
|
||||
if (indexEntity + 1 > backgroundTileTypeIndex) {
|
||||
@@ -213,6 +245,7 @@ void loadEntities(SDL_Renderer *renderer) {
|
||||
registerEntity(fileName, renderer);
|
||||
free(entityNames[i]);
|
||||
}
|
||||
EntityRegistry[GHOST].animation.divisor = 16;
|
||||
}
|
||||
|
||||
int add_entity(EntityArray *arr, Entity t) {
|
||||
@@ -226,4 +259,88 @@ void remove_entity(EntityArray *arr, int index) {
|
||||
if (index < 0 || index >= arr->activeCount) return;
|
||||
arr->activeCount--;
|
||||
arr->entities[index] = arr->entities[arr->activeCount]; // swap with last active
|
||||
}
|
||||
|
||||
|
||||
bool isTileAlreadyTargeted(int x, int y) {
|
||||
for (int i = 0; i < entities.activeCount; i++) {
|
||||
Entity *ent = &entities.entities[i];
|
||||
if (ent->target.x == x && ent->target.y == y) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void spawn_enemy_at_random_tile() {
|
||||
Entity ent;
|
||||
memset(&ent, 0, sizeof(Entity));
|
||||
|
||||
// Start by placing it at a default location (same as before)
|
||||
int offsetX = (rand() % (SPAWN_RADIUS * 2 + 1)) - SPAWN_RADIUS;
|
||||
int offsetY = (rand() % (SPAWN_RADIUS * 2 + 1)) - SPAWN_RADIUS;
|
||||
|
||||
ent.tileRect.x = enemySpawn.x + offsetX;
|
||||
ent.tileRect.y = enemySpawn.y + offsetY;
|
||||
ent.renderRect.x = ent.tileRect.x * TILE_SIZE;
|
||||
ent.renderRect.y = ent.tileRect.y * TILE_SIZE;
|
||||
ent.renderRect.w = TILE_SIZE;
|
||||
ent.renderRect.h = TILE_SIZE;
|
||||
ent.health = 100;
|
||||
ent.type = GHOST;
|
||||
|
||||
// Try to find a unique, walkable target tile near the player
|
||||
for (int attempt = 0; attempt < MAX_SPAWN_ATTEMPTS; attempt++) {
|
||||
int targetOffsetX = (rand() % (SPAWN_RADIUS * 2 + 1)) - SPAWN_RADIUS;
|
||||
int targetOffsetY = (rand() % (SPAWN_RADIUS * 2 + 1)) - SPAWN_RADIUS;
|
||||
|
||||
int targetX = mainPlayer.tileRect.x + targetOffsetX;
|
||||
int targetY = mainPlayer.tileRect.y + targetOffsetY;
|
||||
MiniRect target = {targetX, targetY};
|
||||
|
||||
if (!isWalkable(target)) continue;
|
||||
if (isTileAlreadyTargeted(targetX, targetY)) continue;
|
||||
|
||||
ent.target.x = targetX;
|
||||
ent.target.y = targetY;
|
||||
|
||||
add_entity(&entities, ent);
|
||||
return;
|
||||
}
|
||||
|
||||
printf("Failed to spawn enemy: no unique target found after %d attempts.\n", MAX_SPAWN_ATTEMPTS);
|
||||
}
|
||||
void updateWaveLogic(WaveInfo *info) {
|
||||
if (info->waveRunning) {
|
||||
Wave *currentWave = &info->waves[info->waveCounter];
|
||||
|
||||
// Cooldown between enemy spawns
|
||||
if (--currentSpawnState.spawnCooldown <= 0 &&
|
||||
currentSpawnState.enemiesSpawned < currentWave->enemies[0].count) {
|
||||
|
||||
spawn_enemy_at_random_tile();
|
||||
|
||||
currentSpawnState.enemiesSpawned++;
|
||||
currentSpawnState.spawnCooldown = SPAWN_COOLDOWN;
|
||||
}
|
||||
|
||||
// All enemies spawned
|
||||
if (currentSpawnState.enemiesSpawned >= currentWave->enemies[0].count) {
|
||||
// Wait for all enemies to be dead before ending wave
|
||||
if (entities.activeCount == 0) {
|
||||
info->waveRunning = false;
|
||||
info->waveTimer = info->waves[info->waveCounter].timeUntilNext;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Timer starts only after wave is completely over (including enemies)
|
||||
if (--info->waveTimer <= 0 && info->waveCounter + 1 < info->totalWaves) {
|
||||
// Start next wave
|
||||
info->waveCounter++;
|
||||
info->waveRunning = true;
|
||||
|
||||
currentSpawnState.enemiesSpawned = 0;
|
||||
currentSpawnState.spawnCooldown = 0;
|
||||
}
|
||||
}
|
||||
}
|
@@ -11,9 +11,22 @@
|
||||
|
||||
#define ENTITY_MAX_COUNT 1024
|
||||
|
||||
#define ENEMY_DAMAGE 2
|
||||
#define ENEMY_DAMAGE 1
|
||||
#define ENEMY_RANGE 3
|
||||
|
||||
#define SPAWN_RADIUS 5
|
||||
|
||||
typedef struct EnemySpawnState {
|
||||
int enemiesSpawned;
|
||||
int spawnCooldown;
|
||||
} EnemySpawnState;
|
||||
|
||||
#define SPAWN_COOLDOWN 30 // frames between each enemy spawn
|
||||
|
||||
#define MAX_SPAWN_ATTEMPTS 50
|
||||
|
||||
extern EnemySpawnState currentSpawnState;
|
||||
|
||||
typedef enum EntityType {
|
||||
GHOST,
|
||||
} EntityType;
|
||||
@@ -56,5 +69,6 @@ void renderEntities(SDL_Renderer *renderer, SDL_Rect playerRect);
|
||||
void updateEntities(Player * plr);
|
||||
void registerEntity(char fname[20], SDL_Renderer *renderer);
|
||||
void loadEntities(SDL_Renderer *renderer);
|
||||
void updateWaveLogic(WaveInfo* info);
|
||||
|
||||
#endif //FACTORYGAME_ENTITY_H
|
||||
|
14
items/item.c
@@ -67,7 +67,7 @@ OrientDirection rotateMainDirection(OrientDirection dir, int steps) {
|
||||
int newIndex = (index + steps) % count;
|
||||
if (newIndex < 0) newIndex += count;
|
||||
|
||||
return (OrientDirection)mainDirs[newIndex];
|
||||
return (OrientDirection) mainDirs[newIndex];
|
||||
}
|
||||
|
||||
// Map 8 directions to main 4 for code output
|
||||
@@ -225,9 +225,17 @@ void updateItems() {
|
||||
|
||||
|
||||
if (!putOntoNext(itm, nx, ny, next, &ntt, newLane) && (next->type != TYPE_BELT || newLane >= 2)) {
|
||||
bool alreadyPresent = false;
|
||||
for (uint8_t nLane = 0; nLane < ItemSlotCount; nLane++) {
|
||||
if (putOntoNext(itm, nx, ny, next, &ntt, nLane)) {
|
||||
break;
|
||||
if (next->items[nLane].type == itm->type) {
|
||||
alreadyPresent = true;
|
||||
}
|
||||
}
|
||||
if (!alreadyPresent) {
|
||||
for (uint8_t nLane = 0; nLane < ItemSlotCount; nLane++) {
|
||||
if (putOntoNext(itm, nx, ny, next, &ntt, nLane)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
12
items/item.h
@@ -22,6 +22,8 @@ typedef enum ItemType {
|
||||
TYPE_TURRET,
|
||||
TYPE_SPLITTER,
|
||||
TYPE_CORE,
|
||||
TYPE_AMMOCRAFTER,
|
||||
TYPE_WIRECRAFTER,
|
||||
IRON_ORE = ITEMREGISTRY_SIZE / 2,
|
||||
SILVER_ORE,
|
||||
GOLD_ORE,
|
||||
@@ -30,11 +32,19 @@ typedef enum ItemType {
|
||||
SILVER_INGOT,
|
||||
GOLD_INGOT,
|
||||
PLATINUM_INGOT,
|
||||
LOG,
|
||||
IRON_BULLET,
|
||||
SILVER_BULLET,
|
||||
GOLD_BULLET,
|
||||
PLATINUM_BULLET,
|
||||
LOG,
|
||||
SILVER_PLATE,
|
||||
GOLD_PLATE,
|
||||
PLATINUM_PLATE,
|
||||
IRON_PLATE,
|
||||
SILVER_ROD,
|
||||
GOLD_ROD,
|
||||
PLATINUM_ROD,
|
||||
IRON_ROD,
|
||||
} ItemType;
|
||||
|
||||
|
||||
|
754
main.c
@@ -7,73 +7,12 @@
|
||||
#include "items/item.h"
|
||||
#include "stdlib.h"
|
||||
#include "player/player.h"
|
||||
#include "util/perlin.h"
|
||||
#include "util/atlas.h"
|
||||
#include "entity/entity.h"
|
||||
#include "util/gamestate.h"
|
||||
#include "util/button.h"
|
||||
|
||||
typedef struct GameState {
|
||||
Player player;
|
||||
Tile tileMap[MAP_HEIGHT][MAP_WIDTH];
|
||||
BackgroundTile backgroundTileMap[MAP_HEIGHT][MAP_WIDTH];
|
||||
AudioData audioData;
|
||||
TileArray neededUpdates;
|
||||
EntityArray entities;
|
||||
Node openList[MAX_OPEN_NODES];
|
||||
int openCount;
|
||||
} GameState;
|
||||
GameState gameState;
|
||||
|
||||
int loadGameState(char *filename, Player *plr) {
|
||||
fflush(stdout);
|
||||
FILE *gameSave = fopen(filename, "rb");
|
||||
if (gameSave) {
|
||||
fseek(gameSave, 0L, SEEK_END);
|
||||
long sz = ftell(gameSave);
|
||||
if (sz != sizeof(gameState)) {
|
||||
return 1;
|
||||
}
|
||||
rewind(gameSave);
|
||||
fread(&gameState, sizeof(gameState), 1, gameSave);
|
||||
fclose(gameSave);
|
||||
memcpy(plr, &gameState.player, sizeof(gameState.player));
|
||||
memcpy(tileMap, gameState.tileMap, sizeof(tileMap));
|
||||
memcpy(backgroundMap, gameState.backgroundTileMap, sizeof(backgroundMap));
|
||||
SDL_Rect *tmp = audioData.playerRect;
|
||||
memcpy(&audioData, &gameState.audioData, sizeof(gameState.audioData));
|
||||
audioData.playerRect = tmp;
|
||||
audioData.totalSamples = 0;
|
||||
memcpy(&neededUpdates, &gameState.neededUpdates, sizeof(gameState.neededUpdates));
|
||||
memcpy(&entities, &gameState.entities, sizeof(gameState.entities));
|
||||
openCount = gameState.openCount;
|
||||
memcpy(&openList, &gameState.openList, sizeof(gameState.openList));
|
||||
plr->cursor.targetTile = NULL;
|
||||
plr->cursor.prevTargetTile = NULL;
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
void saveGameState(char *filename, Player *plr) {
|
||||
memcpy(&gameState.player, plr, sizeof(gameState.player));
|
||||
memcpy(gameState.tileMap, tileMap, sizeof(gameState.tileMap));
|
||||
memcpy(gameState.backgroundTileMap, backgroundMap, sizeof(gameState.backgroundTileMap));
|
||||
memcpy(&gameState.audioData, &audioData, sizeof(gameState.audioData));
|
||||
memcpy(&gameState.neededUpdates, &neededUpdates, sizeof(neededUpdates));
|
||||
memcpy(&gameState.entities, &entities, sizeof(entities));
|
||||
memcpy(&gameState.openList, &openList, sizeof(openList));
|
||||
gameState.openCount = openCount;
|
||||
|
||||
FILE *gameSave = fopen(filename, "wb");
|
||||
if (!gameSave) {
|
||||
perror("Failed to open file for saving");
|
||||
return;
|
||||
}
|
||||
|
||||
fwrite(&gameState, sizeof(gameState), 1, gameSave);
|
||||
fclose(gameSave);
|
||||
}
|
||||
|
||||
Player player;
|
||||
#define GAME_NAME "FactoCraft"
|
||||
|
||||
//Screen dimension constants
|
||||
const int targetFPS = 60;
|
||||
@@ -85,8 +24,7 @@ const int delayNeeded = 1000 / targetFPS;
|
||||
#define smallestFont fonts[3]
|
||||
#define reallySmallestFont fonts[4]
|
||||
|
||||
char *autosaveName = "autosave.dat";
|
||||
|
||||
bool largeScreen = false;
|
||||
|
||||
unsigned long frames = 0;
|
||||
bool cursor = true;
|
||||
@@ -100,17 +38,9 @@ void msleep(unsigned int milliseconds) {
|
||||
|
||||
SDL_GLContext glContext;
|
||||
|
||||
void genInitMap();
|
||||
|
||||
int init() {
|
||||
//Initialize SDL
|
||||
|
||||
|
||||
screenRect.x = 0;
|
||||
screenRect.y = 0;
|
||||
screenRect.w = DISPLAY_WIDTH;
|
||||
screenRect.h = DISPLAY_HEIGHT;
|
||||
|
||||
srand(time(NULL));
|
||||
|
||||
memset(tileMap, 0, sizeof(tileMap));
|
||||
@@ -133,8 +63,13 @@ int init() {
|
||||
}
|
||||
|
||||
//Create window
|
||||
window = SDL_CreateWindow("Factory game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, DISPLAY_WIDTH,
|
||||
DISPLAY_HEIGHT, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
|
||||
window = SDL_CreateWindow(GAME_NAME, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, DISPLAY_WIDTH,
|
||||
DISPLAY_HEIGHT, SDL_WINDOW_SHOWN);
|
||||
|
||||
SDL_Surface *surf = IMG_Load("./assets/icon.png");
|
||||
|
||||
SDL_SetWindowIcon(window, surf);
|
||||
SDL_FreeSurface(surf);
|
||||
|
||||
if (window == NULL) {
|
||||
printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
|
||||
@@ -146,22 +81,6 @@ int init() {
|
||||
printf("Renderer could not be created SDL_Error: %s\n", SDL_GetError());
|
||||
return 1;
|
||||
}
|
||||
|
||||
initAtlas(mainRenderer);
|
||||
|
||||
loadBackgroundTiles(mainRenderer);
|
||||
loadTiles(mainRenderer);
|
||||
preSetupTiles();
|
||||
loadItems(mainRenderer);
|
||||
loadEntities(mainRenderer);
|
||||
setupTiles();
|
||||
|
||||
|
||||
// for (ItemType i = 0; i < ITEMREGISTRY_SIZE; i++) {
|
||||
// if (strlen(ItemRegistry[i].name)) {
|
||||
// printf("%d -> %s\n", i, ItemRegistry[i].name);
|
||||
// }
|
||||
// }
|
||||
// Create OpenGL context
|
||||
glContext = SDL_GL_CreateContext(window);
|
||||
if (!glContext) {
|
||||
@@ -172,14 +91,13 @@ int init() {
|
||||
// Use OpenGL context
|
||||
SDL_GL_MakeCurrent(window, glContext); // Make sure OpenGL context is current before any OpenGL rendering
|
||||
|
||||
audioData.playerRect = &player.rect;
|
||||
audioData.maxPanDistance = DISPLAY_WIDTH / 2;
|
||||
audioData.playerRect = &mainPlayer.rect;
|
||||
|
||||
SDL_AudioSpec spec = {0};
|
||||
spec.freq = SAMPLE_RATE;
|
||||
spec.format = AUDIO_F32;
|
||||
spec.channels = 2;
|
||||
spec.samples = 4096;
|
||||
spec.samples = 8192;
|
||||
spec.callback = audio_callback;
|
||||
spec.userdata = &audioData;
|
||||
|
||||
@@ -204,8 +122,9 @@ int init() {
|
||||
SDL_SetRenderDrawColor(mainRenderer, 0, 0, 0, 255);
|
||||
SDL_RenderClear(mainRenderer);
|
||||
|
||||
SDL_Rect viewport = {0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT};
|
||||
SDL_RenderSetViewport(mainRenderer, &viewport);
|
||||
//generateTestMap();
|
||||
|
||||
setTileView(30,15);
|
||||
|
||||
SDL_SetRenderDrawBlendMode(mainRenderer, SDL_BLENDMODE_BLEND);
|
||||
biggerFont = prepText(mainRenderer, 32, "assets/PublicPixel.ttf");
|
||||
@@ -213,65 +132,50 @@ int init() {
|
||||
smallerFont = prepText(mainRenderer, 12, "assets/PublicPixel.ttf");
|
||||
smallestFont = prepText(mainRenderer, 8, "assets/PublicPixel.ttf");
|
||||
reallySmallestFont = prepText(mainRenderer, 4, "assets/PublicPixel.ttf");
|
||||
SDL_RenderSetLogicalSize(mainRenderer, DISPLAY_WIDTH, DISPLAY_HEIGHT);
|
||||
|
||||
initPlayer(&player);
|
||||
|
||||
for (ItemType i = 0; i < 13; i++) {
|
||||
player.inventory.hotKeySlots[i] = i;
|
||||
}
|
||||
|
||||
initTiles();
|
||||
|
||||
//generateTestMap();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool lateInitDone = false;
|
||||
|
||||
void lateInit() {
|
||||
if (lateInitDone) {
|
||||
return;
|
||||
}
|
||||
lateInitDone = true;
|
||||
initAtlas(mainRenderer);
|
||||
|
||||
loadBackgroundTiles(mainRenderer);
|
||||
loadTiles(mainRenderer);
|
||||
preSetupTiles();
|
||||
loadItems(mainRenderer);
|
||||
loadEntities(mainRenderer);
|
||||
setupTiles();
|
||||
initTiles();
|
||||
|
||||
initPlayer(&mainPlayer);
|
||||
|
||||
for (ItemType i = 0; i < 13; i++) {
|
||||
mainPlayer.inventory.hotKeySlots[i] = i;
|
||||
}
|
||||
initWaveInfo(&waveInfo);
|
||||
}
|
||||
|
||||
|
||||
int render() {
|
||||
SDL_SetRenderDrawColor(mainRenderer, 32, 32, 32, 255);
|
||||
SDL_RenderClear(mainRenderer);
|
||||
|
||||
SDL_Rect rect2;
|
||||
rect2.x = 0;
|
||||
rect2.y = 0;
|
||||
rect2.w = ATLAS_SIZE;
|
||||
rect2.h = ATLAS_SIZE;
|
||||
|
||||
renderAllTiles(mainRenderer, player.rect);
|
||||
renderEntities(mainRenderer, player.rect);
|
||||
renderPlayer(&player);
|
||||
renderAllTiles(mainRenderer, mainPlayer.rect);
|
||||
renderEntities(mainRenderer, mainPlayer.rect);
|
||||
renderPlayer(&mainPlayer);
|
||||
|
||||
|
||||
if (renderAtlas == 0) {
|
||||
SDL_RenderCopy(mainRenderer, backgroundTexture, &screenRect, &screenRect);
|
||||
SDL_RenderCopy(mainRenderer, tilesTexture, &screenRect, &screenRect);
|
||||
SDL_RenderCopy(mainRenderer, itemsTexture, &screenRect, &screenRect);
|
||||
SDL_RenderCopy(mainRenderer, entityTexture, &screenRect, &screenRect);
|
||||
SDL_RenderCopy(mainRenderer, hudTexture, &screenRect, &screenRect);
|
||||
|
||||
} else {
|
||||
SDL_RenderCopy(mainRenderer, atlasTexture, &rect2, &rect2);
|
||||
unsigned int ix = ATLAS_SIZE;
|
||||
for (unsigned char i = 1; i < fontCount; i++) {
|
||||
SDL_Rect tmpRectFont = fonts[i].atlasRect;
|
||||
tmpRectFont.x += ix;
|
||||
SDL_RenderCopy(mainRenderer, fonts[i].atlas, &fonts[i].atlasRect, &tmpRectFont);
|
||||
ix += fonts[i].atlasRect.w;
|
||||
}
|
||||
SDL_Rect tmpRectFont = fonts[0].atlasRect;
|
||||
tmpRectFont.x += ATLAS_SIZE;
|
||||
tmpRectFont.y = fonts[1].atlasRect.h;
|
||||
SDL_RenderCopy(mainRenderer, fonts[0].atlas, &fonts[0].atlasRect, &tmpRectFont);
|
||||
}
|
||||
SDL_RenderCopy(mainRenderer, backgroundTexture, &screenRect, &screenRect);
|
||||
SDL_RenderCopy(mainRenderer, tilesTexture, &screenRect, &screenRect);
|
||||
SDL_RenderCopy(mainRenderer, itemsTexture, &screenRect, &screenRect);
|
||||
SDL_RenderCopy(mainRenderer, entityTexture, &screenRect, &screenRect);
|
||||
SDL_RenderCopy(mainRenderer, hudTexture, &screenRect, &screenRect);
|
||||
|
||||
|
||||
SDL_RenderPresent(mainRenderer);
|
||||
frames++;
|
||||
if (!(frames % 60)) {
|
||||
cursor = !cursor;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -283,186 +187,189 @@ int processEvent(SDL_Event e) {
|
||||
int newHeight = e.window.data2;
|
||||
|
||||
// Adjust the viewport to match the new window size;
|
||||
SDL_Rect viewport = {0, 0, newWidth, newHeight};
|
||||
SDL_RenderSetViewport(mainRenderer, &viewport);
|
||||
} else if (e.type == SDL_KEYDOWN) {
|
||||
SDL_KeyCode keySym = e.key.keysym.sym;
|
||||
SDL_Scancode scanCode = e.key.keysym.scancode;
|
||||
SDL_Keymod keyMod = e.key.keysym.mod;
|
||||
cursor = true;
|
||||
switch (keySym) {
|
||||
case SDLK_p:
|
||||
speed = speed == 0 ? 0.004f : 0;
|
||||
break;
|
||||
case SDLK_r:
|
||||
if (player.inventory.activeSlotIndex == 0 && player.cursor.canReach &&
|
||||
player.cursor.targetTile->type != TYPE_AIR) {
|
||||
player.cursor.direction = player.cursor.targetTile->direction;
|
||||
}
|
||||
player.cursor.direction = (player.cursor.direction + 2) % ORIENT_DIRECTION_COUNT;
|
||||
if (player.inventory.activeSlotIndex == 0 && player.cursor.canReach) {
|
||||
player.cursor.targetTile->direction = player.cursor.direction;
|
||||
}
|
||||
break;
|
||||
case SDLK_u:
|
||||
laneTarget = !laneTarget;
|
||||
break;
|
||||
case SDLK_F3:
|
||||
debugMode = !debugMode;
|
||||
break;
|
||||
case SDLK_F10:
|
||||
renderAtlas = !renderAtlas;
|
||||
break;
|
||||
case SDLK_F11:
|
||||
printf("Enemy is at tile X:%d, Y:%d\n", entities.entities[0].tileRect.x,
|
||||
entities.entities[0].tileRect.y);
|
||||
break;
|
||||
|
||||
case SDLK_F2:
|
||||
Entity entTest;
|
||||
memset(&entTest, 0, sizeof(Entity));
|
||||
entTest.tileRect = player.tileRect;
|
||||
entTest.renderRect.w = TILE_SIZE;
|
||||
entTest.renderRect.h = TILE_SIZE;
|
||||
entTest.target.x = -1;
|
||||
entTest.target.y = -1;
|
||||
entTest.health = 100;
|
||||
entTest.type = GHOST;
|
||||
entTest.health = 100;
|
||||
add_entity(&entities, entTest);
|
||||
break;
|
||||
|
||||
case SDLK_F4:
|
||||
Tile *tile = &tileMap[playerTileY][playerTileX];
|
||||
break;
|
||||
case SDLK_LALT:
|
||||
itemViewing = !itemViewing;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
if (newWidth > 1900 && newHeight > 1000) {
|
||||
largeScreen = true;
|
||||
} else {
|
||||
largeScreen = false;
|
||||
}
|
||||
} else if (e.type == SDL_MOUSEWHEEL) {
|
||||
int dAmount = 0;
|
||||
if (e.wheel.y > 0) {
|
||||
dAmount = 1;
|
||||
} else if (e.wheel.y < 0) {
|
||||
dAmount = -1;
|
||||
setTileView(largeScreen ? 60 : 30, largeScreen ? 31 : 15);
|
||||
|
||||
}
|
||||
// }
|
||||
|
||||
if (e.type == SDL_KEYUP) {
|
||||
if (e.key.keysym.mod & KMOD_ALT && e.key.keysym.scancode == SDL_SCANCODE_RETURN) {
|
||||
largeScreen = !largeScreen;
|
||||
setTileView(largeScreen ? 60 : 30, largeScreen ? 31 : 15);
|
||||
}
|
||||
const Uint8 *keyboardState = SDL_GetKeyboardState(NULL);
|
||||
if (keyboardState[SDL_SCANCODE_LCTRL] || keyboardState[SDL_SCANCODE_RCTRL]) {
|
||||
}
|
||||
|
||||
switch (screenType) {
|
||||
case SCREEN_GAME:
|
||||
if (e.type == SDL_KEYDOWN) {
|
||||
SDL_KeyCode keySym = e.key.keysym.sym;
|
||||
SDL_Scancode scanCode = e.key.keysym.scancode;
|
||||
SDL_Keymod keyMod = e.key.keysym.mod;
|
||||
cursor = true;
|
||||
switch (keySym) {
|
||||
case SDLK_p:
|
||||
speed = speed == 0 ? 0.004f : 0;
|
||||
break;
|
||||
case SDLK_r:
|
||||
if (mainPlayer.inventory.activeSlotIndex == 0 && mainPlayer.cursor.canReach &&
|
||||
mainPlayer.cursor.targetTile->type != TYPE_AIR) {
|
||||
mainPlayer.cursor.direction = mainPlayer.cursor.targetTile->direction;
|
||||
}
|
||||
mainPlayer.cursor.direction = (mainPlayer.cursor.direction + 2) % ORIENT_DIRECTION_COUNT;
|
||||
if (mainPlayer.inventory.activeSlotIndex == 0 && mainPlayer.cursor.canReach) {
|
||||
mainPlayer.cursor.targetTile->direction = mainPlayer.cursor.direction;
|
||||
}
|
||||
break;
|
||||
case SDLK_u:
|
||||
laneTarget = !laneTarget;
|
||||
break;
|
||||
case SDLK_F3:
|
||||
debugMode = !debugMode;
|
||||
break;
|
||||
case SDLK_F11:
|
||||
printf("Enemy is at tile X:%d, Y:%d\n", entities.entities[0].tileRect.x,
|
||||
entities.entities[0].tileRect.y);
|
||||
break;
|
||||
|
||||
case SDLK_F4:
|
||||
Tile *tile = &tileMap[playerTileY][playerTileX];
|
||||
break;
|
||||
case SDLK_LALT:
|
||||
itemViewing = !itemViewing;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
} else if (e.type == SDL_MOUSEWHEEL) {
|
||||
int dAmount = 0;
|
||||
if (e.wheel.y > 0) {
|
||||
dAmount = 1;
|
||||
} else if (e.wheel.y < 0) {
|
||||
dAmount = -1;
|
||||
}
|
||||
const Uint8 *keyboardState = SDL_GetKeyboardState(NULL);
|
||||
if (keyboardState[SDL_SCANCODE_LCTRL] || keyboardState[SDL_SCANCODE_RCTRL]) {
|
||||
// currentScale += dAmount / 10.0f;
|
||||
// if (currentScale > 4) {
|
||||
// currentScale = 4;
|
||||
// } else if (currentScale < 0.5f) {
|
||||
// currentScale = 0.5f;
|
||||
// }
|
||||
//setZoom(currentScale);
|
||||
//setZoom(currentScale);
|
||||
|
||||
|
||||
} else {
|
||||
moveActivePlayerSlot(&player, dAmount == -1,
|
||||
!(keyboardState[SDL_SCANCODE_LSHIFT] || keyboardState[SDL_SCANCODE_RSHIFT]));
|
||||
}
|
||||
} else {
|
||||
moveActivePlayerSlot(&mainPlayer, dAmount == -1,
|
||||
!(keyboardState[SDL_SCANCODE_LSHIFT] || keyboardState[SDL_SCANCODE_RSHIFT]));
|
||||
}
|
||||
}
|
||||
break;
|
||||
case SCREEN_MENU:
|
||||
break;
|
||||
case SCREEN_FONTS:
|
||||
break;
|
||||
case SCREEN_ATLAS:
|
||||
break;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
void processMousePosition() {
|
||||
SDL_Rect viewport;
|
||||
SDL_RenderGetViewport(mainRenderer, &viewport);
|
||||
mainPlayer.cursor.tileX = (mainPlayer.cursor.windowX + mainPlayer.rect.x) / TILE_SIZE - (DISPLAY_WIDTH / TILE_SIZE / 2);
|
||||
mainPlayer.cursor.tileY = (mainPlayer.cursor.windowY + mainPlayer.rect.y) / TILE_SIZE - (DISPLAY_HEIGHT / TILE_SIZE / 2);
|
||||
|
||||
uint32_t mouseButtons = SDL_GetMouseState(&player.cursor.windowX, &player.cursor.windowY);
|
||||
if (mouseButtons & SDL_BUTTON_LMASK) {
|
||||
if (player.cursor.canReach && player.cursor.targetTile->type == TYPE_AIR &&
|
||||
player.inventory.activeSlotIndex < tileTypeIndex) {
|
||||
if (player.inventory.slotCounts[player.inventory.activeSlotIndex] > 0) {
|
||||
player.inventory.slotCounts[player.inventory.activeSlotIndex]--;
|
||||
player.cursor.targetTile->type = player.inventory.activeSlotIndex;
|
||||
player.cursor.targetTile->health = TileRegistry[player.inventory.activeSlotIndex].maxHealth;
|
||||
player.cursor.targetTile->rect.x = player.cursor.tileX;
|
||||
player.cursor.targetTile->rect.y = player.cursor.tileY;
|
||||
if (TileRegistry[player.inventory.activeSlotIndex].needsTicks) {
|
||||
player.cursor.targetTile->neededUpdateIndex = add_tile(&neededUpdates,
|
||||
player.cursor.targetTile->rect);
|
||||
if (mainPlayer.cursor.tileX < 0) {
|
||||
mainPlayer.cursor.tileX = 0;
|
||||
}
|
||||
if (mainPlayer.cursor.tileY < 0) {
|
||||
mainPlayer.cursor.tileY = 0;
|
||||
}
|
||||
if (mainPlayer.cursor.tileX >= MAP_WIDTH) {
|
||||
mainPlayer.cursor.tileX = MAP_WIDTH - 1;
|
||||
}
|
||||
if (mainPlayer.cursor.tileY >= MAP_HEIGHT) {
|
||||
mainPlayer.cursor.tileY = MAP_HEIGHT - 1;
|
||||
}
|
||||
|
||||
mainPlayer.cursor.prevTargetTile = mainPlayer.cursor.targetTile;
|
||||
mainPlayer.cursor.targetTile = &tileMap[mainPlayer.cursor.tileY][mainPlayer.cursor.tileX];
|
||||
mainPlayer.cursor.targetTileRect.x = mainPlayer.cursor.tileX * TILE_SIZE;
|
||||
mainPlayer.cursor.targetTileRect.y = mainPlayer.cursor.tileY * TILE_SIZE;
|
||||
mainPlayer.cursor.tileDiffX = mainPlayer.cursor.tileX - playerTileX;
|
||||
mainPlayer.cursor.tileDiffY = mainPlayer.cursor.tileY - playerTileY;
|
||||
mainPlayer.cursor.tileDiff = floorf(sqrtf(powf(mainPlayer.cursor.tileDiffX, 2) + powf(mainPlayer.cursor.tileDiffY, 2)));
|
||||
mainPlayer.cursor.canReach = mainPlayer.cursor.tileDiff <= playerReach;
|
||||
adjustRect(&mainPlayer.cursor.targetTileRect, mainPlayer.rect);
|
||||
|
||||
if (mainPlayer.mouseButtons & SDL_BUTTON_LMASK) {
|
||||
if (mainPlayer.cursor.canReach && mainPlayer.cursor.targetTile->type == TYPE_AIR &&
|
||||
mainPlayer.inventory.activeSlotIndex < tileTypeIndex) {
|
||||
if (mainPlayer.inventory.slotCounts[mainPlayer.inventory.activeSlotIndex] > 0) {
|
||||
mainPlayer.inventory.slotCounts[mainPlayer.inventory.activeSlotIndex]--;
|
||||
mainPlayer.cursor.targetTile->type = mainPlayer.inventory.activeSlotIndex;
|
||||
mainPlayer.cursor.targetTile->health = TileRegistry[mainPlayer.inventory.activeSlotIndex].maxHealth;
|
||||
mainPlayer.cursor.targetTile->rect.x = mainPlayer.cursor.tileX;
|
||||
mainPlayer.cursor.targetTile->rect.y = mainPlayer.cursor.tileY;
|
||||
if (TileRegistry[mainPlayer.inventory.activeSlotIndex].needsTicks) {
|
||||
mainPlayer.cursor.targetTile->neededUpdateIndex = add_tile(&neededUpdates,
|
||||
mainPlayer.cursor.targetTile->rect);
|
||||
}
|
||||
player.cursor.targetTile->direction = player.cursor.direction;
|
||||
mainPlayer.cursor.targetTile->direction = mainPlayer.cursor.direction;
|
||||
}
|
||||
} else if (player.cursor.targetTile->type == player.inventory.activeSlotIndex) {
|
||||
player.cursor.targetTile->direction = player.cursor.direction;
|
||||
} else if (mainPlayer.cursor.targetTile->type == mainPlayer.inventory.activeSlotIndex) {
|
||||
mainPlayer.cursor.targetTile->direction = mainPlayer.cursor.direction;
|
||||
}
|
||||
}
|
||||
if (player.cursor.canReach && mouseButtons & SDL_BUTTON_RMASK) {
|
||||
int tileIndex = player.cursor.targetTile->type;
|
||||
if (mainPlayer.cursor.canReach && mainPlayer.mouseButtons & SDL_BUTTON_RMASK) {
|
||||
int tileIndex = mainPlayer.cursor.targetTile->type;
|
||||
uint16_t targetBreakTime = TileRegistry[tileIndex].breakTime;
|
||||
if (targetBreakTime) {
|
||||
if (player.cursor.breakingProgress >= targetBreakTime) {
|
||||
if (player.cursor.targetTile->type < tileTypeIndex) {
|
||||
player.inventory.slotCounts[player.cursor.targetTile->type]++;
|
||||
if (mainPlayer.cursor.breakingProgress >= targetBreakTime) {
|
||||
if (mainPlayer.cursor.targetTile->type < tileTypeIndex) {
|
||||
mainPlayer.inventory.slotCounts[mainPlayer.cursor.targetTile->type]++;
|
||||
}
|
||||
for (int lane = 0; lane < 2; lane++) {
|
||||
if (player.cursor.targetTile->items[lane].type != 0) {
|
||||
int itemType = player.cursor.targetTile->items[lane].type;
|
||||
if (mainPlayer.cursor.targetTile->items[lane].type != 0) {
|
||||
int itemType = mainPlayer.cursor.targetTile->items[lane].type;
|
||||
if (itemType < itemRegistryIndex) {
|
||||
player.inventory.slotCounts[itemType]++;
|
||||
mainPlayer.inventory.slotCounts[itemType]++;
|
||||
}
|
||||
player.cursor.targetTile->items[lane].type = 0;
|
||||
mainPlayer.cursor.targetTile->items[lane].type = 0;
|
||||
}
|
||||
}
|
||||
audioData.synthVoices[player.cursor.targetTile->audioCh].volume = 0;
|
||||
int neededIndex = player.cursor.targetTile->neededUpdateIndex;
|
||||
if (TileRegistry[player.cursor.targetTile->type].needsTicks &&
|
||||
neededUpdates.tiles[neededIndex].x == player.cursor.targetTile->rect.x &&
|
||||
neededUpdates.tiles[neededIndex].y == player.cursor.targetTile->rect.y) {
|
||||
audioData.synthVoices[mainPlayer.cursor.targetTile->audioCh].volume = 0;
|
||||
int neededIndex = mainPlayer.cursor.targetTile->neededUpdateIndex;
|
||||
if (TileRegistry[mainPlayer.cursor.targetTile->type].needsTicks &&
|
||||
neededUpdates.tiles[neededIndex].x == mainPlayer.cursor.targetTile->rect.x &&
|
||||
neededUpdates.tiles[neededIndex].y == mainPlayer.cursor.targetTile->rect.y) {
|
||||
remove_tile(&neededUpdates, neededIndex);
|
||||
}
|
||||
player.cursor.targetTile->type = TYPE_AIR;
|
||||
player.cursor.breakingProgress = 0;
|
||||
mainPlayer.cursor.targetTile->type = TYPE_AIR;
|
||||
mainPlayer.cursor.breakingProgress = 0;
|
||||
} else {
|
||||
player.cursor.breakingProgress++;
|
||||
mainPlayer.cursor.breakingProgress++;
|
||||
}
|
||||
//printf("Player breaking %d\n", player.cursor.breakingProgress);
|
||||
//printf("Player breaking %d\n", mainPlayer.cursor.breakingProgress);
|
||||
}
|
||||
|
||||
} else {
|
||||
player.cursor.breakingProgress = 0;
|
||||
mainPlayer.cursor.breakingProgress = 0;
|
||||
}
|
||||
if (player.cursor.targetTile != player.cursor.prevTargetTile) {
|
||||
player.cursor.breakingProgress = 0;
|
||||
if (mainPlayer.cursor.targetTile != mainPlayer.cursor.prevTargetTile) {
|
||||
mainPlayer.cursor.breakingProgress = 0;
|
||||
}
|
||||
if (mouseButtons & SDL_BUTTON_MMASK) {
|
||||
if (player.cursor.targetTile->type > 0) {
|
||||
setActivePlayerSlot(&player, player.cursor.targetTile->type);
|
||||
if (mainPlayer.mouseButtons & SDL_BUTTON_MMASK) {
|
||||
if (mainPlayer.cursor.targetTile->type > 0) {
|
||||
setActivePlayerSlot(&mainPlayer, mainPlayer.cursor.targetTile->type);
|
||||
}
|
||||
|
||||
}
|
||||
// Translate mouseRect coordinates to viewport space
|
||||
|
||||
player.cursor.windowX = ((player.cursor.windowX - viewport.x) * DISPLAY_WIDTH) / viewport.w;
|
||||
player.cursor.windowY = (player.cursor.windowY - viewport.y) * DISPLAY_HEIGHT / viewport.h;
|
||||
player.cursor.tileX = (player.cursor.windowX + player.rect.x) / TILE_SIZE - (DISPLAY_WIDTH / TILE_SIZE / 2);
|
||||
player.cursor.tileY = (player.cursor.windowY + player.rect.y) / TILE_SIZE - (DISPLAY_HEIGHT / TILE_SIZE / 2);
|
||||
if (player.cursor.tileX < 0) {
|
||||
player.cursor.tileX = 0;
|
||||
}
|
||||
if (player.cursor.tileY < 0) {
|
||||
player.cursor.tileY = 0;
|
||||
}
|
||||
if (player.cursor.tileX >= MAP_WIDTH) {
|
||||
player.cursor.tileX = MAP_WIDTH - 1;
|
||||
}
|
||||
if (player.cursor.tileY >= MAP_HEIGHT) {
|
||||
player.cursor.tileY = MAP_HEIGHT - 1;
|
||||
}
|
||||
player.cursor.prevTargetTile = player.cursor.targetTile;
|
||||
player.cursor.targetTile = &tileMap[player.cursor.tileY][player.cursor.tileX];
|
||||
|
||||
player.cursor.targetTileRect.x = player.cursor.tileX * TILE_SIZE;
|
||||
player.cursor.targetTileRect.y = player.cursor.tileY * TILE_SIZE;
|
||||
player.cursor.tileDiffX = player.cursor.tileX - playerTileX;
|
||||
player.cursor.tileDiffY = player.cursor.tileY - playerTileY;
|
||||
player.cursor.tileDiff = floorf(sqrtf(powf(player.cursor.tileDiffX, 2) + powf(player.cursor.tileDiffY, 2)));
|
||||
player.cursor.canReach = player.cursor.tileDiff <= playerReach;
|
||||
adjustRect(&player.cursor.targetTileRect, player.rect);
|
||||
}
|
||||
|
||||
void processKeyboardHeld() {
|
||||
@@ -478,49 +385,49 @@ void processKeyboardHeld() {
|
||||
}
|
||||
|
||||
if (keyboardState[SDL_SCANCODE_F8]) {
|
||||
if (player.cursor.targetTile->health) {
|
||||
player.cursor.targetTile->health--;
|
||||
if (mainPlayer.cursor.targetTile->health) {
|
||||
mainPlayer.cursor.targetTile->health--;
|
||||
}
|
||||
}
|
||||
|
||||
if (player.cursor.breakingProgress == 0) {
|
||||
SDL_Rect newRect = player.rect;
|
||||
if (mainPlayer.cursor.breakingProgress == 0) {
|
||||
SDL_Rect newRect = mainPlayer.rect;
|
||||
|
||||
if (keyboardState[SDL_SCANCODE_W]) {
|
||||
newRect.y -= cameraSpeed;
|
||||
if (newRect.y >= 0 && canMoveWithRadius(newRect)) {
|
||||
player.rect = newRect;
|
||||
mainPlayer.rect = newRect;
|
||||
}
|
||||
}
|
||||
|
||||
if (keyboardState[SDL_SCANCODE_S]) {
|
||||
newRect = player.rect;
|
||||
newRect = mainPlayer.rect;
|
||||
newRect.y += cameraSpeed;
|
||||
if (newRect.y + newRect.h <= MAP_HEIGHT * TILE_SIZE && canMoveWithRadius(newRect)) {
|
||||
player.rect = newRect;
|
||||
mainPlayer.rect = newRect;
|
||||
}
|
||||
}
|
||||
|
||||
if (keyboardState[SDL_SCANCODE_A]) {
|
||||
newRect = player.rect;
|
||||
newRect = mainPlayer.rect;
|
||||
newRect.x -= cameraSpeed;
|
||||
if (newRect.x >= 0 && canMoveWithRadius(newRect)) {
|
||||
player.rect = newRect;
|
||||
mainPlayer.rect = newRect;
|
||||
}
|
||||
}
|
||||
|
||||
if (keyboardState[SDL_SCANCODE_D]) {
|
||||
newRect = player.rect;
|
||||
newRect = mainPlayer.rect;
|
||||
newRect.x += cameraSpeed;
|
||||
if (newRect.x + newRect.w <= MAP_WIDTH * TILE_SIZE && canMoveWithRadius(newRect)) {
|
||||
player.rect = newRect;
|
||||
mainPlayer.rect = newRect;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Update tileRect only after actual movement
|
||||
player.tileRect.x = player.rect.x / TILE_SIZE;
|
||||
player.tileRect.y = player.rect.y / TILE_SIZE;
|
||||
mainPlayer.tileRect.x = mainPlayer.rect.x / TILE_SIZE;
|
||||
mainPlayer.tileRect.y = mainPlayer.rect.y / TILE_SIZE;
|
||||
}
|
||||
|
||||
|
||||
@@ -544,7 +451,7 @@ void processKeyboardHeld() {
|
||||
for (uint8_t lane = 0; lane < 2; lane++) {
|
||||
ItemOnBelt *item = &t->items[lane];
|
||||
if (item->type != 0) {
|
||||
player.inventory.slotCounts[item->type]++;
|
||||
mainPlayer.inventory.slotCounts[item->type]++;
|
||||
item->type = 0;
|
||||
}
|
||||
}
|
||||
@@ -554,15 +461,15 @@ void processKeyboardHeld() {
|
||||
}
|
||||
|
||||
if (keyboardState[SDL_SCANCODE_Q]) {
|
||||
if (player.cursor.targetTile->type > 0 && player.inventory.activeSlotIndex == 0) {
|
||||
setActivePlayerSlot(&player, player.cursor.targetTile->type);
|
||||
if (mainPlayer.cursor.targetTile->type > 0 && mainPlayer.inventory.activeSlotIndex == 0) {
|
||||
setActivePlayerSlot(&mainPlayer, mainPlayer.cursor.targetTile->type);
|
||||
} else {
|
||||
setActivePlayerSlot(&player, 0);
|
||||
setActivePlayerSlot(&mainPlayer, 0);
|
||||
}
|
||||
}
|
||||
if (keyboardState[SDL_SCANCODE_E]) {
|
||||
if (player.cursor.targetTile->health < TileRegistry[player.cursor.targetTile->type].maxHealth) {
|
||||
player.cursor.targetTile->health++;
|
||||
if (mainPlayer.cursor.targetTile->health < TileRegistry[mainPlayer.cursor.targetTile->type].maxHealth) {
|
||||
mainPlayer.cursor.targetTile->health++;
|
||||
}
|
||||
for (int x = playerTileX - 2; x < playerTileX + 2; x++) {
|
||||
if (x < 0) {
|
||||
@@ -586,15 +493,15 @@ void processKeyboardHeld() {
|
||||
}
|
||||
}
|
||||
if (keyboardState[SDL_SCANCODE_F9]) {
|
||||
player.inventory.slotCounts[player.inventory.activeSlotIndex]++;
|
||||
mainPlayer.inventory.slotCounts[mainPlayer.inventory.activeSlotIndex]++;
|
||||
}
|
||||
if (keyboardState[SDL_SCANCODE_Y]) {
|
||||
if (player.cursor.canReach && player.cursor.targetTile->type == TYPE_BELT &&
|
||||
player.inventory.slotCounts[player.inventory.activeSlotIndex] > 0) {
|
||||
if (mainPlayer.cursor.canReach && mainPlayer.cursor.targetTile->type == TYPE_BELT &&
|
||||
mainPlayer.inventory.slotCounts[mainPlayer.inventory.activeSlotIndex] > 0) {
|
||||
for (uint8_t lane = 0; lane < 1; lane++) {
|
||||
if (player.cursor.targetTile->items[lane].type == 0) {
|
||||
putItem(player.cursor.tileX, player.cursor.tileY, player.inventory.activeSlotIndex, lane);
|
||||
player.inventory.slotCounts[player.inventory.activeSlotIndex]--;
|
||||
if (mainPlayer.cursor.targetTile->items[lane].type == 0) {
|
||||
putItem(mainPlayer.cursor.tileX, mainPlayer.cursor.tileY, mainPlayer.inventory.activeSlotIndex, lane);
|
||||
mainPlayer.inventory.slotCounts[mainPlayer.inventory.activeSlotIndex]--;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -628,8 +535,19 @@ void processKeyboardHeld() {
|
||||
} else if (keyboardState[SDL_SCANCODE_EQUALS]) {
|
||||
slot = 13;
|
||||
}
|
||||
if (slot > 0 && slot < sizeof(player.inventory.hotKeySlots) / sizeof(player.inventory.hotKeySlots[0])) {
|
||||
setActivePlayerSlot(&player, player.inventory.hotKeySlots[slot]);
|
||||
if (slot > 0 && slot < sizeof(mainPlayer.inventory.hotKeySlots) / sizeof(mainPlayer.inventory.hotKeySlots[0])) {
|
||||
setActivePlayerSlot(&mainPlayer, mainPlayer.inventory.hotKeySlots[slot]);
|
||||
}
|
||||
}
|
||||
|
||||
void basicUtil() {
|
||||
mainPlayer.mouseButtons = SDL_GetMouseState(&mainPlayer.cursor.windowX, &mainPlayer.cursor.windowY);
|
||||
|
||||
mainPlayer.cursor.windowX = ((mainPlayer.cursor.windowX - viewport.x) * DISPLAY_WIDTH) / viewport.w;
|
||||
mainPlayer.cursor.windowY = (mainPlayer.cursor.windowY - viewport.y) * DISPLAY_HEIGHT / viewport.h;
|
||||
SDL_Event e;
|
||||
while (SDL_PollEvent(&e)) {
|
||||
running = processEvent(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -638,63 +556,98 @@ int main(__attribute__((unused)) int argc, __attribute__((unused)) char *args[])
|
||||
if (status) {
|
||||
return status;
|
||||
}
|
||||
if (loadGameState(autosaveName, &player)) {
|
||||
genInitMap();
|
||||
}
|
||||
|
||||
// audioData.synthVoices[0].frequency = 1000;
|
||||
// audioData.synthVoices[0].phase = 0;
|
||||
// audioData.synthVoices[0].sourceRect.w = TILE_SIZE;
|
||||
// audioData.synthVoices[0].sourceRect.h = TILE_SIZE;
|
||||
// audioData.synthVoices[0].sourceRect.x = 100 * TILE_SIZE;
|
||||
// audioData.synthVoices[0].sourceRect.y = 100 * TILE_SIZE;
|
||||
// audioData.synthVoices[0].volume = 255;
|
||||
// audioData.synthVoices[0].waveform = WAVE_SINE;
|
||||
|
||||
//Hack to get window to stay up
|
||||
SDL_Event e;
|
||||
Uint64 start;
|
||||
Uint64 end;
|
||||
while (running) {
|
||||
start = SDL_GetTicks64();
|
||||
|
||||
processMousePosition();
|
||||
processKeyboardHeld();
|
||||
basicUtil();
|
||||
SDL_SetRenderDrawColor(mainRenderer, 32, 32, 32, 255);
|
||||
SDL_RenderClear(mainRenderer);
|
||||
switch (screenType) {
|
||||
|
||||
while (SDL_PollEvent(&e)) {
|
||||
running = processEvent(e);
|
||||
}
|
||||
case SCREEN_MENU:
|
||||
renderText(mainRenderer, fonts[0], GAME_NAME,
|
||||
DISPLAY_WIDTH / 2 - (strlen(GAME_NAME) * fonts[0].size / 2),
|
||||
DISPLAY_HEIGHT / 8 - fonts[0].size);
|
||||
|
||||
if (animationStep % 60 == 0) {
|
||||
for (int i = 0; i < entities.activeCount; i++) {
|
||||
int x = player.tileRect.x;
|
||||
int y = player.tileRect.y;
|
||||
x += (rand() % 10) - 5;
|
||||
y += (rand() % 10) - 5;
|
||||
if (x < 0) {
|
||||
x = 0;
|
||||
break;
|
||||
case SCREEN_FONTS:
|
||||
unsigned int ix = 0;
|
||||
for (unsigned char i = 0; i < fontCount; i++) {
|
||||
SDL_Rect tmpRectFont = fonts[i].atlasRect;
|
||||
tmpRectFont.x += ix;
|
||||
SDL_Rect tmpRectFontOut = tmpRectFont;
|
||||
if (DISPLAY_HEIGHT < 1000) {
|
||||
tmpRectFontOut.w /= 2;
|
||||
tmpRectFontOut.h /= 2;
|
||||
}
|
||||
SDL_RenderCopy(mainRenderer, fonts[i].atlas, &fonts[i].atlasRect, &tmpRectFontOut);
|
||||
ix += fonts[i].atlasRect.w / 2;
|
||||
}
|
||||
if (y < 0) {
|
||||
y = 0;
|
||||
break;
|
||||
case SCREEN_ATLAS:
|
||||
lateInit();
|
||||
SDL_Rect rect2;
|
||||
rect2.x = 0;
|
||||
rect2.y = 0;
|
||||
rect2.w = 2048;
|
||||
rect2.h = 1024;
|
||||
SDL_Rect tmpRectFontOut = rect2;
|
||||
if (!largeScreen) {
|
||||
tmpRectFontOut.w /= 2;
|
||||
tmpRectFontOut.h /= 2;
|
||||
}
|
||||
if (x >= MAP_WIDTH) {
|
||||
x = MAP_WIDTH - 1;
|
||||
SDL_RenderCopy(mainRenderer, atlasTexture, &rect2, &tmpRectFontOut);
|
||||
rect2.x = 0;
|
||||
rect2.y = 1024;
|
||||
rect2.w = 2048;
|
||||
rect2.h = 1024;
|
||||
tmpRectFontOut = rect2;
|
||||
tmpRectFontOut.x = 1024;
|
||||
if (!largeScreen) {
|
||||
tmpRectFontOut.x /= 2;
|
||||
tmpRectFontOut.w /= 2;
|
||||
tmpRectFontOut.h /= 2;
|
||||
}
|
||||
if (y >= MAP_HEIGHT) {
|
||||
y = MAP_HEIGHT - 1;
|
||||
SDL_RenderCopy(mainRenderer, atlasTexture, &rect2, &tmpRectFontOut);
|
||||
break;
|
||||
case SCREEN_CREDITS:
|
||||
renderText(mainRenderer, fonts[0], GAME_NAME,
|
||||
DISPLAY_WIDTH / 2 - (strlen(GAME_NAME) * fonts[0].size / 2),
|
||||
DISPLAY_HEIGHT / 8 - fonts[0].size);
|
||||
char *creditsString = "Code by BRNSystems\nArt by Simi11\nMalo sa to volat Minidustry\nale Simi11 mal iny nazor\n(a ikonu)\nMade for Mr. Kovacev";
|
||||
renderText(mainRenderer, fonts[0], creditsString,
|
||||
0,
|
||||
DISPLAY_HEIGHT / 4 - fonts[0].size);
|
||||
break;
|
||||
|
||||
case SCREEN_GAME:
|
||||
lateInit();
|
||||
processMousePosition();
|
||||
processKeyboardHeld();
|
||||
|
||||
|
||||
updateWaveLogic(&waveInfo);
|
||||
updateItems();
|
||||
updateEntities(&mainPlayer);
|
||||
updatePlayer(&mainPlayer);
|
||||
updateTiles();
|
||||
animationStep++;
|
||||
status = render();
|
||||
if (status) {
|
||||
return status;
|
||||
}
|
||||
entities.entities[i].target.x = x;
|
||||
entities.entities[i].target.y = y;
|
||||
}
|
||||
break;
|
||||
}
|
||||
updateItems();
|
||||
updateEntities(&player);
|
||||
updatePlayer(&player);
|
||||
updateTiles();
|
||||
animationStep++;
|
||||
status = render();
|
||||
if (status) {
|
||||
return status;
|
||||
renderButtons(mainRenderer, mainPlayer);
|
||||
|
||||
SDL_RenderPresent(mainRenderer);
|
||||
frames++;
|
||||
if (!(frames % 60)) {
|
||||
cursor = !cursor;
|
||||
}
|
||||
|
||||
end = SDL_GetTicks64();
|
||||
@@ -702,9 +655,10 @@ int main(__attribute__((unused)) int argc, __attribute__((unused)) char *args[])
|
||||
if (timeNeeded < delayNeeded) {
|
||||
SDL_Delay(delayNeeded - timeNeeded);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
saveGameState(autosaveName, &player);
|
||||
saveGameState(autosaveName, &mainPlayer);
|
||||
|
||||
for (uint8_t i = 0; i < fontCount; i++) {
|
||||
destroyFont(&fonts[i]);
|
||||
@@ -718,93 +672,3 @@ int main(__attribute__((unused)) int argc, __attribute__((unused)) char *args[])
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Main generator:
|
||||
void genInitMap() {
|
||||
const double terrainScale = 2;
|
||||
const double humidityScale = 1;
|
||||
const double oreScale = 2;
|
||||
const int terrainOct = 4;
|
||||
const int humidityOct = 4;
|
||||
const int oreOct = 4;
|
||||
|
||||
int seedX = rand();
|
||||
int seedY = rand();
|
||||
int seedN = rand();
|
||||
|
||||
// Min/max trackers
|
||||
double terrainMin = 1e9, terrainMax = -1e9;
|
||||
double humidityMin = 1e9, humidityMax = -1e9;
|
||||
double oreNrmMin = 1e9, oreNrmMax = -1e9;
|
||||
for (uint16_t y = 0; y < MAP_HEIGHT; y++) {
|
||||
for (uint16_t x = 0; x < MAP_WIDTH; x++) {
|
||||
double terrain = pnoise2d(x + 1000 + seedX,
|
||||
y + 1000 + seedY,
|
||||
terrainScale, terrainOct, seedN);
|
||||
double humidity = pnoise2d(x + seedX,
|
||||
y + seedY,
|
||||
humidityScale, humidityOct, seedN);
|
||||
double oreNrm = pnoise2d(x + 9999 + seedX,
|
||||
y + 1111 + seedY,
|
||||
oreScale, oreOct, seedN);
|
||||
|
||||
// Track min/max
|
||||
if (terrain < terrainMin) terrainMin = terrain;
|
||||
if (terrain > terrainMax) terrainMax = terrain;
|
||||
|
||||
if (humidity < humidityMin) humidityMin = humidity;
|
||||
if (humidity > humidityMax) humidityMax = humidity;
|
||||
|
||||
if (oreNrm < oreNrmMin) oreNrmMin = oreNrm;
|
||||
if (oreNrm > oreNrmMax) oreNrmMax = oreNrm;
|
||||
|
||||
// [Same as your original terrain generation logic...]
|
||||
BackgroundType baseType = BGType_COBBLE0;
|
||||
if (terrain < 0.30) {
|
||||
baseType = (humidity < 0.5) ? BGType_WATER_SHALLOW : BGType_WATER_DEEP;
|
||||
} else if (terrain < 0.35) {
|
||||
if (humidity < 0.3) baseType = BGType_SAND4;
|
||||
else if (humidity < 0.6) baseType = BGType_SAND2;
|
||||
else baseType = BGType_SAND7;
|
||||
} else if (terrain < 0.7) {
|
||||
double grassVal = (terrain - 0.35) / (0.70 - 0.35);
|
||||
int idx = (int) (grassVal * 3.0);
|
||||
if (idx >= 4) idx = 3;
|
||||
if (humidity > 0.6 && ((rand() & 0xFF) < 10)) {
|
||||
int flowerIdx = rand() % 4;
|
||||
baseType = (BackgroundType) (BGType_GRASS_FLOWER0 + flowerIdx);
|
||||
} else {
|
||||
baseType = (BackgroundType) (BGType_GRASS0 + idx);
|
||||
}
|
||||
} else if (terrain < 0.85) {
|
||||
int idx = rand() % 4;
|
||||
baseType = (BackgroundType) (BGType_COBBLE0 + idx);
|
||||
} else {
|
||||
int idx = rand() % 4;
|
||||
baseType = (BackgroundType) (BGType_TILES0 + idx);
|
||||
}
|
||||
|
||||
BackgroundType finalType = baseType;
|
||||
if (baseType != BGType_WATER_SHALLOW && baseType != BGType_WATER_DEEP) {
|
||||
if (oreNrm > 0.86) {
|
||||
double sub = (oreNrm - 0.86) / (1.0 - 0.86);
|
||||
if (sub < 0.25) finalType = BGType_IRON_ORE;
|
||||
else if (sub < 0.50) finalType = BGType_SILVER_ORE;
|
||||
else if (sub < 0.80) finalType = BGType_GOLD_ORE;
|
||||
else finalType = BGType_PLATINUM_ORE;
|
||||
}
|
||||
}
|
||||
|
||||
if (finalType > BGType_END) {
|
||||
finalType = BGType_COBBLE0;
|
||||
}
|
||||
backgroundMap[y][x].type = finalType;
|
||||
}
|
||||
}
|
||||
|
||||
// Debug printout
|
||||
printf("Terrain Noise: min = %f, max = %f\n", terrainMin, terrainMax);
|
||||
printf("Humidity Noise: min = %f, max = %f\n", humidityMin, humidityMax);
|
||||
printf("Ore Normalized: min = %f, max = %f\n", oreNrmMin, oreNrmMax);
|
||||
}
|
||||
|
||||
|
@@ -11,9 +11,11 @@
|
||||
|
||||
#define HEALTH_MARGIN 4
|
||||
|
||||
Player mainPlayer;
|
||||
|
||||
int playerSpeed = 2;
|
||||
|
||||
int playerReach = DISPLAY_MAP_HEIGHT / 2 - 2;
|
||||
int playerReach = 5;
|
||||
|
||||
SDL_Texture *entityTexture;
|
||||
SDL_Texture *hudTexture;
|
||||
@@ -112,8 +114,8 @@ void initPlayer(Player *plr) {
|
||||
plr->health = 64;
|
||||
plr->healthIdle = 0;
|
||||
|
||||
plr->rect.x = DISPLAY_WIDTH / 2;
|
||||
plr->rect.y = DISPLAY_HEIGHT / 2;
|
||||
plr->rect.x = 10 * TILE_SIZE;
|
||||
plr->rect.y = (MAP_HEIGHT - 10) * TILE_SIZE;
|
||||
plr->rect.w = TILE_SIZE;
|
||||
plr->rect.h = TILE_SIZE;
|
||||
|
||||
@@ -140,17 +142,17 @@ void initPlayer(Player *plr) {
|
||||
plr->cursor.targetTileRect.h = TILE_SIZE;
|
||||
|
||||
targetItemBGRect.w = DISPLAY_WIDTH;
|
||||
targetItemBGRect.h = TILE_SIZE + fonts[2].size * 2;
|
||||
targetItemBGRect.x = 0;
|
||||
targetItemBGRect.y = DISPLAY_HEIGHT - TILE_SIZE - fonts[2].size * 2;
|
||||
targetItemBGRect.h = (TILE_SIZE / 2) + fonts[2].size * 2;
|
||||
targetItemBGRect.y = DISPLAY_HEIGHT - (TILE_SIZE / 2) - fonts[2].size * 2;
|
||||
|
||||
waveInfoBGRect.y = 0;
|
||||
waveInfoBGRect.x = 0;
|
||||
waveInfoBGRect.w = 380;
|
||||
waveInfoBGRect.w = 450;
|
||||
waveInfoBGRect.h = 80;
|
||||
|
||||
targetItemRect.w = TILE_SIZE;
|
||||
targetItemRect.h = TILE_SIZE;
|
||||
targetItemRect.w = TILE_SIZE / 2;
|
||||
targetItemRect.h = TILE_SIZE / 2;
|
||||
|
||||
plr->cursor.heldItemRect.w = TILE_SIZE;
|
||||
plr->cursor.heldItemRect.h = TILE_SIZE;
|
||||
@@ -172,8 +174,8 @@ void updatePlayer(Player *plr) {
|
||||
plr->prevHealth = plr->health;
|
||||
|
||||
if (plr->health <= 0) {
|
||||
plr->rect.x = DISPLAY_WIDTH / 2;
|
||||
plr->rect.y = DISPLAY_HEIGHT / 2;
|
||||
plr->rect.x = 10 * TILE_SIZE;
|
||||
plr->rect.y = (MAP_HEIGHT - 10) * TILE_SIZE;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -185,7 +187,8 @@ void renderPlayer(Player *plr) {
|
||||
SDL_SetRenderDrawColor(mainRenderer, 0, 0, 0, 0);
|
||||
|
||||
SDL_SetRenderTarget(mainRenderer, entityTexture);
|
||||
SDL_RenderCopy(mainRenderer, atlasTexture, &playerTextureRect, &PlayerRect);
|
||||
double angle = angle_between_points_deg(PlayerRect.x, PlayerRect.y, plr->cursor.windowX, plr->cursor.windowY) + 90;
|
||||
SDL_RenderCopyEx(mainRenderer, atlasTexture, &playerTextureRect, &PlayerRect, angle, NULL, SDL_FLIP_NONE);
|
||||
//SDL_RenderCopy(mainRenderer, PlayerTexture, NULL, &PlayerRect);
|
||||
SDL_SetRenderTarget(mainRenderer, hudTexture);
|
||||
SDL_RenderClear(mainRenderer);
|
||||
@@ -255,21 +258,24 @@ void renderPlayer(Player *plr) {
|
||||
|
||||
|
||||
SDL_SetRenderDrawColor(mainRenderer, 0, 0, 0, 255);
|
||||
targetItemBGRect.h = (TILE_SIZE / 2) + fonts[2].size * 2;
|
||||
targetItemBGRect.w = DISPLAY_WIDTH;
|
||||
targetItemBGRect.y = DISPLAY_HEIGHT - (TILE_SIZE / 2) - fonts[2].size * 2;
|
||||
SDL_RenderFillRect(mainRenderer, &targetItemBGRect);
|
||||
SDL_RenderFillRect(mainRenderer, &waveInfoBGRect);
|
||||
|
||||
|
||||
char hudStr[50];
|
||||
char waveStr[30];
|
||||
char hudStr[80];
|
||||
char waveStr[50];
|
||||
if (entities.activeCount > 0) {
|
||||
snprintf(waveStr, 30, "Remaining enemies: %d", entities.activeCount);
|
||||
snprintf(waveStr, 50, "Remaining enemies: %d", entities.activeCount);
|
||||
} else {
|
||||
snprintf(waveStr, 30, "Next wave in: %dm %ds", waveInfo.waveTimer / 60, waveInfo.waveTimer % 60);
|
||||
snprintf(waveStr, 50, "Next wave in: %dm %ds", waveInfo.waveTimer / 3600, (waveInfo.waveTimer / 60) % 60);
|
||||
}
|
||||
snprintf(hudStr, 30, "Wave: %d\n%s\n", waveInfo.waveCounter, waveStr);
|
||||
renderText(mainRenderer, fonts[1], hudStr, 0,0);
|
||||
snprintf(hudStr, 80, "Wave: %d/%d\n%s\n", waveInfo.waveCounter, waveInfo.totalWaves, waveStr);
|
||||
renderText(mainRenderer, fonts[1], hudStr, 0, 0);
|
||||
|
||||
targetItemRect.y = DISPLAY_HEIGHT - TILE_SIZE;
|
||||
targetItemRect.y = DISPLAY_HEIGHT - (TILE_SIZE / 2);
|
||||
targetItemRect.x = TILE_SIZE / 4;
|
||||
|
||||
SDL_SetTextureBlendMode(atlasTexture, SDL_BLENDMODE_ADD);
|
||||
@@ -310,7 +316,7 @@ void renderPlayer(Player *plr) {
|
||||
renderText(mainRenderer, fonts[2], itemStringCount, targetItemRect.x - 3,
|
||||
targetItemRect.y - (fonts[2].size * 3 / 2));
|
||||
//targetItemRect.x += (TILE_SIZE / 2) + (TILE_SIZE / 4);
|
||||
targetItemRect.x += TILE_SIZE / 2 * 3;
|
||||
targetItemRect.x += TILE_SIZE;
|
||||
}
|
||||
}
|
||||
SDL_SetTextureBlendMode(atlasTexture, SDL_BLENDMODE_BLEND);
|
||||
|
@@ -9,8 +9,8 @@
|
||||
#include "../items/item.h"
|
||||
|
||||
extern int playerReach;
|
||||
#define playerTileX (player.rect.x / TILE_SIZE)
|
||||
#define playerTileY (player.rect.y / TILE_SIZE)
|
||||
#define playerTileX (mainPlayer.rect.x / TILE_SIZE)
|
||||
#define playerTileY (mainPlayer.rect.y / TILE_SIZE)
|
||||
extern int playerSpeed;
|
||||
|
||||
extern SDL_Texture *entityTexture;
|
||||
@@ -59,8 +59,12 @@ typedef struct Player{
|
||||
uint8_t healthIdle;
|
||||
SDL_Rect rect;
|
||||
MiniRect tileRect;
|
||||
uint32_t mouseButtons;
|
||||
MiniRect coreTileRect;
|
||||
} Player;
|
||||
|
||||
extern SDL_Rect PlayerRect;
|
||||
|
||||
void setActivePlayerSlot(Player *plr, ItemType activeSlotIndex);
|
||||
|
||||
void moveActivePlayerSlot(Player *plr, bool up, bool seek);
|
||||
@@ -71,4 +75,6 @@ void initPlayer(Player *plr);
|
||||
|
||||
void updatePlayer(Player *plr);
|
||||
|
||||
extern Player mainPlayer;
|
||||
|
||||
#endif //FACTORYGAME_PLAYER_H
|
||||
|
34
tiles/ammoCrafter.c
Normal file
@@ -0,0 +1,34 @@
|
||||
//
|
||||
// Created by bruno on 11.6.2025.
|
||||
//
|
||||
|
||||
#include "ammoCrafter.h"
|
||||
|
||||
|
||||
const MachineRecipe AmmoCrafterRecipes[] = {
|
||||
// INPUT1 INPUT2 OUTPUT TIME
|
||||
{IRON_PLATE, IRON_INGOT, IRON_BULLET, 120},
|
||||
{SILVER_PLATE, SILVER_INGOT, SILVER_BULLET, 140},
|
||||
{GOLD_PLATE, GOLD_INGOT, GOLD_BULLET, 160},
|
||||
{PLATINUM_PLATE, PLATINUM_INGOT, PLATINUM_BULLET, 180},
|
||||
|
||||
{IRON_ROD, IRON_INGOT, TYPE_BELT, 30},
|
||||
{IRON_PLATE, IRON_INGOT, TYPE_SPLITTER, 60},
|
||||
|
||||
{GOLD_PLATE, SILVER_INGOT, TYPE_AMMOCRAFTER, 240},
|
||||
{SILVER_PLATE, GOLD_INGOT, TYPE_WIRECRAFTER, 240},
|
||||
{PLATINUM_PLATE, GOLD_INGOT, TYPE_TURRET, 240},
|
||||
|
||||
{SILVER_ROD, SILVER_INGOT, TYPE_FURNACE, 240},
|
||||
{GOLD_ROD, IRON_INGOT, TYPE_BLOCK, 120},
|
||||
};
|
||||
|
||||
void initAmmoCrafterTile() {
|
||||
initMachineTile(TYPE_AMMOCRAFTER, AmmoCrafterRecipes, sizeof(AmmoCrafterRecipes) / sizeof(AmmoCrafterRecipes[0]),
|
||||
1, /* start frame */
|
||||
8 /* frame divisor */);
|
||||
}
|
||||
|
||||
void updateAmmoCrafter(Tile *tile) {
|
||||
updateMachine(tile, AmmoCrafterRecipes, sizeof(AmmoCrafterRecipes) / sizeof(AmmoCrafterRecipes[0]), WAVE_RAMP, 500, 1);
|
||||
}
|
23
tiles/ammoCrafter.h
Normal file
@@ -0,0 +1,23 @@
|
||||
//
|
||||
// Created by bruno on 11.6.2025.
|
||||
//
|
||||
|
||||
#ifndef FACTORYGAME_AMMOCRAFTER_H
|
||||
#define FACTORYGAME_AMMOCRAFTER_H
|
||||
|
||||
#include "../items/item.h"
|
||||
#include "stdint.h"
|
||||
#include "../util/crafter.h"
|
||||
#include "../util/audio.h"
|
||||
|
||||
extern const MachineRecipe AmmoCrafterRecipes[];
|
||||
|
||||
void updateAmmoCrafter(Tile *tile);
|
||||
|
||||
void initAmmoCrafterTile();
|
||||
|
||||
#define AMMO_CRAFTER_INPUT_SLOT 0
|
||||
#define AMMO_CRAFTER_OUTPUT_SLOT 1
|
||||
|
||||
|
||||
#endif //FACTORYGAME_AMMOCRAFTER_H
|
29
tiles/core.c
Normal file
@@ -0,0 +1,29 @@
|
||||
//
|
||||
// Created by bruno on 11.6.2025.
|
||||
//
|
||||
|
||||
#include "core.h"
|
||||
#include "../player/player.h"
|
||||
|
||||
void updateCore(Tile *tile) {
|
||||
for (size_t slot = 0; slot < ItemSlotCount; slot++) {
|
||||
ItemOnBelt *item = &tile->items[slot];
|
||||
if (item->type == TYPE_AIR) {
|
||||
continue;
|
||||
} else if (item->type < ITEMREGISTRY_SIZE) {
|
||||
mainPlayer.inventory.slotCounts[item->type]++;
|
||||
item->type = TYPE_AIR;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void initCoreTile() {
|
||||
// Force slot assignments for allowed items based on recipes
|
||||
for (size_t i = 0; i < ITEMREGISTRY_SIZE; i++) {
|
||||
for (size_t slot = 0; slot < ItemSlotCount; slot++) {
|
||||
TileRegistry[TYPE_CORE].allowedInItems[slot][i] = true;
|
||||
}
|
||||
}
|
||||
// Animation and behavior settings
|
||||
TileRegistry[TYPE_CORE].needsTicks = true;
|
||||
}
|
14
tiles/core.h
Normal file
@@ -0,0 +1,14 @@
|
||||
//
|
||||
// Created by bruno on 11.6.2025.
|
||||
//
|
||||
|
||||
#ifndef FACTORYGAME_CORE_H
|
||||
#define FACTORYGAME_CORE_H
|
||||
|
||||
#include "tile.h"
|
||||
|
||||
void updateCore(Tile *tile);
|
||||
|
||||
void initCoreTile();
|
||||
|
||||
#endif //FACTORYGAME_CORE_H
|
@@ -11,7 +11,6 @@
|
||||
|
||||
// Suppose this is defined somewhere
|
||||
extern const MachineRecipe FurnaceRecipes[];
|
||||
extern const size_t FurnaceRecipeCount;
|
||||
|
||||
void updateFurnace(Tile *tile);
|
||||
|
||||
@@ -20,6 +19,4 @@ void initFurnaceTile();
|
||||
#define FURNACE_INPUT_SLOT 0
|
||||
#define FURNACE_OUTPUT_SLOT 1
|
||||
|
||||
void updateFurnace(Tile * tile);
|
||||
|
||||
#endif //FACTORYGAME_FURNACE_H
|
||||
|
184
tiles/tile.c
@@ -10,6 +10,68 @@
|
||||
#include "../util/font.h"
|
||||
#include "miner.h"
|
||||
#include "turret.h"
|
||||
#include "../util/perlin.h"
|
||||
#include "../util/button.h"
|
||||
#include "../util/audio.h"
|
||||
#include "ammoCrafter.h"
|
||||
#include "wiredrawer.h"
|
||||
#include "core.h"
|
||||
|
||||
MiniRect enemySpawn;
|
||||
MiniRect playerCore;
|
||||
SDL_Rect viewport;
|
||||
|
||||
uint16_t DISPLAY_MAP_WIDTH = 30;
|
||||
uint16_t DISPLAY_MAP_HEIGHT = 16;
|
||||
|
||||
uint16_t DISPLAY_WIDTH = 30 * TILE_SIZE;
|
||||
uint16_t DISPLAY_HEIGHT = 16 * TILE_SIZE;
|
||||
|
||||
void setTileView(uint16_t w, uint16_t h) {
|
||||
DISPLAY_MAP_WIDTH = w;
|
||||
DISPLAY_MAP_HEIGHT = h;
|
||||
DISPLAY_WIDTH = DISPLAY_MAP_WIDTH * TILE_SIZE;
|
||||
DISPLAY_HEIGHT = DISPLAY_MAP_HEIGHT * TILE_SIZE;
|
||||
screenRect.x = 0;
|
||||
screenRect.y = 0;
|
||||
screenRect.w = DISPLAY_WIDTH;
|
||||
screenRect.h = DISPLAY_HEIGHT;
|
||||
SDL_RenderSetLogicalSize(mainRenderer, DISPLAY_WIDTH, DISPLAY_HEIGHT);
|
||||
SDL_SetWindowSize(window, DISPLAY_WIDTH, DISPLAY_HEIGHT);
|
||||
viewport = screenRect;
|
||||
SDL_RenderSetViewport(mainRenderer, &viewport);
|
||||
SDL_SetWindowPosition(window, 0, 0);
|
||||
audioData.maxPanDistance = DISPLAY_WIDTH / 2;
|
||||
|
||||
SDL_DestroyTexture(hudTexture);
|
||||
SDL_DestroyTexture(entityTexture);
|
||||
SDL_DestroyTexture(itemsTexture);
|
||||
SDL_DestroyTexture(tilesTexture);
|
||||
SDL_DestroyTexture(backgroundTexture);
|
||||
|
||||
|
||||
hudTexture = SDL_CreateTexture(mainRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, screenRect.w,
|
||||
screenRect.h);
|
||||
entityTexture = SDL_CreateTexture(mainRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, screenRect.w,
|
||||
screenRect.h);
|
||||
|
||||
SDL_SetTextureBlendMode(entityTexture, SDL_BLENDMODE_BLEND);
|
||||
SDL_SetTextureBlendMode(hudTexture, SDL_BLENDMODE_BLEND);
|
||||
|
||||
|
||||
itemsTexture = SDL_CreateTexture(mainRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, screenRect.w,
|
||||
screenRect.h);
|
||||
tilesTexture = SDL_CreateTexture(mainRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, screenRect.w,
|
||||
screenRect.h);
|
||||
SDL_SetTextureBlendMode(tilesTexture, SDL_BLENDMODE_BLEND);
|
||||
SDL_SetTextureBlendMode(itemsTexture, SDL_BLENDMODE_BLEND);
|
||||
backgroundTexture = SDL_CreateTexture(mainRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET,
|
||||
screenRect.w,
|
||||
screenRect.h);
|
||||
PlayerRect.x = (DISPLAY_WIDTH / 2) - (PlayerRect.w / 2);
|
||||
PlayerRect.y = (DISPLAY_HEIGHT / 2) - (PlayerRect.h / 2);
|
||||
initButtons();
|
||||
}
|
||||
|
||||
int scrollFrame = 0;
|
||||
unsigned long beltFrames = 0;
|
||||
@@ -117,7 +179,7 @@ void registerTile(char fname[20], SDL_Renderer *renderer) {
|
||||
TileRegistry[indexTile].animation.atlasRects[o][frame] = allocate_32x32(textures[o], renderer);
|
||||
}
|
||||
|
||||
printf("Bound %s to %d\n", fname, indexTile);
|
||||
//printf("Bound %s to %d\n", fname, indexTile);
|
||||
TileRegistry[indexTile].type = indexTile;
|
||||
TileRegistry[indexTile].maxHealth = 200;
|
||||
TileRegistry[indexTile].animation.frameCount = frame + 1;
|
||||
@@ -262,8 +324,14 @@ void loadBackgroundTiles(SDL_Renderer *renderer) {
|
||||
void preSetupTiles() {
|
||||
TileRegistry[TYPE_MINER].animation.startFrame = 1;
|
||||
TileRegistry[TYPE_FURNACE].animation.divisor = 8;
|
||||
|
||||
TileRegistry[TYPE_WIRECRAFTER].animation.divisor = 8;
|
||||
TileRegistry[TYPE_SPLITTER].animation.divisor = 8;
|
||||
TileRegistry[TYPE_AMMOCRAFTER].animation.divisor = 8;
|
||||
|
||||
TileRegistry[TYPE_CORE].animation.divisor = 8;
|
||||
BackgroundTileRegistry[BGType_WATER_DEEP].animation.divisor = 16;
|
||||
BackgroundTileRegistry[BGType_ENEMY_FLOOR].animation.divisor = 16;
|
||||
BackgroundTileRegistry[BGType_WATER_SHALLOW].animation.divisor = 12;
|
||||
BackgroundTileRegistry[BGType_GRASS_FLOWER0].animation.divisor = 16;
|
||||
BackgroundTileRegistry[BGType_GRASS_FLOWER1].animation.divisor = 16;
|
||||
@@ -309,6 +377,9 @@ void setupTiles() {
|
||||
BackgroundTileRegistry[BGType_WATER_DEEP].walkable = false;
|
||||
|
||||
initFurnaceTile();
|
||||
initWireDrawerTile();
|
||||
initAmmoCrafterTile();
|
||||
initCoreTile();
|
||||
}
|
||||
|
||||
uint16_t getBreakTime(int type) {
|
||||
@@ -481,4 +552,113 @@ bool isWalkable(MiniRect tileCoords) {
|
||||
|
||||
void updateTiles() {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// Main generator:
|
||||
void genInitMap() {
|
||||
const double terrainScale = 2;
|
||||
const double humidityScale = 1;
|
||||
const double oreScale = 2;
|
||||
const int terrainOct = 4;
|
||||
const int humidityOct = 4;
|
||||
const int oreOct = 4;
|
||||
|
||||
int seedX = rand();
|
||||
int seedY = rand();
|
||||
int seedN = rand();
|
||||
|
||||
// Min/max trackers
|
||||
double terrainMin = 1e9, terrainMax = -1e9;
|
||||
double humidityMin = 1e9, humidityMax = -1e9;
|
||||
double oreNrmMin = 1e9, oreNrmMax = -1e9;
|
||||
for (uint16_t y = 0; y < MAP_HEIGHT; y++) {
|
||||
for (uint16_t x = 0; x < MAP_WIDTH; x++) {
|
||||
double terrain = pnoise2d(x + 1000 + seedX,
|
||||
y + 1000 + seedY,
|
||||
terrainScale, terrainOct, seedN);
|
||||
double humidity = pnoise2d(x + seedX,
|
||||
y + seedY,
|
||||
humidityScale, humidityOct, seedN);
|
||||
double oreNrm = pnoise2d(x + 9999 + seedX,
|
||||
y + 1111 + seedY,
|
||||
oreScale, oreOct, seedN);
|
||||
|
||||
// Track min/max
|
||||
if (terrain < terrainMin) terrainMin = terrain;
|
||||
if (terrain > terrainMax) terrainMax = terrain;
|
||||
|
||||
if (humidity < humidityMin) humidityMin = humidity;
|
||||
if (humidity > humidityMax) humidityMax = humidity;
|
||||
|
||||
if (oreNrm < oreNrmMin) oreNrmMin = oreNrm;
|
||||
if (oreNrm > oreNrmMax) oreNrmMax = oreNrm;
|
||||
|
||||
// [Same as your original terrain generation logic...]
|
||||
BackgroundType baseType = BGType_COBBLE0;
|
||||
if (terrain < 0.30) {
|
||||
baseType = (humidity < 0.5) ? BGType_WATER_SHALLOW : BGType_WATER_DEEP;
|
||||
} else if (terrain < 0.35) {
|
||||
if (humidity < 0.3) baseType = BGType_SAND4;
|
||||
else if (humidity < 0.6) baseType = BGType_SAND2;
|
||||
else baseType = BGType_SAND7;
|
||||
} else if (terrain < 0.7) {
|
||||
double grassVal = (terrain - 0.35) / (0.70 - 0.35);
|
||||
int idx = (int) (grassVal * 3.0);
|
||||
if (idx >= 4) idx = 3;
|
||||
if (humidity > 0.6 && ((rand() & 0xFF) < 10)) {
|
||||
int flowerIdx = rand() % 4;
|
||||
baseType = (BackgroundType) (BGType_GRASS_FLOWER0 + flowerIdx);
|
||||
} else {
|
||||
baseType = (BackgroundType) (BGType_GRASS0 + idx);
|
||||
}
|
||||
} else if (terrain < 0.85) {
|
||||
int idx = rand() % 4;
|
||||
baseType = (BackgroundType) (BGType_COBBLE0 + idx);
|
||||
} else {
|
||||
int idx = rand() % 4;
|
||||
baseType = (BackgroundType) (BGType_TILES0 + idx);
|
||||
}
|
||||
|
||||
BackgroundType finalType = baseType;
|
||||
if (baseType != BGType_WATER_SHALLOW && baseType != BGType_WATER_DEEP) {
|
||||
if (oreNrm > 0.86) {
|
||||
double sub = (oreNrm - 0.86) / (1.0 - 0.86);
|
||||
if (sub < 0.25) finalType = BGType_IRON_ORE;
|
||||
else if (sub < 0.50) finalType = BGType_SILVER_ORE;
|
||||
else if (sub < 0.80) finalType = BGType_GOLD_ORE;
|
||||
else finalType = BGType_PLATINUM_ORE;
|
||||
}
|
||||
}
|
||||
|
||||
if (finalType > BGType_END) {
|
||||
finalType = BGType_COBBLE0;
|
||||
}
|
||||
backgroundMap[y][x].type = finalType;
|
||||
}
|
||||
enemySpawn.x = MAP_WIDTH - 10;
|
||||
enemySpawn.y = 10;
|
||||
for (int enX = enemySpawn.x - 5; enX < enemySpawn.x + 6; enX++) {
|
||||
for (int enY = enemySpawn.y - 5; enY < enemySpawn.y + 6; enY++) {
|
||||
backgroundMap[enY][enX].type = BGType_ENEMY_FLOOR;
|
||||
}
|
||||
}
|
||||
playerCore.x = 8;
|
||||
playerCore.y = MAP_HEIGHT - 10;
|
||||
for (int plX = playerCore.x - 5; plX < playerCore.x + 6; plX++) {
|
||||
for (int plY = playerCore.y - 5; plY < playerCore.y + 6; plY++) {
|
||||
backgroundMap[plY][plX].type = BGType_TILES0;
|
||||
}
|
||||
}
|
||||
tileMap[playerCore.y][playerCore.x].type = TYPE_CORE;
|
||||
tileMap[playerCore.y][playerCore.x].direction = ORIENT_LEFT;
|
||||
tileMap[playerCore.y][playerCore.x].health = TileRegistry[TYPE_CORE].maxHealth;
|
||||
tileMap[playerCore.y][playerCore.x].fixedFrame = 0;
|
||||
tileMap[playerCore.y][playerCore.x].rect = playerCore;
|
||||
|
||||
}
|
||||
|
||||
// Debug printout
|
||||
printf("Terrain Noise: min = %f, max = %f\n", terrainMin, terrainMax);
|
||||
printf("Humidity Noise: min = %f, max = %f\n", humidityMin, humidityMax);
|
||||
printf("Ore Normalized: min = %f, max = %f\n", oreNrmMin, oreNrmMax);
|
||||
}
|
||||
|
23
tiles/tile.h
@@ -9,17 +9,17 @@
|
||||
#include "../items/item.h"
|
||||
#include "tilecallbacks.h"
|
||||
|
||||
#define MAP_WIDTH 500
|
||||
#define MAP_HEIGHT 500
|
||||
#define MAP_WIDTH 200
|
||||
#define MAP_HEIGHT 200
|
||||
|
||||
#define DISPLAY_MAP_WIDTH 60
|
||||
#define DISPLAY_MAP_HEIGHT 31
|
||||
extern uint16_t DISPLAY_MAP_WIDTH;
|
||||
extern uint16_t DISPLAY_MAP_HEIGHT;
|
||||
|
||||
extern uint16_t DISPLAY_WIDTH;
|
||||
extern uint16_t DISPLAY_HEIGHT;
|
||||
|
||||
#define TILE_SIZE 32
|
||||
|
||||
#define DISPLAY_WIDTH DISPLAY_MAP_WIDTH * TILE_SIZE
|
||||
#define DISPLAY_HEIGHT DISPLAY_MAP_HEIGHT * TILE_SIZE
|
||||
|
||||
#define MAX_TILES MAP_WIDTH * MAP_HEIGHT
|
||||
|
||||
|
||||
@@ -73,12 +73,16 @@ typedef enum BackgroundType {
|
||||
BGType_SILVER_ORE,
|
||||
BGType_GOLD_ORE,
|
||||
BGType_PLATINUM_ORE,
|
||||
BGType_ENEMY_FLOOR,
|
||||
BGType_END
|
||||
} BackgroundType;
|
||||
|
||||
#define MAX_BASE_NAMES 512
|
||||
#define MAX_ANIMATION_FRAMES 32
|
||||
|
||||
extern MiniRect enemySpawn;
|
||||
extern MiniRect playerCore;
|
||||
|
||||
typedef void (*UpdateTileCallback)(struct Tile *tile);
|
||||
|
||||
typedef struct TileTypeReg {
|
||||
@@ -166,4 +170,9 @@ uint16_t getBreakTime(int type);
|
||||
|
||||
void initTiles();
|
||||
|
||||
void genInitMap();
|
||||
|
||||
void setTileView(uint16_t w, uint16_t h);
|
||||
extern SDL_Rect viewport;
|
||||
|
||||
#endif //FACTORYGAME_TILE_H
|
||||
|
@@ -6,6 +6,9 @@
|
||||
#include "furnace.h"
|
||||
#include "miner.h"
|
||||
#include "turret.h"
|
||||
#include "ammoCrafter.h"
|
||||
#include "wiredrawer.h"
|
||||
#include "core.h"
|
||||
|
||||
const UpdateTileCallback ItemTileCallbacks[TILEREGISTRY_SIZE] = {
|
||||
[TYPE_AIR] = NULL,
|
||||
@@ -14,4 +17,8 @@ const UpdateTileCallback ItemTileCallbacks[TILEREGISTRY_SIZE] = {
|
||||
[TYPE_FURNACE] = updateFurnace,
|
||||
[TYPE_MINER] = updateMiner,
|
||||
[TYPE_TURRET] = updateTurret,
|
||||
[TYPE_SPLITTER] = NULL, //TODO MOVE implementation from updateItem
|
||||
[TYPE_CORE] = updateCore,
|
||||
[TYPE_WIRECRAFTER] = updateWireDrawer,
|
||||
[TYPE_AMMOCRAFTER] = updateAmmoCrafter,
|
||||
};
|
29
tiles/wiredrawer.c
Normal file
@@ -0,0 +1,29 @@
|
||||
//
|
||||
// Created by bruno on 11.6.2025.
|
||||
//
|
||||
|
||||
#include "wiredrawer.h"
|
||||
#include "tile.h"
|
||||
#include "../util/audio.h"
|
||||
|
||||
const MachineRecipe WireDrawerRecipes[] = {
|
||||
{IRON_INGOT, TYPE_AIR, IRON_PLATE, 120},
|
||||
{SILVER_INGOT, TYPE_AIR, SILVER_PLATE, 140},
|
||||
{GOLD_INGOT, TYPE_AIR, GOLD_PLATE, 160},
|
||||
{PLATINUM_INGOT, TYPE_AIR, PLATINUM_PLATE, 180},
|
||||
|
||||
{IRON_PLATE, TYPE_AIR, IRON_ROD, 120},
|
||||
{SILVER_PLATE, TYPE_AIR, SILVER_ROD, 140},
|
||||
{GOLD_PLATE, TYPE_AIR, GOLD_ROD, 160},
|
||||
{PLATINUM_PLATE, TYPE_AIR, PLATINUM_ROD, 180}
|
||||
};
|
||||
|
||||
void initWireDrawerTile() {
|
||||
initMachineTile(TYPE_WIRECRAFTER, WireDrawerRecipes, sizeof(WireDrawerRecipes) / sizeof(WireDrawerRecipes[0]),
|
||||
1, /* start frame */
|
||||
8 /* frame divisor */);
|
||||
}
|
||||
|
||||
void updateWireDrawer(Tile *tile) {
|
||||
updateMachine(tile, WireDrawerRecipes, sizeof(WireDrawerRecipes) / sizeof(WireDrawerRecipes[0]), WAVE_RAMP, 500, 1);
|
||||
}
|
21
tiles/wiredrawer.h
Normal file
@@ -0,0 +1,21 @@
|
||||
//
|
||||
// Created by bruno on 11.6.2025.
|
||||
//
|
||||
|
||||
#ifndef FACTORYGAME_WIREDRAWER_H
|
||||
#define FACTORYGAME_WIREDRAWER_H
|
||||
|
||||
#include "../util/crafter.h"
|
||||
|
||||
// Suppose this is defined somewhere
|
||||
extern const MachineRecipe WireDrawerRecipes[];
|
||||
|
||||
void updateWireDrawer(Tile *tile);
|
||||
|
||||
void initWireDrawerTile();
|
||||
|
||||
#define WIRE_DRAWER_INPUT_SLOT 0
|
||||
#define WIRE_DRAWER_OUTPUT_SLOT 1
|
||||
|
||||
|
||||
#endif //FACTORYGAME_WIREDRAWER_H
|
212
util/button.c
Normal file
@@ -0,0 +1,212 @@
|
||||
//
|
||||
// Created by bruno on 11.6.2025.
|
||||
//
|
||||
|
||||
#include "button.h"
|
||||
#include "gamestate.h"
|
||||
|
||||
void onStart() {
|
||||
genInitMap();
|
||||
screenType = SCREEN_GAME;
|
||||
}
|
||||
|
||||
void onContinue() {
|
||||
if (loadGameState(autosaveName, &mainPlayer)) {
|
||||
genInitMap();
|
||||
}
|
||||
screenType = SCREEN_GAME;
|
||||
}
|
||||
|
||||
void onCredits() {
|
||||
screenType = SCREEN_CREDITS;
|
||||
}
|
||||
|
||||
void onNext() {
|
||||
if (!waveInfo.waveRunning && waveInfo.waveTimer > 60 * 5) {
|
||||
waveInfo.waveTimer = 60 * 5;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void onQuit() {
|
||||
running = false;
|
||||
}
|
||||
|
||||
void onBack() {
|
||||
screenType = SCREEN_MENU;
|
||||
}
|
||||
|
||||
void onAtlas() {
|
||||
screenType = SCREEN_ATLAS;
|
||||
}
|
||||
|
||||
void onFont() {
|
||||
screenType = SCREEN_FONTS;
|
||||
}
|
||||
|
||||
Button buttons[5][BUTTON_COUNT];
|
||||
|
||||
void initButtons(void) {
|
||||
buttons[0][0] = (Button){
|
||||
.label = "New",
|
||||
.rect = {110, DISPLAY_HEIGHT / 2, 125, 40},
|
||||
.color = {100, 100, 0, 255},
|
||||
.callback = onStart,
|
||||
.font = &fonts[1]
|
||||
};
|
||||
|
||||
buttons[0][1] = (Button){
|
||||
.label = "Continue",
|
||||
.rect = {100, DISPLAY_HEIGHT / 2 + 45, 150, 40},
|
||||
.color = {0, 100, 0, 255},
|
||||
.callback = onContinue,
|
||||
.font = &fonts[1]
|
||||
};
|
||||
|
||||
buttons[0][2] = (Button){
|
||||
.label = "Credits",
|
||||
.rect = {DISPLAY_WIDTH - 200, DISPLAY_HEIGHT - 120, 150, 40},
|
||||
.color = {0, 50, 50, 255},
|
||||
.callback = onCredits,
|
||||
.font = &fonts[1]
|
||||
};
|
||||
|
||||
buttons[0][3] = (Button){
|
||||
.label = "Exit",
|
||||
.rect = {DISPLAY_WIDTH - 200, DISPLAY_HEIGHT - 40, 100, 40},
|
||||
.color = {100, 0, 0, 255},
|
||||
.callback = onQuit,
|
||||
.font = &fonts[1]
|
||||
};
|
||||
|
||||
buttons[1][0] = (Button){
|
||||
.label = "Back",
|
||||
.rect = {DISPLAY_WIDTH / 2 - (100 / 2), DISPLAY_HEIGHT - 40, 100, 40},
|
||||
.color = {100, 0, 0, 255},
|
||||
.callback = onBack,
|
||||
.font = &fonts[1]
|
||||
};
|
||||
|
||||
buttons[1][1] = (Button){
|
||||
.label = "Font atlas",
|
||||
.rect = {DISPLAY_WIDTH - 900, DISPLAY_HEIGHT - 40, 200, 40},
|
||||
.color = {100, 0, 0, 255},
|
||||
.callback = onFont,
|
||||
.font = &fonts[1]
|
||||
};
|
||||
|
||||
buttons[1][2] = (Button){
|
||||
.label = "Texture atlas",
|
||||
.rect = {DISPLAY_WIDTH - 300, DISPLAY_HEIGHT - 40, 250, 40},
|
||||
.color = {100, 0, 0, 255},
|
||||
.callback = onAtlas,
|
||||
.font = &fonts[1]
|
||||
};
|
||||
|
||||
buttons[2][0] = (Button){
|
||||
.label = "Next",
|
||||
.rect = {395, 0, 55, 80},
|
||||
.color = {64, 64, 64, 64},
|
||||
.callback = onNext,
|
||||
.font = &fonts[2]
|
||||
};
|
||||
|
||||
buttons[3][0] = (Button){
|
||||
.label = "Back",
|
||||
.rect = {DISPLAY_WIDTH / 2 - (400 / 2), DISPLAY_HEIGHT - 40, 100, 40},
|
||||
.color = {100, 0, 0, 255},
|
||||
.callback = onCredits,
|
||||
.font = &fonts[1]
|
||||
};
|
||||
|
||||
buttons[4][0] = (Button){
|
||||
.label = "Back",
|
||||
.rect = {DISPLAY_WIDTH / 2 - (400 / 2), DISPLAY_HEIGHT - 40, 100, 40},
|
||||
.color = {100, 0, 0, 255},
|
||||
.callback = onCredits,
|
||||
.font = &fonts[1]
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
void renderButtons(SDL_Renderer *renderer, Player player) {
|
||||
for (int i = 0; i < BUTTON_COUNT; i++) {
|
||||
renderButton(renderer, buttons[screenType][i], player);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void renderButton(SDL_Renderer *renderer, Button btn, Player player) {
|
||||
if (btn.font == NULL || btn.label == NULL || strlen(btn.label) == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Check for hover/click
|
||||
bool hovered = (player.cursor.windowX >= btn.rect.x && player.cursor.windowX <= btn.rect.x + btn.rect.w &&
|
||||
player.cursor.windowY >= btn.rect.y && player.cursor.windowY <= btn.rect.y + btn.rect.h);
|
||||
|
||||
// Set color and draw background
|
||||
SDL_Color bgColor = btn.color;
|
||||
if (hovered) {
|
||||
bgColor.a = btn.color.a / 2;
|
||||
}
|
||||
SDL_SetRenderDrawColor(renderer, bgColor.r, bgColor.g, bgColor.b, bgColor.a);
|
||||
SDL_RenderFillRect(renderer, &btn.rect);
|
||||
|
||||
// Split label into lines
|
||||
const char *label = btn.label;
|
||||
const int maxLines = 10; // Hard cap for sanity
|
||||
const char *lines[maxLines];
|
||||
int lineCount = 0;
|
||||
|
||||
lines[lineCount++] = label;
|
||||
for (const char *p = label; *p && lineCount < maxLines; p++) {
|
||||
if (*p == '\n') {
|
||||
lines[lineCount++] = p + 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Get length of each line and find max width (for centering)
|
||||
int maxLineWidth = 0;
|
||||
int lineWidths[maxLines];
|
||||
int fontSize = btn.font->size;
|
||||
|
||||
for (int i = 0; i < lineCount; i++) {
|
||||
const char *lineStart = lines[i];
|
||||
const char *lineEnd = strchr(lineStart, '\n');
|
||||
int lineLen = lineEnd ? (lineEnd - lineStart) : strlen(lineStart);
|
||||
|
||||
int lineWidth = lineLen * fontSize;
|
||||
lineWidths[i] = lineWidth;
|
||||
if (lineWidth > maxLineWidth) {
|
||||
maxLineWidth = lineWidth;
|
||||
}
|
||||
}
|
||||
|
||||
// Calculate starting Y to vertically center all lines
|
||||
int totalHeight = lineCount * fontSize;
|
||||
int startY = btn.rect.y + (btn.rect.h - totalHeight) / 2;
|
||||
|
||||
// Render each line centered horizontally
|
||||
for (int i = 0; i < lineCount; i++) {
|
||||
const char *lineStart = lines[i];
|
||||
const char *lineEnd = strchr(lineStart, '\n');
|
||||
int lineLen = lineEnd ? (lineEnd - lineStart) : strlen(lineStart);
|
||||
|
||||
char buffer[256];
|
||||
strncpy(buffer, lineStart, lineLen);
|
||||
buffer[lineLen] = '\0';
|
||||
|
||||
int x = btn.rect.x + (btn.rect.w - lineWidths[i]) / 2;
|
||||
int y = startY + i * fontSize;
|
||||
|
||||
renderText(renderer, *btn.font, buffer, x, y);
|
||||
}
|
||||
|
||||
// Trigger callback if clicked
|
||||
if (hovered && (player.mouseButtons & SDL_BUTTON_LMASK)) {
|
||||
if (btn.callback) {
|
||||
btn.callback();
|
||||
}
|
||||
}
|
||||
}
|
28
util/button.h
Normal file
@@ -0,0 +1,28 @@
|
||||
//
|
||||
// Created by bruno on 11.6.2025.
|
||||
//
|
||||
|
||||
#ifndef FACTORYGAME_BUTTON_H
|
||||
#define FACTORYGAME_BUTTON_H
|
||||
#include "SDL2/SDL.h"
|
||||
#include "font.h"
|
||||
#include "../player/player.h"
|
||||
|
||||
typedef struct {
|
||||
char* label;
|
||||
SDL_Rect rect;
|
||||
SDL_Color color;
|
||||
void (*callback)(void);
|
||||
BitmapFont *font;
|
||||
} Button;
|
||||
|
||||
#define BUTTON_COUNT 10
|
||||
|
||||
extern Button buttons[5][BUTTON_COUNT];
|
||||
void initButtons(void);
|
||||
|
||||
void renderButton(SDL_Renderer* renderer, Button btn, Player player);
|
||||
|
||||
void renderButtons(SDL_Renderer *renderer, Player player);
|
||||
|
||||
#endif //FACTORYGAME_BUTTON_H
|
@@ -17,7 +17,7 @@ enum {
|
||||
MACHINE_SLOTS = 3
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
typedef struct MachineRecipe {
|
||||
ItemType input1;
|
||||
ItemType input2; // use TYPE_AIR if single-input
|
||||
ItemType output;
|
||||
@@ -34,4 +34,4 @@ bool findMachineRecipe(const MachineRecipe *recipes, size_t count, ItemType in1,
|
||||
void initMachineTile(ItemType type, const MachineRecipe *recipes, size_t count,
|
||||
uint8_t startFrame, uint8_t divisor);
|
||||
|
||||
#endif //FACTORYGAME_CRAFTER_H
|
||||
#endif //FACTORYGAME_AMMOCRAFTER_H
|
||||
|
61
util/gamestate.c
Normal file
@@ -0,0 +1,61 @@
|
||||
//
|
||||
// Created by bruno on 11.6.2025.
|
||||
//
|
||||
|
||||
#include "gamestate.h"
|
||||
|
||||
GameState gameState;
|
||||
|
||||
char *autosaveName = "autosave.dat";
|
||||
|
||||
int loadGameState(char *filename, Player *plr) {
|
||||
fflush(stdout);
|
||||
FILE *gameSave = fopen(filename, "rb");
|
||||
if (gameSave) {
|
||||
fseek(gameSave, 0L, SEEK_END);
|
||||
long sz = ftell(gameSave);
|
||||
if (sz != sizeof(gameState)) {
|
||||
return 1;
|
||||
}
|
||||
rewind(gameSave);
|
||||
fread(&gameState, sizeof(gameState), 1, gameSave);
|
||||
fclose(gameSave);
|
||||
memcpy(plr, &gameState.player, sizeof(gameState.player));
|
||||
memcpy(tileMap, gameState.tileMap, sizeof(tileMap));
|
||||
memcpy(backgroundMap, gameState.backgroundTileMap, sizeof(backgroundMap));
|
||||
SDL_Rect *tmp = audioData.playerRect;
|
||||
memcpy(&audioData, &gameState.audioData, sizeof(gameState.audioData));
|
||||
audioData.playerRect = tmp;
|
||||
audioData.totalSamples = 0;
|
||||
memcpy(&neededUpdates, &gameState.neededUpdates, sizeof(gameState.neededUpdates));
|
||||
memcpy(&entities, &gameState.entities, sizeof(gameState.entities));
|
||||
openCount = gameState.openCount;
|
||||
memcpy(&openList, &gameState.openList, sizeof(gameState.openList));
|
||||
memcpy(&enemySpawn, &gameState.enemySpawn, sizeof(gameState.enemySpawn));
|
||||
plr->cursor.targetTile = NULL;
|
||||
plr->cursor.prevTargetTile = NULL;
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
void saveGameState(char *filename, Player *plr) {
|
||||
memcpy(&gameState.player, plr, sizeof(gameState.player));
|
||||
memcpy(gameState.tileMap, tileMap, sizeof(gameState.tileMap));
|
||||
memcpy(gameState.backgroundTileMap, backgroundMap, sizeof(gameState.backgroundTileMap));
|
||||
memcpy(&gameState.audioData, &audioData, sizeof(gameState.audioData));
|
||||
memcpy(&gameState.neededUpdates, &neededUpdates, sizeof(neededUpdates));
|
||||
memcpy(&gameState.entities, &entities, sizeof(entities));
|
||||
memcpy(&gameState.openList, &openList, sizeof(openList));
|
||||
memcpy(&gameState.enemySpawn, &enemySpawn, sizeof(enemySpawn));
|
||||
gameState.openCount = openCount;
|
||||
|
||||
FILE *gameSave = fopen(filename, "wb");
|
||||
if (!gameSave) {
|
||||
perror("Failed to open file for saving");
|
||||
return;
|
||||
}
|
||||
|
||||
fwrite(&gameState, sizeof(gameState), 1, gameSave);
|
||||
fclose(gameSave);
|
||||
}
|
33
util/gamestate.h
Normal file
@@ -0,0 +1,33 @@
|
||||
//
|
||||
// Created by bruno on 11.6.2025.
|
||||
//
|
||||
|
||||
#ifndef FACTORYGAME_GAMESTATE_H
|
||||
#define FACTORYGAME_GAMESTATE_H
|
||||
|
||||
|
||||
#include "../tiles/tile.h"
|
||||
#include "../player/player.h"
|
||||
#include "audio.h"
|
||||
#include "../entity/entity.h"
|
||||
|
||||
typedef struct GameState {
|
||||
Player player;
|
||||
Tile tileMap[MAP_HEIGHT][MAP_WIDTH];
|
||||
BackgroundTile backgroundTileMap[MAP_HEIGHT][MAP_WIDTH];
|
||||
AudioData audioData;
|
||||
TileArray neededUpdates;
|
||||
EntityArray entities;
|
||||
Node openList[MAX_OPEN_NODES];
|
||||
int openCount;
|
||||
MiniRect enemySpawn;
|
||||
} GameState;
|
||||
|
||||
int loadGameState(char *filename, Player *plr);
|
||||
|
||||
void saveGameState(char *filename, Player *plr);
|
||||
|
||||
extern GameState gameState;
|
||||
extern char *autosaveName;
|
||||
|
||||
#endif //FACTORYGAME_GAMESTATE_H
|
53
util/util.c
@@ -9,6 +9,8 @@
|
||||
//#include "font.h"
|
||||
|
||||
|
||||
ScreenType screenType = SCREEN_MENU;
|
||||
|
||||
//The window we'll be rendering to
|
||||
SDL_Window *window = NULL;
|
||||
volatile bool running = true;
|
||||
@@ -136,7 +138,8 @@ void renderBar(SDL_Renderer *renderer,
|
||||
char barString[20];
|
||||
sprintf(barString, "%d/%d", currentValue, maxValue);
|
||||
|
||||
renderText(mainRenderer, fonts[1], barString, x + (width / 2 - (fonts[2].size * strlen(barString))), y - fonts[2].size - barRect.h);
|
||||
renderText(mainRenderer, fonts[1], barString, x + (width / 2 - (fonts[2].size * strlen(barString))),
|
||||
y - fonts[2].size - barRect.h);
|
||||
}
|
||||
|
||||
int cmpstringp(const void *p1, const void *p2) {
|
||||
@@ -226,14 +229,14 @@ bool checkCollision(SDL_Rect a, SDL_Rect b) {
|
||||
|
||||
bool canMoveTo(SDL_Rect newRect) {
|
||||
// Round down to get all tiles the rect overlaps
|
||||
int left = newRect.x / TILE_SIZE;
|
||||
int right = (newRect.x + newRect.w - 1) / TILE_SIZE;
|
||||
int top = newRect.y / TILE_SIZE;
|
||||
int left = newRect.x / TILE_SIZE;
|
||||
int right = (newRect.x + newRect.w - 1) / TILE_SIZE;
|
||||
int top = newRect.y / TILE_SIZE;
|
||||
int bottom = (newRect.y + newRect.h - 1) / TILE_SIZE;
|
||||
|
||||
for (int tx = left; tx <= right; ++tx) {
|
||||
for (int ty = top; ty <= bottom; ++ty) {
|
||||
MiniRect tile = { tx, ty };
|
||||
MiniRect tile = {tx, ty};
|
||||
if (!isWalkable(tile)) {
|
||||
return false;
|
||||
}
|
||||
@@ -265,7 +268,7 @@ bool canMoveWithRadius(SDL_Rect centerRect) {
|
||||
int tileTop = y * TILE_SIZE;
|
||||
int tileBottom = tileTop + TILE_SIZE;
|
||||
|
||||
// Bounding box of the player
|
||||
// Bounding box of the mainPlayer
|
||||
int playerLeft = centerRect.x - radius;
|
||||
int playerRight = centerRect.x + radius;
|
||||
int playerTop = centerRect.y - radius;
|
||||
@@ -287,4 +290,40 @@ int compareStrings(const void *a, const void *b) {
|
||||
const char *strA = *(const char **) a;
|
||||
const char *strB = *(const char **) b;
|
||||
return strcmp(strA, strB);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
double angle_between_points_deg(double x1, double y1, double x2, double y2) {
|
||||
double dx = x2 - x1;
|
||||
double dy = y2 - y1;
|
||||
double angle_rad = atan2(dy, dx);
|
||||
double angle_deg = angle_rad * (180.0 / M_PI);
|
||||
|
||||
// Normalize to 0–360 degrees (optional, depending on use case)
|
||||
if (angle_deg < 0) {
|
||||
angle_deg += 360.0;
|
||||
}
|
||||
|
||||
return angle_deg;
|
||||
}
|
||||
|
||||
#define TICKS_PER_SECOND 60
|
||||
|
||||
void initWaveInfo(WaveInfo *info) {
|
||||
info->waveCounter = 0;
|
||||
info->waveTimer = 200 * TICKS_PER_SECOND; // 200 seconds before first wave
|
||||
info->waveRunning = false;
|
||||
info->totalWaves = MAX_WAVES;
|
||||
|
||||
for (int i = 0; i < MAX_WAVES; i++) {
|
||||
int flyerCount = 1 + i * 2; // Each wave increases flyer count
|
||||
|
||||
info->waves[i].enemyCount = 1;
|
||||
info->waves[i].enemies[0] = (EnemyEntry) {ENEMY_TYPE_FLYER, flyerCount};
|
||||
|
||||
// Reduce time between waves gradually (but not below 5 seconds)
|
||||
int secondsBetween = 15 - (i / 5);
|
||||
if (secondsBetween < 5) secondsBetween = 5;
|
||||
info->waves[i].timeUntilNext = secondsBetween * TICKS_PER_SECOND;
|
||||
}
|
||||
}
|
||||
|
43
util/util.h
@@ -49,13 +49,50 @@ typedef struct MiniRect {
|
||||
int y;
|
||||
} MiniRect;
|
||||
|
||||
#define MAX_ENEMIES_PER_WAVE 16
|
||||
#define MAX_WAVES 128
|
||||
|
||||
// Define enemy types (expand as needed)
|
||||
typedef enum EnemyType {
|
||||
ENEMY_TYPE_NONE = 0,
|
||||
ENEMY_TYPE_FLYER,
|
||||
// Add more enemy types here
|
||||
} EnemyType;
|
||||
|
||||
typedef struct EnemyEntry {
|
||||
EnemyType type;
|
||||
int count;
|
||||
} EnemyEntry;
|
||||
|
||||
|
||||
typedef struct Wave {
|
||||
EnemyEntry enemies[MAX_ENEMIES_PER_WAVE];
|
||||
int enemyCount; // Number of different enemy types in this wave
|
||||
int timeUntilNext; // Time until the next wave (in ticks, ms, etc.)
|
||||
} Wave;
|
||||
|
||||
typedef struct WaveInfo {
|
||||
int waveCounter;
|
||||
int waveTimer;
|
||||
int waveCounter; // Current wave number (index)
|
||||
int waveTimer; // Countdown until next wave starts
|
||||
bool waveRunning; // Whether a wave is currently running
|
||||
Wave waves[MAX_WAVES]; // List of all waves
|
||||
int totalWaves; // Total number of defined waves
|
||||
} WaveInfo;
|
||||
|
||||
typedef enum ScreenType {
|
||||
SCREEN_MENU,
|
||||
SCREEN_CREDITS,
|
||||
SCREEN_GAME,
|
||||
SCREEN_FONTS,
|
||||
SCREEN_ATLAS,
|
||||
} ScreenType;
|
||||
|
||||
extern ScreenType screenType;
|
||||
|
||||
extern WaveInfo waveInfo;
|
||||
|
||||
void initWaveInfo(WaveInfo *info);
|
||||
|
||||
|
||||
typedef struct OrientedAnimation {
|
||||
SDL_Rect atlasRects[ORIENT_DIRECTION_COUNT][TILE_SIZE * 2];
|
||||
@@ -92,4 +129,6 @@ bool canMoveTo(SDL_Rect newRect);
|
||||
|
||||
bool canMoveWithRadius(SDL_Rect centerRect);
|
||||
|
||||
double angle_between_points_deg(double x1, double y1, double x2, double y2);
|
||||
|
||||
#endif //FACTORYGAME_UTIL_H
|