start turret

This commit is contained in:
2025-06-10 16:29:17 +02:00
parent 79c8b747cd
commit a17e3abbff
17 changed files with 213 additions and 68 deletions

View File

@@ -9,6 +9,7 @@
#include "../util/atlas.h"
#include "../util/font.h"
#include "miner.h"
#include "turret.h"
int scrollFrame = 0;
unsigned long beltFrames = 0;
@@ -80,7 +81,7 @@ void registerTile(char fname[20], SDL_Renderer *renderer) {
// Load animation frames
int frame = 0;
int indexTile = 0;
ItemType indexTile = 0;
char texturePath[80];
if (sscanf(fname, "%d_%20[^_]_%d.png", &indexTile, name, &frame) == 3) {
@@ -111,12 +112,13 @@ void registerTile(char fname[20], SDL_Renderer *renderer) {
createRotatedTexture(renderer, texture, 270)
};
printf("Bound %s to %d orient %s\n", fname, indexTile, OrientStrings[o]);
// printf("Bound %s to %d orient %s\n", fname, indexTile, OrientStrings[o]);
TileRegistry[indexTile].animation.textures[o][frame] = textures[o];
SDL_SetTextureBlendMode(textures[o], SDL_BLENDMODE_BLEND);
TileRegistry[indexTile].animation.atlasRects[o][frame] = allocate_32x32(textures[o], renderer);
}
printf("Bound %s to %d\n", fname, indexTile);
TileRegistry[indexTile].type = indexTile;
TileRegistry[indexTile].maxHealth = 200;
TileRegistry[indexTile].animation.frameCount = frame + 1;
@@ -155,7 +157,7 @@ void registerBackgroundTile(char fname[20], SDL_Renderer *renderer) {
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_NONE);
printf("Bound %s to %d\n", fname, indexBgTile);
//printf("Bound %s to %d\n", fname, indexBgTile);
BackgroundTileRegistry[indexBgTile].animation.textures[frame] = texture;
BackgroundTileRegistry[indexBgTile].animation.atlasRects[frame] = allocate_32x32(texture, renderer);
@@ -261,7 +263,7 @@ void setupTiles() {
for (uint16_t i = 0; i < ItemSlotCount; i++) {
TileRegistry[TYPE_BELT].outputLane[i] = true;
}
for (uint16_t l = 0; l < ItemSlotCount; l++) {
for (uint16_t l = 0; l < 2; l++) {
for (ItemType i = 0; i < itemRegistryIndex; i++) {
TileRegistry[TYPE_BELT].allowedInItems[l][i] = true;
}
@@ -272,7 +274,7 @@ void setupTiles() {
TileRegistry[TYPE_FURNACE].allowedInItems[FURNACE_INPUT_SLOT][i] = true;
}
}
TileRegistry[TYPE_FURNACE].outputLane[FURNACE_OUTPUT_SLOT] = 1;
; TileRegistry[TYPE_FURNACE].outputLane[FURNACE_OUTPUT_SLOT] = 1;
TileRegistry[TYPE_FURNACE].startFrame = 1;
TileRegistry[TYPE_FURNACE].needsTicks = true;
TileRegistry[TYPE_FURNACE].animation.divisor = 8;
@@ -283,6 +285,9 @@ void setupTiles() {
TileRegistry[TYPE_MINER].startFrame = 1;
TileRegistry[TYPE_AIR].walkable = true;
TileRegistry[TYPE_TURRET].needsTicks = true;
TileRegistry[TYPE_TURRET].allowedInItems[TURRET_AMMO_INPUT_SLOT][IRON_INGOT] = true;
BackgroundTileRegistry[BGType_WATER_DEEP].animation.divisor = 16;
BackgroundTileRegistry[BGType_WATER_SHALLOW].animation.divisor = 12;
BackgroundTileRegistry[BGType_GRASS_FLOWER0].animation.divisor = 16;
@@ -450,6 +455,9 @@ void renderAllTiles(SDL_Renderer *renderer, SDL_Rect playerRect) {
}
bool isWalkable(MiniRect tileCoords) {
if (tileCoords.x < 0 || tileCoords.x >= MAP_WIDTH || tileCoords.y < 0 || tileCoords.y >= MAP_HEIGHT) {
return false;
}
BackgroundTileType bgt = BackgroundTileRegistry[backgroundMap[tileCoords.y][tileCoords.x].type];
TileTypeReg fgt = TileRegistry[tileMap[tileCoords.y][tileCoords.x].type];

View File

@@ -73,10 +73,10 @@ typedef enum BackgroundType {
BGType_COBBLE1,
BGType_COBBLE2,
BGType_COBBLE3,
BGType_PLATINUM_ORE,
BGType_GOLD_ORE,
BGType_SILVER_ORE,
BGType_IRON_ORE,
BGType_SILVER_ORE,
BGType_GOLD_ORE,
BGType_PLATINUM_ORE,
BGType_END
} BackgroundType;

View File

@@ -5,11 +5,13 @@
#include "tilecallbacks.h"
#include "furnace.h"
#include "miner.h"
#include "turret.h"
const UpdateTileCallback ItemTileCallbacks[TILEREGISTRY_SIZE] = {
[TYPE_AIR] = NULL,
[TYPE_BLOCK] = NULL,
[TYPE_BELT] = NULL,
[TYPE_FURNACE] = updateFurnace,
[TYPE_MINER] = updateMiner
[TYPE_MINER] = updateMiner,
[TYPE_TURRET] = updateTurret,
};

61
tiles/turret.c Normal file
View File

@@ -0,0 +1,61 @@
//
// Created by bruno on 6.9.2025.
//
#include "turret.h"
#include "tile.h"
#include "../util/audio.h"
#include "../entity/entity.h"
const uint16_t AmmoDamages[ITEMREGISTRY_SIZE] = {
[IRON_INGOT] = 1
};
void updateTurret(Tile *tile) {
ItemOnBelt *inItem = &tile->items[TURRET_AMMO_INPUT_SLOT];
Item inItemType = ItemRegistry[inItem->type];
uint16_t damage = AmmoDamages[inItem->type];
if (damage > 0) {
bool foundEnt = false;
for (int i = 0; i < entities.activeCount; i++) {
Entity *ent = &entities.entities[i];
int dx = abs(ent->renderRect.x - (tile->rect.x * TILE_SIZE));
int dy = abs(ent->renderRect.y - (tile->rect.y * TILE_SIZE));
int d = sqrt(pow(dx, 2) + pow(dy, 2));
if (d <= (TILE_SIZE * 8)) {
ent->health -= damage;
inItem->type = 0;
tile->audioCh = getAvailableChannel();
if (tile->audioCh < NUM_SYNTH_VOICES) {
audioData.synthVoices[tile->audioCh].volume = 255;
audioData.synthVoices[tile->audioCh].phase = 0;
audioData.synthVoices[tile->audioCh].sourceRect.x = TILE_SIZE * tile->rect.x;
audioData.synthVoices[tile->audioCh].sourceRect.y = TILE_SIZE * tile->rect.y;
audioData.synthVoices[tile->audioCh].waveform = WAVE_TRIANGLE;
audioData.synthVoices[tile->audioCh].frequency = 400;
}
tile->fixedFrame = 0;
foundEnt = true;
break;
}
}
if (!foundEnt) {
audioData.synthVoices[tile->audioCh].volume = 0;
tile->fixedFrame = 1;
}
} else {
if (tile->audioCh < NUM_SYNTH_VOICES) {
audioData.synthVoices[tile->audioCh].volume = 0;
}
tile->fixedFrame = 1;
return;
}
if (audioData.synthVoices[tile->audioCh].frequency > 80) {
audioData.synthVoices[tile->audioCh].frequency--;
}
}

17
tiles/turret.h Normal file
View File

@@ -0,0 +1,17 @@
//
// Created by bruno on 6/9/25.
//
#ifndef FACTORYGAME_TURRET_H
#define FACTORYGAME_TURRET_H
#include "../items/item.h"
#include "stdint.h"
extern const uint16_t AmmoDamages[];
#define TURRET_AMMO_INPUT_SLOT 0
void updateTurret(Tile * tile);
#endif //FACTORYGAME_TURRET_H