Singular tiles resource

This commit is contained in:
StephenAdamson
2025-02-01 01:07:39 +00:00
parent 501c66a1b0
commit 6161c21e22
4 changed files with 69 additions and 34 deletions
+52 -16
View File
@@ -1,6 +1,42 @@
use crate::constants::*;
use crate::tile::{FixtureTile, FloorTile, NeedsOccluded, TileState};
use bevy::prelude::*;
use bevy::utils::HashMap;
#[derive(Resource)]
pub struct Textures {
pub handles: HashMap<String, Handle<Image>>,
}
pub fn initialize_textures(mut commands: Commands, asset_server: Res<AssetServer>) {
let mut textures = HashMap::new();
textures.insert(
"grass_floor".to_string(),
asset_server.load("grass_floor.png"),
);
textures.insert(
"dirt_floor".to_string(),
asset_server.load("dirt_floor.png"),
);
textures.insert(
"rock_floor".to_string(),
asset_server.load("rock_floor.png"),
);
textures.insert(
"bedrock_floor".to_string(),
asset_server.load("bedrock_floor.png"),
);
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"));
textures.insert(
"bedrock_wall".to_string(),
asset_server.load("bedrock_wall.png"),
);
// Insert the textures resource into the world
commands.insert_resource(Textures { handles: textures });
}
#[derive(Bundle)]
pub struct FloorTilePrefab {
@@ -13,11 +49,11 @@ pub struct FloorTilePrefab {
}
impl FloorTilePrefab {
pub fn grass(position: Vec3, asset_server: &Res<AssetServer>) -> Self {
pub fn grass(position: Vec3, textures: &Res<Textures>) -> Self {
FloorTilePrefab {
transform: Transform::from_translation(position).with_scale(Vec3::splat(PIXEL_RATIO)),
sprite: Sprite {
image: asset_server.load("grass_floor.png"),
image: textures.handles.get("grass_floor").unwrap().clone(),
..Default::default()
},
tile: FloorTile {
@@ -35,11 +71,11 @@ impl FloorTilePrefab {
}
}
pub fn dirt(position: Vec3, asset_server: &Res<AssetServer>) -> Self {
pub fn dirt(position: Vec3, textures: &Res<Textures>) -> Self {
FloorTilePrefab {
transform: Transform::from_translation(position).with_scale(Vec3::splat(PIXEL_RATIO)),
sprite: Sprite {
image: asset_server.load("dirt_floor.png"),
image: textures.handles.get("dirt_floor").unwrap().clone(),
..Default::default()
},
tile: FloorTile {
@@ -57,11 +93,11 @@ impl FloorTilePrefab {
}
}
pub fn rock(position: Vec3, asset_server: &Res<AssetServer>) -> Self {
pub fn rock(position: Vec3, textures: &Res<Textures>) -> Self {
FloorTilePrefab {
transform: Transform::from_translation(position).with_scale(Vec3::splat(PIXEL_RATIO)),
sprite: Sprite {
image: asset_server.load("rock_floor.png"),
image: textures.handles.get("rock_floor").unwrap().clone(),
..Default::default()
},
tile: FloorTile {
@@ -79,11 +115,11 @@ impl FloorTilePrefab {
}
}
pub fn air(position: Vec3, asset_server: &Res<AssetServer>) -> Self {
pub fn air(position: Vec3, textures: &Res<Textures>) -> Self {
FloorTilePrefab {
transform: Transform::from_translation(position).with_scale(Vec3::splat(PIXEL_RATIO)),
sprite: Sprite {
image: asset_server.load("sky.png"),
image: textures.handles.get("sky").unwrap().clone(),
..Default::default()
},
tile: FloorTile {
@@ -102,11 +138,11 @@ impl FloorTilePrefab {
}
}
pub fn bedrock(position: Vec3, asset_server: &Res<AssetServer>) -> Self {
pub fn bedrock(position: Vec3, textures: &Res<Textures>) -> Self {
FloorTilePrefab {
transform: Transform::from_translation(position).with_scale(Vec3::splat(PIXEL_RATIO)),
sprite: Sprite {
image: asset_server.load("bedrock_floor.png"),
image: textures.handles.get("bedrock_floor").unwrap().clone(),
..Default::default()
},
tile: FloorTile {
@@ -146,11 +182,11 @@ pub struct FixtureTilePrefab {
}
impl FixtureTilePrefab {
pub fn dirt_wall(position: Vec3, asset_server: &Res<AssetServer>) -> Self {
pub fn dirt_wall(position: Vec3, textures: &Res<Textures>) -> Self {
FixtureTilePrefab {
transform: Transform::from_translation(position).with_scale(Vec3::splat(PIXEL_RATIO)),
sprite: Sprite {
image: asset_server.load("dirt_wall.png"),
image: textures.handles.get("dirt_wall").unwrap().clone(),
..Default::default()
},
tile: FixtureTile {
@@ -164,11 +200,11 @@ impl FixtureTilePrefab {
}
}
pub fn rock_wall(position: Vec3, asset_server: &Res<AssetServer>) -> Self {
pub fn rock_wall(position: Vec3, textures: &Res<Textures>) -> Self {
FixtureTilePrefab {
transform: Transform::from_translation(position).with_scale(Vec3::splat(PIXEL_RATIO)),
sprite: Sprite {
image: asset_server.load("rock_wall.png"),
image: textures.handles.get("rock_wall").unwrap().clone(),
..Default::default()
},
tile: FixtureTile {
@@ -182,11 +218,11 @@ impl FixtureTilePrefab {
}
}
pub fn bedrock_wall(position: Vec3, asset_server: &Res<AssetServer>) -> Self {
pub fn bedrock_wall(position: Vec3, textures: &Res<Textures>) -> Self {
FixtureTilePrefab {
transform: Transform::from_translation(position).with_scale(Vec3::splat(PIXEL_RATIO)),
sprite: Sprite {
image: asset_server.load("bedrock_wall.png"),
image: textures.handles.get("bedrock_wall").unwrap().clone(),
..Default::default()
},
tile: FixtureTile {