-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
155 lines (137 loc) · 4.45 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
#![allow(clippy::single_match)]
#![feature(or_patterns)]
extern crate parallax_scrolling_shader_demo;
use glam::{Quat, Vec3};
use hecs::World;
use parallax_scrolling_shader_demo::{
asset::SpriteAsset,
camera::{ActiveCamera, ParallaxCamera},
App, Game, KeyboardInput, Position, Rotation, Scale, Sprite,
};
use std::time::{Duration, Instant};
use winit::{
event::{ElementState, VirtualKeyCode},
event_loop::EventLoop,
};
#[derive(Clone, Copy)]
struct MoveSpeed(f32);
fn main() {
let event_loop = EventLoop::new();
let app = futures::executor::block_on(App::new("parallax-scrolling-shader-demo", &event_loop));
let mut parallax_demo = Game::new();
let sprite_assets = vec![
SpriteAsset::new("player", vec!["assets/player.png"]),
SpriteAsset::new("apple", vec!["assets/apple.png"]),
SpriteAsset::new("ashberry", vec!["assets/ashberry.png"]),
SpriteAsset::new("baobab", vec!["assets/baobab.png"]),
SpriteAsset::new("beech", vec!["assets/beech.png"]),
];
let movespeed = MoveSpeed(10.0);
let camera = (
ParallaxCamera::new(
Vec3::new(0.0, 3.0, 0.0),
Vec3::new(0.0, 0.0, 1.0),
1.0,
0.1,
500.0,
),
KeyboardInput(None),
ActiveCamera,
movespeed,
);
let player = (
Position(Vec3::new(0.0, 0.0, 20.0)),
Rotation(Quat::from_axis_angle(Vec3::new(0.0, 1.0, 0.0), 0.0)),
Scale(1),
KeyboardInput(None),
Sprite::new("player"),
movespeed,
);
let apple = (
Position(Vec3::new(-2.0, 0.0, 30.0)),
Rotation(Quat::from_axis_angle(Vec3::new(0.0, 1.0, 0.0), 0.0)),
Scale(1),
Sprite::new("apple"),
);
let ashberry = (
Position(Vec3::new(2.0, 0.0, 30.0)),
Rotation(Quat::from_axis_angle(Vec3::new(0.0, 1.0, 0.0), 0.0)),
Scale(1),
Sprite::new("ashberry"),
);
let baobab = (
Position(Vec3::new(3.0, 0.0, 55.0)),
Rotation(Quat::from_axis_angle(Vec3::new(0.0, 1.0, 0.0), 0.0)),
Scale(1),
Sprite::new("baobab"),
);
let beech = (
Position(Vec3::new(-3.5, 0.0, 95.0)),
Rotation(Quat::from_axis_angle(Vec3::new(0.0, 1.0, 0.0), 0.0)),
Scale(1),
Sprite::new("beech"),
);
parallax_demo.spawn_entity(player);
parallax_demo.spawn_entity(apple);
parallax_demo.spawn_entity(ashberry);
parallax_demo.spawn_entity(baobab);
parallax_demo.spawn_entity(beech);
parallax_demo.spawn_entity(camera);
parallax_demo.add_system(&move_player);
parallax_demo.add_system(&move_camera);
app.run(event_loop, parallax_demo, sprite_assets);
}
fn move_player(world: &World, dt: Duration, _instant: Instant) {
let mut q = world.query::<(&KeyboardInput, &mut Position, &MoveSpeed)>();
for (_, (key, pos, speed)) in q.iter() {
if let Some(input) = key.0 {
let dx = Vec3::new(speed.0 * dt.as_secs_f32(), 0.0, 0.0);
match input {
winit::event::KeyboardInput {
state: ElementState::Pressed,
virtual_keycode: Some(VirtualKeyCode::Left),
..
} => {
pos.0 -= dx;
}
winit::event::KeyboardInput {
state: ElementState::Pressed,
virtual_keycode: Some(VirtualKeyCode::Right),
..
} => {
pos.0 += dx;
}
_ => (),
}
}
}
}
fn move_camera(world: &World, dt: Duration, _instant: Instant) {
let mut q = world.query::<(
&ActiveCamera,
&mut ParallaxCamera,
&KeyboardInput,
&MoveSpeed,
)>();
let (_, (_, cam, key, speed)) = q.iter().next().expect("active camera is preset");
if let Some(input) = key.0 {
let dx = Vec3::new(speed.0 * dt.as_secs_f32(), 0.0, 0.0);
match input {
winit::event::KeyboardInput {
state: ElementState::Pressed,
virtual_keycode: Some(VirtualKeyCode::Left),
..
} => {
cam.eye -= dx;
}
winit::event::KeyboardInput {
state: ElementState::Pressed,
virtual_keycode: Some(VirtualKeyCode::Right),
..
} => {
cam.eye += dx;
}
_ => (),
}
}
}