-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathtilemap.rs
More file actions
106 lines (94 loc) · 3.06 KB
/
tilemap.rs
File metadata and controls
106 lines (94 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
use std::{
fs::File,
io::{BufRead, BufReader},
};
use bevy::prelude::*;
use crate::{
ascii::{spawn_ascii_sprite, AsciiSheet},
npc::Npc,
GameState, TILE_SIZE,
};
pub struct TileMapPlugin;
#[derive(Component)]
struct Map;
#[derive(Component)]
pub struct EncounterSpawner;
#[derive(Component)]
pub struct TileCollider;
impl Plugin for TileMapPlugin {
fn build(&self, app: &mut App) {
app.add_system_set(
SystemSet::on_enter(GameState::Overworld).with_system(create_simple_map),
)
.add_system_set(SystemSet::on_resume(GameState::Overworld).with_system(show_map))
.add_system_set(SystemSet::on_pause(GameState::Overworld).with_system(hide_map));
}
}
fn hide_map(
children_query: Query<&Children, With<Map>>,
mut child_visibility_query: Query<&mut Visibility, Without<Map>>,
) {
if let Ok(children) = children_query.get_single() {
for child in children.iter() {
if let Ok(mut child_vis) = child_visibility_query.get_mut(*child) {
child_vis.is_visible = false;
}
}
}
}
fn show_map(
children_query: Query<&Children, With<Map>>,
mut child_visibility_query: Query<&mut Visibility, Without<Map>>,
) {
if let Ok(children) = children_query.get_single() {
for child in children.iter() {
if let Ok(mut child_vis) = child_visibility_query.get_mut(*child) {
child_vis.is_visible = true;
}
}
}
}
fn create_simple_map(mut commands: Commands, ascii: Res<AsciiSheet>) {
let file = File::open("assets/map.txt").expect("No map file found");
let mut tiles = Vec::new();
for (y, line) in BufReader::new(file).lines().enumerate() {
if let Ok(line) = line {
for (x, char) in line.chars().enumerate() {
let color = match char {
'#' => Color::rgb(0.7, 0.7, 0.7),
'@' => Color::rgb(0.5, 0.5, 0.2),
'~' => Color::rgb(0.2, 0.9, 0.2),
_ => Color::rgb(0.9, 0.9, 0.9),
};
let tile = spawn_ascii_sprite(
&mut commands,
&ascii,
char as usize,
color,
Vec3::new(x as f32 * TILE_SIZE, -(y as f32) * TILE_SIZE, 100.0),
Vec3::splat(1.0),
);
if char == '#' {
commands.entity(tile).insert(TileCollider);
}
if char == '@' {
commands
.entity(tile)
.insert(Npc::Healer)
.insert(TileCollider);
}
if char == '~' {
commands.entity(tile).insert(EncounterSpawner);
}
tiles.push(tile);
}
}
}
commands
.spawn()
.insert(Map)
.insert(Name::new("Map"))
.insert(Transform::default())
.insert(GlobalTransform::default())
.push_children(&tiles);
}