Now the spatialmap determines the 'wiggle factor' when there's multiple entities in a cell, which staggers them visually. Closes #78.

This commit is contained in:
Zed A. Shaw 2025-08-03 01:56:34 -04:00
parent 694ee210d6
commit 9c02fb846b
4 changed files with 35 additions and 21 deletions

View file

@ -107,19 +107,19 @@ void Raycaster::sprite_casting(sf::RenderTarget &target) {
// after sorting the sprites, do the projection
for(auto& rec : sprite_order) {
if(!$sprites.contains(rec.second)) continue;
if(!$sprites.contains(rec.entity)) continue;
auto& sprite_texture = $sprites.at(rec.second);
auto& sprite_texture = $sprites.at(rec.entity);
int texture_width = (float)sprite_texture.frame_size.x;
int texture_height =(float)sprite_texture.frame_size.y;
int half_height = texture_height / 2;
auto& sf_sprite = sprite_texture.sprite;
auto sprite_pos = $level.world->get<components::Position>(rec.second);
auto sprite_pos = $level.world->get<components::Position>(rec.entity);
double sprite_x = double(sprite_pos.location.x) - $pos_x + 0.5;
double sprite_y = double(sprite_pos.location.y) - $pos_y + 0.5;
double sprite_x = double(sprite_pos.location.x) - rec.wiggle - $pos_x + 0.5;
double sprite_y = double(sprite_pos.location.y) - rec.wiggle - $pos_y + 0.5;
double inv_det = 1.0 / ($plane_x * $dir_y - $dir_x * $plane_y); // required for correct matrix multiplication
@ -185,8 +185,8 @@ void Raycaster::sprite_casting(sf::RenderTarget &target) {
sf::Vector2f position{x + origin.x * scale.x, y + origin.y * scale.y};
sf::IntRect in_texture{ {tex_x, tex_y}, {tex_render_width, texture_height}};
if($level.world->has<components::Animation>(rec.second)) {
auto& animation = $level.world->get<components::Animation>(rec.second);
if($level.world->has<components::Animation>(rec.entity)) {
auto& animation = $level.world->get<components::Animation>(rec.entity);
if(animation.playing) animation.step(scale, position, in_texture);
}
@ -195,13 +195,9 @@ void Raycaster::sprite_casting(sf::RenderTarget &target) {
sf_sprite->setTextureRect(in_texture);
sf_sprite->setPosition(position);
// the SpatialMap.distance_sorted only calculates the
// (x1-x2)^2 + (y1-y2)^2 portion of distance, so to get
// the actual distance we need to sqrt that.
// float level = sqrt(rec.first);
float level = lights[sprite_pos.location.y][sprite_pos.location.x] * PERCENT;
shared_ptr<sf::Shader> effect = System::sprite_effect($level, rec.second);
shared_ptr<sf::Shader> effect = System::sprite_effect($level, rec.entity);
if(effect) {
apply_sprite_effect(effect, sprite_width, sprite_height);