batch sprite 1

This commit is contained in:
StephenAdamson
2025-02-08 00:23:49 +00:00
parent 4089851734
commit 4ba6fecc27
6 changed files with 151 additions and 106 deletions
+111 -60
View File
@@ -1,47 +1,133 @@
use std::process::exit;
use crate::constants::*;
use crate::tile::{FixtureTile, FloorTile, NeedsOccluded, TileState};
use bevy::prelude::*;
use bevy::utils::HashMap;
use bevy::{prelude::*, transform};
#[derive(Resource)]
pub struct Textures {
pub handles: HashMap<String, Handle<Image>>,
}
#[derive(Resource)]
pub struct TextureIDs {
pub refs: HashMap<u32, String>,
}
const FLOOR_ID_OFFSET: u32 = 0;
const FIXTURE_ID_OFFSET: u32 = 500000;
pub const DEFAULT_TEXTURE: &str = "default.png";
const GRASS_FLOOR_PATH: &str = "grass_floor.png";
const DIRT_FLOOR_PATH: &str = "dirt_floor.png";
const ROCK_FLOOR_PATH: &str = "rock_floor.png";
const BEDROCK_FLOOR_PATH: &str = "bedrock_floor.png";
const SKY_PATH: &str = "sky.png";
const DIRT_WALL_PATH: &str = "dirt_wall.png";
const ROCK_WALL_PATH: &str = "rock_wall.png";
const BEDROCK_WALL_PATH: &str = "bedrock_wall.png";
pub fn initialize_textures(mut commands: Commands, asset_server: Res<AssetServer>) {
let mut textures = HashMap::new();
let mut textures: HashMap<String, Handle<Image>> = HashMap::new();
let mut texture_ids: HashMap<u32, String> = HashMap::new();
texture_ids.insert(u32::MAX, DEFAULT_TEXTURE.to_string());
texture_ids.insert(FLOOR_ID_OFFSET + 0, SKY_PATH.to_string());
textures.insert(SKY_PATH.to_string(), asset_server.load(SKY_PATH));
texture_ids.insert(FLOOR_ID_OFFSET + 1, GRASS_FLOOR_PATH.to_string());
textures.insert(
"grass_floor".to_string(),
asset_server.load("grass_floor.png"),
GRASS_FLOOR_PATH.to_string(),
asset_server.load(GRASS_FLOOR_PATH),
);
texture_ids.insert(FLOOR_ID_OFFSET + 2, DIRT_FLOOR_PATH.to_string());
textures.insert(
"dirt_floor".to_string(),
asset_server.load("dirt_floor.png"),
DIRT_FLOOR_PATH.to_string(),
asset_server.load(DIRT_FLOOR_PATH),
);
texture_ids.insert(FLOOR_ID_OFFSET + 3, ROCK_FLOOR_PATH.to_string());
textures.insert(
"rock_floor".to_string(),
asset_server.load("rock_floor.png"),
ROCK_FLOOR_PATH.to_string(),
asset_server.load(ROCK_FLOOR_PATH),
);
texture_ids.insert(FLOOR_ID_OFFSET + 4, BEDROCK_FLOOR_PATH.to_string());
textures.insert(
"bedrock_floor".to_string(),
asset_server.load("bedrock_floor.png"),
BEDROCK_FLOOR_PATH.to_string(),
asset_server.load(BEDROCK_FLOOR_PATH),
);
textures.insert("sky".to_string(), asset_server.load("sky.png"));
textures.insert("dirt_wall".to_string(), asset_server.load("dirt_wall.png"));
textures.insert("rock_wall".to_string(), asset_server.load("rock_wall.png"));
texture_ids.insert(FIXTURE_ID_OFFSET + 1, DIRT_WALL_PATH.to_string());
textures.insert(
"bedrock_wall".to_string(),
asset_server.load("bedrock_wall.png"),
DIRT_WALL_PATH.to_string(),
asset_server.load(DIRT_WALL_PATH),
);
texture_ids.insert(FIXTURE_ID_OFFSET + 2, ROCK_WALL_PATH.to_string());
textures.insert(
ROCK_WALL_PATH.to_string(),
asset_server.load(ROCK_WALL_PATH),
);
texture_ids.insert(FIXTURE_ID_OFFSET + 3, BEDROCK_WALL_PATH.to_string());
textures.insert(
BEDROCK_WALL_PATH.to_string(),
asset_server.load(BEDROCK_WALL_PATH),
);
// Insert the textures resource into the world
commands.insert_resource(Textures { handles: textures });
commands.insert_resource(TextureIDs { refs: texture_ids });
}
#[derive(Debug, PartialEq)]
pub enum WorldSpriteState {
Inactive,
WaitingForRender,
InProgress,
RenderReady,
}
#[derive(Resource)]
pub struct CurrentWorldSpriteState {
pub state: WorldSpriteState,
}
use std::time::Instant;
pub fn build_world_sprites(
mut query: Query<(&FloorTile, &Transform)>,
mut commands: Commands,
mut cwss: ResMut<CurrentWorldSpriteState>,
textures: Res<Textures>,
texture_ids: Res<TextureIDs>,
) {
if cwss.state != WorldSpriteState::WaitingForRender {
return;
}
let now = Instant::now();
if cwss.state == WorldSpriteState::WaitingForRender {
cwss.state = WorldSpriteState::InProgress;
// async/thread this so we don't block the main thread
for (floortile, transform) in query.iter_mut() {
let id = floortile.id;
let texture_id = texture_ids.refs.get(&id).unwrap();
let texture = textures.handles.get(texture_id).unwrap();
let sprite = Sprite {
image: texture.clone(),
..Default::default()
};
commands.spawn((sprite, *transform));
}
cwss.state = WorldSpriteState::RenderReady;
}
println!("{:?}", cwss.state);
let elapsed = now.elapsed();
println!("Elapsed: {:.2?}", elapsed);
// exit(0);
}
#[derive(Bundle)]
pub struct FloorTilePrefab {
transform: Transform,
sprite: Sprite,
tile: FloorTile,
tile_state: TileState,
visibility: Visibility,
@@ -49,13 +135,9 @@ pub struct FloorTilePrefab {
}
impl FloorTilePrefab {
pub fn grass(position: Vec3, textures: &Res<Textures>) -> Self {
pub fn grass(position: Vec3) -> Self {
FloorTilePrefab {
transform: Transform::from_translation(position).with_scale(Vec3::splat(PIXEL_RATIO)),
sprite: Sprite {
image: textures.handles.get("grass_floor").unwrap().clone(),
..Default::default()
},
tile: FloorTile {
id: 1,
astar_weight: 100,
@@ -71,13 +153,9 @@ impl FloorTilePrefab {
}
}
pub fn dirt(position: Vec3, textures: &Res<Textures>) -> Self {
pub fn dirt(position: Vec3) -> Self {
FloorTilePrefab {
transform: Transform::from_translation(position).with_scale(Vec3::splat(PIXEL_RATIO)),
sprite: Sprite {
image: textures.handles.get("dirt_floor").unwrap().clone(),
..Default::default()
},
tile: FloorTile {
id: 2,
astar_weight: 85,
@@ -93,13 +171,9 @@ impl FloorTilePrefab {
}
}
pub fn rock(position: Vec3, textures: &Res<Textures>) -> Self {
pub fn rock(position: Vec3) -> Self {
FloorTilePrefab {
transform: Transform::from_translation(position).with_scale(Vec3::splat(PIXEL_RATIO)),
sprite: Sprite {
image: textures.handles.get("rock_floor").unwrap().clone(),
..Default::default()
},
tile: FloorTile {
id: 3,
astar_weight: 50,
@@ -115,13 +189,9 @@ impl FloorTilePrefab {
}
}
pub fn air(position: Vec3, textures: &Res<Textures>) -> Self {
pub fn air(position: Vec3) -> Self {
FloorTilePrefab {
transform: Transform::from_translation(position).with_scale(Vec3::splat(PIXEL_RATIO)),
sprite: Sprite {
image: textures.handles.get("sky").unwrap().clone(),
..Default::default()
},
tile: FloorTile {
id: 0,
opaque: false,
@@ -138,13 +208,9 @@ impl FloorTilePrefab {
}
}
pub fn bedrock(position: Vec3, textures: &Res<Textures>) -> Self {
pub fn bedrock(position: Vec3) -> Self {
FloorTilePrefab {
transform: Transform::from_translation(position).with_scale(Vec3::splat(PIXEL_RATIO)),
sprite: Sprite {
image: textures.handles.get("bedrock_floor").unwrap().clone(),
..Default::default()
},
tile: FloorTile {
id: 4,
astar_weight: 150,
@@ -164,7 +230,6 @@ impl FloorTilePrefab {
commands.spawn((
self.tile,
self.transform,
self.sprite,
self.tile_state,
self.visibility,
self.needs_occluded,
@@ -175,22 +240,17 @@ impl FloorTilePrefab {
#[derive(Bundle)]
pub struct FixtureTilePrefab {
transform: Transform,
sprite: Sprite,
tile: FixtureTile,
visibility: Visibility,
needs_occluded: NeedsOccluded,
}
impl FixtureTilePrefab {
pub fn dirt_wall(position: Vec3, textures: &Res<Textures>) -> Self {
pub fn dirt_wall(position: Vec3) -> Self {
FixtureTilePrefab {
transform: Transform::from_translation(position).with_scale(Vec3::splat(PIXEL_RATIO)),
sprite: Sprite {
image: textures.handles.get("dirt_wall").unwrap().clone(),
..Default::default()
},
tile: FixtureTile {
id: 1,
id: FIXTURE_ID_OFFSET + 1,
..Default::default()
},
visibility: Visibility::Hidden,
@@ -200,13 +260,9 @@ impl FixtureTilePrefab {
}
}
pub fn rock_wall(position: Vec3, textures: &Res<Textures>) -> Self {
pub fn rock_wall(position: Vec3) -> Self {
FixtureTilePrefab {
transform: Transform::from_translation(position).with_scale(Vec3::splat(PIXEL_RATIO)),
sprite: Sprite {
image: textures.handles.get("rock_wall").unwrap().clone(),
..Default::default()
},
tile: FixtureTile {
id: 2,
..Default::default()
@@ -218,13 +274,9 @@ impl FixtureTilePrefab {
}
}
pub fn bedrock_wall(position: Vec3, textures: &Res<Textures>) -> Self {
pub fn bedrock_wall(position: Vec3) -> Self {
FixtureTilePrefab {
transform: Transform::from_translation(position).with_scale(Vec3::splat(PIXEL_RATIO)),
sprite: Sprite {
image: textures.handles.get("bedrock_wall").unwrap().clone(),
..Default::default()
},
tile: FixtureTile {
id: 0,
..Default::default()
@@ -240,7 +292,6 @@ impl FixtureTilePrefab {
commands.spawn((
self.tile,
self.transform,
self.sprite,
self.visibility,
self.needs_occluded,
));