Singular tiles resource
This commit is contained in:
+52
-16
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user