61 lines
2.0 KiB
C
61 lines
2.0 KiB
C
//
|
|
// 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--;
|
|
}
|
|
} |