This repository has been archived by the owner on Apr 18, 2022. It is now read-only.
/
main.rs
178 lines (150 loc) · 5.79 KB
/
main.rs
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//! Displays several lines with both methods.
extern crate amethyst;
use amethyst::{
controls::{FlyControlBundle, FlyControlTag},
core::Time,
core::{
nalgebra::{Point3, Vector3},
transform::{Transform, TransformBundle},
},
ecs::{Read, System, Write},
input::InputBundle,
prelude::*,
renderer::*,
utils::application_root_dir,
};
struct ExampleLinesSystem;
impl<'s> System<'s> for ExampleLinesSystem {
type SystemData = (
Write<'s, DebugLines>, // Request DebugLines resource
Read<'s, Time>,
);
fn run(&mut self, (mut debug_lines_resource, time): Self::SystemData) {
// Drawing debug lines, as a resource
{
let t = (time.absolute_time_seconds() as f32).cos();
debug_lines_resource.draw_direction(
[t, 0.0, 0.5].into(),
[0.0, 0.3, 0.0].into(),
[0.5, 0.05, 0.65, 1.0].into(),
);
debug_lines_resource.draw_line(
[t, 0.0, 0.5].into(),
[0.0, 0.0, 0.2].into(),
[0.5, 0.05, 0.65, 1.0].into(),
);
}
}
}
struct ExampleState;
impl SimpleState for ExampleState {
fn on_start(&mut self, data: StateData<GameData>) {
// Setup debug lines as a resource
data.world
.add_resource(DebugLines::new().with_capacity(100));
// Configure width of lines. Optional step
data.world.add_resource(DebugLinesParams {
line_width: 1.0 / 400.0,
});
// Setup debug lines as a component and add lines to render axis&grid
let mut debug_lines_component = DebugLinesComponent::new().with_capacity(100);
debug_lines_component.add_direction(
[0.0, 0.0001, 0.0].into(),
[0.2, 0.0, 0.0].into(),
[1.0, 0.0, 0.23, 1.0].into(),
);
debug_lines_component.add_direction(
[0.0, 0.0, 0.0].into(),
[0.0, 0.2, 0.0].into(),
[0.5, 0.85, 0.1, 1.0].into(),
);
debug_lines_component.add_direction(
[0.0, 0.0001, 0.0].into(),
[0.0, 0.0, 0.2].into(),
[0.2, 0.75, 0.93, 1.0].into(),
);
let width: u32 = 10;
let depth: u32 = 10;
let main_color = [0.4, 0.4, 0.4, 1.0].into();
// Grid lines in X-axis
for x in 0..=width {
let (x, width, depth) = (x as f32, width as f32, depth as f32);
let position = Point3::new(x - width / 2.0, 0.0, -depth / 2.0);
let direction = Vector3::new(0.0, 0.0, depth);
debug_lines_component.add_direction(position, direction, main_color);
// Sub-grid lines
if x != width {
for sub_x in 1..10 {
let sub_offset = Vector3::new((1.0 / 10.0) * sub_x as f32, -0.001, 0.0);
debug_lines_component.add_direction(
position + sub_offset,
direction,
[0.1, 0.1, 0.1, 0.1].into(),
);
}
}
}
// Grid lines in Z-axis
for z in 0..=depth {
let (z, width, depth) = (z as f32, width as f32, depth as f32);
let position = Point3::new(-width / 2.0, 0.0, z - depth / 2.0);
let direction = Vector3::new(width, 0.0, 0.0);
debug_lines_component.add_direction(position, direction, main_color);
// Sub-grid lines
if z != depth {
for sub_z in 1..10 {
let sub_offset = Vector3::new(0.0, -0.001, (1.0 / 10.0) * sub_z as f32);
debug_lines_component.add_direction(
position + sub_offset,
direction,
[0.1, 0.1, 0.1, 0.0].into(),
);
}
}
}
data.world.register::<DebugLinesComponent>();
data.world
.create_entity()
.with(debug_lines_component)
.build();
// Setup camera
let mut local_transform = Transform::default();
local_transform.set_position([0.0, 0.5, 2.0].into());
data.world
.create_entity()
.with(FlyControlTag)
.with(Camera::from(Projection::perspective(
1.33333,
std::f32::consts::FRAC_PI_2,
))).with(local_transform)
.build();
}
}
fn main() -> amethyst::Result<()> {
amethyst::start_logger(Default::default());
let app_root = application_root_dir();
let display_config_path = format!("{}/examples/debug_lines/resources/display.ron", app_root);
let key_bindings_path = format!("{}/examples/debug_lines/resources/input.ron", app_root);
let resources = format!("{}/examples/assets/", app_root);
let pipe = Pipeline::build().with_stage(
Stage::with_backbuffer()
.clear_target([0.001, 0.005, 0.005, 1.0], 1.0)
.with_pass(DrawDebugLines::<PosColorNorm>::new()),
);
let config = DisplayConfig::load(display_config_path);
let fly_control_bundle = FlyControlBundle::<String, String>::new(
Some(String::from("move_x")),
Some(String::from("move_y")),
Some(String::from("move_z")),
).with_sensitivity(0.1, 0.1);
let game_data = GameDataBuilder::default()
.with_bundle(
InputBundle::<String, String>::new().with_bindings_from_file(&key_bindings_path)?,
)?.with(ExampleLinesSystem, "example_lines_system", &[])
.with_bundle(fly_control_bundle)?
.with_bundle(TransformBundle::new().with_dep(&["fly_movement"]))?
.with_bundle(RenderBundle::new(pipe, Some(config)))?;
let mut game = Application::new(resources, ExampleState, game_data)?;
game.run();
Ok(())
}