249 lines
7.8 KiB
Rust
249 lines
7.8 KiB
Rust
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 {
|
|
transform: Transform,
|
|
sprite: Sprite,
|
|
tile: FloorTile,
|
|
tile_state: TileState,
|
|
visibility: Visibility,
|
|
needs_occluded: NeedsOccluded,
|
|
}
|
|
|
|
impl FloorTilePrefab {
|
|
pub fn grass(position: Vec3, textures: &Res<Textures>) -> 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,
|
|
..Default::default()
|
|
},
|
|
tile_state: TileState {
|
|
timer: Timer::from_seconds(1.0, TimerMode::Repeating),
|
|
},
|
|
visibility: Visibility::Hidden,
|
|
needs_occluded: NeedsOccluded {
|
|
has_been_occluded: false,
|
|
},
|
|
}
|
|
}
|
|
|
|
pub fn dirt(position: Vec3, textures: &Res<Textures>) -> 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,
|
|
..Default::default()
|
|
},
|
|
tile_state: TileState {
|
|
timer: Timer::from_seconds(1.0, TimerMode::Repeating),
|
|
},
|
|
visibility: Visibility::Hidden,
|
|
needs_occluded: NeedsOccluded {
|
|
has_been_occluded: false,
|
|
},
|
|
}
|
|
}
|
|
|
|
pub fn rock(position: Vec3, textures: &Res<Textures>) -> 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,
|
|
..Default::default()
|
|
},
|
|
tile_state: TileState {
|
|
timer: Timer::from_seconds(1.0, TimerMode::Repeating),
|
|
},
|
|
visibility: Visibility::Hidden,
|
|
needs_occluded: NeedsOccluded {
|
|
has_been_occluded: false,
|
|
},
|
|
}
|
|
}
|
|
|
|
pub fn air(position: Vec3, textures: &Res<Textures>) -> 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,
|
|
walkable: false,
|
|
..Default::default()
|
|
},
|
|
tile_state: TileState {
|
|
timer: Timer::from_seconds(1.0, TimerMode::Repeating),
|
|
},
|
|
visibility: Visibility::Hidden,
|
|
needs_occluded: NeedsOccluded {
|
|
has_been_occluded: false,
|
|
},
|
|
}
|
|
}
|
|
|
|
pub fn bedrock(position: Vec3, textures: &Res<Textures>) -> 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,
|
|
..Default::default()
|
|
},
|
|
tile_state: TileState {
|
|
timer: Timer::from_seconds(1.0, TimerMode::Repeating),
|
|
},
|
|
visibility: Visibility::Hidden,
|
|
needs_occluded: NeedsOccluded {
|
|
has_been_occluded: false,
|
|
},
|
|
}
|
|
}
|
|
|
|
pub fn spawn(self, commands: &mut Commands) {
|
|
commands.spawn((
|
|
self.tile,
|
|
self.transform,
|
|
self.sprite,
|
|
self.tile_state,
|
|
self.visibility,
|
|
self.needs_occluded,
|
|
));
|
|
}
|
|
}
|
|
|
|
#[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 {
|
|
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,
|
|
..Default::default()
|
|
},
|
|
visibility: Visibility::Hidden,
|
|
needs_occluded: NeedsOccluded {
|
|
has_been_occluded: false,
|
|
},
|
|
}
|
|
}
|
|
|
|
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: textures.handles.get("rock_wall").unwrap().clone(),
|
|
..Default::default()
|
|
},
|
|
tile: FixtureTile {
|
|
id: 2,
|
|
..Default::default()
|
|
},
|
|
visibility: Visibility::Hidden,
|
|
needs_occluded: NeedsOccluded {
|
|
has_been_occluded: false,
|
|
},
|
|
}
|
|
}
|
|
|
|
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: textures.handles.get("bedrock_wall").unwrap().clone(),
|
|
..Default::default()
|
|
},
|
|
tile: FixtureTile {
|
|
id: 0,
|
|
..Default::default()
|
|
},
|
|
visibility: Visibility::Hidden,
|
|
needs_occluded: NeedsOccluded {
|
|
has_been_occluded: false,
|
|
},
|
|
}
|
|
}
|
|
|
|
pub fn spawn(self, commands: &mut Commands) {
|
|
commands.spawn((
|
|
self.tile,
|
|
self.transform,
|
|
self.sprite,
|
|
self.visibility,
|
|
self.needs_occluded,
|
|
));
|
|
}
|
|
}
|