Raycaster now controls the sprite locations with SpatialMap rather than the old way. Quick hack job in main.cpp that shows how they can move too.

This commit is contained in:
Zed A. Shaw 2025-02-01 14:39:08 -05:00
parent a67d25ee10
commit cbf0955786
11 changed files with 93 additions and 65 deletions

View file

@ -34,8 +34,6 @@ Raycaster::Raycaster(sf::RenderWindow& window, TexturePack &textures, Matrix &ma
$width(width), $height(height),
$window(window),
$map(map),
spriteOrder(NUM_SPRITES),
spriteDistance(NUM_SPRITES),
ZBuffer(width),
$anim(256, 256, 10, "assets/monster-1.ogg")
{
@ -77,22 +75,11 @@ void Raycaster::sprite_casting() {
const int halfHeight = TEXTURE_HEIGHT / 2;
// sort sprites from far to close
for(int i = 0; i < NUM_SPRITES; i++) {
auto& sprite = $textures.get_sprite(i);
spriteOrder[i] = i;
// this is just the distance calculation
spriteDistance[i] = (($posX - sprite.x) *
($posX - sprite.x) +
($posY - sprite.y) *
($posY - sprite.y));
}
sort_sprites(spriteOrder, spriteDistance, NUM_SPRITES);
auto sprite_order = $collision.distance_sorted({(size_t)$posX, (size_t)$posY});
// after sorting the sprites, do the projection
for(int i = 0; i < NUM_SPRITES; i++) {
int sprite_index = spriteOrder[i];
Sprite& sprite_rec = $textures.get_sprite(sprite_index);
for(auto& rec : sprite_order) {
Sprite& sprite_rec = $sprites[rec.second];
// TODO: this must die
auto sf_sprite = sprite_rec.sprite.sprite;
@ -317,13 +304,15 @@ void Raycaster::draw_ceiling_floor() {
int cellX = int(floorX);
int cellY = int(floorY);
// get the texture coordinat from the fractional part
// get the texture coordinate from the fractional part
int tx = int(textureWidth * (floorX - cellX)) & (textureWidth - 1);
int ty = int(textureWidth * (floorY - cellY)) & (textureHeight - 1);
floorX += floorStepX;
floorY += floorStepY;
double d = std::sqrt(($posX - floorX) * ($posX - floorX) + ($posY - floorY) * ($posY - floorY));
// now get the pixel from the texture
uint32_t color;
// this uses the previous ty/tx fractional parts of
@ -331,11 +320,11 @@ void Raycaster::draw_ceiling_floor() {
// FLOOR
color = floor_texture[textureWidth * ty + tx];
$pixels[pixcoord(x, y)] = color;
$pixels[pixcoord(x, y)] = dumb_lighting(color, d);
// CEILING
color = ceiling_texture[textureWidth * ty + tx];
$pixels[pixcoord(x, $height - y - 1)] = color;
$pixels[pixcoord(x, $height - y - 1)] = dumb_lighting(color, d);
}
}
}
@ -357,24 +346,6 @@ bool Raycaster::empty_space(int new_x, int new_y) {
}
void Raycaster::sort_sprites(std::vector<int>& order, std::vector<double>& dist, int amount)
{
std::vector<std::pair<double, int>> sprites(amount);
for(int i = 0; i < amount; i++) {
sprites[i].first = dist[i];
sprites[i].second = order[i];
}
std::sort(sprites.begin(), sprites.end());
// restore in reverse order
for(int i = 0; i < amount; i++) {
dist[i] = sprites[amount - i - 1].first;
order[i] = sprites[amount - i - 1].second;
}
}
void Raycaster::run(double speed, int dir) {
double speed_and_dir = speed * dir;
if(empty_space(int($posX + $dirX * speed_and_dir), int($posY))) {
@ -396,3 +367,12 @@ void Raycaster::rotate(double speed, int dir) {
$planeX = $planeX * cos(speed_and_dir) - $planeY * sin(speed_and_dir);
$planeY = oldPlaneX * sin(speed_and_dir) + $planeY * cos(speed_and_dir);
}
DinkyECS::Entity Raycaster::position_sprite(Point pos, string name) {
auto sprite_txt = $textures.sprite_textures[name];
$sprites.emplace_back(pos.x, pos.y, sprite_txt);
DinkyECS::Entity ent = $sprites.size() - 1;
$collision.insert({pos.x, pos.y}, ent);
return ent;
}